1 /*
2 * Copyright 2017-2020 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_spdif.h"
10
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.spdif"
14 #endif
15
16 /*******************************************************************************
17 * Definitations
18 ******************************************************************************/
19 /*! @brief spdif transfer state. */
20 enum
21 {
22 kSPDIF_Busy = 0x0U, /*!< SPDIF is busy */
23 kSPDIF_Idle, /*!< Transfer is done. */
24 kSPDIF_Error /*!< Transfer error occurred. */
25 };
26
27 /*! @brief Typedef for spdif tx interrupt handler. */
28 typedef void (*spdif_isr_t)(SPDIF_Type *base, spdif_handle_t *handle);
29 /*******************************************************************************
30 * Prototypes
31 ******************************************************************************/
32
33 /*******************************************************************************
34 * Variables
35 ******************************************************************************/
36 /* Base pointer array */
37 static SPDIF_Type *const s_spdifBases[] = SPDIF_BASE_PTRS;
38 /*! @brief SPDIF handle pointer */
39 static spdif_handle_t *s_spdifHandle[ARRAY_SIZE(s_spdifBases)][2];
40 /* IRQ number array */
41 static const IRQn_Type s_spdifIRQ[] = SPDIF_IRQS;
42 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
43 /* Clock name array */
44 static const clock_ip_name_t s_spdifClock[] = SPDIF_CLOCKS;
45 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
46 /*! @brief Pointer to IRQ handler for each instance. */
47 static spdif_isr_t s_spdifTxIsr;
48 /*! @brief Pointer to IRQ handler for each instance. */
49 static spdif_isr_t s_spdifRxIsr;
50 /*! @brief Used for spdif gain */
51 static uint8_t s_spdif_gain[8] = {24U, 16U, 12U, 8U, 6U, 4U, 3U, 1U};
52 static uint8_t s_spdif_tx_watermark[4] = {16, 12, 8, 4};
53 static uint8_t s_spdif_rx_watermark[4] = {1, 4, 8, 16};
54
55 /*******************************************************************************
56 * Code
57 ******************************************************************************/
SPDIF_GetInstance(SPDIF_Type * base)58 uint32_t SPDIF_GetInstance(SPDIF_Type *base)
59 {
60 uint32_t instance;
61
62 /* Find the instance index from base address mappings. */
63 for (instance = 0; instance < ARRAY_SIZE(s_spdifBases); instance++)
64 {
65 if (s_spdifBases[instance] == base)
66 {
67 break;
68 }
69 }
70
71 assert(instance < ARRAY_SIZE(s_spdifBases));
72
73 return instance;
74 }
75
76 /*!
77 * brief Initializes the SPDIF peripheral.
78 *
79 * Ungates the SPDIF clock, resets the module, and configures SPDIF with a configuration structure.
80 * The configuration structure can be custom filled or set with default values by
81 * SPDIF_GetDefaultConfig().
82 *
83 * note This API should be called at the beginning of the application to use
84 * the SPDIF driver. Otherwise, accessing the SPDIF module can cause a hard fault
85 * because the clock is not enabled.
86 *
87 * param base SPDIF base pointer
88 * param config SPDIF configuration structure.
89 */
SPDIF_Init(SPDIF_Type * base,const spdif_config_t * config)90 void SPDIF_Init(SPDIF_Type *base, const spdif_config_t *config)
91 {
92 uint32_t val = 0;
93
94 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
95 /* Enable the SPDIF clock */
96 CLOCK_EnableClock(s_spdifClock[SPDIF_GetInstance(base)]);
97 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
98
99 /* Reset the internal logic */
100 base->SCR |= SPDIF_SCR_SOFT_RESET_MASK;
101
102 /* Waiting for reset finish */
103 while ((base->SCR & SPDIF_SCR_SOFT_RESET_MASK) != 0x00U)
104 {
105 }
106
107 /* Setting the SPDIF settings */
108 base->SCR = SPDIF_SCR_RXFIFOFULL_SEL(config->rxFullSelect) | SPDIF_SCR_RXAUTOSYNC(config->isRxAutoSync) |
109 SPDIF_SCR_TXAUTOSYNC(config->isRxAutoSync) | SPDIF_SCR_TXFIFOEMPTY_SEL(config->txFullSelect) |
110 SPDIF_SCR_TXFIFO_CTRL(1U) | SPDIF_SCR_VALCTRL(config->validityConfig) |
111 SPDIF_SCR_TXSEL(config->txSource) | SPDIF_SCR_USRC_SEL(config->uChannelSrc);
112
113 /* Set DPLL clock source */
114 base->SRPC = SPDIF_SRPC_CLKSRC_SEL(config->DPLLClkSource) | SPDIF_SRPC_GAINSEL(config->gain);
115
116 /* Set SPDIF tx clock source */
117 val = base->STC & ~SPDIF_STC_TXCLK_SOURCE_MASK;
118 val |= SPDIF_STC_TXCLK_SOURCE(config->txClkSource);
119 base->STC = val;
120
121 /* clear and diable all the interrupt */
122 #if defined FSL_FEATURE_SPDIF_HAS_NO_SIC_REGISTER && FSL_FEATURE_SPDIF_HAS_NO_SIC_REGISTER
123 base->SIS = (uint32_t)kSPDIF_AllInterrupt;
124 #else
125 base->SIC = (uint32_t)kSPDIF_AllInterrupt;
126 #endif
127 base->SIE &= ~(uint32_t)kSPDIF_AllInterrupt;
128 }
129
130 /*!
131 * brief De-initializes the SPDIF peripheral.
132 *
133 * This API gates the SPDIF clock. The SPDIF module can't operate unless SPDIF_Init is called to enable the clock.
134 *
135 * param base SPDIF base pointer
136 */
SPDIF_Deinit(SPDIF_Type * base)137 void SPDIF_Deinit(SPDIF_Type *base)
138 {
139 SPDIF_TxEnable(base, false);
140 SPDIF_RxEnable(base, false);
141 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
142 CLOCK_DisableClock(s_spdifClock[SPDIF_GetInstance(base)]);
143 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
144 }
145
146 /*!
147 * brief Sets the SPDIF configuration structure to default values.
148 *
149 * This API initializes the configuration structure for use in SPDIF_Init.
150 * The initialized structure can remain unchanged in SPDIF_Init, or it can be modified
151 * before calling SPDIF_Init.
152 * This is an example.
153 code
154 spdif_config_t config;
155 SPDIF_GetDefaultConfig(&config);
156 endcode
157 *
158 * param config pointer to master configuration structure
159 */
SPDIF_GetDefaultConfig(spdif_config_t * config)160 void SPDIF_GetDefaultConfig(spdif_config_t *config)
161 {
162 /* Initializes the configure structure to zero. */
163 (void)memset(config, 0, sizeof(*config));
164
165 config->isTxAutoSync = true;
166 config->isRxAutoSync = true;
167 config->DPLLClkSource = 1;
168 config->txClkSource = 1;
169 config->rxFullSelect = kSPDIF_RxFull8Samples;
170 config->txFullSelect = kSPDIF_TxEmpty8Samples;
171 config->uChannelSrc = kSPDIF_UChannelFromTx;
172 config->txSource = kSPDIF_txNormal;
173 config->validityConfig = kSPDIF_validityFlagAlwaysClear;
174 config->gain = kSPDIF_GAIN_8;
175 }
176
177 /*!
178 * brief Enables/disables the SPDIF Tx.
179 *
180 * param base SPDIF base pointer
181 * param enable True means enable SPDIF Tx, false means disable.
182 */
SPDIF_TxEnable(SPDIF_Type * base,bool enable)183 void SPDIF_TxEnable(SPDIF_Type *base, bool enable)
184 {
185 uint32_t val = 0;
186
187 if (enable)
188 {
189 /* Open Tx FIFO */
190 val = base->SCR & (~SPDIF_SCR_TXFIFO_CTRL_MASK);
191 val |= SPDIF_SCR_TXFIFO_CTRL(1U);
192 base->SCR = val;
193 /* Enable transfer clock */
194 base->STC |= SPDIF_STC_TX_ALL_CLK_EN_MASK;
195 }
196 else
197 {
198 base->SCR &= ~(SPDIF_SCR_TXFIFO_CTRL_MASK | SPDIF_SCR_TXSEL_MASK);
199 /* Disable transfer clock */
200 base->STC &= ~SPDIF_STC_TX_ALL_CLK_EN_MASK;
201 }
202 }
203
204 /*!
205 * brief Configures the SPDIF Tx sample rate.
206 *
207 * The audio format can be changed at run-time. This function configures the sample rate.
208 *
209 * param base SPDIF base pointer.
210 * param sampleRate_Hz SPDIF sample rate frequency in Hz.
211 * param sourceClockFreq_Hz SPDIF tx clock source frequency in Hz.
212 */
SPDIF_TxSetSampleRate(SPDIF_Type * base,uint32_t sampleRate_Hz,uint32_t sourceClockFreq_Hz)213 void SPDIF_TxSetSampleRate(SPDIF_Type *base, uint32_t sampleRate_Hz, uint32_t sourceClockFreq_Hz)
214 {
215 uint32_t clkDiv = sourceClockFreq_Hz / (sampleRate_Hz * 64U);
216 uint32_t mod = sourceClockFreq_Hz % (sampleRate_Hz * 64U);
217 uint32_t val = 0;
218 uint8_t clockSource = (uint8_t)(((base->STC) & SPDIF_STC_TXCLK_SOURCE_MASK) >> SPDIF_STC_TXCLK_SOURCE_SHIFT);
219
220 /* Compute the nearest divider */
221 if (mod > ((sampleRate_Hz * 64U) / 2U))
222 {
223 clkDiv += 1U;
224 }
225
226 /* If use divided systeme clock */
227 if (clockSource == 5U)
228 {
229 if (clkDiv > 256U)
230 {
231 val = base->STC & (~(SPDIF_STC_TXCLK_DF_MASK | SPDIF_STC_SYSCLK_DF_MASK));
232 val |= SPDIF_STC_SYSCLK_DF((clkDiv / 128U) - 1U) | SPDIF_STC_TXCLK_DF(127U);
233 base->STC = val;
234 }
235 else
236 {
237 val = base->STC & (~(SPDIF_STC_TXCLK_DF_MASK | SPDIF_STC_SYSCLK_DF_MASK));
238 val |= SPDIF_STC_SYSCLK_DF(1U) | SPDIF_STC_TXCLK_DF(clkDiv - 1U);
239 base->STC = val;
240 }
241 }
242 else
243 {
244 /* Other clock only uses txclk div */
245 val = base->STC & (~(SPDIF_STC_TXCLK_DF_MASK | SPDIF_STC_SYSCLK_DF_MASK));
246 val |= SPDIF_STC_TXCLK_DF(clkDiv - 1U);
247 base->STC = val;
248 }
249 }
250
251 /*!
252 * brief Configures the SPDIF Rx audio format.
253 *
254 * The audio format can be changed at run-time. This function configures the sample rate and audio data
255 * format to be transferred.
256 *
257 * param base SPDIF base pointer.
258 * param clockSourceFreq_Hz SPDIF system clock frequency in hz.
259 */
SPDIF_GetRxSampleRate(SPDIF_Type * base,uint32_t clockSourceFreq_Hz)260 uint32_t SPDIF_GetRxSampleRate(SPDIF_Type *base, uint32_t clockSourceFreq_Hz)
261 {
262 uint64_t gain = s_spdif_gain[((base->SRPC & SPDIF_SRPC_GAINSEL_MASK) >> SPDIF_SRPC_GAINSEL_SHIFT)];
263 uint32_t measure = 0;
264 uint32_t sampleRate = 0;
265 uint64_t temp = 0;
266
267 /* Wait the DPLL locked */
268 while ((base->SRPC & SPDIF_SRPC_LOCK_MASK) == 0U)
269 {
270 }
271
272 /* Get the measure value */
273 measure = base->SRFM;
274 temp = (uint64_t)measure * (uint64_t)clockSourceFreq_Hz;
275 temp /= 1024U * 1024U * 128U * gain;
276 sampleRate = (uint32_t)temp;
277
278 return sampleRate;
279 }
280
281 /*!
282 * brief Sends data using a blocking method.
283 *
284 * note This function blocks by polling until data is ready to be sent.
285 *
286 * param base SPDIF base pointer.
287 * param buffer Pointer to the data to be written.
288 * param size Bytes to be written.
289 */
SPDIF_WriteBlocking(SPDIF_Type * base,uint8_t * buffer,uint32_t size)290 void SPDIF_WriteBlocking(SPDIF_Type *base, uint8_t *buffer, uint32_t size)
291 {
292 assert(buffer != NULL);
293 assert((size % 6U) == 0U);
294
295 uint32_t i = 0, j = 0, data = 0;
296
297 while (i < size)
298 {
299 /* Wait until it can write data */
300 while ((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_TxFIFOEmpty) == 0x00U)
301 {
302 }
303
304 /* Write left channel data */
305 for (j = 0; j < 3U; j++)
306 {
307 data |= ((uint32_t)(*buffer) << (j * 8U));
308 buffer++;
309 }
310 SPDIF_WriteLeftData(base, data);
311
312 /* Write right channel data */
313 data = 0;
314 for (j = 0; j < 3U; j++)
315 {
316 data |= ((uint32_t)(*buffer) << (j * 8U));
317 buffer++;
318 }
319 SPDIF_WriteRightData(base, data);
320
321 i += 6U;
322 }
323 }
324
325 /*!
326 * brief Receives data using a blocking method.
327 *
328 * note This function blocks by polling until data is ready to be sent.
329 *
330 * param base SPDIF base pointer.
331 * param buffer Pointer to the data to be read.
332 * param size Bytes to be read.
333 */
SPDIF_ReadBlocking(SPDIF_Type * base,uint8_t * buffer,uint32_t size)334 void SPDIF_ReadBlocking(SPDIF_Type *base, uint8_t *buffer, uint32_t size)
335 {
336 assert(buffer != NULL);
337 assert((size % 6U) == 0U);
338
339 uint32_t i = 0, j = 0, data = 0;
340
341 while (i < size)
342 {
343 /* Wait until it can write data */
344 while ((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_RxFIFOFull) == 0x00U)
345 {
346 }
347
348 /* Write left channel data */
349 data = SPDIF_ReadLeftData(base);
350 for (j = 0; j < 3U; j++)
351 {
352 *buffer = ((uint8_t)(data >> (j * 8U)) & 0xFFU);
353 buffer++;
354 }
355
356 /* Write right channel data */
357 data = SPDIF_ReadRightData(base);
358 for (j = 0; j < 3U; j++)
359 {
360 *buffer = ((uint8_t)(data >> (j * 8U)) & 0xFFU);
361 buffer++;
362 }
363
364 i += 6U;
365 }
366 }
367
368 /*!
369 * brief Initializes the SPDIF Tx handle.
370 *
371 * This function initializes the Tx handle for the SPDIF Tx transactional APIs. Call
372 * this function once to get the handle initialized.
373 *
374 * param base SPDIF base pointer
375 * param handle SPDIF handle pointer.
376 * param callback Pointer to the user callback function.
377 * param userData User parameter passed to the callback function
378 */
SPDIF_TransferTxCreateHandle(SPDIF_Type * base,spdif_handle_t * handle,spdif_transfer_callback_t callback,void * userData)379 void SPDIF_TransferTxCreateHandle(SPDIF_Type *base,
380 spdif_handle_t *handle,
381 spdif_transfer_callback_t callback,
382 void *userData)
383 {
384 assert(handle != NULL);
385
386 /* Zero the handle */
387 (void)memset(handle, 0, sizeof(*handle));
388
389 s_spdifHandle[SPDIF_GetInstance(base)][0] = handle;
390
391 handle->callback = callback;
392 handle->userData = userData;
393 handle->watermark =
394 s_spdif_tx_watermark[(base->SCR & SPDIF_SCR_TXFIFOEMPTY_SEL_MASK) >> SPDIF_SCR_TXFIFOEMPTY_SEL_SHIFT];
395
396 /* Set the isr pointer */
397 s_spdifTxIsr = SPDIF_TransferTxHandleIRQ;
398
399 /* Enable Tx irq */
400 (void)EnableIRQ(s_spdifIRQ[SPDIF_GetInstance(base)]);
401 }
402
403 /*!
404 * brief Initializes the SPDIF Rx handle.
405 *
406 * This function initializes the Rx handle for the SPDIF Rx transactional APIs. Call
407 * this function once to get the handle initialized.
408 *
409 * param base SPDIF base pointer.
410 * param handle SPDIF handle pointer.
411 * param callback Pointer to the user callback function.
412 * param userData User parameter passed to the callback function.
413 */
SPDIF_TransferRxCreateHandle(SPDIF_Type * base,spdif_handle_t * handle,spdif_transfer_callback_t callback,void * userData)414 void SPDIF_TransferRxCreateHandle(SPDIF_Type *base,
415 spdif_handle_t *handle,
416 spdif_transfer_callback_t callback,
417 void *userData)
418 {
419 assert(handle != NULL);
420
421 /* Zero the handle */
422 (void)memset(handle, 0, sizeof(*handle));
423
424 s_spdifHandle[SPDIF_GetInstance(base)][1] = handle;
425
426 handle->callback = callback;
427 handle->userData = userData;
428 handle->watermark =
429 s_spdif_rx_watermark[(base->SCR & SPDIF_SCR_RXFIFOFULL_SEL_MASK) >> SPDIF_SCR_RXFIFOFULL_SEL_SHIFT];
430
431 /* Set the isr pointer */
432 s_spdifRxIsr = SPDIF_TransferRxHandleIRQ;
433
434 /* Enable Rx irq */
435 (void)EnableIRQ(s_spdifIRQ[SPDIF_GetInstance(base)]);
436 }
437
438 /*!
439 * brief Performs an interrupt non-blocking send transfer on SPDIF.
440 *
441 * note This API returns immediately after the transfer initiates.
442 * Call the SPDIF_TxGetTransferStatusIRQ to poll the transfer status and check whether
443 * the transfer is finished. If the return status is not kStatus_SPDIF_Busy, the transfer
444 * is finished.
445 *
446 * param base SPDIF base pointer.
447 * param handle Pointer to the spdif_handle_t structure which stores the transfer state.
448 * param xfer Pointer to the spdif_transfer_t structure.
449 * retval kStatus_Success Successfully started the data receive.
450 * retval kStatus_SPDIF_TxBusy Previous receive still not finished.
451 * retval kStatus_InvalidArgument The input parameter is invalid.
452 */
SPDIF_TransferSendNonBlocking(SPDIF_Type * base,spdif_handle_t * handle,spdif_transfer_t * xfer)453 status_t SPDIF_TransferSendNonBlocking(SPDIF_Type *base, spdif_handle_t *handle, spdif_transfer_t *xfer)
454 {
455 assert(handle != NULL);
456
457 /* Check if the queue is full */
458 if (handle->spdifQueue[handle->queueUser].data != NULL)
459 {
460 return kStatus_SPDIF_QueueFull;
461 }
462
463 /* Add into queue */
464 handle->transferSize[handle->queueUser] = xfer->dataSize;
465 handle->spdifQueue[handle->queueUser].data = xfer->data;
466 handle->spdifQueue[handle->queueUser].dataSize = xfer->dataSize;
467 handle->queueUser = (handle->queueUser + 0x01U) % SPDIF_XFER_QUEUE_SIZE;
468
469 /* Set the state to busy */
470 handle->state = kSPDIF_Busy;
471
472 /* Enable interrupt */
473 SPDIF_EnableInterrupts(base, kSPDIF_TxFIFOEmpty);
474
475 /* Enable Tx transfer */
476 SPDIF_TxEnable(base, true);
477
478 return kStatus_Success;
479 }
480
481 /*!
482 * brief Performs an interrupt non-blocking receive transfer on SPDIF.
483 *
484 * note This API returns immediately after the transfer initiates.
485 * Call the SPDIF_RxGetTransferStatusIRQ to poll the transfer status and check whether
486 * the transfer is finished. If the return status is not kStatus_SPDIF_Busy, the transfer
487 * is finished.
488 *
489 * param base SPDIF base pointer
490 * param handle Pointer to the spdif_handle_t structure which stores the transfer state.
491 * param xfer Pointer to the spdif_transfer_t structure.
492 * retval kStatus_Success Successfully started the data receive.
493 * retval kStatus_SPDIF_RxBusy Previous receive still not finished.
494 * retval kStatus_InvalidArgument The input parameter is invalid.
495 */
SPDIF_TransferReceiveNonBlocking(SPDIF_Type * base,spdif_handle_t * handle,spdif_transfer_t * xfer)496 status_t SPDIF_TransferReceiveNonBlocking(SPDIF_Type *base, spdif_handle_t *handle, spdif_transfer_t *xfer)
497 {
498 assert(handle != NULL);
499
500 uint32_t enableInterrupts = (uint32_t)kSPDIF_RxFIFOFull | (uint32_t)kSPDIF_RxControlChannelChange;
501
502 /* Check if the queue is full */
503 if (handle->spdifQueue[handle->queueUser].data != NULL)
504 {
505 return kStatus_SPDIF_QueueFull;
506 }
507
508 /* Add into queue */
509 handle->transferSize[handle->queueUser] = xfer->dataSize;
510 handle->spdifQueue[handle->queueUser].data = xfer->data;
511 handle->spdifQueue[handle->queueUser].dataSize = xfer->dataSize;
512 handle->spdifQueue[handle->queueUser].udata = xfer->udata;
513 handle->spdifQueue[handle->queueUser].qdata = xfer->qdata;
514 handle->queueUser = (handle->queueUser + 0x01U) % SPDIF_XFER_QUEUE_SIZE;
515
516 /* Set state to busy */
517 handle->state = kSPDIF_Busy;
518
519 if (xfer->qdata != NULL)
520 {
521 enableInterrupts |= (uint32_t)kSPDIF_QChannelReceiveRegisterFull;
522 }
523
524 if (xfer->udata != NULL)
525 {
526 enableInterrupts |= (uint32_t)kSPDIF_UChannelReceiveRegisterFull;
527 }
528
529 /* Enable interrupt */
530 SPDIF_EnableInterrupts(base, enableInterrupts);
531
532 /* Enable Rx transfer */
533 SPDIF_RxEnable(base, true);
534
535 return kStatus_Success;
536 }
537
538 /*!
539 * brief Gets a set byte count.
540 *
541 * param base SPDIF base pointer.
542 * param handle Pointer to the spdif_handle_t structure which stores the transfer state.
543 * param count Bytes count sent.
544 * retval kStatus_Success Succeed get the transfer count.
545 * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
546 */
SPDIF_TransferGetSendCount(SPDIF_Type * base,spdif_handle_t * handle,size_t * count)547 status_t SPDIF_TransferGetSendCount(SPDIF_Type *base, spdif_handle_t *handle, size_t *count)
548 {
549 assert(handle != NULL);
550
551 status_t status = kStatus_Success;
552 uint8_t queueDriver = handle->queueDriver;
553
554 if (handle->state != (uint32_t)kSPDIF_Busy)
555 {
556 status = kStatus_NoTransferInProgress;
557 }
558 else
559 {
560 *count = (handle->transferSize[queueDriver] - handle->spdifQueue[queueDriver].dataSize);
561 }
562
563 return status;
564 }
565
566 /*!
567 * brief Gets a received byte count.
568 *
569 * param base SPDIF base pointer.
570 * param handle Pointer to the spdif_handle_t structure which stores the transfer state.
571 * param count Bytes count received.
572 * retval kStatus_Success Succeed get the transfer count.
573 * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
574 */
SPDIF_TransferGetReceiveCount(SPDIF_Type * base,spdif_handle_t * handle,size_t * count)575 status_t SPDIF_TransferGetReceiveCount(SPDIF_Type *base, spdif_handle_t *handle, size_t *count)
576 {
577 assert(handle != NULL);
578
579 status_t status = kStatus_Success;
580 uint8_t queueDriver = handle->queueDriver;
581
582 if (handle->state != (uint32_t)kSPDIF_Busy)
583 {
584 status = kStatus_NoTransferInProgress;
585 }
586 else
587 {
588 *count = (handle->transferSize[queueDriver] - handle->spdifQueue[queueDriver].dataSize);
589 }
590
591 return status;
592 }
593
594 /*!
595 * brief Aborts the current send.
596 *
597 * note This API can be called any time when an interrupt non-blocking transfer initiates
598 * to abort the transfer early.
599 *
600 * param base SPDIF base pointer.
601 * param handle Pointer to the spdif_handle_t structure which stores the transfer state.
602 */
SPDIF_TransferAbortSend(SPDIF_Type * base,spdif_handle_t * handle)603 void SPDIF_TransferAbortSend(SPDIF_Type *base, spdif_handle_t *handle)
604 {
605 assert(handle != NULL);
606
607 /* Use FIFO request interrupt and fifo error */
608 SPDIF_DisableInterrupts(base, kSPDIF_TxFIFOEmpty);
609
610 handle->state = kSPDIF_Idle;
611
612 /* Clear the queue */
613 (void)memset(handle->spdifQueue, 0, sizeof(spdif_transfer_t) * SPDIF_XFER_QUEUE_SIZE);
614 handle->queueDriver = 0;
615 handle->queueUser = 0;
616 }
617
618 /*!
619 * brief Aborts the current IRQ receive.
620 *
621 * note This API can be called when an interrupt non-blocking transfer initiates
622 * to abort the transfer early.
623 *
624 * param base SPDIF base pointer
625 * param handle Pointer to the spdif_handle_t structure which stores the transfer state.
626 */
SPDIF_TransferAbortReceive(SPDIF_Type * base,spdif_handle_t * handle)627 void SPDIF_TransferAbortReceive(SPDIF_Type *base, spdif_handle_t *handle)
628 {
629 assert(handle != NULL);
630
631 /* Disable interrupt */
632 SPDIF_DisableInterrupts(base, (uint32_t)kSPDIF_UChannelReceiveRegisterFull |
633 (uint32_t)kSPDIF_QChannelReceiveRegisterFull | (uint32_t)kSPDIF_RxFIFOFull |
634 (uint32_t)kSPDIF_RxControlChannelChange);
635
636 handle->state = kSPDIF_Idle;
637
638 /* Clear the queue */
639 (void)memset(handle->spdifQueue, 0, sizeof(spdif_transfer_t) * SPDIF_XFER_QUEUE_SIZE);
640 handle->queueDriver = 0;
641 handle->queueUser = 0;
642 }
643
644 /*!
645 * brief Tx interrupt handler.
646 *
647 * param base SPDIF base pointer.
648 * param handle Pointer to the spdif_handle_t structure.
649 */
SPDIF_TransferTxHandleIRQ(SPDIF_Type * base,spdif_handle_t * handle)650 void SPDIF_TransferTxHandleIRQ(SPDIF_Type *base, spdif_handle_t *handle)
651 {
652 assert(handle != NULL);
653
654 uint8_t *buffer = handle->spdifQueue[handle->queueDriver].data;
655 uint8_t dataSize = 0;
656 uint32_t i = 0, j = 0, data = 0;
657
658 /* Do Transfer */
659 if (((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_TxFIFOEmpty) != 0x00U) &&
660 ((base->SIE & (uint32_t)kSPDIF_TxFIFOEmpty) != 0x00U))
661 {
662 dataSize = handle->watermark;
663 while (i < dataSize)
664 {
665 data = 0;
666 /* Write left channel data */
667 for (j = 0; j < 3U; j++)
668 {
669 data |= ((uint32_t)(*buffer) << (j * 8U));
670 buffer++;
671 }
672 SPDIF_WriteLeftData(base, data);
673
674 /* Write right channel data */
675 data = 0;
676 for (j = 0; j < 3U; j++)
677 {
678 data |= ((uint32_t)(*buffer) << (j * 8U));
679 buffer++;
680 }
681 SPDIF_WriteRightData(base, data);
682
683 i++;
684 }
685 handle->spdifQueue[handle->queueDriver].dataSize -= (uint32_t)dataSize * 6U;
686 handle->spdifQueue[handle->queueDriver].data += dataSize * 6U;
687
688 /* If finished a block, call the callback function */
689 if (handle->spdifQueue[handle->queueDriver].dataSize == 0U)
690 {
691 (void)memset(&handle->spdifQueue[handle->queueDriver], 0, sizeof(spdif_transfer_t));
692 handle->queueDriver = (handle->queueDriver + 0x01U) % SPDIF_XFER_QUEUE_SIZE;
693 if (handle->callback != NULL)
694 {
695 (handle->callback)(base, handle, kStatus_SPDIF_TxIdle, handle->userData);
696 }
697 }
698
699 /* If all data finished, just stop the transfer */
700 if (handle->spdifQueue[handle->queueDriver].data == NULL)
701 {
702 SPDIF_TransferAbortSend(base, handle);
703 }
704 }
705 }
706
707 /*!
708 * brief Tx interrupt handler.
709 *
710 * param base SPDIF base pointer.
711 * param handle Pointer to the spdif_handle_t structure.
712 */
SPDIF_TransferRxHandleIRQ(SPDIF_Type * base,spdif_handle_t * handle)713 void SPDIF_TransferRxHandleIRQ(SPDIF_Type *base, spdif_handle_t *handle)
714 {
715 assert(handle != NULL);
716
717 uint8_t *buffer = NULL;
718 uint8_t dataSize = 0;
719 uint32_t i = 0, j = 0, data = 0;
720
721 /* Handle Cnew flag */
722 if ((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_RxControlChannelChange) != 0x00U)
723 {
724 /* Clear the interrupt flag */
725 SPDIF_ClearStatusFlags(base, SPDIF_SIE_CNEW_MASK);
726 if (handle->callback != NULL)
727 {
728 (handle->callback)(base, handle, kStatus_SPDIF_RxCnew, handle->userData);
729 }
730 }
731
732 /* Handle illegal symbol */
733 if ((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_RxIllegalSymbol) != 0x00U)
734 {
735 SPDIF_ClearStatusFlags(base, kSPDIF_RxIllegalSymbol);
736 if (handle->callback != NULL)
737 {
738 (handle->callback)(base, handle, kStatus_SPDIF_RxIllegalSymbol, handle->userData);
739 }
740 }
741
742 /* Handle Parity Bit Error */
743 if ((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_RxParityBitError) != 0x00U)
744 {
745 SPDIF_ClearStatusFlags(base, kSPDIF_RxParityBitError);
746 if (handle->callback != NULL)
747 {
748 (handle->callback)(base, handle, kStatus_SPDIF_RxParityBitError, handle->userData);
749 }
750 }
751
752 /* Handle DPlocked */
753 if ((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_RxDPLLLocked) != 0x00U)
754 {
755 SPDIF_ClearStatusFlags(base, kSPDIF_RxDPLLLocked);
756 if (handle->callback != NULL)
757 {
758 (handle->callback)(base, handle, kStatus_SPDIF_RxDPLLLocked, handle->userData);
759 }
760 }
761
762 /* Handle Q channel full flag */
763 if (((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_QChannelReceiveRegisterFull) != 0x00U) &&
764 ((base->SIE & (uint32_t)kSPDIF_QChannelReceiveRegisterFull) != 0x00U))
765 {
766 buffer = handle->spdifQueue[handle->queueDriver].qdata;
767 if (buffer != NULL)
768 {
769 data = SPDIF_ReadQChannel(base);
770 buffer[0] = (uint8_t)data & 0xFFU;
771 buffer[1] = (uint8_t)(data >> 8U) & 0xFFU;
772 buffer[2] = (uint8_t)(data >> 16U) & 0xFFU;
773 }
774 }
775
776 /* Handle U channel full flag */
777 if (((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_UChannelReceiveRegisterFull) != 0x00U) &&
778 ((base->SIE & (uint32_t)kSPDIF_UChannelReceiveRegisterFull) != 0x00U))
779 {
780 buffer = handle->spdifQueue[handle->queueDriver].udata;
781 if (buffer != NULL)
782 {
783 data = SPDIF_ReadUChannel(base);
784 buffer[0] = (uint8_t)data & 0xFFU;
785 buffer[1] = (uint8_t)(data >> 8U) & 0xFFU;
786 buffer[2] = (uint8_t)(data >> 16U) & 0xFFU;
787 }
788 }
789
790 /* Handle audio data transfer */
791 if (((SPDIF_GetStatusFlag(base) & (uint32_t)kSPDIF_RxFIFOFull) != 0x00U) &&
792 ((base->SIE & (uint32_t)kSPDIF_RxFIFOFull) != 0x00U))
793 {
794 dataSize = handle->watermark;
795 buffer = handle->spdifQueue[handle->queueDriver].data;
796 while (i < dataSize)
797 {
798 /* Read left channel data */
799 data = SPDIF_ReadLeftData(base);
800 for (j = 0; j < 3U; j++)
801 {
802 *buffer = (uint8_t)((data >> (j * 8U)) & 0xFFU);
803 buffer++;
804 }
805
806 /* Read right channel data */
807 data = SPDIF_ReadRightData(base);
808 for (j = 0; j < 3U; j++)
809 {
810 *buffer = (uint8_t)((data >> (j * 8U)) & 0xFFU);
811 buffer++;
812 }
813
814 i++;
815 }
816 handle->spdifQueue[handle->queueDriver].dataSize -= (uint32_t)dataSize * 6U;
817 handle->spdifQueue[handle->queueDriver].data += dataSize * 6U;
818
819 /* If finished a block, call the callback function */
820 if (handle->spdifQueue[handle->queueDriver].dataSize == 0x00U)
821 {
822 (void)memset(&handle->spdifQueue[handle->queueDriver], 0, sizeof(spdif_transfer_t));
823 handle->queueDriver = (handle->queueDriver + 0x01U) % SPDIF_XFER_QUEUE_SIZE;
824 if (handle->callback != NULL)
825 {
826 (handle->callback)(base, handle, kStatus_SPDIF_RxIdle, handle->userData);
827 }
828 }
829
830 /* If all data finished, just stop the transfer */
831 if (handle->spdifQueue[handle->queueDriver].data == NULL)
832 {
833 SPDIF_TransferAbortReceive(base, handle);
834 }
835 }
836 }
837
838 #if defined(SPDIF)
839 void SPDIF_DriverIRQHandler(void);
SPDIF_DriverIRQHandler(void)840 void SPDIF_DriverIRQHandler(void)
841 {
842 if ((s_spdifHandle[0][0] != NULL) && (s_spdifTxIsr != NULL))
843 {
844 s_spdifTxIsr(SPDIF, s_spdifHandle[0][0]);
845 }
846
847 if ((s_spdifHandle[0][1] != NULL) && (s_spdifRxIsr != NULL))
848 {
849 s_spdifRxIsr(SPDIF, s_spdifHandle[0][1]);
850 }
851 SDK_ISR_EXIT_BARRIER;
852 }
853 #endif
854