Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2 | Rev 9 | ||
|---|---|---|---|
| Line 18... | Line 18... | ||
| 18 | (#) Declare a I2S_HandleTypeDef handle structure. |
18 | (#) Declare a I2S_HandleTypeDef handle structure. |
| 19 | (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API: |
19 | (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API: |
| 20 | (##) Enable the SPIx interface clock. |
20 | (##) Enable the SPIx interface clock. |
| 21 | (##) I2S pins configuration: |
21 | (##) I2S pins configuration: |
| 22 | (+++) Enable the clock for the I2S GPIOs. |
22 | (+++) Enable the clock for the I2S GPIOs. |
| 23 | (+++) Configure these I2S pins as alternate function. |
23 | (+++) Configure these I2S pins as alternate function pull-up. |
| 24 | (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT() |
24 | (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT() |
| 25 | and HAL_I2S_Receive_IT() APIs). |
25 | and HAL_I2S_Receive_IT() APIs). |
| 26 | (+++) Configure the I2Sx interrupt priority. |
26 | (+++) Configure the I2Sx interrupt priority. |
| 27 | (+++) Enable the NVIC I2S IRQ handle. |
27 | (+++) Enable the NVIC I2S IRQ handle. |
| 28 | (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA() |
28 | (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA() |
| 29 | and HAL_I2S_Receive_DMA() APIs: |
29 | and HAL_I2S_Receive_DMA() APIs: |
| 30 | (+++) Declare a DMA handle structure for the Tx/Rx Channel. |
30 | (+++) Declare a DMA handle structure for the Tx/Rx Stream/Channel. |
| 31 | (+++) Enable the DMAx interface clock. |
31 | (+++) Enable the DMAx interface clock. |
| 32 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
32 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
| 33 | (+++) Configure the DMA Tx/Rx Channel. |
33 | (+++) Configure the DMA Tx/Rx Stream/Channel. |
| 34 | (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle. |
34 | (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle. |
| 35 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the |
35 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the |
| 36 | DMA Tx/Rx Channel. |
36 | DMA Tx/Rx Stream/Channel. |
| 37 | 37 | ||
| 38 | (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity |
38 | (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity |
| 39 | using HAL_I2S_Init() function. |
39 | using HAL_I2S_Init() function. |
| 40 | 40 | ||
| 41 | -@- The specific I2S interrupts (Transmission complete interrupt, |
41 | -@- The specific I2S interrupts (Transmission complete interrupt, |
| 42 | RXNE interrupt and Error Interrupts) will be managed using the macros |
42 | RXNE interrupt and Error Interrupts) will be managed using the macros |
| 43 | __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process. |
43 | __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process. |
| 44 | -@- The I2SxCLK source is the system clock (provided by the HSI, the HSE or the PLL, and sourcing the AHB clock). |
44 | -@- The I2SxCLK source is the system clock (provided by the HSI, the HSE or the PLL, and sourcing the AHB clock). |
| 45 | For connectivity line devices, the I2SxCLK source can be either SYSCLK or the PLL3 VCO (2 x PLL3CLK) clock |
45 | For connectivity line devices, the I2SxCLK source can be either SYSCLK or the PLL3 VCO (2 x PLL3CLK) clock |
| 46 | in order to achieve the maximum accuracy. |
46 | in order to achieve the maximum accuracy. |
| 47 | -@- Make sure that either: |
47 | -@- Make sure that either: |
| 48 | (+@) External clock source is configured after setting correctly |
48 | (+@) External clock source is configured after setting correctly |
| 49 | the define constant HSE_VALUE in the stm32f1xx_hal_conf.h file. |
49 | the define constant HSE_VALUE in the stm32f1xx_hal_conf.h file. |
| 50 | 50 | ||
| 51 | (#) Three operation modes are available within this driver : |
51 | (#) Three mode of operations are available within this driver : |
| 52 | 52 | ||
| 53 | *** Polling mode IO operation *** |
53 | *** Polling mode IO operation *** |
| 54 | ================================= |
54 | ================================= |
| 55 | [..] |
55 | [..] |
| 56 | (+) Send an amount of data in blocking mode using HAL_I2S_Transmit() |
56 | (+) Send an amount of data in blocking mode using HAL_I2S_Transmit() |
| Line 88... | Line 88... | ||
| 88 | (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can |
88 | (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can |
| 89 | add his own code by customization of function pointer HAL_I2S_ErrorCallback |
89 | add his own code by customization of function pointer HAL_I2S_ErrorCallback |
| 90 | (+) Pause the DMA Transfer using HAL_I2S_DMAPause() |
90 | (+) Pause the DMA Transfer using HAL_I2S_DMAPause() |
| 91 | (+) Resume the DMA Transfer using HAL_I2S_DMAResume() |
91 | (+) Resume the DMA Transfer using HAL_I2S_DMAResume() |
| 92 | (+) Stop the DMA Transfer using HAL_I2S_DMAStop() |
92 | (+) Stop the DMA Transfer using HAL_I2S_DMAStop() |
| - | 93 | In Slave mode, if HAL_I2S_DMAStop is used to stop the communication, an error |
|
| - | 94 | HAL_I2S_ERROR_BUSY_LINE_RX is raised as the master continue to transmit data. |
|
| - | 95 | In this case __HAL_I2S_FLUSH_RX_DR macro must be used to flush the remaining data |
|
| - | 96 | inside DR register and avoid using DeInit/Init process for the next transfer. |
|
| 93 | 97 | ||
| 94 | *** I2S HAL driver macros list *** |
98 | *** I2S HAL driver macros list *** |
| 95 | ============================================= |
99 | =================================== |
| 96 | [..] |
100 | [..] |
| 97 | Below the list of most used macros in I2S HAL driver. |
101 | Below the list of most used macros in I2S HAL driver. |
| 98 | 102 | ||
| 99 | (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode) |
103 | (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode) |
| 100 | (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode) |
104 | (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode) |
| 101 | (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts |
105 | (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts |
| 102 | (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts |
106 | (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts |
| 103 | (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not |
107 | (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not |
| - | 108 | (+) __HAL_I2S_FLUSH_RX_DR: Read DR Register to Flush RX Data |
|
| 104 | 109 | ||
| 105 | [..] |
110 | [..] |
| 106 | (@) You can refer to the I2S HAL driver header file for more useful macros |
111 | (@) You can refer to the I2S HAL driver header file for more useful macros |
| 107 | 112 | ||
| - | 113 | *** I2S HAL driver macros list *** |
|
| - | 114 | =================================== |
|
| - | 115 | [..] |
|
| - | 116 | Callback registration: |
|
| 108 | 117 | ||
| - | 118 | (#) The compilation flag USE_HAL_I2S_REGISTER_CALLBACKS when set to 1U |
|
| - | 119 | allows the user to configure dynamically the driver callbacks. |
|
| - | 120 | Use Functions HAL_I2S_RegisterCallback() to register an interrupt callback. |
|
| - | 121 | ||
| - | 122 | Function HAL_I2S_RegisterCallback() allows to register following callbacks: |
|
| - | 123 | (++) TxCpltCallback : I2S Tx Completed callback |
|
| - | 124 | (++) RxCpltCallback : I2S Rx Completed callback |
|
| - | 125 | (++) TxHalfCpltCallback : I2S Tx Half Completed callback |
|
| - | 126 | (++) RxHalfCpltCallback : I2S Rx Half Completed callback |
|
| - | 127 | (++) ErrorCallback : I2S Error callback |
|
| - | 128 | (++) MspInitCallback : I2S Msp Init callback |
|
| - | 129 | (++) MspDeInitCallback : I2S Msp DeInit callback |
|
| - | 130 | This function takes as parameters the HAL peripheral handle, the Callback ID |
|
| 109 | *** I2C Workarounds linked to Silicon Limitation *** |
131 | and a pointer to the user callback function. |
| - | 132 | ||
| - | 133 | ||
| - | 134 | (#) Use function HAL_I2S_UnRegisterCallback to reset a callback to the default |
|
| - | 135 | weak function. |
|
| - | 136 | HAL_I2S_UnRegisterCallback takes as parameters the HAL peripheral handle, |
|
| - | 137 | and the Callback ID. |
|
| - | 138 | This function allows to reset following callbacks: |
|
| - | 139 | (++) TxCpltCallback : I2S Tx Completed callback |
|
| - | 140 | (++) RxCpltCallback : I2S Rx Completed callback |
|
| - | 141 | (++) TxHalfCpltCallback : I2S Tx Half Completed callback |
|
| - | 142 | (++) RxHalfCpltCallback : I2S Rx Half Completed callback |
|
| 110 | ==================================================== |
143 | (++) ErrorCallback : I2S Error callback |
| - | 144 | (++) MspInitCallback : I2S Msp Init callback |
|
| - | 145 | (++) MspDeInitCallback : I2S Msp DeInit callback |
|
| - | 146 | ||
| - | 147 | [..] |
|
| - | 148 | By default, after the HAL_I2S_Init() and when the state is HAL_I2S_STATE_RESET |
|
| - | 149 | all callbacks are set to the corresponding weak functions: |
|
| - | 150 | examples HAL_I2S_MasterTxCpltCallback(), HAL_I2S_MasterRxCpltCallback(). |
|
| - | 151 | Exception done for MspInit and MspDeInit functions that are |
|
| - | 152 | reset to the legacy weak functions in the HAL_I2S_Init()/ HAL_I2S_DeInit() only when |
|
| - | 153 | these callbacks are null (not registered beforehand). |
|
| - | 154 | If MspInit or MspDeInit are not null, the HAL_I2S_Init()/ HAL_I2S_DeInit() |
|
| - | 155 | keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. |
|
| - | 156 | ||
| 111 | [..] |
157 | [..] |
| - | 158 | Callbacks can be registered/unregistered in HAL_I2S_STATE_READY state only. |
|
| - | 159 | Exception done MspInit/MspDeInit functions that can be registered/unregistered |
|
| - | 160 | in HAL_I2S_STATE_READY or HAL_I2S_STATE_RESET state, |
|
| 112 | (@) Only the 16-bit mode with no data extension can be used when the I2S |
161 | thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. |
| - | 162 | Then, the user first registers the MspInit/MspDeInit user callbacks |
|
| - | 163 | using HAL_I2S_RegisterCallback() before calling HAL_I2S_DeInit() |
|
| - | 164 | or HAL_I2S_Init() function. |
|
| - | 165 | ||
| - | 166 | [..] |
|
| - | 167 | When the compilation define USE_HAL_I2S_REGISTER_CALLBACKS is set to 0 or |
|
| 113 | is in Master and used the PCM long synchronization mode. |
168 | not defined, the callback registering feature is not available |
| - | 169 | and weak (surcharged) callbacks are used. |
|
| 114 | 170 | ||
| - | 171 | *** I2S Workarounds linked to Silicon Limitation *** |
|
| - | 172 | ==================================================== |
|
| - | 173 | [..] |
|
| - | 174 | (@) Only the 16-bit mode with no data extension can be used when the I2S |
|
| - | 175 | is in Master and used the PCM long synchronization mode. |
|
| 115 | 176 | ||
| 116 | @endverbatim |
177 | @endverbatim |
| 117 | ****************************************************************************** |
178 | ****************************************************************************** |
| 118 | * @attention |
179 | * @attention |
| 119 | * |
180 | * |
| 120 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
181 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
| - | 182 | * All rights reserved.</center></h2> |
|
| 121 | * |
183 | * |
| 122 | * Redistribution and use in source and binary forms, with or without modification, |
184 | * This software component is licensed by ST under BSD 3-Clause license, |
| 123 | * are permitted provided that the following conditions are met: |
185 | * the "License"; You may not use this file except in compliance with the |
| 124 | * 1. Redistributions of source code must retain the above copyright notice, |
- | |
| 125 | * this list of conditions and the following disclaimer. |
- | |
| 126 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
- | |
| 127 | * this list of conditions and the following disclaimer in the documentation |
- | |
| 128 | * and/or other materials provided with the distribution. |
186 | * License. You may obtain a copy of the License at: |
| 129 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
- | |
| 130 | * may be used to endorse or promote products derived from this software |
187 | * opensource.org/licenses/BSD-3-Clause |
| 131 | * without specific prior written permission. |
- | |
| 132 | * |
- | |
| 133 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
- | |
| 134 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | |
| 135 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
- | |
| 136 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
- | |
| 137 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | |
| 138 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
- | |
| 139 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
- | |
| 140 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
- | |
| 141 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
- | |
| 142 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
- | |
| 143 | * |
188 | * |
| 144 | ****************************************************************************** |
189 | ****************************************************************************** |
| 145 | */ |
190 | */ |
| 146 | 191 | ||
| 147 | /* Includes ------------------------------------------------------------------*/ |
192 | /* Includes ------------------------------------------------------------------*/ |
| 148 | #include "stm32f1xx_hal.h" |
193 | #include "stm32f1xx_hal.h" |
| 149 | 194 | ||
| - | 195 | #ifdef HAL_I2S_MODULE_ENABLED |
|
| - | 196 | ||
| - | 197 | #if defined(SPI_I2S_SUPPORT) |
|
| 150 | /** @addtogroup STM32F1xx_HAL_Driver |
198 | /** @addtogroup STM32F1xx_HAL_Driver |
| 151 | * @{ |
199 | * @{ |
| 152 | */ |
200 | */ |
| 153 | 201 | ||
| 154 | #ifdef HAL_I2S_MODULE_ENABLED |
- | |
| 155 | #if defined(STM32F103xE) || defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC) |
- | |
| 156 | - | ||
| 157 | /** @defgroup I2S I2S |
202 | /** @defgroup I2S I2S |
| 158 | * @brief I2S HAL module driver |
203 | * @brief I2S HAL module driver |
| 159 | * @{ |
204 | * @{ |
| 160 | */ |
205 | */ |
| 161 | 206 | ||
| 162 | /* Private typedef -----------------------------------------------------------*/ |
207 | /* Private typedef -----------------------------------------------------------*/ |
| 163 | /* Private define ------------------------------------------------------------*/ |
208 | /* Private define ------------------------------------------------------------*/ |
| - | 209 | #define I2S_TIMEOUT_FLAG 100U /*!< Timeout 100 ms */ |
|
| 164 | /* Private macro -------------------------------------------------------------*/ |
210 | /* Private macro -------------------------------------------------------------*/ |
| 165 | /* Private variables ---------------------------------------------------------*/ |
211 | /* Private variables ---------------------------------------------------------*/ |
| 166 | /* Private function prototypes -----------------------------------------------*/ |
212 | /* Private function prototypes -----------------------------------------------*/ |
| 167 | /** @addtogroup I2S_Private_Functions I2S Private Functions |
213 | /** @defgroup I2S_Private_Functions I2S Private Functions |
| 168 | * @{ |
214 | * @{ |
| 169 | */ |
215 | */ |
| 170 | static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma); |
216 | static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma); |
| 171 | static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma); |
217 | static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma); |
| 172 | static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma); |
218 | static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma); |
| 173 | static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma); |
219 | static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma); |
| 174 | static void I2S_DMAError(DMA_HandleTypeDef *hdma); |
220 | static void I2S_DMAError(DMA_HandleTypeDef *hdma); |
| 175 | static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s); |
221 | static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s); |
| 176 | static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s); |
222 | static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s); |
| 177 | static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s); |
- | |
| 178 | static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State, |
223 | static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State, |
| 179 | uint32_t Timeout); |
224 | uint32_t Timeout); |
| 180 | /** |
225 | /** |
| 181 | * @} |
226 | * @} |
| 182 | */ |
227 | */ |
| 183 | 228 | ||
| 184 | /* Exported functions ---------------------------------------------------------*/ |
229 | /* Exported functions ---------------------------------------------------------*/ |
| - | 230 | ||
| 185 | /** @defgroup I2S_Exported_Functions I2S Exported Functions |
231 | /** @defgroup I2S_Exported_Functions I2S Exported Functions |
| 186 | * @{ |
232 | * @{ |
| 187 | */ |
233 | */ |
| 188 | 234 | ||
| 189 | /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions |
235 | /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions |
| 190 | * @brief Initialization and Configuration functions |
236 | * @brief Initialization and Configuration functions |
| 191 | * |
237 | * |
| 192 | @verbatim |
238 | @verbatim |
| 193 | =============================================================================== |
239 | =============================================================================== |
| 194 | ##### Initialization and de-initialization functions ##### |
240 | ##### Initialization and de-initialization functions ##### |
| Line 206... | Line 252... | ||
| 206 | (++) Data Format |
252 | (++) Data Format |
| 207 | (++) MCLK Output |
253 | (++) MCLK Output |
| 208 | (++) Audio frequency |
254 | (++) Audio frequency |
| 209 | (++) Polarity |
255 | (++) Polarity |
| 210 | 256 | ||
| 211 | (+) Call the function HAL_I2S_DeInit() to restore the default configuration |
257 | (+) Call the function HAL_I2S_DeInit() to restore the default configuration |
| 212 | of the selected I2Sx peripheral. |
258 | of the selected I2Sx peripheral. |
| 213 | @endverbatim |
259 | @endverbatim |
| 214 | * @{ |
260 | * @{ |
| 215 | */ |
261 | */ |
| 216 | 262 | ||
| 217 | /** |
263 | /** |
| 218 | * @brief Initializes the I2S according to the specified parameters |
264 | * @brief Initializes the I2S according to the specified parameters |
| 219 | * in the I2S_InitTypeDef and create the associated handle. |
265 | * in the I2S_InitTypeDef and create the associated handle. |
| 220 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
266 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 221 | * the configuration information for I2S module |
267 | * the configuration information for I2S module |
| 222 | * @retval HAL status |
268 | * @retval HAL status |
| 223 | */ |
269 | */ |
| 224 | HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s) |
270 | HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s) |
| 225 | { |
271 | { |
| - | 272 | uint32_t i2sdiv; |
|
| - | 273 | uint32_t i2sodd; |
|
| 226 | uint32_t tmpreg = 0U, i2sdiv = 2U, i2sodd = 0U, packetlength = 16U; |
274 | uint32_t packetlength; |
| - | 275 | uint32_t tmp; |
|
| 227 | uint32_t tmp = 0U, i2sclk = 0U; |
276 | uint32_t i2sclk; |
| 228 | 277 | ||
| 229 | /* Check the I2S handle allocation */ |
278 | /* Check the I2S handle allocation */ |
| 230 | if(hi2s == NULL) |
279 | if (hi2s == NULL) |
| 231 | { |
280 | { |
| 232 | return HAL_ERROR; |
281 | return HAL_ERROR; |
| 233 | } |
282 | } |
| 234 | 283 | ||
| 235 | /* Check the I2S parameters */ |
284 | /* Check the I2S parameters */ |
| Line 239... | Line 288... | ||
| 239 | assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat)); |
288 | assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat)); |
| 240 | assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput)); |
289 | assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput)); |
| 241 | assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq)); |
290 | assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq)); |
| 242 | assert_param(IS_I2S_CPOL(hi2s->Init.CPOL)); |
291 | assert_param(IS_I2S_CPOL(hi2s->Init.CPOL)); |
| 243 | 292 | ||
| 244 | hi2s->State = HAL_I2S_STATE_BUSY; |
293 | if (hi2s->State == HAL_I2S_STATE_RESET) |
| - | 294 | { |
|
| - | 295 | /* Allocate lock resource and initialize it */ |
|
| - | 296 | hi2s->Lock = HAL_UNLOCKED; |
|
| 245 | 297 | ||
| - | 298 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| 246 | /* Initialize Default I2S IrqHandler ISR */ |
299 | /* Init the I2S Callback settings */ |
| - | 300 | hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
|
| - | 301 | hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
|
| - | 302 | hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ |
|
| - | 303 | hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ |
|
| - | 304 | hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */ |
|
| - | 305 | ||
| - | 306 | if (hi2s->MspInitCallback == NULL) |
|
| - | 307 | { |
|
| - | 308 | hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */ |
|
| - | 309 | } |
|
| - | 310 | ||
| - | 311 | /* Init the low level hardware : GPIO, CLOCK, NVIC... */ |
|
| 247 | hi2s->IrqHandlerISR = I2S_IRQHandler; |
312 | hi2s->MspInitCallback(hi2s); |
| - | 313 | #else |
|
| - | 314 | /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ |
|
| - | 315 | HAL_I2S_MspInit(hi2s); |
|
| - | 316 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| - | 317 | } |
|
| 248 | 318 | ||
| 249 | /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ |
- | |
| 250 | HAL_I2S_MspInit(hi2s); |
319 | hi2s->State = HAL_I2S_STATE_BUSY; |
| 251 | 320 | ||
| 252 | /*----------------------- SPIx I2SCFGR & I2SPR Configuration ---------------*/ |
321 | /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/ |
| 253 | /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */ |
322 | /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */ |
| 254 | CLEAR_BIT(hi2s->Instance->I2SCFGR,(SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \ |
323 | CLEAR_BIT(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \ |
| 255 | SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \ |
324 | SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \ |
| 256 | SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD)); |
325 | SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD)); |
| 257 | hi2s->Instance->I2SPR = 0x0002U; |
326 | hi2s->Instance->I2SPR = 0x0002U; |
| 258 | 327 | ||
| 259 | /* Get the I2SCFGR register value */ |
- | |
| 260 | tmpreg = hi2s->Instance->I2SCFGR; |
- | |
| 261 | - | ||
| 262 | /* If the default frequency value has to be written, reinitialize i2sdiv and i2sodd */ |
328 | /*----------------------- I2SPR: I2SDIV and ODD Calculation -----------------*/ |
| 263 | /* If the requested audio frequency is not the default, compute the prescaler */ |
329 | /* If the requested audio frequency is not the default, compute the prescaler */ |
| 264 | if(hi2s->Init.AudioFreq != I2S_AUDIOFREQ_DEFAULT) |
330 | if (hi2s->Init.AudioFreq != I2S_AUDIOFREQ_DEFAULT) |
| 265 | { |
331 | { |
| 266 | /* Check the frame length (For the Prescaler computing) *******************/ |
332 | /* Check the frame length (For the Prescaler computing) ********************/ |
| 267 | /* Set I2S Packet Length value*/ |
- | |
| 268 | if(hi2s->Init.DataFormat != I2S_DATAFORMAT_16B) |
333 | if (hi2s->Init.DataFormat == I2S_DATAFORMAT_16B) |
| 269 | { |
334 | { |
| 270 | /* Packet length is 32 bits */ |
335 | /* Packet length is 16 bits */ |
| 271 | packetlength = 32U; |
336 | packetlength = 16U; |
| 272 | } |
337 | } |
| 273 | else |
338 | else |
| 274 | { |
339 | { |
| 275 | /* Packet length is 16 bits */ |
340 | /* Packet length is 32 bits */ |
| 276 | packetlength = 16U; |
341 | packetlength = 32U; |
| 277 | } |
342 | } |
| 278 | 343 | ||
| 279 | /* I2S standard */ |
344 | /* I2S standard */ |
| 280 | if(hi2s->Init.Standard <= I2S_STANDARD_LSB) |
345 | if (hi2s->Init.Standard <= I2S_STANDARD_LSB) |
| 281 | { |
346 | { |
| 282 | /* In I2S standard packet lenght is multiplied by 2 */ |
347 | /* In I2S standard packet length is multiplied by 2 */ |
| 283 | packetlength = packetlength * 2U; |
348 | packetlength = packetlength * 2U; |
| 284 | } |
349 | } |
| 285 | 350 | ||
| - | 351 | /* Get the source clock value **********************************************/ |
|
| 286 | if(hi2s->Instance == SPI2) |
352 | if (hi2s->Instance == SPI2) |
| 287 | { |
353 | { |
| 288 | /* Get the source clock value: based on SPI2 Instance */ |
354 | /* Get the source clock value: based on SPI2 Instance */ |
| 289 | i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S2); |
355 | i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S2); |
| 290 | } |
356 | } |
| 291 | else if(hi2s->Instance == SPI3) |
357 | else if (hi2s->Instance == SPI3) |
| 292 | { |
358 | { |
| 293 | /* Get the source clock value: based on SPI3 Instance */ |
359 | /* Get the source clock value: based on SPI3 Instance */ |
| 294 | i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S3); |
360 | i2sclk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S3); |
| 295 | } |
361 | } |
| 296 | else |
362 | else |
| 297 | { |
363 | { |
| 298 | /* Get the source clock value: based on System Clock value */ |
364 | /* Get the source clock value: based on System Clock value */ |
| 299 | i2sclk = HAL_RCC_GetSysClockFreq(); |
365 | i2sclk = HAL_RCC_GetSysClockFreq(); |
| 300 | } |
366 | } |
| 301 | - | ||
| 302 | /* Compute the Real divider depending on the MCLK output state, with a floating point */ |
367 | /* Compute the Real divider depending on the MCLK output state, with a floating point */ |
| 303 | if(hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE) |
368 | if (hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE) |
| 304 | { |
369 | { |
| 305 | /* MCLK output is enabled */ |
370 | /* MCLK output is enabled */ |
| 306 | if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B) |
371 | if (hi2s->Init.DataFormat != I2S_DATAFORMAT_16B) |
| 307 | { |
372 | { |
| 308 | tmp = (uint32_t)(((((i2sclk / (packetlength*4)) * 10) / hi2s->Init.AudioFreq)) + 5); |
373 | tmp = (uint32_t)(((((i2sclk / (packetlength * 4U)) * 10U) / hi2s->Init.AudioFreq)) + 5U); |
| 309 | } |
374 | } |
| 310 | else |
375 | else |
| 311 | { |
376 | { |
| 312 | tmp = (uint32_t)(((((i2sclk / (packetlength*8)) * 10) / hi2s->Init.AudioFreq)) + 5); |
377 | tmp = (uint32_t)(((((i2sclk / (packetlength * 8U)) * 10U) / hi2s->Init.AudioFreq)) + 5U); |
| 313 | } |
378 | } |
| 314 | } |
379 | } |
| 315 | else |
380 | else |
| 316 | { |
381 | { |
| 317 | /* MCLK output is disabled */ |
382 | /* MCLK output is disabled */ |
| 318 | tmp = (uint32_t)(((((i2sclk / packetlength) *10 ) / hi2s->Init.AudioFreq)) + 5); |
383 | tmp = (uint32_t)(((((i2sclk / packetlength) * 10U) / hi2s->Init.AudioFreq)) + 5U); |
| 319 | } |
384 | } |
| 320 | 385 | ||
| 321 | /* Remove the flatting point */ |
386 | /* Remove the flatting point */ |
| 322 | tmp = tmp / 10U; |
387 | tmp = tmp / 10U; |
| 323 | 388 | ||
| 324 | /* Check the parity of the divider */ |
389 | /* Check the parity of the divider */ |
| 325 | i2sodd = (uint16_t)(tmp & (uint16_t)1U); |
390 | i2sodd = (uint32_t)(tmp & (uint32_t)1U); |
| 326 | 391 | ||
| 327 | /* Compute the i2sdiv prescaler */ |
392 | /* Compute the i2sdiv prescaler */ |
| 328 | i2sdiv = (uint16_t)((tmp - i2sodd) / 2U); |
393 | i2sdiv = (uint32_t)((tmp - i2sodd) / 2U); |
| 329 | 394 | ||
| 330 | /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */ |
395 | /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */ |
| 331 | i2sodd = (uint32_t) (i2sodd << 8U); |
396 | i2sodd = (uint32_t)(i2sodd << 8U); |
| 332 | } |
397 | } |
| 333 | 398 | else |
|
| 334 | /* Test if the divider is 1 or 0 or greater than 0xFF */ |
- | |
| 335 | if((i2sdiv < 2U) || (i2sdiv > 0xFFU)) |
- | |
| 336 | { |
399 | { |
| 337 | /* Set the default values */ |
400 | /* Set the default values */ |
| 338 | i2sdiv = 2U; |
401 | i2sdiv = 2U; |
| 339 | i2sodd = 0U; |
402 | i2sodd = 0U; |
| - | 403 | } |
|
| 340 | 404 | ||
| - | 405 | /* Test if the divider is 1 or 0 or greater than 0xFF */ |
|
| - | 406 | if ((i2sdiv < 2U) || (i2sdiv > 0xFFU)) |
|
| - | 407 | { |
|
| 341 | /* Set the error code and execute error callback*/ |
408 | /* Set the error code and execute error callback*/ |
| 342 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER); |
409 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_PRESCALER); |
| 343 | HAL_I2S_ErrorCallback(hi2s); |
- | |
| 344 | return HAL_ERROR; |
410 | return HAL_ERROR; |
| 345 | } |
411 | } |
| 346 | 412 | ||
| - | 413 | /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/ |
|
| - | 414 | ||
| 347 | /* Write to SPIx I2SPR register the computed value */ |
415 | /* Write to SPIx I2SPR register the computed value */ |
| 348 | hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput)); |
416 | hi2s->Instance->I2SPR = (uint32_t)((uint32_t)i2sdiv | (uint32_t)(i2sodd | (uint32_t)hi2s->Init.MCLKOutput)); |
| 349 | 417 | ||
| - | 418 | /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */ |
|
| 350 | /* Configure the I2S with the I2S_InitStruct values */ |
419 | /* And configure the I2S with the I2S_InitStruct values */ |
| - | 420 | MODIFY_REG(hi2s->Instance->I2SCFGR, (SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | \ |
|
| - | 421 | SPI_I2SCFGR_CKPOL | SPI_I2SCFGR_I2SSTD | \ |
|
| - | 422 | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \ |
|
| - | 423 | SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD), \ |
|
| 351 | tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(hi2s->Init.Mode | \ |
424 | (SPI_I2SCFGR_I2SMOD | hi2s->Init.Mode | \ |
| 352 | (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \ |
425 | hi2s->Init.Standard | hi2s->Init.DataFormat | \ |
| 353 | (uint16_t)hi2s->Init.CPOL)))); |
426 | hi2s->Init.CPOL)); |
| - | 427 | ||
| - | 428 | #if defined(SPI_I2SCFGR_ASTRTEN) |
|
| - | 429 | if ((hi2s->Init.Standard == I2S_STANDARD_PCM_SHORT) || ((hi2s->Init.Standard == I2S_STANDARD_PCM_LONG))) |
|
| - | 430 | { |
|
| 354 | /* Write to SPIx I2SCFGR */ |
431 | /* Write to SPIx I2SCFGR */ |
| 355 | WRITE_REG(hi2s->Instance->I2SCFGR,tmpreg); |
432 | SET_BIT(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_ASTRTEN); |
| - | 433 | } |
|
| - | 434 | #endif /* SPI_I2SCFGR_ASTRTEN */ |
|
| - | 435 | ||
| 356 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
436 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 357 | hi2s->State = HAL_I2S_STATE_READY; |
437 | hi2s->State = HAL_I2S_STATE_READY; |
| 358 | 438 | ||
| 359 | return HAL_OK; |
439 | return HAL_OK; |
| 360 | } |
440 | } |
| 361 | 441 | ||
| 362 | /** |
442 | /** |
| 363 | * @brief DeInitializes the I2S peripheral |
443 | * @brief DeInitializes the I2S peripheral |
| 364 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
444 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 365 | * the configuration information for I2S module |
445 | * the configuration information for I2S module |
| 366 | * @retval HAL status |
446 | * @retval HAL status |
| 367 | */ |
447 | */ |
| 368 | HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s) |
448 | HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s) |
| 369 | { |
449 | { |
| 370 | /* Check the I2S handle allocation */ |
450 | /* Check the I2S handle allocation */ |
| 371 | if(hi2s == NULL) |
451 | if (hi2s == NULL) |
| 372 | { |
452 | { |
| 373 | return HAL_ERROR; |
453 | return HAL_ERROR; |
| 374 | } |
454 | } |
| 375 | 455 | ||
| - | 456 | /* Check the parameters */ |
|
| - | 457 | assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance)); |
|
| - | 458 | ||
| 376 | hi2s->State = HAL_I2S_STATE_BUSY; |
459 | hi2s->State = HAL_I2S_STATE_BUSY; |
| 377 | 460 | ||
| - | 461 | /* Disable the I2S Peripheral Clock */ |
|
| - | 462 | __HAL_I2S_DISABLE(hi2s); |
|
| - | 463 | ||
| - | 464 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 465 | if (hi2s->MspDeInitCallback == NULL) |
|
| - | 466 | { |
|
| - | 467 | hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */ |
|
| - | 468 | } |
|
| - | 469 | ||
| - | 470 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ |
|
| - | 471 | hi2s->MspDeInitCallback(hi2s); |
|
| - | 472 | #else |
|
| 378 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ |
473 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */ |
| 379 | HAL_I2S_MspDeInit(hi2s); |
474 | HAL_I2S_MspDeInit(hi2s); |
| - | 475 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 380 | 476 | ||
| 381 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
477 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 382 | hi2s->State = HAL_I2S_STATE_RESET; |
478 | hi2s->State = HAL_I2S_STATE_RESET; |
| 383 | 479 | ||
| 384 | /* Release Lock */ |
480 | /* Release Lock */ |
| Line 387... | Line 483... | ||
| 387 | return HAL_OK; |
483 | return HAL_OK; |
| 388 | } |
484 | } |
| 389 | 485 | ||
| 390 | /** |
486 | /** |
| 391 | * @brief I2S MSP Init |
487 | * @brief I2S MSP Init |
| 392 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
488 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 393 | * the configuration information for I2S module |
489 | * the configuration information for I2S module |
| 394 | * @retval None |
490 | * @retval None |
| 395 | */ |
491 | */ |
| 396 | __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s) |
492 | __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s) |
| 397 | { |
493 | { |
| 398 | /* Prevent unused argument(s) compilation warning */ |
494 | /* Prevent unused argument(s) compilation warning */ |
| 399 | UNUSED(hi2s); |
495 | UNUSED(hi2s); |
| - | 496 | ||
| 400 | /* NOTE : This function Should not be modified, when the callback is needed, |
497 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 401 | the HAL_I2S_MspInit could be implemented in the user file |
498 | the HAL_I2S_MspInit could be implemented in the user file |
| 402 | */ |
499 | */ |
| 403 | } |
500 | } |
| 404 | 501 | ||
| 405 | /** |
502 | /** |
| 406 | * @brief I2S MSP DeInit |
503 | * @brief I2S MSP DeInit |
| 407 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
504 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 408 | * the configuration information for I2S module |
505 | * the configuration information for I2S module |
| 409 | * @retval None |
506 | * @retval None |
| 410 | */ |
507 | */ |
| 411 | __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s) |
508 | __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s) |
| 412 | { |
509 | { |
| 413 | /* Prevent unused argument(s) compilation warning */ |
510 | /* Prevent unused argument(s) compilation warning */ |
| 414 | UNUSED(hi2s); |
511 | UNUSED(hi2s); |
| - | 512 | ||
| 415 | /* NOTE : This function Should not be modified, when the callback is needed, |
513 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 416 | the HAL_I2S_MspDeInit could be implemented in the user file |
514 | the HAL_I2S_MspDeInit could be implemented in the user file |
| 417 | */ |
515 | */ |
| 418 | } |
516 | } |
| - | 517 | ||
| - | 518 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 519 | /** |
|
| - | 520 | * @brief Register a User I2S Callback |
|
| - | 521 | * To be used instead of the weak predefined callback |
|
| - | 522 | * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains |
|
| - | 523 | * the configuration information for the specified I2S. |
|
| - | 524 | * @param CallbackID ID of the callback to be registered |
|
| - | 525 | * @param pCallback pointer to the Callback function |
|
| - | 526 | * @retval HAL status |
|
| - | 527 | */ |
|
| - | 528 | HAL_StatusTypeDef HAL_I2S_RegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID, |
|
| - | 529 | pI2S_CallbackTypeDef pCallback) |
|
| - | 530 | { |
|
| - | 531 | HAL_StatusTypeDef status = HAL_OK; |
|
| - | 532 | ||
| - | 533 | if (pCallback == NULL) |
|
| - | 534 | { |
|
| - | 535 | /* Update the error code */ |
|
| - | 536 | hi2s->ErrorCode |= HAL_I2S_ERROR_INVALID_CALLBACK; |
|
| - | 537 | ||
| - | 538 | return HAL_ERROR; |
|
| - | 539 | } |
|
| - | 540 | /* Process locked */ |
|
| - | 541 | __HAL_LOCK(hi2s); |
|
| - | 542 | ||
| - | 543 | if (HAL_I2S_STATE_READY == hi2s->State) |
|
| - | 544 | { |
|
| - | 545 | switch (CallbackID) |
|
| - | 546 | { |
|
| - | 547 | case HAL_I2S_TX_COMPLETE_CB_ID : |
|
| - | 548 | hi2s->TxCpltCallback = pCallback; |
|
| - | 549 | break; |
|
| - | 550 | ||
| - | 551 | case HAL_I2S_RX_COMPLETE_CB_ID : |
|
| - | 552 | hi2s->RxCpltCallback = pCallback; |
|
| - | 553 | break; |
|
| - | 554 | ||
| - | 555 | case HAL_I2S_TX_HALF_COMPLETE_CB_ID : |
|
| - | 556 | hi2s->TxHalfCpltCallback = pCallback; |
|
| - | 557 | break; |
|
| - | 558 | ||
| - | 559 | case HAL_I2S_RX_HALF_COMPLETE_CB_ID : |
|
| - | 560 | hi2s->RxHalfCpltCallback = pCallback; |
|
| - | 561 | break; |
|
| - | 562 | ||
| - | 563 | case HAL_I2S_ERROR_CB_ID : |
|
| - | 564 | hi2s->ErrorCallback = pCallback; |
|
| - | 565 | break; |
|
| - | 566 | ||
| - | 567 | case HAL_I2S_MSPINIT_CB_ID : |
|
| - | 568 | hi2s->MspInitCallback = pCallback; |
|
| - | 569 | break; |
|
| - | 570 | ||
| - | 571 | case HAL_I2S_MSPDEINIT_CB_ID : |
|
| - | 572 | hi2s->MspDeInitCallback = pCallback; |
|
| - | 573 | break; |
|
| - | 574 | ||
| - | 575 | default : |
|
| - | 576 | /* Update the error code */ |
|
| - | 577 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); |
|
| - | 578 | ||
| - | 579 | /* Return error status */ |
|
| - | 580 | status = HAL_ERROR; |
|
| - | 581 | break; |
|
| - | 582 | } |
|
| - | 583 | } |
|
| - | 584 | else if (HAL_I2S_STATE_RESET == hi2s->State) |
|
| - | 585 | { |
|
| - | 586 | switch (CallbackID) |
|
| - | 587 | { |
|
| - | 588 | case HAL_I2S_MSPINIT_CB_ID : |
|
| - | 589 | hi2s->MspInitCallback = pCallback; |
|
| - | 590 | break; |
|
| - | 591 | ||
| - | 592 | case HAL_I2S_MSPDEINIT_CB_ID : |
|
| - | 593 | hi2s->MspDeInitCallback = pCallback; |
|
| - | 594 | break; |
|
| - | 595 | ||
| - | 596 | default : |
|
| - | 597 | /* Update the error code */ |
|
| - | 598 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); |
|
| - | 599 | ||
| - | 600 | /* Return error status */ |
|
| - | 601 | status = HAL_ERROR; |
|
| - | 602 | break; |
|
| - | 603 | } |
|
| - | 604 | } |
|
| - | 605 | else |
|
| - | 606 | { |
|
| - | 607 | /* Update the error code */ |
|
| - | 608 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); |
|
| - | 609 | ||
| - | 610 | /* Return error status */ |
|
| - | 611 | status = HAL_ERROR; |
|
| - | 612 | } |
|
| - | 613 | ||
| - | 614 | /* Release Lock */ |
|
| - | 615 | __HAL_UNLOCK(hi2s); |
|
| - | 616 | return status; |
|
| - | 617 | } |
|
| - | 618 | ||
| - | 619 | /** |
|
| - | 620 | * @brief Unregister an I2S Callback |
|
| - | 621 | * I2S callback is redirected to the weak predefined callback |
|
| - | 622 | * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains |
|
| - | 623 | * the configuration information for the specified I2S. |
|
| - | 624 | * @param CallbackID ID of the callback to be unregistered |
|
| - | 625 | * @retval HAL status |
|
| - | 626 | */ |
|
| - | 627 | HAL_StatusTypeDef HAL_I2S_UnRegisterCallback(I2S_HandleTypeDef *hi2s, HAL_I2S_CallbackIDTypeDef CallbackID) |
|
| - | 628 | { |
|
| - | 629 | HAL_StatusTypeDef status = HAL_OK; |
|
| - | 630 | ||
| - | 631 | /* Process locked */ |
|
| - | 632 | __HAL_LOCK(hi2s); |
|
| - | 633 | ||
| - | 634 | if (HAL_I2S_STATE_READY == hi2s->State) |
|
| - | 635 | { |
|
| - | 636 | switch (CallbackID) |
|
| - | 637 | { |
|
| - | 638 | case HAL_I2S_TX_COMPLETE_CB_ID : |
|
| - | 639 | hi2s->TxCpltCallback = HAL_I2S_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
|
| - | 640 | break; |
|
| - | 641 | ||
| - | 642 | case HAL_I2S_RX_COMPLETE_CB_ID : |
|
| - | 643 | hi2s->RxCpltCallback = HAL_I2S_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
|
| - | 644 | break; |
|
| - | 645 | ||
| - | 646 | case HAL_I2S_TX_HALF_COMPLETE_CB_ID : |
|
| - | 647 | hi2s->TxHalfCpltCallback = HAL_I2S_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ |
|
| - | 648 | break; |
|
| - | 649 | ||
| - | 650 | case HAL_I2S_RX_HALF_COMPLETE_CB_ID : |
|
| - | 651 | hi2s->RxHalfCpltCallback = HAL_I2S_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ |
|
| - | 652 | break; |
|
| - | 653 | ||
| - | 654 | case HAL_I2S_ERROR_CB_ID : |
|
| - | 655 | hi2s->ErrorCallback = HAL_I2S_ErrorCallback; /* Legacy weak ErrorCallback */ |
|
| - | 656 | break; |
|
| - | 657 | ||
| - | 658 | case HAL_I2S_MSPINIT_CB_ID : |
|
| - | 659 | hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */ |
|
| - | 660 | break; |
|
| - | 661 | ||
| - | 662 | case HAL_I2S_MSPDEINIT_CB_ID : |
|
| - | 663 | hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */ |
|
| - | 664 | break; |
|
| - | 665 | ||
| - | 666 | default : |
|
| - | 667 | /* Update the error code */ |
|
| - | 668 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); |
|
| - | 669 | ||
| - | 670 | /* Return error status */ |
|
| - | 671 | status = HAL_ERROR; |
|
| - | 672 | break; |
|
| - | 673 | } |
|
| - | 674 | } |
|
| - | 675 | else if (HAL_I2S_STATE_RESET == hi2s->State) |
|
| - | 676 | { |
|
| - | 677 | switch (CallbackID) |
|
| - | 678 | { |
|
| - | 679 | case HAL_I2S_MSPINIT_CB_ID : |
|
| - | 680 | hi2s->MspInitCallback = HAL_I2S_MspInit; /* Legacy weak MspInit */ |
|
| - | 681 | break; |
|
| - | 682 | ||
| - | 683 | case HAL_I2S_MSPDEINIT_CB_ID : |
|
| - | 684 | hi2s->MspDeInitCallback = HAL_I2S_MspDeInit; /* Legacy weak MspDeInit */ |
|
| - | 685 | break; |
|
| - | 686 | ||
| - | 687 | default : |
|
| - | 688 | /* Update the error code */ |
|
| - | 689 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); |
|
| - | 690 | ||
| - | 691 | /* Return error status */ |
|
| - | 692 | status = HAL_ERROR; |
|
| - | 693 | break; |
|
| - | 694 | } |
|
| - | 695 | } |
|
| - | 696 | else |
|
| - | 697 | { |
|
| - | 698 | /* Update the error code */ |
|
| - | 699 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_INVALID_CALLBACK); |
|
| - | 700 | ||
| - | 701 | /* Return error status */ |
|
| - | 702 | status = HAL_ERROR; |
|
| - | 703 | } |
|
| - | 704 | ||
| - | 705 | /* Release Lock */ |
|
| - | 706 | __HAL_UNLOCK(hi2s); |
|
| - | 707 | return status; |
|
| - | 708 | } |
|
| - | 709 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 419 | /** |
710 | /** |
| 420 | * @} |
711 | * @} |
| 421 | */ |
712 | */ |
| 422 | 713 | ||
| 423 | /** @defgroup I2S_Exported_Functions_Group2 IO operation functions |
714 | /** @defgroup I2S_Exported_Functions_Group2 IO operation functions |
| 424 | * @brief Data transfers functions |
715 | * @brief Data transfers functions |
| 425 | * |
716 | * |
| 426 | @verbatim |
717 | @verbatim |
| 427 | =============================================================================== |
718 | =============================================================================== |
| 428 | ##### IO operation functions ##### |
719 | ##### IO operation functions ##### |
| 429 | =============================================================================== |
720 | =============================================================================== |
| Line 461... | Line 752... | ||
| 461 | @endverbatim |
752 | @endverbatim |
| 462 | * @{ |
753 | * @{ |
| 463 | */ |
754 | */ |
| 464 | 755 | ||
| 465 | /** |
756 | /** |
| 466 | * @brief Transmit an amount of data in blocking mode |
757 | * @brief Transmit an amount of data in blocking mode |
| 467 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
758 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 468 | * the configuration information for I2S module |
759 | * the configuration information for I2S module |
| 469 | * @param pData: a 16-bit pointer to data buffer. |
760 | * @param pData a 16-bit pointer to data buffer. |
| 470 | * @param Size: number of data sample to be sent: |
761 | * @param Size number of data sample to be sent: |
| 471 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
762 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
| 472 | * configuration phase, the Size parameter means the number of 16-bit data length |
763 | * configuration phase, the Size parameter means the number of 16-bit data length |
| 473 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
764 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
| 474 | * the Size parameter means the number of 16-bit data length. |
765 | * the Size parameter means the number of 16-bit data length. |
| 475 | * @param Timeout: Timeout duration |
766 | * @param Timeout Timeout duration |
| 476 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
767 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
| 477 | * between Master and Slave(example: audio streaming). |
768 | * between Master and Slave(example: audio streaming). |
| 478 | * @retval HAL status |
769 | * @retval HAL status |
| 479 | */ |
770 | */ |
| 480 | HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout) |
771 | HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout) |
| 481 | { |
772 | { |
| 482 | uint32_t tmp1 = 0U; |
773 | uint32_t tmpreg_cfgr; |
| 483 | 774 | ||
| 484 | if((pData == NULL ) || (Size == 0U)) |
775 | if ((pData == NULL) || (Size == 0U)) |
| 485 | { |
776 | { |
| 486 | return HAL_ERROR; |
777 | return HAL_ERROR; |
| 487 | } |
778 | } |
| 488 | 779 | ||
| 489 | if(hi2s->State == HAL_I2S_STATE_READY) |
- | |
| 490 | { |
- | |
| 491 | tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
- | |
| 492 | - | ||
| 493 | if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B)) |
- | |
| 494 | { |
- | |
| 495 | hi2s->TxXferSize = (Size << 1U); |
- | |
| 496 | hi2s->TxXferCount = (Size << 1U); |
- | |
| 497 | } |
- | |
| 498 | else |
- | |
| 499 | { |
- | |
| 500 | hi2s->TxXferSize = Size; |
780 | /* Process Locked */ |
| 501 | hi2s->TxXferCount = Size; |
781 | __HAL_LOCK(hi2s); |
| 502 | } |
- | |
| 503 | 782 | ||
| 504 | /* Process Locked */ |
783 | if (hi2s->State != HAL_I2S_STATE_READY) |
| - | 784 | { |
|
| 505 | __HAL_LOCK(hi2s); |
785 | __HAL_UNLOCK(hi2s); |
| - | 786 | return HAL_BUSY; |
|
| - | 787 | } |
|
| 506 | 788 | ||
| - | 789 | /* Set state and reset error code */ |
|
| - | 790 | hi2s->State = HAL_I2S_STATE_BUSY_TX; |
|
| 507 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
791 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 508 | hi2s->State = HAL_I2S_STATE_BUSY_TX; |
792 | hi2s->pTxBuffPtr = pData; |
| 509 | 793 | ||
| 510 | /* Check if the I2S is already enabled */ |
- | |
| 511 | if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
794 | tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
| 512 | { |
- | |
| 513 | /* Enable I2S peripheral */ |
- | |
| 514 | __HAL_I2S_ENABLE(hi2s); |
- | |
| 515 | } |
- | |
| 516 | 795 | ||
| - | 796 | if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) |
|
| - | 797 | { |
|
| - | 798 | hi2s->TxXferSize = (Size << 1U); |
|
| 517 | while(hi2s->TxXferCount > 0U) |
799 | hi2s->TxXferCount = (Size << 1U); |
| - | 800 | } |
|
| - | 801 | else |
|
| 518 | { |
802 | { |
| 519 | hi2s->Instance->DR = (*pData++); |
803 | hi2s->TxXferSize = Size; |
| 520 | hi2s->TxXferCount--; |
804 | hi2s->TxXferCount = Size; |
| - | 805 | } |
|
| 521 | 806 | ||
| 522 | /* Wait until TXE flag is set */ |
- | |
| 523 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK) |
- | |
| 524 | { |
- | |
| 525 | /* Set the error code and execute error callback*/ |
- | |
| 526 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
807 | tmpreg_cfgr = hi2s->Instance->I2SCFGR; |
| 527 | HAL_I2S_ErrorCallback(hi2s); |
- | |
| 528 | return HAL_TIMEOUT; |
- | |
| 529 | } |
- | |
| 530 | 808 | ||
| 531 | /* Check if an underrun occurs */ |
809 | /* Check if the I2S is already enabled */ |
| 532 | if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) |
810 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
| 533 | { |
811 | { |
| 534 | /* Clear underrun flag */ |
812 | /* Enable I2S peripheral */ |
| 535 | __HAL_I2S_CLEAR_UDRFLAG(hi2s); |
813 | __HAL_I2S_ENABLE(hi2s); |
| 536 | /* Set the I2S State ready */ |
814 | } |
| 537 | hi2s->State = HAL_I2S_STATE_READY; |
- | |
| 538 | 815 | ||
| - | 816 | /* Wait until TXE flag is set */ |
|
| - | 817 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK) |
|
| - | 818 | { |
|
| 539 | /* Process Unlocked */ |
819 | /* Set the error code */ |
| - | 820 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
|
| - | 821 | hi2s->State = HAL_I2S_STATE_READY; |
|
| 540 | __HAL_UNLOCK(hi2s); |
822 | __HAL_UNLOCK(hi2s); |
| - | 823 | return HAL_ERROR; |
|
| - | 824 | } |
|
| 541 | 825 | ||
| 542 | /* Set the error code and execute error callback*/ |
826 | while (hi2s->TxXferCount > 0U) |
| - | 827 | { |
|
| 543 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR); |
828 | hi2s->Instance->DR = (*hi2s->pTxBuffPtr); |
| - | 829 | hi2s->pTxBuffPtr++; |
|
| 544 | HAL_I2S_ErrorCallback(hi2s); |
830 | hi2s->TxXferCount--; |
| 545 | 831 | ||
| 546 | return HAL_ERROR; |
832 | /* Wait until TXE flag is set */ |
| - | 833 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK) |
|
| 547 | } |
834 | { |
| - | 835 | /* Set the error code */ |
|
| - | 836 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
|
| - | 837 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 838 | __HAL_UNLOCK(hi2s); |
|
| - | 839 | return HAL_ERROR; |
|
| 548 | } |
840 | } |
| 549 | hi2s->State = HAL_I2S_STATE_READY; |
- | |
| 550 | 841 | ||
| - | 842 | /* Check if an underrun occurs */ |
|
| - | 843 | if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) |
|
| - | 844 | { |
|
| 551 | /* Process Unlocked */ |
845 | /* Clear underrun flag */ |
| 552 | __HAL_UNLOCK(hi2s); |
846 | __HAL_I2S_CLEAR_UDRFLAG(hi2s); |
| 553 | 847 | ||
| 554 | return HAL_OK; |
848 | /* Set the error code */ |
| - | 849 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR); |
|
| - | 850 | } |
|
| 555 | } |
851 | } |
| 556 | else |
852 | |
| - | 853 | /* Check if Slave mode is selected */ |
|
| - | 854 | if (((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX) |
|
| - | 855 | || ((tmpreg_cfgr & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_RX)) |
|
| 557 | { |
856 | { |
| - | 857 | /* Wait until Busy flag is reset */ |
|
| - | 858 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK) |
|
| - | 859 | { |
|
| - | 860 | /* Set the error code */ |
|
| - | 861 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
|
| - | 862 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 863 | __HAL_UNLOCK(hi2s); |
|
| 558 | return HAL_BUSY; |
864 | return HAL_ERROR; |
| - | 865 | } |
|
| 559 | } |
866 | } |
| - | 867 | ||
| - | 868 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 869 | __HAL_UNLOCK(hi2s); |
|
| - | 870 | return HAL_OK; |
|
| 560 | } |
871 | } |
| 561 | 872 | ||
| 562 | /** |
873 | /** |
| 563 | * @brief Receive an amount of data in blocking mode |
874 | * @brief Receive an amount of data in blocking mode |
| 564 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
875 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 565 | * the configuration information for I2S module |
876 | * the configuration information for I2S module |
| 566 | * @param pData: a 16-bit pointer to data buffer |
877 | * @param pData a 16-bit pointer to data buffer. |
| 567 | * @param Size: number of data sample to be sent: |
878 | * @param Size number of data sample to be sent: |
| 568 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
879 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
| 569 | * configuration phase, the Size parameter means the number of 16-bit data length |
880 | * configuration phase, the Size parameter means the number of 16-bit data length |
| 570 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
881 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
| 571 | * the Size parameter means the number of 16-bit data length. |
882 | * the Size parameter means the number of 16-bit data length. |
| 572 | * @param Timeout: Timeout duration |
883 | * @param Timeout Timeout duration |
| 573 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
884 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
| 574 | * between Master and Slave(example: audio streaming) |
885 | * between Master and Slave(example: audio streaming). |
| 575 | * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate |
886 | * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate |
| 576 | * in continuous way and as the I2S is not disabled at the end of the I2S transaction |
887 | * in continuous way and as the I2S is not disabled at the end of the I2S transaction. |
| 577 | * @retval HAL status |
888 | * @retval HAL status |
| 578 | */ |
889 | */ |
| 579 | HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout) |
890 | HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout) |
| 580 | { |
891 | { |
| 581 | uint32_t tmp1 = 0U; |
892 | uint32_t tmpreg_cfgr; |
| 582 | 893 | ||
| 583 | if((pData == NULL ) || (Size == 0U)) |
894 | if ((pData == NULL) || (Size == 0U)) |
| 584 | { |
895 | { |
| 585 | return HAL_ERROR; |
896 | return HAL_ERROR; |
| 586 | } |
897 | } |
| 587 | 898 | ||
| 588 | if(hi2s->State == HAL_I2S_STATE_READY) |
- | |
| 589 | { |
- | |
| 590 | tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
- | |
| 591 | if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B)) |
- | |
| 592 | { |
- | |
| 593 | hi2s->RxXferSize = (Size << 1U); |
- | |
| 594 | hi2s->RxXferCount = (Size << 1U); |
- | |
| 595 | } |
- | |
| 596 | else |
- | |
| 597 | { |
- | |
| 598 | hi2s->RxXferSize = Size; |
- | |
| 599 | hi2s->RxXferCount = Size; |
- | |
| 600 | } |
- | |
| 601 | /* Process Locked */ |
899 | /* Process Locked */ |
| 602 | __HAL_LOCK(hi2s); |
900 | __HAL_LOCK(hi2s); |
| 603 | - | ||
| 604 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
- | |
| 605 | hi2s->State = HAL_I2S_STATE_BUSY_RX; |
- | |
| 606 | - | ||
| 607 | /* Check if the I2S is already enabled */ |
- | |
| 608 | if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
- | |
| 609 | { |
- | |
| 610 | /* Enable I2S peripheral */ |
- | |
| 611 | __HAL_I2S_ENABLE(hi2s); |
- | |
| 612 | } |
- | |
| 613 | - | ||
| 614 | /* Check if Master Receiver mode is selected */ |
- | |
| 615 | if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX) |
- | |
| 616 | { |
- | |
| 617 | /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read |
- | |
| 618 | access to the SPI_SR register. */ |
- | |
| 619 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
- | |
| 620 | } |
- | |
| 621 | 901 | ||
| 622 | /* Receive data */ |
- | |
| 623 | while(hi2s->RxXferCount > 0U) |
902 | if (hi2s->State != HAL_I2S_STATE_READY) |
| 624 | { |
903 | { |
| 625 | /* Wait until RXNE flag is set */ |
- | |
| 626 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK) |
- | |
| 627 | { |
- | |
| 628 | /* Set the error code and execute error callback*/ |
- | |
| 629 | SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_TIMEOUT); |
- | |
| 630 | HAL_I2S_ErrorCallback(hi2s); |
904 | __HAL_UNLOCK(hi2s); |
| 631 | return HAL_TIMEOUT; |
905 | return HAL_BUSY; |
| 632 | } |
906 | } |
| 633 | 907 | ||
| 634 | /* Check if an overrun occurs */ |
908 | /* Set state and reset error code */ |
| 635 | if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET) |
909 | hi2s->State = HAL_I2S_STATE_BUSY_RX; |
| 636 | { |
- | |
| 637 | /* Clear overrun flag */ |
910 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 638 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
911 | hi2s->pRxBuffPtr = pData; |
| 639 | 912 | ||
| 640 | /* Set the I2S State ready */ |
- | |
| 641 | hi2s->State = HAL_I2S_STATE_READY; |
913 | tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
| 642 | 914 | ||
| - | 915 | if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) |
|
| - | 916 | { |
|
| 643 | /* Process Unlocked */ |
917 | hi2s->RxXferSize = (Size << 1U); |
| - | 918 | hi2s->RxXferCount = (Size << 1U); |
|
| - | 919 | } |
|
| - | 920 | else |
|
| - | 921 | { |
|
| 644 | __HAL_UNLOCK(hi2s); |
922 | hi2s->RxXferSize = Size; |
| - | 923 | hi2s->RxXferCount = Size; |
|
| - | 924 | } |
|
| 645 | 925 | ||
| 646 | /* Set the error code and execute error callback*/ |
926 | /* Check if the I2S is already enabled */ |
| 647 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR); |
927 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
| - | 928 | { |
|
| - | 929 | /* Enable I2S peripheral */ |
|
| 648 | HAL_I2S_ErrorCallback(hi2s); |
930 | __HAL_I2S_ENABLE(hi2s); |
| - | 931 | } |
|
| 649 | 932 | ||
| - | 933 | /* Check if Master Receiver mode is selected */ |
|
| - | 934 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX) |
|
| - | 935 | { |
|
| - | 936 | /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read |
|
| - | 937 | access to the SPI_SR register. */ |
|
| 650 | return HAL_ERROR; |
938 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
| 651 | } |
939 | } |
| 652 | 940 | ||
| - | 941 | /* Receive data */ |
|
| - | 942 | while (hi2s->RxXferCount > 0U) |
|
| - | 943 | { |
|
| - | 944 | /* Wait until RXNE flag is set */ |
|
| - | 945 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK) |
|
| - | 946 | { |
|
| - | 947 | /* Set the error code */ |
|
| - | 948 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
|
| 653 | (*pData++) = hi2s->Instance->DR; |
949 | hi2s->State = HAL_I2S_STATE_READY; |
| 654 | hi2s->RxXferCount--; |
950 | __HAL_UNLOCK(hi2s); |
| - | 951 | return HAL_ERROR; |
|
| 655 | } |
952 | } |
| 656 | 953 | ||
| 657 | hi2s->State = HAL_I2S_STATE_READY; |
954 | (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR; |
| - | 955 | hi2s->pRxBuffPtr++; |
|
| - | 956 | hi2s->RxXferCount--; |
|
| 658 | 957 | ||
| - | 958 | /* Check if an overrun occurs */ |
|
| - | 959 | if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET) |
|
| - | 960 | { |
|
| 659 | /* Process Unlocked */ |
961 | /* Clear overrun flag */ |
| 660 | __HAL_UNLOCK(hi2s); |
962 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
| 661 | 963 | ||
| 662 | return HAL_OK; |
964 | /* Set the error code */ |
| - | 965 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR); |
|
| 663 | } |
966 | } |
| 664 | else |
- | |
| 665 | { |
- | |
| 666 | return HAL_BUSY; |
- | |
| 667 | } |
967 | } |
| - | 968 | ||
| - | 969 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 970 | __HAL_UNLOCK(hi2s); |
|
| - | 971 | return HAL_OK; |
|
| 668 | } |
972 | } |
| 669 | 973 | ||
| 670 | /** |
974 | /** |
| 671 | * @brief Transmit an amount of data in non-blocking mode with Interrupt |
975 | * @brief Transmit an amount of data in non-blocking mode with Interrupt |
| 672 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
976 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 673 | * the configuration information for I2S module |
977 | * the configuration information for I2S module |
| 674 | * @param pData: a 16-bit pointer to data buffer. |
978 | * @param pData a 16-bit pointer to data buffer. |
| 675 | * @param Size: number of data sample to be sent: |
979 | * @param Size number of data sample to be sent: |
| 676 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
980 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
| 677 | * configuration phase, the Size parameter means the number of 16-bit data length |
981 | * configuration phase, the Size parameter means the number of 16-bit data length |
| 678 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
982 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
| 679 | * the Size parameter means the number of 16-bit data length. |
983 | * the Size parameter means the number of 16-bit data length. |
| 680 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
984 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
| 681 | * between Master and Slave(example: audio streaming). |
985 | * between Master and Slave(example: audio streaming). |
| 682 | * @retval HAL status |
986 | * @retval HAL status |
| 683 | */ |
987 | */ |
| 684 | HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
988 | HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
| 685 | { |
989 | { |
| 686 | uint32_t tmp1 = 0U; |
990 | uint32_t tmpreg_cfgr; |
| 687 | 991 | ||
| 688 | if(hi2s->State == HAL_I2S_STATE_READY) |
992 | if ((pData == NULL) || (Size == 0U)) |
| 689 | { |
993 | { |
| 690 | if((pData == NULL) || (Size == 0U)) |
- | |
| 691 | { |
- | |
| 692 | return HAL_ERROR; |
994 | return HAL_ERROR; |
| 693 | } |
- | |
| 694 | - | ||
| 695 | hi2s->pTxBuffPtr = pData; |
- | |
| 696 | tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
- | |
| 697 | if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B)) |
- | |
| 698 | { |
- | |
| 699 | hi2s->TxXferSize = (Size << 1U); |
- | |
| 700 | hi2s->TxXferCount = (Size << 1U); |
- | |
| 701 | } |
- | |
| 702 | else |
- | |
| 703 | { |
- | |
| 704 | hi2s->TxXferSize = Size; |
- | |
| 705 | hi2s->TxXferCount = Size; |
- | |
| 706 | } |
995 | } |
| 707 | - | ||
| 708 | /* Process Locked */ |
- | |
| 709 | __HAL_LOCK(hi2s); |
- | |
| 710 | 996 | ||
| 711 | hi2s->State = HAL_I2S_STATE_BUSY_TX; |
997 | /* Process Locked */ |
| 712 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
998 | __HAL_LOCK(hi2s); |
| 713 | 999 | ||
| 714 | /* Enable TXE and ERR interrupt */ |
1000 | if (hi2s->State != HAL_I2S_STATE_READY) |
| - | 1001 | { |
|
| 715 | __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); |
1002 | __HAL_UNLOCK(hi2s); |
| - | 1003 | return HAL_BUSY; |
|
| - | 1004 | } |
|
| 716 | 1005 | ||
| 717 | /* Check if the I2S is already enabled */ |
1006 | /* Set state and reset error code */ |
| 718 | if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
1007 | hi2s->State = HAL_I2S_STATE_BUSY_TX; |
| 719 | { |
- | |
| 720 | /* Enable I2S peripheral */ |
1008 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 721 | __HAL_I2S_ENABLE(hi2s); |
1009 | hi2s->pTxBuffPtr = pData; |
| 722 | } |
- | |
| 723 | 1010 | ||
| 724 | /* Process Unlocked */ |
- | |
| 725 | __HAL_UNLOCK(hi2s); |
1011 | tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
| 726 | 1012 | ||
| - | 1013 | if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) |
|
| - | 1014 | { |
|
| 727 | return HAL_OK; |
1015 | hi2s->TxXferSize = (Size << 1U); |
| - | 1016 | hi2s->TxXferCount = (Size << 1U); |
|
| 728 | } |
1017 | } |
| 729 | else |
1018 | else |
| 730 | { |
1019 | { |
| - | 1020 | hi2s->TxXferSize = Size; |
|
| 731 | return HAL_BUSY; |
1021 | hi2s->TxXferCount = Size; |
| 732 | } |
1022 | } |
| - | 1023 | ||
| - | 1024 | /* Enable TXE and ERR interrupt */ |
|
| - | 1025 | __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); |
|
| - | 1026 | ||
| - | 1027 | /* Check if the I2S is already enabled */ |
|
| - | 1028 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
|
| - | 1029 | { |
|
| - | 1030 | /* Enable I2S peripheral */ |
|
| - | 1031 | __HAL_I2S_ENABLE(hi2s); |
|
| - | 1032 | } |
|
| - | 1033 | ||
| - | 1034 | __HAL_UNLOCK(hi2s); |
|
| - | 1035 | return HAL_OK; |
|
| 733 | } |
1036 | } |
| 734 | 1037 | ||
| 735 | /** |
1038 | /** |
| 736 | * @brief Receive an amount of data in non-blocking mode with Interrupt |
1039 | * @brief Receive an amount of data in non-blocking mode with Interrupt |
| 737 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1040 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 738 | * the configuration information for I2S module |
1041 | * the configuration information for I2S module |
| 739 | * @param pData: a 16-bit pointer to the Receive data buffer. |
1042 | * @param pData a 16-bit pointer to the Receive data buffer. |
| 740 | * @param Size: number of data sample to be sent: |
1043 | * @param Size number of data sample to be sent: |
| 741 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
1044 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
| 742 | * configuration phase, the Size parameter means the number of 16-bit data length |
1045 | * configuration phase, the Size parameter means the number of 16-bit data length |
| 743 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
1046 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
| 744 | * the Size parameter means the number of 16-bit data length. |
1047 | * the Size parameter means the number of 16-bit data length. |
| 745 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
1048 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
| 746 | * between Master and Slave(example: audio streaming). |
1049 | * between Master and Slave(example: audio streaming). |
| 747 | * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation |
1050 | * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronization |
| 748 | * between Master and Slave otherwise the I2S interrupt should be optimized. |
1051 | * between Master and Slave otherwise the I2S interrupt should be optimized. |
| 749 | * @retval HAL status |
1052 | * @retval HAL status |
| 750 | */ |
1053 | */ |
| 751 | HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
1054 | HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
| 752 | { |
1055 | { |
| 753 | uint32_t tmp1 = 0U; |
1056 | uint32_t tmpreg_cfgr; |
| 754 | 1057 | ||
| 755 | if(hi2s->State == HAL_I2S_STATE_READY) |
1058 | if ((pData == NULL) || (Size == 0U)) |
| 756 | { |
1059 | { |
| 757 | if((pData == NULL) || (Size == 0U)) |
- | |
| 758 | { |
- | |
| 759 | return HAL_ERROR; |
1060 | return HAL_ERROR; |
| 760 | } |
- | |
| 761 | - | ||
| 762 | hi2s->pRxBuffPtr = pData; |
- | |
| 763 | tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
- | |
| 764 | if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B)) |
- | |
| 765 | { |
- | |
| 766 | hi2s->RxXferSize = (Size << 1U); |
- | |
| 767 | hi2s->RxXferCount = (Size << 1U); |
- | |
| 768 | } |
- | |
| 769 | else |
- | |
| 770 | { |
- | |
| 771 | hi2s->RxXferSize = Size; |
- | |
| 772 | hi2s->RxXferCount = Size; |
- | |
| 773 | } |
1061 | } |
| 774 | /* Process Locked */ |
- | |
| 775 | __HAL_LOCK(hi2s); |
- | |
| 776 | 1062 | ||
| 777 | hi2s->State = HAL_I2S_STATE_BUSY_RX; |
1063 | /* Process Locked */ |
| 778 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
1064 | __HAL_LOCK(hi2s); |
| 779 | 1065 | ||
| 780 | /* Enable TXE and ERR interrupt */ |
1066 | if (hi2s->State != HAL_I2S_STATE_READY) |
| - | 1067 | { |
|
| 781 | __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); |
1068 | __HAL_UNLOCK(hi2s); |
| - | 1069 | return HAL_BUSY; |
|
| - | 1070 | } |
|
| 782 | 1071 | ||
| 783 | /* Check if the I2S is already enabled */ |
1072 | /* Set state and reset error code */ |
| 784 | if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
1073 | hi2s->State = HAL_I2S_STATE_BUSY_RX; |
| 785 | { |
- | |
| 786 | /* Enable I2S peripheral */ |
1074 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 787 | __HAL_I2S_ENABLE(hi2s); |
1075 | hi2s->pRxBuffPtr = pData; |
| 788 | } |
- | |
| 789 | 1076 | ||
| 790 | /* Process Unlocked */ |
- | |
| 791 | __HAL_UNLOCK(hi2s); |
1077 | tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
| 792 | 1078 | ||
| - | 1079 | if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) |
|
| - | 1080 | { |
|
| 793 | return HAL_OK; |
1081 | hi2s->RxXferSize = (Size << 1U); |
| - | 1082 | hi2s->RxXferCount = (Size << 1U); |
|
| 794 | } |
1083 | } |
| 795 | - | ||
| 796 | else |
1084 | else |
| 797 | { |
1085 | { |
| - | 1086 | hi2s->RxXferSize = Size; |
|
| 798 | return HAL_BUSY; |
1087 | hi2s->RxXferCount = Size; |
| 799 | } |
1088 | } |
| - | 1089 | ||
| - | 1090 | /* Enable RXNE and ERR interrupt */ |
|
| - | 1091 | __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); |
|
| - | 1092 | ||
| - | 1093 | /* Check if the I2S is already enabled */ |
|
| - | 1094 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
|
| - | 1095 | { |
|
| - | 1096 | /* Enable I2S peripheral */ |
|
| - | 1097 | __HAL_I2S_ENABLE(hi2s); |
|
| - | 1098 | } |
|
| - | 1099 | ||
| - | 1100 | __HAL_UNLOCK(hi2s); |
|
| - | 1101 | return HAL_OK; |
|
| 800 | } |
1102 | } |
| 801 | 1103 | ||
| 802 | /** |
1104 | /** |
| 803 | * @brief Transmit an amount of data in non-blocking mode with DMA |
1105 | * @brief Transmit an amount of data in non-blocking mode with DMA |
| 804 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1106 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 805 | * the configuration information for I2S module |
1107 | * the configuration information for I2S module |
| 806 | * @param pData: a 16-bit pointer to the Transmit data buffer. |
1108 | * @param pData a 16-bit pointer to the Transmit data buffer. |
| 807 | * @param Size: number of data sample to be sent: |
1109 | * @param Size number of data sample to be sent: |
| 808 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
1110 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
| 809 | * configuration phase, the Size parameter means the number of 16-bit data length |
1111 | * configuration phase, the Size parameter means the number of 16-bit data length |
| 810 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
1112 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
| 811 | * the Size parameter means the number of 16-bit data length. |
1113 | * the Size parameter means the number of 16-bit data length. |
| 812 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
1114 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
| 813 | * between Master and Slave(example: audio streaming). |
1115 | * between Master and Slave(example: audio streaming). |
| 814 | * @retval HAL status |
1116 | * @retval HAL status |
| 815 | */ |
1117 | */ |
| 816 | HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
1118 | HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
| 817 | { |
1119 | { |
| 818 | uint32_t *tmp = NULL; |
- | |
| 819 | uint32_t tmp1 = 0U; |
1120 | uint32_t tmpreg_cfgr; |
| 820 | 1121 | ||
| 821 | if((pData == NULL) || (Size == 0U)) |
1122 | if ((pData == NULL) || (Size == 0U)) |
| 822 | { |
1123 | { |
| 823 | return HAL_ERROR; |
1124 | return HAL_ERROR; |
| 824 | } |
1125 | } |
| 825 | 1126 | ||
| 826 | if(hi2s->State == HAL_I2S_STATE_READY) |
- | |
| 827 | { |
- | |
| 828 | hi2s->pTxBuffPtr = pData; |
- | |
| 829 | tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
- | |
| 830 | if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B)) |
- | |
| 831 | { |
- | |
| 832 | hi2s->TxXferSize = (Size << 1U); |
- | |
| 833 | hi2s->TxXferCount = (Size << 1U); |
- | |
| 834 | } |
- | |
| 835 | else |
- | |
| 836 | { |
- | |
| 837 | hi2s->TxXferSize = Size; |
1127 | /* Process Locked */ |
| 838 | hi2s->TxXferCount = Size; |
1128 | __HAL_LOCK(hi2s); |
| 839 | } |
- | |
| 840 | 1129 | ||
| 841 | /* Process Locked */ |
1130 | if (hi2s->State != HAL_I2S_STATE_READY) |
| - | 1131 | { |
|
| 842 | __HAL_LOCK(hi2s); |
1132 | __HAL_UNLOCK(hi2s); |
| - | 1133 | return HAL_BUSY; |
|
| - | 1134 | } |
|
| 843 | 1135 | ||
| - | 1136 | /* Set state and reset error code */ |
|
| - | 1137 | hi2s->State = HAL_I2S_STATE_BUSY_TX; |
|
| 844 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
1138 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| 845 | hi2s->State = HAL_I2S_STATE_BUSY_TX; |
1139 | hi2s->pTxBuffPtr = pData; |
| 846 | 1140 | ||
| 847 | /* Set the I2S Tx DMA Half transfer complete callback */ |
- | |
| 848 | hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt; |
1141 | tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
| 849 | 1142 | ||
| - | 1143 | if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) |
|
| - | 1144 | { |
|
| 850 | /* Set the I2S Tx DMA transfer complete callback */ |
1145 | hi2s->TxXferSize = (Size << 1U); |
| 851 | hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt; |
1146 | hi2s->TxXferCount = (Size << 1U); |
| - | 1147 | } |
|
| - | 1148 | else |
|
| - | 1149 | { |
|
| - | 1150 | hi2s->TxXferSize = Size; |
|
| - | 1151 | hi2s->TxXferCount = Size; |
|
| - | 1152 | } |
|
| 852 | 1153 | ||
| 853 | /* Set the DMA error callback */ |
1154 | /* Set the I2S Tx DMA Half transfer complete callback */ |
| 854 | hi2s->hdmatx->XferErrorCallback = I2S_DMAError; |
1155 | hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt; |
| 855 | 1156 | ||
| 856 | /* Enable the Tx DMA Stream */ |
1157 | /* Set the I2S Tx DMA transfer complete callback */ |
| 857 | tmp = (uint32_t*)&pData; |
1158 | hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt; |
| 858 | HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize); |
- | |
| 859 | 1159 | ||
| 860 | /* Check if the I2S is already enabled */ |
1160 | /* Set the DMA error callback */ |
| 861 | if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
1161 | hi2s->hdmatx->XferErrorCallback = I2S_DMAError; |
| 862 | { |
- | |
| 863 | /* Enable I2S peripheral */ |
- | |
| 864 | __HAL_I2S_ENABLE(hi2s); |
- | |
| 865 | } |
- | |
| 866 | 1162 | ||
| 867 | /* Check if the I2S Tx request is already enabled */ |
1163 | /* Enable the Tx DMA Stream/Channel */ |
| - | 1164 | if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmatx, |
|
| - | 1165 | (uint32_t)hi2s->pTxBuffPtr, |
|
| 868 | if((hi2s->Instance->CR2 & SPI_CR2_TXDMAEN) != SPI_CR2_TXDMAEN) |
1166 | (uint32_t)&hi2s->Instance->DR, |
| - | 1167 | hi2s->TxXferSize)) |
|
| 869 | { |
1168 | { |
| 870 | /* Enable Tx DMA Request */ |
1169 | /* Update SPI error code */ |
| 871 | SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); |
1170 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); |
| 872 | } |
1171 | hi2s->State = HAL_I2S_STATE_READY; |
| 873 | 1172 | ||
| 874 | /* Process Unlocked */ |
- | |
| 875 | __HAL_UNLOCK(hi2s); |
1173 | __HAL_UNLOCK(hi2s); |
| - | 1174 | return HAL_ERROR; |
|
| - | 1175 | } |
|
| 876 | 1176 | ||
| - | 1177 | /* Check if the I2S is already enabled */ |
|
| - | 1178 | if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE)) |
|
| - | 1179 | { |
|
| - | 1180 | /* Enable I2S peripheral */ |
|
| 877 | return HAL_OK; |
1181 | __HAL_I2S_ENABLE(hi2s); |
| 878 | } |
1182 | } |
| 879 | else |
1183 | |
| - | 1184 | /* Check if the I2S Tx request is already enabled */ |
|
| - | 1185 | if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_TXDMAEN)) |
|
| 880 | { |
1186 | { |
| 881 | return HAL_BUSY; |
1187 | /* Enable Tx DMA Request */ |
| - | 1188 | SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); |
|
| 882 | } |
1189 | } |
| - | 1190 | ||
| - | 1191 | __HAL_UNLOCK(hi2s); |
|
| - | 1192 | return HAL_OK; |
|
| 883 | } |
1193 | } |
| 884 | 1194 | ||
| 885 | /** |
1195 | /** |
| 886 | * @brief Receive an amount of data in non-blocking mode with DMA |
1196 | * @brief Receive an amount of data in non-blocking mode with DMA |
| 887 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1197 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 888 | * the configuration information for I2S module |
1198 | * the configuration information for I2S module |
| 889 | * @param pData: a 16-bit pointer to the Receive data buffer. |
1199 | * @param pData a 16-bit pointer to the Receive data buffer. |
| 890 | * @param Size: number of data sample to be sent: |
1200 | * @param Size number of data sample to be sent: |
| 891 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
1201 | * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S |
| 892 | * configuration phase, the Size parameter means the number of 16-bit data length |
1202 | * configuration phase, the Size parameter means the number of 16-bit data length |
| 893 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
1203 | * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected |
| 894 | * the Size parameter means the number of 16-bit data length. |
1204 | * the Size parameter means the number of 16-bit data length. |
| 895 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
1205 | * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization |
| 896 | * between Master and Slave(example: audio streaming). |
1206 | * between Master and Slave(example: audio streaming). |
| 897 | * @retval HAL status |
1207 | * @retval HAL status |
| 898 | */ |
1208 | */ |
| 899 | HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
1209 | HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size) |
| 900 | { |
1210 | { |
| 901 | uint32_t *tmp = NULL; |
- | |
| 902 | uint32_t tmp1 = 0U; |
1211 | uint32_t tmpreg_cfgr; |
| 903 | 1212 | ||
| 904 | if((pData == NULL) || (Size == 0U)) |
1213 | if ((pData == NULL) || (Size == 0U)) |
| 905 | { |
1214 | { |
| 906 | return HAL_ERROR; |
1215 | return HAL_ERROR; |
| 907 | } |
1216 | } |
| 908 | 1217 | ||
| - | 1218 | /* Process Locked */ |
|
| - | 1219 | __HAL_LOCK(hi2s); |
|
| - | 1220 | ||
| 909 | if(hi2s->State == HAL_I2S_STATE_READY) |
1221 | if (hi2s->State != HAL_I2S_STATE_READY) |
| 910 | { |
1222 | { |
| 911 | hi2s->pRxBuffPtr = pData; |
1223 | __HAL_UNLOCK(hi2s); |
| 912 | tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
- | |
| 913 | if((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B)) |
- | |
| 914 | { |
- | |
| 915 | hi2s->RxXferSize = (Size << 1U); |
- | |
| 916 | hi2s->RxXferCount = (Size << 1U); |
- | |
| 917 | } |
- | |
| 918 | else |
- | |
| 919 | { |
- | |
| 920 | hi2s->RxXferSize = Size; |
- | |
| 921 | hi2s->RxXferCount = Size; |
1224 | return HAL_BUSY; |
| 922 | } |
1225 | } |
| 923 | /* Process Locked */ |
- | |
| 924 | __HAL_LOCK(hi2s); |
- | |
| 925 | 1226 | ||
| - | 1227 | /* Set state and reset error code */ |
|
| 926 | hi2s->State = HAL_I2S_STATE_BUSY_RX; |
1228 | hi2s->State = HAL_I2S_STATE_BUSY_RX; |
| 927 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
1229 | hi2s->ErrorCode = HAL_I2S_ERROR_NONE; |
| - | 1230 | hi2s->pRxBuffPtr = pData; |
|
| 928 | 1231 | ||
| 929 | /* Set the I2S Rx DMA Half transfer complete callback */ |
- | |
| 930 | hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt; |
1232 | tmpreg_cfgr = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN); |
| 931 | 1233 | ||
| - | 1234 | if ((tmpreg_cfgr == I2S_DATAFORMAT_24B) || (tmpreg_cfgr == I2S_DATAFORMAT_32B)) |
|
| - | 1235 | { |
|
| 932 | /* Set the I2S Rx DMA transfer complete callback */ |
1236 | hi2s->RxXferSize = (Size << 1U); |
| 933 | hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt; |
1237 | hi2s->RxXferCount = (Size << 1U); |
| - | 1238 | } |
|
| - | 1239 | else |
|
| - | 1240 | { |
|
| - | 1241 | hi2s->RxXferSize = Size; |
|
| - | 1242 | hi2s->RxXferCount = Size; |
|
| - | 1243 | } |
|
| 934 | 1244 | ||
| 935 | /* Set the DMA error callback */ |
1245 | /* Set the I2S Rx DMA Half transfer complete callback */ |
| 936 | hi2s->hdmarx->XferErrorCallback = I2S_DMAError; |
1246 | hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt; |
| 937 | 1247 | ||
| 938 | /* Check if Master Receiver mode is selected */ |
1248 | /* Set the I2S Rx DMA transfer complete callback */ |
| 939 | if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX) |
1249 | hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt; |
| 940 | { |
- | |
| 941 | /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read |
- | |
| 942 | access to the SPI_SR register. */ |
- | |
| 943 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
- | |
| 944 | } |
- | |
| 945 | 1250 | ||
| 946 | /* Enable the Rx DMA Stream */ |
1251 | /* Set the DMA error callback */ |
| 947 | tmp = (uint32_t*)&pData; |
- | |
| 948 | HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize); |
1252 | hi2s->hdmarx->XferErrorCallback = I2S_DMAError; |
| 949 | 1253 | ||
| 950 | /* Check if the I2S is already enabled */ |
1254 | /* Check if Master Receiver mode is selected */ |
| 951 | if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE) |
1255 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX) |
| 952 | { |
1256 | { |
| - | 1257 | /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read |
|
| 953 | /* Enable I2S peripheral */ |
1258 | access to the SPI_SR register. */ |
| 954 | __HAL_I2S_ENABLE(hi2s); |
1259 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
| 955 | } |
1260 | } |
| 956 | 1261 | ||
| 957 | /* Check if the I2S Rx request is already enabled */ |
1262 | /* Enable the Rx DMA Stream/Channel */ |
| - | 1263 | if (HAL_OK != HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, (uint32_t)hi2s->pRxBuffPtr, |
|
| 958 | if((hi2s->Instance->CR2 &SPI_CR2_RXDMAEN) != SPI_CR2_RXDMAEN) |
1264 | hi2s->RxXferSize)) |
| 959 | { |
1265 | { |
| 960 | /* Enable Rx DMA Request */ |
1266 | /* Update SPI error code */ |
| 961 | SET_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN); |
1267 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); |
| 962 | } |
1268 | hi2s->State = HAL_I2S_STATE_READY; |
| 963 | 1269 | ||
| 964 | /* Process Unlocked */ |
- | |
| 965 | __HAL_UNLOCK(hi2s); |
1270 | __HAL_UNLOCK(hi2s); |
| - | 1271 | return HAL_ERROR; |
|
| - | 1272 | } |
|
| 966 | 1273 | ||
| - | 1274 | /* Check if the I2S is already enabled */ |
|
| - | 1275 | if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE)) |
|
| - | 1276 | { |
|
| - | 1277 | /* Enable I2S peripheral */ |
|
| 967 | return HAL_OK; |
1278 | __HAL_I2S_ENABLE(hi2s); |
| 968 | } |
1279 | } |
| 969 | else |
1280 | |
| - | 1281 | /* Check if the I2S Rx request is already enabled */ |
|
| - | 1282 | if (HAL_IS_BIT_CLR(hi2s->Instance->CR2, SPI_CR2_RXDMAEN)) |
|
| 970 | { |
1283 | { |
| 971 | return HAL_BUSY; |
1284 | /* Enable Rx DMA Request */ |
| - | 1285 | SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); |
|
| 972 | } |
1286 | } |
| - | 1287 | ||
| - | 1288 | __HAL_UNLOCK(hi2s); |
|
| - | 1289 | return HAL_OK; |
|
| 973 | } |
1290 | } |
| 974 | 1291 | ||
| 975 | /** |
1292 | /** |
| 976 | * @brief Pauses the audio channel playing from the Media. |
1293 | * @brief Pauses the audio DMA Stream/Channel playing from the Media. |
| 977 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1294 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 978 | * the configuration information for I2S module |
1295 | * the configuration information for I2S module |
| 979 | * @retval HAL status |
1296 | * @retval HAL status |
| 980 | */ |
1297 | */ |
| 981 | HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s) |
1298 | HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s) |
| 982 | { |
1299 | { |
| 983 | /* Process Locked */ |
1300 | /* Process Locked */ |
| 984 | __HAL_LOCK(hi2s); |
1301 | __HAL_LOCK(hi2s); |
| 985 | 1302 | ||
| 986 | if(hi2s->State == HAL_I2S_STATE_BUSY_TX) |
1303 | if (hi2s->State == HAL_I2S_STATE_BUSY_TX) |
| 987 | { |
1304 | { |
| 988 | /* Disable the I2S DMA Tx request */ |
1305 | /* Disable the I2S DMA Tx request */ |
| 989 | CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN); |
1306 | CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); |
| 990 | } |
1307 | } |
| 991 | else if(hi2s->State == HAL_I2S_STATE_BUSY_RX) |
1308 | else if (hi2s->State == HAL_I2S_STATE_BUSY_RX) |
| 992 | { |
1309 | { |
| 993 | /* Disable the I2S DMA Rx request */ |
1310 | /* Disable the I2S DMA Rx request */ |
| 994 | CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN); |
1311 | CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); |
| - | 1312 | } |
|
| - | 1313 | else |
|
| - | 1314 | { |
|
| - | 1315 | /* nothing to do */ |
|
| 995 | } |
1316 | } |
| 996 | 1317 | ||
| 997 | /* Process Unlocked */ |
1318 | /* Process Unlocked */ |
| 998 | __HAL_UNLOCK(hi2s); |
1319 | __HAL_UNLOCK(hi2s); |
| 999 | 1320 | ||
| 1000 | return HAL_OK; |
1321 | return HAL_OK; |
| 1001 | } |
1322 | } |
| 1002 | 1323 | ||
| 1003 | /** |
1324 | /** |
| 1004 | * @brief Resumes the audio channel playing from the Media. |
1325 | * @brief Resumes the audio DMA Stream/Channel playing from the Media. |
| 1005 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1326 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1006 | * the configuration information for I2S module |
1327 | * the configuration information for I2S module |
| 1007 | * @retval HAL status |
1328 | * @retval HAL status |
| 1008 | */ |
1329 | */ |
| 1009 | HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s) |
1330 | HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s) |
| 1010 | { |
1331 | { |
| 1011 | /* Process Locked */ |
1332 | /* Process Locked */ |
| 1012 | __HAL_LOCK(hi2s); |
1333 | __HAL_LOCK(hi2s); |
| 1013 | 1334 | ||
| 1014 | if(hi2s->State == HAL_I2S_STATE_BUSY_TX) |
1335 | if (hi2s->State == HAL_I2S_STATE_BUSY_TX) |
| 1015 | { |
1336 | { |
| 1016 | /* Enable the I2S DMA Tx request */ |
1337 | /* Enable the I2S DMA Tx request */ |
| 1017 | SET_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN); |
1338 | SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); |
| 1018 | } |
1339 | } |
| 1019 | else if(hi2s->State == HAL_I2S_STATE_BUSY_RX) |
1340 | else if (hi2s->State == HAL_I2S_STATE_BUSY_RX) |
| 1020 | { |
1341 | { |
| 1021 | /* Enable the I2S DMA Rx request */ |
1342 | /* Enable the I2S DMA Rx request */ |
| 1022 | SET_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN); |
1343 | SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); |
| - | 1344 | } |
|
| - | 1345 | else |
|
| - | 1346 | { |
|
| - | 1347 | /* nothing to do */ |
|
| 1023 | } |
1348 | } |
| 1024 | 1349 | ||
| 1025 | /* If the I2S peripheral is still not enabled, enable it */ |
1350 | /* If the I2S peripheral is still not enabled, enable it */ |
| 1026 | if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) == 0U) |
1351 | if (HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE)) |
| 1027 | { |
1352 | { |
| 1028 | /* Enable I2S peripheral */ |
1353 | /* Enable I2S peripheral */ |
| 1029 | __HAL_I2S_ENABLE(hi2s); |
1354 | __HAL_I2S_ENABLE(hi2s); |
| 1030 | } |
1355 | } |
| 1031 | 1356 | ||
| Line 1034... | Line 1359... | ||
| 1034 | 1359 | ||
| 1035 | return HAL_OK; |
1360 | return HAL_OK; |
| 1036 | } |
1361 | } |
| 1037 | 1362 | ||
| 1038 | /** |
1363 | /** |
| 1039 | * @brief Resumes the audio channel playing from the Media. |
1364 | * @brief Stops the audio DMA Stream/Channel playing from the Media. |
| 1040 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1365 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1041 | * the configuration information for I2S module |
1366 | * the configuration information for I2S module |
| 1042 | * @retval HAL status |
1367 | * @retval HAL status |
| 1043 | */ |
1368 | */ |
| 1044 | HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s) |
1369 | HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s) |
| 1045 | { |
1370 | { |
| 1046 | /* Process Locked */ |
1371 | HAL_StatusTypeDef errorcode = HAL_OK; |
| - | 1372 | /* The Lock is not implemented on this API to allow the user application |
|
| - | 1373 | to call the HAL SPI API under callbacks HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback() |
|
| - | 1374 | when calling HAL_DMA_Abort() API the DMA TX or RX Transfer complete interrupt is generated |
|
| - | 1375 | and the correspond call back is executed HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback() |
|
| 1047 | __HAL_LOCK(hi2s); |
1376 | */ |
| 1048 | 1377 | ||
| 1049 | if(hi2s->State == HAL_I2S_STATE_BUSY_TX) |
1378 | if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX)) |
| 1050 | { |
1379 | { |
| - | 1380 | /* Abort the I2S DMA tx Stream/Channel */ |
|
| - | 1381 | if (hi2s->hdmatx != NULL) |
|
| - | 1382 | { |
|
| - | 1383 | /* Disable the I2S DMA tx Stream/Channel */ |
|
| - | 1384 | if (HAL_OK != HAL_DMA_Abort(hi2s->hdmatx)) |
|
| - | 1385 | { |
|
| - | 1386 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); |
|
| - | 1387 | errorcode = HAL_ERROR; |
|
| - | 1388 | } |
|
| - | 1389 | } |
|
| - | 1390 | ||
| - | 1391 | /* Wait until TXE flag is set */ |
|
| - | 1392 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, I2S_TIMEOUT_FLAG) != HAL_OK) |
|
| - | 1393 | { |
|
| - | 1394 | /* Set the error code */ |
|
| - | 1395 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
|
| - | 1396 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 1397 | errorcode = HAL_ERROR; |
|
| - | 1398 | } |
|
| - | 1399 | ||
| - | 1400 | /* Wait until BSY flag is Reset */ |
|
| - | 1401 | if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, I2S_TIMEOUT_FLAG) != HAL_OK) |
|
| - | 1402 | { |
|
| - | 1403 | /* Set the error code */ |
|
| - | 1404 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT); |
|
| - | 1405 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 1406 | errorcode = HAL_ERROR; |
|
| - | 1407 | } |
|
| - | 1408 | ||
| - | 1409 | /* Disable I2S peripheral */ |
|
| - | 1410 | __HAL_I2S_DISABLE(hi2s); |
|
| - | 1411 | ||
| - | 1412 | /* Clear UDR flag */ |
|
| - | 1413 | __HAL_I2S_CLEAR_UDRFLAG(hi2s); |
|
| - | 1414 | ||
| 1051 | /* Disable the I2S DMA requests */ |
1415 | /* Disable the I2S Tx DMA requests */ |
| 1052 | CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN); |
1416 | CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); |
| 1053 | 1417 | ||
| 1054 | /* Disable the I2S DMA Channel */ |
- | |
| 1055 | HAL_DMA_Abort(hi2s->hdmatx); |
- | |
| 1056 | } |
1418 | } |
| - | 1419 | ||
| 1057 | else if(hi2s->State == HAL_I2S_STATE_BUSY_RX) |
1420 | else if ((hi2s->Init.Mode == I2S_MODE_MASTER_RX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_RX)) |
| 1058 | { |
1421 | { |
| - | 1422 | /* Abort the I2S DMA rx Stream/Channel */ |
|
| - | 1423 | if (hi2s->hdmarx != NULL) |
|
| - | 1424 | { |
|
| 1059 | /* Disable the I2S DMA requests */ |
1425 | /* Disable the I2S DMA rx Stream/Channel */ |
| - | 1426 | if (HAL_OK != HAL_DMA_Abort(hi2s->hdmarx)) |
|
| - | 1427 | { |
|
| 1060 | CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN); |
1428 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); |
| - | 1429 | errorcode = HAL_ERROR; |
|
| - | 1430 | } |
|
| - | 1431 | } |
|
| - | 1432 | ||
| - | 1433 | /* Disable I2S peripheral */ |
|
| - | 1434 | __HAL_I2S_DISABLE(hi2s); |
|
| 1061 | 1435 | ||
| - | 1436 | /* Clear OVR flag */ |
|
| - | 1437 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
|
| - | 1438 | ||
| 1062 | /* Disable the I2S DMA Channel */ |
1439 | /* Disable the I2S Rx DMA request */ |
| - | 1440 | CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); |
|
| - | 1441 | ||
| - | 1442 | if (hi2s->Init.Mode == I2S_MODE_SLAVE_RX) |
|
| - | 1443 | { |
|
| - | 1444 | /* Set the error code */ |
|
| - | 1445 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_BUSY_LINE_RX); |
|
| - | 1446 | ||
| - | 1447 | /* Set the I2S State ready */ |
|
| - | 1448 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 1449 | errorcode = HAL_ERROR; |
|
| - | 1450 | } |
|
| - | 1451 | else |
|
| - | 1452 | { |
|
| - | 1453 | /* Read DR to Flush RX Data */ |
|
| 1063 | HAL_DMA_Abort(hi2s->hdmarx); |
1454 | READ_REG((hi2s->Instance)->DR); |
| - | 1455 | } |
|
| 1064 | } |
1456 | } |
| 1065 | /* Disable I2S peripheral */ |
- | |
| 1066 | __HAL_I2S_DISABLE(hi2s); |
- | |
| 1067 | 1457 | ||
| 1068 | hi2s->State = HAL_I2S_STATE_READY; |
1458 | hi2s->State = HAL_I2S_STATE_READY; |
| 1069 | 1459 | ||
| 1070 | /* Process Unlocked */ |
- | |
| 1071 | __HAL_UNLOCK(hi2s); |
- | |
| 1072 | - | ||
| 1073 | return HAL_OK; |
1460 | return errorcode; |
| 1074 | } |
1461 | } |
| 1075 | 1462 | ||
| 1076 | /** |
1463 | /** |
| 1077 | * @brief This function handles I2S interrupt request. |
1464 | * @brief This function handles I2S interrupt request. |
| 1078 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1465 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1079 | * the configuration information for I2S module |
1466 | * the configuration information for I2S module |
| 1080 | * @retval None |
1467 | * @retval None |
| 1081 | */ |
1468 | */ |
| 1082 | void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s) |
1469 | void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s) |
| 1083 | { |
1470 | { |
| - | 1471 | uint32_t itsource = hi2s->Instance->CR2; |
|
| - | 1472 | uint32_t itflag = hi2s->Instance->SR; |
|
| - | 1473 | ||
| - | 1474 | /* I2S in mode Receiver ------------------------------------------------*/ |
|
| - | 1475 | if ((I2S_CHECK_FLAG(itflag, I2S_FLAG_OVR) == RESET) && |
|
| - | 1476 | (I2S_CHECK_FLAG(itflag, I2S_FLAG_RXNE) != RESET) && (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_RXNE) != RESET)) |
|
| - | 1477 | { |
|
| - | 1478 | I2S_Receive_IT(hi2s); |
|
| - | 1479 | return; |
|
| - | 1480 | } |
|
| - | 1481 | ||
| - | 1482 | /* I2S in mode Tramitter -----------------------------------------------*/ |
|
| - | 1483 | if ((I2S_CHECK_FLAG(itflag, I2S_FLAG_TXE) != RESET) && (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_TXE) != RESET)) |
|
| - | 1484 | { |
|
| - | 1485 | I2S_Transmit_IT(hi2s); |
|
| - | 1486 | return; |
|
| - | 1487 | } |
|
| - | 1488 | ||
| - | 1489 | /* I2S interrupt error -------------------------------------------------*/ |
|
| - | 1490 | if (I2S_CHECK_IT_SOURCE(itsource, I2S_IT_ERR) != RESET) |
|
| - | 1491 | { |
|
| - | 1492 | /* I2S Overrun error interrupt occurred ---------------------------------*/ |
|
| - | 1493 | if (I2S_CHECK_FLAG(itflag, I2S_FLAG_OVR) != RESET) |
|
| - | 1494 | { |
|
| - | 1495 | /* Disable RXNE and ERR interrupt */ |
|
| - | 1496 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); |
|
| - | 1497 | ||
| - | 1498 | /* Set the error code and execute error callback*/ |
|
| - | 1499 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR); |
|
| - | 1500 | } |
|
| - | 1501 | ||
| - | 1502 | /* I2S Underrun error interrupt occurred --------------------------------*/ |
|
| - | 1503 | if (I2S_CHECK_FLAG(itflag, I2S_FLAG_UDR) != RESET) |
|
| - | 1504 | { |
|
| - | 1505 | /* Disable TXE and ERR interrupt */ |
|
| - | 1506 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); |
|
| - | 1507 | ||
| 1084 | /* Call the IrqHandler ISR set during HAL_I2S_INIT */ |
1508 | /* Set the error code and execute error callback*/ |
| - | 1509 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR); |
|
| - | 1510 | } |
|
| - | 1511 | ||
| - | 1512 | /* Set the I2S State ready */ |
|
| - | 1513 | hi2s->State = HAL_I2S_STATE_READY; |
|
| - | 1514 | ||
| - | 1515 | /* Call user error callback */ |
|
| - | 1516 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| 1085 | hi2s->IrqHandlerISR(hi2s); |
1517 | hi2s->ErrorCallback(hi2s); |
| - | 1518 | #else |
|
| - | 1519 | HAL_I2S_ErrorCallback(hi2s); |
|
| - | 1520 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| - | 1521 | } |
|
| 1086 | } |
1522 | } |
| 1087 | 1523 | ||
| 1088 | /** |
1524 | /** |
| 1089 | * @brief Tx Transfer Half completed callbacks |
1525 | * @brief Tx Transfer Half completed callbacks |
| 1090 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1526 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1091 | * the configuration information for I2S module |
1527 | * the configuration information for I2S module |
| 1092 | * @retval None |
1528 | * @retval None |
| 1093 | */ |
1529 | */ |
| 1094 | __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) |
1530 | __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s) |
| 1095 | { |
1531 | { |
| 1096 | /* Prevent unused argument(s) compilation warning */ |
1532 | /* Prevent unused argument(s) compilation warning */ |
| 1097 | UNUSED(hi2s); |
1533 | UNUSED(hi2s); |
| - | 1534 | ||
| 1098 | /* NOTE : This function Should not be modified, when the callback is needed, |
1535 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 1099 | the HAL_I2S_TxHalfCpltCallback could be implemented in the user file |
1536 | the HAL_I2S_TxHalfCpltCallback could be implemented in the user file |
| 1100 | */ |
1537 | */ |
| 1101 | } |
1538 | } |
| 1102 | 1539 | ||
| 1103 | /** |
1540 | /** |
| 1104 | * @brief Tx Transfer completed callbacks |
1541 | * @brief Tx Transfer completed callbacks |
| 1105 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1542 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1106 | * the configuration information for I2S module |
1543 | * the configuration information for I2S module |
| 1107 | * @retval None |
1544 | * @retval None |
| 1108 | */ |
1545 | */ |
| 1109 | __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) |
1546 | __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) |
| 1110 | { |
1547 | { |
| 1111 | /* Prevent unused argument(s) compilation warning */ |
1548 | /* Prevent unused argument(s) compilation warning */ |
| 1112 | UNUSED(hi2s); |
1549 | UNUSED(hi2s); |
| - | 1550 | ||
| 1113 | /* NOTE : This function Should not be modified, when the callback is needed, |
1551 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 1114 | the HAL_I2S_TxCpltCallback could be implemented in the user file |
1552 | the HAL_I2S_TxCpltCallback could be implemented in the user file |
| 1115 | */ |
1553 | */ |
| 1116 | } |
1554 | } |
| 1117 | 1555 | ||
| 1118 | /** |
1556 | /** |
| 1119 | * @brief Rx Transfer half completed callbacks |
1557 | * @brief Rx Transfer half completed callbacks |
| 1120 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1558 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1121 | * the configuration information for I2S module |
1559 | * the configuration information for I2S module |
| 1122 | * @retval None |
1560 | * @retval None |
| 1123 | */ |
1561 | */ |
| 1124 | __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s) |
1562 | __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s) |
| 1125 | { |
1563 | { |
| 1126 | /* Prevent unused argument(s) compilation warning */ |
1564 | /* Prevent unused argument(s) compilation warning */ |
| 1127 | UNUSED(hi2s); |
1565 | UNUSED(hi2s); |
| - | 1566 | ||
| 1128 | /* NOTE : This function Should not be modified, when the callback is needed, |
1567 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 1129 | the HAL_I2S_RxHalfCpltCallback could be implemented in the user file |
1568 | the HAL_I2S_RxHalfCpltCallback could be implemented in the user file |
| 1130 | */ |
1569 | */ |
| 1131 | } |
1570 | } |
| 1132 | 1571 | ||
| 1133 | /** |
1572 | /** |
| 1134 | * @brief Rx Transfer completed callbacks |
1573 | * @brief Rx Transfer completed callbacks |
| 1135 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1574 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1136 | * the configuration information for I2S module |
1575 | * the configuration information for I2S module |
| 1137 | * @retval None |
1576 | * @retval None |
| 1138 | */ |
1577 | */ |
| 1139 | __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) |
1578 | __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s) |
| 1140 | { |
1579 | { |
| 1141 | /* Prevent unused argument(s) compilation warning */ |
1580 | /* Prevent unused argument(s) compilation warning */ |
| 1142 | UNUSED(hi2s); |
1581 | UNUSED(hi2s); |
| - | 1582 | ||
| 1143 | /* NOTE : This function Should not be modified, when the callback is needed, |
1583 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 1144 | the HAL_I2S_RxCpltCallback could be implemented in the user file |
1584 | the HAL_I2S_RxCpltCallback could be implemented in the user file |
| 1145 | */ |
1585 | */ |
| 1146 | } |
1586 | } |
| 1147 | 1587 | ||
| 1148 | /** |
1588 | /** |
| 1149 | * @brief I2S error callbacks |
1589 | * @brief I2S error callbacks |
| 1150 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1590 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1151 | * the configuration information for I2S module |
1591 | * the configuration information for I2S module |
| 1152 | * @retval None |
1592 | * @retval None |
| 1153 | */ |
1593 | */ |
| 1154 | __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) |
1594 | __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s) |
| 1155 | { |
1595 | { |
| 1156 | /* Prevent unused argument(s) compilation warning */ |
1596 | /* Prevent unused argument(s) compilation warning */ |
| 1157 | UNUSED(hi2s); |
1597 | UNUSED(hi2s); |
| - | 1598 | ||
| 1158 | /* NOTE : This function Should not be modified, when the callback is needed, |
1599 | /* NOTE : This function Should not be modified, when the callback is needed, |
| 1159 | the HAL_I2S_ErrorCallback could be implemented in the user file |
1600 | the HAL_I2S_ErrorCallback could be implemented in the user file |
| 1160 | */ |
1601 | */ |
| 1161 | } |
1602 | } |
| 1162 | 1603 | ||
| 1163 | /** |
1604 | /** |
| 1164 | * @} |
1605 | * @} |
| 1165 | */ |
1606 | */ |
| 1166 | 1607 | ||
| 1167 | /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions |
1608 | /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions |
| 1168 | * @brief Peripheral State functions |
1609 | * @brief Peripheral State functions |
| 1169 | * |
1610 | * |
| 1170 | @verbatim |
1611 | @verbatim |
| 1171 | =============================================================================== |
1612 | =============================================================================== |
| 1172 | ##### Peripheral State and Errors functions ##### |
1613 | ##### Peripheral State and Errors functions ##### |
| 1173 | =============================================================================== |
1614 | =============================================================================== |
| Line 1179... | Line 1620... | ||
| 1179 | * @{ |
1620 | * @{ |
| 1180 | */ |
1621 | */ |
| 1181 | 1622 | ||
| 1182 | /** |
1623 | /** |
| 1183 | * @brief Return the I2S state |
1624 | * @brief Return the I2S state |
| 1184 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1625 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1185 | * the configuration information for I2S module |
1626 | * the configuration information for I2S module |
| 1186 | * @retval HAL state |
1627 | * @retval HAL state |
| 1187 | */ |
1628 | */ |
| 1188 | HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s) |
1629 | HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s) |
| 1189 | { |
1630 | { |
| 1190 | return hi2s->State; |
1631 | return hi2s->State; |
| 1191 | } |
1632 | } |
| 1192 | 1633 | ||
| 1193 | /** |
1634 | /** |
| 1194 | * @brief Return the I2S error code |
1635 | * @brief Return the I2S error code |
| 1195 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1636 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1196 | * the configuration information for I2S module |
1637 | * the configuration information for I2S module |
| 1197 | * @retval I2S Error Code |
1638 | * @retval I2S Error Code |
| 1198 | */ |
1639 | */ |
| 1199 | uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s) |
1640 | uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s) |
| 1200 | { |
1641 | { |
| Line 1206... | Line 1647... | ||
| 1206 | 1647 | ||
| 1207 | /** |
1648 | /** |
| 1208 | * @} |
1649 | * @} |
| 1209 | */ |
1650 | */ |
| 1210 | 1651 | ||
| 1211 | /* Private functions ---------------------------------------------------------*/ |
- | |
| 1212 | /** @addtogroup I2S_Private_Functions I2S Private Functions |
1652 | /** @addtogroup I2S_Private_Functions I2S Private Functions |
| 1213 | * @{ |
1653 | * @{ |
| 1214 | */ |
1654 | */ |
| 1215 | /** |
1655 | /** |
| 1216 | * @brief DMA I2S transmit process complete callback |
1656 | * @brief DMA I2S transmit process complete callback |
| 1217 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
1657 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
| 1218 | * the configuration information for the specified DMA module. |
1658 | * the configuration information for the specified DMA module. |
| 1219 | * @retval None |
1659 | * @retval None |
| 1220 | */ |
1660 | */ |
| 1221 | static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma) |
1661 | static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma) |
| 1222 | { |
1662 | { |
| 1223 | I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
1663 | I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
| 1224 | 1664 | ||
| - | 1665 | /* if DMA is configured in DMA_NORMAL Mode */ |
|
| 1225 | if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC)) |
1666 | if (hdma->Init.Mode == DMA_NORMAL) |
| 1226 | { |
1667 | { |
| 1227 | /* Disable Tx DMA Request */ |
1668 | /* Disable Tx DMA Request */ |
| 1228 | CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_TXDMAEN); |
1669 | CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN); |
| 1229 | 1670 | ||
| 1230 | hi2s->TxXferCount = 0U; |
1671 | hi2s->TxXferCount = 0U; |
| 1231 | hi2s->State = HAL_I2S_STATE_READY; |
1672 | hi2s->State = HAL_I2S_STATE_READY; |
| 1232 | } |
1673 | } |
| - | 1674 | /* Call user Tx complete callback */ |
|
| - | 1675 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1676 | hi2s->TxCpltCallback(hi2s); |
|
| - | 1677 | #else |
|
| 1233 | HAL_I2S_TxCpltCallback(hi2s); |
1678 | HAL_I2S_TxCpltCallback(hi2s); |
| - | 1679 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1234 | } |
1680 | } |
| - | 1681 | ||
| 1235 | /** |
1682 | /** |
| 1236 | * @brief DMA I2S transmit process half complete callback |
1683 | * @brief DMA I2S transmit process half complete callback |
| 1237 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
1684 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
| 1238 | * the configuration information for the specified DMA module. |
1685 | * the configuration information for the specified DMA module. |
| 1239 | * @retval None |
1686 | * @retval None |
| 1240 | */ |
1687 | */ |
| 1241 | static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma) |
1688 | static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma) |
| 1242 | { |
1689 | { |
| 1243 | I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
1690 | I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
| 1244 | 1691 | ||
| - | 1692 | /* Call user Tx half complete callback */ |
|
| - | 1693 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1694 | hi2s->TxHalfCpltCallback(hi2s); |
|
| - | 1695 | #else |
|
| 1245 | HAL_I2S_TxHalfCpltCallback(hi2s); |
1696 | HAL_I2S_TxHalfCpltCallback(hi2s); |
| - | 1697 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1246 | } |
1698 | } |
| 1247 | 1699 | ||
| 1248 | /** |
1700 | /** |
| 1249 | * @brief DMA I2S receive process complete callback |
1701 | * @brief DMA I2S receive process complete callback |
| 1250 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
1702 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
| 1251 | * the configuration information for the specified DMA module. |
1703 | * the configuration information for the specified DMA module. |
| 1252 | * @retval None |
1704 | * @retval None |
| 1253 | */ |
1705 | */ |
| 1254 | static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma) |
1706 | static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma) |
| 1255 | { |
1707 | { |
| 1256 | I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
1708 | I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
| 1257 | 1709 | ||
| - | 1710 | /* if DMA is configured in DMA_NORMAL Mode */ |
|
| 1258 | if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC)) |
1711 | if (hdma->Init.Mode == DMA_NORMAL) |
| 1259 | { |
1712 | { |
| 1260 | /* Disable Rx DMA Request */ |
1713 | /* Disable Rx DMA Request */ |
| 1261 | CLEAR_BIT(hi2s->Instance->CR2,SPI_CR2_RXDMAEN); |
1714 | CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN); |
| 1262 | hi2s->RxXferCount = 0U; |
1715 | hi2s->RxXferCount = 0U; |
| 1263 | hi2s->State = HAL_I2S_STATE_READY; |
1716 | hi2s->State = HAL_I2S_STATE_READY; |
| 1264 | } |
1717 | } |
| - | 1718 | /* Call user Rx complete callback */ |
|
| - | 1719 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1720 | hi2s->RxCpltCallback(hi2s); |
|
| - | 1721 | #else |
|
| 1265 | HAL_I2S_RxCpltCallback(hi2s); |
1722 | HAL_I2S_RxCpltCallback(hi2s); |
| - | 1723 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1266 | } |
1724 | } |
| 1267 | 1725 | ||
| 1268 | /** |
1726 | /** |
| 1269 | * @brief DMA I2S receive process half complete callback |
1727 | * @brief DMA I2S receive process half complete callback |
| 1270 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
1728 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
| 1271 | * the configuration information for the specified DMA module. |
1729 | * the configuration information for the specified DMA module. |
| 1272 | * @retval None |
1730 | * @retval None |
| 1273 | */ |
1731 | */ |
| 1274 | static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma) |
1732 | static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma) |
| 1275 | { |
1733 | { |
| 1276 | I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
1734 | I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
| 1277 | 1735 | ||
| - | 1736 | /* Call user Rx half complete callback */ |
|
| - | 1737 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1738 | hi2s->RxHalfCpltCallback(hi2s); |
|
| - | 1739 | #else |
|
| 1278 | HAL_I2S_RxHalfCpltCallback(hi2s); |
1740 | HAL_I2S_RxHalfCpltCallback(hi2s); |
| - | 1741 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1279 | } |
1742 | } |
| 1280 | 1743 | ||
| 1281 | /** |
1744 | /** |
| 1282 | * @brief DMA I2S communication error callback |
1745 | * @brief DMA I2S communication error callback |
| 1283 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
1746 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
| 1284 | * the configuration information for the specified DMA module. |
1747 | * the configuration information for the specified DMA module. |
| 1285 | * @retval None |
1748 | * @retval None |
| 1286 | */ |
1749 | */ |
| 1287 | static void I2S_DMAError(DMA_HandleTypeDef *hdma) |
1750 | static void I2S_DMAError(DMA_HandleTypeDef *hdma) |
| 1288 | { |
1751 | { |
| 1289 | I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
1752 | I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
| 1290 | 1753 | ||
| 1291 | /* Disable Rx and Tx DMA Request */ |
1754 | /* Disable Rx and Tx DMA Request */ |
| 1292 | CLEAR_BIT(hi2s->Instance->CR2,(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN)); |
1755 | CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN)); |
| 1293 | hi2s->TxXferCount = 0U; |
1756 | hi2s->TxXferCount = 0U; |
| 1294 | hi2s->RxXferCount = 0U; |
1757 | hi2s->RxXferCount = 0U; |
| 1295 | 1758 | ||
| 1296 | hi2s->State= HAL_I2S_STATE_READY; |
1759 | hi2s->State = HAL_I2S_STATE_READY; |
| 1297 | 1760 | ||
| - | 1761 | /* Set the error code and execute error callback*/ |
|
| 1298 | SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_DMA); |
1762 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA); |
| - | 1763 | /* Call user error callback */ |
|
| - | 1764 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1765 | hi2s->ErrorCallback(hi2s); |
|
| - | 1766 | #else |
|
| 1299 | HAL_I2S_ErrorCallback(hi2s); |
1767 | HAL_I2S_ErrorCallback(hi2s); |
| - | 1768 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1300 | } |
1769 | } |
| 1301 | 1770 | ||
| 1302 | /** |
1771 | /** |
| 1303 | * @brief Transmit an amount of data in non-blocking mode with Interrupt |
1772 | * @brief Transmit an amount of data in non-blocking mode with Interrupt |
| 1304 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1773 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1305 | * the configuration information for I2S module |
1774 | * the configuration information for I2S module |
| 1306 | * @retval HAL status |
1775 | * @retval None |
| 1307 | */ |
1776 | */ |
| 1308 | static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s) |
1777 | static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s) |
| 1309 | { |
1778 | { |
| 1310 | /* Transmit data */ |
1779 | /* Transmit data */ |
| 1311 | hi2s->Instance->DR = (*hi2s->pTxBuffPtr++); |
1780 | hi2s->Instance->DR = (*hi2s->pTxBuffPtr); |
| - | 1781 | hi2s->pTxBuffPtr++; |
|
| 1312 | hi2s->TxXferCount--; |
1782 | hi2s->TxXferCount--; |
| 1313 | 1783 | ||
| 1314 | if(hi2s->TxXferCount == 0U) |
1784 | if (hi2s->TxXferCount == 0U) |
| 1315 | { |
1785 | { |
| 1316 | /* Disable TXE and ERR interrupt */ |
1786 | /* Disable TXE and ERR interrupt */ |
| 1317 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); |
1787 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); |
| 1318 | 1788 | ||
| 1319 | hi2s->State = HAL_I2S_STATE_READY; |
1789 | hi2s->State = HAL_I2S_STATE_READY; |
| - | 1790 | /* Call user Tx complete callback */ |
|
| - | 1791 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1792 | hi2s->TxCpltCallback(hi2s); |
|
| - | 1793 | #else |
|
| 1320 | HAL_I2S_TxCpltCallback(hi2s); |
1794 | HAL_I2S_TxCpltCallback(hi2s); |
| - | 1795 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1321 | } |
1796 | } |
| 1322 | } |
1797 | } |
| 1323 | 1798 | ||
| 1324 | /** |
1799 | /** |
| 1325 | * @brief Receive an amount of data in non-blocking mode with Interrupt |
1800 | * @brief Receive an amount of data in non-blocking mode with Interrupt |
| 1326 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1801 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1327 | * the configuration information for I2S module |
1802 | * the configuration information for I2S module |
| 1328 | * @retval HAL status |
1803 | * @retval None |
| 1329 | */ |
1804 | */ |
| 1330 | static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s) |
1805 | static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s) |
| 1331 | { |
1806 | { |
| 1332 | /* Receive data */ |
1807 | /* Receive data */ |
| 1333 | (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR; |
1808 | (*hi2s->pRxBuffPtr) = (uint16_t)hi2s->Instance->DR; |
| - | 1809 | hi2s->pRxBuffPtr++; |
|
| 1334 | hi2s->RxXferCount--; |
1810 | hi2s->RxXferCount--; |
| 1335 | 1811 | ||
| 1336 | if(hi2s->RxXferCount == 0U) |
1812 | if (hi2s->RxXferCount == 0U) |
| 1337 | { |
1813 | { |
| 1338 | /* Disable RXNE and ERR interrupt */ |
1814 | /* Disable RXNE and ERR interrupt */ |
| 1339 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); |
1815 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); |
| 1340 | 1816 | ||
| 1341 | hi2s->State = HAL_I2S_STATE_READY; |
1817 | hi2s->State = HAL_I2S_STATE_READY; |
| - | 1818 | /* Call user Rx complete callback */ |
|
| - | 1819 | #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U) |
|
| - | 1820 | hi2s->RxCpltCallback(hi2s); |
|
| - | 1821 | #else |
|
| 1342 | HAL_I2S_RxCpltCallback(hi2s); |
1822 | HAL_I2S_RxCpltCallback(hi2s); |
| - | 1823 | #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */ |
|
| 1343 | } |
1824 | } |
| 1344 | } |
1825 | } |
| 1345 | 1826 | ||
| 1346 | /** |
1827 | /** |
| 1347 | * @brief This function handles I2S interrupt request. |
- | |
| 1348 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
- | |
| 1349 | * the configuration information for I2S module |
- | |
| 1350 | * @retval None |
- | |
| 1351 | */ |
- | |
| 1352 | static void I2S_IRQHandler(I2S_HandleTypeDef *hi2s) |
- | |
| 1353 | { |
- | |
| 1354 | __IO uint32_t i2ssr = hi2s->Instance->SR; |
- | |
| 1355 | - | ||
| 1356 | if(hi2s->State == HAL_I2S_STATE_BUSY_RX) |
- | |
| 1357 | { |
- | |
| 1358 | /* I2S in mode Receiver ------------------------------------------------*/ |
- | |
| 1359 | if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET)) |
- | |
| 1360 | { |
- | |
| 1361 | I2S_Receive_IT(hi2s); |
- | |
| 1362 | } |
- | |
| 1363 | - | ||
| 1364 | /* I2S Overrun error interrupt occurred -------------------------------------*/ |
- | |
| 1365 | if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET)) |
- | |
| 1366 | { |
- | |
| 1367 | /* Disable RXNE and ERR interrupt */ |
- | |
| 1368 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR)); |
- | |
| 1369 | - | ||
| 1370 | /* Clear Overrun flag */ |
- | |
| 1371 | __HAL_I2S_CLEAR_OVRFLAG(hi2s); |
- | |
| 1372 | - | ||
| 1373 | /* Set the I2S State ready */ |
- | |
| 1374 | hi2s->State = HAL_I2S_STATE_READY; |
- | |
| 1375 | - | ||
| 1376 | - | ||
| 1377 | /* Set the error code and execute error callback*/ |
- | |
| 1378 | SET_BIT(hi2s->ErrorCode,HAL_I2S_ERROR_OVR); |
- | |
| 1379 | HAL_I2S_ErrorCallback(hi2s); |
- | |
| 1380 | } |
- | |
| 1381 | } |
- | |
| 1382 | - | ||
| 1383 | if(hi2s->State == HAL_I2S_STATE_BUSY_TX) |
- | |
| 1384 | { |
- | |
| 1385 | /* I2S in mode Transmitter -----------------------------------------------*/ |
- | |
| 1386 | if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET)) |
- | |
| 1387 | { |
- | |
| 1388 | I2S_Transmit_IT(hi2s); |
- | |
| 1389 | } |
- | |
| 1390 | - | ||
| 1391 | /* I2S Underrun error interrupt occurred --------------------------------*/ |
- | |
| 1392 | if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET)) |
- | |
| 1393 | { |
- | |
| 1394 | /* Disable TXE and ERR interrupt */ |
- | |
| 1395 | __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR)); |
- | |
| 1396 | - | ||
| 1397 | /* Clear Underrun flag */ |
- | |
| 1398 | __HAL_I2S_CLEAR_UDRFLAG(hi2s); |
- | |
| 1399 | - | ||
| 1400 | /* Set the I2S State ready */ |
- | |
| 1401 | hi2s->State = HAL_I2S_STATE_READY; |
- | |
| 1402 | - | ||
| 1403 | /* Set the error code and execute error callback*/ |
- | |
| 1404 | SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR); |
- | |
| 1405 | HAL_I2S_ErrorCallback(hi2s); |
- | |
| 1406 | } |
- | |
| 1407 | } |
- | |
| 1408 | } |
- | |
| 1409 | - | ||
| 1410 | /** |
- | |
| 1411 | * @brief This function handles I2S Communication Timeout. |
1828 | * @brief This function handles I2S Communication Timeout. |
| 1412 | * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains |
1829 | * @param hi2s pointer to a I2S_HandleTypeDef structure that contains |
| 1413 | * the configuration information for I2S module |
1830 | * the configuration information for I2S module |
| 1414 | * @param Flag: Flag checked |
1831 | * @param Flag Flag checked |
| 1415 | * @param State: Value of the flag expected |
1832 | * @param State Value of the flag expected |
| 1416 | * @param Timeout: Duration of the timeout |
1833 | * @param Timeout Duration of the timeout |
| 1417 | * @retval HAL status |
1834 | * @retval HAL status |
| 1418 | */ |
1835 | */ |
| 1419 | static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State, |
1836 | static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, FlagStatus State, |
| 1420 | uint32_t Timeout) |
1837 | uint32_t Timeout) |
| 1421 | { |
1838 | { |
| - | 1839 | uint32_t tickstart; |
|
| - | 1840 | ||
| - | 1841 | /* Get tick */ |
|
| 1422 | uint32_t tickstart = HAL_GetTick(); |
1842 | tickstart = HAL_GetTick(); |
| 1423 | 1843 | ||
| 1424 | /* Wait until flag is set to status*/ |
1844 | /* Wait until flag is set to status*/ |
| 1425 | while(((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State) |
1845 | while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State) |
| 1426 | { |
1846 | { |
| 1427 | if(Timeout != HAL_MAX_DELAY) |
1847 | if (Timeout != HAL_MAX_DELAY) |
| 1428 | { |
1848 | { |
| 1429 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
1849 | if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U)) |
| 1430 | { |
1850 | { |
| 1431 | /* Set the I2S State ready */ |
1851 | /* Set the I2S State ready */ |
| 1432 | hi2s->State = HAL_I2S_STATE_READY; |
1852 | hi2s->State = HAL_I2S_STATE_READY; |
| 1433 | 1853 | ||
| 1434 | /* Process Unlocked */ |
1854 | /* Process Unlocked */ |
| Line 1447... | Line 1867... | ||
| 1447 | 1867 | ||
| 1448 | /** |
1868 | /** |
| 1449 | * @} |
1869 | * @} |
| 1450 | */ |
1870 | */ |
| 1451 | 1871 | ||
| 1452 | #endif /* STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */ |
- | |
| 1453 | #endif /* HAL_I2S_MODULE_ENABLED */ |
- | |
| 1454 | /** |
1872 | /** |
| 1455 | * @} |
1873 | * @} |
| 1456 | */ |
1874 | */ |
| - | 1875 | #endif /* SPI_I2S_SUPPORT */ |
|
| - | 1876 | ||
| - | 1877 | #endif /* HAL_I2S_MODULE_ENABLED */ |
|
| 1457 | 1878 | ||
| 1458 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
1879 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |