Library to control and transfer data from NXP SGTL5000. As used on the Teensy Audio Shield. It uses DMA to transfer I2S FIFO data.

The Library now supports dual codecs. Allowing all 4 channels of the Teensy I2S interface to RX and TX data to separate SGTL5000 devices.

The ISR routines that handles pointer swaps for double buffering has been fully coded in assembler to reduce overhead and now takes < 800nS per FIFO transfer when using all 4 channels.

Support added for all typical sample rates and system Clock speeds of 96Mhz or 120Mhz.

Pause and Resume functions added to allow quick and simple suppression of IRQs and stream halting and restart. This required software triggered IRQ, in order to ensure accurate word sync control.

Committer:
aidan1971
Date:
Wed Sep 27 10:39:01 2017 +0000
Revision:
11:392c09372ae1
Parent:
10:49bb33f71d32
Child:
12:9ce7f3828c6a
A few documentation updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aidan1971 3:62c03088f256 1 /*!
aidan1971 0:8f28f25e3435 2 @ author Aidan Walton, aidan.walton@gmail.com
aidan1971 0:8f28f25e3435 3
aidan1971 0:8f28f25e3435 4 @section LICENSE
aidan1971 0:8f28f25e3435 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
aidan1971 0:8f28f25e3435 6 * of this software and associated documentation files (the "Software"), to deal
aidan1971 0:8f28f25e3435 7 * in the Software without restriction, including without limitation the rights
aidan1971 0:8f28f25e3435 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aidan1971 0:8f28f25e3435 9 * copies of the Software, and to permit persons to whom the Software is
aidan1971 0:8f28f25e3435 10 * furnished to do so, subject to the following conditions:
aidan1971 0:8f28f25e3435 11 *
aidan1971 0:8f28f25e3435 12 * The above copyright notice, development funding notice, and this permission
aidan1971 0:8f28f25e3435 13 * notice shall be included in all copies or substantial portions of the Software.
aidan1971 0:8f28f25e3435 14 *
aidan1971 0:8f28f25e3435 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aidan1971 0:8f28f25e3435 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aidan1971 0:8f28f25e3435 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aidan1971 0:8f28f25e3435 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aidan1971 0:8f28f25e3435 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aidan1971 0:8f28f25e3435 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
aidan1971 0:8f28f25e3435 21 * THE SOFTWARE.
aidan1971 0:8f28f25e3435 22
aidan1971 0:8f28f25e3435 23 @section DESCRIPTION
aidan1971 0:8f28f25e3435 24 Library for NXP SGTL5000 Codec
aidan1971 0:8f28f25e3435 25 */
aidan1971 0:8f28f25e3435 26
aidan1971 0:8f28f25e3435 27 #ifndef MBED_SGTL5000_H
aidan1971 0:8f28f25e3435 28 #define MBED_SGTL5000_H
aidan1971 0:8f28f25e3435 29
aidan1971 0:8f28f25e3435 30 #include "mbed.h"
aidan1971 0:8f28f25e3435 31 #include "platform.h"
aidan1971 0:8f28f25e3435 32 #include "Callback.h"
aidan1971 0:8f28f25e3435 33 #include "sgtl5000_defs.h"
aidan1971 0:8f28f25e3435 34
aidan1971 10:49bb33f71d32 35
aidan1971 3:62c03088f256 36 /** SGTL5000 namespace
aidan1971 3:62c03088f256 37 */
aidan1971 0:8f28f25e3435 38 namespace SGTL5000
aidan1971 0:8f28f25e3435 39 {
aidan1971 0:8f28f25e3435 40
aidan1971 10:49bb33f71d32 41
aidan1971 3:62c03088f256 42 /*! SGTL5000 codec driver class
aidan1971 3:62c03088f256 43 */
aidan1971 3:62c03088f256 44 /*! Class for NXP SGTL5000 codec instance.
aidan1971 11:392c09372ae1 45 * Supports dual codecs. One using I2S TX&RX channel_0 the other I2S TX&RX channel_1. The instance created for codec1 (I2S channel_0 CODEC CS = LOW) is the master,
aidan1971 11:392c09372ae1 46 all stream ctrl functions (start, stop, pause & resume) are synchronous to this master codec and must be initiated by the master codec object.
aidan1971 3:62c03088f256 47 * @code
aidan1971 3:62c03088f256 48 * #include 'SGTL5000.h'
aidan1971 3:62c03088f256 49 *
aidan1971 3:62c03088f256 50 * SGTL5000::SGTL5000 codec(I2C_SDA, I2C_SCL);
aidan1971 3:62c03088f256 51 *
aidan1971 3:62c03088f256 52 * static q31_t *RX_AudioL = NULL;
aidan1971 3:62c03088f256 53 * static q31_t *RX_AudioR = NULL;
aidan1971 3:62c03088f256 54 * static q31_t *TX_AudioL = NULL;
aidan1971 3:62c03088f256 55 * static q31_t *TX_AudioR = NULL;
aidan1971 3:62c03088f256 56 *
aidan1971 3:62c03088f256 57 * const uint32_t I2S_FIFO_BS = 4;
aidan1971 3:62c03088f256 58 *
aidan1971 3:62c03088f256 59 *
aidan1971 3:62c03088f256 60 * uint32_t main()
aidan1971 3:62c03088f256 61 * {
aidan1971 3:62c03088f256 62 * codec.modify_i2c(SGTL5000_ANA_HP_CTRL, 0x18, SGTL5000_ANA_HP_CTRL_HP_VOL_RIGHT_MASK); // Headphone volume control with 0.5 dB steps.0x00 = +12 dB, 0x01 = +11.5 dB, 0x18 = 0 dB,...0x7F = -51.5 dB
aidan1971 3:62c03088f256 63 * codec.modify_i2c(SGTL5000_ANA_HP_CTRL, 0x18, SGTL5000_ANA_HP_CTRL_HP_VOL_LEFT_MASK);
aidan1971 11:392c09372ae1 64 * codec.init();
aidan1971 3:62c03088f256 65 * codec.attach_SYNC_NB((uint32_t)&I2S_SYNC_ISR);
aidan1971 11:392c09372ae1 66 * codec.sample_rate(96);
aidan1971 3:62c03088f256 67 * codec.start_SYNC((uint32_t)&RX_AudioL, (uint32_t)&RX_AudioR, (uint32_t)&TX_AudioL, (uint32_t)&TX_AudioR, I2S_FIFO_BS)
aidan1971 3:62c03088f256 68 * }
aidan1971 3:62c03088f256 69 *
aidan1971 3:62c03088f256 70 * void I2S_SYNC_ISR(void)
aidan1971 3:62c03088f256 71 * {
aidan1971 3:62c03088f256 72 * for(uint32_t i = 0; i < (I2S_FIFO_BS >> 1); ++i)
aidan1971 3:62c03088f256 73 * {
aidan1971 3:62c03088f256 74 * TX_AudioL[i] = RX_AudioL[i];
aidan1971 3:62c03088f256 75 * TX_AudioR[i] = RX_AudioR[i];
aidan1971 3:62c03088f256 76 * }
aidan1971 3:62c03088f256 77 * }
aidan1971 3:62c03088f256 78 @endcode
aidan1971 11:392c09372ae1 79 *
aidan1971 11:392c09372ae1 80 * To implement dual CODECs
aidan1971 11:392c09372ae1 81 * @code
aidan1971 11:392c09372ae1 82 * #include 'SGTL5000.h'
aidan1971 11:392c09372ae1 83 *
aidan1971 11:392c09372ae1 84 * SGTL5000::SGTL5000 codec1_ctrl(I2C_SDA, I2C_SCL);
aidan1971 11:392c09372ae1 85 * SGTL5000::SGTL5000 codec2(I2C_SDA, I2C_SCL);
aidan1971 11:392c09372ae1 86 *
aidan1971 11:392c09372ae1 87 * static q31_t *RX_AudioL1 = NULL;
aidan1971 11:392c09372ae1 88 * static q31_t *RX_AudioR1 = NULL;
aidan1971 11:392c09372ae1 89 * static q31_t *TX_AudioL1 = NULL;
aidan1971 11:392c09372ae1 90 * static q31_t *TX_AudioR1 = NULL;
aidan1971 11:392c09372ae1 91 * static q31_t *RX_AudioL2 = NULL;
aidan1971 11:392c09372ae1 92 * static q31_t *RX_AudioR2 = NULL;
aidan1971 11:392c09372ae1 93 * static q31_t *TX_AudioL2 = NULL;
aidan1971 11:392c09372ae1 94 * static q31_t *TX_AudioR2 = NULL;
aidan1971 11:392c09372ae1 95 *
aidan1971 11:392c09372ae1 96 * const uint32_t I2S_FIFO_BS = 4;
aidan1971 11:392c09372ae1 97 *
aidan1971 11:392c09372ae1 98 *
aidan1971 11:392c09372ae1 99 * uint32_t main()
aidan1971 11:392c09372ae1 100 * {
aidan1971 11:392c09372ae1 101 * codec1_ctrl.modify_i2c(SGTL5000_ANA_HP_CTRL, 0x18, SGTL5000_ANA_HP_CTRL_HP_VOL_RIGHT_MASK); // Headphone volume control with 0.5 dB steps.0x00 = +12 dB, 0x01 = +11.5 dB, 0x18 = 0 dB,...0x7F = -51.5 dB
aidan1971 11:392c09372ae1 102 * codec1_ctrl.modify_i2c(SGTL5000_ANA_HP_CTRL, 0x18, SGTL5000_ANA_HP_CTRL_HP_VOL_LEFT_MASK);
aidan1971 11:392c09372ae1 103 * codec2.modify_i2c(SGTL5000_ANA_HP_CTRL, 0x01, SGTL5000_ANA_HP_CTRL_HP_VOL_RIGHT_MASK); // Headphone volume control with 0.5 dB steps.0x00 = +12 dB, 0x01 = +11.5 dB, 0x18 = 0 dB,...0x7F = -51.5 dB
aidan1971 11:392c09372ae1 104 * codec2.modify_i2c(SGTL5000_ANA_HP_CTRL, 0x01, SGTL5000_ANA_HP_CTRL_HP_VOL_LEFT_MASK); // Seperate codec settings.
aidan1971 11:392c09372ae1 105 * codec1_ctrl.init();
aidan1971 11:392c09372ae1 106 * codec2.init();
aidan1971 11:392c09372ae1 107 * codec1_ctrl.attach_SYNC_NB((uint32_t)&I2S_SYNC_ISR);
aidan1971 11:392c09372ae1 108 * codec1_ctrl.sample_rate(96);
aidan1971 11:392c09372ae1 109 * codec1_ctrl.start_SYNC((uint32_t)&RX_AudioL1, (uint32_t)&RX_AudioR1, (uint32_t)&TX_AudioL1, (uint32_t)&TX_AudioR1, I2S_FIFO_BS, true, true, false, false, 14, 15,
aidan1971 11:392c09372ae1 110 * (uint32_t)&RX_AudioL2, (uint32_t)&RX_AudioR2, (uint32_t)&TX_AudioL2, (uint32_t)&TX_AudioR2)
aidan1971 11:392c09372ae1 111 * }
aidan1971 11:392c09372ae1 112 *
aidan1971 11:392c09372ae1 113 * void I2S_SYNC_ISR(void)
aidan1971 11:392c09372ae1 114 * {
aidan1971 11:392c09372ae1 115 * for(uint32_t i = 0; i < (I2S_FIFO_BS >> 1); ++i)
aidan1971 11:392c09372ae1 116 * {
aidan1971 11:392c09372ae1 117 * // CODEC 1
aidan1971 11:392c09372ae1 118 * TX_AudioL1[i] = RX_AudioL1[i];
aidan1971 11:392c09372ae1 119 * TX_AudioR1[i] = RX_AudioR1[i];
aidan1971 11:392c09372ae1 120 *
aidan1971 11:392c09372ae1 121 * // CODEC 2
aidan1971 11:392c09372ae1 122 * TX_AudioL2[i] = RX_AudioL2[i];
aidan1971 11:392c09372ae1 123 * TX_AudioR2[i] = RX_AudioR2[i];
aidan1971 11:392c09372ae1 124 * }
aidan1971 11:392c09372ae1 125 * }
aidan1971 11:392c09372ae1 126 @endcode
aidan1971 3:62c03088f256 127 */
aidan1971 0:8f28f25e3435 128 class SGTL5000
aidan1971 0:8f28f25e3435 129
aidan1971 0:8f28f25e3435 130 {
aidan1971 0:8f28f25e3435 131 public:
aidan1971 3:62c03088f256 132 //Constructor
aidan1971 3:62c03088f256 133 /*!
aidan1971 0:8f28f25e3435 134 @brief Create an SGTL5000 object defined on the I2C port using DMA transfers of I2S data. The class is not defined as a singleton, as future development may require
aidan1971 11:392c09372ae1 135 multiple instances. However currently only a single object for each CODEC should only be instantiated. It is possible to instantiate more, but care must be taken.
aidan1971 11:392c09372ae1 136 After the init() function is called, state within the class becomes indpendent of the object that modified it.
aidan1971 11:392c09372ae1 137 The class is wrapped in the SGTL5000 namespace to avoid collisions with statics needed by the ISRs. Only the CODEC using CTRL_ADR0_CS = 0 can be used to manage
aidan1971 11:392c09372ae1 138 the I2S setup and data flow, such as sample_rate, attach, start, stop etc. If a second CODEC is available then its data flow is locked to the 1st,
aidan1971 11:392c09372ae1 139 TX & RX FIFO buffers of both CODECs will be synchronised and only one DMA channel is used to TX data to both codecs and one DMA channel to RX data from both codecs.
aidan1971 0:8f28f25e3435 140
aidan1971 0:8f28f25e3435 141 @param i2c_sda i2c Serial data pin (D18 Teensy 3.2 header / PTB3 MK20DX256)
aidan1971 0:8f28f25e3435 142 @param i2c_scl i2c Serial clock pin (D19 Teensy 3.2 header / PTB2 MK20DX256)
aidan1971 0:8f28f25e3435 143 @param i2c_freq Frequency in Hz at which the i2c codec interface clocks data
aidan1971 0:8f28f25e3435 144 @param i2c_ctrl_adr0_cs State on SGTL5000 CTRL_ADR0_CS pin i2c addr = 0n01010(R/W) :R/W = 1 to write R/W = 0 to read, n = 0 pull down / n = 1 pull up on CTRL_ADR0_CS pin of SGTL5000)
aidan1971 10:49bb33f71d32 145 @param _ctrl_IRQn A system IRQ number used to control the codec. All time sensitive commands are elevated to highest IRQ priority using this IRQ number.
aidan1971 10:49bb33f71d32 146 Note: This is only used by the master codec (which is the codec with CS pin LOW). All commands to control data flow must be issued through the master codec object.
aidan1971 10:49bb33f71d32 147 This includes setting sample rate. Both codecs are linked by default at the same rate. Setting the master codecs rate also sets the same rate for the 2nd codec.
aidan1971 10:49bb33f71d32 148 However each codec instance has fully independent access to all the other codec internal features, through its respective object.
aidan1971 0:8f28f25e3435 149
aidan1971 0:8f28f25e3435 150
aidan1971 0:8f28f25e3435 151 // Pin Configs for i2s hardcoded as follows to match Teensy Audio Shield
aidan1971 0:8f28f25e3435 152 i2s_mclk i2s master clock (D11 Teensy 3.2 header / PTC6 MK20DX256)
aidan1971 0:8f28f25e3435 153 i2s_bclk i2s bit clock (D9 Teensy 3.2 header / PTC3 MK20DX256)
aidan1971 0:8f28f25e3435 154 i2s_fs i2s Frame Sync / L/R clock / WordSelect (D23 Teensy 3.2 header / PTC2 MK20DX256)
aidan1971 0:8f28f25e3435 155 i2s_rx i2s tx_data (from bus master perspective) (D22 Teensy 3.2 header / PTC1 MK20DX256)
aidan1971 0:8f28f25e3435 156 i2s_tx i2s rx_data (from bus master perspective) (D13 Teensy 3.2 header /PTC5 MK20DX256)
aidan1971 0:8f28f25e3435 157
aidan1971 0:8f28f25e3435 158 */
aidan1971 10:49bb33f71d32 159 SGTL5000(PinName i2c_sda, PinName i2c_scl, int _i2c_freq = 100000, bool i2c_ctrl_adr0_cs = 0, IRQn _ctrl_IRQn = Reserved109_IRQn);
aidan1971 0:8f28f25e3435 160
aidan1971 3:62c03088f256 161 /*!
aidan1971 3:62c03088f256 162 @brief Read 16bit register of SGTL5000
aidan1971 3:62c03088f256 163 @param reg_addr 16bit address of the codec control register
aidan1971 3:62c03088f256 164 @param data 16bit data to read from the address
aidan1971 3:62c03088f256 165 @param mask 16bit mask applied over the data read from the codec. The final returned value is the register data automatically shifted to the position of the first masked bit.
aidan1971 10:49bb33f71d32 166 @param _i2c_addr Default = 0. If none zero, overrides the address associated with the current object.
aidan1971 3:62c03088f256 167 @returns 0 = register data, -1 = fail
aidan1971 3:62c03088f256 168 */
aidan1971 10:49bb33f71d32 169 int32_t read_i2c(uint32_t reg_addr, uint32_t mask = 0xFFFF, int _i2c_addr = 0);
aidan1971 6:4ab5aaeaa064 170
aidan1971 3:62c03088f256 171 /*!
aidan1971 3:62c03088f256 172 @brief Write 16bit register of SGTL5000
aidan1971 3:62c03088f256 173 @param reg_addr 16bit address of the codec control register
aidan1971 3:62c03088f256 174 @param data 16bit data to write into the address
aidan1971 10:49bb33f71d32 175 @param _i2c_addr Default = 0. If none zero, overrides the address associated with the current object.
aidan1971 3:62c03088f256 176 @returns 0 = success, -1 = fail
aidan1971 3:62c03088f256 177 */
aidan1971 10:49bb33f71d32 178 int32_t write_i2c(uint32_t reg_addr, uint32_t data, int _i2c_addr = 0);
aidan1971 6:4ab5aaeaa064 179
aidan1971 3:62c03088f256 180 /*!
aidan1971 3:62c03088f256 181 @brief Modify masked bits within 16bit register of SGTL5000
aidan1971 3:62c03088f256 182 @param reg_addr 16bit address of the codec control register
aidan1971 3:62c03088f256 183 @param data 16bit data to write into the address
aidan1971 3:62c03088f256 184 @param mask 16bit mask of the bits to modify.
aidan1971 3:62c03088f256 185 The function automatically shifts the data to the position of the first masked bit.
aidan1971 10:49bb33f71d32 186 @param _i2c_addr Default = 0. If none zero, overrides the address associated with the current object.
aidan1971 3:62c03088f256 187 @returns 0 = success, -1 = fail
aidan1971 3:62c03088f256 188 */
aidan1971 10:49bb33f71d32 189 int32_t modify_i2c(uint32_t reg_addr, uint32_t data, uint32_t mask, int _i2c_addr = 0);
aidan1971 6:4ab5aaeaa064 190
aidan1971 3:62c03088f256 191 /*!
aidan1971 0:8f28f25e3435 192 @brief Attach a callback function to TX
aidan1971 3:62c03088f256 193 @param func Address of the user function to be called from the TX FIFO triggered ISR.
aidan1971 3:62c03088f256 194 This is blocking. If the user function does not complete before the next DMA completes the system will likely crash,
aidan1971 3:62c03088f256 195 however using this function avoids the latency of an IRQ stack push.
aidan1971 3:62c03088f256 196 @returns 0 = success, -1 = fail.
aidan1971 3:62c03088f256 197 Fails if already attached, must detach first.
aidan1971 0:8f28f25e3435 198 */
aidan1971 0:8f28f25e3435 199 int32_t attach_TX(Callback<void()> func);
aidan1971 6:4ab5aaeaa064 200
aidan1971 3:62c03088f256 201 /*!
aidan1971 3:62c03088f256 202 @brief Attach an ISR function to DMA TX
aidan1971 10:49bb33f71d32 203 @param user_ISR User function address pointer to be assigned as the NVIC vector for the DMA TX FIFO triggered user_ISR.
aidan1971 0:8f28f25e3435 204 @param irq_pri Set the system wide priority of the user_ISR.
aidan1971 0:8f28f25e3435 205 @param sw_irq The IRQ assigned. Default uses Reserved54_IRQn. See "MK20DX256.h" for available.
aidan1971 0:8f28f25e3435 206 This is non-blocking provided the priority of the IRQ associated with user_ISR is lower than the priority of the DMA triggered ISR.
aidan1971 0:8f28f25e3435 207 It can be useful to use a non-blocking call, however this involves the extra time needed to push the stack and manageing IRQ priorities
aidan1971 0:8f28f25e3435 208 across the whole system needs consideration.
aidan1971 0:8f28f25e3435 209 */
aidan1971 10:49bb33f71d32 210 int32_t attach_TX_NB(void* user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved54_IRQn);
aidan1971 6:4ab5aaeaa064 211
aidan1971 3:62c03088f256 212 /*!
aidan1971 0:8f28f25e3435 213 @brief Stop TX channel and flag as detached.
aidan1971 0:8f28f25e3435 214 During running stream, the callback based function can not be changed. However changes to the NB IRQ based attachment can have the vector changed on-the-fly
aidan1971 0:8f28f25e3435 215 */
aidan1971 8:9fdf8501d14b 216 int32_t detach_TX(void);
aidan1971 0:8f28f25e3435 217
aidan1971 3:62c03088f256 218 /*!
aidan1971 10:49bb33f71d32 219 @brief Disables I2S TX function. This stops all DMA requests and supresses any IRQs from the driver and tristates the inbound CODEC I2S interface.
aidan1971 10:49bb33f71d32 220 It also stops bit clocks and word sync clocks.
aidan1971 10:49bb33f71d32 221 Note: Stopping the TX will also stop the RX stream because the RX is synchronous to the TX function. It is recommended that TX is the last enabled and first disabled.
aidan1971 0:8f28f25e3435 222 */
aidan1971 8:9fdf8501d14b 223 int32_t stop_TX(void);
aidan1971 6:4ab5aaeaa064 224
aidan1971 3:62c03088f256 225 /*!
aidan1971 10:49bb33f71d32 226 @brief Pauses I2S TX channels. Halts the TX stream(s) by masking all data words.
aidan1971 10:49bb33f71d32 227 This can be used to suspend the codec when a user wishes to run critical tasks where IRQs must be suppressed. To restart call the resume function.
aidan1971 10:49bb33f71d32 228 */
aidan1971 10:49bb33f71d32 229 int32_t pause_TX(void);
aidan1971 10:49bb33f71d32 230
aidan1971 10:49bb33f71d32 231 /*!
aidan1971 10:49bb33f71d32 232 @brief Resumes a paused I2S TX channels. Resumes the TX stream(s) by un-masking all data words.
aidan1971 10:49bb33f71d32 233 */
aidan1971 10:49bb33f71d32 234 int32_t resume_TX(void);
aidan1971 10:49bb33f71d32 235
aidan1971 10:49bb33f71d32 236 /*!
aidan1971 0:8f28f25e3435 237 @brief Starts the codec I2S interface and begins transferring TX buffers. Transfers use DMA.
aidan1971 10:49bb33f71d32 238 @param _BufTX_L_safe A pointer address to the TX Left channel_0 data.
aidan1971 11:392c09372ae1 239 The address pointed to by the users pointer is managed by the library and changes to implement a double buffer.
aidan1971 0:8f28f25e3435 240 It is suggested that a suitable declaration in the users code would be in the form: 'q31_t *TX_AudioL = NULL;'
aidan1971 0:8f28f25e3435 241 To pass into the class, dereference this pointer and cast as uint32_t, as follows: 'codec.start_SYNC((uint32_t)&TX_AudioL .....'
aidan1971 10:49bb33f71d32 242 @param _BufTX_R_safe A pointer address to the TX Right channel_0 data.
aidan1971 9:40e0ff8c2ba2 243 @param _block_size 2 | 4 | 8 words of both Left and Right channels combined.
aidan1971 0:8f28f25e3435 244 This defines the number of samples that are transferred to the TX FIFO each time a FIFO demand is detected.
aidan1971 9:40e0ff8c2ba2 245 @param _packed_TX If true 2 * 16bit words for wire transmission are expected packed into a single 32bit word.
aidan1971 4:91354c908416 246 If False each 32bit word from the user should contain a single 16bit word for transmission.
aidan1971 9:40e0ff8c2ba2 247 @param _TX_shift True = The MS16bits of TX buffer are sent to the TX FIFO. Default = true.
aidan1971 0:8f28f25e3435 248 False = The LS16bits of TX buffer are sent to the TX FIFO.
aidan1971 4:91354c908416 249 If packed is true, then shift has no relevance.
aidan1971 9:40e0ff8c2ba2 250 @param _tx_DMAch Defines the system DMA channel to assign to the TX transfer. Default is 15.
aidan1971 0:8f28f25e3435 251 15 is used as default to avoid using channels 0 - 3 which are the only channels available for gated triggers.
aidan1971 0:8f28f25e3435 252 Gated triggering is not needed, so these 4 channels are avoided.
aidan1971 9:40e0ff8c2ba2 253 @param _DMA_irq_pri Default = 0. Highest priority. This is the priority of the I2S TX DMA demands.
aidan1971 10:49bb33f71d32 254 @param _BufTX_L_safe2 A pointer address to the TX Left channel_1 data.
aidan1971 10:49bb33f71d32 255 @param _BufTX_R_safe2 A pointer address to the TX Right channel_1 data.
aidan1971 3:62c03088f256 256 @returns 0 = success, -1 = fail
aidan1971 3:62c03088f256 257 Fails on variable sanity checks.
aidan1971 0:8f28f25e3435 258 */
aidan1971 9:40e0ff8c2ba2 259 int32_t start_TX(uint32_t _BufTX_L_safe, uint32_t _BufTX_R_safe,
aidan1971 10:49bb33f71d32 260 uint32_t _block_size = 4, bool _packed_TX = false, bool _TX_shift = true, uint32_t _TX_DMAch = 15, uint32_t _DMA_irq_pri = 0, uint32_t _BufTX_L_safe2 = NULL, uint32_t _BufTX_R_safe2 = NULL);
aidan1971 0:8f28f25e3435 261
aidan1971 3:62c03088f256 262 /*!
aidan1971 0:8f28f25e3435 263 @brief Attach a callback function to RX
aidan1971 0:8f28f25e3435 264 @param func User function to be called from the RX FIFO triggered ISR.
aidan1971 0:8f28f25e3435 265 This is blocking. If the user function does not complete before the next DMA completes the system will crash,
aidan1971 0:8f28f25e3435 266 however using this function avoids the latency of a stack push.
aidan1971 3:62c03088f256 267 @returns 0 = success, -1 = fail.
aidan1971 3:62c03088f256 268 Fails if already attached, must detach first.
aidan1971 0:8f28f25e3435 269 */
aidan1971 0:8f28f25e3435 270 int32_t attach_RX(Callback<void()> func);
aidan1971 6:4ab5aaeaa064 271
aidan1971 3:62c03088f256 272 /*!
aidan1971 3:62c03088f256 273 @brief Attach an ISR function to DMA RX
aidan1971 10:49bb33f71d32 274 @param user_ISR User function address pointer to be assigned as the NVIC vector for the DMA RX FIFO triggered user_ISR.
aidan1971 0:8f28f25e3435 275 @param irq_pri Set the system wide priority of the user_ISR.
aidan1971 0:8f28f25e3435 276 @param sw_irq The IRQ assigned. Default uses Reserved55_IRQn. See "MK20DX256.h" for available.
aidan1971 0:8f28f25e3435 277 This is non-blocking provided the priority of the IRQ associated with user_ISR is lower than the priority of the DMA triggered ISR.
aidan1971 0:8f28f25e3435 278 It can be useful to use a non-blocking call, however this involves the extra time needed to push the stack and manageing IRQ priorities
aidan1971 0:8f28f25e3435 279 across the whole system needs consideration.
aidan1971 0:8f28f25e3435 280 */
aidan1971 10:49bb33f71d32 281 int32_t attach_RX_NB(void* user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved55_IRQn);
aidan1971 6:4ab5aaeaa064 282
aidan1971 3:62c03088f256 283 /*!
aidan1971 0:8f28f25e3435 284 @brief Stop RX channel and flag as detached.
aidan1971 9:40e0ff8c2ba2 285 During running stream, the callback based function can not be changed. However changes to the NB IRQ based attachment can have the vector changed on-the-fly
aidan1971 0:8f28f25e3435 286 */
aidan1971 8:9fdf8501d14b 287 int32_t detach_RX(void);
aidan1971 0:8f28f25e3435 288
aidan1971 3:62c03088f256 289 /*!
aidan1971 10:49bb33f71d32 290 @brief Disables I2S RX function. Stops all DMA requests and supresses any IRQs from the driver and tristates the outbound CODEC I2S interface(s).
aidan1971 10:49bb33f71d32 291 Note: Bit clock and Word Sync clock will continue as long as TX is running (started).
aidan1971 0:8f28f25e3435 292 */
aidan1971 8:9fdf8501d14b 293 int32_t stop_RX(void);
aidan1971 6:4ab5aaeaa064 294
aidan1971 3:62c03088f256 295 /*!
aidan1971 10:49bb33f71d32 296 @brief Pauses I2S RX channels. Halts the RX stream(s) by masking all data words.
aidan1971 10:49bb33f71d32 297 This can be used to suspend the codec when a user wishes to run critical tasks where IRQs must be suppressed. To restart call the resume function.
aidan1971 10:49bb33f71d32 298 */
aidan1971 10:49bb33f71d32 299 int32_t pause_RX(void);
aidan1971 10:49bb33f71d32 300
aidan1971 10:49bb33f71d32 301 /*!
aidan1971 10:49bb33f71d32 302 @brief Resumes a paused I2S RX channels. Resumes the RX stream(s) by un-masking all data words.
aidan1971 10:49bb33f71d32 303 */
aidan1971 10:49bb33f71d32 304 int32_t resume_RX(void);
aidan1971 10:49bb33f71d32 305
aidan1971 10:49bb33f71d32 306 /*!
aidan1971 0:8f28f25e3435 307 @brief Starts the codec I2S interface and begins transferring RX buffers. Transfers use DMA.
aidan1971 10:49bb33f71d32 308 @param _BufRX_L_safe A pointer address to the RX Left channel_0 data.
aidan1971 11:392c09372ae1 309 The address pointed to by the users pointer is managed by the library and changes to implement a double buffer.
aidan1971 0:8f28f25e3435 310 It is suggested that a suitable declaration in the users code would be in the form: 'q31_t *RX_AudioL = NULL;'
aidan1971 0:8f28f25e3435 311 To pass into the class, dereference this pointer and cast as uint32_t, as follows: 'codec.start_SYNC((uint32_t)&RX_AudioL .....'
aidan1971 10:49bb33f71d32 312 @param _BufRX_R_safe A pointer address to the RX Right channel_0 data.
aidan1971 9:40e0ff8c2ba2 313 @param _block_size 2 | 4 | 8 words of both Left and Right channels combined.
aidan1971 0:8f28f25e3435 314 This defines the number of samples that are transferred to the RX FIFO each time a FIFO demand is detected.
aidan1971 9:40e0ff8c2ba2 315 @param _packed_RX If true the 2 * 16bit words from the codec are packed into a single 32bit word towards the user. This allows user code to use SIMD operations on the data
aidan1971 4:91354c908416 316 If False a single 16bit word from the wire is placed into a single 32bit word towards the user.
aidan1971 9:40e0ff8c2ba2 317 @param _RX_shift True = The 16bits of RX FIFO data are shifted to the MSBs of the RX buffer. Default = true
aidan1971 0:8f28f25e3435 318 False = The 16bits of RX FIFO data are placed in the LSBs of the RX buffer
aidan1971 0:8f28f25e3435 319 Note: If data is not shifted, the 32bit word delivered to the user will not be sign extended.
aidan1971 4:91354c908416 320 If packed is true, then shift has no relevance.
aidan1971 9:40e0ff8c2ba2 321 @param _rx_DMAch Defines the system DMA channel to assign to the RX transfer. Default is 14.
aidan1971 0:8f28f25e3435 322 14 is used as default to avoid using channels 0 - 3 which are the only channels available for gated triggers.
aidan1971 0:8f28f25e3435 323 Gated triggering is not needed, so these 4 channels are avoided.
aidan1971 9:40e0ff8c2ba2 324 @param _DMA_irq_pri Default = 0. Highest priority. This is the priority of the I2S RX DMA demands.
aidan1971 10:49bb33f71d32 325 @param _BufRX_L_safe2 A pointer address to the RX Left channel_1 data.
aidan1971 10:49bb33f71d32 326 @param _BufRX_R_safe2 A pointer address to the RX Right channel_1 data.
aidan1971 3:62c03088f256 327 @returns 0 = success, -1 = fail
aidan1971 3:62c03088f256 328 Fails on variable sanity checks.
aidan1971 0:8f28f25e3435 329 */
aidan1971 9:40e0ff8c2ba2 330 int32_t start_RX(uint32_t _BufRX_L_safe, uint32_t _BufRX_R_safe,
aidan1971 10:49bb33f71d32 331 uint32_t _block_size = 4, bool _packed_RX = false, bool _RX_shift = true, uint32_t _RX_DMAch = 14, uint32_t _DMA_irq_pri = 0, uint32_t _BufRX_L_safe2 = NULL, uint32_t _BufRX_R_safe2 = NULL);
aidan1971 0:8f28f25e3435 332
aidan1971 3:62c03088f256 333 /*!
aidan1971 0:8f28f25e3435 334 @brief Attach a callback function to DMA SYNC
aidan1971 0:8f28f25e3435 335 @param func User function to be called from the DMA SYNC FIFO triggered ISR.
aidan1971 0:8f28f25e3435 336 This is blocking. If the user function does not complete before the next DMA triggered IRQ the system will crash,
aidan1971 0:8f28f25e3435 337 however using this function avoids the latency of a stack push.
aidan1971 3:62c03088f256 338 @returns 0 = success, -1 = fail.
aidan1971 3:62c03088f256 339 Fails if already attached, must detach first.
aidan1971 0:8f28f25e3435 340 */
aidan1971 0:8f28f25e3435 341 int32_t attach_SYNC(Callback<void()> func);
aidan1971 6:4ab5aaeaa064 342
aidan1971 10:49bb33f71d32 343
aidan1971 3:62c03088f256 344 /*!
aidan1971 0:8f28f25e3435 345 @brief Attach a ISR function to DMA SYNC
aidan1971 10:49bb33f71d32 346 @param user_ISR User function address pointer to be assigned as the NVIC vector for the DMA SYNC FIFO triggered user_ISR.
aidan1971 0:8f28f25e3435 347 @param irq_pri Set the system wide priority of the user_ISR.
aidan1971 0:8f28f25e3435 348 @param sw_irq The IRQ assigned. Default uses Reserved53_IRQn. See "MK20DX256.h" for available.
aidan1971 0:8f28f25e3435 349 This creates a non-blocking call, which tests to see if the users ISR has completed before calling again.
aidan1971 0:8f28f25e3435 350 It requires that the priority of the IRQ associated with user_ISR is lower than the priority of the DMA triggered ISR.
aidan1971 0:8f28f25e3435 351 It can be useful to use a non-blocking call, however this involves the extra time needed to push the stack and manageing IRQ priorities
aidan1971 0:8f28f25e3435 352 across the whole system needs consideration.
aidan1971 0:8f28f25e3435 353 */
aidan1971 10:49bb33f71d32 354 int32_t attach_SYNC_NB(void* user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved53_IRQn);
aidan1971 0:8f28f25e3435 355
aidan1971 3:62c03088f256 356 /*!
aidan1971 0:8f28f25e3435 357 @brief Stop both TX & RX channels and flag as detached.
aidan1971 0:8f28f25e3435 358 During running stream, the callback based function can not be changed. However changes to the NB IRQ based attachment can have the vector changed on-the-fly
aidan1971 0:8f28f25e3435 359 */
aidan1971 8:9fdf8501d14b 360 int32_t detach_SYNC(void);
aidan1971 0:8f28f25e3435 361
aidan1971 3:62c03088f256 362 /**
aidan1971 0:8f28f25e3435 363 @brief Starts the codec I2S interface and begins transferring RX and TX buffers. Transfers use DMA.
aidan1971 10:49bb33f71d32 364 @param _BufRX_L_safe A pointer address to the RX Left channel_0 data.
aidan1971 11:392c09372ae1 365 The address pointed to by the users pointer is managed by the library and changes to implement a double buffer.
aidan1971 0:8f28f25e3435 366 It is suggested that a suitable declaration in the users code would be in the form: 'q31_t *RX_AudioL = NULL;'
aidan1971 0:8f28f25e3435 367 To pass into the class, dereference this pointer and cast as uint32_t, as follows: 'codec.start_SYNC((uint32_t)&RX_AudioL .....'
aidan1971 10:49bb33f71d32 368 @param _BufRX_R_safe A pointer address to the RX Right channel_0 data.
aidan1971 10:49bb33f71d32 369 @param _BufTX_L_safe A pointer address to the TX Left channel_0 data.
aidan1971 10:49bb33f71d32 370 @param _BufTX_R_safe A pointer address to the TX Right channel_0 data.
aidan1971 9:40e0ff8c2ba2 371 @param _block_size 2 | 4 | 8 words of both Left and Right channels combined.
aidan1971 0:8f28f25e3435 372 This defines the number of samples that are transferred to both FIFOs each time a FIFO demand is detected.
aidan1971 9:40e0ff8c2ba2 373 @param _packed_TX If true the 2 * 16bit words for wire transmission are expected packed into a single 32bit word.
aidan1971 4:91354c908416 374 If False each 32bit word from the user should contain a single 16bit word for transmission.
aidan1971 9:40e0ff8c2ba2 375 @param _packed_RX If true the 2 * 16bit words from the codec are packed into a single 32bit word towards the user. This allows user code to use SIMD operations on the data
aidan1971 4:91354c908416 376 If False a single 16bit word from the wire is placed into a single 32bit word towards the user.
aidan1971 0:8f28f25e3435 377 @param _RX_shift True = The 16bits of RX FIFO data are shifted to the MSBs of the RX buffer. Default = true
aidan1971 0:8f28f25e3435 378 False = The 16bits of RX FIFO data are placed in the LSBs of the RX buffer.
aidan1971 0:8f28f25e3435 379 Note: If data is not shifted, the 32bit word delivered to the user will not be sign extended.
aidan1971 4:91354c908416 380 If RX packed is true, then shift has no relevance.
aidan1971 0:8f28f25e3435 381 @param _TX_shift True = The MS16bits of TX buffer are sent to the TX FIFO. Default = true.
aidan1971 0:8f28f25e3435 382 False = The LS16bits of TX buffer are sent to the TX FIFO.
aidan1971 4:91354c908416 383 If TX packed is true, then shift has no relevance.
aidan1971 0:8f28f25e3435 384 @param _RX_DMAch Defines the system DMA channel to assign to the RX transfer. Default is 14.
aidan1971 0:8f28f25e3435 385 @param _TX_DMAch Defines the system DMA channel to assign to the TX transfer. Default is 15.
aidan1971 0:8f28f25e3435 386 14 & 15 are used as default to avoid using channels 0 - 3 which are the only channels available for gated triggers.
aidan1971 0:8f28f25e3435 387 Gated triggering is not needed, so these 4 channels are avoided.
aidan1971 9:40e0ff8c2ba2 388 @param _DMA_irq_pri Default = 0. Highest priority. This is the priority of the I2S DMA demands.
aidan1971 10:49bb33f71d32 389 @param _BufRX_L_safe2 A pointer address to the RX Left channel_1 data.
aidan1971 10:49bb33f71d32 390 @param _BufRX_R_safe2 A pointer address to the RX Right channel_1 data.
aidan1971 10:49bb33f71d32 391 @param _BufTX_L_safe2 A pointer address to the TX Left channel_1 data.
aidan1971 10:49bb33f71d32 392 @param _BufTX_R_safe2 A pointer address to the TX Right channel_1 data.
aidan1971 3:62c03088f256 393 @returns 0 = success, -1 = fail
aidan1971 3:62c03088f256 394 Fails on variable sanity checks.
aidan1971 0:8f28f25e3435 395 */
aidan1971 9:40e0ff8c2ba2 396 int32_t start_SYNC(uint32_t _BufRX_L_safe, uint32_t _BufRX_R_safe, uint32_t _BufTX_L_safe, uint32_t _BufTX_R_safe,
aidan1971 10:49bb33f71d32 397 uint32_t _block_size = 4, bool _packed_RX = false, bool _packed_TX = false, bool _RX_shift = true, bool _TX_shift = true, uint32_t _RX_DMAch = 14, uint32_t _TX_DMAch = 15, uint32_t _DMA_irq_pri = 0, uint32_t _BufRX_L_safe2 = NULL, uint32_t _BufRX_R_safe2 = NULL, uint32_t _BufTX_L_safe2 = NULL, uint32_t _BufTX_R_safe2 = NULL);
aidan1971 0:8f28f25e3435 398
aidan1971 3:62c03088f256 399 /*!
aidan1971 10:49bb33f71d32 400 @brief Stops I2S TX & RX channels. Stops all DMA requests and supresses any IRQs from the driver and tristates the CODEC I2S interface.
aidan1971 0:8f28f25e3435 401 */
aidan1971 8:9fdf8501d14b 402 int32_t stop_SYNC(void);
aidan1971 0:8f28f25e3435 403
aidan1971 3:62c03088f256 404 /*!
aidan1971 10:49bb33f71d32 405 @brief Pauses I2S RX & TX channels. Halts the RX & TX stream(s) by masking all data words.
aidan1971 10:49bb33f71d32 406 This can be used to suspend the codec when a user wishes to run critical tasks where IRQs must be suppressed. To restart call the resume function.
aidan1971 10:49bb33f71d32 407 */
aidan1971 10:49bb33f71d32 408 int32_t pause_SYNC(void);
aidan1971 10:49bb33f71d32 409
aidan1971 10:49bb33f71d32 410 /*!
aidan1971 10:49bb33f71d32 411 @brief Resumes a paused I2S RX & TX channels. Resumes the RX & TX stream(s) by un-masking all data words.
aidan1971 10:49bb33f71d32 412 */
aidan1971 10:49bb33f71d32 413 int32_t resume_SYNC(void);
aidan1971 10:49bb33f71d32 414
aidan1971 10:49bb33f71d32 415 /*!
aidan1971 10:49bb33f71d32 416 @brief Set codec and I2S Sampling frequency
aidan1971 0:8f28f25e3435 417 @param rate 8, 11, 12, 16, 22, 24, 32, 44, 48, 96, 192
aidan1971 0:8f28f25e3435 418 Base sampling rate of the codec
aidan1971 0:8f28f25e3435 419
aidan1971 0:8f28f25e3435 420 In all cases the SGTL5000 is programmed to use MCLK 256 times faster than sampling freq.
aidan1971 0:8f28f25e3435 421 MCU MCLK output = MCLK_Input((FRACT + 1)/(DIVIDE + 1))
aidan1971 0:8f28f25e3435 422 MCU MCLK Divide Register ratio is therefore = (Fs * 256)/PLL Clk
aidan1971 10:49bb33f71d32 423 The Teensy 3.1 & 3.2 have PLL freq @ 96MHz. However 120Mhz is supported dependent on the global 'SystemCoreClock' variable indicating this.
aidan1971 0:8f28f25e3435 424
aidan1971 3:62c03088f256 425 Note: To achieve some of these rates the codec SYS_FS is adjusted.
aidan1971 0:8f28f25e3435 426 This needs to be considered for several internal codec processes such as filter co-efficients and AVC.
aidan1971 3:62c03088f256 427 @returns 0 = success, -1 = fail
aidan1971 0:8f28f25e3435 428 */
aidan1971 10:49bb33f71d32 429 int32_t sample_rate(uint32_t rate);
aidan1971 10:49bb33f71d32 430
aidan1971 8:9fdf8501d14b 431 /*!
aidan1971 8:9fdf8501d14b 432 @brief Initialise codec.
aidan1971 10:49bb33f71d32 433 Resets the codec and sends initial default configuration data to the codec over I2C.
aidan1971 8:9fdf8501d14b 434 This function must be called after instantiation and before most other functions. It allows control over when I2C communications with the codec takes place.
aidan1971 8:9fdf8501d14b 435 Failure to initialize will prevent operation of the codec. However it possible to attach and detach functions before init.
aidan1971 8:9fdf8501d14b 436 @returns 0 = success, -1 = fail
aidan1971 8:9fdf8501d14b 437 */
aidan1971 8:9fdf8501d14b 438 int32_t init(void);
aidan1971 10:49bb33f71d32 439
aidan1971 3:62c03088f256 440 /*!
aidan1971 3:62c03088f256 441 @brief Read debug data from the codec
aidan1971 3:62c03088f256 442 @param index 0-15
aidan1971 10:49bb33f71d32 443 @param finished a simple semaphore, indicating that debug data should be aquired again. (Can be used in combination with the internal bool SGTL5000::debug_read, to gate data collection.)
aidan1971 3:62c03088f256 444 Just a simple way for user code to grab running variables if you need it.
aidan1971 3:62c03088f256 445 @returns 0 = success, -1 = fail
aidan1971 3:62c03088f256 446 */
aidan1971 10:49bb33f71d32 447 int32_t read_debug(uint32_t index, bool finished = false);
aidan1971 10:49bb33f71d32 448 static bool debug_read;
aidan1971 0:8f28f25e3435 449
aidan1971 0:8f28f25e3435 450
aidan1971 0:8f28f25e3435 451 protected:
aidan1971 6:4ab5aaeaa064 452 static uint32_t debug[16];
aidan1971 10:49bb33f71d32 453 static void t_stamp_start(void);
aidan1971 10:49bb33f71d32 454 static void t_stamp_stop(void);
aidan1971 10:49bb33f71d32 455 static uint32_t t1;
aidan1971 10:49bb33f71d32 456 static uint32_t proc_time;
aidan1971 10:49bb33f71d32 457 static uint32_t SYST_CVAL;// = 0xE000E018; // Address of SysTick current value register
aidan1971 0:8f28f25e3435 458
aidan1971 0:8f28f25e3435 459
aidan1971 0:8f28f25e3435 460 private:
aidan1971 10:49bb33f71d32 461
aidan1971 10:49bb33f71d32 462 static IRQn CODEC_CTRL_IRQ; // Default IRQ used to elevate priority of user commands.
aidan1971 10:49bb33f71d32 463 static uint32_t ctrl_command; // Command translation
aidan1971 10:49bb33f71d32 464 #define STOP_SYNC 0x1
aidan1971 10:49bb33f71d32 465 #define STOP_TX 0x2
aidan1971 10:49bb33f71d32 466 #define STOP_RX 0x3
aidan1971 10:49bb33f71d32 467 #define PAUSE_TX 0x4
aidan1971 10:49bb33f71d32 468 #define PAUSE_RX 0x5
aidan1971 10:49bb33f71d32 469 #define PAUSE_SYNC 0x6
aidan1971 10:49bb33f71d32 470 #define RESUME_TX 0x7
aidan1971 10:49bb33f71d32 471 #define RESUME_RX 0x8
aidan1971 10:49bb33f71d32 472 #define RESUME_SYNC 0x9
aidan1971 10:49bb33f71d32 473
aidan1971 10:49bb33f71d32 474 static void stream_ctrl_ISR(void); // High priority control commands trigger by user through software ISR
aidan1971 10:49bb33f71d32 475
aidan1971 10:49bb33f71d32 476
aidan1971 7:d65476c153a4 477 I2C mI2C; // Create I2C instance
aidan1971 10:49bb33f71d32 478
aidan1971 10:49bb33f71d32 479 int i2c_addr;
aidan1971 0:8f28f25e3435 480
aidan1971 10:49bb33f71d32 481 int32_t init_i2s(void); // Configure I2S Default Settings
aidan1971 0:8f28f25e3435 482
aidan1971 8:9fdf8501d14b 483 int32_t init_codec(void); // Configure codec Default Settings
aidan1971 0:8f28f25e3435 484
aidan1971 0:8f28f25e3435 485 void init_DMA(void); // Configure SYNC DMA settings on MK20DX256
aidan1971 0:8f28f25e3435 486
aidan1971 10:49bb33f71d32 487 static void SYNC_TX_dma_ISR(void);
aidan1971 6:4ab5aaeaa064 488
aidan1971 10:49bb33f71d32 489 static void SYNC_RX_dma_ISR(void);
aidan1971 0:8f28f25e3435 490
aidan1971 10:49bb33f71d32 491 static void SYNC_pointer_swap(void);
aidan1971 0:8f28f25e3435 492
aidan1971 10:49bb33f71d32 493 static void TX_dma_ISR(void); // Handle TX DMA transfers complete.
aidan1971 6:4ab5aaeaa064 494
aidan1971 10:49bb33f71d32 495 static void RX_dma_ISR(void); // Handle RX DMA transfers complete using Callback
aidan1971 0:8f28f25e3435 496
aidan1971 9:40e0ff8c2ba2 497 static void tx_I2S_WS_ISR(void); // Handle TX word start allignment
aidan1971 0:8f28f25e3435 498
aidan1971 9:40e0ff8c2ba2 499 static void rx_I2S_WS_ISR(void); // Handle RX word start allignment
aidan1971 0:8f28f25e3435 500
aidan1971 9:40e0ff8c2ba2 501 static void sync_I2S_WS_ISR(void); // Handle SYNC word start allignment
aidan1971 0:8f28f25e3435 502
aidan1971 0:8f28f25e3435 503
aidan1971 10:49bb33f71d32 504 //const uint32_t DMA_INT_ADDR = 0x40008024; // Hard code the DMA->INT address its faster when inline assembler optimises
aidan1971 6:4ab5aaeaa064 505
aidan1971 10:49bb33f71d32 506
aidan1971 10:49bb33f71d32 507 static volatile uint32_t * volatile BufRX_L_safe; // Define statics for ISRs
aidan1971 10:49bb33f71d32 508 static volatile uint32_t * volatile BufRX_R_safe;
aidan1971 10:49bb33f71d32 509 static volatile uint32_t * volatile BufTX_L_safe;
aidan1971 10:49bb33f71d32 510 static volatile uint32_t * volatile BufTX_R_safe;
aidan1971 10:49bb33f71d32 511 static volatile uint32_t * volatile BufRX_L_safe2; // Define statics for ISRs
aidan1971 10:49bb33f71d32 512 static volatile uint32_t * volatile BufRX_R_safe2;
aidan1971 10:49bb33f71d32 513 static volatile uint32_t * volatile BufTX_L_safe2;
aidan1971 10:49bb33f71d32 514 static volatile uint32_t * volatile BufTX_R_safe2;
aidan1971 10:49bb33f71d32 515 static uint32_t BufRX_L_safeA; // Precalculated double buffer addresses
aidan1971 10:49bb33f71d32 516 static uint32_t BufRX_R_safeA;
aidan1971 10:49bb33f71d32 517 static uint32_t BufTX_L_safeA;
aidan1971 10:49bb33f71d32 518 static uint32_t BufTX_R_safeA;
aidan1971 10:49bb33f71d32 519 static uint32_t BufRX_L_safeB; // Precalculated double buffer addresses
aidan1971 10:49bb33f71d32 520 static uint32_t BufRX_R_safeB;
aidan1971 10:49bb33f71d32 521 static uint32_t BufTX_L_safeB;
aidan1971 10:49bb33f71d32 522 static uint32_t BufTX_R_safeB;
aidan1971 10:49bb33f71d32 523 static uint32_t BufRX_L_safeA2; // Precalculated double buffer addresses
aidan1971 10:49bb33f71d32 524 static uint32_t BufRX_R_safeA2;
aidan1971 10:49bb33f71d32 525 static uint32_t BufTX_L_safeA2;
aidan1971 10:49bb33f71d32 526 static uint32_t BufTX_R_safeA2;
aidan1971 10:49bb33f71d32 527 static uint32_t BufRX_L_safeB2; // Precalculated double buffer addresses
aidan1971 10:49bb33f71d32 528 static uint32_t BufRX_R_safeB2;
aidan1971 10:49bb33f71d32 529 static uint32_t BufTX_L_safeB2;
aidan1971 10:49bb33f71d32 530 static uint32_t BufTX_R_safeB2;
aidan1971 10:49bb33f71d32 531 static uint32_t I2S_RX_Buffer[16];
aidan1971 10:49bb33f71d32 532 static uint32_t I2S_TX_Buffer[16];
aidan1971 10:49bb33f71d32 533 static uint32_t I2S_RX_Buffer2[16];
aidan1971 10:49bb33f71d32 534 static uint32_t I2S_TX_Buffer2[16];
aidan1971 10:49bb33f71d32 535 static uint32_t active_RX_DMAch_bm;
aidan1971 10:49bb33f71d32 536 static uint32_t active_TX_DMAch_bm;
aidan1971 10:49bb33f71d32 537 static uint32_t sync_dma_irq_acks;
aidan1971 0:8f28f25e3435 538 static IRQn SYNC_swIRQ;
aidan1971 0:8f28f25e3435 539 static IRQn TX_swIRQ;
aidan1971 0:8f28f25e3435 540 static IRQn RX_swIRQ;
aidan1971 0:8f28f25e3435 541 static Callback<void()> TX_user_func;
aidan1971 0:8f28f25e3435 542 static Callback<void()> RX_user_func;
aidan1971 0:8f28f25e3435 543 static Callback<void()> SYNC_user_func;
aidan1971 9:40e0ff8c2ba2 544 static uint32_t db_phase_sync;
aidan1971 9:40e0ff8c2ba2 545 static uint32_t db_phase_tx;
aidan1971 9:40e0ff8c2ba2 546 static uint32_t db_phase_rx;
aidan1971 10:49bb33f71d32 547 static uint32_t SYNC_attach_type;
aidan1971 10:49bb33f71d32 548 static uint32_t TX_attach_type;
aidan1971 10:49bb33f71d32 549 static uint32_t RX_attach_type;
aidan1971 10:49bb33f71d32 550 static bool i2s_configured;
aidan1971 10:49bb33f71d32 551 static bool codec2_active;
aidan1971 10:49bb33f71d32 552 static bool codec1_active;
aidan1971 10:49bb33f71d32 553 static uint32_t dump;
aidan1971 10:49bb33f71d32 554 static uint32_t NULL_DMA_TX;
aidan1971 10:49bb33f71d32 555 static uint32_t NULL_DMA_RX;
aidan1971 10:49bb33f71d32 556 static uint32_t RX_DMAch;
aidan1971 10:49bb33f71d32 557 static uint32_t TX_DMAch;
aidan1971 10:49bb33f71d32 558 static bool SYNC_run;
aidan1971 10:49bb33f71d32 559 static bool TX_run;
aidan1971 10:49bb33f71d32 560 static bool RX_run;
aidan1971 0:8f28f25e3435 561
aidan1971 10:49bb33f71d32 562 int i2c_freq;
aidan1971 6:4ab5aaeaa064 563 uint32_t TX_block_size;
aidan1971 6:4ab5aaeaa064 564 uint32_t RX_block_size;
aidan1971 0:8f28f25e3435 565 uint32_t TX_bs_bytes;
aidan1971 0:8f28f25e3435 566 uint32_t RX_bs_bytes;
aidan1971 10:49bb33f71d32 567 bool ctrl_codec;
aidan1971 8:9fdf8501d14b 568 bool codec_configured;
aidan1971 9:40e0ff8c2ba2 569 bool codec_I2S_active;
aidan1971 10:49bb33f71d32 570
aidan1971 0:8f28f25e3435 571 bool SYNC_attached;
aidan1971 0:8f28f25e3435 572 bool TX_attached;
aidan1971 0:8f28f25e3435 573 bool RX_attached;
aidan1971 0:8f28f25e3435 574 bool TX_shift;
aidan1971 0:8f28f25e3435 575 bool RX_shift;
aidan1971 4:91354c908416 576 bool packed_RX;
aidan1971 4:91354c908416 577 bool packed_TX;
aidan1971 10:49bb33f71d32 578
aidan1971 10:49bb33f71d32 579
aidan1971 0:8f28f25e3435 580 };
aidan1971 0:8f28f25e3435 581 }
aidan1971 0:8f28f25e3435 582 #endif