Skip to content

Commit 8fd7d7e

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 5260810 + 360b25a commit 8fd7d7e

5 files changed

Lines changed: 85 additions & 26 deletions

File tree

stm/audio.c

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,56 +45,104 @@ void audio_drain(void) {
4545
}
4646
}
4747

48+
/******************************************************************************/
49+
// Micro Python bindings
50+
51+
typedef struct _pyb_audio_t {
52+
mp_obj_base_t base;
53+
int dac_id; // 1 or 2
54+
} pyb_audio_t;
55+
4856
// direct access to DAC
49-
mp_obj_t pyb_audio_dac(mp_obj_t val) {
50-
DAC_SetChannel2Data(DAC_Align_8b_R, mp_obj_get_int(val));
57+
mp_obj_t pyb_audio_dac(mp_obj_t self_in, mp_obj_t val) {
58+
pyb_audio_t *self = self_in;
59+
if (self->dac_id == 1) {
60+
DAC_SetChannel1Data(DAC_Align_8b_R, mp_obj_get_int(val));
61+
} else {
62+
DAC_SetChannel2Data(DAC_Align_8b_R, mp_obj_get_int(val));
63+
}
5164
return mp_const_none;
5265
}
5366

54-
mp_obj_t pyb_audio_is_full(void) {
67+
mp_obj_t pyb_audio_is_full(mp_obj_t self_in) {
5568
if (audio_is_full()) {
5669
return mp_const_true;
5770
} else {
5871
return mp_const_false;
5972
}
6073
}
6174

62-
mp_obj_t pyb_audio_fill(mp_obj_t val) {
75+
mp_obj_t pyb_audio_fill(mp_obj_t self_in, mp_obj_t val) {
6376
audio_fill(mp_obj_get_int(val));
6477
return mp_const_none;
6578
}
6679

67-
void audio_init(void) {
80+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_audio_dac_obj, pyb_audio_dac);
81+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_audio_is_full_obj, pyb_audio_is_full);
82+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_audio_fill_obj, pyb_audio_fill);
83+
84+
STATIC const mp_method_t pyb_audio_methods[] = {
85+
{ "dac", &pyb_audio_dac_obj },
86+
{ "is_full", &pyb_audio_is_full_obj },
87+
{ "fill", &pyb_audio_fill_obj },
88+
{ NULL, NULL },
89+
};
90+
91+
STATIC const mp_obj_type_t pyb_audio_type = {
92+
{ &mp_type_type },
93+
.name = MP_QSTR_,
94+
.methods = pyb_audio_methods,
95+
};
96+
97+
STATIC const pyb_audio_t pyb_audio_channel_1 = {{&pyb_audio_type}, 1};
98+
STATIC const pyb_audio_t pyb_audio_channel_2 = {{&pyb_audio_type}, 2};
99+
100+
// create the audio object
101+
// currently support either DAC1 on X5 (id = 1) or DAC2 on X6 (id = 2)
102+
103+
STATIC mp_obj_t pyb_Audio(mp_obj_t id) {
68104
// DAC peripheral clock
69105
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
70106

71-
// DAC channel 2 (DAC_OUT2 = PA.5) configuration
107+
int dac_id = mp_obj_get_int(id);
108+
uint pin;
109+
uint channel;
110+
mp_obj_t dac_obj;
111+
112+
if (dac_id == 1) {
113+
pin = GPIO_Pin_4;
114+
channel = DAC_Channel_1;
115+
dac_obj = (mp_obj_t)&pyb_audio_channel_1;
116+
} else {
117+
pin = GPIO_Pin_5;
118+
channel = DAC_Channel_2;
119+
dac_obj = (mp_obj_t)&pyb_audio_channel_2;
120+
}
121+
122+
// DAC channel configuration
72123
GPIO_InitTypeDef GPIO_InitStructure;
73-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
124+
GPIO_InitStructure.GPIO_Pin = pin;
74125
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
75126
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
76127
GPIO_Init(GPIOA, &GPIO_InitStructure);
77128

78-
// DAC channel2 Configuration
129+
// DAC channel Configuration
79130
DAC_InitTypeDef DAC_InitStructure;
80131
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
81132
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
82133
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
83-
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
134+
DAC_Init(channel, &DAC_InitStructure);
84135

85-
// Enable DAC Channel2
86-
DAC_Cmd(DAC_Channel_2, ENABLE);
136+
// Enable DAC Channel
137+
DAC_Cmd(channel, ENABLE);
87138

88-
// from now on use DAC_SetChannel2Data to trigger a conversion
139+
// from now on use DAC_SetChannel[12]Data to trigger a conversion
89140

90141
sample_buf_in = 0;
91142
sample_buf_out = 0;
92-
// enable interrupt
93143

94-
// Python interface
95-
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("audio"));
96-
rt_store_attr(m, QSTR_FROM_STR_STATIC("dac"), rt_make_function_n(1, pyb_audio_dac));
97-
rt_store_attr(m, QSTR_FROM_STR_STATIC("is_full"), rt_make_function_n(0, pyb_audio_is_full));
98-
rt_store_attr(m, QSTR_FROM_STR_STATIC("fill"), rt_make_function_n(1, pyb_audio_fill));
99-
rt_store_name(QSTR_FROM_STR_STATIC("audio"), m);
144+
// return static object
145+
return dac_obj;
100146
}
147+
148+
MP_DEFINE_CONST_FUN_OBJ_1(pyb_Audio_obj, pyb_Audio);

stm/audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
void audio_init(void);
1+
MP_DECLARE_CONST_FUN_OBJ(pyb_Audio_obj);

stm/boards/PYBOARD4/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#define MICROPY_HW_ENABLE_RTC (1)
1313
#define MICROPY_HW_ENABLE_TIMER (1)
1414
#define MICROPY_HW_ENABLE_SERVO (1)
15-
#define MICROPY_HW_ENABLE_AUDIO (0)
15+
#define MICROPY_HW_ENABLE_AUDIO (1)
1616

1717
// USRSW has no pullup or pulldown, and pressing the switch makes the input go low
1818
#define USRSW_PIN (pin_B3)

stm/main.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ mp_obj_t pyb_delay(mp_obj_t count) {
110110
return mp_const_none;
111111
}
112112

113+
mp_obj_t pyb_udelay(mp_obj_t usec) {
114+
uint32_t count = 0;
115+
const uint32_t utime = (168 * mp_obj_get_int(usec) / 5);
116+
for (;;) {
117+
if (++count > utime) {
118+
return mp_const_none;
119+
}
120+
}
121+
}
122+
113123
void fatality(void) {
114124
led_state(PYB_LED_R1, 1);
115125
led_state(PYB_LED_G1, 1);
@@ -387,11 +397,6 @@ int main(void) {
387397
servo_init();
388398
#endif
389399

390-
#if MICROPY_HW_ENABLE_AUDIO
391-
// audio
392-
audio_init();
393-
#endif
394-
395400
#if MICROPY_HW_ENABLE_TIMER
396401
// timer
397402
timer_init();
@@ -420,6 +425,7 @@ int main(void) {
420425
rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main));
421426
rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync));
422427
rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay));
428+
rt_store_attr(m, MP_QSTR_udelay, rt_make_function_n(1, pyb_udelay));
423429
#if MICROPY_HW_HAS_SWITCH
424430
rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj);
425431
#endif
@@ -450,6 +456,10 @@ int main(void) {
450456
rt_store_attr(m, MP_QSTR_ADC, (mp_obj_t)&pyb_ADC_obj);
451457
rt_store_attr(m, qstr_from_str("millis"), rt_make_function_n(0, pyb_millis));
452458

459+
#if MICROPY_HW_ENABLE_AUDIO
460+
rt_store_attr(m, qstr_from_str("Audio"), (mp_obj_t)&pyb_Audio_obj);
461+
#endif
462+
453463
pin_map_init(m);
454464
gpio_init(m);
455465
exti_init(m);

stm/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Q(main)
1111
Q(sync)
1212
Q(gc)
1313
Q(delay)
14+
Q(udelay)
1415
Q(switch)
1516
Q(SW)
1617
Q(servo)

0 commit comments

Comments
 (0)