|
| 1 | +/** |
| 2 | + * This sample will demo the the configuration object of a timer. |
| 3 | + */ |
| 4 | +#include <core/io/UART.hpp> |
| 5 | +#include <core/io/pin.hpp> |
| 6 | +#include <core/manager.hpp> |
| 7 | +#include <core/utils/time.hpp> |
| 8 | + |
| 9 | +namespace io = core::io; |
| 10 | +namespace dev = core::dev; |
| 11 | + |
| 12 | +io::GPIO* ledGPIO; |
| 13 | +io::GPIO* interruptGPIO2Hz; |
| 14 | +io::GPIO* interruptGPIOStopStart; |
| 15 | +io::GPIO* reloadGPIO; |
| 16 | + |
| 17 | +void timer2IRQHandler(void* htim) { |
| 18 | + io::GPIO::State state = ledGPIO->readPin(); |
| 19 | + io::GPIO::State toggleState = state == io::GPIO::State::HIGH ? io::GPIO::State::LOW : io::GPIO::State::HIGH; |
| 20 | + ledGPIO->writePin(toggleState); |
| 21 | + interruptGPIO2Hz->writePin(toggleState); |
| 22 | +} |
| 23 | + |
| 24 | +void timer15IRQHandler(void* htim) { |
| 25 | + io::GPIO::State state = interruptGPIOStopStart->readPin(); |
| 26 | + io::GPIO::State toggleState = state == io::GPIO::State::HIGH ? io::GPIO::State::LOW : io::GPIO::State::HIGH; |
| 27 | + interruptGPIOStopStart->writePin(toggleState); |
| 28 | +} |
| 29 | + |
| 30 | +void timer16IRQHandler(void* htim) { |
| 31 | + io::GPIO::State state = reloadGPIO->readPin(); |
| 32 | + io::GPIO::State toggleState = state == io::GPIO::State::HIGH ? io::GPIO::State::LOW : io::GPIO::State::HIGH; |
| 33 | + reloadGPIO->writePin(toggleState); |
| 34 | +} |
| 35 | + |
| 36 | +int main() { |
| 37 | + // Initialize system |
| 38 | + core::platform::init(); |
| 39 | + |
| 40 | + // Setup GPIO |
| 41 | + ledGPIO = &io::getGPIO<io::Pin::LED>(); |
| 42 | + interruptGPIO2Hz = &io::getGPIO<io::Pin::PC_3>(io::GPIO::Direction::OUTPUT); |
| 43 | + interruptGPIOStopStart = &io::getGPIO<io::Pin::PC_2>(io::GPIO::Direction::OUTPUT); |
| 44 | + reloadGPIO = &io::getGPIO<io::Pin::PC_0>(io::GPIO::Direction::OUTPUT); |
| 45 | + |
| 46 | + dev::TimerConfiguration_t configuration = { |
| 47 | + TIM_COUNTERMODE_UP, // Tell the counter to count upwards towards the clock period |
| 48 | + TIM_CLOCKDIVISION_DIV1, // Used the default clock division |
| 49 | + TIM_AUTORELOAD_PRELOAD_ENABLE, // Enables the auto reload preload which makes TIMx_ARR buffered. |
| 50 | + TIM_CLOCKSOURCE_INTERNAL, // Set the clock source to be the microcontrollers internal clock. |
| 51 | + TIM_TRGO_RESET, // Tells the timer to reset whenever it enters a Master / Slave configuration. Will not be triggered or used in this configuration. |
| 52 | + TIM_MASTERSLAVEMODE_DISABLE // Disable Masster Slave Mode for the timer |
| 53 | + }; |
| 54 | + |
| 55 | + // Set up the Timer |
| 56 | + dev::Timer& sampleTimer1 = dev::getTimer<dev::MCUTimer::Timer2>(1000, configuration); |
| 57 | + |
| 58 | +#ifdef STM32F4xx |
| 59 | + // F4xx does not support Timers 15 & 16, change them to Timer11 & Timer12 |
| 60 | + dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer11>(200, configuration); |
| 61 | + dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer12>(200, configuration); |
| 62 | +#else |
| 63 | + dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer15>(1000, configuration); |
| 64 | + dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer16>(1000, configuration); |
| 65 | +#endif |
| 66 | + |
| 67 | + sampleTimer1.startTimer(timer2IRQHandler); |
| 68 | + sampleTimer2.startTimer(timer15IRQHandler); |
| 69 | + sampleTimer3.startTimer(timer16IRQHandler); |
| 70 | + |
| 71 | + while (1) { |
| 72 | + core::time::wait(500); |
| 73 | + sampleTimer2.stopTimer(); |
| 74 | + sampleTimer3.reloadTimer(); |
| 75 | + core::time::wait(500); |
| 76 | + sampleTimer2.startTimer(); |
| 77 | + } |
| 78 | +} |
0 commit comments