NetBurner 3.5.7
PDF Version
sai.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5
137#include <predef.h>
138#include <sim.h>
139#include <nbrtos.h>
140#include <edma.h>
141
148#define SAI_HW_COUNT 3
149
151#define SAI_TXBD_COUNT 4
152
154#define SAI_RXBD_COUNT 4
155
156 // end of sai_constants
157
204typedef void (*SAI_BufferDoneFn_t)(void *buffer, int doneInvalid);
205
206 // end of sai_callbacks
207
220enum clkSrc_t
221{
224};
225
239
251
265
285
299
312
326
340
360
368enum bitOrder_t
369{
373
381enum bufferDepletionBehavior_t
382{
385};
386
400
401 // end of sai_enums
402
531
563{
569 uint32_t txClkFreq{12288000};
570
575 uint32_t rxClkFreq{12288000};
576
579
581 I2S_Type *sai;
582
585
588};
589
590 // end of sai_structs
591
610
613
616
619
622
625
628
631
634
635 // end of I2S slave configs
636
647
650
653
656
659
662
665
668
671
672 // end of left-justified configs
673 // end of sai_presets
674
731{
732public:
745
746private:
747 // Static members for managing all SAI instances
748 static DmaTcd sDmaTcd[SAI_HW_COUNT][SAI_TXBD_COUNT + SAI_RXBD_COUNT];
749 static SAICtx_t *sCtx[SAI_HW_COUNT];
750 static int sDmaIsr(void *ctx, DmaCh *dmaCh, bool haveError);
751 static void sIsr();
752
753 // Hardware and DMA resources
754 I2S_Type *sai;
755 DmaCh *txDma;
756 uint32_t txBDFlag;
757 DmaCh *rxDma;
758 uint32_t rxBDFlag;
759
760 // Synchronization primitives
761 OS_SEM txSem;
762 OS_SEM rxSem;
763
764 // User callbacks
765 SAI_BufferDoneFn_t txBufDone;
766 SAI_BufferDoneFn_t rxBufDone;
767
768 // Statistics and status
769 uint32_t txReadyCount;
770 uint32_t rxReadyCount;
771 uint32_t nTxIrq;
772 uint32_t nTxErr;
773 uint32_t nTxPost;
774 uint32_t nTxCall;
775 uint32_t nTxStart;
776 uint32_t nTxAdd;
777 uint32_t nRxIrq;
778 uint32_t nRxErr;
779 uint32_t nRxPost;
780 uint32_t nRxCall;
781 uint32_t nRxStart;
782 uint32_t nRxAdd;
783 uint8_t stickyTxErr;
784 uint8_t stickyRxErr;
785
786 ctxState_t state;
787
788 // Internal methods
789 void InitBDs();
790 void initHw_Pins(const SAI_cfg_t &cfg);
791 void initHw_SAI(const SAI_cfg_t &cfg);
792 void initHw_DMA(const SAI_cfg_t &cfg);
793 int validateClockSync(const SAI_cfg_t &cfg) const;
794 int validateCfg(const SAI_cfg_t &cfg) const;
795 int InitClocks(uint32_t *saiClk, uint32_t divRem);
796
797 DmaTcd &getFirstTxBD() const;
798 DmaTcd &getLastTxBD() const;
799 DmaTcd &getNextTxBD() const;
800 DmaTcd &getFirstRxBD() const;
801 DmaTcd &getLastRxBD() const;
802 DmaTcd &getNextRxBD() const;
803 DmaTcd &getFirstBD(const DmaCh *dma) const;
804 DmaTcd &getLastBD(const DmaCh *dma) const;
805 DmaTcd &getNextBD(const DmaCh *dma) const;
806
807 int queueBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded,
808 bool enableHalfNotify, DmaCh *dma);
809 int getIdx() const;
810 int DmaIsr(DmaCh *dma, bool haveError);
811 void Isr();
812
813public:
843 int Init(const SAI_cfg_t &cfg);
844
861 void Shutdown();
862
868 inline int GetMaxTxBuffers() { return SAI_TXBD_COUNT; }
869
875 inline int GetMaxRxBuffers() { return SAI_RXBD_COUNT; }
876
897
909 inline ctxState_t getState() const { return state; }
910
956 int TransmitBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded,
957 bool enableHalfNotify = false);
958
998 int ReadyReceiveBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded,
999 bool enableHalfNotify = false);
1000
1026
1051
1058 inline void clrStickyTxError() { stickyTxErr = 0; }
1059
1066 inline void clrStickyRxError() { stickyRxErr = 0; }
1067
1076 inline uint8_t getStickyTxError() { return stickyTxErr; }
1077
1086 inline uint8_t getStickyRxError() { return stickyRxErr; }
1087
1105 void dump();
1106};
1107
1108 // end of sai_driver
1109
1123extern SAICtx_t sai;
1124
1125
1126 // end of saiGroup
1127
Main driver class for SAI peripheral operation.
Definition sai.h:731
void Shutdown()
Shut down the SAI hardware and release driver resources.
void RegisterRxBufferCB(SAI_BufferDoneFn_t cb)
Register a callback for receive buffer completion.
int getCurrentConfig(SAI_cfg_t &cfg)
Retrieve the current active configuration.
ctxState_t getState() const
Get the current operational state of the driver.
Definition sai.h:909
int GetMaxTxBuffers()
Get the maximum number of transmit buffers that can be queued.
Definition sai.h:868
void RegisterTxBufferCB(SAI_BufferDoneFn_t cb)
Register a callback for transmit buffer completion.
uint8_t getStickyRxError()
Get the sticky receive error status.
Definition sai.h:1086
int GetMaxRxBuffers()
Get the maximum number of receive buffers that can be queued.
Definition sai.h:875
void dump()
Dump driver state and statistics to the console.
void clrStickyTxError()
Clear the sticky transmit error flag.
Definition sai.h:1058
uint8_t getStickyTxError()
Get the sticky transmit error status.
Definition sai.h:1076
void clrStickyRxError()
Clear the sticky receive error flag.
Definition sai.h:1066
int ReadyReceiveBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded, bool enableHalfNotify=false)
Queue a buffer for receiving data.
int Init(const SAI_cfg_t &cfg)
Initialize the SAI hardware and driver context.
int TransmitBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded, bool enableHalfNotify=false)
Queue a buffer for transmission.
ctxState_t
Current operational state of the SAI driver.
Definition sai.h:738
@ CTX_STATE_TXRX
Definition sai.h:743
@ CTX_STATE_STOPPED
Definition sai.h:739
@ CTX_STATE_INIT
Definition sai.h:740
@ CTX_STATE_TX
Definition sai.h:741
@ CTX_STATE_RX
Definition sai.h:742
SAICtx_t sai
Global SAI driver instance.
void(* SAI_BufferDoneFn_t)(void *buffer, int doneInvalid)
Callback function type for buffer completion notifications.
Definition sai.h:204
#define SAI_RXBD_COUNT
Definition sai.h:154
#define SAI_TXBD_COUNT
Definition sai.h:151
#define SAI_HW_COUNT
Definition sai.h:148
clkPol_t
Clock polarity configuration.
Definition sai.h:247
rxTxSync_t
Synchronization mode between RX and TX channels.
Definition sai.h:395
clkSrc_t
Clock source selection for SAI bit clock generation.
Definition sai.h:221
clkCh_t
Clock channel selection for determining which clock signal to use.
Definition sai.h:234
frameSyncOut_t
Frame synchronization signal output mode.
Definition sai.h:335
clkGate_t
Clock gating mode for power optimization.
Definition sai.h:294
bufferDepletionBehavior_t
DMA behavior when all queued buffers are exhausted.
Definition sai.h:382
frameEdge_t
Frame sync edge that indicates the start of a new frame.
Definition sai.h:308
dataPacking_t
Buffer word format and data justification.
Definition sai.h:353
clkOut_t
Bit clock output mode configuration.
Definition sai.h:321
startCond_t
Starting conditions that trigger SAI data transfers.
Definition sai.h:274
bitOrder_t
Bit transmission order within each word.
Definition sai.h:369
frameSyncCh_t
Frame synchronization channel selection.
Definition sai.h:260
@ CLK_ACTIVE_LOW
Definition sai.h:249
@ CLK_ACTIVE_HIGH
Definition sai.h:248
@ RXTX_SYNC_TX
Definition sai.h:398
@ RXTX_SYNC_RX
Definition sai.h:397
@ RXTX_ASYNC
Definition sai.h:396
@ CLK_SRC_MCK
Definition sai.h:222
@ CLK_SRC_EXT
Definition sai.h:223
@ CLK_CH_RK
Definition sai.h:236
@ CLK_CH_CH
Definition sai.h:235
@ CLK_CH_TK
Definition sai.h:237
@ FRAME_SYNC_NEGATIVE
Definition sai.h:337
@ FRAME_SYNC_INPUT
Definition sai.h:336
@ FRAME_SYNC_POSITIVE
Definition sai.h:338
@ CLK_GATE_FRAME_HIGH
Definition sai.h:297
@ CLK_GATE_FRAME_LOW
Definition sai.h:296
@ CLK_GATE_CONTINUOUS
Definition sai.h:295
@ DEPLETED_REPEAT_LAST
Definition sai.h:384
@ DEPLETED_PAUSE
Definition sai.h:383
@ FRAME_SYNC_RISING
Definition sai.h:309
@ FRAME_SYNC_FALLING
Definition sai.h:310
@ BUFFER_WORD8
Definition sai.h:354
@ BUFFER_WORD16_RIGHT
Definition sai.h:356
@ BUFFER_WORD16_LEFT
Definition sai.h:355
@ BUFFER_WORD32_RIGHT
Definition sai.h:358
@ BUFFER_WORD32_LEFT
Definition sai.h:357
@ CLK_OUT_CONTINUOUS
Definition sai.h:323
@ CLK_OUT_TRANSFER
Definition sai.h:324
@ CLK_OUT_INPUT
Definition sai.h:322
@ START_FRAME_RISING
Definition sai.h:280
@ START_FRAME_FALLING
Definition sai.h:279
@ START_FRAME_LEVEL
Definition sai.h:281
@ START_CONTINUOUS
Definition sai.h:275
@ START_FRAME_LOW
Definition sai.h:277
@ START_FRAME_HIGH
Definition sai.h:278
@ START_FRAME_EDGE
Definition sai.h:282
@ START_CMP_0
Definition sai.h:283
@ START_SYNC_RX_TX
Definition sai.h:276
@ MOST_SIG_FIRST
Definition sai.h:371
@ LEAST_SIG_FIRST
Definition sai.h:370
@ SYNC_CH_CH
Definition sai.h:261
@ SYNC_CH_RX
Definition sai.h:262
@ SYNC_CH_TX
Definition sai.h:263
const SAI_cfg_t SAI_I2S_SLAVE_8_TXRX_RK
const SAI_cfg_t SAI_LJUST_SLAVE_8_TXRX_TKRK
const SAI_cfg_t SAI_I2S_SLAVE_8_TXRX_TK
const SAI_cfg_t SAI_LJUST_SLAVE_24_TXRX_RK
const SAI_cfg_t SAI_I2S_SLAVE_16_TXRX_TKRK
const SAI_cfg_t SAI_LJUST_SLAVE_16_TXRX_TK
const SAI_cfg_t SAI_LJUST_SLAVE_16_TXRX_RK
const SAI_cfg_t SAI_I2S_SLAVE_24_TXRX_TKRK
const SAI_cfg_t SAI_LJUST_SLAVE_16_TXRX_TKRK
const SAI_cfg_t SAI_LJUST_SLAVE_24_TXRX_TKRK
const SAI_cfg_t SAI_LJUST_SLAVE_8_TXRX_TK
const SAI_cfg_t SAI_I2S_SLAVE_8_TXRX_TKRK
const SAI_cfg_t SAI_I2S_SLAVE_24_TXRX_TK
const SAI_cfg_t SAI_LJUST_SLAVE_24_TXRX_TK
const SAI_cfg_t SAI_I2S_SLAVE_24_TXRX_RK
const SAI_cfg_t SAI_I2S_SLAVE_16_TXRX_TK
const SAI_cfg_t SAI_I2S_SLAVE_16_TXRX_RK
const SAI_cfg_t SAI_LJUST_SLAVE_8_TXRX_RK
Complete configuration structure for the SAI driver.
Definition sai.h:563
SAI_rxtx_cfg_t rx
Definition sai.h:584
uint32_t txClkFreq
Definition sai.h:569
rxTxSync_t rxTxSync
Definition sai.h:578
I2S_Type * sai
Definition sai.h:581
SAI_rxtx_cfg_t tx
Definition sai.h:587
uint32_t rxClkFreq
Definition sai.h:575
Configuration structure for one direction (TX or RX) of the SAI module.
Definition sai.h:439
bool lineIdleState
Definition sai.h:510
clkCh_t clkCh
Definition sai.h:464
frameSyncCh_t syncCh
Definition sai.h:483
clkGate_t clkGate
Definition sai.h:455
dataPacking_t dataPacking
Definition sai.h:494
bufferDepletionBehavior_t depletionBehavior
Definition sai.h:529
frameEdge_t syncEdge
Definition sai.h:477
bool enable
Definition sai.h:441
clkSrc_t clkSrc
Definition sai.h:461
clkPol_t clkPol
Definition sai.h:467
uint8_t startDly
Definition sai.h:449
uint8_t wordsPerFrame
Definition sai.h:491
uint8_t bitsPerWord
Definition sai.h:516
frameSyncOut_t syncOut
Definition sai.h:480
bitOrder_t bitOrder
Definition sai.h:503
clkOut_t clkOut
Definition sai.h:458
startCond_t startCond
Definition sai.h:452
bool unifyDataStream
Definition sai.h:500
uint32_t wordMask
Definition sai.h:526
uint8_t syncLen
Definition sai.h:474