diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-08 15:20:36 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-08 15:20:36 +0000 |
commit | 8fd7d7e102372a3fe067030aa0f2049f744b1567 (patch) | |
tree | e27fb73e638460a0da3e88163a7a247f19d1b981 | |
parent | 5260810d70311b9baaea249ef4a8bcd80f56f5dc (diff) | |
parent | 360b25ab0f8df087d6ad986e4cd6fa2cff1d256b (diff) | |
download | micropython-8fd7d7e102372a3fe067030aa0f2049f744b1567.tar.gz micropython-8fd7d7e102372a3fe067030aa0f2049f744b1567.zip |
Merge branch 'master' of github.com:micropython/micropython
-rw-r--r-- | stm/audio.c | 86 | ||||
-rw-r--r-- | stm/audio.h | 2 | ||||
-rw-r--r-- | stm/boards/PYBOARD4/mpconfigboard.h | 2 | ||||
-rw-r--r-- | stm/main.c | 20 | ||||
-rw-r--r-- | stm/qstrdefsport.h | 1 |
5 files changed, 85 insertions, 26 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); diff --git a/stm/audio.h b/stm/audio.h index be5f481557..9b1f124a33 100644 --- a/stm/audio.h +++ b/stm/audio.h @@ -1 +1 @@ -void audio_init(void); +MP_DECLARE_CONST_FUN_OBJ(pyb_Audio_obj); diff --git a/stm/boards/PYBOARD4/mpconfigboard.h b/stm/boards/PYBOARD4/mpconfigboard.h index 8bc8ee2ae6..155901da1b 100644 --- a/stm/boards/PYBOARD4/mpconfigboard.h +++ b/stm/boards/PYBOARD4/mpconfigboard.h @@ -12,7 +12,7 @@ #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_TIMER (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 #define USRSW_PIN (pin_B3) diff --git a/stm/main.c b/stm/main.c index 5ab17cd3db..e5f5d4e763 100644 --- a/stm/main.c +++ b/stm/main.c @@ -110,6 +110,16 @@ mp_obj_t pyb_delay(mp_obj_t count) { return mp_const_none; } +mp_obj_t pyb_udelay(mp_obj_t usec) { + uint32_t count = 0; + const uint32_t utime = (168 * mp_obj_get_int(usec) / 5); + for (;;) { + if (++count > utime) { + return mp_const_none; + } + } +} + void fatality(void) { led_state(PYB_LED_R1, 1); led_state(PYB_LED_G1, 1); @@ -387,11 +397,6 @@ soft_reset: servo_init(); #endif -#if MICROPY_HW_ENABLE_AUDIO - // audio - audio_init(); -#endif - #if MICROPY_HW_ENABLE_TIMER // timer timer_init(); @@ -420,6 +425,7 @@ soft_reset: rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main)); rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync)); rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay)); + rt_store_attr(m, MP_QSTR_udelay, rt_make_function_n(1, pyb_udelay)); #if MICROPY_HW_HAS_SWITCH rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj); #endif @@ -450,6 +456,10 @@ soft_reset: 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)); +#if MICROPY_HW_ENABLE_AUDIO + rt_store_attr(m, qstr_from_str("Audio"), (mp_obj_t)&pyb_Audio_obj); +#endif + pin_map_init(m); gpio_init(m); exti_init(m); diff --git a/stm/qstrdefsport.h b/stm/qstrdefsport.h index 645c2b8c72..2978f8276d 100644 --- a/stm/qstrdefsport.h +++ b/stm/qstrdefsport.h @@ -11,6 +11,7 @@ Q(main) Q(sync) Q(gc) Q(delay) +Q(udelay) Q(switch) Q(SW) Q(servo) |