|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "mpconfigport.h" |
| 31 | + |
| 32 | +#include "py/gc.h" |
| 33 | +#include "py/mperrno.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "common-hal/audiobusio/I2SOut.h" |
| 36 | +#include "shared-bindings/audiobusio/I2SOut.h" |
| 37 | +#include "shared-bindings/microcontroller/Pin.h" |
| 38 | +#include "shared-module/audiocore/__init__.h" |
| 39 | +#include "bindings/rp2pio/StateMachine.h" |
| 40 | +#include "supervisor/shared/translate.h" |
| 41 | + |
| 42 | +const uint16_t i2s_program[] = { |
| 43 | +// ; Load the next set of samples |
| 44 | +// ; /--- LRCLK |
| 45 | +// ; |/-- BCLK |
| 46 | +// ; || |
| 47 | +// pull noblock side 0b01 ; Loads OSR with the next FIFO value or X |
| 48 | + 0x8880, |
| 49 | +// mov x osr side 0b01 ; Save the new value in case we need it again |
| 50 | + 0xa827, |
| 51 | +// set y 14 side 0b01 |
| 52 | + 0xe84e, |
| 53 | +// bitloop1: |
| 54 | +// out pins 1 side 0b00 [2] |
| 55 | + 0x6201, |
| 56 | +// jmp y-- bitloop1 side 0b01 [2] |
| 57 | + 0x0a83, |
| 58 | +// out pins 1 side 0b10 [2] |
| 59 | + 0x7201, |
| 60 | +// set y 14 side 0b11 [2] |
| 61 | + 0xfa4e, |
| 62 | +// bitloop0: |
| 63 | +// out pins 1 side 0b10 [2] |
| 64 | + 0x7201, |
| 65 | +// jmp y-- bitloop0 side 0b11 [2] |
| 66 | + 0x1a87, |
| 67 | +// out pins 1 side 0b00 [2] |
| 68 | + 0x6201 |
| 69 | +}; |
| 70 | + |
| 71 | +const uint16_t i2s_program_left_justified[] = { |
| 72 | +// ; Load the next set of samples |
| 73 | +// ; /--- LRCLK |
| 74 | +// ; |/-- BCLK |
| 75 | +// ; || |
| 76 | +// pull noblock side 0b11 ; Loads OSR with the next FIFO value or X |
| 77 | + 0x9880, |
| 78 | +// mov x osr side 0b11 ; Save the new value in case we need it again |
| 79 | + 0xb827, |
| 80 | +// set y 14 side 0b11 |
| 81 | + 0xf84e, |
| 82 | +// bitloop1: |
| 83 | +// out pins 1 side 0b00 [2] |
| 84 | + 0x6201, |
| 85 | +// jmp y-- bitloop1 side 0b01 [2] |
| 86 | + 0x0a83, |
| 87 | +// out pins 1 side 0b10 [2] |
| 88 | + 0x6201, |
| 89 | +// set y 14 side 0b01 [2] |
| 90 | + 0xea4e, |
| 91 | +// bitloop0: |
| 92 | +// out pins 1 side 0b10 [2] |
| 93 | + 0x7201, |
| 94 | +// jmp y-- bitloop0 side 0b11 [2] |
| 95 | + 0x1a87, |
| 96 | +// out pins 1 side 0b10 [2] |
| 97 | + 0x7201 |
| 98 | +}; |
| 99 | + |
| 100 | +void i2sout_reset(void) { |
| 101 | +} |
| 102 | + |
| 103 | +// Caller validates that pins are free. |
| 104 | +void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t* self, |
| 105 | + const mcu_pin_obj_t* bit_clock, const mcu_pin_obj_t* word_select, |
| 106 | + const mcu_pin_obj_t* data, bool left_justified) { |
| 107 | + if (bit_clock->number != word_select->number - 1) { |
| 108 | + mp_raise_ValueError(translate("Bit clock and word select must be sequential pins")); |
| 109 | + } |
| 110 | + |
| 111 | + const uint16_t* program = i2s_program; |
| 112 | + size_t program_len = sizeof(i2s_program) / sizeof(i2s_program[0]); |
| 113 | + if (left_justified) { |
| 114 | + program = i2s_program_left_justified; |
| 115 | + program_len = sizeof(i2s_program_left_justified) / sizeof(i2s_program_left_justified[0]);; |
| 116 | + } |
| 117 | + |
| 118 | + // Use the state machine to manage pins. |
| 119 | + common_hal_rp2pio_statemachine_construct(&self->state_machine, |
| 120 | + program, program_len, |
| 121 | + 44100 * 32 * 6, // Clock at 44.1 khz to warm the DAC up. |
| 122 | + NULL, 0, |
| 123 | + data, 1, 0, 0xffffffff, // out pin |
| 124 | + NULL, 0, // in pins |
| 125 | + NULL, 0, 0, 0x1f, // set pins |
| 126 | + bit_clock, 2, 0, 0x1f, // sideset pins |
| 127 | + true, // exclusive pin use |
| 128 | + false, 32, false, // shift out left to start with MSB |
| 129 | + false, // Wait for txstall |
| 130 | + false, 32, false); // in settings |
| 131 | + |
| 132 | + self->playing = false; |
| 133 | + audio_dma_init(&self->dma); |
| 134 | +} |
| 135 | + |
| 136 | +bool common_hal_audiobusio_i2sout_deinited(audiobusio_i2sout_obj_t* self) { |
| 137 | + return common_hal_rp2pio_statemachine_deinited(&self->state_machine); |
| 138 | +} |
| 139 | + |
| 140 | +void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t* self) { |
| 141 | + if (common_hal_audiobusio_i2sout_deinited(self)) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + common_hal_rp2pio_statemachine_deinit(&self->state_machine); |
| 146 | +} |
| 147 | + |
| 148 | +void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self, |
| 149 | + mp_obj_t sample, bool loop) { |
| 150 | + if (common_hal_audiobusio_i2sout_get_playing(self)) { |
| 151 | + common_hal_audiobusio_i2sout_stop(self); |
| 152 | + } |
| 153 | + uint8_t bits_per_sample = audiosample_bits_per_sample(sample); |
| 154 | + // Make sure we transmit a minimum of 16 bits. |
| 155 | + // TODO: Maybe we need an intermediate object to upsample instead. This is |
| 156 | + // only needed for some I2S devices that expect at least 8. |
| 157 | + if (bits_per_sample < 16) { |
| 158 | + bits_per_sample = 16; |
| 159 | + } |
| 160 | + // We always output stereo so output twice as many bits. |
| 161 | + uint16_t bits_per_sample_output = bits_per_sample * 2; |
| 162 | + size_t clocks_per_bit = 6; |
| 163 | + uint32_t frequency = bits_per_sample_output * audiosample_sample_rate(sample); |
| 164 | + common_hal_rp2pio_statemachine_set_frequency(&self->state_machine, clocks_per_bit * frequency); |
| 165 | + common_hal_rp2pio_statemachine_restart(&self->state_machine); |
| 166 | + |
| 167 | + uint8_t channel_count = audiosample_channel_count(sample); |
| 168 | + if (channel_count > 2) { |
| 169 | + mp_raise_ValueError(translate("Too many channels in sample.")); |
| 170 | + } |
| 171 | + |
| 172 | + audio_dma_result result = audio_dma_setup_playback( |
| 173 | + &self->dma, |
| 174 | + sample, |
| 175 | + loop, |
| 176 | + false, // single channel |
| 177 | + 0, // audio channel |
| 178 | + true, // output signed |
| 179 | + bits_per_sample, |
| 180 | + (uint32_t) &self->state_machine.pio->txf[self->state_machine.state_machine], // output register |
| 181 | + self->state_machine.tx_dreq); // data request line |
| 182 | + |
| 183 | + if (result == AUDIO_DMA_DMA_BUSY) { |
| 184 | + common_hal_audiobusio_i2sout_stop(self); |
| 185 | + mp_raise_RuntimeError(translate("No DMA channel found")); |
| 186 | + } else if (result == AUDIO_DMA_MEMORY_ERROR) { |
| 187 | + common_hal_audiobusio_i2sout_stop(self); |
| 188 | + mp_raise_RuntimeError(translate("Unable to allocate buffers for signed conversion")); |
| 189 | + } |
| 190 | + |
| 191 | + // Turn on the state machine's clock. |
| 192 | + |
| 193 | + self->playing = true; |
| 194 | +} |
| 195 | + |
| 196 | +void common_hal_audiobusio_i2sout_pause(audiobusio_i2sout_obj_t* self) { |
| 197 | + audio_dma_pause(&self->dma); |
| 198 | +} |
| 199 | + |
| 200 | +void common_hal_audiobusio_i2sout_resume(audiobusio_i2sout_obj_t* self) { |
| 201 | + // Maybe: Clear any overrun/underrun errors |
| 202 | + |
| 203 | + audio_dma_resume(&self->dma); |
| 204 | +} |
| 205 | + |
| 206 | +bool common_hal_audiobusio_i2sout_get_paused(audiobusio_i2sout_obj_t* self) { |
| 207 | + return audio_dma_get_paused(&self->dma); |
| 208 | +} |
| 209 | + |
| 210 | +void common_hal_audiobusio_i2sout_stop(audiobusio_i2sout_obj_t* self) { |
| 211 | + audio_dma_stop(&self->dma); |
| 212 | + |
| 213 | + common_hal_rp2pio_statemachine_stop(&self->state_machine); |
| 214 | + |
| 215 | + self->playing = false; |
| 216 | +} |
| 217 | + |
| 218 | +bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t* self) { |
| 219 | + bool playing = audio_dma_get_playing(&self->dma); |
| 220 | + if (!playing && self->playing) { |
| 221 | + common_hal_audiobusio_i2sout_stop(self); |
| 222 | + } |
| 223 | + return playing; |
| 224 | +} |
0 commit comments