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.

Revision:
8:9fdf8501d14b
Parent:
7:d65476c153a4
Child:
9:40e0ff8c2ba2
--- a/sgtl5000.h	Wed Jul 05 17:30:08 2017 +0000
+++ b/sgtl5000.h	Fri Jul 07 11:38:57 2017 +0000
@@ -145,18 +145,18 @@
                         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
                         across the whole system needs consideration.
     */
-    void attach_TX_NB(uint32_t user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved54_IRQn);
+    int32_t attach_TX_NB(uint32_t user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved54_IRQn);
 
     /*!
     @brief Stop TX channel and flag as detached.
                         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
     */
-    void detach_TX(void);
+    int32_t detach_TX(void);
 
     /*!
     @brief Stops i2s TX channel but maintains clocking.
     */
-    void stop_TX(void);
+    int32_t stop_TX(void);
 
     /*!
     @brief  Starts the codec I2S interface and begins transferring TX buffers. Transfers use DMA.
@@ -202,18 +202,18 @@
                         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
                         across the whole system needs consideration.
     */
-    void attach_RX_NB(uint32_t user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved55_IRQn);
+    int32_t attach_RX_NB(uint32_t user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved55_IRQn);
 
     /*!
     @brief Stop RX channel and flag as detached.
                         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
     */
-    void detach_RX(void);
+    int32_t detach_RX(void);
 
     /*!
     @brief Stops i2s RX channel but maintains clocking.
     */
-    void stop_RX(void);
+    int32_t stop_RX(void);
 
     /*!
     @brief  Starts the codec I2S interface and begins transferring RX buffers. Transfers use DMA.
@@ -260,13 +260,13 @@
                         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
                         across the whole system needs consideration.
     */
-    void attach_SYNC_NB(uint32_t user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved53_IRQn);
+    int32_t attach_SYNC_NB(uint32_t user_ISR, uint32_t irq_pri = 1, IRQn sw_irq = Reserved53_IRQn);
 
     /*!
     @brief Stop both TX & RX channels and flag as detached.
                         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
     */
-    void detach_SYNC(void);
+    int32_t detach_SYNC(void);
 
     /**
     @brief  Starts the codec I2S interface and begins transferring RX and TX buffers. Transfers use DMA.
@@ -304,7 +304,7 @@
     /*!
     @brief Stops i2s TX & RX channels but maintains clocking.
     */
-    void stop_SYNC(void);
+    int32_t stop_SYNC(void);
 
     /*!
     @brief Set codec and i2s Sampling frequency
@@ -321,13 +321,23 @@
     @returns            0 = success, -1 = fail
     */
     int32_t freq(uint32_t rate);
+    
+    /*!
+    @brief  Initialise codec.
+                        Sends initial default configuration data to the codec over I2C.
+                        This function must be called after instantiation and before most other functions. It allows control over when I2C communications with the codec takes place.
+                        Failure to initialize will prevent operation of the codec. However it possible to attach and detach functions before init.
+    @returns            0 = success, -1 = fail
+    */
+    int32_t init(void);
+    
     /*!
     @brief  Read debug data from the codec
     @param  index       0-15
                         Just a simple way for user code to grab running variables if you need it.
     @returns            0 = success, -1 = fail
     */
-    uint32_t read_debug(uint32_t index);
+    int32_t read_debug(uint32_t index);
 
 
 protected:
@@ -340,7 +350,7 @@
 
     void init_i2s(void);                                                        // Configure I2S Default Settings
 
-    void init_codec(void);                                                      // Configure codec Default Settings
+    int32_t init_codec(void);                                                   // Configure codec Default Settings
 
     void init_DMA(void);                                                        // Configure SYNC DMA settings on MK20DX256
 
@@ -386,6 +396,7 @@
     uint32_t RX_block_size;
     uint32_t TX_bs_bytes;
     uint32_t RX_bs_bytes;
+    bool codec_configured;
     bool SYNC_run;
     bool TX_run;
     bool RX_run;