diff options
Diffstat (limited to 'stm/audio.c')
-rw-r--r-- | stm/audio.c | 86 |
1 files changed, 67 insertions, 19 deletions
diff --git a/stm/audio.c b/stm/audio.c index c1a0c84747..343ff7c19d 100644 --- a/stm/audio.c +++ b/stm/audio.c @@ -45,13 +45,26 @@ void audio_drain(void) { } } +/******************************************************************************/ +// Micro Python bindings + +typedef struct _pyb_audio_t { + mp_obj_base_t base; + int dac_id; // 1 or 2 +} pyb_audio_t; + // direct access to DAC -mp_obj_t pyb_audio_dac(mp_obj_t val) { - DAC_SetChannel2Data(DAC_Align_8b_R, mp_obj_get_int(val)); +mp_obj_t pyb_audio_dac(mp_obj_t self_in, mp_obj_t val) { + pyb_audio_t *self = self_in; + if (self->dac_id == 1) { + DAC_SetChannel1Data(DAC_Align_8b_R, mp_obj_get_int(val)); + } else { + DAC_SetChannel2Data(DAC_Align_8b_R, mp_obj_get_int(val)); + } return mp_const_none; } -mp_obj_t pyb_audio_is_full(void) { +mp_obj_t pyb_audio_is_full(mp_obj_t self_in) { if (audio_is_full()) { return mp_const_true; } else { @@ -59,42 +72,77 @@ mp_obj_t pyb_audio_is_full(void) { } } -mp_obj_t pyb_audio_fill(mp_obj_t val) { +mp_obj_t pyb_audio_fill(mp_obj_t self_in, mp_obj_t val) { audio_fill(mp_obj_get_int(val)); return mp_const_none; } -void audio_init(void) { +STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_audio_dac_obj, pyb_audio_dac); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_audio_is_full_obj, pyb_audio_is_full); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_audio_fill_obj, pyb_audio_fill); + +STATIC const mp_method_t pyb_audio_methods[] = { + { "dac", &pyb_audio_dac_obj }, + { "is_full", &pyb_audio_is_full_obj }, + { "fill", &pyb_audio_fill_obj }, + { NULL, NULL }, +}; + +STATIC const mp_obj_type_t pyb_audio_type = { + { &mp_type_type }, + .name = MP_QSTR_, + .methods = pyb_audio_methods, +}; + +STATIC const pyb_audio_t pyb_audio_channel_1 = {{&pyb_audio_type}, 1}; +STATIC const pyb_audio_t pyb_audio_channel_2 = {{&pyb_audio_type}, 2}; + +// create the audio object +// currently support either DAC1 on X5 (id = 1) or DAC2 on X6 (id = 2) + +STATIC mp_obj_t pyb_Audio(mp_obj_t id) { // DAC peripheral clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); - // DAC channel 2 (DAC_OUT2 = PA.5) configuration + int dac_id = mp_obj_get_int(id); + uint pin; + uint channel; + mp_obj_t dac_obj; + + if (dac_id == 1) { + pin = GPIO_Pin_4; + channel = DAC_Channel_1; + dac_obj = (mp_obj_t)&pyb_audio_channel_1; + } else { + pin = GPIO_Pin_5; + channel = DAC_Channel_2; + dac_obj = (mp_obj_t)&pyb_audio_channel_2; + } + + // DAC channel configuration GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; + GPIO_InitStructure.GPIO_Pin = pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); - // DAC channel2 Configuration + // DAC channel Configuration DAC_InitTypeDef DAC_InitStructure; DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; - DAC_Init(DAC_Channel_2, &DAC_InitStructure); + DAC_Init(channel, &DAC_InitStructure); - // Enable DAC Channel2 - DAC_Cmd(DAC_Channel_2, ENABLE); + // Enable DAC Channel + DAC_Cmd(channel, ENABLE); - // from now on use DAC_SetChannel2Data to trigger a conversion + // from now on use DAC_SetChannel[12]Data to trigger a conversion sample_buf_in = 0; sample_buf_out = 0; - // enable interrupt - // Python interface - mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("audio")); - rt_store_attr(m, QSTR_FROM_STR_STATIC("dac"), rt_make_function_n(1, pyb_audio_dac)); - rt_store_attr(m, QSTR_FROM_STR_STATIC("is_full"), rt_make_function_n(0, pyb_audio_is_full)); - rt_store_attr(m, QSTR_FROM_STR_STATIC("fill"), rt_make_function_n(1, pyb_audio_fill)); - rt_store_name(QSTR_FROM_STR_STATIC("audio"), m); + // return static object + return dac_obj; } + +MP_DEFINE_CONST_FUN_OBJ_1(pyb_Audio_obj, pyb_Audio); |