HAL_I2S.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. ******************************************************************************
  3. * @file HAL_I2S.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief I2S HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Integrated Interchip Sound (I2S) peripheral:
  9. * + Initialization functions
  10. * + IO operation functions
  11. * + Peripheral State
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /*********************************************************************************
  16. * Function : HAL_I2S_IRQHandler
  17. * Description : This function handles I2S interrupt request.
  18. * Input :
  19. * Outpu :
  20. * Author : Chris_Kyle Data : 2020定
  21. **********************************************************************************/
  22. void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
  23. {
  24. /* Tx Buffer empty */
  25. if (hi2s->Instance->STATUS & I2S_STATUS_TXBE)
  26. {
  27. if (hi2s->u32_Tx_Count < hi2s->u32_Tx_Size)
  28. {
  29. hi2s->Instance->DAT = hi2s->u32_Tx_Buffer[hi2s->u32_Tx_Count++];
  30. }
  31. else
  32. {
  33. hi2s->Instance->IE &= ~I2S_DIE_TBEIE;
  34. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  35. }
  36. }
  37. /* Rx Buffer not empty */
  38. if (hi2s->Instance->STATUS & I2S_STATUS_RXBNE)
  39. {
  40. if (hi2s->u32_Rx_Count < hi2s->u32_Rx_Size)
  41. {
  42. hi2s->u32_Tx_Buffer[hi2s->u32_Rx_Count++] = hi2s->Instance->DAT;
  43. }
  44. else
  45. {
  46. /* Disable I2S */
  47. hi2s->Instance->CTL &= ~I2S_CTL_I2SEN;
  48. hi2s->Instance->IE &= ~I2S_DIE_RBNEIE;
  49. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  50. }
  51. }
  52. }
  53. /*********************************************************************************
  54. * Function : HAL_I2S_MspInit
  55. * Description :
  56. * Input :
  57. * Outpu :
  58. * Author : Chris_Kyle Data : 2020定
  59. **********************************************************************************/
  60. __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
  61. {
  62. /*
  63. NOTE : This function should be modified by the user.
  64. */
  65. /* For Example */
  66. GPIO_InitTypeDef GPIO_Handle;
  67. if (hi2s->Instance == I2S1)
  68. {
  69. /* Enable Clock */
  70. System_Module_Enable(EN_I2S1);
  71. System_Module_Enable(EN_GPIOAB);
  72. /* I2S1 WS PortA Pin4 */
  73. /* I2S1 CLK PortA Pin5 */
  74. /* I2S1 MCK PortA Pin6 */
  75. /* I2S1 SD PortA Pin7 */
  76. GPIO_Handle.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  77. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  78. GPIO_Handle.Pull = GPIO_PULLUP;
  79. GPIO_Handle.Alternate = GPIO_FUNCTION_8;
  80. HAL_GPIO_Init(GPIOA, &GPIO_Handle);
  81. /* Clear Pending Interrupt */
  82. NVIC_ClearPendingIRQ(I2S_IRQn);
  83. /* Enable External Interrupt */
  84. NVIC_EnableIRQ(I2S_IRQn);
  85. }
  86. }
  87. /*********************************************************************************
  88. * Function : HAL_I2S_MspDeInit
  89. * Description :
  90. * Input :
  91. * Outpu :
  92. * Author : Chris_Kyle Data : 2020定
  93. **********************************************************************************/
  94. __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
  95. {
  96. /*
  97. NOTE : This function should be modified by the user.
  98. */
  99. /* For Example */
  100. if (hi2s->Instance == I2S1)
  101. {
  102. /* I2S1 WS PortA Pin4 */
  103. /* I2S1 CLK PortA Pin5 */
  104. /* I2S1 MCK PortA Pin6 */
  105. /* I2S1 SD PortA Pin7 */
  106. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
  107. /* Clear Pending Interrupt */
  108. NVIC_ClearPendingIRQ(I2S_IRQn);
  109. /* Disable External Interrupt */
  110. NVIC_DisableIRQ(I2S_IRQn);
  111. }
  112. }
  113. /*********************************************************************************
  114. * Function : HAL_I2S_Init
  115. * Description : Initializes the I2S according to the specified parameters
  116. * in the I2S_InitTypeDef and create the associated handle.
  117. * Input : hi2s: pointer to a I2S_HandleTypeDef structure that contains
  118. * the configuration information for I2S module
  119. * Outpu :
  120. * Author : Chris_Kyle Data : 2020定
  121. **********************************************************************************/
  122. HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
  123. {
  124. #if (USE_FULL_ASSERT == 1)
  125. /* Check I2S Parameter */
  126. if (!IS_I2S_MODE(hi2s->Init.u32_Mode)) return HAL_ERROR;
  127. if (!IS_I2S_STANDARD(hi2s->Init.u32_Standard)) return HAL_ERROR;
  128. if (!IS_I2S_DATAFORMAT(hi2s->Init.u32_DataFormat)) return HAL_ERROR;
  129. if (!IS_I2S_MCLK_OUTPUT(hi2s->Init.u32_MCLKOutput)) return HAL_ERROR;
  130. if (!IS_I2S_MCLK_CPOL(hi2s->Init.u32_CPOL)) return HAL_ERROR;
  131. if (!IS_I2S_OF(hi2s->Init.u32_FreqOF)) return HAL_ERROR;
  132. if (!IS_I2S_DIV(hi2s->Init.u32_FreqDIV)) return HAL_ERROR;
  133. #endif
  134. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  135. HAL_I2S_MspInit(hi2s);
  136. /* Clear Config */
  137. hi2s->Instance->CTL = 0x00000000;
  138. hi2s->Instance->PSC = 0x00000000;
  139. /* Mode、Standard、CPOL、Dataformat */
  140. hi2s->Instance->CTL = hi2s->Init.u32_Mode | hi2s->Init.u32_Standard | hi2s->Init.u32_CPOL | hi2s->Init.u32_DataFormat;
  141. /* Frequency */
  142. hi2s->Instance->PSC = hi2s->Init.u32_MCLKOutput | hi2s->Init.u32_FreqOF | hi2s->Init.u32_FreqDIV;
  143. /* I2S Enable */
  144. if (hi2s->Init.u32_Mode != I2S_MODE_MASTER_RX)
  145. {
  146. hi2s->Instance->CTL |= I2S_CTL_I2SEN;
  147. }
  148. /* I2S Status ready */
  149. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  150. return HAL_OK;
  151. }
  152. /*********************************************************************************
  153. * Function : HAL_I2S_DeInit
  154. * Description : DeInitializes the I2S peripheral
  155. * Input : hi2s: pointer to a I2S_HandleTypeDef structure that contains
  156. * the configuration information for I2S module
  157. * Outpu :
  158. * Author : Chris_Kyle Data : 2020定
  159. **********************************************************************************/
  160. HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
  161. {
  162. /* Check the I2S handle allocation */
  163. if (hi2s == NULL)
  164. {
  165. return HAL_ERROR;
  166. }
  167. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  168. HAL_I2S_MspDeInit(hi2s);
  169. System_Module_Reset(RST_I2S1);
  170. return HAL_OK;
  171. }
  172. /*********************************************************************************
  173. * Function : HAL_I2S_Transmit
  174. * Description : Transmit an amount of data in blocking mode
  175. * Input : hi2s: pointer to a I2S_HandleTypeDef structure that contains
  176. * the configuration information for I2S module
  177. * Input : fp32_Data: 32-bit pointer to data buffer.
  178. * Input : Size: number of data sample to be sent
  179. * Input : fu32_Timeout: Timeout duration
  180. * Outpu :
  181. * Author : Chris_Kyle Data : 2020定
  182. **********************************************************************************/
  183. HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint32_t *fp32_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
  184. {
  185. uint32_t i;
  186. uint32_t lu32_Timeout;
  187. /* Parameter Check */
  188. if ((fp32_Data == NULL) || (fu32_Size == 0U))
  189. {
  190. return HAL_ERROR;
  191. }
  192. /* I2S Ready? */
  193. if (hi2s->I2S_Status != HAL_I2S_STATE_READY)
  194. {
  195. return HAL_BUSY;
  196. }
  197. hi2s->I2S_Status = HAL_I2S_STATE_BUSY_TX;
  198. /* transmit */
  199. for (i = 0; i < fu32_Size; i++)
  200. {
  201. hi2s->Instance->DAT = fp32_Data[i];
  202. /* have no timeout */
  203. if (fu32_Timeout == 0)
  204. {
  205. while(!(hi2s->Instance->STATUS & I2S_STATUS_TXBE));
  206. }
  207. else
  208. {
  209. lu32_Timeout = fu32_Timeout * 0xFF;
  210. while(!(hi2s->Instance->STATUS & I2S_STATUS_TXBE))
  211. {
  212. if (lu32_Timeout-- == 0)
  213. {
  214. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  215. return HAL_TIMEOUT;
  216. }
  217. }
  218. }
  219. }
  220. /* Wait for the last Byte */
  221. while (hi2s->Instance->STATUS & I2S_STATUS_TRANS);
  222. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  223. return HAL_OK;
  224. }
  225. /*********************************************************************************
  226. * Function : HAL_I2S_Receive
  227. * Description : Receive an amount of data in blocking mode
  228. * Input : hi2s pointer to a I2S_HandleTypeDef structure that contains
  229. * the configuration information for I2S module
  230. * Input : fp32_Data: a 32-bit pointer to data buffer.
  231. * Input : Size: number of data sample to be Receive
  232. * Input : fu32_Timeout: Timeout duration
  233. * Outpu :
  234. * Author : Chris_Kyle Data : 2020定
  235. **********************************************************************************/
  236. HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint32_t *fp32_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
  237. {
  238. uint32_t i;
  239. uint32_t lu32_Timeout;
  240. /* Parameter Check */
  241. if ((fp32_Data == NULL) || (fu32_Size == 0U))
  242. {
  243. return HAL_ERROR;
  244. }
  245. /* I2S Ready? */
  246. if (hi2s->I2S_Status != HAL_I2S_STATE_READY)
  247. {
  248. return HAL_BUSY;
  249. }
  250. hi2s->I2S_Status = HAL_I2S_STATE_BUSY_RX;
  251. /* I2S Enable */
  252. hi2s->Instance->CTL |= I2S_CTL_I2SEN;
  253. /* Receive */
  254. for (i = 0; i < fu32_Size; i++)
  255. {
  256. /* have no timeout */
  257. if (fu32_Timeout == 0)
  258. {
  259. while(!(hi2s->Instance->STATUS & I2S_STATUS_RXBNE));
  260. fp32_Data[i] = hi2s->Instance->DAT;
  261. }
  262. else
  263. {
  264. lu32_Timeout = fu32_Timeout * 0xFF;
  265. while(!(hi2s->Instance->STATUS & I2S_STATUS_RXBNE))
  266. {
  267. if (lu32_Timeout-- == 0)
  268. {
  269. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  270. return HAL_TIMEOUT;
  271. }
  272. }
  273. fp32_Data[i] = hi2s->Instance->DAT;
  274. }
  275. }
  276. /* Disable I2S */
  277. hi2s->Instance->CTL &= ~I2S_CTL_I2SEN;
  278. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  279. return HAL_OK;
  280. }
  281. /*********************************************************************************
  282. * Function : HAL_I2S_Transmit_IT
  283. * Description : Transmit an amount of data in non-blocking mode with Interrupt
  284. * Input : hi2s pointer to a I2S_HandleTypeDef structure that contains
  285. * the configuration information for I2S module
  286. * Input : fp32_Data: a 32-bit pointer to data buffer.
  287. * Input : Size: number of data sample to be send
  288. * Outpu :
  289. * Author : Chris_Kyle Data : 2020定
  290. **********************************************************************************/
  291. HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint32_t *fp32_Data, uint32_t fu32_Size)
  292. {
  293. /* Parameter Check */
  294. if ((fp32_Data == NULL) || (fu32_Size == 0U))
  295. {
  296. return HAL_ERROR;
  297. }
  298. /* I2S Ready? */
  299. if (hi2s->I2S_Status != HAL_I2S_STATE_READY)
  300. {
  301. return HAL_BUSY;
  302. }
  303. hi2s->I2S_Status = HAL_I2S_STATE_BUSY_TX;
  304. hi2s->u32_Tx_Buffer = fp32_Data;
  305. hi2s->u32_Tx_Size = fu32_Size;
  306. hi2s->u32_Tx_Count = 0;
  307. hi2s->Instance->IE |= I2S_DIE_TBEIE;
  308. return HAL_OK;
  309. }
  310. /*********************************************************************************
  311. * Function : HAL_I2S_Receive_IT
  312. * Description : Receive an amount of data in non-blocking mode with Interrupt
  313. * Input : hi2s pointer to a I2S_HandleTypeDef structure that contains
  314. * the configuration information for I2S module
  315. * Input : fp32_Data: a 32-bit pointer to data buffer.
  316. * Input : Size: number of data sample to be Receive
  317. * Outpu :
  318. * Author : Chris_Kyle Data : 2020定
  319. **********************************************************************************/
  320. HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint32_t *fp32_Data, uint32_t fu32_Size)
  321. {
  322. uint32_t lu32_Tempvalue;
  323. /* Parameter Check */
  324. if ((fp32_Data == NULL) || (fu32_Size == 0U))
  325. {
  326. return HAL_ERROR;
  327. }
  328. /* I2S Ready? */
  329. if (hi2s->I2S_Status != HAL_I2S_STATE_READY)
  330. {
  331. return HAL_BUSY;
  332. }
  333. /* Clear Rx Buffer */
  334. while (hi2s->Instance->STATUS & I2S_STATUS_RXBNE)
  335. {
  336. lu32_Tempvalue = hi2s->Instance->DAT;
  337. }
  338. hi2s->I2S_Status = HAL_I2S_STATE_BUSY_RX;
  339. hi2s->u32_Rx_Buffer = fp32_Data;
  340. hi2s->u32_Rx_Size = fu32_Size;
  341. hi2s->u32_Rx_Count = 0;
  342. hi2s->Instance->IE |= I2S_DIE_RBNEIE;
  343. /* I2S Enable */
  344. hi2s->Instance->CTL |= I2S_CTL_I2SEN;
  345. return HAL_OK;
  346. }
  347. /*********************************************************************************
  348. * Function : HAL_I2S_Transmit_DMA
  349. * Description : Transmit an amount of data in non-blocking mode with DMA
  350. * Input : hi2s: pointer to a I2S_HandleTypeDef structure that contains
  351. * the configuration information for I2S module
  352. * Input : fp32_Data: 32-bit pointer to data buffer.
  353. * Input : Size: number of data sample to be sent
  354. * Outpu :
  355. * Author : Chris_Kyle Data : 2020定
  356. **********************************************************************************/
  357. HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint32_t *fp32_Data, uint32_t fu32_Size)
  358. {
  359. /* Parameter Check */
  360. if ((fp32_Data == NULL) || (fu32_Size == 0U))
  361. {
  362. return HAL_ERROR;
  363. }
  364. /* DMA transfer complete */
  365. if (!(hi2s->I2S_Status & I2S_STATUS_TRANS))
  366. {
  367. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  368. }
  369. /* I2S Ready? */
  370. if (hi2s->I2S_Status != HAL_I2S_STATE_READY)
  371. {
  372. return HAL_BUSY;
  373. }
  374. hi2s->I2S_Status = HAL_I2S_STATE_BUSY_TX;
  375. hi2s->Instance->IE |= I2S_DIE_DMATEN;
  376. HAL_DMA_Start_IT(hi2s->HDMA_Tx, (uint32_t)fp32_Data, (uint32_t)&hi2s->Instance->DAT, fu32_Size);
  377. return HAL_OK;
  378. }
  379. /*********************************************************************************
  380. * Function : HAL_I2S_Receive_DMA
  381. * Description : Receive an amount of data in non-blocking mode with DMA
  382. * Input : hi2s pointer to a I2S_HandleTypeDef structure that contains
  383. * the configuration information for I2S module
  384. * Input : fp32_Data: a 32-bit pointer to data buffer.
  385. * Input : Size: number of data sample to be Receive
  386. * Outpu :
  387. * Author : Chris_Kyle Data : 2020定
  388. **********************************************************************************/
  389. HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint32_t *fp32_Data, uint32_t fu32_Size)
  390. {
  391. /* Parameter Check */
  392. if ((fp32_Data == NULL) || (fu32_Size == 0U))
  393. {
  394. return HAL_ERROR;
  395. }
  396. /* DMA transfer complete */
  397. if (!(hi2s->I2S_Status & I2S_STATUS_TRANS))
  398. {
  399. hi2s->I2S_Status = HAL_I2S_STATE_READY;
  400. }
  401. /* I2S Ready? */
  402. if (hi2s->I2S_Status != HAL_I2S_STATE_READY)
  403. {
  404. return HAL_BUSY;
  405. }
  406. hi2s->I2S_Status = HAL_I2S_STATE_BUSY_RX;
  407. hi2s->Instance->IE |= I2S_DIE_DMATEN;
  408. HAL_DMA_Start_IT(hi2s->HDMA_Rx, (uint32_t)&hi2s->Instance->DAT, (uint32_t)fp32_Data, fu32_Size);
  409. return HAL_OK;
  410. }