1 | #include "r2ca.h"
|
---|
2 | #include "r2ca_lib.h"
|
---|
3 |
|
---|
4 | extern "C" {
|
---|
5 | extern void yield(void);
|
---|
6 | extern void r2ca_ena_int(uint_t intno);
|
---|
7 | extern void r2ca_dis_int(uint_t intno);
|
---|
8 | }
|
---|
9 |
|
---|
10 | #ifdef R2CA_ENABLE_PROFILING
|
---|
11 | volatile uint32_t r2ca_idle_result;
|
---|
12 | volatile uint32_t r2ca_isr_result;
|
---|
13 | volatile uint32_t r2ca_dispatch_result;
|
---|
14 | volatile uint32_t r2ca_timer_isr_result;
|
---|
15 | volatile uint32_t r2ca_usb_isr_result;
|
---|
16 | volatile uint32_t r2ca_sercom0_isr_result;
|
---|
17 | volatile uint32_t r2ca_sercom2_isr_result;
|
---|
18 | volatile uint32_t r2ca_sercom3_isr_result;
|
---|
19 | volatile uint32_t r2ca_sercom4_isr_result;
|
---|
20 | volatile uint32_t r2ca_sercom5_isr_result;
|
---|
21 | volatile uint32_t r2ca_eic_isr_result;
|
---|
22 | volatile uint32_t r2ca_tc5_isr_result;
|
---|
23 | volatile uint32_t r2ca_rtc_isr_result;
|
---|
24 |
|
---|
25 | static volatile uint32_t r2ca_idle_cnt;
|
---|
26 | static volatile uint32_t r2ca_isr_cnt;
|
---|
27 | static volatile uint32_t r2ca_dispatch_cnt;
|
---|
28 | static volatile uint32_t r2ca_timer_isr_cnt;
|
---|
29 | static volatile uint32_t r2ca_usb_isr_cnt;
|
---|
30 | static volatile uint32_t r2ca_sercom0_isr_cnt;
|
---|
31 | static volatile uint32_t r2ca_sercom2_isr_cnt;
|
---|
32 | static volatile uint32_t r2ca_sercom3_isr_cnt;
|
---|
33 | static volatile uint32_t r2ca_sercom4_isr_cnt;
|
---|
34 | static volatile uint32_t r2ca_sercom5_isr_cnt;
|
---|
35 | static volatile uint32_t r2ca_eic_isr_cnt;
|
---|
36 | static volatile uint32_t r2ca_tc5_isr_cnt;
|
---|
37 | static volatile uint32_t r2ca_rtc_isr_cnt;
|
---|
38 | static volatile uint32_t r2ca_profiling_cyccnt;
|
---|
39 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
40 |
|
---|
41 | Inline void
|
---|
42 | r2ca_di(void){
|
---|
43 | Asm("cpsid f":::"memory");
|
---|
44 | }
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * FAULTMASKÌNA
|
---|
48 | */
|
---|
49 | Inline void
|
---|
50 | r2ca_ei(void){
|
---|
51 | Asm("cpsie f":::"memory");
|
---|
52 | }
|
---|
53 |
|
---|
54 | void
|
---|
55 | r2ca_init(intptr_t exinf)
|
---|
56 | {
|
---|
57 | init(); //wiring.c
|
---|
58 | }
|
---|
59 |
|
---|
60 | void
|
---|
61 | r2ca_ena_int(uint_t intno) {
|
---|
62 | if (ena_int(intno + 16) != E_OK) {
|
---|
63 | while(1); //ToDo
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | void
|
---|
68 | r2ca_dis_int(uint_t intno) {
|
---|
69 | if (dis_int(intno + 16) != E_OK) {
|
---|
70 | while(1); //ToDo
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | extern const DeviceVectors exception_table;
|
---|
75 |
|
---|
76 | static uint32_t time_slice = 0;
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Cyclic Handler
|
---|
80 | */
|
---|
81 | void
|
---|
82 | r2ca_CycHandler(intptr_t exinf)
|
---|
83 | {
|
---|
84 | int i;
|
---|
85 | if (++time_slice == R2CA_RR_SCHEDULE_CYCLE) {
|
---|
86 | time_slice = 0;
|
---|
87 | for(i = 1; i < 16; i++){
|
---|
88 | if (R2CA_RR_SCHEDULE_PRI & (1 << i)) {
|
---|
89 | irot_rdq(i);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | #ifdef R2CA_ENABLE_PROFILING
|
---|
94 | r2ca_di();
|
---|
95 | r2ca_isr_cnt++;
|
---|
96 | r2ca_timer_isr_cnt++;
|
---|
97 | r2ca_profiling_cyccnt++;
|
---|
98 | if(r2ca_profiling_cyccnt == R2CA_PROFILING_CYC_MS){
|
---|
99 | r2ca_idle_result= r2ca_idle_cnt;
|
---|
100 | r2ca_isr_result= r2ca_isr_cnt;
|
---|
101 | r2ca_dispatch_result= r2ca_dispatch_cnt;
|
---|
102 | r2ca_timer_isr_result= r2ca_timer_isr_cnt;
|
---|
103 | r2ca_usb_isr_result= r2ca_usb_isr_cnt;
|
---|
104 | r2ca_sercom0_isr_result= r2ca_sercom0_isr_cnt;
|
---|
105 | r2ca_sercom2_isr_result= r2ca_sercom2_isr_cnt;
|
---|
106 | r2ca_sercom3_isr_result= r2ca_sercom3_isr_cnt;
|
---|
107 | r2ca_sercom4_isr_result= r2ca_sercom4_isr_cnt;
|
---|
108 | r2ca_sercom5_isr_result= r2ca_sercom5_isr_cnt;
|
---|
109 | r2ca_eic_isr_result= r2ca_eic_isr_cnt;
|
---|
110 | r2ca_tc5_isr_result= r2ca_tc5_isr_cnt;
|
---|
111 | r2ca_rtc_isr_result= r2ca_rtc_isr_cnt;
|
---|
112 | r2ca_idle_cnt = 0;
|
---|
113 | r2ca_isr_cnt = 0;
|
---|
114 | r2ca_dispatch_cnt = 0;
|
---|
115 | r2ca_timer_isr_cnt = 0;
|
---|
116 | r2ca_usb_isr_cnt = 0;
|
---|
117 | r2ca_sercom0_isr_cnt = 0;
|
---|
118 | r2ca_sercom2_isr_cnt = 0;
|
---|
119 | r2ca_sercom3_isr_cnt = 0;
|
---|
120 | r2ca_sercom4_isr_cnt = 0;
|
---|
121 | r2ca_sercom5_isr_cnt = 0;
|
---|
122 | r2ca_eic_isr_cnt = 0;
|
---|
123 | r2ca_tc5_isr_cnt = 0;
|
---|
124 | r2ca_rtc_isr_cnt = 0;
|
---|
125 | r2ca_profiling_cyccnt = 0;
|
---|
126 | }
|
---|
127 | r2ca_ei();
|
---|
128 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
129 | ((void(*)(void))(exception_table.pfnSysTick_Handler))();
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Interrupt Handler
|
---|
134 | */
|
---|
135 | void
|
---|
136 | r2ca_USB_Handler(void)
|
---|
137 | {
|
---|
138 | #ifdef R2CA_ENABLE_PROFILING
|
---|
139 | r2ca_di();
|
---|
140 | r2ca_usb_isr_cnt++;
|
---|
141 | r2ca_isr_cnt++;
|
---|
142 | r2ca_ei();
|
---|
143 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
144 | ((void(*)(void))(exception_table.pfnUSB_Handler))();
|
---|
145 | }
|
---|
146 |
|
---|
147 | void
|
---|
148 | r2ca_SERCOM0_Handler(void)
|
---|
149 | {
|
---|
150 | #ifdef R2CA_ENABLE_PROFILING
|
---|
151 | r2ca_di();
|
---|
152 | r2ca_sercom0_isr_cnt++;
|
---|
153 | r2ca_isr_cnt++;
|
---|
154 | r2ca_ei();
|
---|
155 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
156 | ((void(*)(void))(exception_table.pfnSERCOM0_Handler))();
|
---|
157 | }
|
---|
158 |
|
---|
159 | #ifdef R2CA_USE_SERIAL3
|
---|
160 | #define PIN_SERIAL3_RX 49 /* D5 */
|
---|
161 | #define PIN_SERIAL3_TX 48 /* D4 */
|
---|
162 |
|
---|
163 | Uart Serial3(&sercom2, PIN_SERIAL3_RX, PIN_SERIAL3_TX);
|
---|
164 |
|
---|
165 | void
|
---|
166 | r2ca_SERCOM2_Handler(void)
|
---|
167 | {
|
---|
168 | #ifdef R2CA_ENABLE_PROFILING
|
---|
169 | r2ca_di();
|
---|
170 | r2ca_sercom2_isr_cnt++;
|
---|
171 | r2ca_isr_cnt++;
|
---|
172 | r2ca_ei();
|
---|
173 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
174 | Serial3.IrqHandler();
|
---|
175 | }
|
---|
176 | #endif /* R2CA_USE_SERIAL3 */
|
---|
177 |
|
---|
178 | void
|
---|
179 | r2ca_SERCOM3_Handler(void)
|
---|
180 | {
|
---|
181 | #ifdef R2CA_ENABLE_PROFILING
|
---|
182 | r2ca_di();
|
---|
183 | r2ca_sercom3_isr_cnt++;
|
---|
184 | r2ca_isr_cnt++;
|
---|
185 | r2ca_ei();
|
---|
186 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
187 | ((void(*)(void))(exception_table.pfnSERCOM3_Handler))();
|
---|
188 | }
|
---|
189 |
|
---|
190 | void
|
---|
191 | r2ca_SERCOM4_Handler(void)
|
---|
192 | {
|
---|
193 | #ifdef R2CA_ENABLE_PROFILING
|
---|
194 | r2ca_di();
|
---|
195 | r2ca_sercom4_isr_cnt++;
|
---|
196 | r2ca_isr_cnt++;
|
---|
197 | r2ca_ei();
|
---|
198 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
199 | ((void(*)(void))(exception_table.pfnSERCOM4_Handler))();
|
---|
200 | }
|
---|
201 |
|
---|
202 | #ifdef TOPPERS_USE_ARDUINO_SERIAL
|
---|
203 | void
|
---|
204 | r2ca_SERCOM5_Handler(void)
|
---|
205 | {
|
---|
206 | #ifdef R2CA_ENABLE_PROFILING
|
---|
207 | r2ca_di();
|
---|
208 | r2ca_sercom5_isr_cnt++;
|
---|
209 | r2ca_isr_cnt++;
|
---|
210 | r2ca_ei();
|
---|
211 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
212 | ((void(*)(void))(exception_table.pfnSERCOM5_Handler))();
|
---|
213 | }
|
---|
214 | #endif /* TOPPERS_USE_ARDUINO_SERIAL */
|
---|
215 |
|
---|
216 | void
|
---|
217 | r2ca_EIC_Handler(void)
|
---|
218 | {
|
---|
219 | #ifdef R2CA_ENABLE_PROFILING
|
---|
220 | r2ca_di();
|
---|
221 | r2ca_eic_isr_cnt++;
|
---|
222 | r2ca_isr_cnt++;
|
---|
223 | r2ca_ei();
|
---|
224 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
225 | ((void(*)(void))(exception_table.pfnEIC_Handler))();
|
---|
226 | }
|
---|
227 |
|
---|
228 | void
|
---|
229 | r2ca_TC5_Handler(void)
|
---|
230 | {
|
---|
231 | #ifdef R2CA_ENABLE_PROFILING
|
---|
232 | r2ca_di();
|
---|
233 | r2ca_tc5_isr_cnt++;
|
---|
234 | r2ca_isr_cnt++;
|
---|
235 | r2ca_ei();
|
---|
236 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
237 | ((void(*)(void))(exception_table.pfnTC5_Handler))();
|
---|
238 | }
|
---|
239 |
|
---|
240 | void
|
---|
241 | r2ca_RTC_Handler(void)
|
---|
242 | {
|
---|
243 | #ifdef R2CA_ENABLE_PROFILING
|
---|
244 | r2ca_di();
|
---|
245 | r2ca_rtc_isr_cnt++;
|
---|
246 | r2ca_isr_cnt++;
|
---|
247 | r2ca_ei();
|
---|
248 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
249 | ((void(*)(void))(exception_table.pfnRTC_Handler))();
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * For delay
|
---|
254 | */
|
---|
255 | void
|
---|
256 | yield(void){
|
---|
257 | time_slice = 0;
|
---|
258 | #ifdef R2CA_ENABLE_PROFILING
|
---|
259 | r2ca_di();
|
---|
260 | r2ca_dispatch_cnt++;
|
---|
261 | r2ca_ei();
|
---|
262 | #endif /* R2CA_ENABLE_PROFILING */
|
---|
263 | dly_tsk(0);
|
---|
264 | }
|
---|
265 |
|
---|
266 | extern void setup(void);
|
---|
267 | extern void loop(void);
|
---|
268 |
|
---|
269 |
|
---|
270 | const ID task_id[] = {
|
---|
271 | #if R2CA_NUM_TASK > 0
|
---|
272 | R2CA_TASK1,
|
---|
273 | #endif /* R2CA_NUM_TASK > 0 */
|
---|
274 | #if R2CA_NUM_TASK > 1
|
---|
275 | R2CA_TASK2,
|
---|
276 | #endif /* R2CA_NUM_TASK > 1 */
|
---|
277 | #if R2CA_NUM_TASK > 2
|
---|
278 | R2CA_TASK3,
|
---|
279 | #endif /* R2CA_NUM_TASK > 2 */
|
---|
280 | #if R2CA_NUM_TASK > 3
|
---|
281 | R2CA_TASK4,
|
---|
282 | #endif /* R2CA_NUM_TASK > 3 */
|
---|
283 | #if R2CA_NUM_TASK > 4
|
---|
284 | R2CA_TASK5,
|
---|
285 | #endif /* R2CA_NUM_TASK > 4 */
|
---|
286 | };
|
---|
287 |
|
---|
288 | void
|
---|
289 | r2ca_maintask(intptr_t exinf)
|
---|
290 | {
|
---|
291 | int i;
|
---|
292 |
|
---|
293 | syslog(LOG_NOTICE, "Arduino Main Task start!");
|
---|
294 |
|
---|
295 | #ifdef USBCON
|
---|
296 | USBDevice.init();
|
---|
297 | USBDevice.attach();
|
---|
298 | #endif /* USBCON */
|
---|
299 |
|
---|
300 | analogReference(AR_DEFAULT);
|
---|
301 |
|
---|
302 | setup();
|
---|
303 |
|
---|
304 | for(i = 0; i < R2CA_NUM_TASK; i++) {
|
---|
305 | act_tsk(task_id[i]);
|
---|
306 | }
|
---|
307 |
|
---|
308 | while(1){
|
---|
309 | loop();
|
---|
310 | if (serialEventRun) serialEventRun();
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | #define R2CA_TASK_BODY(NUM) \
|
---|
315 | extern void loop##NUM(void); \
|
---|
316 | \
|
---|
317 | void \
|
---|
318 | r2ca_task##NUM(intptr_t exinf) \
|
---|
319 | { \
|
---|
320 | while(1){ \
|
---|
321 | loop##NUM(); \
|
---|
322 | } \
|
---|
323 | }
|
---|
324 |
|
---|
325 | #if R2CA_NUM_TASK > 0
|
---|
326 | R2CA_TASK_BODY(1)
|
---|
327 | #endif /* R2CA_NUM_TASK > 0 */
|
---|
328 |
|
---|
329 | #if R2CA_NUM_TASK > 1
|
---|
330 | R2CA_TASK_BODY(2)
|
---|
331 | #endif /* R2CA_NUM_TASK > 1 */
|
---|
332 |
|
---|
333 | #if R2CA_NUM_TASK > 2
|
---|
334 | R2CA_TASK_BODY(3)
|
---|
335 | #endif /* R2CA_NUM_TASK > 2 */
|
---|
336 |
|
---|
337 | #if R2CA_NUM_TASK > 3
|
---|
338 | R2CA_TASK_BODY(4)
|
---|
339 | #endif /* R2CA_NUM_TASK > 3 */
|
---|
340 |
|
---|
341 | #if R2CA_NUM_TASK > 4
|
---|
342 | R2CA_TASK_BODY(5)
|
---|
343 | #endif /* R2CA_NUM_TASK > 4 */
|
---|
344 |
|
---|
345 | #ifdef R2CA_ENABLE_PROFILING
|
---|
346 | void
|
---|
347 | r2ca_idle_task(intptr_t exinf) {
|
---|
348 | while(1){
|
---|
349 | r2ca_di();
|
---|
350 | r2ca_idle_cnt++;
|
---|
351 | r2ca_ei();
|
---|
352 | }
|
---|
353 | }
|
---|
354 | #endif /* R2CA_ENABLE_PROFILING */
|
---|