ECE4760 - Laser Projector (ipb7, jcc384, pfc38)  1
Raster Laser Projection
Data Structures | Macros | Typedefs | Functions | Variables
pt_cornell_1_2_1.h File Reference
#include <plib.h>

Go to the source code of this file.

Data Structures

struct  pt
 
struct  pt_sem
 

Macros

#define __LC_ADDRLABELS_H__
 
#define LC_INIT(s)   s = NULL
 
#define LC_RESUME(s)
 
#define LC_CONCAT2(s1, s2)   s1##s2
 
#define LC_CONCAT(s1, s2)   LC_CONCAT2(s1, s2)
 
#define LC_SET(s)
 
#define LC_END(s)
 
#define PT_WAITING   0
 
#define PT_YIELDED   1
 
#define PT_EXITED   2
 
#define PT_ENDED   3
 
#define __PT_SEM_H__
 
#define PT_SEM_INIT(s, c)   (s)->count = c
 
#define PT_SEM_WAIT(pt, s)
 
#define PT_SEM_SIGNAL(pt, s)
 
#define PT_YIELD_TIME_msec(delay_time)
 
#define PT_GET_TIME()   (time_tick_millsec)
 
#define PT_RATE_INIT()   int pt_pri_count = 0;
 
#define PT_RATE_LOOP()   pt_pri_count = (pt_pri_count+1) & 0xf ;
 
#define PT_RATE_SCHEDULE(f, rate)
 
#define PT_DEBUG_VALUE(level, duration)
 
#define PT_SEM_SET(s)   (s)->count=1
 
#define PT_SEM_CLEAR(s)   (s)->count=0
 
#define PT_SEM_READ(s)   (s)->count
 
#define PT_SEM_ACCEPT(s)
 
#define PB_DIVISOR   (1 << OSCCONbits.PBDIV)
 
#define PB_FREQ   sys_clock/PB_DIVISOR
 
#define clrscr()   printf( "\x1b[2J")
 
#define home()   printf( "\x1b[H")
 
#define pcr()   printf( '\r')
 
#define crlf   putchar(0x0a); putchar(0x0d);
 
#define backspace   0x7f
 
#define max_chars   64
 
Initialization
#define PT_INIT(pt)
 
Declaration and definition
#define PT_THREAD(name_args)
 
#define PT_BEGIN(pt)
 
#define PT_END(pt)
 
Blocked wait
#define PT_WAIT_UNTIL(pt, condition)
 
#define PT_WAIT_WHILE(pt, cond)
 
Hierarchical protothreads
#define PT_WAIT_THREAD(pt, thread)
 
#define PT_SPAWN(pt, child, thread)
 
Exiting and restarting
#define PT_RESTART(pt)
 
#define PT_EXIT(pt)
 
Calling a protothread
#define PT_SCHEDULE(f)
 
Yielding from a protothread
#define PT_YIELD(pt)
 
#define PT_YIELD_UNTIL(pt, cond)
 Yield from the protothread until a condition occurs. More...
 

Typedefs

typedef void * lc_t
 

Functions

int PT_GetSerialBuffer (struct pt *pt)
 
int PutSerialBuffer (struct pt *pt)
 
int PT_DMA_PutSerialBuffer (struct pt *pt)
 
void __ISR (_TIMER_5_VECTOR, IPL2AUTO) Timer5Handler(void)
 
void PT_setup (void)
 

Variables

char PT_term_buffer [max_chars]
 
int num_char
 
char PT_send_buffer [max_chars]
 
int num_send_chars
 
int CVRCON_setup
 
volatile unsigned int time_tick_millsec
 

Detailed Description

Protothreads implementation.

Author
Adam Dunkels adam@.nosp@m.sics.nosp@m..se

Definition in file pt_cornell_1_2_1.h.

Macro Definition Documentation

§ __LC_ADDRLABELS_H__

#define __LC_ADDRLABELS_H__

Definition at line 173 of file pt_cornell_1_2_1.h.

§ __PT_SEM_H__

#define __PT_SEM_H__

Definition at line 475 of file pt_cornell_1_2_1.h.

§ backspace

#define backspace   0x7f

Definition at line 615 of file pt_cornell_1_2_1.h.

§ clrscr

#define clrscr ( )    printf( "\x1b[2J")

Definition at line 611 of file pt_cornell_1_2_1.h.

§ crlf

#define crlf   putchar(0x0a); putchar(0x0d);

Definition at line 614 of file pt_cornell_1_2_1.h.

§ home

#define home ( )    printf( "\x1b[H")

Definition at line 612 of file pt_cornell_1_2_1.h.

§ LC_CONCAT

#define LC_CONCAT (   s1,
  s2 
)    LC_CONCAT2(s1, s2)

Definition at line 188 of file pt_cornell_1_2_1.h.

§ LC_CONCAT2

#define LC_CONCAT2 (   s1,
  s2 
)    s1##s2

Definition at line 187 of file pt_cornell_1_2_1.h.

§ LC_END

#define LC_END (   s)

Definition at line 196 of file pt_cornell_1_2_1.h.

§ LC_INIT

#define LC_INIT (   s)    s = NULL

Definition at line 178 of file pt_cornell_1_2_1.h.

§ LC_RESUME

#define LC_RESUME (   s)
Value:
do { \
if(s != NULL) { \
goto *s; \
} \
} while(0)

Definition at line 180 of file pt_cornell_1_2_1.h.

§ LC_SET

#define LC_SET (   s)
Value:
do { \
LC_CONCAT(LC_LABEL, __LINE__): \
(s) = &&LC_CONCAT(LC_LABEL, __LINE__); \
} while(0)
#define LC_CONCAT(s1, s2)

Definition at line 190 of file pt_cornell_1_2_1.h.

§ max_chars

#define max_chars   64

Definition at line 616 of file pt_cornell_1_2_1.h.

§ PB_DIVISOR

#define PB_DIVISOR   (1 << OSCCONbits.PBDIV)

Definition at line 609 of file pt_cornell_1_2_1.h.

§ PB_FREQ

#define PB_FREQ   sys_clock/PB_DIVISOR

Definition at line 610 of file pt_cornell_1_2_1.h.

§ pcr

#define pcr ( )    printf( '\r')

Definition at line 613 of file pt_cornell_1_2_1.h.

§ PT_BEGIN

#define PT_BEGIN (   pt)

Declare the start of a protothread inside the C function implementing the protothread.

This macro is used to declare the starting point of a protothread. It should be placed at the start of the function in which the protothread runs. All C statements above the PT_BEGIN() invokation will be executed each time the protothread is scheduled.

Parameters
ptA pointer to the protothread control structure.

Definition at line 265 of file pt_cornell_1_2_1.h.

§ PT_DEBUG_VALUE

#define PT_DEBUG_VALUE (   level,
  duration 
)
Value:
do { static int i ; \
CVRCON = CVRCON_setup | (level & 0xf); \
if (duration>0){ \
for (i=0; i<duration*7; i++){};\
CVRCON = CVRCON_setup; \
} \
} while(0);
int CVRCON_setup

Definition at line 586 of file pt_cornell_1_2_1.h.

§ PT_END

#define PT_END (   pt)

Declare the end of a protothread.

This macro is used for declaring that a protothread ends. It must always be used together with a matching PT_BEGIN() macro.

Parameters
ptA pointer to the protothread control structure.

Definition at line 277 of file pt_cornell_1_2_1.h.

§ PT_ENDED

#define PT_ENDED   3

Definition at line 211 of file pt_cornell_1_2_1.h.

§ PT_EXIT

#define PT_EXIT (   pt)

Exit the protothread.

This macro causes the protothread to exit. If the protothread was spawned by another protothread, the parent protothread will become unblocked and can continue to run.

Parameters
ptA pointer to the protothread control structure.

Definition at line 396 of file pt_cornell_1_2_1.h.

§ PT_EXITED

#define PT_EXITED   2

Definition at line 210 of file pt_cornell_1_2_1.h.

§ PT_GET_TIME

#define PT_GET_TIME ( )    (time_tick_millsec)

Definition at line 557 of file pt_cornell_1_2_1.h.

§ PT_INIT

#define PT_INIT (   pt)

Initialize a protothread.

Initializes a protothread. Initialization must be done prior to starting to execute the protothread.

Parameters
ptA pointer to the protothread control structure.
See also
PT_SPAWN()

Definition at line 230 of file pt_cornell_1_2_1.h.

§ PT_RATE_INIT

#define PT_RATE_INIT ( )    int pt_pri_count = 0;

Definition at line 562 of file pt_cornell_1_2_1.h.

§ PT_RATE_LOOP

#define PT_RATE_LOOP ( )    pt_pri_count = (pt_pri_count+1) & 0xf ;

Definition at line 565 of file pt_cornell_1_2_1.h.

§ PT_RATE_SCHEDULE

#define PT_RATE_SCHEDULE (   f,
  rate 
)
Value:
if((rate==0) | \
(rate==1 && ((pt_pri_count & 0b1)==0) ) | \
(rate==2 && ((pt_pri_count & 0b11)==0) ) | \
(rate==3 && ((pt_pri_count & 0b111)==0)) | \
(rate==4 && ((pt_pri_count & 0b1111)==0))) \
PT_SCHEDULE(f);

Definition at line 573 of file pt_cornell_1_2_1.h.

§ PT_RESTART

#define PT_RESTART (   pt)

Restart the protothread.

This macro will block and cause the running protothread to restart its execution at the place of the PT_BEGIN() call.

Parameters
ptA pointer to the protothread control structure.

Definition at line 379 of file pt_cornell_1_2_1.h.

§ PT_SCHEDULE

#define PT_SCHEDULE (   f)

Schedule a protothread.

This function shedules a protothread. The return value of the function is non-zero if the protothread is running or zero if the protothread has exited.

Parameters
fThe call to the C function implementing the protothread to be scheduled

Definition at line 421 of file pt_cornell_1_2_1.h.

§ PT_SEM_ACCEPT

#define PT_SEM_ACCEPT (   s)
Value:
s->count; \
if (s->count) s->count-- ; \

Definition at line 599 of file pt_cornell_1_2_1.h.

§ PT_SEM_CLEAR

#define PT_SEM_CLEAR (   s)    (s)->count=0

Definition at line 597 of file pt_cornell_1_2_1.h.

§ PT_SEM_INIT

#define PT_SEM_INIT (   s,
 
)    (s)->count = c

Initialize a semaphore

This macro initializes a semaphore with a value for the counter. Internally, the semaphores use an "unsigned int" to represent the counter, and therefore the "count" argument should be within range of an unsigned int.

Parameters
s(struct pt_sem *) A pointer to the pt_sem struct representing the semaphore
c(unsigned int) The initial count of the semaphore. initializer

Definition at line 497 of file pt_cornell_1_2_1.h.

§ PT_SEM_READ

#define PT_SEM_READ (   s)    (s)->count

Definition at line 598 of file pt_cornell_1_2_1.h.

§ PT_SEM_SET

#define PT_SEM_SET (   s)    (s)->count=1

Definition at line 596 of file pt_cornell_1_2_1.h.

§ PT_SEM_SIGNAL

#define PT_SEM_SIGNAL (   pt,
 
)

Signal a semaphore

This macro carries out the "signal" operation on the semaphore. The signal operation increments the counter inside the semaphore, which eventually will cause waiting protothreads to continue executing.

Parameters
pt(struct pt *) A pointer to the protothread (struct pt) in which the operation is executed.
s(struct pt_sem *) A pointer to the pt_sem struct representing the semaphore

Definition at line 536 of file pt_cornell_1_2_1.h.

§ PT_SEM_WAIT

#define PT_SEM_WAIT (   pt,
 
)

Wait for a semaphore

This macro carries out the "wait" operation on the semaphore. The wait operation causes the protothread to block while the counter is zero. When the counter reaches a value larger than zero, the protothread will continue.

Parameters
pt(struct pt *) A pointer to the protothread (struct pt) in which the operation is executed.
s(struct pt_sem *) A pointer to the pt_sem struct representing the semaphore

Definition at line 515 of file pt_cornell_1_2_1.h.

§ PT_SPAWN

#define PT_SPAWN (   pt,
  child,
  thread 
)

Spawn a child protothread and wait until it exits.

This macro spawns a child protothread and waits until it exits. The macro can only be used within a protothread.

Parameters
ptA pointer to the protothread control structure.
childA pointer to the child protothread's control structure.
threadThe child protothread with arguments

Definition at line 356 of file pt_cornell_1_2_1.h.

§ PT_THREAD

#define PT_THREAD (   name_args)

Declaration of a protothread.

This macro is used to declare a protothread. All protothreads must be declared with this macro.

Parameters
name_argsThe name and arguments of the C function implementing the protothread.

Definition at line 250 of file pt_cornell_1_2_1.h.

§ PT_WAIT_THREAD

#define PT_WAIT_THREAD (   pt,
  thread 
)

Block and wait until a child protothread completes.

This macro schedules a child protothread. The current protothread will block until the child protothread completes.

Note
The child protothread must be manually initialized with the PT_INIT() function before this function is used.
Parameters
ptA pointer to the protothread control structure.
threadThe child protothread with arguments
See also
PT_SPAWN()

Definition at line 342 of file pt_cornell_1_2_1.h.

§ PT_WAIT_UNTIL

#define PT_WAIT_UNTIL (   pt,
  condition 
)

Block and wait until condition is true.

This macro blocks the protothread until the specified condition is true.

Parameters
ptA pointer to the protothread control structure.
conditionThe condition.

Definition at line 298 of file pt_cornell_1_2_1.h.

§ PT_WAIT_WHILE

#define PT_WAIT_WHILE (   pt,
  cond 
)

Block and wait while condition is true.

This function blocks and waits while condition is true. See PT_WAIT_UNTIL().

Parameters
ptA pointer to the protothread control structure.
condThe condition.

Definition at line 317 of file pt_cornell_1_2_1.h.

§ PT_WAITING

#define PT_WAITING   0

Definition at line 208 of file pt_cornell_1_2_1.h.

§ PT_YIELD

#define PT_YIELD (   pt)

Yield from the current protothread.

This function will yield the protothread, thereby allowing other processing to take place in the system.

Parameters
ptA pointer to the protothread control structure.

Definition at line 441 of file pt_cornell_1_2_1.h.

§ PT_YIELD_TIME_msec

#define PT_YIELD_TIME_msec (   delay_time)
Value:
do { static unsigned int time_thread ;\
time_thread = time_tick_millsec + (unsigned int)delay_time ; \
PT_YIELD_UNTIL(pt, (time_tick_millsec >= time_thread)); \
} while(0);
volatile unsigned int time_tick_millsec

Definition at line 550 of file pt_cornell_1_2_1.h.

§ PT_YIELD_UNTIL

#define PT_YIELD_UNTIL (   pt,
  cond 
)

Yield from the protothread until a condition occurs.

Parameters
ptA pointer to the protothread control structure.
condThe condition.
        This function will yield the protothread, until the
        specified condition evaluates to true.

Definition at line 461 of file pt_cornell_1_2_1.h.

§ PT_YIELDED

#define PT_YIELDED   1

Definition at line 209 of file pt_cornell_1_2_1.h.

Typedef Documentation

§ lc_t

typedef void* lc_t

Definition at line 176 of file pt_cornell_1_2_1.h.

Function Documentation

§ __ISR()

void __ISR ( _TIMER_5_VECTOR  ,
IPL2AUTO   
)

Definition at line 729 of file pt_cornell_1_2_1.h.

730 {
731  // clear the interrupt flag
732  mT5ClearIntFlag();
733  //count milliseconds
735 }
volatile unsigned int time_tick_millsec

§ PT_DMA_PutSerialBuffer()

int PT_DMA_PutSerialBuffer ( struct pt pt)

Definition at line 695 of file pt_cornell_1_2_1.h.

696 {
697  PT_BEGIN(pt);
698  //mPORTBSetBits(BIT_0);
699  // check for null string
700  if (PT_send_buffer[0]==0)PT_EXIT(pt);
701  // sent the first character
702  PT_YIELD_UNTIL(pt, UARTTransmitterIsReady(UART2));
703  UARTSendDataByte(UART2, PT_send_buffer[0]);
704  //DmaChnStartTxfer(DMA_CHANNEL1, DMA_WAIT_NOT, 0);
705  // start the DMA
706  DmaChnEnable(DMA_CHANNEL1);
707  // wait for DMA done
708  //mPORTBClearBits(BIT_0);
709  PT_YIELD_UNTIL(pt, DmaChnGetEvFlags(DMA_CHANNEL1) & DMA_EV_BLOCK_DONE);
710  //wait until the transmit buffer is empty
711  PT_YIELD_UNTIL(pt, U2STA&0x100);
712 
713  // kill this output thread, to allow spawning thread to execute
714  PT_EXIT(pt);
715  // and indicate the end of the thread
716  PT_END(pt);
717 }
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
#define PT_EXIT(pt)
#define PT_END(pt)
#define PT_BEGIN(pt)
char PT_send_buffer[max_chars]

§ PT_GetSerialBuffer()

int PT_GetSerialBuffer ( struct pt pt)

Definition at line 622 of file pt_cornell_1_2_1.h.

623 {
624  static char character;
625  // mark the beginnning of the input thread
626  PT_BEGIN(pt);
627 
628  num_char = 0;
629  //memset(term_buffer, 0, max_chars);
630 
631  while(num_char < max_chars)
632  {
633  // get the character
634  // yield until there is a valid character so that other
635  // threads can execute
636  PT_YIELD_UNTIL(pt, UARTReceivedDataIsAvailable(UART2));
637  // while(!UARTReceivedDataIsAvailable(UART2)){};
638  character = UARTGetDataByte(UART2);
639  PT_YIELD_UNTIL(pt, UARTTransmitterIsReady(UART2));
640  UARTSendDataByte(UART2, character);
641 
642  // unomment to check backspace character!!!
643  //printf("--%x--",character );
644 
645  // end line
646  if(character == '\r'){
647  PT_term_buffer[num_char] = 0; // zero terminate the string
648  //crlf; // send a new line
649  PT_YIELD_UNTIL(pt, UARTTransmitterIsReady(UART2));
650  UARTSendDataByte(UART2, '\n');
651  break;
652  }
653  // backspace
654  else if (character == backspace){
655  PT_YIELD_UNTIL(pt, UARTTransmitterIsReady(UART2));
656  UARTSendDataByte(UART2, ' ');
657  PT_YIELD_UNTIL(pt, UARTTransmitterIsReady(UART2));
658  UARTSendDataByte(UART2, backspace);
659  num_char--;
660  // check for buffer underflow
661  if (num_char<0) {num_char = 0 ;}
662  }
663  else {PT_term_buffer[num_char++] = character ;}
664  //if (character == backspace)
665 
666  } //end while(num_char < max_size)
667 
668  // kill this input thread, to allow spawning thread to execute
669  PT_EXIT(pt);
670  // and indicate the end of the thread
671  PT_END(pt);
672 }
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
#define backspace
#define PT_EXIT(pt)
char PT_term_buffer[max_chars]
#define PT_END(pt)
#define max_chars
#define PT_BEGIN(pt)
int num_char

§ PT_setup()

void PT_setup ( void  )

Definition at line 737 of file pt_cornell_1_2_1.h.

738 {
739  // Configure the device for maximum performance but do not change the PBDIV
740  // Given the options, this function will change the flash wait states, RAM
741  // wait state and enable prefetch cache but will not change the PBDIV.
742  // The PBDIV value is already set via the pragma FPBDIV option above..
743  SYSTEMConfig(sys_clock, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
744 
745  ANSELA =0; //make sure analog is cleared
746  ANSELB =0;
747 
748 #ifdef use_uart_serial
749  // === init the uart2 ===================
750  PPSInput (2, U2RX, RPB11); //Assign U2RX to pin RPB11 -- Physical pin 22 on 28 PDIP
751  PPSOutput(4, RPB10, U2TX); //Assign U2TX to pin RPB10 -- Physical pin 21 on 28 PDIP
752  UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
753  UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
754  UARTSetDataRate(UART2, pb_clock, BAUDRATE);
755  UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
756  printf("\n\r..protothreads start..\n\r");
757  // === set up DMA for UART output =========
758  // configure the channel and enable end-on-match
759  DmaChnOpen(DMA_CHANNEL1, DMA_CHN_PRI2, DMA_OPEN_MATCH);
760  // trigger a byte everytime the UART is empty
761  DmaChnSetEventControl(DMA_CHANNEL1, DMA_EV_START_IRQ_EN|DMA_EV_MATCH_EN|DMA_EV_START_IRQ(_UART2_TX_IRQ));
762  // source and destination
763  DmaChnSetTxfer(DMA_CHANNEL1, PT_send_buffer+1, (void*)&U2TXREG, max_chars, 1, 1);
764  // signal when done
765  DmaChnSetEvEnableFlags(DMA_CHANNEL1, DMA_EV_BLOCK_DONE);
766  // set null as ending character (of a string)
767  DmaChnSetMatchPattern(DMA_CHANNEL1, 0x00);
768 #endif //#ifdef use_uart_serial
769 
770  // ===Set up timer5 ======================
771  // timer 5: on, interrupts, internal clock,
772  // set up to count millsec
773  OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_1 , pb_clock/1000);
774  // set up the timer interrupt with a priority of 2
775  ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_2);
776  mT5ClearIntFlag(); // and clear the interrupt flag
777  // zero the system time tick
778  time_tick_millsec = 0;
779 
780  //=== Set up VREF as a debugger output =======
781  #ifdef use_vref_debug
782  // set up the Vref pin and use as a DAC
783  // enable module| eanble output | use low range output | use internal reference | desired step
784  CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
785  // And read back setup from CVRCON for speed later
786  // 0x8060 is enabled with output enabled, Vdd ref, and 0-0.6(Vdd) range
787  CVRCON_setup = CVRCON; //CVRCON = 0x8060 from Tahmid http://tahmidmc.blogspot.com/
788 
789 #endif //#ifdef use_vref_debug
790 
791 }
#define sys_clock
Definition: config.h:35
int CVRCON_setup
volatile unsigned int time_tick_millsec
#define pb_clock
Definition: config.h:38
#define max_chars
char PT_send_buffer[max_chars]

§ PutSerialBuffer()

int PutSerialBuffer ( struct pt pt)

Definition at line 678 of file pt_cornell_1_2_1.h.

679 {
680  PT_BEGIN(pt);
681  num_send_chars = 0;
682  while (PT_send_buffer[num_send_chars] != 0){
683  PT_YIELD_UNTIL(pt, UARTTransmitterIsReady(UART2));
684  UARTSendDataByte(UART2, PT_send_buffer[num_send_chars]);
685  num_send_chars++;
686  }
687  // kill this output thread, to allow spawning thread to execute
688  PT_EXIT(pt);
689  // and indicate the end of the thread
690  PT_END(pt);
691 }
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
#define PT_EXIT(pt)
#define PT_END(pt)
int num_send_chars
#define PT_BEGIN(pt)
char PT_send_buffer[max_chars]

Variable Documentation

§ CVRCON_setup

int CVRCON_setup

Definition at line 722 of file pt_cornell_1_2_1.h.

§ num_char

int num_char

Definition at line 621 of file pt_cornell_1_2_1.h.

§ num_send_chars

int num_send_chars

Definition at line 677 of file pt_cornell_1_2_1.h.

§ PT_send_buffer

char PT_send_buffer[max_chars]

Definition at line 676 of file pt_cornell_1_2_1.h.

§ PT_term_buffer

char PT_term_buffer[max_chars]

Definition at line 620 of file pt_cornell_1_2_1.h.

§ time_tick_millsec

volatile unsigned int time_tick_millsec

Definition at line 725 of file pt_cornell_1_2_1.h.