circuitpython/ports/stm/peripherals/timers.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

490 lines
12 KiB
C
Raw Normal View History

2020-06-25 17:27:01 -04:00
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "timers.h"
#include "py/mpconfig.h"
#include "py/gc.h"
#include "py/obj.h"
#include "py/runtime.h"
2022-05-27 15:59:54 -04:00
#include "supervisor/shared/translate/translate.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
2020-07-16 17:11:24 -04:00
#if !(CPY_STM32H7)
#define ALL_CLOCKS 0xFFFF
#define NULL_IRQ 0xFF
2020-06-25 17:27:01 -04:00
static bool stm_timer_reserved[MP_ARRAY_SIZE(mcu_tim_banks)];
static bool stm_timer_never_reset[MP_ARRAY_SIZE(mcu_tim_banks)];
typedef void (*stm_timer_callback_t)(void);
// Array of function pointers.
static stm_timer_callback_t stm_timer_callback[MP_ARRAY_SIZE(mcu_tim_banks)];
static size_t irq_map[] = {
#ifdef TIM1
TIM1_CC_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM2
TIM2_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM3
TIM3_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM4
TIM4_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM5
TIM5_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM6
2021-03-15 09:57:36 -04:00
#if !defined(DAC_BASE) && !defined(DAC1_BASE)
TIM6_IRQn,
#else
TIM6_DAC_IRQn,
#endif
#else
NULL_IRQ,
#endif
#ifdef TIM7
TIM7_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM8
TIM8_CC_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM9
TIM1_BRK_TIM9_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM10
TIM1_UP_TIM10_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM11
TIM1_TRG_COM_TIM11_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM12
TIM8_BRK_TIM12_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM13
TIM8_UP_TIM13_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM14
TIM8_TRG_COM_TIM14_IRQn,
#else
NULL_IRQ,
#endif
#ifdef TIM15
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#ifdef STM32L4
TIM1_BRK_TIM15_IRQn,
#else
TIM15_IRQn,
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#endif
#else
NULL_IRQ,
#endif
#ifdef TIM16
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#ifdef STM32L4
TIM1_UP_TIM16_IRQn,
#else
TIM16_IRQn,
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#endif
#else
NULL_IRQ,
#endif
#ifdef TIM17
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#ifdef STM32L4
TIM1_TRG_COM_TIM17_IRQn
#else
TIM17_IRQn,
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#endif
#else
NULL_IRQ,
#endif
};
2020-06-25 17:27:01 -04:00
// Get the frequency (in Hz) of the source clock for the given timer.
// On STM32F405/407/415/417 there are 2 cases for how the clock freq is set.
// If the APB prescaler is 1, then the timer clock is equal to its respective
// APB clock. Otherwise (APB prescaler > 1) the timer clock is twice its
// respective APB clock. See DM00031020 Rev 4, page 115.
uint32_t stm_peripherals_timer_get_source_freq(TIM_TypeDef *timer) {
size_t tim_id = stm_peripherals_timer_get_index(timer);
2020-06-25 17:27:01 -04:00
uint32_t source, clk_div;
if (tim_id == 1 || (8 <= tim_id && tim_id <= 11)) {
// TIM{1,8,9,10,11} are on APB2
source = HAL_RCC_GetPCLK2Freq();
clk_div = RCC->CFGR & RCC_CFGR_PPRE2;
} else {
// TIM{2,3,4,5,6,7,12,13,14} are on APB1
source = HAL_RCC_GetPCLK1Freq();
clk_div = RCC->CFGR & RCC_CFGR_PPRE1;
}
if (clk_div != 0) {
// APB prescaler for this timer is > 1
source *= 2;
}
return source;
}
size_t stm_peripherals_timer_get_irqnum(TIM_TypeDef *instance) {
2020-07-09 16:45:39 -04:00
size_t tim_id = stm_peripherals_timer_get_index(instance);
return irq_map[tim_id];
}
void timers_reset(void) {
uint16_t never_reset_mask = 0x00;
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_tim_banks); i++) {
if (!stm_timer_never_reset[i]) {
stm_timer_reserved[i] = false;
} else {
never_reset_mask |= 1 << i;
}
}
tim_clock_disable(ALL_CLOCKS & ~(never_reset_mask));
}
TIM_TypeDef *stm_peripherals_find_timer(void) {
// Check for timers on pins outside the package size
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_tim_banks); i++) {
bool timer_in_package = false;
// Find each timer instance on the given bank
for (size_t j = 0; j < MP_ARRAY_SIZE(mcu_tim_pin_list); j++) {
// If a pin is claimed, we skip it
if ((mcu_tim_pin_list[j].tim_index == i)
2021-03-15 09:57:36 -04:00
&& (common_hal_mcu_pin_is_free(mcu_tim_pin_list[j].pin) == true)) {
// Search whether any pins in the package array match it
for (size_t k = 0; k < mcu_pin_globals.map.alloc; k++) {
2021-03-15 09:57:36 -04:00
if ((mcu_tim_pin_list[j].pin == (mcu_pin_obj_t *)(mcu_pin_globals.map.table[k].value))) {
timer_in_package = true;
}
}
}
}
// If no results are found, no unclaimed pins with this timer are in this package,
// and it is safe to pick
if (timer_in_package == false && mcu_tim_banks[i] != NULL) {
return mcu_tim_banks[i];
}
}
2021-03-15 09:57:36 -04:00
// TODO: secondary search for timers outside the pins in the board profile
2020-06-25 17:27:01 -04:00
// Work backwards - higher index timers have fewer pin allocations
for (size_t i = (MP_ARRAY_SIZE(mcu_tim_banks) - 1); i >= 0; i--) {
2020-06-25 17:27:01 -04:00
if ((!stm_timer_reserved[i]) && (mcu_tim_banks[i] != NULL)) {
return mcu_tim_banks[i];
}
}
mp_raise_RuntimeError(translate("All timers in use"));
return NULL;
}
void stm_peripherals_timer_preinit(TIM_TypeDef *instance, uint8_t prio, void (*callback)(void)) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
stm_timer_callback[tim_idx] = callback;
tim_clock_enable(1 << tim_idx);
HAL_NVIC_SetPriority(irq_map[tim_idx], prio, 0);
HAL_NVIC_EnableIRQ(irq_map[tim_idx]);
}
void stm_peripherals_timer_reserve(TIM_TypeDef *instance) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
stm_timer_reserved[tim_idx] = true;
}
void stm_peripherals_timer_set_callback(void (*callback)(void), TIM_TypeDef *timer) {
stm_timer_callback[stm_peripherals_timer_get_index(timer)] = callback;
2020-06-25 17:27:01 -04:00
}
void stm_peripherals_timer_free(TIM_TypeDef *instance) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
2020-07-22 13:58:57 -04:00
HAL_NVIC_DisableIRQ(irq_map[tim_idx]);
stm_timer_callback[tim_idx] = NULL;
tim_clock_disable(1 << tim_idx);
stm_timer_reserved[tim_idx] = false;
stm_timer_never_reset[tim_idx] = false;
}
void stm_peripherals_timer_never_reset(TIM_TypeDef *instance) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
stm_timer_never_reset[tim_idx] = true;
}
void stm_peripherals_timer_reset_ok(TIM_TypeDef *instance) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
stm_timer_never_reset[tim_idx] = false;
}
bool stm_peripherals_timer_is_never_reset(TIM_TypeDef *instance) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
return stm_timer_never_reset[tim_idx];
}
bool stm_peripherals_timer_is_reserved(TIM_TypeDef *instance) {
size_t tim_idx = stm_peripherals_timer_get_index(instance);
return stm_timer_reserved[tim_idx];
}
2020-06-25 17:27:01 -04:00
size_t stm_peripherals_timer_get_index(TIM_TypeDef *instance) {
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_tim_banks); i++) {
if (instance == mcu_tim_banks[i]) {
return i;
}
}
return ~(size_t)0;
2020-06-25 17:27:01 -04:00
}
void tim_clock_enable(uint16_t mask) {
2020-06-25 17:27:01 -04:00
#ifdef TIM1
if (mask & (1 << 0)) {
__HAL_RCC_TIM1_CLK_ENABLE();
}
#endif
#ifdef TIM2
if (mask & (1 << 1)) {
__HAL_RCC_TIM2_CLK_ENABLE();
}
#endif
#ifdef TIM3
if (mask & (1 << 2)) {
__HAL_RCC_TIM3_CLK_ENABLE();
}
#endif
#ifdef TIM4
if (mask & (1 << 3)) {
__HAL_RCC_TIM4_CLK_ENABLE();
}
#endif
#ifdef TIM5
if (mask & (1 << 4)) {
__HAL_RCC_TIM5_CLK_ENABLE();
}
#endif
2021-03-15 09:57:36 -04:00
// 6 and 7 are reserved ADC timers
2020-06-25 17:27:01 -04:00
#ifdef TIM8
if (mask & (1 << 7)) {
__HAL_RCC_TIM8_CLK_ENABLE();
}
#endif
#ifdef TIM9
if (mask & (1 << 8)) {
__HAL_RCC_TIM9_CLK_ENABLE();
}
#endif
#ifdef TIM10
if (mask & (1 << 9)) {
__HAL_RCC_TIM10_CLK_ENABLE();
}
#endif
#ifdef TIM11
if (mask & (1 << 10)) {
__HAL_RCC_TIM11_CLK_ENABLE();
}
#endif
#ifdef TIM12
if (mask & (1 << 11)) {
__HAL_RCC_TIM12_CLK_ENABLE();
}
#endif
#ifdef TIM13
if (mask & (1 << 12)) {
__HAL_RCC_TIM13_CLK_ENABLE();
}
#endif
#ifdef TIM14
if (mask & (1 << 13)) {
__HAL_RCC_TIM14_CLK_ENABLE();
}
#endif
}
void tim_clock_disable(uint16_t mask) {
2020-06-25 17:27:01 -04:00
#ifdef TIM1
if (mask & (1 << 0)) {
__HAL_RCC_TIM1_CLK_DISABLE();
}
#endif
#ifdef TIM2
if (mask & (1 << 1)) {
__HAL_RCC_TIM2_CLK_DISABLE();
}
#endif
#ifdef TIM3
if (mask & (1 << 2)) {
__HAL_RCC_TIM3_CLK_DISABLE();
}
#endif
#ifdef TIM4
if (mask & (1 << 3)) {
__HAL_RCC_TIM4_CLK_DISABLE();
}
#endif
#ifdef TIM5
if (mask & (1 << 4)) {
__HAL_RCC_TIM5_CLK_DISABLE();
}
#endif
2021-03-15 09:57:36 -04:00
// 6 and 7 are reserved ADC timers
2020-06-25 17:27:01 -04:00
#ifdef TIM8
if (mask & (1 << 7)) {
__HAL_RCC_TIM8_CLK_DISABLE();
}
#endif
#ifdef TIM9
if (mask & (1 << 8)) {
__HAL_RCC_TIM9_CLK_DISABLE();
}
#endif
#ifdef TIM10
if (mask & (1 << 9)) {
__HAL_RCC_TIM10_CLK_DISABLE();
}
#endif
#ifdef TIM11
if (mask & (1 << 10)) {
__HAL_RCC_TIM11_CLK_DISABLE();
}
#endif
#ifdef TIM12
if (mask & (1 << 11)) {
__HAL_RCC_TIM12_CLK_DISABLE();
}
#endif
#ifdef TIM13
if (mask & (1 << 12)) {
__HAL_RCC_TIM13_CLK_DISABLE();
}
#endif
#ifdef TIM14
if (mask & (1 << 13)) {
__HAL_RCC_TIM14_CLK_DISABLE();
}
#endif
}
STATIC void callback_router(size_t index) {
if (stm_timer_callback[index - 1]) {
(*stm_timer_callback[index - 1])();
2020-06-25 17:27:01 -04:00
}
}
void TIM1_CC_IRQHandler(void) { // Advanced timer
2020-06-25 17:27:01 -04:00
callback_router(1);
}
void TIM2_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(2);
}
void TIM3_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(3);
}
void TIM4_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(4);
}
void TIM5_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(5);
}
void TIM6_DAC_IRQHandler(void) { // Basic timer (DAC)
2020-06-25 17:27:01 -04:00
callback_router(6);
}
void TIM7_IRQHandler(void) { // Basic timer
2020-06-25 17:27:01 -04:00
callback_router(7);
}
void TIM8_CC_IRQHandler(void) { // Advanced timer
2020-06-25 17:27:01 -04:00
callback_router(8);
}
// Advanced timer interrupts are currently unused.
void TIM1_BRK_TIM9_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(9);
}
void TIM1_UP_TIM10_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(10);
}
void TIM1_TRG_COM_TIM11_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(11);
}
void TIM8_BRK_TIM12_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(12);
}
void TIM8_UP_TIM13_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(13);
}
void TIM8_TRG_COM_TIM14_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(14);
}
#if (CPY_STM32H7)
void TIM15_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(15);
}
void TIM16_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(16);
}
void TIM17_IRQHandler(void) {
2020-06-25 17:27:01 -04:00
callback_router(17);
}
#endif
2020-07-16 17:11:24 -04:00
#endif