STM32F4xx HAL Driver master
STM32CubeF4 HAL / LL Drivers API Reference
Loading...
Searching...
No Matches
stm32f4xx_hal_i2s_ex.c
Go to the documentation of this file.
1
87
88/* Includes ------------------------------------------------------------------*/
89#include "stm32f4xx_hal.h"
90
94
95#ifdef HAL_I2S_MODULE_ENABLED
96
101
102#if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
103
104/* Private typedef -----------------------------------------------------------*/
108typedef enum
109{
110 I2S_USE_I2S = 0x00U,
111 I2S_USE_I2SEXT = 0x01U,
112} I2S_UseTypeDef;
116/* Private define ------------------------------------------------------------*/
117/* Private macro -------------------------------------------------------------*/
118/* Private variables ---------------------------------------------------------*/
119/* Private function prototypes -----------------------------------------------*/
123static void I2SEx_TxRxDMAHalfCplt(DMA_HandleTypeDef *hdma);
124static void I2SEx_TxRxDMACplt(DMA_HandleTypeDef *hdma);
125static void I2SEx_TxRxDMAError(DMA_HandleTypeDef *hdma);
126static void I2SEx_RxISR_I2S(I2S_HandleTypeDef *hi2s);
127static void I2SEx_RxISR_I2SExt(I2S_HandleTypeDef *hi2s);
128static void I2SEx_TxISR_I2S(I2S_HandleTypeDef *hi2s);
129static void I2SEx_TxISR_I2SExt(I2S_HandleTypeDef *hi2s);
130static HAL_StatusTypeDef I2SEx_FullDuplexWaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s,
131 uint32_t Flag,
132 uint32_t State,
133 uint32_t Timeout,
134 I2S_UseTypeDef i2sUsed);
138
142
143/* Private functions ---------------------------------------------------------*/
144/* Exported functions --------------------------------------------------------*/
145
149
153
206HAL_StatusTypeDef HAL_I2SEx_TransmitReceive(I2S_HandleTypeDef *hi2s,
207 uint16_t *pTxData,
208 uint16_t *pRxData,
209 uint16_t Size,
210 uint32_t Timeout)
211{
212 uint32_t tmp1 = 0U;
213
214 if (hi2s->State != HAL_I2S_STATE_READY)
215 {
216 return HAL_BUSY;
217 }
218
219 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
220 {
221 return HAL_ERROR;
222 }
223
224 /* Process Locked */
225 __HAL_LOCK(hi2s);
226
227 tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
228 /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
229 is selected during the I2S configuration phase, the Size parameter means the number
230 of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
231 frame is selected the Size parameter means the number of 24-bit or 32-bit data length. */
232 if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
233 {
234 hi2s->TxXferSize = (Size << 1U);
235 hi2s->TxXferCount = (Size << 1U);
236 hi2s->RxXferSize = (Size << 1U);
237 hi2s->RxXferCount = (Size << 1U);
238 }
239 else
240 {
241 hi2s->TxXferSize = Size;
242 hi2s->TxXferCount = Size;
243 hi2s->RxXferSize = Size;
244 hi2s->RxXferCount = Size;
245 }
246
247 /* Set state and reset error code */
250
251 tmp1 = hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG;
252 /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
253 if ((tmp1 == I2S_MODE_MASTER_TX) || (tmp1 == I2S_MODE_SLAVE_TX))
254 {
255 /* Prepare the First Data before enabling the I2S */
256 hi2s->Instance->DR = (*pTxData++);
257 hi2s->TxXferCount--;
258
259 /* Enable I2Sext(receiver) before enabling I2Sx peripheral */
260 __HAL_I2SEXT_ENABLE(hi2s);
261
262 /* Enable I2Sx peripheral */
263 __HAL_I2S_ENABLE(hi2s);
264
265 /* Check if Master Receiver mode is selected */
266 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX)
267 {
268 /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
269 access to the SPI_SR register. */
270 __HAL_I2SEXT_CLEAR_OVRFLAG(hi2s);
271 }
272
273 while ((hi2s->RxXferCount > 0U) || (hi2s->TxXferCount > 0U))
274 {
275 if (hi2s->TxXferCount > 0U)
276 {
277 /* Wait until TXE flag is set */
278 if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout, I2S_USE_I2S) != HAL_OK)
279 {
280 /* Set the error code */
281 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
283
284 /* Process UnLock */
285 __HAL_UNLOCK(hi2s);
286 return HAL_ERROR;
287 }
288 /* Write Data on DR register */
289 hi2s->Instance->DR = (*pTxData++);
290 hi2s->TxXferCount--;
291
292 /* Check if an underrun occurs */
293 if ((__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) && (tmp1 == I2S_MODE_SLAVE_TX))
294 {
295 /* Clear Underrun flag */
297
298 /* Set the error code */
299 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
300 }
301 }
302 if (hi2s->RxXferCount > 0U)
303 {
304 /* Wait until RXNE flag is set */
305 if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout, I2S_USE_I2SEXT) != HAL_OK)
306 {
307 /* Set the error code */
308 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
310
311 /* Process UnLock */
312 __HAL_UNLOCK(hi2s);
313 return HAL_ERROR;
314 }
315 /* Read Data from DR register */
316 (*pRxData++) = I2SxEXT(hi2s->Instance)->DR;
317 hi2s->RxXferCount--;
318
319 /* Check if an overrun occurs */
320 if (__HAL_I2SEXT_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
321 {
322 /* Clear Overrun flag */
324
325 /* Set the error code */
326 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
327 }
328 }
329 }
330 }
331 /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
332 else
333 {
334 /* Prepare the First Data before enabling the I2S */
335 I2SxEXT(hi2s->Instance)->DR = (*pTxData++);
336 hi2s->TxXferCount--;
337
338 /* Enable I2Sext(transmitter) after enabling I2Sx peripheral */
339 __HAL_I2SEXT_ENABLE(hi2s);
340
341 /* Enable I2S peripheral before the I2Sext*/
342 __HAL_I2S_ENABLE(hi2s);
343
344 /* Check if Master Receiver mode is selected */
345 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
346 {
347 /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
348 access to the SPI_SR register. */
350 }
351
352 while ((hi2s->RxXferCount > 0U) || (hi2s->TxXferCount > 0U))
353 {
354 if (hi2s->TxXferCount > 0U)
355 {
356 /* Wait until TXE flag is set */
357 if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout, I2S_USE_I2SEXT) != HAL_OK)
358 {
359 /* Set the error code */
360 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
362
363 /* Process UnLock */
364 __HAL_UNLOCK(hi2s);
365 return HAL_ERROR;
366 }
367 /* Write Data on DR register */
368 I2SxEXT(hi2s->Instance)->DR = (*pTxData++);
369 hi2s->TxXferCount--;
370
371 /* Check if an underrun occurs */
372 if ((__HAL_I2SEXT_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) && (tmp1 == I2S_MODE_SLAVE_RX))
373 {
374 /* Clear Underrun flag */
376
377 /* Set the error code */
378 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
379 }
380 }
381 if (hi2s->RxXferCount > 0U)
382 {
383 /* Wait until RXNE flag is set */
384 if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout, I2S_USE_I2S) != HAL_OK)
385 {
386 /* Set the error code */
387 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
389
390 /* Process UnLock */
391 __HAL_UNLOCK(hi2s);
392 return HAL_ERROR;
393 }
394 /* Read Data from DR register */
395 (*pRxData++) = hi2s->Instance->DR;
396 hi2s->RxXferCount--;
397
398 /* Check if an overrun occurs */
399 if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
400 {
401 /* Clear Overrun flag */
403
404 /* Set the error code */
405 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
406 }
407 }
408 }
409 }
410
412 __HAL_UNLOCK(hi2s);
413
414 if (hi2s->ErrorCode != HAL_I2S_ERROR_NONE)
415 {
416 return HAL_ERROR;
417 }
418 else
419 {
420 return HAL_OK;
421 }
422}
423
439HAL_StatusTypeDef HAL_I2SEx_TransmitReceive_IT(I2S_HandleTypeDef *hi2s,
440 uint16_t *pTxData,
441 uint16_t *pRxData,
442 uint16_t Size)
443{
444 uint32_t tmp1 = 0U;
445
446 if (hi2s->State != HAL_I2S_STATE_READY)
447 {
448 return HAL_BUSY;
449 }
450
451 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
452 {
453 return HAL_ERROR;
454 }
455
456 /* Process Locked */
457 __HAL_LOCK(hi2s);
458
459 hi2s->pTxBuffPtr = pTxData;
460 hi2s->pRxBuffPtr = pRxData;
461
462 tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
463 /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
464 is selected during the I2S configuration phase, the Size parameter means the number
465 of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
466 frame is selected the Size parameter means the number of 24-bit or 32-bit data length. */
467 if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
468 {
469 hi2s->TxXferSize = (Size << 1U);
470 hi2s->TxXferCount = (Size << 1U);
471 hi2s->RxXferSize = (Size << 1U);
472 hi2s->RxXferCount = (Size << 1U);
473 }
474 else
475 {
476 hi2s->TxXferSize = Size;
477 hi2s->TxXferCount = Size;
478 hi2s->RxXferSize = Size;
479 hi2s->RxXferCount = Size;
480 }
481
484
485 /* Set the function for IT treatment */
486 if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
487 {
488 /* Enable I2Sext RXNE and ERR interrupts */
489 __HAL_I2SEXT_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
490
491 /* Enable I2Sx TXE and ERR interrupts */
493
494 /* Transmit First data */
495 hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
496 hi2s->TxXferCount--;
497
498 if (hi2s->TxXferCount == 0U)
499 {
500 /* Disable TXE and ERR interrupt */
502 }
503 }
504 else /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
505 {
506 /* Enable I2Sext TXE and ERR interrupts */
507 __HAL_I2SEXT_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
508
509 /* Enable I2Sext RXNE and ERR interrupts */
511
512 /* Transmit First data */
513 I2SxEXT(hi2s->Instance)->DR = (*hi2s->pTxBuffPtr++);
514 hi2s->TxXferCount--;
515
516 if (hi2s->TxXferCount == 0U)
517 {
518 /* Disable I2Sext TXE and ERR interrupt */
519 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
520 }
521 }
522
523 __HAL_UNLOCK(hi2s);
524 /* Enable I2Sext peripheral */
525 __HAL_I2SEXT_ENABLE(hi2s);
526
527 /* Enable I2S peripheral */
528 __HAL_I2S_ENABLE(hi2s);
529
530 return HAL_OK;
531}
532
548HAL_StatusTypeDef HAL_I2SEx_TransmitReceive_DMA(I2S_HandleTypeDef *hi2s,
549 uint16_t *pTxData,
550 uint16_t *pRxData,
551 uint16_t Size)
552{
553 uint32_t *tmp = NULL;
554 uint32_t tmp1 = 0U;
555 HAL_StatusTypeDef status;
556
557 if (hi2s->State != HAL_I2S_STATE_READY)
558 {
559 return HAL_BUSY;
560 }
561
562 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
563 {
564 return HAL_ERROR;
565 }
566
567 /* Process Locked */
568 __HAL_LOCK(hi2s);
569
570 hi2s->pTxBuffPtr = pTxData;
571 hi2s->pRxBuffPtr = pRxData;
572
573 tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
574 /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
575 is selected during the I2S configuration phase, the Size parameter means the number
576 of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
577 frame is selected the Size parameter means the number of 24-bit or 32-bit data length. */
578 if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
579 {
580 hi2s->TxXferSize = (Size << 1U);
581 hi2s->TxXferCount = (Size << 1U);
582 hi2s->RxXferSize = (Size << 1U);
583 hi2s->RxXferCount = (Size << 1U);
584 }
585 else
586 {
587 hi2s->TxXferSize = Size;
588 hi2s->TxXferCount = Size;
589 hi2s->RxXferSize = Size;
590 hi2s->RxXferCount = Size;
591 }
592
595
596 /* Set the I2S Rx DMA Half transfer complete callback */
597 hi2s->hdmarx->XferHalfCpltCallback = I2SEx_TxRxDMAHalfCplt;
598
599 /* Set the I2S Rx DMA transfer complete callback */
600 hi2s->hdmarx->XferCpltCallback = I2SEx_TxRxDMACplt;
601
602 /* Set the I2S Rx DMA error callback */
603 hi2s->hdmarx->XferErrorCallback = I2SEx_TxRxDMAError;
604
605 /* Set the I2S Tx DMA Half transfer complete callback as NULL */
606 hi2s->hdmatx->XferHalfCpltCallback = NULL;
607
608 /* Set the I2S Tx DMA transfer complete callback as NULL */
609 hi2s->hdmatx->XferCpltCallback = NULL;
610
611 /* Set the I2S Tx DMA error callback */
612 hi2s->hdmatx->XferErrorCallback = I2SEx_TxRxDMAError;
613
614 tmp1 = hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG;
615 /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
616 if ((tmp1 == I2S_MODE_MASTER_TX) || (tmp1 == I2S_MODE_SLAVE_TX))
617 {
618 /* Enable the Rx DMA Stream */
619 tmp = (uint32_t *)&pRxData;
620 status = HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&I2SxEXT(hi2s->Instance)->DR, *(uint32_t *)tmp, hi2s->RxXferSize);
621
622 /* Enable Rx DMA Request */
623 SET_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
624
625 /* Enable the Tx DMA Stream */
626 tmp = (uint32_t *)&pTxData;
627 status = HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t *)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
628
629 /* Enable Tx DMA Request */
630 SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
631 }
632 else
633 {
634 /* Check if Master Receiver mode is selected */
635 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
636 {
637 /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
638 access to the SPI_SR register. */
640 }
641 /* Enable the Tx DMA Stream */
642 tmp = (uint32_t *)&pTxData;
643 status = HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t *)tmp, (uint32_t)&I2SxEXT(hi2s->Instance)->DR, hi2s->TxXferSize);
644
645 /* Enable Tx DMA Request */
646 SET_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
647
648 /* Enable the Rx DMA Stream */
649 tmp = (uint32_t *)&pRxData;
650 status = HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t *)tmp, hi2s->RxXferSize);
651
652 /* Enable Rx DMA Request */
653 SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
654 }
655
656 __HAL_UNLOCK(hi2s);
657 /* Check if the I2S is already enabled */
658 if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
659 {
660 /* Enable I2Sext(transmitter) before enabling I2Sx peripheral */
661 __HAL_I2SEXT_ENABLE(hi2s);
662 /* Enable I2S peripheral before the I2Sext */
663 __HAL_I2S_ENABLE(hi2s);
664 }
665
666 return status;
667}
668
674void HAL_I2SEx_FullDuplex_IRQHandler(I2S_HandleTypeDef *hi2s)
675{
676 __IO uint32_t i2ssr = hi2s->Instance->SR;
677 __IO uint32_t i2sextsr = I2SxEXT(hi2s->Instance)->SR;
678 __IO uint32_t i2scr2 = hi2s->Instance->CR2;
679 __IO uint32_t i2sextcr2 = I2SxEXT(hi2s->Instance)->CR2;
680
681 /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
682 if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
683 {
684 /* I2S in mode Transmitter -------------------------------------------------*/
685 if (((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && ((i2scr2 & I2S_IT_TXE) != RESET))
686 {
687 /* When the I2S mode is configured as I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX,
688 the I2S TXE interrupt will be generated to manage the full-duplex transmit phase. */
689 I2SEx_TxISR_I2S(hi2s);
690 }
691
692 /* I2Sext in mode Receiver -----------------------------------------------*/
693 if (((i2sextsr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && ((i2sextcr2 & I2S_IT_RXNE) != RESET))
694 {
695 /* When the I2S mode is configured as I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX,
696 the I2Sext RXNE interrupt will be generated to manage the full-duplex receive phase. */
697 I2SEx_RxISR_I2SExt(hi2s);
698 }
699
700 /* I2Sext Overrun error interrupt occurred --------------------------------*/
701 if (((i2sextsr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && ((i2sextcr2 & I2S_IT_ERR) != RESET))
702 {
703 /* Disable RXNE and ERR interrupt */
704 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
705
706 /* Disable TXE and ERR interrupt */
708
709 /* Clear Overrun flag */
711
712 /* Set the I2S State ready */
714
715 /* Set the error code and execute error callback*/
716 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
717 /* Call user error callback */
718#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
719 hi2s->ErrorCallback(hi2s);
720#else
722#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
723 }
724
725 /* I2S Underrun error interrupt occurred ----------------------------------*/
726 if (((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && ((i2scr2 & I2S_IT_ERR) != RESET))
727 {
728 /* Disable TXE and ERR interrupt */
730
731 /* Disable RXNE and ERR interrupt */
732 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
733
734 /* Clear underrun flag */
736
737 /* Set the I2S State ready */
739
740 /* Set the error code and execute error callback*/
741 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
742 /* Call user error callback */
743#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
744 hi2s->ErrorCallback(hi2s);
745#else
747#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
748 }
749 }
750 /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
751 else
752 {
753 /* I2Sext in mode Transmitter ----------------------------------------------*/
754 if (((i2sextsr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && ((i2sextcr2 & I2S_IT_TXE) != RESET))
755 {
756 /* When the I2S mode is configured as I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX,
757 the I2Sext TXE interrupt will be generated to manage the full-duplex transmit phase. */
758 I2SEx_TxISR_I2SExt(hi2s);
759 }
760
761 /* I2S in mode Receiver --------------------------------------------------*/
762 if (((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && ((i2scr2 & I2S_IT_RXNE) != RESET))
763 {
764 /* When the I2S mode is configured as I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX,
765 the I2S RXNE interrupt will be generated to manage the full-duplex receive phase. */
766 I2SEx_RxISR_I2S(hi2s);
767 }
768
769 /* I2S Overrun error interrupt occurred -------------------------------------*/
770 if (((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && ((i2scr2 & I2S_IT_ERR) != RESET))
771 {
772 /* Disable RXNE and ERR interrupt */
774
775 /* Disable TXE and ERR interrupt */
776 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
777
778 /* Set the I2S State ready */
780
781 /* Set the error code and execute error callback*/
782 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
783 /* Call user error callback */
784#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
785 hi2s->ErrorCallback(hi2s);
786#else
788#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
789 }
790
791 /* I2Sext Underrun error interrupt occurred -------------------------------*/
792 if (((i2sextsr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && ((i2sextcr2 & I2S_IT_ERR) != RESET))
793 {
794 /* Disable TXE and ERR interrupt */
795 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
796
797 /* Disable RXNE and ERR interrupt */
799
800 /* Set the I2S State ready */
802
803 /* Set the error code and execute error callback*/
804 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
805 /* Call user error callback */
806#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
807 hi2s->ErrorCallback(hi2s);
808#else
810#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
811 }
812 }
813}
814
820__weak void HAL_I2SEx_TxRxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
821{
822 /* Prevent unused argument(s) compilation warning */
823 UNUSED(hi2s);
824
825 /* NOTE : This function Should not be modified, when the callback is needed,
826 the HAL_I2SEx_TxRxHalfCpltCallback could be implemented in the user file
827 */
828}
829
835__weak void HAL_I2SEx_TxRxCpltCallback(I2S_HandleTypeDef *hi2s)
836{
837 /* Prevent unused argument(s) compilation warning */
838 UNUSED(hi2s);
839
840 /* NOTE : This function should not be modified, when the callback is needed,
841 the HAL_I2SEx_TxRxCpltCallback could be implemented in the user file
842 */
843}
844
848
852
856
863static void I2SEx_TxRxDMAHalfCplt(DMA_HandleTypeDef *hdma)
864{
865 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
866
867 /* Call user TxRx Half complete callback */
868#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
869 hi2s->TxRxHalfCpltCallback(hi2s);
870#else
871 HAL_I2SEx_TxRxHalfCpltCallback(hi2s);
872#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
873}
874
881static void I2SEx_TxRxDMACplt(DMA_HandleTypeDef *hdma)
882{
883 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
884
885 /* If DMA is configured in DMA_NORMAL mode */
886 if (hdma->Init.Mode == DMA_NORMAL)
887 {
888 if (((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX) || \
889 ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX))
890 /* Disable Tx & Rx DMA Requests */
891 {
892 CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
893 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
894 }
895 else
896 {
897 CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
898 CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
899 }
900
901 hi2s->RxXferCount = 0U;
902 hi2s->TxXferCount = 0U;
903
905 }
906
907 /* Call user TxRx complete callback */
908#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
909 hi2s->TxRxCpltCallback(hi2s);
910#else
911 HAL_I2SEx_TxRxCpltCallback(hi2s);
912#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
913}
914
920static void I2SEx_TxRxDMAError(DMA_HandleTypeDef *hdma)
921{
922 I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
923
924 /* Disable Rx and Tx DMA Request */
925 CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
926 CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
927
928 hi2s->TxXferCount = 0U;
929 hi2s->RxXferCount = 0U;
930
932
933 /* Set the error code and execute error callback*/
934 SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
935 /* Call user error callback */
936#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
937 hi2s->ErrorCallback(hi2s);
938#else
940#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
941}
942
948static void I2SEx_TxISR_I2S(I2S_HandleTypeDef *hi2s)
949{
950 /* Write Data on DR register */
951 hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
952 hi2s->TxXferCount--;
953
954 if (hi2s->TxXferCount == 0U)
955 {
956 /* Disable TXE and ERR interrupt */
958
959 if (hi2s->RxXferCount == 0U)
960 {
962 /* Call user TxRx complete callback */
963#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
964 hi2s->TxRxCpltCallback(hi2s);
965#else
966 HAL_I2SEx_TxRxCpltCallback(hi2s);
967#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
968 }
969 }
970}
971
977static void I2SEx_TxISR_I2SExt(I2S_HandleTypeDef *hi2s)
978{
979 /* Write Data on DR register */
980 I2SxEXT(hi2s->Instance)->DR = (*hi2s->pTxBuffPtr++);
981 hi2s->TxXferCount--;
982
983 if (hi2s->TxXferCount == 0U)
984 {
985 /* Disable I2Sext TXE and ERR interrupt */
986 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
987
988 if (hi2s->RxXferCount == 0U)
989 {
991 /* Call user TxRx complete callback */
992#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
993 hi2s->TxRxCpltCallback(hi2s);
994#else
995 HAL_I2SEx_TxRxCpltCallback(hi2s);
996#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
997 }
998 }
999}
1000
1006static void I2SEx_RxISR_I2S(I2S_HandleTypeDef *hi2s)
1007{
1008 /* Read Data from DR register */
1009 (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
1010 hi2s->RxXferCount--;
1011
1012 if (hi2s->RxXferCount == 0U)
1013 {
1014 /* Disable RXNE and ERR interrupt */
1016
1017 if (hi2s->TxXferCount == 0U)
1018 {
1019 hi2s->State = HAL_I2S_STATE_READY;
1020 /* Call user TxRx complete callback */
1021#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1022 hi2s->TxRxCpltCallback(hi2s);
1023#else
1024 HAL_I2SEx_TxRxCpltCallback(hi2s);
1025#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1026 }
1027 }
1028}
1029
1035static void I2SEx_RxISR_I2SExt(I2S_HandleTypeDef *hi2s)
1036{
1037 /* Read Data from DR register */
1038 (*hi2s->pRxBuffPtr++) = I2SxEXT(hi2s->Instance)->DR;
1039 hi2s->RxXferCount--;
1040
1041 if (hi2s->RxXferCount == 0U)
1042 {
1043 /* Disable I2Sext RXNE and ERR interrupt */
1044 __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
1045
1046 if (hi2s->TxXferCount == 0U)
1047 {
1048 hi2s->State = HAL_I2S_STATE_READY;
1049 /* Call user TxRx complete callback */
1050#if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1051 hi2s->TxRxCpltCallback(hi2s);
1052#else
1053 HAL_I2SEx_TxRxCpltCallback(hi2s);
1054#endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1055 }
1056 }
1057}
1058
1068static HAL_StatusTypeDef I2SEx_FullDuplexWaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s,
1069 uint32_t Flag,
1070 uint32_t State,
1071 uint32_t Timeout,
1072 I2S_UseTypeDef i2sUsed)
1073{
1074 uint32_t tickstart = HAL_GetTick();
1075
1076 if (i2sUsed == I2S_USE_I2S)
1077 {
1078 /* Wait until flag is reset */
1079 while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
1080 {
1081 if (Timeout != HAL_MAX_DELAY)
1082 {
1083 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1084 {
1085 /* Set the I2S State ready */
1086 hi2s->State = HAL_I2S_STATE_READY;
1087
1088 /* Process Unlocked */
1089 __HAL_UNLOCK(hi2s);
1090
1091 return HAL_TIMEOUT;
1092 }
1093 }
1094 }
1095 }
1096 else /* i2sUsed == I2S_USE_I2SEXT */
1097 {
1098 /* Wait until flag is reset */
1099 while (((__HAL_I2SEXT_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
1100 {
1101 if (Timeout != HAL_MAX_DELAY)
1102 {
1103 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1104 {
1105 /* Set the I2S State ready */
1106 hi2s->State = HAL_I2S_STATE_READY;
1107
1108 /* Process Unlocked */
1109 __HAL_UNLOCK(hi2s);
1110
1111 return HAL_TIMEOUT;
1112 }
1113 }
1114 }
1115 }
1116 return HAL_OK;
1117}
1118
1122#endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
1123
1127#endif /* HAL_I2S_MODULE_ENABLED */
1128
1132
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
struct __DMA_HandleTypeDef DMA_HandleTypeDef
DMA handle Structure definition.
#define DMA_NORMAL
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define I2S_DATAFORMAT_24B
#define I2S_DATAFORMAT_32B
#define HAL_I2S_ERROR_UDR
#define HAL_I2S_ERROR_NONE
#define HAL_I2S_ERROR_DMA
#define HAL_I2S_ERROR_TIMEOUT
#define HAL_I2S_ERROR_OVR
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
struct __I2S_HandleTypeDef I2S_HandleTypeDef
I2S handle Structure definition.
@ HAL_I2S_STATE_BUSY_TX_RX
@ HAL_I2S_STATE_READY
#define __HAL_I2S_ENABLE(__HANDLE__)
Enable the specified SPI peripheral (in I2S mode).
#define __HAL_I2S_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the specified I2S interrupts.
#define __HAL_I2S_GET_FLAG(__HANDLE__, __FLAG__)
Checks whether the specified I2S flag is set or not.
#define __HAL_I2S_CLEAR_OVRFLAG(__HANDLE__)
Clears the I2S OVR pending flag.
#define __HAL_I2S_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the specified I2S interrupts.
#define __HAL_I2S_CLEAR_UDRFLAG(__HANDLE__)
Clears the I2S UDR pending flag.
#define I2S_FLAG_OVR
#define I2S_FLAG_RXNE
#define I2S_FLAG_TXE
#define I2S_FLAG_UDR
#define I2S_IT_RXNE
#define I2S_IT_ERR
#define I2S_IT_TXE
#define I2S_MODE_SLAVE_RX
#define I2S_MODE_MASTER_TX
#define I2S_MODE_SLAVE_TX
#define I2S_MODE_MASTER_RX
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_TIMEOUT
@ HAL_ERROR
@ HAL_OK
@ HAL_BUSY
#define __HAL_UNLOCK(__HANDLE__)
#define HAL_MAX_DELAY
#define __HAL_LOCK(__HANDLE__)
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
volatile uint16_t RxXferSize
volatile uint16_t TxXferSize
DMA_HandleTypeDef * hdmarx
volatile HAL_I2S_StateTypeDef State
DMA_HandleTypeDef * hdmatx
volatile uint32_t ErrorCode
volatile uint16_t TxXferCount
volatile uint16_t RxXferCount