stm: Improve DAC (audio) bindings.
This commit is contained in:
parent
6fc765c928
commit
cd000645f8
86
stm/audio.c
86
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
|
// direct access to DAC
|
||||||
mp_obj_t pyb_audio_dac(mp_obj_t val) {
|
mp_obj_t pyb_audio_dac(mp_obj_t self_in, mp_obj_t val) {
|
||||||
DAC_SetChannel2Data(DAC_Align_8b_R, mp_obj_get_int(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;
|
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()) {
|
if (audio_is_full()) {
|
||||||
return mp_const_true;
|
return mp_const_true;
|
||||||
} else {
|
} 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));
|
audio_fill(mp_obj_get_int(val));
|
||||||
return mp_const_none;
|
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
|
// DAC peripheral clock
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
|
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_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_Mode = GPIO_Mode_AN;
|
||||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||||
|
|
||||||
// DAC channel2 Configuration
|
// DAC channel Configuration
|
||||||
DAC_InitTypeDef DAC_InitStructure;
|
DAC_InitTypeDef DAC_InitStructure;
|
||||||
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
|
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
|
||||||
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
|
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
|
||||||
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
|
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
|
||||||
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
|
DAC_Init(channel, &DAC_InitStructure);
|
||||||
|
|
||||||
// Enable DAC Channel2
|
// Enable DAC Channel
|
||||||
DAC_Cmd(DAC_Channel_2, ENABLE);
|
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_in = 0;
|
||||||
sample_buf_out = 0;
|
sample_buf_out = 0;
|
||||||
// enable interrupt
|
|
||||||
|
|
||||||
// Python interface
|
// return static object
|
||||||
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("audio"));
|
return dac_obj;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_Audio_obj, pyb_Audio);
|
||||||
|
@ -1 +1 @@
|
|||||||
void audio_init(void);
|
MP_DECLARE_CONST_FUN_OBJ(pyb_Audio_obj);
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
#define MICROPY_HW_ENABLE_RTC (1)
|
#define MICROPY_HW_ENABLE_RTC (1)
|
||||||
#define MICROPY_HW_ENABLE_TIMER (1)
|
#define MICROPY_HW_ENABLE_TIMER (1)
|
||||||
#define MICROPY_HW_ENABLE_SERVO (1)
|
#define MICROPY_HW_ENABLE_SERVO (1)
|
||||||
#define MICROPY_HW_ENABLE_AUDIO (0)
|
#define MICROPY_HW_ENABLE_AUDIO (1)
|
||||||
|
|
||||||
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
|
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
|
||||||
#define USRSW_PIN (pin_B3)
|
#define USRSW_PIN (pin_B3)
|
||||||
|
@ -387,11 +387,6 @@ soft_reset:
|
|||||||
servo_init();
|
servo_init();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MICROPY_HW_ENABLE_AUDIO
|
|
||||||
// audio
|
|
||||||
audio_init();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if MICROPY_HW_ENABLE_TIMER
|
#if MICROPY_HW_ENABLE_TIMER
|
||||||
// timer
|
// timer
|
||||||
timer_init();
|
timer_init();
|
||||||
@ -450,6 +445,10 @@ soft_reset:
|
|||||||
rt_store_attr(m, MP_QSTR_ADC, (mp_obj_t)&pyb_ADC_obj);
|
rt_store_attr(m, MP_QSTR_ADC, (mp_obj_t)&pyb_ADC_obj);
|
||||||
rt_store_attr(m, qstr_from_str("millis"), rt_make_function_n(0, pyb_millis));
|
rt_store_attr(m, qstr_from_str("millis"), rt_make_function_n(0, pyb_millis));
|
||||||
|
|
||||||
|
#if MICROPY_HW_ENABLE_AUDIO
|
||||||
|
rt_store_attr(m, qstr_from_str("Audio"), (mp_obj_t)&pyb_Audio_obj);
|
||||||
|
#endif
|
||||||
|
|
||||||
pin_map_init(m);
|
pin_map_init(m);
|
||||||
gpio_init(m);
|
gpio_init(m);
|
||||||
exti_init(m);
|
exti_init(m);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user