8000 nucleo: fix ADC support, TIM6 => TIM9 · nuraci/micropython@8effc84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8effc84

Browse files
committed
nucleo: fix ADC support, TIM6 => TIM9
Also disable TIM6,7,8, TIM12,13 and 14 (not avaliable on STM32F4x1). Signed-off-by: Nunzio Raciti <nunzio.raciti@gmail.com>
1 parent 29909f3 commit 8effc84

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

stmhal/adc.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,34 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
223223
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
224224
int typesize = mp_binary_get_size('@', bufinfo.typecode, NULL);
225225

226+
#if !defined(STM32F401xE)
226227
// Init TIM6 at the required frequency (in Hz)
227228
timer_tim6_init(mp_obj_get_int(freq_in));
228229

229230
// Start timer
230231
HAL_TIM_Base_Start(&TIM6_Handle);
232+
#else
233+
// Init TIM9 at the required frequency (in Hz)
234+
timer_tim9_init(mp_obj_get_int(freq_in));
235+
236+
// Start timer
237+
HAL_TIM_Base_Start(&TIM9_Handle);
238+
#endif
231239

232240
// This uses the timer in polling mode to do the sampling
233241
// We could use DMA, but then we can't convert the values correctly for the buffer
234242
adc_config_channel(self);
235243
for (uint index = 0; index < bufinfo.len; index++) {
236244
// Wait for the timer to trigger
245+
#if !defined(STM32F401xE)
237246
while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) {
238247
}
239248
__HAL_TIM_CLEAR_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE);
249+
#else
250+
while (__HAL_TIM_GET_FLAG(&TIM9_Handle, TIM_FLAG_UPDATE) == RESET) {
251+
}
252+
__HAL_TIM_CLEAR_FLAG(&TIM9_Handle, TIM_FLAG_UPDATE);
253+
#endif
240254
uint value = adc_read_channel(&self->handle);
241255
if (typesize == 1) {
242256
value >>= 4;
@@ -245,7 +259,11 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
245259
}
246260

247261
// Stop timer
262+
#if !defined(STM32F401xE)
248263
HAL_TIM_Base_Stop(&TIM6_Handle);
264+
#else
265+
HAL_TIM_Base_Stop(&TIM9_Handle);
266+
#endif
249267

250268
return mp_obj_new_int(bufinfo.len);
251269
}

stmhal/timer.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ typedef struct _pyb_timer_obj_t {
143143

144144
TIM_HandleTypeDef TIM3_Handle;
145145
TIM_HandleTypeDef TIM5_Handle;
146+
#if !defined(STM32F401xE)
146147
TIM_HandleTypeDef TIM6_Handle;
148+
#else
149+
TIM_HandleTypeDef TIM9_Handle;
150+
#endif
147151

148152
// Used to divide down TIM3 and periodically call the flash storage IRQ
149153
STATIC uint32_t tim3_counter = 0;
@@ -221,7 +225,7 @@ void timer_tim5_init(void) {
221225

222226
HAL_TIM_PWM_Init(&TIM5_Handle);
223227
}
224-
228+
#if !defined(STM32F401xE)
225229
// Init TIM6 with a counter-overflow at the given frequency (given in Hz)
226230
// TIM6 is used by the DAC and ADC for auto sampling at a given frequency
227231
// This function inits but does not start the timer
@@ -246,11 +250,40 @@ void timer_tim6_init(uint freq) {
246250
TIM6_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM6
247251
HAL_TIM_Base_Init(&TIM6_Handle);
248252
}
253+
#else
254+
// Init TIM9 with a counter-overflow at the given frequency (given in Hz)
255+
// TIM9 is used by ADC for auto sampling at a given frequency
256+
// This function inits but does not start the timer
257+
void timer_tim9_init(uint freq) {
258+
// TIM9 clock enable
259+
__TIM9_CLK_ENABLE();
260+
261+
// Timer runs at SystemCoreClock / 2
262+
// Compute the prescaler value so TIM9 triggers at freq-Hz
263+
uint32_t period = MAX(1, timer_get_source_freq(9) / freq);
264+
uint32_t prescaler = 1;
265+
while (period > 0xffff) {
266+
period >>= 1;
267+
prescaler <<= 1;
268+
}
269+
270+
// Time base clock configuration
271+
TIM9_Handle.Instance = TIM9;
272+
TIM9_Handle.Init.Period = period - 1;
273+
TIM9_Handle.Init.Prescaler = prescaler - 1;
274+
TIM9_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // unused for TIM9
275+
TIM9_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM9
276+
HAL_TIM_Base_Init(&TIM9_Handle);
277+
}
278+
279+
#endif
249280

250281
// Interrupt dispatch
251282
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
252283
if (htim == &TIM3_Handle) {
284+
#if !defined(STM32F401xE)
253285
USBD_CDC_HAL_TIM_PeriodElapsedCallback();
286+
#endif
254287

255288
// Periodically raise a flash IRQ for the flash storage controller
256289
if (tim3_counter++ >= 500 / USBD_CDC_POLLING_INTERVAL) {
@@ -585,15 +618,19 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, mp_uint_t n_args, c
585618
case 3: __TIM3_CLK_ENABLE(); break;
586619
case 4: __TIM4_CLK_ENABLE(); break;
587620
case 5: __TIM5_CLK_ENABLE(); break;
621+
#if !defined(STM32F401xE)
588622
case 6: __TIM6_CLK_ENABLE(); break;
589623
case 7: __TIM7_CLK_ENABLE(); break;
590624
case 8: __TIM8_CLK_ENABLE(); break;
625+
#endif
591626
case 9: __TIM9_CLK_ENABLE(); break;
592627
case 10: __TIM10_CLK_ENABLE(); break;
593628
case 11: __TIM11_CLK_ENABLE(); break;
629+
#if !defined(STM32F401xE)
594630
case 12: __TIM12_CLK_ENABLE(); break;
595631
case 13: __TIM13_CLK_ENABLE(); break;
596632
case 14: __TIM14_CLK_ENABLE(); break;
633+
#endif
597634
}
598635

599636
// set IRQ priority (if not a special timer)
@@ -641,15 +678,19 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
641678
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
642679
case 4: tim->tim.Instance = TIM4; tim->irqn = TIM4_IRQn; break;
643680
case 5: tim->tim.Instance = TIM5; tim->irqn = TIM5_IRQn; tim->is_32bit = true; break;
681+
#if !defined(STM32F401xE)
644682
case 6: tim->tim.Instance = TIM6; tim->irqn = TIM6_DAC_IRQn; break;
645683
case 7: tim->tim.Instance = TIM7; tim->irqn = TIM7_IRQn; break;
646684
case 8: tim->tim.Instance = TIM8; tim->irqn = TIM8_UP_TIM13_IRQn; break;
685+
#endif
647686
case 9: tim->tim.Instance = TIM9; tim->irqn = TIM1_BRK_TIM9_IRQn; break;
648687
case 10: tim->tim.Instance = TIM10; tim->irqn = TIM1_UP_TIM10_IRQn; break;
649688
case 11: tim->tim.Instance = TIM11; tim->irqn = TIM1_TRG_COM_TIM11_IRQn; break;
689+
#if !defined(STM32F401xE)
650690
case 12: tim->tim.Instance = TIM12; tim->irqn = TIM8_BRK_TIM12_IRQn; break;
651691
case 13: tim->tim.Instance = TIM13; tim->irqn = TIM8_UP_TIM13_IRQn; break;
652692
case 14: tim->tim.Instance = TIM14; tim->irqn = TIM8_TRG_COM_TIM14_IRQn; break;
693+
#endif
653694
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d does not exist", tim->tim_id));
654695
}
655696

stmhal/timer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@
3131

3232
extern TIM_HandleTypeDef TIM3_Handle;
3333
extern TIM_HandleTypeDef TIM5_Handle;
34+
#if !defined(STM32F401xE)
3435
extern TIM_HandleTypeDef TIM6_Handle;
36+
#else
37+
extern TIM_HandleTypeDef TIM9_Handle;
38+
#endif
3539

3640
extern const mp_obj_type_t pyb_timer_type;
3741

3842
void timer_init0(void);
3943
void timer_tim3_init(void);
4044
void timer_tim5_init(void);
45+
#if !defined(STM32F401xE)
4146
void timer_tim6_init(uint freq);
47+
#else
48+
void timer_tim9_init(uint freq);
49+
#endif
4250

4351
void timer_deinit(void);
4452

0 commit comments

Comments
 (0)
0