@@ -18,6 +18,9 @@ uint dmaChannel = 0;
18
18
PIO pio = pio0;
19
19
uint sm = 0 ;
20
20
21
+ // PIO program offset
22
+ static uint offset;
23
+
21
24
// raw buffers contain PDM data
22
25
#define RAW_BUFFER_SIZE 512 // should be a multiple of (decimation / 8)
23
26
uint8_t rawBuffer0[RAW_BUFFER_SIZE];
@@ -103,8 +106,13 @@ int PDMClass::begin(int channels, int sampleRate)
103
106
104
107
// Configure PIO state machine
105
108
float clkDiv = (float )clock_get_hz (clk_sys) / sampleRate / decimation / 2 ;
106
- uint offset = pio_add_program (pio, &pdm_pio_program);
107
- pdm_pio_program_init (pio, sm, offset, _clkPin, _dinPin, clkDiv);
109
+ if (pio_can_add_program (pio, &pdm_pio_program)) {
110
+ offset = pio_add_program (pio, &pdm_pio_program);
111
+ pdm_pio_program_init (pio, sm, offset, _clkPin, _dinPin, clkDiv);
112
+ } else {
113
+ mbed_error_printf (" Cannot load pio program\n " );
114
+ mbed_die ();
115
+ }
108
116
109
117
// Wait for microphone
110
118
delay (100 );
@@ -137,10 +145,12 @@ int PDMClass::begin(int channels, int sampleRate)
137
145
138
146
void PDMClass::end ()
139
147
{
148
+ pio_remove_program (pio, &pdm_pio_program, offset);
140
149
dma_channel_abort (dmaChannel);
141
150
pinMode (_clkPin, INPUT);
142
151
decimation = 128 ;
143
152
rawBufferIndex = 0 ;
153
+ offset = 0 ;
144
154
}
145
155
146
156
int PDMClass::available ()
0 commit comments