agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
arduino.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 ykat UG (haftungsbeschraenkt) - All Rights Reserved
3  *
4  * Permission for non-commercial use is hereby granted,
5  * free of charge, without warranty of any kind.
6  */
7 #ifndef ZRTOS_BOARDS_ARDUINO_H
8 #define ZRTOS_BOARDS_ARDUINO_H
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #if defined(ZRTOS_BOARD__ARDUINO) || defined(__DOXYGEN__)
14 #define ZRTOS_BOARD__FOUND
15 
16 
17 //https://android.googlesource.com/platform/external/arduino/+/d5790d78880d4bd60be277ee20e53a851aa8c116/hardware/arduino/cores/arduino/wiring.c
18 
19 /*
20 // the prescaler is set so that timer0 ticks every 64 clock cycles, and the
21 // the overflow handler is called every 256 ticks.
22 #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
23 // the whole number of milliseconds per timer0 overflow
24 #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
25 // the fractional number of milliseconds per timer0 overflow. we shift right
26 // by three to fit these numbers into a byte. (for the clock speeds we care
27 // about - 8 and 16 MHz - this doesn't lose precision.)
28 #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
29 #define FRACT_MAX (1000 >> 3)
30 volatile unsigned long timer0_overflow_count = 0;
31 volatile unsigned long timer0_millis = 0;
32 static unsigned char timer0_fract = 0;
33 
34 Timer0: Timer0 is a 8bit timer. In the Arduino world Timer0 is been used for
35  the timer functions, like delay(), millis() and micros(). If you change
36  Timer0 registers, this may influence the Arduino timer function. So you
37  should know what you are doing. Timer1: Timer1 is a 16bit timer. In the
38  Arduino world the Servo library uses Timer1 on Arduino Uno (Timer5 on
39  Arduino Mega). Timer2: Timer2 is a 8bit timer like Timer0. In
40  the Arduino work the tone() function uses Timer2. Timer3, Timer4, Timer5:
41  Timer 3,4,5 are only available on Arduino Mega boards. These timers are
42  all 16bit timers. Timer Register You can change the Timer behaviour
43  through the timer register. The most important timer registers are:
44 
45  https://oscarliang.com/arduino-timer-and-interrupt-tutorial/
46 //https://deepbluembedded.com/arduino-timer-calculator-code-generator/
47 
48 
49 
50 SIGNAL(TIMER0_OVF_vect){
51  // copy these to local variables so they can be stored in registers
52  // (volatile variables must be read from memory on every access)
53  unsigned long m = timer0_millis;
54  unsigned char f = timer0_fract;
55  m += MILLIS_INC;
56  f += FRACT_INC;
57  if (f >= FRACT_MAX) {
58  f -= FRACT_MAX;
59  m += 1;
60  }
61  timer0_fract = f;
62  timer0_millis = m;
63  timer0_overflow_count++;
64 }
65 
66 void delay(unsigned long ms)
67 {
68  uint16_t start = (uint16_t)micros();
69  while (ms > 0) {
70  if (((uint16_t)micros() - start) >= 1000) {
71  ms--;
72  start += 1000;
73  }
74  }
75 }
76 */
77 
78 #define ZRTOS_BOARD__TICK_PERIOD_MS\
79  (MICROSECONDS_PER_TIMER0_OVERFLOW/1000)
80 
82  unsigned long m = timer0_millis;
83  unsigned char f = timer0_fract;
84  m += MILLIS_INC;
85  f += FRACT_INC;
86  if (f >= FRACT_MAX) {
87  f -= FRACT_MAX;
88  m += 1;
89  }
90  timer0_fract = f;
91  timer0_millis = m;
92  timer0_overflow_count++;
93 }
94 /*
95 void zrtos_board_arduino__on_ovf1(void){
96  system_tick();
97 }
98 */
99 
100 ISR(TIMER0_OVF_vect,ISR_NAKED){
101  //called every 16 ms
102  ZRTOS_ARCH__SAVE_CPU_STATE((void*)SP);
103  // @todo save register on stack
106  //avr/time.h system_tick();
107  ZRTOS_ARCH__LOAD_CPU_STATE((void*)SP);
110 }
111 
112 #define ZRTOS_BOARD__WATCH_DOG_START() \
113  wdt_enable(WDTO_8S);
114 
115 #define ZRTOS_BOARD__WATCH_DOG_STOP() \
116  wdt_disable();
117 
118 #define ZRTOS_BOARD__WATCH_DOG_RESET() \
119  wdt_reset();
120 
121 __attribute__((naked))ISR(WDT_vect){
122  ZRTOS_BOARD__ON_WATCH_DOG();
123 }
124 
125 #endif
126 
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 #endif
#define ZRTOS_ARCH__SAVE_CPU_STATE()
Definition: atmega2560.h:195
#define ZRTOS_ARCH__LOAD_CPU_STATE()
Definition: atmega2560.h:245
void(* zrtos_board__on_tick_naked)(void)
Definition: board.h:34
#define ZRTOS_ARCH__RETURN_FROM_INTERRUPT()
Definition: atmega2560.h:293
ISR(TIMER0_OVF_vect, ISR_NAKED)
Definition: arduino.h:100
void(* zrtos_board__on_tick)(void)
Definition: board.h:33
void zrtos_board_arduino__on_ovf(void)
Definition: arduino.h:81