stmhal: Exclude code for those timers that don't exist in the hardware.

This commit is contained in:
Damien George 2015-04-18 15:54:15 +01:00
parent 7d6595fd18
commit c92c7a69fd
3 changed files with 45 additions and 2 deletions

View File

@ -212,6 +212,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
/// print(val) # print the value out
///
/// This function does not allocate any memory.
#if defined(TIM6)
STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) {
pyb_obj_adc_t *self = self_in;
@ -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_
return mp_obj_new_int(bufinfo.len);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(adc_read_timed_obj, adc_read_timed);
#endif
STATIC const mp_map_elem_t adc_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj},
#if defined(TIM6)
{ MP_OBJ_NEW_QSTR(MP_QSTR_read_timed), (mp_obj_t)&adc_read_timed_obj},
#endif
};
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);

View File

@ -77,6 +77,7 @@ void dac_init(void) {
HAL_DAC_Init(&DAC_Handle);
}
#if defined(TIM6)
STATIC void TIM6_Config(uint freq) {
// Init TIM6 at the required frequency (in Hz)
timer_tim6_init(freq);
@ -90,6 +91,7 @@ STATIC void TIM6_Config(uint freq) {
// TIM6 start counter
HAL_TIM_Base_Start(&TIM6_Handle);
}
#endif
/******************************************************************************/
// 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
return dac;
}
#if defined(TIM6)
/// \method noise(freq)
/// Generate a pseudo-random noise signal. A new random sample is written
/// 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) {
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_noise_obj, pyb_dac_noise);
#endif
#if defined(TIM6)
/// \method triangle(freq)
/// Generate a triangle wave. The value on the DAC output changes at
/// 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) {
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_triangle_obj, pyb_dac_triangle);
#endif
/// \method write(value)
/// 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) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_write_obj, pyb_dac_write);
#if defined(TIM6)
/// \method write_timed(data, freq, *, mode=DAC.NORMAL)
/// Initiates a burst of RAM to DAC using a DMA transfer.
/// 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
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_dac_write_timed_obj, 1, pyb_dac_write_timed);
#endif
STATIC const mp_map_elem_t pyb_dac_locals_dict_table[] = {
// instance methods
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_dac_write_obj },
#if defined(TIM6)
{ MP_OBJ_NEW_QSTR(MP_QSTR_noise), (mp_obj_t)&pyb_dac_noise_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_triangle), (mp_obj_t)&pyb_dac_triangle_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_dac_write_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_write_timed), (mp_obj_t)&pyb_dac_write_timed_obj },
#endif
// class constants
{ MP_OBJ_NEW_QSTR(MP_QSTR_NORMAL), MP_OBJ_NEW_SMALL_INT(DMA_NORMAL) },

View File

@ -223,6 +223,7 @@ void timer_tim5_init(void) {
HAL_TIM_PWM_Init(&TIM5_Handle);
}
#if defined(TIM6)
// Init TIM6 with a counter-overflow at the given frequency (given in Hz)
// TIM6 is used by the DAC and ADC for auto sampling at a given frequency
// This function inits but does not start the timer
@ -247,6 +248,7 @@ void timer_tim6_init(uint freq) {
TIM6_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM6
HAL_TIM_Base_Init(&TIM6_Handle);
}
#endif
// Interrupt dispatch
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
case 3: __TIM3_CLK_ENABLE(); break;
case 4: __TIM4_CLK_ENABLE(); break;
case 5: __TIM5_CLK_ENABLE(); break;
#if defined(TIM6)
case 6: __TIM6_CLK_ENABLE(); break;
#endif
#if defined(TIM7)
case 7: __TIM7_CLK_ENABLE(); break;
#endif
#if defined(TIM8)
case 8: __TIM8_CLK_ENABLE(); break;
#endif
case 9: __TIM9_CLK_ENABLE(); break;
case 10: __TIM10_CLK_ENABLE(); break;
case 11: __TIM11_CLK_ENABLE(); break;
#if defined(TIM12)
case 12: __TIM12_CLK_ENABLE(); break;
#endif
#if defined(TIM13)
case 13: __TIM13_CLK_ENABLE(); break;
#endif
#if defined(TIM14)
case 14: __TIM14_CLK_ENABLE(); break;
#endif
}
// 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
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 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;
#if defined(TIM6)
case 6: tim->tim.Instance = TIM6; tim->irqn = TIM6_DAC_IRQn; break;
#endif
#if defined(TIM7)
case 7: tim->tim.Instance = TIM7; tim->irqn = TIM7_IRQn; break;
#endif
#if defined(TIM8)
case 8: tim->tim.Instance = TIM8; tim->irqn = TIM8_UP_TIM13_IRQn; break;
#endif
case 9: tim->tim.Instance = TIM9; tim->irqn = TIM1_BRK_TIM9_IRQn; break;
case 10: tim->tim.Instance = TIM10; tim->irqn = TIM1_UP_TIM10_IRQn; break;
case 11: tim->tim.Instance = TIM11; tim->irqn = TIM1_TRG_COM_TIM11_IRQn; break;
#if defined(TIM12)
case 12: tim->tim.Instance = TIM12; tim->irqn = TIM8_BRK_TIM12_IRQn; break;
#endif
#if defined(TIM13)
case 13: tim->tim.Instance = TIM13; tim->irqn = TIM8_UP_TIM13_IRQn; break;
#endif
#if defined(TIM14)
case 14: tim->tim.Instance = TIM14; tim->irqn = TIM8_TRG_COM_TIM14_IRQn; break;
#endif
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d does not exist", tim->tim_id));
}
@ -986,7 +1012,10 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
&& self->tim.Instance != TIM3
&& self->tim.Instance != TIM4
&& self->tim.Instance != TIM5
&& self->tim.Instance != TIM8 ) {
#if defined(TIM8)
&& self->tim.Instance != TIM8
#endif
) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "encoder not supported on timer %d", self->tim_id));
}