mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jun 24 14:45:08 2014 +0100
Revision:
237:f3da66175598
Child:
375:3d36234a1087
Synchronized with git revision 8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7

Full URL: https://github.com/mbedmicro/mbed/commit/8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7/

[NUCLEO_F334R8] Add platform files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 237:f3da66175598 1 /**
mbed_official 237:f3da66175598 2 ******************************************************************************
mbed_official 237:f3da66175598 3 * @file stm32f3xx_hal_irda.c
mbed_official 237:f3da66175598 4 * @author MCD Application Team
mbed_official 237:f3da66175598 5 * @version V1.0.1
mbed_official 237:f3da66175598 6 * @date 18-June-2014
mbed_official 237:f3da66175598 7 * @brief IRDA HAL module driver.
mbed_official 237:f3da66175598 8 *
mbed_official 237:f3da66175598 9 * This file provides firmware functions to manage the following
mbed_official 237:f3da66175598 10 * functionalities of the IrDA (Infrared Data Association) Peripheral
mbed_official 237:f3da66175598 11 * (IRDA)
mbed_official 237:f3da66175598 12 * + Initialization and de-initialization functions
mbed_official 237:f3da66175598 13 * + IO operation functions
mbed_official 237:f3da66175598 14 * + Peripheral Control functions
mbed_official 237:f3da66175598 15 *
mbed_official 237:f3da66175598 16 *
mbed_official 237:f3da66175598 17 @verbatim
mbed_official 237:f3da66175598 18 ===============================================================================
mbed_official 237:f3da66175598 19 ##### How to use this driver #####
mbed_official 237:f3da66175598 20 ===============================================================================
mbed_official 237:f3da66175598 21 [..]
mbed_official 237:f3da66175598 22 The IRDA HAL driver can be used as follows:
mbed_official 237:f3da66175598 23
mbed_official 237:f3da66175598 24 (#) Declare a IRDA_HandleTypeDef handle structure.
mbed_official 237:f3da66175598 25 (#) Initialize the IRDA low level resources by implementing the HAL_IRDA_MspInit() API
mbed_official 237:f3da66175598 26 in setting the associated USART or UART in IRDA mode:
mbed_official 237:f3da66175598 27 (##) Enable the USARTx/UARTx interface clock.
mbed_official 237:f3da66175598 28 (##) USARTx/UARTx pins configuration:
mbed_official 237:f3da66175598 29 (+) Enable the clock for the USARTx/UARTx GPIOs.
mbed_official 237:f3da66175598 30 (+) Configure these USARTx/UARTx pins as alternate function pull-up.
mbed_official 237:f3da66175598 31 (##) NVIC configuration if you need to use interrupt process (HAL_IRDA_Transmit_IT()
mbed_official 237:f3da66175598 32 and HAL_IRDA_Receive_IT() APIs):
mbed_official 237:f3da66175598 33 (+) Configure the USARTx/UARTx interrupt priority.
mbed_official 237:f3da66175598 34 (+) Enable the NVIC IRDA IRQ handle.
mbed_official 237:f3da66175598 35 (@) The specific IRDA interrupts (Transmission complete interrupt,
mbed_official 237:f3da66175598 36 RXNE interrupt and Error Interrupts) will be managed using the macros
mbed_official 237:f3da66175598 37 __HAL_IRDA_ENABLE_IT() and __HAL_IRDA_DISABLE_IT() inside the transmit and receive process.
mbed_official 237:f3da66175598 38 (##) DMA Configuration if you need to use DMA process (HAL_IRDA_Transmit_DMA()
mbed_official 237:f3da66175598 39 and HAL_IRDA_Receive_DMA() APIs):
mbed_official 237:f3da66175598 40 (+) Declare a DMA handle structure for the Tx/Rx stream.
mbed_official 237:f3da66175598 41 (+) Enable the DMAx interface clock.
mbed_official 237:f3da66175598 42 (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.
mbed_official 237:f3da66175598 43 (+) Configure the DMA Tx/Rx Stream.
mbed_official 237:f3da66175598 44 (+) Associate the initilalized DMA handle to the IRDA DMA Tx/Rx handle.
mbed_official 237:f3da66175598 45 (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
mbed_official 237:f3da66175598 46
mbed_official 237:f3da66175598 47 (#) Program the Baud Rate, Word Length and Parity and Mode(Receiver/Transmitter),
mbed_official 237:f3da66175598 48 the normal or low power mode and the clock prescaler in the hirda Init structure.
mbed_official 237:f3da66175598 49
mbed_official 237:f3da66175598 50 (#) Initialize the IRDA registers by calling
mbed_official 237:f3da66175598 51 the HAL_IRDA_Init() API.
mbed_official 237:f3da66175598 52
mbed_official 237:f3da66175598 53 (@) This API (HAL_IRDA_Init()) configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
mbed_official 237:f3da66175598 54 by calling the customized HAL_IRDA_MspInit() API.
mbed_official 237:f3da66175598 55
mbed_official 237:f3da66175598 56 @endverbatim
mbed_official 237:f3da66175598 57 ******************************************************************************
mbed_official 237:f3da66175598 58 * @attention
mbed_official 237:f3da66175598 59 *
mbed_official 237:f3da66175598 60 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 237:f3da66175598 61 *
mbed_official 237:f3da66175598 62 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 237:f3da66175598 63 * are permitted provided that the following conditions are met:
mbed_official 237:f3da66175598 64 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 237:f3da66175598 65 * this list of conditions and the following disclaimer.
mbed_official 237:f3da66175598 66 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 237:f3da66175598 67 * this list of conditions and the following disclaimer in the documentation
mbed_official 237:f3da66175598 68 * and/or other materials provided with the distribution.
mbed_official 237:f3da66175598 69 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 237:f3da66175598 70 * may be used to endorse or promote products derived from this software
mbed_official 237:f3da66175598 71 * without specific prior written permission.
mbed_official 237:f3da66175598 72 *
mbed_official 237:f3da66175598 73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 237:f3da66175598 74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 237:f3da66175598 75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 237:f3da66175598 76 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 237:f3da66175598 77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 237:f3da66175598 78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 237:f3da66175598 79 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 237:f3da66175598 80 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 237:f3da66175598 81 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 237:f3da66175598 82 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 237:f3da66175598 83 *
mbed_official 237:f3da66175598 84 ******************************************************************************
mbed_official 237:f3da66175598 85 */
mbed_official 237:f3da66175598 86
mbed_official 237:f3da66175598 87 /* Includes ------------------------------------------------------------------*/
mbed_official 237:f3da66175598 88 #include "stm32f3xx_hal.h"
mbed_official 237:f3da66175598 89
mbed_official 237:f3da66175598 90 /** @addtogroup STM32F3xx_HAL_Driver
mbed_official 237:f3da66175598 91 * @{
mbed_official 237:f3da66175598 92 */
mbed_official 237:f3da66175598 93
mbed_official 237:f3da66175598 94 /** @defgroup IRDA
mbed_official 237:f3da66175598 95 * @brief HAL IRDA module driver
mbed_official 237:f3da66175598 96 * @{
mbed_official 237:f3da66175598 97 */
mbed_official 237:f3da66175598 98 #ifdef HAL_IRDA_MODULE_ENABLED
mbed_official 237:f3da66175598 99
mbed_official 237:f3da66175598 100 /* Private typedef -----------------------------------------------------------*/
mbed_official 237:f3da66175598 101 /* Private define ------------------------------------------------------------*/
mbed_official 237:f3da66175598 102 #define TEACK_REACK_TIMEOUT 1000
mbed_official 237:f3da66175598 103 #define IRDA_TXDMA_TIMEOUTVALUE 22000
mbed_official 237:f3da66175598 104 #define IRDA_TIMEOUT_VALUE 22000
mbed_official 237:f3da66175598 105 #define IRDA_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE \
mbed_official 237:f3da66175598 106 | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE))
mbed_official 237:f3da66175598 107 /* Private macro -------------------------------------------------------------*/
mbed_official 237:f3da66175598 108 /* Private variables ---------------------------------------------------------*/
mbed_official 237:f3da66175598 109 /* Private function prototypes -----------------------------------------------*/
mbed_official 237:f3da66175598 110 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma);
mbed_official 237:f3da66175598 111 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
mbed_official 237:f3da66175598 112 static void IRDA_DMAError(DMA_HandleTypeDef *hdma);
mbed_official 237:f3da66175598 113 static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda);
mbed_official 237:f3da66175598 114 static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda);
mbed_official 237:f3da66175598 115 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
mbed_official 237:f3da66175598 116 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda);
mbed_official 237:f3da66175598 117 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda);
mbed_official 237:f3da66175598 118 /* Private functions ---------------------------------------------------------*/
mbed_official 237:f3da66175598 119
mbed_official 237:f3da66175598 120 /** @defgroup IRDA_Private_Functions
mbed_official 237:f3da66175598 121 * @{
mbed_official 237:f3da66175598 122 */
mbed_official 237:f3da66175598 123
mbed_official 237:f3da66175598 124 /** @defgroup HAL_IRDA_Group1 Initialization/de-initialization functions
mbed_official 237:f3da66175598 125 * @brief Initialization and Configuration functions
mbed_official 237:f3da66175598 126 *
mbed_official 237:f3da66175598 127 @verbatim
mbed_official 237:f3da66175598 128 ===============================================================================
mbed_official 237:f3da66175598 129 ##### Initialization and Configuration functions #####
mbed_official 237:f3da66175598 130 ===============================================================================
mbed_official 237:f3da66175598 131 [..]
mbed_official 237:f3da66175598 132 This subsection provides a set of functions allowing to initialize the USARTx
mbed_official 237:f3da66175598 133 in asynchronous IRDA mode.
mbed_official 237:f3da66175598 134 (+) For the asynchronous mode only these parameters can be configured:
mbed_official 237:f3da66175598 135 (++) Baud Rate
mbed_official 237:f3da66175598 136 (++) Word Length
mbed_official 237:f3da66175598 137 (++) Parity: If the parity is enabled, then the MSB bit of the data written
mbed_official 237:f3da66175598 138 in the data register is transmitted but is changed by the parity bit.
mbed_official 237:f3da66175598 139 Depending on the frame length defined by the M bit (8-bits or 9-bits)
mbed_official 237:f3da66175598 140 or by the M1 and M0 bits (7-bit, 8-bit or 9-bit),
mbed_official 237:f3da66175598 141 the possible IRDA frame formats are as listed in the following table:
mbed_official 237:f3da66175598 142 +---------------------------------------------------------------+
mbed_official 237:f3da66175598 143 | M bit | PCE bit | IRDA frame |
mbed_official 237:f3da66175598 144 |-----------|-----------|---------------------------------------|
mbed_official 237:f3da66175598 145 | 0 | 0 | | SB | 8-bit data | STB | |
mbed_official 237:f3da66175598 146 |-----------|-----------|---------------------------------------|
mbed_official 237:f3da66175598 147 | 0 | 1 | | SB | 7-bit data | PB | STB | |
mbed_official 237:f3da66175598 148 |-----------|-----------|---------------------------------------|
mbed_official 237:f3da66175598 149 | 1 | 0 | | SB | 9-bit data | STB | |
mbed_official 237:f3da66175598 150 |-----------|-----------|---------------------------------------|
mbed_official 237:f3da66175598 151 | 1 | 1 | | SB | 8-bit data | PB | STB | |
mbed_official 237:f3da66175598 152 +---------------------------------------------------------------+
mbed_official 237:f3da66175598 153 | M1M0 bits | PCE bit | IRDA frame |
mbed_official 237:f3da66175598 154 |-----------------------|---------------------------------------|
mbed_official 237:f3da66175598 155 | 10 | 0 | | SB | 7-bit data | STB | |
mbed_official 237:f3da66175598 156 |-----------|-----------|---------------------------------------|
mbed_official 237:f3da66175598 157 | 10 | 1 | | SB | 6-bit data | PB | STB | |
mbed_official 237:f3da66175598 158 +---------------------------------------------------------------+
mbed_official 237:f3da66175598 159
mbed_official 237:f3da66175598 160 (++) Power mode
mbed_official 237:f3da66175598 161 (++) Prescaler setting
mbed_official 237:f3da66175598 162 (++) Receiver/transmitter modes
mbed_official 237:f3da66175598 163
mbed_official 237:f3da66175598 164 [..]
mbed_official 237:f3da66175598 165 The HAL_IRDA_Init() API follows the USART asynchronous configuration procedures
mbed_official 237:f3da66175598 166 (details for the procedures are available in reference manual).
mbed_official 237:f3da66175598 167
mbed_official 237:f3da66175598 168 @endverbatim
mbed_official 237:f3da66175598 169 * @{
mbed_official 237:f3da66175598 170 */
mbed_official 237:f3da66175598 171
mbed_official 237:f3da66175598 172 /**
mbed_official 237:f3da66175598 173 * @brief Initializes the IRDA mode according to the specified
mbed_official 237:f3da66175598 174 * parameters in the IRDA_InitTypeDef and creates the associated handle .
mbed_official 237:f3da66175598 175 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 176 * @retval HAL status
mbed_official 237:f3da66175598 177 */
mbed_official 237:f3da66175598 178 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 179 {
mbed_official 237:f3da66175598 180 /* Check the IRDA handle allocation */
mbed_official 237:f3da66175598 181 if(hirda == NULL)
mbed_official 237:f3da66175598 182 {
mbed_official 237:f3da66175598 183 return HAL_ERROR;
mbed_official 237:f3da66175598 184 }
mbed_official 237:f3da66175598 185
mbed_official 237:f3da66175598 186 /* Check the USART/UART associated to the IRDA handle */
mbed_official 237:f3da66175598 187 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
mbed_official 237:f3da66175598 188
mbed_official 237:f3da66175598 189 if(hirda->State == HAL_IRDA_STATE_RESET)
mbed_official 237:f3da66175598 190 {
mbed_official 237:f3da66175598 191 /* Init the low level hardware : GPIO, CLOCK */
mbed_official 237:f3da66175598 192 HAL_IRDA_MspInit(hirda);
mbed_official 237:f3da66175598 193 }
mbed_official 237:f3da66175598 194
mbed_official 237:f3da66175598 195 hirda->State = HAL_IRDA_STATE_BUSY;
mbed_official 237:f3da66175598 196
mbed_official 237:f3da66175598 197 /* Disable the Peripheral to update the configuration registers */
mbed_official 237:f3da66175598 198 __HAL_IRDA_DISABLE(hirda);
mbed_official 237:f3da66175598 199
mbed_official 237:f3da66175598 200 /* Set the IRDA Communication parameters */
mbed_official 237:f3da66175598 201 if (IRDA_SetConfig(hirda) == HAL_ERROR)
mbed_official 237:f3da66175598 202 {
mbed_official 237:f3da66175598 203 return HAL_ERROR;
mbed_official 237:f3da66175598 204 }
mbed_official 237:f3da66175598 205
mbed_official 237:f3da66175598 206 /* In IRDA mode, the following bits must be kept cleared:
mbed_official 237:f3da66175598 207 - LINEN, STOP and CLKEN bits in the USART_CR2 register,
mbed_official 237:f3da66175598 208 - SCEN and HDSEL bits in the USART_CR3 register.*/
mbed_official 237:f3da66175598 209 hirda->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN | USART_CR2_STOP);
mbed_official 237:f3da66175598 210 hirda->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL);
mbed_official 237:f3da66175598 211
mbed_official 237:f3da66175598 212 /* set the UART/USART in IRDA mode */
mbed_official 237:f3da66175598 213 hirda->Instance->CR3 |= USART_CR3_IREN;
mbed_official 237:f3da66175598 214
mbed_official 237:f3da66175598 215 /* Enable the Peripheral */
mbed_official 237:f3da66175598 216 __HAL_IRDA_ENABLE(hirda);
mbed_official 237:f3da66175598 217
mbed_official 237:f3da66175598 218 /* TEACK and/or REACK to check before moving hirda->State to Ready */
mbed_official 237:f3da66175598 219 return (IRDA_CheckIdleState(hirda));
mbed_official 237:f3da66175598 220 }
mbed_official 237:f3da66175598 221
mbed_official 237:f3da66175598 222
mbed_official 237:f3da66175598 223
mbed_official 237:f3da66175598 224
mbed_official 237:f3da66175598 225
mbed_official 237:f3da66175598 226
mbed_official 237:f3da66175598 227 /**
mbed_official 237:f3da66175598 228 * @brief DeInitializes the IRDA peripheral
mbed_official 237:f3da66175598 229 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 230 * @retval HAL status
mbed_official 237:f3da66175598 231 */
mbed_official 237:f3da66175598 232 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 233 {
mbed_official 237:f3da66175598 234 /* Check the IRDA handle allocation */
mbed_official 237:f3da66175598 235 if(hirda == NULL)
mbed_official 237:f3da66175598 236 {
mbed_official 237:f3da66175598 237 return HAL_ERROR;
mbed_official 237:f3da66175598 238 }
mbed_official 237:f3da66175598 239
mbed_official 237:f3da66175598 240 /* Check the USART/UART associated to the IRDA handle */
mbed_official 237:f3da66175598 241 assert_param(IS_IRDA_INSTANCE(hirda->Instance));
mbed_official 237:f3da66175598 242
mbed_official 237:f3da66175598 243 hirda->State = HAL_IRDA_STATE_BUSY;
mbed_official 237:f3da66175598 244
mbed_official 237:f3da66175598 245 /* DeInit the low level hardware */
mbed_official 237:f3da66175598 246 HAL_IRDA_MspDeInit(hirda);
mbed_official 237:f3da66175598 247 /* Disable the Peripheral */
mbed_official 237:f3da66175598 248 __HAL_IRDA_DISABLE(hirda);
mbed_official 237:f3da66175598 249
mbed_official 237:f3da66175598 250 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 251 hirda->State = HAL_IRDA_STATE_RESET;
mbed_official 237:f3da66175598 252
mbed_official 237:f3da66175598 253 /* Process Unlock */
mbed_official 237:f3da66175598 254 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 255
mbed_official 237:f3da66175598 256 return HAL_OK;
mbed_official 237:f3da66175598 257 }
mbed_official 237:f3da66175598 258
mbed_official 237:f3da66175598 259 /**
mbed_official 237:f3da66175598 260 * @brief IRDA MSP Init
mbed_official 237:f3da66175598 261 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 262 * @retval None
mbed_official 237:f3da66175598 263 */
mbed_official 237:f3da66175598 264 __weak void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 265 {
mbed_official 237:f3da66175598 266 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 267 the HAL_IRDA_MspInit can be implemented in the user file
mbed_official 237:f3da66175598 268 */
mbed_official 237:f3da66175598 269 }
mbed_official 237:f3da66175598 270
mbed_official 237:f3da66175598 271 /**
mbed_official 237:f3da66175598 272 * @brief IRDA MSP DeInit
mbed_official 237:f3da66175598 273 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 274 * @retval None
mbed_official 237:f3da66175598 275 */
mbed_official 237:f3da66175598 276 __weak void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 277 {
mbed_official 237:f3da66175598 278 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 279 the HAL_IRDA_MspDeInit can be implemented in the user file
mbed_official 237:f3da66175598 280 */
mbed_official 237:f3da66175598 281 }
mbed_official 237:f3da66175598 282
mbed_official 237:f3da66175598 283 /**
mbed_official 237:f3da66175598 284 * @}
mbed_official 237:f3da66175598 285 */
mbed_official 237:f3da66175598 286
mbed_official 237:f3da66175598 287 /** @defgroup HAL_IRDA_Group2 IO operation functions
mbed_official 237:f3da66175598 288 * @brief IRDA Transmit/Receive functions
mbed_official 237:f3da66175598 289 *
mbed_official 237:f3da66175598 290 @verbatim
mbed_official 237:f3da66175598 291 ===============================================================================
mbed_official 237:f3da66175598 292 ##### I/O operation functions #####
mbed_official 237:f3da66175598 293 ===============================================================================
mbed_official 237:f3da66175598 294 This subsection provides a set of functions allowing to manage the IRDA asynchronous
mbed_official 237:f3da66175598 295 data transfers.
mbed_official 237:f3da66175598 296
mbed_official 237:f3da66175598 297 (#) There are two modes of transfer:
mbed_official 237:f3da66175598 298 (+) Blocking mode: the communication is performed in polling mode.
mbed_official 237:f3da66175598 299 The HAL status of all data processing is returned by the same function
mbed_official 237:f3da66175598 300 after finishing transfer.
mbed_official 237:f3da66175598 301 (+) No-Blocking mode: the communication is performed using Interrupts
mbed_official 237:f3da66175598 302 or DMA, these API's return the HAL status.
mbed_official 237:f3da66175598 303 The end of the data processing will be indicated through the
mbed_official 237:f3da66175598 304 dedicated IRDA IRQ when using Interrupt mode or the DMA IRQ when
mbed_official 237:f3da66175598 305 using DMA mode.
mbed_official 237:f3da66175598 306 The HAL_IRDA_TxCpltCallback(), HAL_IRDA_RxCpltCallback() user callbacks
mbed_official 237:f3da66175598 307 will be executed respectivelly at the end of the Transmit or Receive process
mbed_official 237:f3da66175598 308 The HAL_IRDA_ErrorCallback() user callback will be executed when a communication error is detected
mbed_official 237:f3da66175598 309
mbed_official 237:f3da66175598 310 (#) Blocking mode API's are :
mbed_official 237:f3da66175598 311 (+) HAL_IRDA_Transmit()
mbed_official 237:f3da66175598 312 (+) HAL_IRDA_Receive()
mbed_official 237:f3da66175598 313
mbed_official 237:f3da66175598 314 (#) Non-Blocking mode API's with Interrupt are :
mbed_official 237:f3da66175598 315 (+) HAL_IRDA_Transmit_IT()
mbed_official 237:f3da66175598 316 (+) HAL_IRDA_Receive_IT()
mbed_official 237:f3da66175598 317 (+) HAL_IRDA_IRQHandler()
mbed_official 237:f3da66175598 318 (+) IRDA_Transmit_IT()
mbed_official 237:f3da66175598 319 (+) IRDA_Receive_IT()
mbed_official 237:f3da66175598 320
mbed_official 237:f3da66175598 321 (#) Non-Blocking mode functions with DMA are :
mbed_official 237:f3da66175598 322 (+) HAL_IRDA_Transmit_DMA()
mbed_official 237:f3da66175598 323 (+) HAL_IRDA_Receive_DMA()
mbed_official 237:f3da66175598 324
mbed_official 237:f3da66175598 325 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
mbed_official 237:f3da66175598 326 (+) HAL_IRDA_TxCpltCallback()
mbed_official 237:f3da66175598 327 (+) HAL_IRDA_RxCpltCallback()
mbed_official 237:f3da66175598 328 (+) HAL_IRDA_ErrorCallback()
mbed_official 237:f3da66175598 329
mbed_official 237:f3da66175598 330 @endverbatim
mbed_official 237:f3da66175598 331 * @{
mbed_official 237:f3da66175598 332 */
mbed_official 237:f3da66175598 333
mbed_official 237:f3da66175598 334 /**
mbed_official 237:f3da66175598 335 * @brief Send an amount of data in blocking mode
mbed_official 237:f3da66175598 336 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 337 * @param pData: pointer to data buffer
mbed_official 237:f3da66175598 338 * @param Size: amount of data to be sent
mbed_official 237:f3da66175598 339 * @param Timeout: Duration of the timeout
mbed_official 237:f3da66175598 340 * @retval HAL status
mbed_official 237:f3da66175598 341 */
mbed_official 237:f3da66175598 342 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
mbed_official 237:f3da66175598 343 {
mbed_official 237:f3da66175598 344 uint16_t* tmp;
mbed_official 237:f3da66175598 345
mbed_official 237:f3da66175598 346 if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
mbed_official 237:f3da66175598 347 {
mbed_official 237:f3da66175598 348 if((pData == NULL) || (Size == 0))
mbed_official 237:f3da66175598 349 {
mbed_official 237:f3da66175598 350 return HAL_ERROR;
mbed_official 237:f3da66175598 351 }
mbed_official 237:f3da66175598 352
mbed_official 237:f3da66175598 353 /* Process Locked */
mbed_official 237:f3da66175598 354 __HAL_LOCK(hirda);
mbed_official 237:f3da66175598 355 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 356
mbed_official 237:f3da66175598 357 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
mbed_official 237:f3da66175598 358 {
mbed_official 237:f3da66175598 359 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
mbed_official 237:f3da66175598 360 }
mbed_official 237:f3da66175598 361 else
mbed_official 237:f3da66175598 362 {
mbed_official 237:f3da66175598 363 hirda->State = HAL_IRDA_STATE_BUSY_TX;
mbed_official 237:f3da66175598 364 }
mbed_official 237:f3da66175598 365
mbed_official 237:f3da66175598 366 hirda->TxXferSize = Size;
mbed_official 237:f3da66175598 367 hirda->TxXferCount = Size;
mbed_official 237:f3da66175598 368 while(hirda->TxXferCount > 0)
mbed_official 237:f3da66175598 369 {
mbed_official 237:f3da66175598 370 hirda->TxXferCount--;
mbed_official 237:f3da66175598 371
mbed_official 237:f3da66175598 372 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TXE, RESET, Timeout) != HAL_OK)
mbed_official 237:f3da66175598 373 {
mbed_official 237:f3da66175598 374 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 375 }
mbed_official 237:f3da66175598 376 if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
mbed_official 237:f3da66175598 377 {
mbed_official 237:f3da66175598 378 tmp = (uint16_t*) pData;
mbed_official 237:f3da66175598 379 hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
mbed_official 237:f3da66175598 380 pData +=2;
mbed_official 237:f3da66175598 381 }
mbed_official 237:f3da66175598 382 else
mbed_official 237:f3da66175598 383 {
mbed_official 237:f3da66175598 384 hirda->Instance->TDR = (*pData++ & (uint8_t)0xFF);
mbed_official 237:f3da66175598 385 }
mbed_official 237:f3da66175598 386 }
mbed_official 237:f3da66175598 387
mbed_official 237:f3da66175598 388 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, Timeout) != HAL_OK)
mbed_official 237:f3da66175598 389 {
mbed_official 237:f3da66175598 390 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 391 }
mbed_official 237:f3da66175598 392
mbed_official 237:f3da66175598 393 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
mbed_official 237:f3da66175598 394 {
mbed_official 237:f3da66175598 395 hirda->State = HAL_IRDA_STATE_BUSY_RX;
mbed_official 237:f3da66175598 396 }
mbed_official 237:f3da66175598 397 else
mbed_official 237:f3da66175598 398 {
mbed_official 237:f3da66175598 399 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 400 }
mbed_official 237:f3da66175598 401
mbed_official 237:f3da66175598 402 /* Process Unlocked */
mbed_official 237:f3da66175598 403 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 404
mbed_official 237:f3da66175598 405 return HAL_OK;
mbed_official 237:f3da66175598 406 }
mbed_official 237:f3da66175598 407 else
mbed_official 237:f3da66175598 408 {
mbed_official 237:f3da66175598 409 return HAL_BUSY;
mbed_official 237:f3da66175598 410 }
mbed_official 237:f3da66175598 411 }
mbed_official 237:f3da66175598 412
mbed_official 237:f3da66175598 413 /**
mbed_official 237:f3da66175598 414 * @brief Receive an amount of data in blocking mode
mbed_official 237:f3da66175598 415 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 416 * @param pData: pointer to data buffer
mbed_official 237:f3da66175598 417 * @param Size: amount of data to be received
mbed_official 237:f3da66175598 418 * @param Timeout: Duration of the timeout
mbed_official 237:f3da66175598 419 * @retval HAL status
mbed_official 237:f3da66175598 420 */
mbed_official 237:f3da66175598 421 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout)
mbed_official 237:f3da66175598 422 {
mbed_official 237:f3da66175598 423 uint16_t* tmp;
mbed_official 237:f3da66175598 424 uint16_t uhMask;
mbed_official 237:f3da66175598 425
mbed_official 237:f3da66175598 426 if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
mbed_official 237:f3da66175598 427 {
mbed_official 237:f3da66175598 428 if((pData == NULL) || (Size == 0))
mbed_official 237:f3da66175598 429 {
mbed_official 237:f3da66175598 430 return HAL_ERROR;
mbed_official 237:f3da66175598 431 }
mbed_official 237:f3da66175598 432
mbed_official 237:f3da66175598 433 /* Process Locked */
mbed_official 237:f3da66175598 434 __HAL_LOCK(hirda);
mbed_official 237:f3da66175598 435 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 436
mbed_official 237:f3da66175598 437 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
mbed_official 237:f3da66175598 438 {
mbed_official 237:f3da66175598 439 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
mbed_official 237:f3da66175598 440 }
mbed_official 237:f3da66175598 441 else
mbed_official 237:f3da66175598 442 {
mbed_official 237:f3da66175598 443 hirda->State = HAL_IRDA_STATE_BUSY_RX;
mbed_official 237:f3da66175598 444 }
mbed_official 237:f3da66175598 445
mbed_official 237:f3da66175598 446 hirda->RxXferSize = Size;
mbed_official 237:f3da66175598 447 hirda->RxXferCount = Size;
mbed_official 237:f3da66175598 448
mbed_official 237:f3da66175598 449 /* Computation of the mask to apply to the RDR register
mbed_official 237:f3da66175598 450 of the UART associated to the IRDA */
mbed_official 237:f3da66175598 451 __HAL_IRDA_MASK_COMPUTATION(hirda);
mbed_official 237:f3da66175598 452 uhMask = hirda->Mask;
mbed_official 237:f3da66175598 453
mbed_official 237:f3da66175598 454 /* Check data remaining to be received */
mbed_official 237:f3da66175598 455 while(hirda->RxXferCount > 0)
mbed_official 237:f3da66175598 456 {
mbed_official 237:f3da66175598 457 hirda->RxXferCount--;
mbed_official 237:f3da66175598 458
mbed_official 237:f3da66175598 459 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_RXNE, RESET, Timeout) != HAL_OK)
mbed_official 237:f3da66175598 460 {
mbed_official 237:f3da66175598 461 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 462 }
mbed_official 237:f3da66175598 463 if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
mbed_official 237:f3da66175598 464 {
mbed_official 237:f3da66175598 465 tmp = (uint16_t*) pData ;
mbed_official 237:f3da66175598 466 *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
mbed_official 237:f3da66175598 467 pData +=2;
mbed_official 237:f3da66175598 468 }
mbed_official 237:f3da66175598 469 else
mbed_official 237:f3da66175598 470 {
mbed_official 237:f3da66175598 471 *pData++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
mbed_official 237:f3da66175598 472 }
mbed_official 237:f3da66175598 473 }
mbed_official 237:f3da66175598 474
mbed_official 237:f3da66175598 475 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
mbed_official 237:f3da66175598 476 {
mbed_official 237:f3da66175598 477 hirda->State = HAL_IRDA_STATE_BUSY_TX;
mbed_official 237:f3da66175598 478 }
mbed_official 237:f3da66175598 479 else
mbed_official 237:f3da66175598 480 {
mbed_official 237:f3da66175598 481 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 482 }
mbed_official 237:f3da66175598 483
mbed_official 237:f3da66175598 484 /* Process Unlocked */
mbed_official 237:f3da66175598 485 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 486
mbed_official 237:f3da66175598 487 return HAL_OK;
mbed_official 237:f3da66175598 488 }
mbed_official 237:f3da66175598 489 else
mbed_official 237:f3da66175598 490 {
mbed_official 237:f3da66175598 491 return HAL_BUSY;
mbed_official 237:f3da66175598 492 }
mbed_official 237:f3da66175598 493 }
mbed_official 237:f3da66175598 494
mbed_official 237:f3da66175598 495 /**
mbed_official 237:f3da66175598 496 * @brief Send an amount of data in interrupt mode
mbed_official 237:f3da66175598 497 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 498 * @param pData: pointer to data buffer
mbed_official 237:f3da66175598 499 * @param Size: amount of data to be sent
mbed_official 237:f3da66175598 500 * @retval HAL status
mbed_official 237:f3da66175598 501 */
mbed_official 237:f3da66175598 502 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
mbed_official 237:f3da66175598 503 {
mbed_official 237:f3da66175598 504 if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
mbed_official 237:f3da66175598 505 {
mbed_official 237:f3da66175598 506 if((pData == NULL) || (Size == 0))
mbed_official 237:f3da66175598 507 {
mbed_official 237:f3da66175598 508 return HAL_ERROR;
mbed_official 237:f3da66175598 509 }
mbed_official 237:f3da66175598 510
mbed_official 237:f3da66175598 511 /* Process Locked */
mbed_official 237:f3da66175598 512 __HAL_LOCK(hirda);
mbed_official 237:f3da66175598 513
mbed_official 237:f3da66175598 514 hirda->pTxBuffPtr = pData;
mbed_official 237:f3da66175598 515 hirda->TxXferSize = Size;
mbed_official 237:f3da66175598 516 hirda->TxXferCount = Size;
mbed_official 237:f3da66175598 517
mbed_official 237:f3da66175598 518 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 519 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
mbed_official 237:f3da66175598 520 {
mbed_official 237:f3da66175598 521 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
mbed_official 237:f3da66175598 522 }
mbed_official 237:f3da66175598 523 else
mbed_official 237:f3da66175598 524 {
mbed_official 237:f3da66175598 525 hirda->State = HAL_IRDA_STATE_BUSY_TX;
mbed_official 237:f3da66175598 526 }
mbed_official 237:f3da66175598 527
mbed_official 237:f3da66175598 528 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 237:f3da66175598 529 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
mbed_official 237:f3da66175598 530
mbed_official 237:f3da66175598 531 /* Process Unlocked */
mbed_official 237:f3da66175598 532 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 533
mbed_official 237:f3da66175598 534 /* Enable the IRDA Transmit Data Register Empty Interrupt */
mbed_official 237:f3da66175598 535 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_TXE);
mbed_official 237:f3da66175598 536
mbed_official 237:f3da66175598 537 return HAL_OK;
mbed_official 237:f3da66175598 538 }
mbed_official 237:f3da66175598 539 else
mbed_official 237:f3da66175598 540 {
mbed_official 237:f3da66175598 541 return HAL_BUSY;
mbed_official 237:f3da66175598 542 }
mbed_official 237:f3da66175598 543 }
mbed_official 237:f3da66175598 544
mbed_official 237:f3da66175598 545 /**
mbed_official 237:f3da66175598 546 * @brief Receive an amount of data in interrupt mode
mbed_official 237:f3da66175598 547 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 548 * @param pData: pointer to data buffer
mbed_official 237:f3da66175598 549 * @param Size: amount of data to be received
mbed_official 237:f3da66175598 550 * @retval HAL status
mbed_official 237:f3da66175598 551 */
mbed_official 237:f3da66175598 552 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
mbed_official 237:f3da66175598 553 {
mbed_official 237:f3da66175598 554 if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
mbed_official 237:f3da66175598 555 {
mbed_official 237:f3da66175598 556 if((pData == NULL) || (Size == 0))
mbed_official 237:f3da66175598 557 {
mbed_official 237:f3da66175598 558 return HAL_ERROR;
mbed_official 237:f3da66175598 559 }
mbed_official 237:f3da66175598 560
mbed_official 237:f3da66175598 561 /* Process Locked */
mbed_official 237:f3da66175598 562 __HAL_LOCK(hirda);
mbed_official 237:f3da66175598 563
mbed_official 237:f3da66175598 564 hirda->pRxBuffPtr = pData;
mbed_official 237:f3da66175598 565 hirda->RxXferSize = Size;
mbed_official 237:f3da66175598 566 hirda->RxXferCount = Size;
mbed_official 237:f3da66175598 567
mbed_official 237:f3da66175598 568 /* Computation of the mask to apply to the RDR register
mbed_official 237:f3da66175598 569 of the UART associated to the IRDA */
mbed_official 237:f3da66175598 570 __HAL_IRDA_MASK_COMPUTATION(hirda);
mbed_official 237:f3da66175598 571
mbed_official 237:f3da66175598 572 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 573 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
mbed_official 237:f3da66175598 574 {
mbed_official 237:f3da66175598 575 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
mbed_official 237:f3da66175598 576 }
mbed_official 237:f3da66175598 577 else
mbed_official 237:f3da66175598 578 {
mbed_official 237:f3da66175598 579 hirda->State = HAL_IRDA_STATE_BUSY_RX;
mbed_official 237:f3da66175598 580 }
mbed_official 237:f3da66175598 581
mbed_official 237:f3da66175598 582 /* Enable the IRDA Parity Error Interrupt */
mbed_official 237:f3da66175598 583 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_PE);
mbed_official 237:f3da66175598 584
mbed_official 237:f3da66175598 585 /* Enable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 237:f3da66175598 586 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_ERR);
mbed_official 237:f3da66175598 587
mbed_official 237:f3da66175598 588 /* Process Unlocked */
mbed_official 237:f3da66175598 589 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 590
mbed_official 237:f3da66175598 591 /* Enable the IRDA Data Register not empty Interrupt */
mbed_official 237:f3da66175598 592 __HAL_IRDA_ENABLE_IT(hirda, IRDA_IT_RXNE);
mbed_official 237:f3da66175598 593
mbed_official 237:f3da66175598 594 return HAL_OK;
mbed_official 237:f3da66175598 595 }
mbed_official 237:f3da66175598 596 else
mbed_official 237:f3da66175598 597 {
mbed_official 237:f3da66175598 598 return HAL_BUSY;
mbed_official 237:f3da66175598 599 }
mbed_official 237:f3da66175598 600 }
mbed_official 237:f3da66175598 601
mbed_official 237:f3da66175598 602 /**
mbed_official 237:f3da66175598 603 * @brief Send an amount of data in DMA mode
mbed_official 237:f3da66175598 604 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 605 * @param pData: pointer to data buffer
mbed_official 237:f3da66175598 606 * @param Size: amount of data to be sent
mbed_official 237:f3da66175598 607 * @retval HAL status
mbed_official 237:f3da66175598 608 */
mbed_official 237:f3da66175598 609 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
mbed_official 237:f3da66175598 610 {
mbed_official 237:f3da66175598 611 uint32_t *tmp;
mbed_official 237:f3da66175598 612
mbed_official 237:f3da66175598 613 if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_RX))
mbed_official 237:f3da66175598 614 {
mbed_official 237:f3da66175598 615 if((pData == NULL) || (Size == 0))
mbed_official 237:f3da66175598 616 {
mbed_official 237:f3da66175598 617 return HAL_ERROR;
mbed_official 237:f3da66175598 618 }
mbed_official 237:f3da66175598 619
mbed_official 237:f3da66175598 620 /* Process Locked */
mbed_official 237:f3da66175598 621 __HAL_LOCK(hirda);
mbed_official 237:f3da66175598 622
mbed_official 237:f3da66175598 623 hirda->pTxBuffPtr = pData;
mbed_official 237:f3da66175598 624 hirda->TxXferSize = Size;
mbed_official 237:f3da66175598 625 hirda->TxXferCount = Size;
mbed_official 237:f3da66175598 626
mbed_official 237:f3da66175598 627 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 628
mbed_official 237:f3da66175598 629 if(hirda->State == HAL_IRDA_STATE_BUSY_RX)
mbed_official 237:f3da66175598 630 {
mbed_official 237:f3da66175598 631 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
mbed_official 237:f3da66175598 632 }
mbed_official 237:f3da66175598 633 else
mbed_official 237:f3da66175598 634 {
mbed_official 237:f3da66175598 635 hirda->State = HAL_IRDA_STATE_BUSY_TX;
mbed_official 237:f3da66175598 636 }
mbed_official 237:f3da66175598 637
mbed_official 237:f3da66175598 638 /* Set the IRDA DMA transfer complete callback */
mbed_official 237:f3da66175598 639 hirda->hdmatx->XferCpltCallback = IRDA_DMATransmitCplt;
mbed_official 237:f3da66175598 640
mbed_official 237:f3da66175598 641 /* Set the DMA error callback */
mbed_official 237:f3da66175598 642 hirda->hdmatx->XferErrorCallback = IRDA_DMAError;
mbed_official 237:f3da66175598 643
mbed_official 237:f3da66175598 644 /* Enable the IRDA transmit DMA channel */
mbed_official 237:f3da66175598 645 tmp = (uint32_t*)&pData;
mbed_official 237:f3da66175598 646 HAL_DMA_Start_IT(hirda->hdmatx, *(uint32_t*)tmp, (uint32_t)&hirda->Instance->TDR, Size);
mbed_official 237:f3da66175598 647
mbed_official 237:f3da66175598 648 /* Enable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 237:f3da66175598 649 in the IRDA CR3 register */
mbed_official 237:f3da66175598 650 hirda->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 237:f3da66175598 651
mbed_official 237:f3da66175598 652 /* Process Unlocked */
mbed_official 237:f3da66175598 653 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 654
mbed_official 237:f3da66175598 655 return HAL_OK;
mbed_official 237:f3da66175598 656 }
mbed_official 237:f3da66175598 657 else
mbed_official 237:f3da66175598 658 {
mbed_official 237:f3da66175598 659 return HAL_BUSY;
mbed_official 237:f3da66175598 660 }
mbed_official 237:f3da66175598 661 }
mbed_official 237:f3da66175598 662
mbed_official 237:f3da66175598 663 /**
mbed_official 237:f3da66175598 664 * @brief Receive an amount of data in DMA mode
mbed_official 237:f3da66175598 665 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 666 * @param pData: pointer to data buffer
mbed_official 237:f3da66175598 667 * @param Size: amount of data to be received
mbed_official 237:f3da66175598 668 * @note When the IRDA parity is enabled (PCE = 1), the received data contain
mbed_official 237:f3da66175598 669 * the parity bit (MSB position)
mbed_official 237:f3da66175598 670 * @retval HAL status
mbed_official 237:f3da66175598 671 */
mbed_official 237:f3da66175598 672 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size)
mbed_official 237:f3da66175598 673 {
mbed_official 237:f3da66175598 674 uint32_t *tmp;
mbed_official 237:f3da66175598 675
mbed_official 237:f3da66175598 676 if ((hirda->State == HAL_IRDA_STATE_READY) || (hirda->State == HAL_IRDA_STATE_BUSY_TX))
mbed_official 237:f3da66175598 677 {
mbed_official 237:f3da66175598 678 if((pData == NULL) || (Size == 0))
mbed_official 237:f3da66175598 679 {
mbed_official 237:f3da66175598 680 return HAL_ERROR;
mbed_official 237:f3da66175598 681 }
mbed_official 237:f3da66175598 682
mbed_official 237:f3da66175598 683 /* Process Locked */
mbed_official 237:f3da66175598 684 __HAL_LOCK(hirda);
mbed_official 237:f3da66175598 685
mbed_official 237:f3da66175598 686 hirda->pRxBuffPtr = pData;
mbed_official 237:f3da66175598 687 hirda->RxXferSize = Size;
mbed_official 237:f3da66175598 688
mbed_official 237:f3da66175598 689 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 690 if(hirda->State == HAL_IRDA_STATE_BUSY_TX)
mbed_official 237:f3da66175598 691 {
mbed_official 237:f3da66175598 692 hirda->State = HAL_IRDA_STATE_BUSY_TX_RX;
mbed_official 237:f3da66175598 693 }
mbed_official 237:f3da66175598 694 else
mbed_official 237:f3da66175598 695 {
mbed_official 237:f3da66175598 696 hirda->State = HAL_IRDA_STATE_BUSY_RX;
mbed_official 237:f3da66175598 697 }
mbed_official 237:f3da66175598 698
mbed_official 237:f3da66175598 699 /* Set the IRDA DMA transfer complete callback */
mbed_official 237:f3da66175598 700 hirda->hdmarx->XferCpltCallback = IRDA_DMAReceiveCplt;
mbed_official 237:f3da66175598 701
mbed_official 237:f3da66175598 702 /* Set the DMA error callback */
mbed_official 237:f3da66175598 703 hirda->hdmarx->XferErrorCallback = IRDA_DMAError;
mbed_official 237:f3da66175598 704
mbed_official 237:f3da66175598 705 /* Enable the DMA channel */
mbed_official 237:f3da66175598 706 tmp = (uint32_t*)&pData;
mbed_official 237:f3da66175598 707 HAL_DMA_Start_IT(hirda->hdmarx, (uint32_t)&hirda->Instance->RDR, *(uint32_t*)tmp, Size);
mbed_official 237:f3da66175598 708
mbed_official 237:f3da66175598 709 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
mbed_official 237:f3da66175598 710 in the IRDA CR3 register */
mbed_official 237:f3da66175598 711 hirda->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 237:f3da66175598 712
mbed_official 237:f3da66175598 713 /* Process Unlocked */
mbed_official 237:f3da66175598 714 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 715
mbed_official 237:f3da66175598 716 return HAL_OK;
mbed_official 237:f3da66175598 717 }
mbed_official 237:f3da66175598 718 else
mbed_official 237:f3da66175598 719 {
mbed_official 237:f3da66175598 720 return HAL_BUSY;
mbed_official 237:f3da66175598 721 }
mbed_official 237:f3da66175598 722 }
mbed_official 237:f3da66175598 723
mbed_official 237:f3da66175598 724 /**
mbed_official 237:f3da66175598 725 * @brief This function handles IRDA interrupt request.
mbed_official 237:f3da66175598 726 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 727 * @retval None
mbed_official 237:f3da66175598 728 */
mbed_official 237:f3da66175598 729 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 730 {
mbed_official 237:f3da66175598 731 /* IRDA parity error interrupt occurred -------------------------------------*/
mbed_official 237:f3da66175598 732 if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_PE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_PE) != RESET))
mbed_official 237:f3da66175598 733 {
mbed_official 237:f3da66175598 734 __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_PEF);
mbed_official 237:f3da66175598 735
mbed_official 237:f3da66175598 736 hirda->ErrorCode |= HAL_IRDA_ERROR_PE;
mbed_official 237:f3da66175598 737 /* Set the IRDA state ready to be able to start again the process */
mbed_official 237:f3da66175598 738 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 739 }
mbed_official 237:f3da66175598 740
mbed_official 237:f3da66175598 741 /* IRDA frame error interrupt occured --------------------------------------*/
mbed_official 237:f3da66175598 742 if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_FE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
mbed_official 237:f3da66175598 743 {
mbed_official 237:f3da66175598 744 __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_FEF);
mbed_official 237:f3da66175598 745
mbed_official 237:f3da66175598 746 hirda->ErrorCode |= HAL_IRDA_ERROR_FE;
mbed_official 237:f3da66175598 747 /* Set the IRDA state ready to be able to start again the process */
mbed_official 237:f3da66175598 748 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 749 }
mbed_official 237:f3da66175598 750
mbed_official 237:f3da66175598 751 /* IRDA noise error interrupt occured --------------------------------------*/
mbed_official 237:f3da66175598 752 if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_NE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
mbed_official 237:f3da66175598 753 {
mbed_official 237:f3da66175598 754 __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_NEF);
mbed_official 237:f3da66175598 755
mbed_official 237:f3da66175598 756 hirda->ErrorCode |= HAL_IRDA_ERROR_NE;
mbed_official 237:f3da66175598 757 /* Set the IRDA state ready to be able to start again the process */
mbed_official 237:f3da66175598 758 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 759 }
mbed_official 237:f3da66175598 760
mbed_official 237:f3da66175598 761 /* IRDA Over-Run interrupt occured -----------------------------------------*/
mbed_official 237:f3da66175598 762 if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_ORE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_ERR) != RESET))
mbed_official 237:f3da66175598 763 {
mbed_official 237:f3da66175598 764 __HAL_IRDA_CLEAR_IT(hirda, IRDA_CLEAR_OREF);
mbed_official 237:f3da66175598 765
mbed_official 237:f3da66175598 766 hirda->ErrorCode |= HAL_IRDA_ERROR_ORE;
mbed_official 237:f3da66175598 767 /* Set the IRDA state ready to be able to start again the process */
mbed_official 237:f3da66175598 768 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 769 }
mbed_official 237:f3da66175598 770
mbed_official 237:f3da66175598 771 /* Call IRDA Error Call back function if need be --------------------------*/
mbed_official 237:f3da66175598 772 if(hirda->ErrorCode != HAL_IRDA_ERROR_NONE)
mbed_official 237:f3da66175598 773 {
mbed_official 237:f3da66175598 774 HAL_IRDA_ErrorCallback(hirda);
mbed_official 237:f3da66175598 775 }
mbed_official 237:f3da66175598 776
mbed_official 237:f3da66175598 777 /* IRDA in mode Receiver ---------------------------------------------------*/
mbed_official 237:f3da66175598 778 if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_RXNE) != RESET) && (__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_RXNE) != RESET))
mbed_official 237:f3da66175598 779 {
mbed_official 237:f3da66175598 780 IRDA_Receive_IT(hirda);
mbed_official 237:f3da66175598 781 /* Clear RXNE interrupt flag */
mbed_official 237:f3da66175598 782 __HAL_IRDA_SEND_REQ(hirda, IRDA_RXDATA_FLUSH_REQUEST);
mbed_official 237:f3da66175598 783 }
mbed_official 237:f3da66175598 784
mbed_official 237:f3da66175598 785
mbed_official 237:f3da66175598 786 /* IRDA in mode Transmitter ------------------------------------------------*/
mbed_official 237:f3da66175598 787 if((__HAL_IRDA_GET_IT(hirda, IRDA_IT_TXE) != RESET) &&(__HAL_IRDA_GET_IT_SOURCE(hirda, IRDA_IT_TXE) != RESET))
mbed_official 237:f3da66175598 788 {
mbed_official 237:f3da66175598 789 IRDA_Transmit_IT(hirda);
mbed_official 237:f3da66175598 790 }
mbed_official 237:f3da66175598 791
mbed_official 237:f3da66175598 792 }
mbed_official 237:f3da66175598 793
mbed_official 237:f3da66175598 794 /**
mbed_official 237:f3da66175598 795 * @brief DMA IRDA Tx transfer completed callback
mbed_official 237:f3da66175598 796 * @param hdma: DMA handle
mbed_official 237:f3da66175598 797 * @retval None
mbed_official 237:f3da66175598 798 */
mbed_official 237:f3da66175598 799 static void IRDA_DMATransmitCplt(DMA_HandleTypeDef *hdma)
mbed_official 237:f3da66175598 800 {
mbed_official 237:f3da66175598 801 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 237:f3da66175598 802 hirda->TxXferCount = 0;
mbed_official 237:f3da66175598 803
mbed_official 237:f3da66175598 804 /* Disable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 237:f3da66175598 805 in the IRDA CR3 register */
mbed_official 237:f3da66175598 806 hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
mbed_official 237:f3da66175598 807
mbed_official 237:f3da66175598 808 /* Wait for IRDA TC Flag */
mbed_official 237:f3da66175598 809 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_FLAG_TC, RESET, IRDA_TXDMA_TIMEOUTVALUE) != HAL_OK)
mbed_official 237:f3da66175598 810 {
mbed_official 237:f3da66175598 811 /* Timeout Occured */
mbed_official 237:f3da66175598 812 HAL_IRDA_ErrorCallback(hirda);
mbed_official 237:f3da66175598 813 }
mbed_official 237:f3da66175598 814 else
mbed_official 237:f3da66175598 815 {
mbed_official 237:f3da66175598 816 /* No Timeout */
mbed_official 237:f3da66175598 817 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
mbed_official 237:f3da66175598 818 {
mbed_official 237:f3da66175598 819 hirda->State = HAL_IRDA_STATE_BUSY_RX;
mbed_official 237:f3da66175598 820 }
mbed_official 237:f3da66175598 821 else
mbed_official 237:f3da66175598 822 {
mbed_official 237:f3da66175598 823 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 824 }
mbed_official 237:f3da66175598 825 HAL_IRDA_TxCpltCallback(hirda);
mbed_official 237:f3da66175598 826 }
mbed_official 237:f3da66175598 827 }
mbed_official 237:f3da66175598 828
mbed_official 237:f3da66175598 829 /**
mbed_official 237:f3da66175598 830 * @brief DMA IRDA Rx Transfer completed callback
mbed_official 237:f3da66175598 831 * @param hdma: DMA handle
mbed_official 237:f3da66175598 832 * @retval None
mbed_official 237:f3da66175598 833 */
mbed_official 237:f3da66175598 834 static void IRDA_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
mbed_official 237:f3da66175598 835 {
mbed_official 237:f3da66175598 836 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 237:f3da66175598 837 hirda->RxXferCount = 0;
mbed_official 237:f3da66175598 838
mbed_official 237:f3da66175598 839 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
mbed_official 237:f3da66175598 840 in the IRDA CR3 register */
mbed_official 237:f3da66175598 841 hirda->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
mbed_official 237:f3da66175598 842
mbed_official 237:f3da66175598 843 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
mbed_official 237:f3da66175598 844 {
mbed_official 237:f3da66175598 845 hirda->State = HAL_IRDA_STATE_BUSY_TX;
mbed_official 237:f3da66175598 846 }
mbed_official 237:f3da66175598 847 else
mbed_official 237:f3da66175598 848 {
mbed_official 237:f3da66175598 849 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 850 }
mbed_official 237:f3da66175598 851
mbed_official 237:f3da66175598 852 HAL_IRDA_RxCpltCallback(hirda);
mbed_official 237:f3da66175598 853 }
mbed_official 237:f3da66175598 854
mbed_official 237:f3da66175598 855 /**
mbed_official 237:f3da66175598 856 * @brief DMA IRDA communication error callback
mbed_official 237:f3da66175598 857 * @param hdma: DMA handle
mbed_official 237:f3da66175598 858 * @retval None
mbed_official 237:f3da66175598 859 */
mbed_official 237:f3da66175598 860 static void IRDA_DMAError(DMA_HandleTypeDef *hdma)
mbed_official 237:f3da66175598 861 {
mbed_official 237:f3da66175598 862 IRDA_HandleTypeDef* hirda = ( IRDA_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 237:f3da66175598 863 hirda->RxXferCount = 0;
mbed_official 237:f3da66175598 864 hirda->TxXferCount = 0;
mbed_official 237:f3da66175598 865 hirda->State= HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 866 hirda->ErrorCode |= HAL_IRDA_ERROR_DMA;
mbed_official 237:f3da66175598 867 HAL_IRDA_ErrorCallback(hirda);
mbed_official 237:f3da66175598 868 }
mbed_official 237:f3da66175598 869
mbed_official 237:f3da66175598 870 /**
mbed_official 237:f3da66175598 871 * @brief Tx Transfer completed callback
mbed_official 237:f3da66175598 872 * @param hirda: irda handle
mbed_official 237:f3da66175598 873 * @retval None
mbed_official 237:f3da66175598 874 */
mbed_official 237:f3da66175598 875 __weak void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 876 {
mbed_official 237:f3da66175598 877 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 878 the HAL_IRDA_TxCpltCallback can be implemented in the user file
mbed_official 237:f3da66175598 879 */
mbed_official 237:f3da66175598 880 }
mbed_official 237:f3da66175598 881
mbed_official 237:f3da66175598 882 /**
mbed_official 237:f3da66175598 883 * @brief Rx Transfer completed callback
mbed_official 237:f3da66175598 884 * @param hirda: irda handle
mbed_official 237:f3da66175598 885 * @retval None
mbed_official 237:f3da66175598 886 */
mbed_official 237:f3da66175598 887 __weak void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 888 {
mbed_official 237:f3da66175598 889 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 890 the HAL_IRDA_TxCpltCallback can be implemented in the user file
mbed_official 237:f3da66175598 891 */
mbed_official 237:f3da66175598 892 }
mbed_official 237:f3da66175598 893
mbed_official 237:f3da66175598 894 /**
mbed_official 237:f3da66175598 895 * @brief IRDA error callback
mbed_official 237:f3da66175598 896 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 897 * @retval None
mbed_official 237:f3da66175598 898 */
mbed_official 237:f3da66175598 899 __weak void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 900 {
mbed_official 237:f3da66175598 901 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 237:f3da66175598 902 the HAL_IRDA_ErrorCallback can be implemented in the user file
mbed_official 237:f3da66175598 903 */
mbed_official 237:f3da66175598 904 }
mbed_official 237:f3da66175598 905
mbed_official 237:f3da66175598 906 /**
mbed_official 237:f3da66175598 907 * @brief Receive an amount of data in non blocking mode.
mbed_official 237:f3da66175598 908 * Function called under interruption only, once
mbed_official 237:f3da66175598 909 * interruptions have been enabled by HAL_IRDA_Transmit_IT()
mbed_official 237:f3da66175598 910 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 911 * @retval HAL status
mbed_official 237:f3da66175598 912 */
mbed_official 237:f3da66175598 913 static HAL_StatusTypeDef IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 914 {
mbed_official 237:f3da66175598 915 uint16_t* tmp;
mbed_official 237:f3da66175598 916
mbed_official 237:f3da66175598 917 if((hirda->State == HAL_IRDA_STATE_BUSY_TX) || (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX))
mbed_official 237:f3da66175598 918 {
mbed_official 237:f3da66175598 919
mbed_official 237:f3da66175598 920 if(hirda->TxXferCount == 0)
mbed_official 237:f3da66175598 921 {
mbed_official 237:f3da66175598 922 /* Disable the IRDA Transmit Data Register Empty Interrupt */
mbed_official 237:f3da66175598 923 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
mbed_official 237:f3da66175598 924
mbed_official 237:f3da66175598 925 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
mbed_official 237:f3da66175598 926 {
mbed_official 237:f3da66175598 927 hirda->State = HAL_IRDA_STATE_BUSY_RX;
mbed_official 237:f3da66175598 928 }
mbed_official 237:f3da66175598 929 else
mbed_official 237:f3da66175598 930 {
mbed_official 237:f3da66175598 931 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 237:f3da66175598 932 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
mbed_official 237:f3da66175598 933
mbed_official 237:f3da66175598 934 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 935 }
mbed_official 237:f3da66175598 936
mbed_official 237:f3da66175598 937 /* Wait on TC flag to be able to start a second transfer */
mbed_official 237:f3da66175598 938 if(IRDA_WaitOnFlagUntilTimeout(hirda, IRDA_IT_TC, RESET, IRDA_TIMEOUT_VALUE) != HAL_OK)
mbed_official 237:f3da66175598 939 {
mbed_official 237:f3da66175598 940 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 941 }
mbed_official 237:f3da66175598 942
mbed_official 237:f3da66175598 943 HAL_IRDA_TxCpltCallback(hirda);
mbed_official 237:f3da66175598 944
mbed_official 237:f3da66175598 945 return HAL_OK;
mbed_official 237:f3da66175598 946 }
mbed_official 237:f3da66175598 947 else
mbed_official 237:f3da66175598 948 {
mbed_official 237:f3da66175598 949 if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
mbed_official 237:f3da66175598 950 {
mbed_official 237:f3da66175598 951 tmp = (uint16_t*) hirda->pTxBuffPtr;
mbed_official 237:f3da66175598 952 hirda->Instance->TDR = (*tmp & (uint16_t)0x01FF);
mbed_official 237:f3da66175598 953 hirda->pTxBuffPtr += 2;
mbed_official 237:f3da66175598 954 }
mbed_official 237:f3da66175598 955 else
mbed_official 237:f3da66175598 956 {
mbed_official 237:f3da66175598 957 hirda->Instance->TDR = (uint8_t)(*hirda->pTxBuffPtr++ & (uint8_t)0xFF);
mbed_official 237:f3da66175598 958 }
mbed_official 237:f3da66175598 959 hirda->TxXferCount--;
mbed_official 237:f3da66175598 960
mbed_official 237:f3da66175598 961 return HAL_OK;
mbed_official 237:f3da66175598 962 }
mbed_official 237:f3da66175598 963 }
mbed_official 237:f3da66175598 964 else
mbed_official 237:f3da66175598 965 {
mbed_official 237:f3da66175598 966 return HAL_BUSY;
mbed_official 237:f3da66175598 967 }
mbed_official 237:f3da66175598 968 }
mbed_official 237:f3da66175598 969
mbed_official 237:f3da66175598 970 /**
mbed_official 237:f3da66175598 971 * @brief Receive an amount of data in non blocking mode.
mbed_official 237:f3da66175598 972 * Function called under interruption only, once
mbed_official 237:f3da66175598 973 * interruptions have been enabled by HAL_IRDA_Receive_IT()
mbed_official 237:f3da66175598 974 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 975 * @retval HAL status
mbed_official 237:f3da66175598 976 */
mbed_official 237:f3da66175598 977 static HAL_StatusTypeDef IRDA_Receive_IT(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 978 {
mbed_official 237:f3da66175598 979 uint16_t* tmp;
mbed_official 237:f3da66175598 980 uint16_t uhMask = hirda->Mask;
mbed_official 237:f3da66175598 981
mbed_official 237:f3da66175598 982 if ((hirda->State == HAL_IRDA_STATE_BUSY_RX) || (hirda->State == HAL_IRDA_STATE_BUSY_TX_RX))
mbed_official 237:f3da66175598 983 {
mbed_official 237:f3da66175598 984
mbed_official 237:f3da66175598 985 if ((hirda->Init.WordLength == IRDA_WORDLENGTH_9B) && (hirda->Init.Parity == IRDA_PARITY_NONE))
mbed_official 237:f3da66175598 986 {
mbed_official 237:f3da66175598 987 tmp = (uint16_t*) hirda->pRxBuffPtr ;
mbed_official 237:f3da66175598 988 *tmp = (uint16_t)(hirda->Instance->RDR & uhMask);
mbed_official 237:f3da66175598 989 hirda->pRxBuffPtr +=2;
mbed_official 237:f3da66175598 990 }
mbed_official 237:f3da66175598 991 else
mbed_official 237:f3da66175598 992 {
mbed_official 237:f3da66175598 993 *hirda->pRxBuffPtr++ = (uint8_t)(hirda->Instance->RDR & (uint8_t)uhMask);
mbed_official 237:f3da66175598 994 }
mbed_official 237:f3da66175598 995
mbed_official 237:f3da66175598 996 if(--hirda->RxXferCount == 0)
mbed_official 237:f3da66175598 997 {
mbed_official 237:f3da66175598 998 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
mbed_official 237:f3da66175598 999
mbed_official 237:f3da66175598 1000 if(hirda->State == HAL_IRDA_STATE_BUSY_TX_RX)
mbed_official 237:f3da66175598 1001 {
mbed_official 237:f3da66175598 1002 hirda->State = HAL_IRDA_STATE_BUSY_TX;
mbed_official 237:f3da66175598 1003 }
mbed_official 237:f3da66175598 1004 else
mbed_official 237:f3da66175598 1005 {
mbed_official 237:f3da66175598 1006 /* Disable the IRDA Parity Error Interrupt */
mbed_official 237:f3da66175598 1007 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
mbed_official 237:f3da66175598 1008
mbed_official 237:f3da66175598 1009 /* Disable the IRDA Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 237:f3da66175598 1010 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
mbed_official 237:f3da66175598 1011
mbed_official 237:f3da66175598 1012 hirda->State = HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 1013 }
mbed_official 237:f3da66175598 1014
mbed_official 237:f3da66175598 1015 HAL_IRDA_RxCpltCallback(hirda);
mbed_official 237:f3da66175598 1016
mbed_official 237:f3da66175598 1017 return HAL_OK;
mbed_official 237:f3da66175598 1018 }
mbed_official 237:f3da66175598 1019
mbed_official 237:f3da66175598 1020 return HAL_OK;
mbed_official 237:f3da66175598 1021 }
mbed_official 237:f3da66175598 1022 else
mbed_official 237:f3da66175598 1023 {
mbed_official 237:f3da66175598 1024 return HAL_BUSY;
mbed_official 237:f3da66175598 1025 }
mbed_official 237:f3da66175598 1026 }
mbed_official 237:f3da66175598 1027
mbed_official 237:f3da66175598 1028 /**
mbed_official 237:f3da66175598 1029 * @}
mbed_official 237:f3da66175598 1030 */
mbed_official 237:f3da66175598 1031
mbed_official 237:f3da66175598 1032 /** @defgroup HAL_IRDA_Group3 Peripheral Control functions
mbed_official 237:f3da66175598 1033 * @brief IRDA control functions
mbed_official 237:f3da66175598 1034 *
mbed_official 237:f3da66175598 1035 @verbatim
mbed_official 237:f3da66175598 1036 ===============================================================================
mbed_official 237:f3da66175598 1037 ##### Peripheral Control functions #####
mbed_official 237:f3da66175598 1038 ===============================================================================
mbed_official 237:f3da66175598 1039 [..]
mbed_official 237:f3da66175598 1040 This subsection provides a set of functions allowing to control the IRDA.
mbed_official 237:f3da66175598 1041 (+) HAL_IRDA_GetState() API can be helpful to check in run-time the state of the IRDA peripheral.
mbed_official 237:f3da66175598 1042 (+) IRDA_SetConfig() API is used to configure the IRDA communications parameters.
mbed_official 237:f3da66175598 1043 @endverbatim
mbed_official 237:f3da66175598 1044 * @{
mbed_official 237:f3da66175598 1045 */
mbed_official 237:f3da66175598 1046
mbed_official 237:f3da66175598 1047 /**
mbed_official 237:f3da66175598 1048 * @brief return the IRDA state
mbed_official 237:f3da66175598 1049 * @param hirda: irda handle
mbed_official 237:f3da66175598 1050 * @retval HAL state
mbed_official 237:f3da66175598 1051 */
mbed_official 237:f3da66175598 1052 HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 1053 {
mbed_official 237:f3da66175598 1054 return hirda->State;
mbed_official 237:f3da66175598 1055 }
mbed_official 237:f3da66175598 1056
mbed_official 237:f3da66175598 1057 /**
mbed_official 237:f3da66175598 1058 * @brief Return the IRDA error code
mbed_official 237:f3da66175598 1059 * @param hirda : pointer to a IRDA_HandleTypeDef structure that contains
mbed_official 237:f3da66175598 1060 * the configuration information for the specified IRDA.
mbed_official 237:f3da66175598 1061 * @retval IRDA Error Code
mbed_official 237:f3da66175598 1062 */
mbed_official 237:f3da66175598 1063 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 1064 {
mbed_official 237:f3da66175598 1065 return hirda->ErrorCode;
mbed_official 237:f3da66175598 1066 }
mbed_official 237:f3da66175598 1067
mbed_official 237:f3da66175598 1068 /**
mbed_official 237:f3da66175598 1069 * @}
mbed_official 237:f3da66175598 1070 */
mbed_official 237:f3da66175598 1071
mbed_official 237:f3da66175598 1072 /**
mbed_official 237:f3da66175598 1073 * @brief Configure the IRDA peripheral
mbed_official 237:f3da66175598 1074 * @param hirda: irda handle
mbed_official 237:f3da66175598 1075 * @retval None
mbed_official 237:f3da66175598 1076 */
mbed_official 237:f3da66175598 1077 static HAL_StatusTypeDef IRDA_SetConfig(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 1078 {
mbed_official 237:f3da66175598 1079 uint32_t tmpreg = 0x00000000;
mbed_official 237:f3da66175598 1080 IRDA_ClockSourceTypeDef clocksource = IRDA_CLOCKSOURCE_UNDEFINED;
mbed_official 237:f3da66175598 1081 HAL_StatusTypeDef ret = HAL_OK;
mbed_official 237:f3da66175598 1082
mbed_official 237:f3da66175598 1083 /* Check the communication parameters */
mbed_official 237:f3da66175598 1084 assert_param(IS_IRDA_BAUDRATE(hirda->Init.BaudRate));
mbed_official 237:f3da66175598 1085 assert_param(IS_IRDA_WORD_LENGTH(hirda->Init.WordLength));
mbed_official 237:f3da66175598 1086 assert_param(IS_IRDA_PARITY(hirda->Init.Parity));
mbed_official 237:f3da66175598 1087 assert_param(IS_IRDA_TX_RX_MODE(hirda->Init.Mode));
mbed_official 237:f3da66175598 1088 assert_param(IS_IRDA_PRESCALER(hirda->Init.Prescaler));
mbed_official 237:f3da66175598 1089 assert_param(IS_IRDA_POWERMODE(hirda->Init.PowerMode));
mbed_official 237:f3da66175598 1090
mbed_official 237:f3da66175598 1091
mbed_official 237:f3da66175598 1092
mbed_official 237:f3da66175598 1093
mbed_official 237:f3da66175598 1094 /*-------------------------- USART CR1 Configuration -----------------------*/
mbed_official 237:f3da66175598 1095 /* Configure the IRDA Word Length, Parity and transfer Mode:
mbed_official 237:f3da66175598 1096 Set the M bits according to hirda->Init.WordLength value
mbed_official 237:f3da66175598 1097 Set PCE and PS bits according to hirda->Init.Parity value
mbed_official 237:f3da66175598 1098 Set TE and RE bits according to hirda->Init.Mode value */
mbed_official 237:f3da66175598 1099 tmpreg = (uint32_t)hirda->Init.WordLength | hirda->Init.Parity | hirda->Init.Mode ;
mbed_official 237:f3da66175598 1100
mbed_official 237:f3da66175598 1101 MODIFY_REG(hirda->Instance->CR1, IRDA_CR1_FIELDS, tmpreg);
mbed_official 237:f3da66175598 1102
mbed_official 237:f3da66175598 1103 /*-------------------------- USART CR3 Configuration -----------------------*/
mbed_official 237:f3da66175598 1104 MODIFY_REG(hirda->Instance->CR3, USART_CR3_IRLP, hirda->Init.PowerMode);
mbed_official 237:f3da66175598 1105
mbed_official 237:f3da66175598 1106 /*-------------------------- USART GTPR Configuration ----------------------*/
mbed_official 237:f3da66175598 1107 MODIFY_REG(hirda->Instance->GTPR, USART_GTPR_PSC, hirda->Init.Prescaler);
mbed_official 237:f3da66175598 1108
mbed_official 237:f3da66175598 1109 /*-------------------------- USART BRR Configuration -----------------------*/
mbed_official 237:f3da66175598 1110 __HAL_IRDA_GETCLOCKSOURCE(hirda, clocksource);
mbed_official 237:f3da66175598 1111 switch (clocksource)
mbed_official 237:f3da66175598 1112 {
mbed_official 237:f3da66175598 1113 case IRDA_CLOCKSOURCE_PCLK1:
mbed_official 237:f3da66175598 1114 hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hirda->Init.BaudRate);
mbed_official 237:f3da66175598 1115 break;
mbed_official 237:f3da66175598 1116 case IRDA_CLOCKSOURCE_PCLK2:
mbed_official 237:f3da66175598 1117 hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hirda->Init.BaudRate);
mbed_official 237:f3da66175598 1118 break;
mbed_official 237:f3da66175598 1119 case IRDA_CLOCKSOURCE_HSI:
mbed_official 237:f3da66175598 1120 hirda->Instance->BRR = (uint16_t)(HSI_VALUE / hirda->Init.BaudRate);
mbed_official 237:f3da66175598 1121 break;
mbed_official 237:f3da66175598 1122 case IRDA_CLOCKSOURCE_SYSCLK:
mbed_official 237:f3da66175598 1123 hirda->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hirda->Init.BaudRate);
mbed_official 237:f3da66175598 1124 break;
mbed_official 237:f3da66175598 1125 case IRDA_CLOCKSOURCE_LSE:
mbed_official 237:f3da66175598 1126 hirda->Instance->BRR = (uint16_t)(LSE_VALUE / hirda->Init.BaudRate);
mbed_official 237:f3da66175598 1127 break;
mbed_official 237:f3da66175598 1128 case IRDA_CLOCKSOURCE_UNDEFINED:
mbed_official 237:f3da66175598 1129 default:
mbed_official 237:f3da66175598 1130 ret = HAL_ERROR;
mbed_official 237:f3da66175598 1131 break;
mbed_official 237:f3da66175598 1132 }
mbed_official 237:f3da66175598 1133
mbed_official 237:f3da66175598 1134 return ret;
mbed_official 237:f3da66175598 1135 }
mbed_official 237:f3da66175598 1136
mbed_official 237:f3da66175598 1137 /**
mbed_official 237:f3da66175598 1138 * @brief Check the IRDA Idle State
mbed_official 237:f3da66175598 1139 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 1140 * @retval HAL status
mbed_official 237:f3da66175598 1141 */
mbed_official 237:f3da66175598 1142 static HAL_StatusTypeDef IRDA_CheckIdleState(IRDA_HandleTypeDef *hirda)
mbed_official 237:f3da66175598 1143 {
mbed_official 237:f3da66175598 1144
mbed_official 237:f3da66175598 1145 /* Initialize the IRDA ErrorCode */
mbed_official 237:f3da66175598 1146 hirda->ErrorCode = HAL_IRDA_ERROR_NONE;
mbed_official 237:f3da66175598 1147
mbed_official 237:f3da66175598 1148 /* Check if the Transmitter is enabled */
mbed_official 237:f3da66175598 1149 if((hirda->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
mbed_official 237:f3da66175598 1150 {
mbed_official 237:f3da66175598 1151 /* Wait until TEACK flag is set */
mbed_official 237:f3da66175598 1152 if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
mbed_official 237:f3da66175598 1153 {
mbed_official 237:f3da66175598 1154 /* Timeout Occured */
mbed_official 237:f3da66175598 1155 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 1156 }
mbed_official 237:f3da66175598 1157 }
mbed_official 237:f3da66175598 1158 /* Check if the Receiver is enabled */
mbed_official 237:f3da66175598 1159 if((hirda->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
mbed_official 237:f3da66175598 1160 {
mbed_official 237:f3da66175598 1161 if(IRDA_WaitOnFlagUntilTimeout(hirda, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
mbed_official 237:f3da66175598 1162 {
mbed_official 237:f3da66175598 1163 /* Timeout Occured */
mbed_official 237:f3da66175598 1164 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 1165 }
mbed_official 237:f3da66175598 1166 }
mbed_official 237:f3da66175598 1167
mbed_official 237:f3da66175598 1168 /* Initialize the IRDA state*/
mbed_official 237:f3da66175598 1169 hirda->State= HAL_IRDA_STATE_READY;
mbed_official 237:f3da66175598 1170 /* Process Unlocked */
mbed_official 237:f3da66175598 1171 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 1172
mbed_official 237:f3da66175598 1173 return HAL_OK;
mbed_official 237:f3da66175598 1174 }
mbed_official 237:f3da66175598 1175
mbed_official 237:f3da66175598 1176
mbed_official 237:f3da66175598 1177 /**
mbed_official 237:f3da66175598 1178 * @brief Handle IRDA Communication Timeout.
mbed_official 237:f3da66175598 1179 * @param hirda: IRDA handle
mbed_official 237:f3da66175598 1180 * @param Flag: specifies the IRDA flag to check.
mbed_official 237:f3da66175598 1181 * @param Status: the flag status (SET or RESET). The function is locked in a while loop as long as the flag remains set to Status.
mbed_official 237:f3da66175598 1182 * @param Timeout: Timeout duration
mbed_official 237:f3da66175598 1183 * @retval HAL status
mbed_official 237:f3da66175598 1184 */
mbed_official 237:f3da66175598 1185 static HAL_StatusTypeDef IRDA_WaitOnFlagUntilTimeout(IRDA_HandleTypeDef *hirda, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
mbed_official 237:f3da66175598 1186 {
mbed_official 237:f3da66175598 1187 uint32_t tickstart = HAL_GetTick();
mbed_official 237:f3da66175598 1188
mbed_official 237:f3da66175598 1189 /* Wait until flag is set */
mbed_official 237:f3da66175598 1190 if(Status == RESET)
mbed_official 237:f3da66175598 1191 {
mbed_official 237:f3da66175598 1192 while(__HAL_IRDA_GET_FLAG(hirda, Flag) == RESET)
mbed_official 237:f3da66175598 1193 {
mbed_official 237:f3da66175598 1194 /* Check for the Timeout */
mbed_official 237:f3da66175598 1195 if(Timeout != HAL_MAX_DELAY)
mbed_official 237:f3da66175598 1196 {
mbed_official 237:f3da66175598 1197 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 237:f3da66175598 1198 {
mbed_official 237:f3da66175598 1199 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
mbed_official 237:f3da66175598 1200 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
mbed_official 237:f3da66175598 1201 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
mbed_official 237:f3da66175598 1202 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
mbed_official 237:f3da66175598 1203 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
mbed_official 237:f3da66175598 1204
mbed_official 237:f3da66175598 1205 hirda->State= HAL_IRDA_STATE_TIMEOUT;
mbed_official 237:f3da66175598 1206
mbed_official 237:f3da66175598 1207 /* Process Unlocked */
mbed_official 237:f3da66175598 1208 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 1209
mbed_official 237:f3da66175598 1210 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 1211 }
mbed_official 237:f3da66175598 1212 }
mbed_official 237:f3da66175598 1213 }
mbed_official 237:f3da66175598 1214 }
mbed_official 237:f3da66175598 1215 else
mbed_official 237:f3da66175598 1216 {
mbed_official 237:f3da66175598 1217 while(__HAL_IRDA_GET_FLAG(hirda, Flag) != RESET)
mbed_official 237:f3da66175598 1218 {
mbed_official 237:f3da66175598 1219 /* Check for the Timeout */
mbed_official 237:f3da66175598 1220 if(Timeout != HAL_MAX_DELAY)
mbed_official 237:f3da66175598 1221 {
mbed_official 237:f3da66175598 1222 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 237:f3da66175598 1223 {
mbed_official 237:f3da66175598 1224 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
mbed_official 237:f3da66175598 1225 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_TXE);
mbed_official 237:f3da66175598 1226 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_RXNE);
mbed_official 237:f3da66175598 1227 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_PE);
mbed_official 237:f3da66175598 1228 __HAL_IRDA_DISABLE_IT(hirda, IRDA_IT_ERR);
mbed_official 237:f3da66175598 1229
mbed_official 237:f3da66175598 1230 hirda->State= HAL_IRDA_STATE_TIMEOUT;
mbed_official 237:f3da66175598 1231
mbed_official 237:f3da66175598 1232 /* Process Unlocked */
mbed_official 237:f3da66175598 1233 __HAL_UNLOCK(hirda);
mbed_official 237:f3da66175598 1234
mbed_official 237:f3da66175598 1235 return HAL_TIMEOUT;
mbed_official 237:f3da66175598 1236 }
mbed_official 237:f3da66175598 1237 }
mbed_official 237:f3da66175598 1238 }
mbed_official 237:f3da66175598 1239 }
mbed_official 237:f3da66175598 1240 return HAL_OK;
mbed_official 237:f3da66175598 1241 }
mbed_official 237:f3da66175598 1242
mbed_official 237:f3da66175598 1243 /**
mbed_official 237:f3da66175598 1244 * @}
mbed_official 237:f3da66175598 1245 */
mbed_official 237:f3da66175598 1246
mbed_official 237:f3da66175598 1247 #endif /* HAL_IRDA_MODULE_ENABLED */
mbed_official 237:f3da66175598 1248 /**
mbed_official 237:f3da66175598 1249 * @}
mbed_official 237:f3da66175598 1250 */
mbed_official 237:f3da66175598 1251
mbed_official 237:f3da66175598 1252 /**
mbed_official 237:f3da66175598 1253 * @}
mbed_official 237:f3da66175598 1254 */
mbed_official 237:f3da66175598 1255
mbed_official 237:f3da66175598 1256 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/