/* primitive multitasking example for NXP's LPC1788 using IAR IDE it contains only two tasks. Both tasks are only simple LED flashes in infinite loops. tasks are preemted always from one to the other it is done by SysTick interrupt handler which switches the context 1/ interrupt is set up 2/ stack for task 2 created 3/ main enters task 1 so task 1 uses stack originally used by main program 4/the tasks are regulary interupted and switched to the other one */ #include #define LED1 (1<<13) #define LED2 (1<<18) #define STACK_SIZE 50 unsigned int *task1SP; unsigned int *task2SP; unsigned int current_task; //stack for task 2 unsigned int stack2 [STACK_SIZE]; /*change LED state at given IO pin*/ void flash_LED(unsigned int led){ //check pin state and change it if(FIO1PIN & led){ FIO1CLR = led; } else{ FIO1SET = led; } } /*demo task 1 flash LED1 */ void task1(){ while(1){ //wait for(int i=0;i<1000000;i++){ } flash_LED(LED1); } } /*demo task 2 flash LED2 */ void task2(){ while(1){ //wait for(int i=0;i<1000000;i++){ } flash_LED(LED2); } } /*basic hw setup */ void hw_init(){ // diodes at P1.13 a 15 as output FIO1DIR = (1<<13|1<<18); //SYSTICK set up //enable + interupt + cpu freq STCTRL = (0x7); // value to count-down for systick reloadREG = (fcpu * T) - 1 STRELOAD = 0x1D31D;//0xFFFFFE 0xB67753 0x123F22 0x1D31D //max 1s 100ms 10ms } /*this function sets up a stack for task2. This is the most difficult part. contents of the stack from the top adress PSR - manual copy from stack of task 1 ;) (Program Status Register) PC - adress of task2 (Program Counter) LR - to be poped into LR (Link Register) R12 - to be poped into (R3 to R0) - to be poped into EXC_RETURN value - so processor knows that it returns from an interrupt 0x0 - from top to this point values are pushed autmatically when entering an interrupt and poped back when leaving interrupt (R4 to R11) - values to be pushed after context switch */ void create_task2(){ //init whole array to zeros for(int i=0;i