Skip to content

Commit 2653455

Browse files
authored
Merge pull request #2321 from jepler/nrf-i2s-buglets
Nrf i2s buglets
2 parents d154d11 + 5115fda commit 2653455

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

ports/nrf/common-hal/audiobusio/I2SOut.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void choose_i2s_clocking(audiobusio_i2sout_obj_t *self, uint32_t sample_rate) {
108108

109109
static void i2s_buffer_fill(audiobusio_i2sout_obj_t* self) {
110110
void *buffer = self->buffers[self->next_buffer];
111+
void *buffer_start = buffer;
111112
NRF_I2S->TXD.PTR = (uintptr_t)buffer;
112113
self->next_buffer = !self->next_buffer;
113114
size_t bytesleft = self->buffer_length;
@@ -157,15 +158,17 @@ static void i2s_buffer_fill(audiobusio_i2sout_obj_t* self) {
157158

158159
// Find the last frame of real audio data and replicate its samples until
159160
// you have 32 bits worth, which is the fundamental unit of nRF I2S DMA
160-
if (self->bytes_per_sample == 1 && self->channel_count == 1) {
161-
// For 8-bit mono, 4 copies of the final sample are required
162-
self->hold_value = 0x01010101 * *(uint8_t*)(buffer-1);
163-
} else if (self->bytes_per_sample == 2 && self->channel_count == 2) {
164-
// For 16-bit stereo, 1 copy of the final sample is required
165-
self->hold_value = *(uint32_t*)(buffer-4);
166-
} else {
167-
// For 8-bit stereo and 16-bit mono, 2 copies of the final sample are required
168-
self->hold_value = 0x00010001 * *(uint16_t*)(buffer-2);
161+
if(buffer != buffer_start) {
162+
if (self->bytes_per_sample == 1 && self->channel_count == 1) {
163+
// For 8-bit mono, 4 copies of the final sample are required
164+
self->hold_value = 0x01010101 * *(uint8_t*)(buffer-1);
165+
} else if (self->bytes_per_sample == 2 && self->channel_count == 2) {
166+
// For 16-bit stereo, 1 copy of the final sample is required
167+
self->hold_value = *(uint32_t*)(buffer-4);
168+
} else {
169+
// For 8-bit stereo and 16-bit mono, 2 copies of the final sample are required
170+
self->hold_value = 0x00010001 * *(uint16_t*)(buffer-2);
171+
}
169172
}
170173

171174
// Emulate pausing and stopping by filling the DMA buffer with copies of
@@ -218,6 +221,8 @@ void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t* self) {
218221
if (common_hal_audiobusio_i2sout_deinited(self)) {
219222
return;
220223
}
224+
NRF_I2S->TASKS_STOP = 1;
225+
NRF_I2S->ENABLE = I2S_ENABLE_ENABLE_Disabled;
221226
reset_pin_number(self->bit_clock_pin_number);
222227
self->bit_clock_pin_number = 0xff;
223228
reset_pin_number(self->word_select_pin_number);

shared-bindings/audiobusio/I2SOut.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ STATIC mp_obj_t audiobusio_i2sout_make_new(const mp_obj_type_t *type, size_t n_a
117117
assert_pin(data_obj, false);
118118
const mcu_pin_obj_t *data = MP_OBJ_TO_PTR(data_obj);
119119

120-
audiobusio_i2sout_obj_t *self = m_new_obj(audiobusio_i2sout_obj_t);
120+
audiobusio_i2sout_obj_t *self = m_new_obj_with_finaliser(audiobusio_i2sout_obj_t);
121121
self->base.type = &audiobusio_i2sout_type;
122122
common_hal_audiobusio_i2sout_construct(self, bit_clock, word_select, data, args[ARG_left_justified].u_bool);
123123

@@ -268,6 +268,7 @@ const mp_obj_property_t audiobusio_i2sout_paused_obj = {
268268

269269
STATIC const mp_rom_map_elem_t audiobusio_i2sout_locals_dict_table[] = {
270270
// Methods
271+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiobusio_i2sout_deinit_obj) },
271272
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiobusio_i2sout_deinit_obj) },
272273
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
273274
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiobusio_i2sout___exit___obj) },

0 commit comments

Comments
 (0)