NetBurner 3.1
ssc_i2s.h
1 #ifndef __SSC_I2S_H
2 #define __SSC_I2S_H
3 /*NB_REVISION*/
4 
5 /*NB_COPYRIGHT*/
6 
7 #include <predef.h>
8 #include <sim.h> /*on-chip register definitions*/
9 #include <pins.h>
10 #include <nbrtos.h>
11 #include <xdmac.h>
12 
13 //class SSC_I2S : private Ssc {
14 //
15 //
16 //public:
17 // inline void Reset() { SSC_CR = SSC_CR_SWRST; }
18 // inline void EnableTx(bool enable) {
19 // SSC_CR = (enable ? SSC_CR_TXEN : SSC_CR_TXDIS);
20 // }
21 // inline void EnableRx(bool enable) {
22 // SSC_CR = (enable ? SSC_CR_RXEN : SSC_CR_RXDIS);
23 // }
24 // inline uint16_t SetClkDiv(uint16_t div) {
25 // return SSC_CMR = div & SSC_CMR_DIV_Msk;
26 // }
27 //
28 // static Ssc *get() { return reinterpret_cast<SSC_I2S*>(SSC); }
29 // Ssc &GetRegisers() { return *this; }
30 //
31 //} __attribute__((packed));
32 //
33 //static_assert( sizeof (SSC_I2S == sizeof(Ssc) );
34 //
35 
36 typedef void (*SSC_BufferDoneFn_t)(void *buffer, bool valid);
37 /****************************************/
38 /* SSC Configuration Enums */
39 /****************************************/
43 enum clkSrc_t {
44  CLK_SRC_MCK,
45  CLK_SRC_RK,
46  CLK_SRC_TK
47 };
48 
72 enum startCond_t {
73  START_CONTINUOUS,
74  START_SYNC_RX_TX,
75  START_FRAME_LOW,
76  START_FRAME_HIGH,
77  START_FRAME_FALLING,
78  START_FRAME_RISING,
79  START_FRAME_LEVEL,
80  START_FRAME_EDGE,
81  START_CMP_0
82 };
83 
87 enum clkGate_t {
88  CLK_GATE_CONTINUOUS,
89  CLK_GATE_FRAME_LOW,
90  CLK_GATE_FRAME_HIGH,
91 };
92 
96 enum dataValid_t {
97  DATA_VALID_RISING,
98  DATA_VALID_FALLING,
99 };
100 
104 enum frameEdge_t {
105  FRAME_SYNC_RISING,
106  FRAME_SYNC_FALLING
107 };
108 
112 enum clkOut_t {
113  CLK_OUT_INPUT,
115  CLK_OUT_CONTINUOUS,
116  CLK_OUT_TRANSFER
117 };
118 
122 enum frameSyncOut_t {
123  FRAME_SYNC_INPUT,
125  FRAME_SYNC_NEGATIVE,
127  FRAME_SYNC_POSITIVE,
129  FRAME_SYNC_LOW,
130  FRAME_SYNC_HIGH,
131  FRAME_SYNC_TOGGLE
132 };
133 
137 enum bitOrder_t {
138  LEAST_SIG_FIRST,
139  MOST_SIG_FIRST
140 };
141 
152 enum bufferDepletionBehavior_t {
153  DEPLETED_PAUSE,
154  DEPLETED_REPEAT_LAST,
155 };
156 
157 /****************************************/
158 /* SSC Configuration Struct */
159 /****************************************/
160 
204  bool enable{false};
205  uint8_t period{64};
206  uint8_t startDly{1};
207  startCond_t startCond{START_FRAME_EDGE};
208  clkGate_t clkGate{CLK_GATE_CONTINUOUS};
209  dataValid_t dataValid{DATA_VALID_RISING};
210  clkOut_t clkOut{CLK_OUT_INPUT};
211  clkSrc_t clkSrc{CLK_SRC_RK};
212 
213  uint8_t syncLen{0};
214  frameEdge_t syncEdge{FRAME_SYNC_FALLING};
215  bool syncDataEnabled{false};
216  frameSyncOut_t syncOut{FRAME_SYNC_INPUT};
217  uint8_t wordsPerFrame{1};
218  bitOrder_t bitOrder{MOST_SIG_FIRST};
219  bool lineIdleState{1};
220  uint8_t bitsPerWord{24};
221 
222  bufferDepletionBehavior_t depletionBehavior{DEPLETED_PAUSE};
223 };
224 
238 struct SSC_cfg_t {
239  uint16_t clkDiv{150};
240 
243 };
244 
285 extern const SSC_cfg_t SSC_I2S_SLAVE_24_TXRX_RK;
286 extern const SSC_cfg_t SSC_I2S_SLAVE_24_TXRX_TK;
287 extern const SSC_cfg_t SSC_I2S_SLAVE_24_TXRX_TKRK;
288 extern const SSC_cfg_t SSC_I2S_SLAVE_16_TXRX_RK;
289 extern const SSC_cfg_t SSC_I2S_SLAVE_16_TXRX_TK;
290 extern const SSC_cfg_t SSC_I2S_SLAVE_16_TXRX_TKRK;
291 extern const SSC_cfg_t SSC_I2S_SLAVE_8_TXRX_RK;
292 extern const SSC_cfg_t SSC_I2S_SLAVE_8_TXRX_TK;
293 extern const SSC_cfg_t SSC_I2S_SLAVE_8_TXRX_TKRK;
294 
295 extern const SSC_cfg_t SSC_LJUST_SLAVE_24_TXRX_RK;
296 extern const SSC_cfg_t SSC_LJUST_SLAVE_24_TXRX_TK;
297 extern const SSC_cfg_t SSC_LJUST_SLAVE_24_TXRX_TKRK;
298 extern const SSC_cfg_t SSC_LJUST_SLAVE_16_TXRX_RK;
299 extern const SSC_cfg_t SSC_LJUST_SLAVE_16_TXRX_TK;
300 extern const SSC_cfg_t SSC_LJUST_SLAVE_16_TXRX_TKRK;
301 extern const SSC_cfg_t SSC_LJUST_SLAVE_8_TXRX_RK;
302 extern const SSC_cfg_t SSC_LJUST_SLAVE_8_TXRX_TK;
303 extern const SSC_cfg_t SSC_LJUST_SLAVE_8_TXRX_TKRK;
304 
305 
306 
307 /****************************************/
308 /* SSC Driver Context Object */
309 /****************************************/
310 
317 class SSCCtx_t {
318 public:
319  enum ctxState_t {
320  CTX_STATE_STOPPED,
321  CTX_STATE_INIT,
322  CTX_STATE_TX,
323  CTX_STATE_RX,
324  CTX_STATE_TXRX,
325  };
326 
327 private:
328  XdmaCh_t * txCh;
329  XdmaCh_t * rxCh;
330  OS_SEM txSem;
331  OS_SEM rxSem;
332  SSC_BufferDoneFn_t txBufDone;
333  SSC_BufferDoneFn_t rxBufDone;
334  uint32_t txReadyCount;
335  uint32_t rxReadyCount;
336  uint32_t nTxIrq;
337  uint32_t nTxErr;
338  uint32_t nTxPost;
339  uint32_t nTxCall;
340  uint32_t nTxStart;
341  uint32_t nTxAdd;
342 
343  uint32_t nRxIrq;
344  uint32_t nRxErr;
345  uint32_t nRxPost;
346  uint32_t nRxCall;
347  uint32_t nRxStart;
348  uint32_t nRxAdd;
349 
350  Desc0Ring TxRing;
351  Desc0Ring RxRing;
352  uint8_t rxByteWidth;
353  uint8_t txByteWidth;
354 
355  ctxState_t state;
356  void initBDs(bool loopRx, bool loopTx);
357  void initHw_Pins(const SSC_cfg_t &cfg);
358  void initHw_SSC(const SSC_cfg_t &cfg);
359  void initHw_DMA(const SSC_cfg_t &cfg);
360 public:
361 
368  int Init(const SSC_cfg_t &cfg);
372  void Shutdown();
373 
380  int getCurrentConfig(SSC_cfg_t &cfg);
385  ctxState_t getState();
386 
387 
399  int TransmitBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded);
411  int ReadyReceiveBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded);
412 
416  void RegisterTxBufferDoneCB(SSC_BufferDoneFn_t cb);
420  void RegisterRxBufferDoneCB(SSC_BufferDoneFn_t cb);
421 
422  void TxIsr();
423  void RxIsr();
424 
425  void dump();
426 };
427 
428 extern SSCCtx_t ssc;
429 
430 #endif /* ----- #ifndef __SSC_I2S_H ----- */
ctxState_t getState()
Returns the current driver state.
NetBurner Real-Time Operating System API.
Configuration structure for a given direction (rx or tx) of the SSC module. Passed to the initialize ...
Definition: ssc_i2s.h:203
int ReadyReceiveBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded)
Hands off a buffer to be written to by the SSC driver.
Definition: ssc_i2s.cpp:484
bool lineIdleState
Definition: ssc_i2s.h:219
dataValid_t dataValid
Definition: ssc_i2s.h:209
uint8_t syncLen
Definition: ssc_i2s.h:213
Semaphores are used to control access to shared resource critical section, or to communicate between ...
Definition: nbrtos.h:318
uint8_t startDly
Definition: ssc_i2s.h:206
Configuration structure for the SSC driver. Passed to the initialize function to configure the hardwa...
Definition: ssc_i2s.h:238
Definition: ssc_i2s.h:317
clkSrc_t clkSrc
Definition: ssc_i2s.h:211
uint8_t period
Definition: ssc_i2s.h:205
uint8_t bitsPerWord
Definition: ssc_i2s.h:220
SSC_rxtx_cfg_t tx
Definition: ssc_i2s.h:242
bool enable
Definition: ssc_i2s.h:204
void Shutdown()
Shuts down the SSC hardware and driver.
Definition: ssc_i2s.cpp:388
startCond_t startCond
Definition: ssc_i2s.h:207
uint8_t wordsPerFrame
Definition: ssc_i2s.h:217
uint16_t clkDiv
Definition: ssc_i2s.h:239
void RegisterTxBufferDoneCB(SSC_BufferDoneFn_t cb)
Registers a callback for when a transmit buffer is finished.
Definition: ssc_i2s.cpp:557
bitOrder_t bitOrder
Definition: ssc_i2s.h:218
frameEdge_t syncEdge
Definition: ssc_i2s.h:214
clkGate_t clkGate
Definition: ssc_i2s.h:208
int getCurrentConfig(SSC_cfg_t &cfg)
Fills in the config object with the current active configuration.
SSC_rxtx_cfg_t rx
Definition: ssc_i2s.h:241
void RegisterRxBufferDoneCB(SSC_BufferDoneFn_t cb)
Registers a callback for when a receive buffer is finished.
Definition: ssc_i2s.cpp:562
frameSyncOut_t syncOut
Definition: ssc_i2s.h:216
clkOut_t clkOut
Definition: ssc_i2s.h:210
int TransmitBuffer(void *buffer, uint32_t bufferLen, bool waitIfNeeded)
Hands off a buffer to be transmitted by the SSC driver.
Definition: ssc_i2s.cpp:419
bufferDepletionBehavior_t depletionBehavior
Definition: ssc_i2s.h:222
bool syncDataEnabled
Definition: ssc_i2s.h:215
int Init(const SSC_cfg_t &cfg)
Initializes the SSC hardware and driver context.
Definition: ssc_i2s.cpp:365