root/webserver/example/Crossworks_taskinglib/main.c

Revision 13, 3.1 kB (checked in by phil, 15 years ago)

Crossworks Lib Example (multiple tasks)

Line 
1 #include <ctl_api.h>
2
3 #include "LPC23xx.h"
4
5 // Pin I/O LED Control Maskbit
6 #define  LED1       0x02000000                                                          // P3.25(0000 00x0 0000 0000 0000 0000 0000 0000)
7 #define  LED2       0x04000000                                                          // P3.26(0000 0x00 0000 0000 0000 0000 0000 0000)
8
9 #define  LED1_ON()  FIO3CLR = LED1                                              // LED1 Pin = 0 (ON LED)
10 #define  LED1_OFF() FIO3SET = LED1                                                      // LED1 Pin = 1 (OFF LED)
11 #define  LED2_ON()  FIO3CLR = LED2                                                      // LED2 Pin = 0 (ON LED)
12 #define  LED2_OFF() FIO3SET = LED2                                                      // LED2 Pin = 1 (OFF LED)
13
14 /* pototype  section */
15 void delay(unsigned long int);                                                          // Delay Time Function
16
17 int led_1_state = 0;
18 int led_2_state = 0;
19
20 void ConfigBlinky(void)
21 {
22   // Config Pin GPIO3[26:25]   
23   PINSEL7  &= 0xFFC3FFFF;                                                                       // P3[26:25] = GPIO Function(xxxx xxxx xx00 00xx xxxx xxxx xxxx xxxx)
24   PINMODE7 &= 0xFFC3FFFF;                                                                       // Enable Puul-Up on P3[26:25]
25    
26   FIO3DIR  |= LED1;                                                                     // Set GPIO-3[26:25] = Output(xxxx x11x xxxx xxxx xxxx xxxx xxxx xxxx)
27   FIO3DIR  |= LED2;
28   LED1_OFF();                                                                                           // Default LED Status
29   LED2_OFF();
30 }
31
32 /***********************/
33 /* Delay Time Function */
34 /*    1-4294967296     */
35 /***********************/
36 void delay(unsigned long int count1)
37 {
38   while(count1 > 0) {count1--;}                                                         // Loop Decrease Counter       
39 }
40
41 void task1(void *p)
42 {
43   volatile int i;
44   while (1)
45   {
46     for (i = 0; i < 10; i++)
47     {
48       i++;
49     }
50
51     if (led_1_state == 0)
52     {
53       LED1_ON();
54       led_1_state = 1;
55     }
56       else
57     {
58       LED1_OFF();
59       led_1_state = 0;
60     }
61     delay(5000000);     
62
63     ctl_task_reschedule();
64   // task code, on return task will be terminated
65   }
66 }
67
68 void task2(void *p)
69 {
70   volatile int k;
71   while (1)
72   {
73     for (k = 0; k < 10; k++)
74     {
75       k++;
76     }
77
78     if (led_2_state == 0)
79     {
80       LED2_ON();
81       led_2_state = 1;
82     }
83       else
84     {
85       LED2_OFF();
86       led_2_state = 0;
87     }
88     delay(5000000);             
89
90
91     ctl_task_reschedule();
92   }
93   // task code, on return task will be terminated
94 }
95
96
97
98 static CTL_TASK_t mainTask;
99
100 static CTL_TASK_t task1Task;
101 static unsigned task1Stack[64];
102
103 static CTL_TASK_t task2Task;
104 static unsigned task2Stack[64];
105
106
107 int
108 main(void)
109 {
110   // Turn myself into a task running at the highest priority.
111   ctl_task_init(&mainTask, 255, "main");
112
113   // Initialise the stack of task1.
114   memset(task1Stack, 0xba, sizeof(task1Stack));
115
116   // Initialise the stack of task2.
117   memset(task2Stack, 0xba, sizeof(task2Stack));
118  
119   ConfigBlinky(); /* Configure LEDs */
120
121   // Make another task ready to run.
122   ctl_task_run(&task1Task, 1, task1, 0, "task1", sizeof(task1Stack) / sizeof(unsigned), task1Stack, 0);
123
124   // Make another task ready to run.
125   ctl_task_run(&task2Task, 1, task2, 0, "task2", sizeof(task2Stack) / sizeof(unsigned), task2Stack, 0);
126
127
128   // Now all the tasks have been created go to lowest priority.
129   ctl_task_set_priority(&mainTask, 0);
130
131   // Main task, if activated because task1 is suspended, just
132   // enters low power mode and waits for task1 to run again
133   // (for example, because an interrupt wakes it).
134   for (;;)
135     {
136       // Go into low power mode
137       //sleep();
138     }
139 }
140
Note: See TracBrowser for help on using the browser.