[260] | 1 | #include "r2ca.h"
|
---|
| 2 | #include "r2ca_lib.h"
|
---|
[136] | 3 |
|
---|
| 4 | extern "C" {
|
---|
| 5 | extern void yield(void);
|
---|
[260] | 6 | extern void r2ca_ena_int(uint_t intno);
|
---|
| 7 | extern void r2ca_dis_int(uint_t intno);
|
---|
[136] | 8 | }
|
---|
| 9 |
|
---|
[260] | 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;
|
---|
[136] | 24 |
|
---|
[260] | 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 */
|
---|
[136] | 40 |
|
---|
| 41 | Inline void
|
---|
[260] | 42 | r2ca_di(void){
|
---|
[136] | 43 | Asm("cpsid f":::"memory");
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | * FAULTMASKÌNA
|
---|
| 48 | */
|
---|
| 49 | Inline void
|
---|
[260] | 50 | r2ca_ei(void){
|
---|
[136] | 51 | Asm("cpsie f":::"memory");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | void
|
---|
[260] | 55 | r2ca_init(intptr_t exinf)
|
---|
[136] | 56 | {
|
---|
[260] | 57 | init(); //wiring.c
|
---|
[136] | 58 | }
|
---|
| 59 |
|
---|
| 60 | void
|
---|
[260] | 61 | r2ca_ena_int(uint_t intno) {
|
---|
| 62 | if (ena_int(intno + 16) != E_OK) {
|
---|
| 63 | while(1); //ToDo
|
---|
| 64 | }
|
---|
[136] | 65 | }
|
---|
| 66 |
|
---|
| 67 | void
|
---|
[260] | 68 | r2ca_dis_int(uint_t intno) {
|
---|
| 69 | if (dis_int(intno + 16) != E_OK) {
|
---|
| 70 | while(1); //ToDo
|
---|
| 71 | }
|
---|
[136] | 72 | }
|
---|
| 73 |
|
---|
| 74 | extern const DeviceVectors exception_table;
|
---|
| 75 |
|
---|
| 76 | static uint32_t time_slice = 0;
|
---|
| 77 |
|
---|
| 78 | /*
|
---|
| 79 | * Cyclic Handler
|
---|
| 80 | */
|
---|
| 81 | void
|
---|
[260] | 82 | r2ca_CycHandler(intptr_t exinf)
|
---|
[136] | 83 | {
|
---|
[260] | 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))();
|
---|
[136] | 130 | }
|
---|
| 131 |
|
---|
| 132 | /*
|
---|
| 133 | * Interrupt Handler
|
---|
| 134 | */
|
---|
| 135 | void
|
---|
[260] | 136 | r2ca_USB_Handler(void)
|
---|
[136] | 137 | {
|
---|
[260] | 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))();
|
---|
[136] | 145 | }
|
---|
| 146 |
|
---|
| 147 | void
|
---|
[260] | 148 | r2ca_SERCOM0_Handler(void)
|
---|
[136] | 149 | {
|
---|
[260] | 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))();
|
---|
[136] | 157 | }
|
---|
| 158 |
|
---|
[260] | 159 | #ifdef R2CA_USE_SERIAL3
|
---|
[136] | 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
|
---|
[260] | 166 | r2ca_SERCOM2_Handler(void)
|
---|
[136] | 167 | {
|
---|
[260] | 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();
|
---|
[136] | 175 | }
|
---|
[260] | 176 | #endif /* R2CA_USE_SERIAL3 */
|
---|
[136] | 177 |
|
---|
| 178 | void
|
---|
[260] | 179 | r2ca_SERCOM3_Handler(void)
|
---|
[136] | 180 | {
|
---|
[260] | 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))();
|
---|
[136] | 188 | }
|
---|
| 189 |
|
---|
| 190 | void
|
---|
[260] | 191 | r2ca_SERCOM4_Handler(void)
|
---|
[136] | 192 | {
|
---|
[260] | 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))();
|
---|
[136] | 200 | }
|
---|
| 201 |
|
---|
| 202 | #ifdef TOPPERS_USE_ARDUINO_SERIAL
|
---|
| 203 | void
|
---|
[260] | 204 | r2ca_SERCOM5_Handler(void)
|
---|
[136] | 205 | {
|
---|
[260] | 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))();
|
---|
[136] | 213 | }
|
---|
| 214 | #endif /* TOPPERS_USE_ARDUINO_SERIAL */
|
---|
| 215 |
|
---|
| 216 | void
|
---|
[260] | 217 | r2ca_EIC_Handler(void)
|
---|
[136] | 218 | {
|
---|
[260] | 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))();
|
---|
[136] | 226 | }
|
---|
| 227 |
|
---|
| 228 | void
|
---|
[260] | 229 | r2ca_TC5_Handler(void)
|
---|
[136] | 230 | {
|
---|
[260] | 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))();
|
---|
[136] | 238 | }
|
---|
| 239 |
|
---|
| 240 | void
|
---|
[260] | 241 | r2ca_RTC_Handler(void)
|
---|
[136] | 242 | {
|
---|
[260] | 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))();
|
---|
[136] | 250 | }
|
---|
| 251 |
|
---|
| 252 | /*
|
---|
| 253 | * For delay
|
---|
| 254 | */
|
---|
| 255 | void
|
---|
| 256 | yield(void){
|
---|
[260] | 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);
|
---|
[136] | 264 | }
|
---|
| 265 |
|
---|
| 266 | extern void setup(void);
|
---|
| 267 | extern void loop(void);
|
---|
| 268 |
|
---|
[260] | 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 |
|
---|
[136] | 288 | void
|
---|
[260] | 289 | r2ca_maintask(intptr_t exinf)
|
---|
[136] | 290 | {
|
---|
[260] | 291 | int i;
|
---|
| 292 |
|
---|
| 293 | syslog(LOG_NOTICE, "Arduino Main Task start!");
|
---|
| 294 |
|
---|
[136] | 295 | #ifdef USBCON
|
---|
[260] | 296 | USBDevice.init();
|
---|
| 297 | USBDevice.attach();
|
---|
[136] | 298 | #endif /* USBCON */
|
---|
[220] | 299 |
|
---|
[260] | 300 | analogReference(AR_DEFAULT);
|
---|
[220] | 301 |
|
---|
[260] | 302 | setup();
|
---|
[249] | 303 |
|
---|
[260] | 304 | for(i = 0; i < R2CA_NUM_TASK; i++) {
|
---|
| 305 | act_tsk(task_id[i]);
|
---|
| 306 | }
|
---|
[249] | 307 |
|
---|
[260] | 308 | while(1){
|
---|
| 309 | loop();
|
---|
| 310 | if (serialEventRun) serialEventRun();
|
---|
| 311 | }
|
---|
[136] | 312 | }
|
---|
| 313 |
|
---|
[260] | 314 | #define R2CA_TASK_BODY(NUM) \
|
---|
| 315 | extern void loop##NUM(void); \
|
---|
[136] | 316 | \
|
---|
| 317 | void \
|
---|
[260] | 318 | r2ca_task##NUM(intptr_t exinf) \
|
---|
[136] | 319 | { \
|
---|
| 320 | while(1){ \
|
---|
[260] | 321 | loop##NUM(); \
|
---|
[136] | 322 | } \
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[260] | 325 | #if R2CA_NUM_TASK > 0
|
---|
| 326 | R2CA_TASK_BODY(1)
|
---|
| 327 | #endif /* R2CA_NUM_TASK > 0 */
|
---|
[136] | 328 |
|
---|
[260] | 329 | #if R2CA_NUM_TASK > 1
|
---|
| 330 | R2CA_TASK_BODY(2)
|
---|
| 331 | #endif /* R2CA_NUM_TASK > 1 */
|
---|
[136] | 332 |
|
---|
[260] | 333 | #if R2CA_NUM_TASK > 2
|
---|
| 334 | R2CA_TASK_BODY(3)
|
---|
| 335 | #endif /* R2CA_NUM_TASK > 2 */
|
---|
[136] | 336 |
|
---|
[260] | 337 | #if R2CA_NUM_TASK > 3
|
---|
| 338 | R2CA_TASK_BODY(4)
|
---|
| 339 | #endif /* R2CA_NUM_TASK > 3 */
|
---|
[136] | 340 |
|
---|
[260] | 341 | #if R2CA_NUM_TASK > 4
|
---|
| 342 | R2CA_TASK_BODY(5)
|
---|
| 343 | #endif /* R2CA_NUM_TASK > 4 */
|
---|
[136] | 344 |
|
---|
[260] | 345 | #ifdef R2CA_ENABLE_PROFILING
|
---|
[136] | 346 | void
|
---|
[260] | 347 | r2ca_idle_task(intptr_t exinf) {
|
---|
| 348 | while(1){
|
---|
| 349 | r2ca_di();
|
---|
| 350 | r2ca_idle_cnt++;
|
---|
| 351 | r2ca_ei();
|
---|
| 352 | }
|
---|
[136] | 353 | }
|
---|
[260] | 354 | #endif /* R2CA_ENABLE_PROFILING */
|
---|