1 /** 2 **************************************************************************************** 3 * 4 * @file gr55xx_hal_i2s.h 5 * @author BLE Driver Team 6 * @brief Header file containing functions prototypes of I2S HAL library. 7 * 8 **************************************************************************************** 9 * @attention 10 #####Copyright (c) 2019 GOODIX 11 All rights reserved. 12 13 Redistribution and use in source and binary forms, with or without 14 modification, are permitted provided that the following conditions are met: 15 * Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 * Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in the 19 documentation and/or other materials provided with the distribution. 20 * Neither the name of GOODIX nor the names of its contributors may be used 21 to endorse or promote products derived from this software without 22 specific prior written permission. 23 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35 **************************************************************************************** 36 */ 37 38 /** @addtogroup PERIPHERAL Peripheral Driver 39 * @{ 40 */ 41 42 /** @addtogroup HAL_DRIVER HAL Driver 43 * @{ 44 */ 45 46 /** @defgroup HAL_I2S I2S 47 * @brief I2S HAL module driver. 48 * @{ 49 */ 50 51 /* Define to prevent recursive inclusion -------------------------------------*/ 52 #ifndef __GR55xx_HAL_I2S_H__ 53 #define __GR55xx_HAL_I2S_H__ 54 55 /* Includes ------------------------------------------------------------------*/ 56 #include "gr55xx_ll_i2s.h" 57 #include "gr55xx_hal_def.h" 58 59 #ifdef __cplusplus 60 extern "C" { 61 #endif 62 63 /* Exported types ------------------------------------------------------------*/ 64 /** @addtogroup HAL_I2S_ENUMERATIONS Enumerations 65 * @{ 66 */ 67 68 /** @defgroup HAL_I2S_state HAL I2S state 69 * @{ 70 */ 71 72 /** 73 * @brief HAL I2S State Enumerations definition 74 */ 75 typedef enum { 76 HAL_I2S_STATE_RESET = 0x00, /**< Peripheral not initialized */ 77 HAL_I2S_STATE_READY = 0x01, /**< Peripheral initialized and ready for use */ 78 HAL_I2S_STATE_BUSY = 0x02, /**< An internal process is ongoing */ 79 HAL_I2S_STATE_BUSY_TX = 0x12, /**< Data Transmission process is ongoing */ 80 HAL_I2S_STATE_BUSY_RX = 0x22, /**< Data Reception process is ongoing */ 81 HAL_I2S_STATE_BUSY_TX_RX = 0x32, /**< Data Transmission and Reception process is ongoing */ 82 HAL_I2S_STATE_ABORT = 0x08, /**< Peripheral with abort request ongoing */ 83 HAL_I2S_STATE_ERROR = 0x04 /**< Peripheral in error */ 84 } hal_i2s_state_t; 85 86 /** @} */ 87 88 /** @} */ 89 90 /** @addtogroup HAL_I2S_STRUCTURES Structures 91 * @{ 92 */ 93 94 /** @defgroup I2S_Configuration I2S Configuration 95 * @{ 96 */ 97 98 /** 99 * @brief I2S init Structure definition 100 */ 101 typedef struct _i2s_init { 102 uint32_t data_size; /**< Specifies the data size for I2S communication. 103 This parameter can be a value of @ref I2S_Data_Size */ 104 105 uint32_t clock_source; /**< Specifies the source of the I2S clock. 106 This parameter can be a value of @ref I2S_Clock_Source */ 107 108 uint32_t audio_freq; /**< Specifies the frequency selected for the I2S communication. 109 @note The communication clock is derived from the master 110 clock. The slave clock does not need to be set. */ 111 #if I2S_CHANNEL_NUM > 1 112 uint32_t channel_active; /**< Specifies the active channels for I2S communication. 113 This parameter can be one or more value of @ref I2S_Channel */ 114 #endif 115 } i2s_init_t; 116 /** @} */ 117 118 /** @defgroup I2S_handle I2S handle 119 * @{ 120 */ 121 122 /** 123 * @brief I2S handle Structure definition 124 */ 125 typedef struct _i2s_handle { 126 i2s_regs_t *p_instance; /**< I2S registers base address */ 127 128 i2s_init_t init; /**< I2S communication parameters */ 129 130 uint16_t *p_tx_buffer; /**< Pointer to I2S TX transfer Buffer */ 131 132 __IO uint32_t tx_xfer_size; /**< I2S TX Transfer size */ 133 134 __IO uint32_t tx_xfer_count; /**< I2S TX Transfer Counter */ 135 136 uint16_t *p_rx_buffer; /**< Pointer to I2S RX transfer Buffer */ 137 138 __IO uint32_t rx_xfer_size; /**< I2S RX Transfer size */ 139 140 __IO uint32_t rx_xfer_count; /**< I2S RX Transfer Counter */ 141 142 void (*write_fifo)(struct _i2s_handle *p_i2s); /**< Pointer to I2S Tx transfer FIFO write function */ 143 144 void (*read_fifo)(struct _i2s_handle *p_i2s); /**< Pointer to I2S Rx transfer FIFO read function */ 145 146 dma_handle_t *p_dmatx; /**< I2S TX DMA Handle parameters */ 147 148 dma_handle_t *p_dmarx; /**< I2S RX DMA Handle parameters */ 149 150 __IO hal_lock_t lock; /**< Locking object */ 151 152 __IO hal_i2s_state_t state; /**< I2S communication state */ 153 154 __IO uint32_t error_code; /**< I2S Error code */ 155 156 uint32_t timeout; /**< Timeout for the I2S memory access */ 157 158 uint32_t retention[7]; /**< I2S important register information. */ 159 } i2s_handle_t; 160 /** @} */ 161 162 /** @} */ 163 164 /** @addtogroup HAL_I2S_CALLBACK_STRUCTURES Callback Structures 165 * @{ 166 */ 167 168 /** @defgroup HAL_I2S_Callback Callback 169 * @{ 170 */ 171 172 /** 173 * @brief HAL_I2S Callback function definition 174 */ 175 176 typedef struct _hal_i2s_callback { 177 void (*i2s_msp_init)(i2s_handle_t *p_i2s); /**< I2S init MSP callback */ 178 void (*i2s_msp_deinit)(i2s_handle_t *p_i2s); /**< I2S de-init MSP callback */ 179 void (*i2s_error_callback)(i2s_handle_t *p_i2s); /**< I2S error callback */ 180 void (*i2s_rx_cplt_callback)(i2s_handle_t *p_i2s); /**< I2S rx transfer completed callback */ 181 void (*i2s_tx_cplt_callback)(i2s_handle_t *p_i2s); /**< I2S tx transfer completed callbac */ 182 void (*i2s_tx_rx_cplt_callback)(i2s_handle_t *p_i2s); /**< I2S tx/rx transfer completed callback */ 183 } hal_i2s_callback_t; 184 185 /** @} */ 186 187 /** @} */ 188 189 /** 190 * @defgroup HAL_I2S_MACRO Defines 191 * @{ 192 */ 193 194 /* Exported constants --------------------------------------------------------*/ 195 /** @defgroup I2S_Exported_Constants I2S Exported Constants 196 * @{ 197 */ 198 199 /** @defgroup I2S_Direction I2S Direction 200 * @{ 201 */ 202 #define I2S_DIRECTION_FULL_DUPLEX LL_I2S_FULL_DUPLEX /**< Full Duplex: Transmit & Receive */ 203 #define I2S_DIRECTION_SIMPLEX_TX LL_I2S_SIMPLEX_TX /**< Simplex TX: Transmit only */ 204 #define I2S_DIRECTION_SIMPLEX_RX LL_I2S_SIMPLEX_RX /**< Simplex RX: Receive only */ 205 /** @} */ 206 207 /** @defgroup I2S_Error_Code I2S Error Code 208 * @{ 209 */ 210 #define HAL_I2S_ERROR_NONE ((uint32_t)0x00000000) /**< No error */ 211 #define HAL_I2S_ERROR_TIMEOUT ((uint32_t)0x00000001) /**< Timeout error */ 212 #define HAL_I2S_ERROR_TRANSFER ((uint32_t)0x00000002) /**< Transfer error */ 213 #define HAL_I2S_ERROR_DMA ((uint32_t)0x00000004) /**< DMA transfer error */ 214 #define HAL_I2S_ERROR_INVALID_PARAM ((uint32_t)0x00000008) /**< Invalid parameters error */ 215 /** @} */ 216 217 /** @defgroup I2S_Data_Size I2S Data Size 218 * @{ 219 */ 220 #define I2S_DATASIZE_12BIT LL_I2S_DATASIZE_12BIT /**< 12-bit serial data transfer */ 221 #define I2S_DATASIZE_16BIT LL_I2S_DATASIZE_16BIT /**< 16-bit serial data transfer */ 222 #define I2S_DATASIZE_20BIT LL_I2S_DATASIZE_20BIT /**< 20-bit serial data transfer */ 223 #define I2S_DATASIZE_24BIT LL_I2S_DATASIZE_24BIT /**< 24-bit serial data transfer */ 224 #define I2S_DATASIZE_32BIT LL_I2S_DATASIZE_32BIT /**< 32-bit serial data transfer */ 225 /** @} */ 226 227 /** @defgroup I2S_Clock_Source I2S Clock Source 228 * @{ 229 */ 230 #define I2S_CLOCK_SRC_96M LL_I2S_CLOCK_SRC_96M /**< Inactive state of SCLK is low */ 231 #define I2S_CLOCK_SRC_32M LL_I2S_CLOCK_SRC_32M /**< Inactive state of SCLK is high */ 232 /** @} */ 233 234 /** @defgroup I2S_FIFO_LEVEL_MAX I2S FIFO Level Max 235 * @{ 236 */ 237 #define I2S_TX_FIFO_LEVEL_MAX 16 /**< I2S TX FIFO Level Max Value */ 238 #define I2S_RX_FIFO_LEVEL_MAX 16 /**< I2S RX FIFO Level Max Value */ 239 /** @} */ 240 241 /** @defgroup I2S_Flags_definition I2S Flags Definition 242 * @{ 243 */ 244 #define I2S_FLAG_TXFO LL_I2S_STATUS_TXFO /**< TX FIFO write overflow flag */ 245 #define I2S_FLAG_TXFE LL_I2S_STATUS_TXFE /**< TX FIFO empty trigger flag */ 246 #define I2S_FLAG_RXFO LL_I2S_STATUS_RXFO /**< RX FIFO receive overflow flag */ 247 #define I2S_FLAG_RXDA LL_I2S_STATUS_RXDA /**< RX FIFO data available flag */ 248 /** @} */ 249 250 /** @defgroup I2S_Interrupt_definition I2S Interrupt Definition 251 * @{ 252 */ 253 #define I2S_IT_TXFO LL_I2S_INT_TXFO /**< TX FIFO write overflow interrupt */ 254 #define I2S_IT_TXFE LL_I2S_INT_TXFE /**< TX FIFO empty trigger interrupt */ 255 #define I2S_IT_RXFO LL_I2S_INT_RXFO /**< RX FIFO receive overflow interrupt */ 256 #define I2S_IT_RXDA LL_I2S_INT_RXDA /**< RX FIFO data available interrupt */ 257 /** @} */ 258 259 /** @defgroup I2S_Timeout_definition I2S Timeout_definition 260 * @{ 261 */ 262 #define HAL_I2S_TIMEOUT_DEFAULT_VALUE ((uint32_t)5000) /**< 5s */ 263 /** @} */ 264 265 /** @} */ 266 267 /* Exported macro ------------------------------------------------------------*/ 268 /** @defgroup I2S_Exported_Macros I2S Exported Macros 269 * @{ 270 */ 271 272 /** @brief Reset I2S handle states. 273 * @param __HANDLE__ I2S handle. 274 * @retval None 275 */ 276 #define HAL_I2S_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->state = HAL_I2S_STATE_RESET) 277 278 /** @brief Enable the specified I2S peripheral. 279 * @param __HANDLE__ Specifies the I2S Handle. 280 * @retval None 281 */ 282 #define HAL_I2S_ENABLE(__HANDLE__) SET_BITS((__HANDLE__)->p_instance->ENABLE, I2S_ENABLE_EN) 283 284 /** @brief Disable the specified I2S peripheral. 285 * @param __HANDLE__ Specifies the I2S Handle. 286 * @retval None 287 */ 288 #define HAL_I2S_DISABLE(__HANDLE__) CLEAR_BITS((__HANDLE__)->p_instance->ENABLE, I2S_ENABLE_EN) 289 290 /** @brief Enable the specified I2S clock. 291 * @param __HANDLE__ Specifies the I2S Handle. 292 * @retval None 293 */ 294 #define HAL_I2S_ENABLE_CLOCK(__HANDLE__) SET_BITS((__HANDLE__)->p_instance->CLKEN, I2S_CLKEN_EN) 295 296 /** @brief Disable the specified I2S clock. 297 * @param __HANDLE__ Specifies the I2S Handle. 298 * @retval None 299 */ 300 #define HAL_I2S_DISABLE_CLOCK(__HANDLE__) CLEAR_BITS((__HANDLE__)->p_instance->CLKEN, I2S_CLKEN_EN) 301 302 /** @brief Enable the specified I2S transmitter block. 303 * @param __HANDLE__ Specifies the I2S Handle. 304 * @retval None 305 */ 306 #define HAL_I2S_ENABLE_TX_BLOCK(__HANDLE__) ll_i2s_enable_txblock((__HANDLE__)->p_instance) 307 308 /** @brief Disable the specified I2S transmitter block. 309 * @param __HANDLE__ Specifies the I2S Handle. 310 * @retval None 311 */ 312 #define HAL_I2S_DISABLE_TX_BLOCK(__HANDLE__) ll_i2s_disable_txblock((__HANDLE__)->p_instance) 313 314 /** @brief Enable the specified I2S receiver block. 315 * @param __HANDLE__ Specifies the I2S Handle. 316 * @retval None 317 */ 318 #define HAL_I2S_ENABLE_RX_BLOCK(__HANDLE__) ll_i2s_enable_rxblock((__HANDLE__)->p_instance) 319 320 /** @brief Disable the specified I2S receiver block. 321 * @param __HANDLE__ Specifies the I2S Handle. 322 * @retval None 323 */ 324 #define HAL_I2S_DISABLE_RX_BLOCK(__HANDLE__) ll_i2s_disable_rxblock((__HANDLE__)->p_instance) 325 326 /** @brief Enable the specified I2S transmitter channel. 327 * @param __HANDLE__ Specifies the I2S Handle. 328 * @param __CH__ Specifies the I2S channel. 329 * @retval None 330 */ 331 #define HAL_I2S_ENABLE_TX_CHANNEL(__HANDLE__, __CH__) ll_i2s_enable_tx((__HANDLE__)->p_instance, (__CH__)) 332 333 /** @brief Disable the specified I2S transmitter channel. 334 * @param __HANDLE__ Specifies the I2S Handle. 335 * @param __CH__ Specifies the I2S channel. 336 * @retval None 337 */ 338 #define HAL_I2S_DISABLE_TX_CHANNEL(__HANDLE__, __CH__) ll_i2s_disable_tx((__HANDLE__)->p_instance, (__CH__)) 339 340 /** @brief Enable the specified I2S receiver channel. 341 * @param __HANDLE__ Specifies the I2S Handle. 342 * @param __CH__ Specifies the I2S channel. 343 * @retval None 344 */ 345 #define HAL_I2S_ENABLE_RX_CHANNEL(__HANDLE__, __CH__) ll_i2s_enable_rx((__HANDLE__)->p_instance, (__CH__)) 346 347 /** @brief Disable the specified I2S receiver channel. 348 * @param __HANDLE__ Specifies the I2S Handle. 349 * @param __CH__ Specifies the I2S channel. 350 * @retval None 351 */ 352 #define HAL_I2S_DISABLE_RX_CHANNEL(__HANDLE__, __CH__) ll_i2s_disable_rx((__HANDLE__)->p_instance, (__CH__)) 353 354 /** @brief Flush the I2S transmitter FIFO. 355 * @param __HANDLE__ Specifies the I2S Handle. 356 * @retval None 357 */ 358 #define HAL_I2S_FLUSH_TX_FIFO(__HANDLE__) ll_i2s_clr_txfifo_all((__HANDLE__)->p_instance) 359 360 /** @brief Flush the I2S receiver FIFO. 361 * @param __HANDLE__ Specifies the I2S Handle. 362 * @retval None 363 */ 364 #define HAL_I2S_FLUSH_RX_FIFO(__HANDLE__) ll_i2s_clr_rxfifo_all((__HANDLE__)->p_instance) 365 366 /** @brief Enable the I2S DMA Request. 367 * @param __HANDLE__ Specifies the I2S Handle. 368 * @retval None 369 */ 370 #define HAL_I2S_ENABLE_DMA(__HANDLE__) ll_i2s_enable_dma((__HANDLE__)->p_instance) 371 372 /** @brief Disable the I2S DMA Request. 373 * @param __HANDLE__ Specifies the I2S Handle. 374 * @retval None 375 */ 376 #define HAL_I2S_DISABLE_DMA(__HANDLE__) ll_i2s_disable_dma((__HANDLE__)->p_instance) 377 378 /** @brief Reset the I2S TX DMA request to the lowest enabled channel. 379 * @param __HANDLE__ Specifies the I2S Handle. 380 * @retval None 381 */ 382 #define HAL_I2S_RESET_TXDMA(__HANDLE__) WRITE_REG((__HANDLE__)->p_instance->TXDMA_RST, I2S_TXDMA_RST) 383 384 /** @brief Reset the I2S RX DMA request to the lowest enabled channel. 385 * @param __HANDLE__ Specifies the I2S Handle. 386 * @retval None 387 */ 388 #define HAL_I2S_RESET_RXDMA(__HANDLE__) WRITE_REG((__HANDLE__)->p_instance->RXDMA_RST, I2S_RXDMA_RST) 389 390 /** @brief Enable the specified I2S interrupts. 391 * @param __HANDLE__ Specifies the I2S Handle. 392 * @param __INTERRUPT__ Specifies the interrupt source to enable. 393 * This parameter can be one of the following values: 394 * @arg @ref I2S_IT_TXFO TX FIFO write overflow interrupt 395 * @arg @ref I2S_IT_TXFE TX FIFO empty trigger interrupt 396 * @arg @ref I2S_IT_RXFO RX FIFO receive overflow interrupt 397 * @arg @ref I2S_IT_RXDA RX FIFO data available interrupt 398 * @retval None 399 */ 400 #define HAL_I2S_ENABLE_IT(__HANDLE__, __INTERRUPT__) \ 401 CLEAR_BITS((__HANDLE__)->p_instance->I2S_CHANNEL[0].INTMASK, (__INTERRUPT__)) 402 403 /** @brief Disable the specified I2S interrupts. 404 * @param __HANDLE__ Specifies the I2S handle. 405 * @param __INTERRUPT__ Specifies the interrupt source to disable. 406 * This parameter can be one of the following values: 407 * @arg @ref I2S_IT_TXFO TX FIFO write overflow interrupt 408 * @arg @ref I2S_IT_TXFE TX FIFO empty trigger interrupt 409 * @arg @ref I2S_IT_RXFO RX FIFO receive overflow interrupt 410 * @arg @ref I2S_IT_RXDA RX FIFO data available interrupt 411 * @retval None 412 */ 413 #define HAL_I2S_DISABLE_IT(__HANDLE__, __INTERRUPT__) \ 414 SET_BITS((__HANDLE__)->p_instance->I2S_CHANNEL[0].INTMASK, (__INTERRUPT__)) 415 416 /** @brief Check whether the specified I2S flag is set or not. 417 * @param __HANDLE__ Specifies the I2S Handle. 418 * @param __FLAG__ Specifies the flag to check. 419 * This parameter can be one of the following values: 420 * @arg @ref I2S_FLAG_TXFO TX FIFO write overflow flag 421 * @arg @ref I2S_FLAG_TXFE TX FIFO empty trigger flag 422 * @arg @ref I2S_FLAG_RXFO RX FIFO receive overflow flag 423 * @arg @ref I2S_FLAG_RXDA RX FIFO data available flag 424 * @retval The new state of __FLAG__ (TRUE or FALSE). 425 */ 426 #define HAL_I2S_GET_FLAG(__HANDLE__, __FLAG__) \ 427 ((READ_BITS((__HANDLE__)->p_instance->I2S_CHANNEL[0].INTSTAT, (__FLAG__)) != 0) ? SET : RESET) 428 429 /* Private macros ------------------------------------------------------------*/ 430 /** @defgroup I2S_Private_Macro I2S Private Macros 431 * @{ 432 */ 433 434 /** @brief Check if I2S Direction Mode is valid. 435 * @param __MODE__ I2S Direction Mode. 436 * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid) 437 */ 438 #define IS_I2S_DIRECTION(__MODE__) (((__MODE__) == I2S_DIRECTION_FULL_DUPLEX) || \ 439 ((__MODE__) == I2S_DIRECTION_SIMPLEX_TX) || \ 440 ((__MODE__) == I2S_DIRECTION_SIMPLEX_RX)) 441 442 /** @brief Check if I2S Data Size is valid. 443 * @param __DATASIZE__ I2S Data Size. 444 * @retval SET (__DATASIZE__ is valid) or RESET (__DATASIZE__ is invalid) 445 */ 446 #define IS_I2S_DATASIZE(__DATASIZE__) (((__DATASIZE__) == I2S_DATASIZE_12BIT) || \ 447 ((__DATASIZE__) == I2S_DATASIZE_16BIT) || \ 448 ((__DATASIZE__) == I2S_DATASIZE_20BIT) || \ 449 ((__DATASIZE__) == I2S_DATASIZE_24BIT) || \ 450 ((__DATASIZE__) == I2S_DATASIZE_32BIT)) 451 452 /** @brief Check if I2S Clock Polarity is valid. 453 * @param __CPOL__ I2S Clock Polarity. 454 * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid) 455 */ 456 #define IS_I2S_CPOL(__CPOL__) (((__CPOL__) == I2S_POLARITY_LOW) || \ 457 ((__CPOL__) == I2S_POLARITY_HIGH)) 458 459 /** @brief Check if I2S Audio Frequency is valid. 460 * @param __FREQUENCY__ I2S Audio Frequency. 461 * @retval SET (__FREQUENCY__ is valid) or RESET (__FREQUENCY__ is invalid) 462 */ 463 #define IS_I2S_AUDIO_FREQUENCY(__FREQUENCY__) (((__FREQUENCY__) > 0) && ((__FREQUENCY__) <= 1500000)) 464 465 /** @brief Check if I2S FIFO Threshold is valid. 466 * @param __THR__ I2S FIFO Threshold. 467 * @retval SET (__THR__ is valid) or RESET (__THR__ is invalid) 468 */ 469 #define IS_I2S_FIFO_THRESHOLD(__THR__) (((__THR__) >= 0) && ((__THR__) <= I2S_TX_FIFO_LEVEL_MAX)) 470 471 /** @} */ 472 473 /** @} */ 474 475 /* Exported functions --------------------------------------------------------*/ 476 /** @addtogroup HAL_I2S_DRIVER_FUNCTIONS Functions 477 * @{ 478 */ 479 480 /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions 481 * @brief Initialization and de-initializations functions 482 * 483 @verbatim 484 =============================================================================== 485 ##### Initialization and de-initialization functions ##### 486 =============================================================================== 487 [..] This subsection provides a set of functions allowing to initialize and 488 de-initialize the I2Sx peripheral. 489 490 (+) User must implement hal_i2s_msp_init() function in which he configures 491 all related peripherals resources (GPIO, DMA, IT and NVIC ). 492 493 (+) Call the function hal_i2s_init() to configure the selected device with 494 the selected configuration: 495 (++) Data Size 496 (++) Clock Polarity 497 (++) Audio Frequency 498 499 (+) Call the function hal_i2s_deinit() to restore the default configuration 500 of the selected I2Sx peripheral. 501 502 @endverbatim 503 * @{ 504 */ 505 506 /** 507 **************************************************************************************** 508 * @brief Initialize the I2S according to the specified parameters 509 * in the i2s_init_t and initialize the associated handle. 510 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 511 * @retval ::HAL_OK: Operation is OK. 512 * @retval ::HAL_ERROR: Parameter error or operation not supported. 513 * @retval ::HAL_BUSY: Driver is busy. 514 * @retval ::HAL_TIMEOUT: Timeout occurred. 515 **************************************************************************************** 516 */ 517 hal_status_t hal_i2s_init(i2s_handle_t *p_i2s); 518 519 /** 520 **************************************************************************************** 521 * @brief De-initialize the I2S peripheral. 522 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 523 * @retval ::HAL_OK: Operation is OK. 524 * @retval ::HAL_ERROR: Parameter error or operation not supported. 525 * @retval ::HAL_BUSY: Driver is busy. 526 * @retval ::HAL_TIMEOUT: Timeout occurred. 527 **************************************************************************************** 528 */ 529 hal_status_t hal_i2s_deinit(i2s_handle_t *p_i2s); 530 531 /** 532 **************************************************************************************** 533 * @brief Initialize the I2S MSP. 534 * @note This function should not be modified. When the callback is needed, 535 the hal_i2s_msp_deinit can be implemented in the user file. 536 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 537 **************************************************************************************** 538 */ 539 void hal_i2s_msp_init(i2s_handle_t *p_i2s); 540 541 /** 542 **************************************************************************************** 543 * @brief De-initialize the I2S MSP. 544 * @note This function should not be modified. When the callback is needed, 545 the hal_i2s_msp_deinit can be implemented in the user file. 546 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 547 **************************************************************************************** 548 */ 549 void hal_i2s_msp_deinit(i2s_handle_t *p_i2s); 550 551 /** @} */ 552 553 /** @defgroup I2S_Exported_Functions_Group2 IO operation functions 554 * @brief Data transfers functions 555 * 556 @verbatim 557 ============================================================================== 558 ##### IO operation functions ##### 559 =============================================================================== 560 [..] 561 This subsection provides a set of functions allowing to manage the I2S 562 data transfers. 563 564 [..] The I2S supports master and slave mode: 565 566 (#) There are two modes of transfer: 567 (++) Blocking mode: The communication is performed in polling mode. 568 The HAL status of all data processing is returned by the same function 569 after finishing transfer. 570 (++) No-Blocking mode: The communication is performed using Interrupts 571 or DMA, These APIs return the HAL status. 572 The end of the data processing will be indicated through the 573 dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when 574 using DMA mode. 575 The hal_i2s_tx_cplt_callback(), hal_i2s_rx_cplt_callback() and hal_i2s_tx_rx_cplt_callback() user callbacks 576 will be executed respectively at the end of the transmit or Receive process 577 The hal_i2s_error_callback() user callback will be executed when a communication error is detected. 578 579 (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA) 580 exist for 1-Line (simplex) and 2-Line (full duplex) modes. 581 582 @endverbatim 583 * @{ 584 */ 585 586 /** 587 **************************************************************************************** 588 * @brief Transmit an amount of data in blocking mode. 589 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 590 * @param[in] p_data: Pointer to data buffer 591 * @param[in] length: Amount of data to be sent in halfword, data of a channel. 592 * For example, when 32 bytes of data need to be sent in each of the left and right channels, length = 16. 593 * @param[in] timeout: Timeout duration 594 * @retval ::HAL_OK: Operation is OK. 595 * @retval ::HAL_ERROR: Parameter error or operation not supported. 596 * @retval ::HAL_BUSY: Driver is busy. 597 * @retval ::HAL_TIMEOUT: Timeout occurred. 598 **************************************************************************************** 599 */ 600 hal_status_t hal_i2s_transmit(i2s_handle_t *p_i2s, uint16_t *p_data, uint32_t length, uint32_t timeout); 601 602 /** 603 **************************************************************************************** 604 * @brief Receive an amount of data in blocking mode. 605 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 606 * @param[out] p_data: Pointer to data buffer 607 * @param[in] length: Amount of data to be received in halfword, data of a channel. 608 * For example, when 32 bytes of data need to be sent in each of the left and right channels, length = 16. 609 * @param[in] timeout: Timeout duration 610 * @retval ::HAL_OK: Operation is OK. 611 * @retval ::HAL_ERROR: Parameter error or operation not supported. 612 * @retval ::HAL_BUSY: Driver is busy. 613 * @retval ::HAL_TIMEOUT: Timeout occurred. 614 **************************************************************************************** 615 */ 616 hal_status_t hal_i2s_receive(i2s_handle_t *p_i2s, uint16_t *p_data, uint32_t length, uint32_t timeout); 617 618 /** 619 **************************************************************************************** 620 * @brief Transmit and Receive an amount of data in blocking mode. 621 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 622 * @param[in] p_tx_data: Pointer to transmission data buffer 623 * @param[out] p_rx_data: Pointer to reception data buffer 624 * @param[in] length: Amount of data to be sent and received in bytes 625 * @param[in] timeout: Timeout duration 626 * @retval ::HAL_OK: Operation is OK. 627 * @retval ::HAL_ERROR: Parameter error or operation not supported. 628 * @retval ::HAL_BUSY: Driver is busy. 629 * @retval ::HAL_TIMEOUT: Timeout occurred. 630 **************************************************************************************** 631 */ 632 hal_status_t hal_i2s_transmit_receive(i2s_handle_t *p_i2s, uint16_t *p_tx_data, uint16_t *p_rx_data, 633 uint32_t length, uint32_t timeout); 634 635 /** 636 **************************************************************************************** 637 * @brief Transmit an amount of data in non-blocking mode with Interrupt. 638 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 639 * @param[in] p_data: Pointer to data buffer 640 * @param[in] length: Amount of data to be sent in halfword, data of a channel. 641 * For example, when 32 bytes of data need to be sent in each of the left and right channels, length = 16. 642 * @retval ::HAL_OK: Operation is OK. 643 * @retval ::HAL_ERROR: Parameter error or operation not supported. 644 * @retval ::HAL_BUSY: Driver is busy. 645 * @retval ::HAL_TIMEOUT: Timeout occurred. 646 **************************************************************************************** 647 */ 648 hal_status_t hal_i2s_transmit_it(i2s_handle_t *p_i2s, uint16_t *p_data, uint32_t length); 649 650 /** 651 **************************************************************************************** 652 * @brief Receive an amount of data in non-blocking mode with Interrupt. 653 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 654 * @param[out] p_data: Pointer to data buffer 655 * @param[in] length: Amount of data to be sent in halfword, data of a channel. 656 * For example, when 32 bytes of data need to be sent in each of the left and right channels, length = 16. 657 * @retval ::HAL_OK: Operation is OK. 658 * @retval ::HAL_ERROR: Parameter error or operation not supported. 659 * @retval ::HAL_BUSY: Driver is busy. 660 * @retval ::HAL_TIMEOUT: Timeout occurred. 661 **************************************************************************************** 662 */ 663 hal_status_t hal_i2s_receive_it(i2s_handle_t *p_i2s, uint16_t *p_data, uint32_t length); 664 665 /** 666 **************************************************************************************** 667 * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt. 668 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified SPI module. 669 * @param[in] p_tx_data: Pointer to transmission data buffer 670 * @param[out] p_rx_data: Pointer to reception data buffer 671 * @param[in] length: Amount of data to be sent and received in bytes 672 * @retval ::HAL_OK: Operation is OK. 673 * @retval ::HAL_ERROR: Parameter error or operation not supported. 674 * @retval ::HAL_BUSY: Driver is busy. 675 * @retval ::HAL_TIMEOUT: Timeout occurred. 676 **************************************************************************************** 677 */ 678 hal_status_t hal_i2s_transmit_receive_it(i2s_handle_t *p_i2s, uint16_t *p_tx_data, 679 uint16_t *p_rx_data, uint32_t length); 680 681 /** 682 **************************************************************************************** 683 * @brief Transmit an amount of data in non-blocking mode with DMA. 684 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 685 * @param[in] p_data: Pointer to data buffer 686 * @param[in] length: Amount of data to be sent in halfword, data of a channel, ranging between 1 and 4095. 687 * For example, when 32 bytes of data need to be sent in each of the left and right channels, length = 16. 688 * @retval ::HAL_OK: Operation is OK. 689 * @retval ::HAL_ERROR: Parameter error or operation not supported. 690 * @retval ::HAL_BUSY: Driver is busy. 691 * @retval ::HAL_TIMEOUT: Timeout occurred. 692 **************************************************************************************** 693 */ 694 hal_status_t hal_i2s_transmit_dma(i2s_handle_t *p_i2s, uint16_t *p_data, uint32_t length); 695 696 /** 697 **************************************************************************************** 698 * @brief Receive an amount of data in non-blocking mode with DMA. 699 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 700 * @param[out] p_data: Pointer to data buffer 701 * @param[in] length: Amount of data to be sent in halfword, data of a channel, ranging between 1 and 4095. 702 * For example, when 32 bytes of data need to be sent in each of the left and right channels, length = 16. 703 * @retval ::HAL_OK: Operation is OK. 704 * @retval ::HAL_ERROR: Parameter error or operation not supported. 705 * @retval ::HAL_BUSY: Driver is busy. 706 * @retval ::HAL_TIMEOUT: Timeout occurred. 707 **************************************************************************************** 708 */ 709 hal_status_t hal_i2s_receive_dma(i2s_handle_t *p_i2s, uint16_t *p_data, uint32_t length); 710 711 /** 712 **************************************************************************************** 713 * @brief Transmit and Receive an amount of data in non-blocking mode with DMA. 714 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 715 * @param[in] p_tx_data: Pointer to transmission data buffer 716 * @param[out] p_rx_data: Pointer to reception data buffer 717 * @param[in] length: Amount of data to be sent in bytes, ranging between 0 and 4095. 718 * @retval ::HAL_OK: Operation is OK. 719 * @retval ::HAL_ERROR: Parameter error or operation not supported. 720 * @retval ::HAL_BUSY: Driver is busy. 721 * @retval ::HAL_TIMEOUT: Timeout occurred. 722 **************************************************************************************** 723 */ 724 hal_status_t hal_i2s_transmit_receive_dma(i2s_handle_t *p_i2s, uint16_t *p_tx_data, 725 uint16_t *p_rx_data, uint32_t length); 726 727 /** 728 **************************************************************************************** 729 * @brief Start the I2S master clock. 730 * @note In case of SLAVE mode, this function will not take effect. 731 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 732 * @retval ::HAL_OK: Operation is OK. 733 * @retval ::HAL_ERROR: Parameter error or operation not supported. 734 * @retval ::HAL_BUSY: Driver is busy. 735 * @retval ::HAL_TIMEOUT: Timeout occurred. 736 **************************************************************************************** 737 */ 738 hal_status_t hal_i2s_start_clock(i2s_handle_t *p_i2s); 739 740 /** 741 **************************************************************************************** 742 * @brief Stop the I2S master clock. 743 * @note In case of SLAVE mode, this function will not take effect. 744 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 745 * @retval ::HAL_OK: Operation is OK. 746 * @retval ::HAL_ERROR: Parameter error or operation not supported. 747 * @retval ::HAL_BUSY: Driver is busy. 748 * @retval ::HAL_TIMEOUT: Timeout occurred. 749 **************************************************************************************** 750 */ 751 hal_status_t hal_i2s_stop_clock(i2s_handle_t *p_i2s); 752 753 /** 754 **************************************************************************************** 755 * @brief Abort ongoing transfer (blocking mode). 756 * @param[in] p_i2s: I2S handle. 757 * @note This procedure could be used for aborting any ongoing transfer (TX and RX), 758 * started in Interrupt or DMA mode. 759 * This procedure performs following operations : 760 * - Disable I2S Interrupts (depending of transfer direction) 761 * - Disable the DMA transfer in the peripheral register (if enabled) 762 * - Abort DMA transfer by calling hal_dma_abort (in case of transfer in DMA mode) 763 * - Set handle State to READY 764 * @note This procedure is executed in blocking mode: When exiting function, Abort is considered as completed. 765 * @retval ::HAL_OK: Operation is OK. 766 * @retval ::HAL_ERROR: Parameter error or operation not supported. 767 * @retval ::HAL_BUSY: Driver is busy. 768 * @retval ::HAL_TIMEOUT: Timeout occurred. 769 **************************************************************************************** 770 */ 771 hal_status_t hal_i2s_abort(i2s_handle_t *p_i2s); 772 773 /** @} */ 774 775 /** @addtogroup I2S_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks 776 * @brief IRQ Handler and Callbacks functions 777 * @{ 778 */ 779 780 /** 781 **************************************************************************************** 782 * @brief Handle I2S interrupt request. 783 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 784 **************************************************************************************** 785 */ 786 void hal_i2s_irq_handler(i2s_handle_t *p_i2s); 787 788 /** 789 **************************************************************************************** 790 * @brief TX Transfer completed callback. 791 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 792 **************************************************************************************** 793 */ 794 void hal_i2s_tx_cplt_callback(i2s_handle_t *p_i2s); 795 796 /** 797 **************************************************************************************** 798 * @brief RX Transfer completed callback. 799 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 800 **************************************************************************************** 801 */ 802 void hal_i2s_rx_cplt_callback(i2s_handle_t *p_i2s); 803 804 /** 805 **************************************************************************************** 806 * @brief TX/RX Transfer completed callback. 807 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 808 **************************************************************************************** 809 */ 810 void hal_i2s_tx_rx_cplt_callback(i2s_handle_t *p_i2s); 811 812 /** 813 **************************************************************************************** 814 * @brief I2S error callback. 815 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 816 **************************************************************************************** 817 */ 818 void hal_i2s_error_callback(i2s_handle_t *p_i2s); 819 820 /** @} */ 821 822 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions 823 * @brief I2S control functions 824 * 825 @verbatim 826 =============================================================================== 827 ##### Peripheral State and Errors functions ##### 828 =============================================================================== 829 [..] 830 This subsection provides a set of functions allowing to control the I2S. 831 (+) hal_i2s_get_state() API can be helpful to check in run-time the state of the I2S peripheral 832 (+) hal_i2s_get_error() check in run-time Errors occurring during communication 833 (+) hal_i2s_set_timeout() set the timeout during internal process 834 (+) hal_i2s_set_tx_fifo_threshold() set the TX FIFO Threshold 835 (+) hal_i2s_set_rx_fifo_threshold() set the RX FIFO Threshold 836 (+) hal_i2s_get_tx_fifo_threshold() get the TX FIFO Threshold 837 (+) hal_i2s_get_rx_fifo_threshold() get the RX FIFO Threshold 838 @endverbatim 839 * @{ 840 */ 841 842 /** 843 **************************************************************************************** 844 * @brief Return the I2S handle state. 845 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 846 * @retval ::HAL_I2S_STATE_RESET: Peripheral not initialized. 847 * @retval ::HAL_I2S_STATE_READY: Peripheral initialized and ready for use. 848 * @retval ::HAL_I2S_STATE_BUSY: An internal process is ongoing. 849 * @retval ::HAL_I2S_STATE_BUSY_TX: Data Transmii2son process is ongoing. 850 * @retval ::HAL_I2S_STATE_BUSY_RX: Data Reception process is ongoing. 851 * @retval ::HAL_I2S_STATE_ABORT: Peripheral with abort request ongoing. 852 * @retval ::HAL_I2S_STATE_ERROR: Peripheral in error. 853 **************************************************************************************** 854 */ 855 hal_i2s_state_t hal_i2s_get_state(i2s_handle_t *p_i2s); 856 857 /** 858 **************************************************************************************** 859 * @brief Return the I2S error code. 860 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 861 * @return I2S error code in bitmap format 862 **************************************************************************************** 863 */ 864 uint32_t hal_i2s_get_error(i2s_handle_t *p_i2s); 865 866 /** 867 **************************************************************************************** 868 * @brief Set the TX FIFO threshold. 869 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 870 * @param[in] threshold: TX FIFO threshold value ranging bwtween 0x0U ~ 0x7U. 871 * @retval ::HAL_OK: Operation is OK. 872 * @retval ::HAL_ERROR: Parameter error or operation not supported. 873 * @retval ::HAL_BUSY: Driver is busy. 874 * @retval ::HAL_TIMEOUT: Timeout occurred. 875 **************************************************************************************** 876 */ 877 hal_status_t hal_i2s_set_tx_fifo_threshold(i2s_handle_t *p_i2s, uint32_t threshold); 878 879 /** 880 **************************************************************************************** 881 * @brief Set the RX FIFO threshold. 882 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 883 * @param[in] threshold: RX FIFO threshold value ranging bwtween 0x0U ~ 0x7U. 884 * @retval ::HAL_OK: Operation is OK. 885 * @retval ::HAL_ERROR: Parameter error or operation not supported. 886 * @retval ::HAL_BUSY: Driver is busy. 887 * @retval ::HAL_TIMEOUT: Timeout occurred. 888 **************************************************************************************** 889 */ 890 hal_status_t hal_i2s_set_rx_fifo_threshold(i2s_handle_t *p_i2s, uint32_t threshold); 891 892 /** 893 **************************************************************************************** 894 * @brief Get the TX FIFO threshold. 895 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 896 * @return TX FIFO threshold 897 **************************************************************************************** 898 */ 899 uint32_t hal_i2s_get_tx_fifo_threshold(i2s_handle_t *p_i2s); 900 901 /** 902 **************************************************************************************** 903 * @brief Get the RX FIFO threshold. 904 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration information for the specified I2S module. 905 * @return RX FIFO threshold 906 **************************************************************************************** 907 */ 908 uint32_t hal_i2s_get_rx_fifo_threshold(i2s_handle_t *p_i2s); 909 910 /** 911 **************************************************************************************** 912 * @brief Suspend some registers related to I2S configuration before sleep. 913 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration 914 * information for the specified I2S module. 915 * @retval ::HAL_OK: Operation is OK. 916 * @retval ::HAL_ERROR: Parameter error or operation not supported. 917 * @retval ::HAL_BUSY: Driver is busy. 918 * @retval ::HAL_TIMEOUT: Timeout occurred. 919 **************************************************************************************** 920 */ 921 hal_status_t hal_i2s_suspend_reg(i2s_handle_t *p_i2s); 922 923 /** 924 **************************************************************************************** 925 * @brief Restore some registers related to I2S configuration after sleep. 926 * This function must be used in conjunction with the hal_i2s_suspend_reg(). 927 * @param[in] p_i2s: Pointer to a I2S handle which contains the configuration 928 * information for the specified I2S module. 929 * @retval ::HAL_OK: Operation is OK. 930 * @retval ::HAL_ERROR: Parameter error or operation not supported. 931 * @retval ::HAL_BUSY: Driver is busy. 932 * @retval ::HAL_TIMEOUT: Timeout occurred. 933 **************************************************************************************** 934 */ 935 hal_status_t hal_i2s_resume_reg(i2s_handle_t *p_i2s); 936 937 /** @} */ 938 939 /** @} */ 940 941 #ifdef __cplusplus 942 } 943 #endif 944 945 #endif /* __GR55xx_HAL_I2S_H__ */ 946 947 /** @} */ 948 949 /** @} */ 950 951 /** @} */ 952