1 | #include "rca.h"
|
---|
2 | #include "rca_lib.h"
|
---|
3 |
|
---|
4 | extern "C" {
|
---|
5 | extern void yield(void);
|
---|
6 | extern void rca_ena_int(uint_t intno);
|
---|
7 | extern void rca_dis_int(uint_t intno);
|
---|
8 | }
|
---|
9 |
|
---|
10 | #ifdef RCA_ENABLE_PROFILING
|
---|
11 | volatile uint32_t rca_idle_result;
|
---|
12 | volatile uint32_t rca_isr_result;
|
---|
13 | volatile uint32_t rca_dispatch_result;
|
---|
14 | volatile uint32_t rca_timer_isr_result;
|
---|
15 | volatile uint32_t rca_usb_isr_result;
|
---|
16 | volatile uint32_t rca_sercom0_isr_result;
|
---|
17 | volatile uint32_t rca_sercom2_isr_result;
|
---|
18 | volatile uint32_t rca_sercom3_isr_result;
|
---|
19 | volatile uint32_t rca_sercom4_isr_result;
|
---|
20 | volatile uint32_t rca_sercom5_isr_result;
|
---|
21 | volatile uint32_t rca_eic_isr_result;
|
---|
22 | volatile uint32_t rca_tc5_isr_result;
|
---|
23 | volatile uint32_t rca_rtc_isr_result;
|
---|
24 |
|
---|
25 | static volatile uint32_t rca_idle_cnt;
|
---|
26 | static volatile uint32_t rca_isr_cnt;
|
---|
27 | static volatile uint32_t rca_dispatch_cnt;
|
---|
28 | static volatile uint32_t rca_timer_isr_cnt;
|
---|
29 | static volatile uint32_t rca_usb_isr_cnt;
|
---|
30 | static volatile uint32_t rca_sercom0_isr_cnt;
|
---|
31 | static volatile uint32_t rca_sercom2_isr_cnt;
|
---|
32 | static volatile uint32_t rca_sercom3_isr_cnt;
|
---|
33 | static volatile uint32_t rca_sercom4_isr_cnt;
|
---|
34 | static volatile uint32_t rca_sercom5_isr_cnt;
|
---|
35 | static volatile uint32_t rca_eic_isr_cnt;
|
---|
36 | static volatile uint32_t rca_tc5_isr_cnt;
|
---|
37 | static volatile uint32_t rca_rtc_isr_cnt;
|
---|
38 | static volatile uint32_t rca_profiling_cyccnt;
|
---|
39 | #endif /* RCA_ENABLE_PROFILING */
|
---|
40 |
|
---|
41 | Inline void
|
---|
42 | rca_di(void){
|
---|
43 | Asm("cpsid f":::"memory");
|
---|
44 | }
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * FAULTMASKÌNA
|
---|
48 | */
|
---|
49 | Inline void
|
---|
50 | rca_ei(void){
|
---|
51 | Asm("cpsie f":::"memory");
|
---|
52 | }
|
---|
53 |
|
---|
54 | void
|
---|
55 | rca_init(intptr_t exinf)
|
---|
56 | {
|
---|
57 | init(); //wiring.c
|
---|
58 | }
|
---|
59 |
|
---|
60 | void
|
---|
61 | rca_ena_int(uint_t intno) {
|
---|
62 | if (ena_int(intno + 16) != E_OK) {
|
---|
63 | while(1); //ToDo
|
---|
64 | }
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | void
|
---|
69 | rca_dis_int(uint_t intno) {
|
---|
70 | if (dis_int(intno + 16) != E_OK) {
|
---|
71 | while(1); //ToDo
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | extern const DeviceVectors exception_table;
|
---|
76 |
|
---|
77 | #ifdef RCA_RR_SCHEDULE
|
---|
78 | static uint32_t time_slice = 0;
|
---|
79 | #endif /* RCA_RR_SCHEDULE */
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Cyclic Handler
|
---|
83 | */
|
---|
84 | void
|
---|
85 | rca_CycHandler(intptr_t exinf)
|
---|
86 | {
|
---|
87 | #ifdef RCA_RR_SCHEDULE
|
---|
88 | int i;
|
---|
89 | if (++time_slice == RCA_RR_SCHEDULE_CYCLE) {
|
---|
90 | time_slice = 0;
|
---|
91 | for(i = 1; i < 16; i++){
|
---|
92 | if (RCA_RR_SCHEDULE & (1 << i)) {
|
---|
93 | irot_rdq(i);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | #endif /* RCA_RR_SCHEDULE */
|
---|
98 | #ifdef RCA_ENABLE_PROFILING
|
---|
99 | rca_di();
|
---|
100 | rca_isr_cnt++;
|
---|
101 | rca_timer_isr_cnt++;
|
---|
102 | rca_profiling_cyccnt++;
|
---|
103 | if(rca_profiling_cyccnt == RCA_PROFILING_CYC_MS){
|
---|
104 | rca_idle_result= rca_idle_cnt;
|
---|
105 | rca_isr_result= rca_isr_cnt;
|
---|
106 | rca_dispatch_result= rca_dispatch_cnt;
|
---|
107 | rca_timer_isr_result= rca_timer_isr_cnt;
|
---|
108 | rca_usb_isr_result= rca_usb_isr_cnt;
|
---|
109 | rca_sercom0_isr_result= rca_sercom0_isr_cnt;
|
---|
110 | rca_sercom2_isr_result= rca_sercom2_isr_cnt;
|
---|
111 | rca_sercom3_isr_result= rca_sercom3_isr_cnt;
|
---|
112 | rca_sercom4_isr_result= rca_sercom4_isr_cnt;
|
---|
113 | rca_sercom5_isr_result= rca_sercom5_isr_cnt;
|
---|
114 | rca_eic_isr_result= rca_eic_isr_cnt;
|
---|
115 | rca_tc5_isr_result= rca_tc5_isr_cnt;
|
---|
116 | rca_rtc_isr_result= rca_rtc_isr_cnt;
|
---|
117 | rca_idle_cnt = 0;
|
---|
118 | rca_isr_cnt = 0;
|
---|
119 | rca_dispatch_cnt = 0;
|
---|
120 | rca_timer_isr_cnt = 0;
|
---|
121 | rca_usb_isr_cnt = 0;
|
---|
122 | rca_sercom0_isr_cnt = 0;
|
---|
123 | rca_sercom2_isr_cnt = 0;
|
---|
124 | rca_sercom3_isr_cnt = 0;
|
---|
125 | rca_sercom4_isr_cnt = 0;
|
---|
126 | rca_sercom5_isr_cnt = 0;
|
---|
127 | rca_eic_isr_cnt = 0;
|
---|
128 | rca_tc5_isr_cnt = 0;
|
---|
129 | rca_rtc_isr_cnt = 0;
|
---|
130 | rca_profiling_cyccnt = 0;
|
---|
131 | }
|
---|
132 | rca_ei();
|
---|
133 | #endif /* RCA_ENABLE_PROFILING */
|
---|
134 | ((void(*)(void))(exception_table.pfnSysTick_Handler))();
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Interrupt Handler
|
---|
139 | */
|
---|
140 | void
|
---|
141 | rca_USB_Handler(void)
|
---|
142 | {
|
---|
143 | #ifdef RCA_ENABLE_PROFILING
|
---|
144 | rca_di();
|
---|
145 | rca_usb_isr_cnt++;
|
---|
146 | rca_isr_cnt++;
|
---|
147 | rca_ei();
|
---|
148 | #endif /* RCA_ENABLE_PROFILING */
|
---|
149 | ((void(*)(void))(exception_table.pfnUSB_Handler))();
|
---|
150 | }
|
---|
151 |
|
---|
152 | void
|
---|
153 | rca_SERCOM0_Handler(void)
|
---|
154 | {
|
---|
155 | #ifdef RCA_ENABLE_PROFILING
|
---|
156 | rca_di();
|
---|
157 | rca_sercom0_isr_cnt++;
|
---|
158 | rca_isr_cnt++;
|
---|
159 | rca_ei();
|
---|
160 | #endif /* RCA_ENABLE_PROFILING */
|
---|
161 | ((void(*)(void))(exception_table.pfnSERCOM0_Handler))();
|
---|
162 | }
|
---|
163 |
|
---|
164 | #ifdef RCA_USE_SERIAL3
|
---|
165 | #define PIN_SERIAL3_RX 49 /* D5 */
|
---|
166 | #define PIN_SERIAL3_TX 48 /* D4 */
|
---|
167 |
|
---|
168 | Uart Serial3(&sercom2, PIN_SERIAL3_RX, PIN_SERIAL3_TX);
|
---|
169 |
|
---|
170 | void
|
---|
171 | rca_SERCOM2_Handler(void)
|
---|
172 | {
|
---|
173 | #ifdef RCA_ENABLE_PROFILING
|
---|
174 | rca_di();
|
---|
175 | rca_sercom2_isr_cnt++;
|
---|
176 | rca_isr_cnt++;
|
---|
177 | rca_ei();
|
---|
178 | #endif /* RCA_ENABLE_PROFILING */
|
---|
179 | Serial3.IrqHandler();
|
---|
180 | }
|
---|
181 | #endif /* RCA_USE_SERIAL3 */
|
---|
182 |
|
---|
183 | void
|
---|
184 | rca_SERCOM3_Handler(void)
|
---|
185 | {
|
---|
186 | #ifdef RCA_ENABLE_PROFILING
|
---|
187 | rca_di();
|
---|
188 | rca_sercom3_isr_cnt++;
|
---|
189 | rca_isr_cnt++;
|
---|
190 | rca_ei();
|
---|
191 | #endif /* RCA_ENABLE_PROFILING */
|
---|
192 | ((void(*)(void))(exception_table.pfnSERCOM3_Handler))();
|
---|
193 | }
|
---|
194 |
|
---|
195 | void
|
---|
196 | rca_SERCOM4_Handler(void)
|
---|
197 | {
|
---|
198 | #ifdef RCA_ENABLE_PROFILING
|
---|
199 | rca_di();
|
---|
200 | rca_sercom4_isr_cnt++;
|
---|
201 | rca_isr_cnt++;
|
---|
202 | rca_ei();
|
---|
203 | #endif /* RCA_ENABLE_PROFILING */
|
---|
204 | ((void(*)(void))(exception_table.pfnSERCOM4_Handler))();
|
---|
205 | }
|
---|
206 |
|
---|
207 | #ifdef TOPPERS_USE_ARDUINO_SERIAL
|
---|
208 | void
|
---|
209 | rca_SERCOM5_Handler(void)
|
---|
210 | {
|
---|
211 | #ifdef RCA_ENABLE_PROFILING
|
---|
212 | rca_di();
|
---|
213 | rca_sercom5_isr_cnt++;
|
---|
214 | rca_isr_cnt++;
|
---|
215 | rca_ei();
|
---|
216 | #endif /* RCA_ENABLE_PROFILING */
|
---|
217 | ((void(*)(void))(exception_table.pfnSERCOM5_Handler))();
|
---|
218 | }
|
---|
219 | #endif /* TOPPERS_USE_ARDUINO_SERIAL */
|
---|
220 |
|
---|
221 | void
|
---|
222 | rca_EIC_Handler(void)
|
---|
223 | {
|
---|
224 | #ifdef RCA_ENABLE_PROFILING
|
---|
225 | rca_di();
|
---|
226 | rca_eic_isr_cnt++;
|
---|
227 | rca_isr_cnt++;
|
---|
228 | rca_ei();
|
---|
229 | #endif /* RCA_ENABLE_PROFILING */
|
---|
230 | ((void(*)(void))(exception_table.pfnEIC_Handler))();
|
---|
231 | }
|
---|
232 |
|
---|
233 | void
|
---|
234 | rca_TC5_Handler(void)
|
---|
235 | {
|
---|
236 | #ifdef RCA_ENABLE_PROFILING
|
---|
237 | rca_di();
|
---|
238 | rca_tc5_isr_cnt++;
|
---|
239 | rca_isr_cnt++;
|
---|
240 | rca_ei();
|
---|
241 | #endif /* RCA_ENABLE_PROFILING */
|
---|
242 | ((void(*)(void))(exception_table.pfnTC5_Handler))();
|
---|
243 | }
|
---|
244 |
|
---|
245 | void
|
---|
246 | rca_RTC_Handler(void)
|
---|
247 | {
|
---|
248 | #ifdef RCA_ENABLE_PROFILING
|
---|
249 | rca_di();
|
---|
250 | rca_rtc_isr_cnt++;
|
---|
251 | rca_isr_cnt++;
|
---|
252 | rca_ei();
|
---|
253 | #endif /* RCA_ENABLE_PROFILING */
|
---|
254 | ((void(*)(void))(exception_table.pfnRTC_Handler))();
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * For delay
|
---|
259 | */
|
---|
260 | void
|
---|
261 | yield(void){
|
---|
262 | #ifdef RCA_RR_SCHEDULE
|
---|
263 | time_slice = 0;
|
---|
264 | #endif /* RCA_RR_SCHEDULE */
|
---|
265 | #ifdef RCA_ENABLE_PROFILING
|
---|
266 | rca_di();
|
---|
267 | rca_dispatch_cnt++;
|
---|
268 | rca_ei();
|
---|
269 | #endif /* RCA_ENABLE_PROFILING */
|
---|
270 | dly_tsk(0);
|
---|
271 | }
|
---|
272 |
|
---|
273 | extern void setup(void);
|
---|
274 | extern void loop(void);
|
---|
275 |
|
---|
276 | void
|
---|
277 | rca_maintask(intptr_t exinf)
|
---|
278 | {
|
---|
279 | syslog(LOG_NOTICE, "Arduino Main Task start!");
|
---|
280 | dly_tsk(1);
|
---|
281 | #ifdef USBCON
|
---|
282 | USBDevice.init();
|
---|
283 | USBDevice.attach();
|
---|
284 | #endif /* USBCON */
|
---|
285 |
|
---|
286 | analogReference(AR_DEFAULT);
|
---|
287 |
|
---|
288 | setup();
|
---|
289 |
|
---|
290 | sig_sem(SETUP_SEM);
|
---|
291 |
|
---|
292 | chg_pri(0, RCA_MAINTASK_LOOP_PRI);
|
---|
293 |
|
---|
294 | while(1){
|
---|
295 | loop();
|
---|
296 | if (serialEventRun) serialEventRun();
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | #define RCA_TASK_BODY(NUM) \
|
---|
301 | extern void task##NUM##_setup(void); \
|
---|
302 | extern void task##NUM##_loop(void); \
|
---|
303 | \
|
---|
304 | void \
|
---|
305 | rca_task##NUM(intptr_t exinf) \
|
---|
306 | { \
|
---|
307 | wai_sem(SETUP_SEM);\
|
---|
308 | task##NUM##_setup(); \
|
---|
309 | sig_sem(SETUP_SEM);\
|
---|
310 | chg_pri(0, RCA_TASK##NUM##_LOOP_PRI); \
|
---|
311 | \
|
---|
312 | while(1){ \
|
---|
313 | task##NUM##_loop(); \
|
---|
314 | } \
|
---|
315 | }
|
---|
316 |
|
---|
317 | #if RCA_NUM_TASK > 0
|
---|
318 | RCA_TASK_BODY(1)
|
---|
319 | #endif /* RCA_NUM_TASK > 0 */
|
---|
320 |
|
---|
321 | #if RCA_NUM_TASK > 1
|
---|
322 | RCA_TASK_BODY(2)
|
---|
323 | #endif /* RCA_NUM_TASK > 1 */
|
---|
324 |
|
---|
325 | #if RCA_NUM_TASK > 2
|
---|
326 | RCA_TASK_BODY(3)
|
---|
327 | #endif /* RCA_NUM_TASK > 2 */
|
---|
328 |
|
---|
329 | #if RCA_NUM_TASK > 3
|
---|
330 | RCA_TASK_BODY(4)
|
---|
331 | #endif /* RCA_NUM_TASK > 3 */
|
---|
332 |
|
---|
333 | #if RCA_NUM_TASK > 4
|
---|
334 | RCA_TASK_BODY(5)
|
---|
335 | #endif /* RCA_NUM_TASK > 4 */
|
---|
336 |
|
---|
337 | #ifdef RCA_ENABLE_PROFILING
|
---|
338 | void
|
---|
339 | rca_idle_task(intptr_t exinf) {
|
---|
340 | while(1){
|
---|
341 | rca_di();
|
---|
342 | rca_idle_cnt++;
|
---|
343 | rca_ei();
|
---|
344 | }
|
---|
345 | }
|
---|
346 | #endif /* RCA_ENABLE_PROFILING */
|
---|