Skip to content

Commit c92c7a6

Browse files
committed
stmhal: Exclude code for those timers that don't exist in the hardware.
1 parent 7d6595f commit c92c7a6

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

stmhal/adc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
212212
/// print(val) # print the value out
213213
///
214214
/// This function does not allocate any memory.
215+
#if defined(TIM6)
215216
STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) {
216217
pyb_obj_adc_t *self = self_in;
217218

@@ -274,10 +275,13 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
274275
return mp_obj_new_int(bufinfo.len);
275276
}
276277
STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_obj, adc_read_timed);
278+
#endif
277279

278280
STATIC const mp_map_elem_t adc_locals_dict_table[] = {
279281
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj},
282+
#if defined(TIM6)
280283
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_timed), (mp_obj_t)&adc_read_timed_obj},
284+
#endif
281285
};
282286

283287
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);

stmhal/dac.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void dac_init(void) {
7777
HAL_DAC_Init(&DAC_Handle);
7878
}
7979

80+
#if defined(TIM6)
8081
STATIC void TIM6_Config(uint freq) {
8182
// Init TIM6 at the required frequency (in Hz)
8283
timer_tim6_init(freq);
@@ -90,6 +91,7 @@ STATIC void TIM6_Config(uint freq) {
9091
// TIM6 start counter
9192
HAL_TIM_Base_Start(&TIM6_Handle);
9293
}
94+
#endif
9395

9496
/******************************************************************************/
9597
// Micro Python bindings
@@ -167,6 +169,7 @@ STATIC mp_obj_t pyb_dac_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
167169
return dac;
168170
}
169171

172+
#if defined(TIM6)
170173
/// \method noise(freq)
171174
/// Generate a pseudo-random noise signal. A new random sample is written
172175
/// to the DAC output at the given frequency.
@@ -193,7 +196,9 @@ STATIC mp_obj_t pyb_dac_noise(mp_obj_t self_in, mp_obj_t freq) {
193196
return mp_const_none;
194197
}
195198
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_noise_obj, pyb_dac_noise);
199+
#endif
196200

201+
#if defined(TIM6)
197202
/// \method triangle(freq)
198203
/// Generate a triangle wave. The value on the DAC output changes at
199204
/// the given frequency, and the frequence of the repeating triangle wave
@@ -221,6 +226,7 @@ STATIC mp_obj_t pyb_dac_triangle(mp_obj_t self_in, mp_obj_t freq) {
221226
return mp_const_none;
222227
}
223228
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_triangle_obj, pyb_dac_triangle);
229+
#endif
224230

225231
/// \method write(value)
226232
/// Direct access to the DAC output (8 bit only at the moment).
@@ -242,6 +248,7 @@ STATIC mp_obj_t pyb_dac_write(mp_obj_t self_in, mp_obj_t val) {
242248
}
243249
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_write_obj, pyb_dac_write);
244250

251+
#if defined(TIM6)
245252
/// \method write_timed(data, freq, *, mode=DAC.NORMAL)
246253
/// Initiates a burst of RAM to DAC using a DMA transfer.
247254
/// The input data is treated as an array of bytes (8 bit data).
@@ -352,13 +359,16 @@ mp_obj_t pyb_dac_write_timed(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
352359
return mp_const_none;
353360
}
354361
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_dac_write_timed_obj, 1, pyb_dac_write_timed);
362+
#endif
355363

356364
STATIC const mp_map_elem_t pyb_dac_locals_dict_table[] = {
357365
// instance methods
366+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_dac_write_obj },
367+
#if defined(TIM6)
358368
{ MP_OBJ_NEW_QSTR(MP_QSTR_noise), (mp_obj_t)&pyb_dac_noise_obj },
359369
{ MP_OBJ_NEW_QSTR(MP_QSTR_triangle), (mp_obj_t)&pyb_dac_triangle_obj },
360-
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_dac_write_obj },
361370
{ MP_OBJ_NEW_QSTR(MP_QSTR_write_timed), (mp_obj_t)&pyb_dac_write_timed_obj },
371+
#endif
362372

363373
// class constants
364374
{ MP_OBJ_NEW_QSTR(MP_QSTR_NORMAL), MP_OBJ_NEW_SMALL_INT(DMA_NORMAL) },

stmhal/timer.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ void timer_tim5_init(void) {
223223
HAL_TIM_PWM_Init(&TIM5_Handle);
224224
}
225225

226+
#if defined(TIM6)
226227
// Init TIM6 with a counter-overflow at the given frequency (given in Hz)
227228
// TIM6 is used by the DAC and ADC for auto sampling at a given frequency
228229
// This function inits but does not start the timer
@@ -247,6 +248,7 @@ void timer_tim6_init(uint freq) {
247248
TIM6_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM6
248249
HAL_TIM_Base_Init(&TIM6_Handle);
249250
}
251+
#endif
250252

251253
// Interrupt dispatch
252254
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
@@ -587,15 +589,27 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, mp_uint_t n_args, c
587589
case 3: __TIM3_CLK_ENABLE(); break;
588590
case 4: __TIM4_CLK_ENABLE(); break;
589591
case 5: __TIM5_CLK_ENABLE(); break;
592+
#if defined(TIM6)
590593
case 6: __TIM6_CLK_ENABLE(); break;
594+
#endif
595+
#if defined(TIM7)
591596
case 7: __TIM7_CLK_ENABLE(); break;
597+
#endif
598+
#if defined(TIM8)
592599
case 8: __TIM8_CLK_ENABLE(); break;
600+
#endif
593601
case 9: __TIM9_CLK_ENABLE(); break;
594602
case 10: __TIM10_CLK_ENABLE(); break;
595603
case 11: __TIM11_CLK_ENABLE(); break;
604+
#if defined(TIM12)
596605
case 12: __TIM12_CLK_ENABLE(); break;
606+
#endif
607+
#if defined(TIM13)
597608
case 13: __TIM13_CLK_ENABLE(); break;
609+
#endif
610+
#if defined(TIM14)
598611
case 14: __TIM14_CLK_ENABLE(); break;
612+
#endif
599613
}
600614

601615
// set IRQ priority (if not a special timer)
@@ -643,15 +657,27 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
643657
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
644658
case 4: tim->tim.Instance = TIM4; tim->irqn = TIM4_IRQn; break;
645659
case 5: tim->tim.Instance = TIM5; tim->irqn = TIM5_IRQn; tim->is_32bit = true; break;
660+
#if defined(TIM6)
646661
case 6: tim->tim.Instance = TIM6; tim->irqn = TIM6_DAC_IRQn; break;
662+
#endif
663+
#if defined(TIM7)
647664
case 7: tim->tim.Instance = TIM7; tim->irqn = TIM7_IRQn; break;
665+
#endif
666+
#if defined(TIM8)
648667
case 8: tim->tim.Instance = TIM8; tim->irqn = TIM8_UP_TIM13_IRQn; break;
668+
#endif
649669
case 9: tim->tim.Instance = TIM9; tim->irqn = TIM1_BRK_TIM9_IRQn; break;
650670
case 10: tim->tim.Instance = TIM10; tim->irqn = TIM1_UP_TIM10_IRQn; break;
651671
case 11: tim->tim.Instance = TIM11; tim->irqn = TIM1_TRG_COM_TIM11_IRQn; break;
672+
#if defined(TIM12)
652673
case 12: tim->tim.Instance = TIM12; tim->irqn = TIM8_BRK_TIM12_IRQn; break;
674+
#endif
675+
#if defined(TIM13)
653676
case 13: tim->tim.Instance = TIM13; tim->irqn = TIM8_UP_TIM13_IRQn; break;
677+
#endif
678+
#if defined(TIM14)
654679
case 14: tim->tim.Instance = TIM14; tim->irqn = TIM8_TRG_COM_TIM14_IRQn; break;
680+
#endif
655681
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d does not exist", tim->tim_id));
656682
}
657683

@@ -986,7 +1012,10 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
9861012
&& self->tim.Instance != TIM3
9871013
&& self->tim.Instance != TIM4
9881014
&& self->tim.Instance != TIM5
989-
&& self->tim.Instance != TIM8 ) {
1015+
#if defined(TIM8)
1016+
&& self->tim.Instance != TIM8
1017+
#endif
1018+
) {
9901019
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "encoder not supported on timer %d", self->tim_id));
9911020
}
9921021

0 commit comments

Comments
 (0)