8000 stmhal: Make TIM3 available for use by the user. · lispmeister/micropython@ea89b80 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea89b80

Browse files
committed
stmhal: Make TIM3 available for use by the user.
TIM3 is no longer used by USB CDC for triggering outgoing data, so we can now make it available to the user. PWM fading on LED(4) is now gone, but will be reinstated in a new way.
1 parent d363133 commit ea89b80

File tree

8 files changed

+2
-138
lines changed

8 files changed

+2
-138
lines changed

stmhal/boards/PYBLITEV10/mpconfigboard.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282

8383
// USB config
8484
#define MICROPY_HW_USB_VBUS_DETECT_PIN (pin_A9)
85-
#define MICROPY_HW_USE_ALT_IRQ_FOR_CDC (1)
8685

8786
// MMA accelerometer config
8887
#define MICROPY_HW_MMA_AVDD_PIN (pin_A10)

stmhal/led.c

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -83,46 +83,12 @@ void led_init(void) {
8383
GPIO_InitStructure.Pin = led_pin->pin_mask;
8484
HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure);
8585
}
86-
87-
#if MICROPY_HW_LED4_PWM
88-
// LED4 (blue) is on PB4 which is TIM3_CH1
89-
// we use PWM on this channel to fade the LED
90-
91-
// LED3 (yellow) is on PA15 which has TIM2_CH1, so we could PWM that as well
92-
93-
// GPIO configuration
94-
GPIO_InitStructure.Pin = MICROPY_HW_LED4.pin_mask;
95-
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
96-
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
97-
GPIO_InitStructure.Pull = GPIO_NOPULL;
98-
GPIO_InitStructure.Alternate = GPIO_AF2_TIM3;
99-
HAL_GPIO_Init(MICROPY_HW_LED4.gpio, &GPIO_InitStructure);
100-
101-
// PWM mode configuration
102-
TIM_OC_InitTypeDef oc_init;
103-
oc_init.OCMode = TIM_OCMODE_PWM1;
104-
oc_init.Pulse = 0; // off
105-
oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
106-
oc_init.OCFastMode = TIM_OCFAST_DISABLE;
107-
HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &oc_init, TIM_CHANNEL_1);
108-
109-
// start PWM
110-
TIM_CCxChannelCmd(TIM3, TIM_CHANNEL_1, TIM_CCx_ENABLE);
111-
#endif
11286
}
11387

11488
void led_state(pyb_led_t led, int state) {
11589
if (led < 1 || led > NUM_LEDS) {
11690
return;
11791
}
118-
if (MICROPY_HW_LED4_PWM && led == 4) {
119-
if (state) {
120-
TIM3->CCR1 = 0xffff;
121-
} else {
122-
TIM3->CCR1 = 0;
123-
}
124-
return;
125-
}
12692
const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
12793
//printf("led_state(%d,%d)\n", led, state);
12894
if (state == 0) {
@@ -139,15 +105,6 @@ void led_toggle(pyb_led_t led) {
139105
return;
140106
}
141107

142-
if (MICROPY_HW_LED4_PWM && led == 4) {
143-
if (TIM3->CCR1 == 0) {
144-
TIM3->CCR1 = 0xffff;
145-
} else {
146-
TIM3->CCR1 = 0;
147-
}
148-
return;
149-
}
150-
151108
// toggle the output data register to toggle the LED state
152109
const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
153110
led_pin->gpio->ODR ^= led_pin->pin_mask;
@@ -158,14 +115,6 @@ int led_get_intensity(pyb_led_t led) {
158115
return 0;
159116
}
160117

161-
if (MICROPY_HW_LED4_PWM && led == 4) {
162-
mp_uint_t i = (TIM3->CCR1 * 255 + (USBD_CDC_POLLING_INTERVAL*1000) - 2) / ((USBD_CDC_POLLING_INTERVAL*1000) - 1);
163-
if (i > 255) {
164-
i = 255;
165-
}
166-
return i;
167-
}
168-
169118
const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
170119
GPIO_TypeDef *gpio = led_pin->gpio;
171120

@@ -180,19 +129,6 @@ int led_get_intensity(pyb_led_t led) {
180129
}
181130

182131
void led_set_intensity(pyb_led_t led, mp_int_t intensity) {
183-
if (MICROPY_HW_LED4_PWM && led == 4) {
184-
// set intensity using PWM pulse width
185-
if (intensity < 0) {
186-
intensity = 0;
187-
} else if (intensity >= 255) {
188-
intensity = 0xffff;
189-
} else {
190-
intensity = intensity * ((USBD_CDC_POLLING_INTERVAL*1000) - 1) / 255;
191-
}
192-
TIM3->CCR1 = intensity;
193-
return;
194-
}
195-
196132
// intensity not supported for this LED; just turn it on/off
197133
led_state(led, intensity > 0);
198134
}

stmhal/main.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,6 @@ int main(void) {
395395

396396
// basic sub-system init
397397
pendsv_init();
398-
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
399-
HAL_NVIC_SetPriority(PVD_IRQn, 6, 0); // same priority as USB
400-
HAL_NVIC_EnableIRQ(PVD_IRQn);
401-
#else
402-
timer_tim3_init();
403-
#endif
404398
led_init();
405399
#if MICROPY_HW_HAS_SWITCH
406400
switch_init0();

stmhal/modmachine.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
253253
//printf("%lu %lu %lu %lu %lu\n", sysclk_source, m, n, p, q);
254254

255255
// let the USB CDC have a chance to process before we change the clock
256-
HAL_Delay(USBD_CDC_POLLING_INTERVAL + 2);
256+
HAL_Delay(5);
257257

258258
// desired system clock source is in sysclk_source
259259
RCC_ClkInitTypeDef RCC_ClkInitStruct;
@@ -316,9 +316,6 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
316316
}
317317
}
318318

319-
// re-init TIM3 for USB CDC rate
320-
timer_tim3_init();
321-
322319
#if defined(MICROPY_HW_CLK_LAST_FREQ) && MICROPY_HW_CLK_LAST_FREQ
323320
#if defined(MCU_SERIES_F7)
324321
#define FREQ_BKP BKP31R

stmhal/stm32_it.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,6 @@ void SysTick_Handler(void) {
278278
// be generalised in the future then a dispatch table can be used as
279279
// follows: ((void(*)(void))(systick_dispatch[uwTick & 0xf]))();
280280

281-
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
282-
if (((uwTick) & 7) == 4) { // every 8ms
283-
NVIC->STIR = PVD_IRQn;
284-
}
285-
#endif
286-
287281
if (STORAGE_IDLE_TICK(uwTick)) {
288282
NVIC->STIR = FLASH_IRQn;
289283
}
@@ -479,8 +473,6 @@ void EXTI15_10_IRQHandler(void) {
479473

480474
void PVD_IRQHandler(void) {
481475
IRQ_ENTER(PVD_IRQn);
482-
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
483-
#endif
484476
Handle_EXTI_Irq(EXTI_PVD_OUTPUT);
485477
IRQ_EXIT(PVD_IRQn);
486478
}
@@ -539,11 +531,7 @@ void TIM2_IRQHandler(void) {
539531

540532
void TIM3_IRQHandler(void) {
541533
IRQ_ENTER(TIM3_IRQn);
542-
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
543534
timer_irq_handler(3);
544-
#else
545-
HAL_TIM_IRQHandler(&TIM3_Handle);
546-
#endif
547535
IRQ_EXIT(TIM3_IRQn);
548536
}
549537

stmhal/timer.c

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,14 @@
6868
/// tim.callback(lambda t: ...) # set callback for update interrupt (t=tim instance)
6969
/// tim.callback(None) # clear callback
7070
///
71-
/// *Note:* Timer 3 is reserved for internal use. Timer 5 controls
71+
/// *Note:* Timer 3 is used for fading the blue LED. Timer 5 controls
7272
/// the servo driver, and Timer 6 is used for timed ADC/DAC reading/writing.
7373
/// It is recommended to use the other timers in your programs.
7474

7575
// The timers can be used by multiple drivers, and need a common point for
7676
// the interrupts to be dispatched, so they are all collected here.
7777
//
7878
// TIM3:
79-
// - flash storage controller, to flush the cache
80-
// - USB CDC interface, interval, to check for new data
8179
// - LED 4, PWM to set the LED intensity
8280
//
8381
// TIM5:
@@ -144,7 +142,6 @@ typedef struct _pyb_timer_obj_t {
144142
#define TIMER_CNT_MASK(self) ((self)->is_32bit ? 0xffffffff : 0xffff)
145143
#define TIMER_CHANNEL(self) ((((self)->channel) - 1) << 2)
146144

147-
TIM_HandleTypeDef TIM3_Handle;
148145
TIM_HandleTypeDef TIM5_Handle;
149146
TIM_HandleTypeDef TIM6_Handle;
150147

@@ -171,34 +168,6 @@ void timer_deinit(void) {
171168
}
172169
}
173170

174-
// TIM3 is set-up for the USB CDC interface
175-
void timer_tim3_init(void) {
176-
// set up the timer for USBD CDC
177-
__TIM3_CLK_ENABLE();
178-
179-
TIM3_Handle.Instance = TIM3;
180-
TIM3_Handle.Init.Period = (USBD_CDC_POLLING_INTERVAL*1000) - 1; // TIM3 fires every USBD_CDC_POLLING_INTERVAL ms
181-
TIM3_Handle.Init.Prescaler = timer_get_source_freq(3) / 1000000 - 1; // TIM3 runs at 1MHz
182-
TIM3_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
183-
TIM3_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
184-
HAL_TIM_Base_Init(&TIM3_Handle);
185-
186-
HAL_NVIC_SetPriority(TIM3_IRQn, IRQ_PRI_TIM3, IRQ_SUBPRI_TIM3);
187-
HAL_NVIC_EnableIRQ(TIM3_IRQn);
188-
189-
if (HAL_TIM_Base_Start(&TIM3_Handle) != HAL_OK) {
190-
/* Starting Error */
191-
}
192-
}
193-
194-
/* unused
195-
void timer_tim3_deinit(void) {
196-
// reset TIM3 timer
197-
__TIM3_FORCE_RESET();
198-
__TIM3_RELEASE_RESET();
199-
}
200-
*/
201-
202171
// TIM5 is set-up for the servo controller
203172
// This function inits but does not start the timer
204173
void timer_tim5_init(void) {
@@ -250,10 +219,6 @@ TIM_HandleTypeDef *timer_tim6_init(uint freq) {
250219

251220
// Interrupt dispatch
252221
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
253-
#if !defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
254-
if (htim == &TIM3_Handle) {
255-
} else
256-
#endif
257222
if (htim == &TIM5_Handle) {
258223
servo_timer_irq_callback();
259224
}
@@ -655,11 +620,7 @@ STATIC mp_obj_t pyb_timer_make_new(const mp_obj_type_t *type, mp_uint_t n_args,
655620
switch (tim->tim_id) {
656621
case 1: tim->tim.Instance = TIM1; tim->irqn = TIM1_UP_TIM10_IRQn; break;
657622
case 2: tim->tim.Instance = TIM2; tim->irqn = TIM2_IRQn; tim->is_32bit = true; break;
658-
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
659623
case 3: tim->tim.Instance = TIM3; tim->irqn = TIM3_IRQn; break;
660-
#else
661-
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
662-
#endif
663624
case 4: tim->tim.Instance = TIM4; tim->irqn = TIM4_IRQn; break;
664625
case 5: tim->tim.Instance = TIM5; tim->irqn = TIM5_IRQn; tim->is_32bit = true; break;
665626
#if defined(TIM6)

stmhal/timer.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
// Periodically, the state of the buffer "UserTxBuffer" is checked.
28-
// The period depends on USBD_CDC_POLLING_INTERVAL
29-
// The value is in ms. The max is 65 and the min is 1.
30-
#define USBD_CDC_POLLING_INTERVAL (10)
31-
32-
extern TIM_HandleTypeDef TIM3_Handle;
3327
extern TIM_HandleTypeDef TIM5_Handle;
3428

3529
extern const mp_obj_type_t pyb_timer_type;
3630

3731
void timer_init0(void);
38-
void timer_tim3_init(void);
3932
void timer_tim5_init(void);
4033
TIM_HandleTypeDef *timer_tim6_init(uint freq);
4134

stmhal/usbd_cdc_interface.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ static int8_t CDC_Itf_Init(void)
140140
TIM_Config();
141141
#endif
142142

143-
/*##-4- Start the TIM Base generation in interrupt mode ####################*/
144-
/* Start Channel1 */
145-
__HAL_TIM_ENABLE_IT(&TIM3_Handle, TIM_IT_UPDATE);
146-
147143
/*##-5- Set Application Buffers ############################################*/
148144
USBD_CDC_SetTxBuffer(&hUSBDDevice, UserTxBuffer, 0);
149145
USBD_CDC_SetRxBuffer(&hUSBDDevice, UserRxBuffer);

0 commit comments

Comments
 (0)
0