Commit 66b96822 authored by Damien George's avatar Damien George
Browse files

stmhal: Add option to free up TIM3 from USB VCP polling.

This is a hack to free up TIM3 so that it can be used by the user.
Instead we use the PVD irq to call the USB VCP polling function, and
trigger it from SysTick (so SysTick itself does not do any processing).

The feature is enabled for pyboard lite only, since it lacks timers.
parent 9aaf888b
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
#define MICROPY_HW_LED2 (pin_A14) // green #define MICROPY_HW_LED2 (pin_A14) // green
#define MICROPY_HW_LED3 (pin_A15) // yellow #define MICROPY_HW_LED3 (pin_A15) // yellow
#define MICROPY_HW_LED4 (pin_B4) // blue #define MICROPY_HW_LED4 (pin_B4) // blue
#define MICROPY_HW_LED4_PWM (1) #define MICROPY_HW_LED4_PWM (0) // TIM3 is now a user timer
#define MICROPY_HW_LED_OTYPE (GPIO_MODE_OUTPUT_PP) #define MICROPY_HW_LED_OTYPE (GPIO_MODE_OUTPUT_PP)
#define MICROPY_HW_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask) #define MICROPY_HW_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask)
#define MICROPY_HW_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask) #define MICROPY_HW_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask)
...@@ -77,6 +77,7 @@ ...@@ -77,6 +77,7 @@
// USB config // USB config
#define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9) #define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9)
#define MICROPY_HW_USE_ALT_IRQ_FOR_CDC (1)
// MMA accelerometer config // MMA accelerometer config
#define MICROPY_HW_MMA_AVDD_PIN (pin_A10) #define MICROPY_HW_MMA_AVDD_PIN (pin_A10)
...@@ -393,7 +393,12 @@ int main(void) { ...@@ -393,7 +393,12 @@ int main(void) {
// basic sub-system init // basic sub-system init
pendsv_init(); pendsv_init();
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
HAL_NVIC_SetPriority(PVD_IRQn, 6, 0); // same priority as USB
HAL_NVIC_EnableIRQ(PVD_IRQn);
#else
timer_tim3_init(); timer_tim3_init();
#endif
led_init(); led_init();
#if MICROPY_HW_HAS_SWITCH #if MICROPY_HW_HAS_SWITCH
switch_init0(); switch_init0();
......
...@@ -277,6 +277,12 @@ void SysTick_Handler(void) { ...@@ -277,6 +277,12 @@ void SysTick_Handler(void) {
// be generalised in the future then a dispatch table can be used as // be generalised in the future then a dispatch table can be used as
// follows: ((void(*)(void))(systick_dispatch[uwTick & 0xf]))(); // follows: ((void(*)(void))(systick_dispatch[uwTick & 0xf]))();
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
if (((uwTick) & 7) == 4) { // every 8ms
NVIC->STIR = PVD_IRQn;
}
#endif
if (STORAGE_IDLE_TICK(uwTick)) { if (STORAGE_IDLE_TICK(uwTick)) {
NVIC->STIR = FLASH_IRQn; NVIC->STIR = FLASH_IRQn;
} }
...@@ -425,6 +431,10 @@ void EXTI15_10_IRQHandler(void) { ...@@ -425,6 +431,10 @@ void EXTI15_10_IRQHandler(void) {
} }
void PVD_IRQHandler(void) { void PVD_IRQHandler(void) {
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
extern void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void);
USBD_CDC_HAL_TIM_PeriodElapsedCallback();
#endif
Handle_EXTI_Irq(EXTI_PVD_OUTPUT); Handle_EXTI_Irq(EXTI_PVD_OUTPUT);
} }
...@@ -465,7 +475,11 @@ void TIM2_IRQHandler(void) { ...@@ -465,7 +475,11 @@ void TIM2_IRQHandler(void) {
} }
void TIM3_IRQHandler(void) { void TIM3_IRQHandler(void) {
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
timer_irq_handler(3);
#else
HAL_TIM_IRQHandler(&TIM3_Handle); HAL_TIM_IRQHandler(&TIM3_Handle);
#endif
} }
void TIM4_IRQHandler(void) { void TIM4_IRQHandler(void) {
......
...@@ -250,9 +250,12 @@ TIM_HandleTypeDef *timer_tim6_init(uint freq) { ...@@ -250,9 +250,12 @@ TIM_HandleTypeDef *timer_tim6_init(uint freq) {
// Interrupt dispatch // Interrupt dispatch
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
#if !defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
if (htim == &TIM3_Handle) { if (htim == &TIM3_Handle) {
USBD_CDC_HAL_TIM_PeriodElapsedCallback(); USBD_CDC_HAL_TIM_PeriodElapsedCallback();
} else if (htim == &TIM5_Handle) { } else
#endif
if (htim == &TIM5_Handle) {
servo_timer_irq_callback(); servo_timer_irq_callback();
} }
} }
...@@ -653,7 +656,11 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t ...@@ -653,7 +656,11 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
switch (tim->tim_id) { switch (tim->tim_id) {
case 1: tim->tim.Instance = TIM1; tim->irqn = TIM1_UP_TIM10_IRQn; break; case 1: tim->tim.Instance = TIM1; tim->irqn = TIM1_UP_TIM10_IRQn; break;
case 2: tim->tim.Instance = TIM2; tim->irqn = TIM2_IRQn; tim->is_32bit = true; break; case 2: tim->tim.Instance = TIM2; tim->irqn = TIM2_IRQn; tim->is_32bit = true; break;
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
case 3: tim->tim.Instance = TIM3; tim->irqn = TIM3_IRQn; break;
#else
case 3: nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Timer 3 is for internal use only")); // TIM3 used for low-level stuff; go via regs if necessary case 3: nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Timer 3 is for internal use only")); // TIM3 used for low-level stuff; go via regs if necessary
#endif
case 4: tim->tim.Instance = TIM4; tim->irqn = TIM4_IRQn; break; case 4: tim->tim.Instance = TIM4; tim->irqn = TIM4_IRQn; break;
case 5: tim->tim.Instance = TIM5; tim->irqn = TIM5_IRQn; tim->is_32bit = true; break; case 5: tim->tim.Instance = TIM5; tim->irqn = TIM5_IRQn; tim->is_32bit = true; break;
#if defined(TIM6) #if defined(TIM6)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment