STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_i2s.c
Go to the documentation of this file.
1 
139 /* Includes ------------------------------------------------------------------*/
140 #include "stm32f7xx_hal.h"
141 
151 #ifdef HAL_I2S_MODULE_ENABLED
152 
153 /* Private typedef -----------------------------------------------------------*/
154 /* Private define ------------------------------------------------------------*/
155 /* Private macro -------------------------------------------------------------*/
156 /* Private variables ---------------------------------------------------------*/
157 /* Private function prototypes -----------------------------------------------*/
161 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
162 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
163 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
164 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
165 static void I2S_DMAError(DMA_HandleTypeDef *hdma);
166 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
167 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
168 static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef *hi2s);
169 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State, uint32_t Timeout);
174 /* Exported functions ---------------------------------------------------------*/
175 
217 {
218  uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
219  uint32_t tmp = 0, i2sclk = 0;
220 
221  /* Check the I2S handle allocation */
222  if(hi2s == NULL)
223  {
224  return HAL_ERROR;
225  }
226 
227  /* Check the parameters */
236 
237  if(hi2s->State == HAL_I2S_STATE_RESET)
238  {
239  /* Allocate lock resource and initialize it */
240  hi2s->Lock = HAL_UNLOCKED;
241  /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
242  HAL_I2S_MspInit(hi2s);
243  }
244 
245  hi2s->State = HAL_I2S_STATE_BUSY;
246 
247  /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
248  /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
250  SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \
251  SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD);
252  hi2s->Instance->I2SPR = 0x0002;
253 
254  /* Get the I2SCFGR register value */
255  tmpreg = hi2s->Instance->I2SCFGR;
256 
257  /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
259  {
260  i2sodd = (uint16_t)0;
261  i2sdiv = (uint16_t)2;
262  }
263  /* If the requested audio frequency is not the default, compute the prescaler */
264  else
265  {
266  /* Check the frame length (For the Prescaler computing) *******************/
267  if(hi2s->Init.DataFormat == I2S_DATAFORMAT_16B)
268  {
269  /* Packet length is 16 bits */
270  packetlength = 1;
271  }
272  else
273  {
274  /* Packet length is 32 bits */
275  packetlength = 2;
276  }
277 
278  /* Get I2S source Clock frequency ****************************************/
279 
280  /* If an external I2S clock has to be used, the specific define should be set
281  in the project configuration or in the stm32f3xx_conf.h file */
282  if(hi2s->Init.ClockSource == I2S_CLOCK_EXTERNAL)
283  {
284  /* Set the I2S clock to the external clock value */
285  i2sclk = EXTERNAL_CLOCK_VALUE;
286  }
287  else
288  {
289  /* Get the I2S source clock value */
290  i2sclk = I2S_GetClockFreq(hi2s);
291  }
292 
293  /* Compute the Real divider depending on the MCLK output state, with a floating point */
295  {
296  /* MCLK output is enabled */
297  tmp = (uint16_t)(((((i2sclk / 256) * 10) / hi2s->Init.AudioFreq)) + 5);
298  }
299  else
300  {
301  /* MCLK output is disabled */
302  tmp = (uint16_t)(((((i2sclk / (32 * packetlength)) *10 ) / hi2s->Init.AudioFreq)) + 5);
303  }
304 
305  /* Remove the flatting point */
306  tmp = tmp / 10;
307 
308  /* Check the parity of the divider */
309  i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
310 
311  /* Compute the i2sdiv prescaler */
312  i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
313 
314  /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
315  i2sodd = (uint16_t) (i2sodd << 8);
316  }
317 
318  /* Test if the divider is 1 or 0 or greater than 0xFF */
319  if((i2sdiv < 2) || (i2sdiv > 0xFF))
320  {
321  /* Set the default values */
322  i2sdiv = 2;
323  i2sodd = 0;
324  }
325 
326  /* Write to SPIx I2SPR register the computed value */
327  hi2s->Instance->I2SPR = (uint16_t)((uint16_t)i2sdiv | (uint16_t)(i2sodd | (uint16_t)hi2s->Init.MCLKOutput));
328 
329  /* Configure the I2S with the I2S_InitStruct values */
330  tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(hi2s->Init.Mode | \
331  (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \
332  (uint16_t)hi2s->Init.CPOL))));
333 
334  /* Write to SPIx I2SCFGR */
335  hi2s->Instance->I2SCFGR = tmpreg;
336 
338  hi2s->State= HAL_I2S_STATE_READY;
339 
340  return HAL_OK;
341 }
342 
350 {
351  /* Check the I2S handle allocation */
352  if(hi2s == NULL)
353  {
354  return HAL_ERROR;
355  }
356 
357  /* Check the parameters */
359 
360  hi2s->State = HAL_I2S_STATE_BUSY;
361 
362  /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
363  HAL_I2S_MspDeInit(hi2s);
364 
366  hi2s->State = HAL_I2S_STATE_RESET;
367 
368  /* Release Lock */
369  __HAL_UNLOCK(hi2s);
370 
371  return HAL_OK;
372 }
373 
380  __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
381 {
382  /* Prevent unused argument(s) compilation warning */
383  UNUSED(hi2s);
384 
385  /* NOTE : This function Should not be modified, when the callback is needed,
386  the HAL_I2S_MspInit could be implemented in the user file
387  */
388 }
389 
396  __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
397 {
398  /* Prevent unused argument(s) compilation warning */
399  UNUSED(hi2s);
400 
401  /* NOTE : This function Should not be modified, when the callback is needed,
402  the HAL_I2S_MspDeInit could be implemented in the user file
403  */
404 }
405 
467 HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
468 {
469  if((pData == NULL ) || (Size == 0))
470  {
471  return HAL_ERROR;
472  }
473 
474  if(hi2s->State == HAL_I2S_STATE_READY)
475  {
478  {
479  hi2s->TxXferSize = (Size << 1);
480  hi2s->TxXferCount = (Size << 1);
481  }
482  else
483  {
484  hi2s->TxXferSize = Size;
485  hi2s->TxXferCount = Size;
486  }
487 
488  /* Process Locked */
489  __HAL_LOCK(hi2s);
490 
493 
494  /* Check if the I2S is already enabled */
496  {
497  /* Enable I2S peripheral */
498  __HAL_I2S_ENABLE(hi2s);
499  }
500 
501  while(hi2s->TxXferCount > 0)
502  {
503  hi2s->Instance->DR = (*pData++);
504  hi2s->TxXferCount--;
505  /* Wait until TXE flag is set */
506  if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
507  {
508  /* Set the error code and execute error callback*/
510  HAL_I2S_ErrorCallback(hi2s);
511  return HAL_TIMEOUT;
512  }
513 
514  /* Check if an underrun occurs */
515  if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
516  {
517  /* Set the I2S State ready */
518  hi2s->State = HAL_I2S_STATE_READY;
519 
520  /* Process Unlocked */
521  __HAL_UNLOCK(hi2s);
522 
523  /* Set the error code and execute error callback*/
524  hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
525  HAL_I2S_ErrorCallback(hi2s);
526 
527  return HAL_ERROR;
528  }
529  }
530 
531  /* Check if Slave mode is selected */
533  {
534  /* Wait until Busy flag is reset */
535  if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, SET, Timeout) != HAL_OK)
536  {
537  /* Set the error code and execute error callback*/
539  HAL_I2S_ErrorCallback(hi2s);
540  return HAL_TIMEOUT;
541  }
542  }
543 
544  hi2s->State = HAL_I2S_STATE_READY;
545 
546  /* Process Unlocked */
547  __HAL_UNLOCK(hi2s);
548 
549  return HAL_OK;
550  }
551  else
552  {
553  return HAL_BUSY;
554  }
555 }
556 
574 HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
575 {
576  if((pData == NULL ) || (Size == 0))
577  {
578  return HAL_ERROR;
579  }
580 
581  if(hi2s->State == HAL_I2S_STATE_READY)
582  {
585  {
586  hi2s->RxXferSize = (Size << 1);
587  hi2s->RxXferCount = (Size << 1);
588  }
589  else
590  {
591  hi2s->RxXferSize = Size;
592  hi2s->RxXferCount = Size;
593  }
594  /* Process Locked */
595  __HAL_LOCK(hi2s);
596 
599 
600  /* Check if the I2S is already enabled */
602  {
603  /* Enable I2S peripheral */
604  __HAL_I2S_ENABLE(hi2s);
605  }
606 
607  /* Check if Master Receiver mode is selected */
609  {
610  /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
611  access to the SPI_SR register. */
613  }
614 
615  /* Receive data */
616  while(hi2s->RxXferCount > 0)
617  {
618  /* Wait until RXNE flag is set */
619  if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
620  {
621  /* Set the error code and execute error callback*/
623  HAL_I2S_ErrorCallback(hi2s);
624  return HAL_TIMEOUT;
625  }
626 
627  /* Check if an overrun occurs */
628  if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
629  {
630  /* Set the I2S State ready */
631  hi2s->State = HAL_I2S_STATE_READY;
632 
633  /* Process Unlocked */
634  __HAL_UNLOCK(hi2s);
635 
636  /* Set the error code and execute error callback*/
637  hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
638  HAL_I2S_ErrorCallback(hi2s);
639 
640  return HAL_ERROR;
641  }
642 
643  (*pData++) = hi2s->Instance->DR;
644  hi2s->RxXferCount--;
645  }
646 
647  hi2s->State = HAL_I2S_STATE_READY;
648 
649  /* Process Unlocked */
650  __HAL_UNLOCK(hi2s);
651 
652  return HAL_OK;
653  }
654  else
655  {
656  return HAL_BUSY;
657  }
658 }
659 
674 HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
675 {
676  if(hi2s->State == HAL_I2S_STATE_READY)
677  {
678  if((pData == NULL) || (Size == 0))
679  {
680  return HAL_ERROR;
681  }
682 
683  hi2s->pTxBuffPtr = pData;
686  {
687  hi2s->TxXferSize = (Size << 1);
688  hi2s->TxXferCount = (Size << 1);
689  }
690  else
691  {
692  hi2s->TxXferSize = Size;
693  hi2s->TxXferCount = Size;
694  }
695 
696  /* Process Locked */
697  __HAL_LOCK(hi2s);
698 
701 
702  /* Enable TXE and ERR interrupt */
704 
705  /* Check if the I2S is already enabled */
707  {
708  /* Enable I2S peripheral */
709  __HAL_I2S_ENABLE(hi2s);
710  }
711 
712  /* Process Unlocked */
713  __HAL_UNLOCK(hi2s);
714 
715  return HAL_OK;
716  }
717  else
718  {
719  return HAL_BUSY;
720  }
721 }
722 
739 HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
740 {
741  if(hi2s->State == HAL_I2S_STATE_READY)
742  {
743  if((pData == NULL) || (Size == 0))
744  {
745  return HAL_ERROR;
746  }
747 
748  hi2s->pRxBuffPtr = pData;
751  {
752  hi2s->RxXferSize = (Size << 1);
753  hi2s->RxXferCount = (Size << 1);
754  }
755  else
756  {
757  hi2s->RxXferSize = Size;
758  hi2s->RxXferCount = Size;
759  }
760  /* Process Locked */
761  __HAL_LOCK(hi2s);
762 
765 
766  /* Enable TXE and ERR interrupt */
768 
769  /* Check if the I2S is already enabled */
771  {
772  /* Enable I2S peripheral */
773  __HAL_I2S_ENABLE(hi2s);
774  }
775 
776  /* Process Unlocked */
777  __HAL_UNLOCK(hi2s);
778 
779  return HAL_OK;
780  }
781  else
782  {
783  return HAL_BUSY;
784  }
785 }
786 
801 HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
802 {
803  uint32_t *tmp;
804 
805  if((pData == NULL) || (Size == 0))
806  {
807  return HAL_ERROR;
808  }
809 
810  if(hi2s->State == HAL_I2S_STATE_READY)
811  {
812  hi2s->pTxBuffPtr = pData;
815  {
816  hi2s->TxXferSize = (Size << 1);
817  hi2s->TxXferCount = (Size << 1);
818  }
819  else
820  {
821  hi2s->TxXferSize = Size;
822  hi2s->TxXferCount = Size;
823  }
824 
825  /* Process Locked */
826  __HAL_LOCK(hi2s);
827 
830 
831  /* Set the I2S Tx DMA Half transfer complete callback */
832  hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
833 
834  /* Set the I2S TxDMA transfer complete callback */
835  hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
836 
837  /* Set the DMA error callback */
838  hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
839 
840  /* Enable the Tx DMA Channel */
841  tmp = (uint32_t*)&pData;
842  HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
843 
844  /* Check if the I2S is already enabled */
846  {
847  /* Enable I2S peripheral */
848  __HAL_I2S_ENABLE(hi2s);
849  }
850 
851  /* Enable Tx DMA Request */
852  hi2s->Instance->CR2 |= SPI_CR2_TXDMAEN;
853 
854  /* Process Unlocked */
855  __HAL_UNLOCK(hi2s);
856 
857  return HAL_OK;
858  }
859  else
860  {
861  return HAL_BUSY;
862  }
863 }
864 
879 HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
880 {
881  uint32_t *tmp;
882 
883  if((pData == NULL) || (Size == 0))
884  {
885  return HAL_ERROR;
886  }
887 
888  if(hi2s->State == HAL_I2S_STATE_READY)
889  {
890  hi2s->pRxBuffPtr = pData;
893  {
894  hi2s->RxXferSize = (Size << 1);
895  hi2s->RxXferCount = (Size << 1);
896  }
897  else
898  {
899  hi2s->RxXferSize = Size;
900  hi2s->RxXferCount = Size;
901  }
902  /* Process Locked */
903  __HAL_LOCK(hi2s);
904 
907 
908  /* Set the I2S Rx DMA Half transfer complete callback */
909  hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
910 
911  /* Set the I2S Rx DMA transfer complete callback */
912  hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
913 
914  /* Set the DMA error callback */
915  hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
916 
917  /* Check if Master Receiver mode is selected */
919  {
920  /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
921  access to the SPI_SR register. */
923  }
924 
925  /* Enable the Rx DMA Channel */
926  tmp = (uint32_t*)&pData;
927  HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize);
928 
929  /* Check if the I2S is already enabled */
931  {
932  /* Enable I2S peripheral */
933  __HAL_I2S_ENABLE(hi2s);
934  }
935 
936  /* Enable Rx DMA Request */
937  hi2s->Instance->CR2 |= SPI_CR2_RXDMAEN;
938 
939  /* Process Unlocked */
940  __HAL_UNLOCK(hi2s);
941 
942  return HAL_OK;
943  }
944  else
945  {
946  return HAL_BUSY;
947  }
948 }
949 
957 {
958  /* Process Locked */
959  __HAL_LOCK(hi2s);
960 
961  if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
962  {
963  /* Disable the I2S DMA Tx request */
964  hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
965  }
966  else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
967  {
968  /* Disable the I2S DMA Rx request */
969  hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
970  }
971  else if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
972  {
973  if((hi2s->Init.Mode == I2S_MODE_SLAVE_TX)||(hi2s->Init.Mode == I2S_MODE_MASTER_TX))
974  {
975  /* Disable the I2S DMA Tx request */
976  hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
977  }
978  else
979  {
980  /* Disable the I2S DMA Rx request */
981  hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
982  }
983  }
984 
985  /* Process Unlocked */
986  __HAL_UNLOCK(hi2s);
987 
988  return HAL_OK;
989 }
990 
998 {
999  /* Process Locked */
1000  __HAL_LOCK(hi2s);
1001 
1002  if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
1003  {
1004  /* Enable the I2S DMA Tx request */
1006  }
1007  else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
1008  {
1009  /* Enable the I2S DMA Rx request */
1011  }
1012 
1013  /* If the I2S peripheral is still not enabled, enable it */
1015  {
1016  /* Enable I2S peripheral */
1017  __HAL_I2S_ENABLE(hi2s);
1018  }
1019 
1020  /* Process Unlocked */
1021  __HAL_UNLOCK(hi2s);
1022 
1023  return HAL_OK;
1024 }
1025 
1033 {
1034  /* Process Locked */
1035  __HAL_LOCK(hi2s);
1036 
1037  /* Disable the I2S Tx/Rx DMA requests */
1040 
1041  /* Abort the I2S DMA Channel tx */
1042  if(hi2s->hdmatx != NULL)
1043  {
1044  /* Disable the I2S DMA channel */
1045  __HAL_DMA_DISABLE(hi2s->hdmatx);
1046  HAL_DMA_Abort(hi2s->hdmatx);
1047  }
1048  /* Abort the I2S DMA Channel rx */
1049  if(hi2s->hdmarx != NULL)
1050  {
1051  /* Disable the I2S DMA channel */
1052  __HAL_DMA_DISABLE(hi2s->hdmarx);
1053  HAL_DMA_Abort(hi2s->hdmarx);
1054  }
1055 
1056  /* Disable I2S peripheral */
1057  __HAL_I2S_DISABLE(hi2s);
1058 
1059  hi2s->State = HAL_I2S_STATE_READY;
1060 
1061  /* Process Unlocked */
1062  __HAL_UNLOCK(hi2s);
1063 
1064  return HAL_OK;
1065 }
1066 
1074 {
1075  __IO uint32_t i2ssr = hi2s->Instance->SR;
1076 
1077  if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
1078  {
1079  /* I2S in mode Receiver ----------------------------------------------------*/
1080  if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
1081  {
1082  I2S_Receive_IT(hi2s);
1083  }
1084 
1085  /* I2S Overrun error interrupt occurred -------------------------------------*/
1086  if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
1087  {
1088  /* Disable RXNE and ERR interrupt */
1090 
1091  /* Set the I2S State ready */
1092  hi2s->State = HAL_I2S_STATE_READY;
1093 
1094  /* Set the error code and execute error callback*/
1095  hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
1096  HAL_I2S_ErrorCallback(hi2s);
1097  }
1098  }
1099  else if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
1100  {
1101  /* I2S in mode Transmitter ---------------------------------------------------*/
1102  if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
1103  {
1104  I2S_Transmit_IT(hi2s);
1105  }
1106 
1107  /* I2S Underrun error interrupt occurred ------------------------------------*/
1108  if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
1109  {
1110  /* Disable TXE and ERR interrupt */
1112 
1113  /* Set the I2S State ready */
1114  hi2s->State = HAL_I2S_STATE_READY;
1115 
1116  /* Set the error code and execute error callback*/
1117  hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
1118  HAL_I2S_ErrorCallback(hi2s);
1119  }
1120  }
1121 }
1122 
1143 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag,
1144  uint32_t State, uint32_t Timeout)
1145 {
1146  uint32_t tickstart = 0;
1147 
1148  /* Get tick */
1149  tickstart = HAL_GetTick();
1150 
1151  /* Wait until flag is set */
1152  if(State == RESET)
1153  {
1154  while(__HAL_I2S_GET_FLAG(hi2s, Flag) == RESET)
1155  {
1156  if(Timeout != HAL_MAX_DELAY)
1157  {
1158  if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1159  {
1160  /* Set the I2S State ready */
1161  hi2s->State= HAL_I2S_STATE_READY;
1162 
1163  /* Process Unlocked */
1164  __HAL_UNLOCK(hi2s);
1165 
1166  return HAL_TIMEOUT;
1167  }
1168  }
1169  }
1170  }
1171  else
1172  {
1173  while(__HAL_I2S_GET_FLAG(hi2s, Flag) != RESET)
1174  {
1175  if(Timeout != HAL_MAX_DELAY)
1176  {
1177  if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1178  {
1179  /* Set the I2S State ready */
1180  hi2s->State= HAL_I2S_STATE_READY;
1181 
1182  /* Process Unlocked */
1183  __HAL_UNLOCK(hi2s);
1184 
1185  return HAL_TIMEOUT;
1186  }
1187  }
1188  }
1189  }
1190  return HAL_OK;
1191 }
1210 {
1211  /* Prevent unused argument(s) compilation warning */
1212  UNUSED(hi2s);
1213 
1214  /* NOTE : This function Should not be modified, when the callback is needed,
1215  the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
1216  */
1217 }
1218 
1225  __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
1226 {
1227  /* Prevent unused argument(s) compilation warning */
1228  UNUSED(hi2s);
1229 
1230  /* NOTE : This function Should not be modified, when the callback is needed,
1231  the HAL_I2S_TxCpltCallback could be implemented in the user file
1232  */
1233 }
1234 
1242 {
1243  /* Prevent unused argument(s) compilation warning */
1244  UNUSED(hi2s);
1245 
1246  /* NOTE : This function Should not be modified, when the callback is needed,
1247  the HAL_I2S_RxCpltCallback could be implemented in the user file
1248  */
1249 }
1250 
1257 __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
1258 {
1259  /* Prevent unused argument(s) compilation warning */
1260  UNUSED(hi2s);
1261 
1262  /* NOTE : This function Should not be modified, when the callback is needed,
1263  the HAL_I2S_RxCpltCallback could be implemented in the user file
1264  */
1265 }
1266 
1273  __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
1274 {
1275  /* Prevent unused argument(s) compilation warning */
1276  UNUSED(hi2s);
1277 
1278  /* NOTE : This function Should not be modified, when the callback is needed,
1279  the HAL_I2S_ErrorCallback could be implemented in the user file
1280  */
1281 }
1282 
1309 {
1310  return hi2s->State;
1311 }
1312 
1319 uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
1320 {
1321  return hi2s->ErrorCode;
1322 }
1337 static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef *hi2s)
1338 {
1339  uint32_t tmpreg = 0;
1340  /* This variable used to store the VCO Input (value in Hz) */
1341  uint32_t vcoinput = 0;
1342  /* This variable used to store the I2S_CK_x (value in Hz) */
1343  uint32_t i2sclocksource = 0;
1344 
1345  /* Configure I2S Clock based on I2S source clock selection */
1346 
1347  /* I2S_CLK_x : I2S Block Clock configuration for different clock sources selected */
1348  switch(hi2s->Init.ClockSource)
1349  {
1350  case I2S_CLOCK_PLL :
1351  {
1352  /* Configure the PLLI2S division factor */
1353  /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
1354  if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)
1355  {
1356  /* In Case the PLL Source is HSI (Internal Clock) */
1357  vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));
1358  }
1359  else
1360  {
1361  /* In Case the PLL Source is HSE (External Clock) */
1362  vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));
1363  }
1364 
1365  /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
1366  /* I2S_CLK(first level) = PLLI2S_VCO Output/PLLI2SR */
1367  tmpreg = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28;
1368  i2sclocksource = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6))/(tmpreg);
1369 
1370  break;
1371  }
1372  case I2S_CLOCK_EXTERNAL :
1373  {
1374  i2sclocksource = EXTERNAL_CLOCK_VALUE;
1375  break;
1376  }
1377  default :
1378  {
1379  break;
1380  }
1381  }
1382 
1383  /* the return result is the value of I2S clock */
1384  return i2sclocksource;
1385 }
1386 
1396 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
1397 {
1398  I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1399 
1400  if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
1401  {
1402  hi2s->TxXferCount = 0;
1403 
1404  /* Disable Tx DMA Request */
1405  hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
1406 
1407  if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
1408  {
1409  if(hi2s->RxXferCount == 0)
1410  {
1411  hi2s->State = HAL_I2S_STATE_READY;
1412  }
1413  }
1414  else
1415  {
1416  hi2s->State = HAL_I2S_STATE_READY;
1417  }
1418  }
1419  HAL_I2S_TxCpltCallback(hi2s);
1420 }
1421 
1428 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1429 {
1430  I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1431 
1433 }
1434 
1441 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
1442 {
1443  I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1444 
1445  if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
1446  {
1447  /* Disable Rx DMA Request */
1448  hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
1449 
1450  hi2s->RxXferCount = 0;
1451  if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)
1452  {
1453  if(hi2s->TxXferCount == 0)
1454  {
1455  hi2s->State = HAL_I2S_STATE_READY;
1456  }
1457  }
1458  else
1459  {
1460  hi2s->State = HAL_I2S_STATE_READY;
1461  }
1462  }
1463  HAL_I2S_RxCpltCallback(hi2s);
1464 }
1465 
1472 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1473 {
1474  I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1475 
1477 }
1478 
1485 static void I2S_DMAError(DMA_HandleTypeDef *hdma)
1486 {
1487  I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1488 
1489  /* Disable Rx and Tx DMA Request */
1490  hi2s->Instance->CR2 &= (uint32_t)(~(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
1491  hi2s->TxXferCount = 0;
1492  hi2s->RxXferCount = 0;
1493 
1494  hi2s->State= HAL_I2S_STATE_READY;
1495 
1496  /* Set the error code and execute error callback*/
1497  hi2s->ErrorCode |= HAL_I2S_ERROR_DMA;
1498  HAL_I2S_ErrorCallback(hi2s);
1499 }
1500 
1507 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
1508 {
1509  /* Transmit data */
1510  hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
1511  hi2s->TxXferCount--;
1512 
1513  if(hi2s->TxXferCount == 0)
1514  {
1515  /* Disable TXE and ERR interrupt */
1517 
1518  hi2s->State = HAL_I2S_STATE_READY;
1519  HAL_I2S_TxCpltCallback(hi2s);
1520  }
1521 }
1522 
1528 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
1529 {
1530  /* Receive data */
1531  (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
1532  hi2s->RxXferCount--;
1533 
1534  if(hi2s->RxXferCount == 0)
1535  {
1536  /* Disable RXNE and ERR interrupt */
1538 
1539  hi2s->State = HAL_I2S_STATE_READY;
1540  HAL_I2S_RxCpltCallback(hi2s);
1541  }
1542 }
1547 #endif /* HAL_I2S_MODULE_ENABLED */
1548 
1556 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define CLEAR_BIT(REG, BIT)
Definition: stm32f7xx.h:180
HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)
__IO uint32_t I2SCFGR
Definition: stm32f745xx.h:850
#define I2S_DATAFORMAT_24B
#define HAL_I2S_ERROR_NONE
__IO uint32_t DR
Definition: stm32f745xx.h:846
__IO HAL_I2S_StateTypeDef State
#define I2S_CLOCK_EXTERNAL
#define I2S_IT_ERR
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
#define I2S_FLAG_TXE
#define assert_param(expr)
Include module&#39;s header file.
SPI_TypeDef * Instance
#define HSI_VALUE
Internal High Speed oscillator (HSI) value. This value is used by the RCC HAL module to compute the s...
#define __HAL_I2S_CLEAR_OVRFLAG(__HANDLE__)
Clears the I2S OVR pending flag.
#define RCC_PLLSOURCE_HSI
Definition: stm32f7xx.h:155
void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
I2S handle Structure definition.
void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
#define I2S_IT_RXNE
#define __HAL_UNLOCK(__HANDLE__)
#define SPI_CR2_RXDMAEN
Definition: stm32f745xx.h:6609
#define I2S_FLAG_RXNE
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
#define __HAL_I2S_DISABLE(__HANDLE__)
__IO uint16_t TxXferSize
#define IS_I2S_AUDIO_FREQ(FREQ)
#define HAL_I2S_ERROR_OVR
HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
#define I2S_DATAFORMAT_32B
#define __HAL_I2S_DISABLE_IT(__HANDLE__, __INTERRUPT__)
__IO uint32_t SR
Definition: stm32f745xx.h:845
#define SPI_I2SCFGR_I2SCFG
Definition: stm32f745xx.h:6665
#define I2S_FLAG_OVR
#define RCC
Definition: stm32f745xx.h:1325
#define __HAL_I2S_GET_FLAG(__HANDLE__, __FLAG__)
Checks whether the specified I2S flag is set or not.
#define I2S_FLAG_BSY
#define HAL_I2S_ERROR_UDR
#define I2S_MODE_MASTER_RX
#define I2S_DATAFORMAT_16B
HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
#define HSE_VALUE
Adjust the value of External High Speed oscillator (HSE) used in your application. This value is used by the RCC HAL module to compute the system frequency (when HSE is used as system clock source, directly or through the PLL).
#define I2S_FLAG_UDR
#define __HAL_I2S_ENABLE(__HANDLE__)
Enable or disable the specified SPI peripheral (in I2S mode).
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
#define __HAL_LOCK(__HANDLE__)
#define EXTERNAL_CLOCK_VALUE
External clock source for I2S peripheral This value is used by the I2S HAL module to compute the I2S ...
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
#define NULL
Definition: usbd_def.h:53
#define DMA_SxCR_CIRC
Definition: stm32f745xx.h:3313
#define HAL_MAX_DELAY
#define __HAL_I2S_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__)
Checks if the specified I2S interrupt source is enabled or disabled.
#define RCC_PLLCFGR_PLLM
Definition: stm32f745xx.h:5206
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
#define __IO
Definition: core_cm0.h:213
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
#define I2S_MODE_SLAVE_RX
#define RCC_PLLCFGR_PLLSRC
Definition: stm32f745xx.h:5226
__IO uint16_t RxXferCount
#define RCC_PLLI2SCFGR_PLLI2SN
Definition: stm32f745xx.h:5631
This file contains all the functions prototypes for the HAL module driver.
__IO uint16_t RxXferSize
#define HAL_IS_BIT_CLR(REG, BIT)
DMA_Stream_TypeDef * Instance
#define SPI_I2SCFGR_DATLEN
Definition: stm32f745xx.h:6657
I2S_InitTypeDef Init
#define I2S_CLOCK_PLL
void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
#define IS_I2S_STANDARD(STANDARD)
#define IS_I2S_CPOL(CPOL)
HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
#define I2S_IT_TXE
HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
#define IS_I2S_DATA_FORMAT(FORMAT)
HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)
__IO uint16_t TxXferCount
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
__IO uint32_t CR
Definition: stm32f745xx.h:392
HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
#define UNUSED(x)
HAL_I2S_StateTypeDef
HAL State structures definition.
#define IS_I2S_ALL_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:8859
#define SPI_CR2_TXDMAEN
Definition: stm32f745xx.h:6610
#define SET_BIT(REG, BIT)
Definition: stm32f7xx.h:178
#define I2S_AUDIOFREQ_DEFAULT
#define IS_I2S_MCLK_OUTPUT(OUTPUT)
HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)
#define __HAL_DMA_DISABLE(__HANDLE__)
Disable the specified DMA Stream.
#define SPI_I2SCFGR_CKPOL
Definition: stm32f745xx.h:6660
DMA handle Structure definition.
__IO uint32_t ErrorCode
#define SPI_I2SCFGR_PCMSYNC
Definition: stm32f745xx.h:6664
HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
DMA_HandleTypeDef * hdmatx
#define IS_I2S_CLOCKSOURCE(CLOCK)
__IO uint32_t I2SPR
Definition: stm32f745xx.h:851
uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
#define I2S_MCLKOUTPUT_ENABLE
#define __HAL_I2S_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable or disable the specified I2S interrupts.
#define SPI_I2SCFGR_I2SE
Definition: stm32f745xx.h:6668
#define IS_I2S_MODE(MODE)
__IO HAL_LockTypeDef Lock
#define I2S_MODE_SLAVE_TX
#define HAL_I2S_ERROR_DMA
HAL_StatusTypeDef
HAL Status structures definition.
#define SPI_I2SCFGR_CHLEN
Definition: stm32f745xx.h:6656
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
__IO uint32_t CR2
Definition: stm32f745xx.h:844
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define I2S_MODE_MASTER_TX
#define HAL_I2S_ERROR_TIMEOUT
DMA_HandleTypeDef * hdmarx
#define SPI_I2SCFGR_I2SMOD
Definition: stm32f745xx.h:6669
HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
#define RCC_PLLI2SCFGR_PLLI2SR
Definition: stm32f745xx.h:5649
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)