|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Argument_None.ino |
| 3 | + For Portenta_H7 boards |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_TimerInterrupt |
| 7 | + Licensed under MIT license |
| 8 | +
|
| 9 | + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by |
| 10 | + unsigned long miliseconds), you just consume only one Portenta_H7 STM32 timer and avoid conflicting with other cores' tasks. |
| 11 | + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
| 12 | + Therefore, their executions are not blocked by bad-behaving functions / tasks. |
| 13 | + This important feature is absolutely necessary for mission-critical tasks. |
| 14 | +
|
| 15 | + Based on SimpleTimer - A timer library for Arduino. |
| 16 | + |
| 17 | + Copyright (c) 2010 OTTOTECNICA Italy |
| 18 | +
|
| 19 | + Based on BlynkTimer.h |
| 20 | + Author: Volodymyr Shymanskyy |
| 21 | +
|
| 22 | + Version: 1.2.1 |
| 23 | +
|
| 24 | + Version Modified By Date Comments |
| 25 | + ------- ----------- ---------- ----------- |
| 26 | + 1.2.1 K.Hoang 15/09/2021 Initial coding for Portenta_H7. |
| 27 | +*****************************************************************************************************************************/ |
| 28 | + |
| 29 | +/* |
| 30 | + Notes: |
| 31 | + Special design is necessary to share data between interrupt code and the rest of your program. |
| 32 | + Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume |
| 33 | + variable can not spontaneously change. Because your function may change variables while your program is using them, |
| 34 | + the compiler needs this hint. But volatile alone is often not enough. |
| 35 | + When accessing shared variables, usually interrupts must be disabled. Even with volatile, |
| 36 | + if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. |
| 37 | + If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled |
| 38 | + or the entire sequence of your code which accesses the data. |
| 39 | +*/ |
| 40 | + |
| 41 | +#if !( ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) && defined(ARDUINO_ARCH_MBED) ) |
| 42 | + #error This code is designed to run on Portenta_H7 platform! Please check your Tools->Board setting. |
| 43 | +#endif |
| 44 | + |
| 45 | +// These define's must be placed at the beginning before #include "Portenta_H7_TimerInterrupt.h" |
| 46 | +// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 |
| 47 | +// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. |
| 48 | +#define _TIMERINTERRUPT_LOGLEVEL_ 4 |
| 49 | + |
| 50 | +#include "Portenta_H7_TimerInterrupt.h" |
| 51 | + |
| 52 | +#define LED_OFF HIGH |
| 53 | +#define LED_ON LOW |
| 54 | + |
| 55 | +#ifndef LED_BUILTIN |
| 56 | + #define LED_BUILTIN 24 //LEDG // Pin 24 control on-board LED_GREEN on Portenta_H7 |
| 57 | +#endif |
| 58 | + |
| 59 | +#ifndef LED_BLUE |
| 60 | + #define LED_BLUE 25 //LEDB // Pin 25 control on-board LED_BLUE on Portenta_H7 |
| 61 | +#endif |
| 62 | + |
| 63 | +#ifndef LED_RED |
| 64 | + #define LED_RED 23 // LEDR // Pin 23 control on-board LED_RED on Portenta_H7 |
| 65 | +#endif |
| 66 | + |
| 67 | +// In Portenta_H7, avoid doing something fancy in ISR, for example Serial.print |
| 68 | +// Or you can get this run-time error / crash |
| 69 | + |
| 70 | +void TimerHandler0() |
| 71 | +{ |
| 72 | + static bool toggle0 = false; |
| 73 | + |
| 74 | + //timer interrupt toggles pin LED_BUILTIN |
| 75 | + digitalWrite(LED_BUILTIN, toggle0); |
| 76 | + toggle0 = !toggle0; |
| 77 | +} |
| 78 | + |
| 79 | +void TimerHandler1() |
| 80 | +{ |
| 81 | + static bool toggle1 = false; |
| 82 | + |
| 83 | + //timer interrupt toggles outputPin |
| 84 | + digitalWrite(LED_BLUE, toggle1); |
| 85 | + toggle1 = !toggle1; |
| 86 | +} |
| 87 | + |
| 88 | +void TimerHandler2() |
| 89 | +{ |
| 90 | + static bool toggle2 = false; |
| 91 | + |
| 92 | + //timer interrupt toggles outputPin |
| 93 | + digitalWrite(LED_RED, toggle2); |
| 94 | + toggle2 = !toggle2; |
| 95 | +} |
| 96 | + |
| 97 | +#define TIMER0_INTERVAL_MS 1000 |
| 98 | +#define TIMER1_INTERVAL_MS 2000 |
| 99 | +#define TIMER2_INTERVAL_MS 5000 |
| 100 | + |
| 101 | +// Depending on the board, you can select STM32H7 Hardware Timer from TIM1-TIM22 |
| 102 | +// If you select a Timer not correctly, you'll get a message from compiler |
| 103 | +// 'TIMxx' was not declared in this scope; did you mean 'TIMyy'? |
| 104 | + |
| 105 | +// Portenta_H7 OK : TIM1, TIM4, TIM7, TIM8, TIM12, TIM13, TIM14, TIM15, TIM16, TIM17 |
| 106 | +// Portenta_H7 Not OK : TIM2, TIM3, TIM5, TIM6, TIM18, TIM19, TIM20, TIM21, TIM22 |
| 107 | +// Portenta_H7 No timer : TIM9, TIM10, TIM11. Only for STM32F2, STM32F4 and STM32L1 |
| 108 | +// Portenta_H7 No timer : TIM18, TIM19, TIM20, TIM21, TIM22 |
| 109 | + |
| 110 | +// Init timer TIM15 |
| 111 | +Portenta_H7_Timer ITimer0(TIM15); |
| 112 | +// Init timer TIM16 |
| 113 | +Portenta_H7_Timer ITimer1(TIM16); |
| 114 | +// Init timer TIM17 |
| 115 | +Portenta_H7_Timer ITimer2(TIM17); |
| 116 | + |
| 117 | +void setup() |
| 118 | +{ |
| 119 | + pinMode(LED_BUILTIN, OUTPUT); |
| 120 | + pinMode(LED_BLUE, OUTPUT); |
| 121 | + pinMode(LED_RED, OUTPUT); |
| 122 | + |
| 123 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 124 | + digitalWrite(LED_BLUE, LED_OFF); |
| 125 | + digitalWrite(LED_RED, LED_OFF); |
| 126 | + |
| 127 | + |
| 128 | + Serial.begin(115200); |
| 129 | + while (!Serial); |
| 130 | + |
| 131 | + delay(100); |
| 132 | + |
| 133 | + Serial.print(F("\nStarting Argument_None on ")); Serial.println(BOARD_NAME); |
| 134 | + Serial.println(PORTENTA_H7_TIMER_INTERRUPT_VERSION); |
| 135 | + |
| 136 | + // Interval in microsecs |
| 137 | + if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0)) |
| 138 | + { |
| 139 | + Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(millis()); |
| 140 | + } |
| 141 | + else |
| 142 | + Serial.println(F("Can't set ITimer0. Select another freq. or timer")); |
| 143 | + |
| 144 | + // Interval in microsecs |
| 145 | + if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1)) |
| 146 | + { |
| 147 | + Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(millis()); |
| 148 | + } |
| 149 | + else |
| 150 | + Serial.println(F("Can't set ITimer1. Select another freq. or timer")); |
| 151 | + |
| 152 | + // Interval in microsecs |
| 153 | + if (ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS * 1000, TimerHandler2)) |
| 154 | + { |
| 155 | + Serial.print(F("Starting ITimer2 OK, millis() = ")); Serial.println(millis()); |
| 156 | + } |
| 157 | + else |
| 158 | + Serial.println(F("Can't set ITimer2. Select another freq. or timer")); |
| 159 | +} |
| 160 | + |
| 161 | +void loop() |
| 162 | +{ |
| 163 | + |
| 164 | +} |
0 commit comments