|
| 1 | +/* |
| 2 | + * I2S Master library for the Raspberry Pi Pico RP2040 |
| 3 | + * |
| 4 | + * Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com> |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include <Arduino.h> |
| 22 | +#include "I2S.h" |
| 23 | + |
| 24 | +I2SClass::I2SClass() { |
| 25 | + _running = false; |
| 26 | + _pool = nullptr; |
| 27 | + _curBuff = nullptr; |
| 28 | + _bps = 0; |
| 29 | + _writtenHalf = false; |
| 30 | +} |
| 31 | + |
| 32 | +bool I2SClass::begin(long sampleRate, pin_size_t sck, pin_size_t data) { |
| 33 | + if (_running) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + bzero(&_audio_format, sizeof(_audio_format)); |
| 38 | + _audio_format.sample_freq = (uint32_t)sampleRate; |
| 39 | + _audio_format.format = AUDIO_BUFFER_FORMAT_PCM_S16; |
| 40 | + _audio_format.channel_count = 2; |
| 41 | + |
| 42 | + bzero(&_producer_format, sizeof(_producer_format)); |
| 43 | + _producer_format.format = &_audio_format; |
| 44 | + _producer_format.sample_stride = 4; |
| 45 | + |
| 46 | + if (!_pool) { |
| 47 | + _pool = audio_new_producer_pool(&_producer_format, 3, 256); |
| 48 | + } |
| 49 | + |
| 50 | + bzero(&_config, sizeof(_config)); |
| 51 | + _config.data_pin = data; |
| 52 | + _config.clock_pin_base = sck; |
| 53 | + _config.dma_channel = 0; |
| 54 | + _config.pio_sm = 1; |
| 55 | + |
| 56 | + if (!audio_i2s_setup(&_audio_format, &_config)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if (!audio_i2s_connect(_pool)) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + audio_i2s_set_enabled(true); |
| 65 | + |
| 66 | + _curBuff = take_audio_buffer(_pool, true); |
| 67 | + _curBuff->sample_count = 0; |
| 68 | + |
| 69 | + _bps = 16; |
| 70 | + _freq = sampleRate; |
| 71 | + _running = true; |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +void I2SClass::end() { |
| 76 | + if (_running) { |
| 77 | + audio_i2s_set_enabled(false); |
| 78 | + if (_curBuff) { |
| 79 | + release_audio_buffer(_pool, _curBuff); |
| 80 | + _curBuff = nullptr; |
| 81 | + } |
| 82 | + } |
| 83 | + _running = false; |
| 84 | + _bps = 0; |
| 85 | + _freq = 0; |
| 86 | + _writtenHalf = false; |
| 87 | +} |
| 88 | + |
| 89 | +int I2SClass::availableForWrite() { |
| 90 | + if (!_running) { |
| 91 | + return 0; |
| 92 | + } |
| 93 | + // Can we get a whole new buffer to work with? |
| 94 | + if (!_curBuff) { |
| 95 | + _curBuff = take_audio_buffer(_pool, false); |
| 96 | + _curBuff->sample_count = 0; |
| 97 | + } |
| 98 | + if (!_curBuff) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + return _curBuff->max_sample_count - _curBuff->sample_count; |
| 102 | +} |
| 103 | + |
| 104 | +void I2SClass::flush() { |
| 105 | + if (!_curBuff || !_curBuff->sample_count) { |
| 106 | + return; |
| 107 | + } |
| 108 | + _audio_format.sample_freq = _freq; |
| 109 | + give_audio_buffer(_pool, _curBuff); |
| 110 | + _curBuff = nullptr; |
| 111 | +} |
| 112 | + |
| 113 | +bool I2SClass::setFrequency(int newFreq) { |
| 114 | + if (newFreq != _freq) { |
| 115 | + flush(); |
| 116 | + _freq = newFreq; |
| 117 | + } |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +size_t I2SClass::write(uint8_t s) { |
| 122 | + return write((int16_t)s); |
| 123 | +} |
| 124 | + |
| 125 | +size_t I2SClass::write(const uint8_t *buffer, size_t size) { |
| 126 | + return write((const void *)buffer, size); |
| 127 | +} |
| 128 | + |
| 129 | +size_t I2SClass::write(int16_t s) { |
| 130 | + if (!_running) { |
| 131 | + return 0; |
| 132 | + } |
| 133 | + |
| 134 | + // Because our HW really wants 32b writes, store any 16b writes until another |
| 135 | + // 16b write comes in and then send the combined write down. |
| 136 | + if (_bps == 16) { |
| 137 | + if (_writtenHalf) { |
| 138 | + _writtenData <<= 16; |
| 139 | + _writtenData |= 0xffff & s; |
| 140 | + _writtenHalf = false; |
| 141 | + if (!_curBuff) { |
| 142 | + _curBuff = take_audio_buffer(_pool, true); |
| 143 | + _curBuff->sample_count = 0; |
| 144 | + } |
| 145 | + int32_t *samples = (int32_t *)_curBuff->buffer->bytes; |
| 146 | + samples[_curBuff->sample_count++] = _writtenData; |
| 147 | + if (_curBuff->sample_count == _curBuff->max_sample_count) { |
| 148 | + _audio_format.sample_freq = _freq; |
| 149 | + give_audio_buffer(_pool, _curBuff); |
| 150 | + _curBuff = nullptr; |
| 151 | + } |
| 152 | + } else { |
| 153 | + _writtenHalf = true; |
| 154 | + _writtenData = s & 0xffff; |
| 155 | + } |
| 156 | + } |
| 157 | + return 1; |
| 158 | +} |
| 159 | + |
| 160 | +// Mostly non-blocking |
| 161 | +size_t I2SClass::write(const void *buffer, size_t size) { |
| 162 | + if (!_running) { |
| 163 | + return 0; |
| 164 | + } |
| 165 | + // We have no choice here because we need to write at least 1 byte... |
| 166 | + if (!_curBuff) { |
| 167 | + _curBuff = take_audio_buffer(_pool, true); |
| 168 | + _curBuff->sample_count = 0; |
| 169 | + } |
| 170 | + |
| 171 | + int32_t *inSamples = (int32_t *)buffer; |
| 172 | + int written = 0; |
| 173 | + int wantToWrite = size / 4; |
| 174 | + while (wantToWrite) { |
| 175 | + if (!_curBuff) { |
| 176 | + _curBuff = take_audio_buffer(_pool, false); |
| 177 | + if (_curBuff) { |
| 178 | + _curBuff->sample_count = 0; |
| 179 | + } else { |
| 180 | + break; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + int avail = _curBuff->max_sample_count - _curBuff->sample_count; |
| 185 | + int writeSize = (avail > wantToWrite) ? wantToWrite : avail; |
| 186 | + int32_t *samples = (int32_t *)_curBuff->buffer->bytes; |
| 187 | + memcpy(samples + _curBuff->sample_count, inSamples, writeSize * 4); |
| 188 | + _curBuff->sample_count += writeSize; |
| 189 | + inSamples += writeSize; |
| 190 | + written += writeSize; |
| 191 | + wantToWrite -= writeSize; |
| 192 | + if (_curBuff->sample_count == _curBuff->max_sample_count) { |
| 193 | + _audio_format.sample_freq = _freq; |
| 194 | + give_audio_buffer(_pool, _curBuff); |
| 195 | + _curBuff = nullptr; |
| 196 | + } |
| 197 | + } |
| 198 | + return written; |
| 199 | +} |
| 200 | + |
| 201 | +I2SClass I2S; |
0 commit comments