2 ******************************************************************************
3 * @file stm32f4xx_hal_irda.h
4 * @author MCD Application Team
7 * @brief Header file of IRDA HAL module.
8 ******************************************************************************
11 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ******************************************************************************
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F4xx_HAL_IRDA_H
40 #define __STM32F4xx_HAL_IRDA_H
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f4xx_hal_def.h"
49 /** @addtogroup STM32F4xx_HAL_Driver
57 /* Exported types ------------------------------------------------------------*/
60 * @brief IRDA Init Structure definition
64 uint32_t BaudRate; /*!< This member configures the IRDA communication baud rate.
65 The baud rate is computed using the following formula:
66 - IntegerDivider = ((PCLKx) / (8 * (hirda->Init.BaudRate)))
67 - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 8) + 0.5 */
69 uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
70 This parameter can be a value of @ref IRDA_Word_Length */
73 uint32_t Parity; /*!< Specifies the parity mode.
74 This parameter can be a value of @ref IRDA_Parity
75 @note When parity is enabled, the computed parity is inserted
76 at the MSB position of the transmitted data (9th bit when
77 the word length is set to 9 data bits; 8th bit when the
78 word length is set to 8 data bits). */
80 uint32_t Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
81 This parameter can be a value of @ref IRDA_Mode */
83 uint8_t Prescaler; /*!< Specifies the Prescaler */
85 uint32_t IrDAMode; /*!< Specifies the IrDA mode
86 This parameter can be a value of @ref IrDA_Low_Power */
90 * @brief HAL State structures definition
94 HAL_IRDA_STATE_RESET = 0x00, /*!< Peripheral is not yet Initialized */
95 HAL_IRDA_STATE_READY = 0x01, /*!< Peripheral Initialized and ready for use */
96 HAL_IRDA_STATE_BUSY = 0x02, /*!< an internal process is ongoing */
97 HAL_IRDA_STATE_BUSY_TX = 0x12, /*!< Data Transmission process is ongoing */
98 HAL_IRDA_STATE_BUSY_RX = 0x22, /*!< Data Reception process is ongoing */
99 HAL_IRDA_STATE_BUSY_TX_RX = 0x32, /*!< Data Transmission and Reception process is ongoing */
100 HAL_IRDA_STATE_TIMEOUT = 0x03, /*!< Timeout state */
101 HAL_IRDA_STATE_ERROR = 0x04 /*!< Error */
102 }HAL_IRDA_StateTypeDef;
105 * @brief HAL IRDA Error Code structure definition
109 HAL_IRDA_ERROR_NONE = 0x00, /*!< No error */
110 HAL_IRDA_ERROR_PE = 0x01, /*!< Parity error */
111 HAL_IRDA_ERROR_NE = 0x02, /*!< Noise error */
112 HAL_IRDA_ERROR_FE = 0x04, /*!< frame error */
113 HAL_IRDA_ERROR_ORE = 0x08, /*!< Overrun error */
114 HAL_IRDA_ERROR_DMA = 0x10 /*!< DMA transfer error */
115 }HAL_IRDA_ErrorTypeDef;
118 * @brief IRDA handle Structure definition
122 USART_TypeDef *Instance; /* USART registers base address */
124 IRDA_InitTypeDef Init; /* IRDA communication parameters */
126 uint8_t *pTxBuffPtr; /* Pointer to IRDA Tx transfer Buffer */
128 uint16_t TxXferSize; /* IRDA Tx Transfer size */
130 uint16_t TxXferCount; /* IRDA Tx Transfer Counter */
132 uint8_t *pRxBuffPtr; /* Pointer to IRDA Rx transfer Buffer */
134 uint16_t RxXferSize; /* IRDA Rx Transfer size */
136 uint16_t RxXferCount; /* IRDA Rx Transfer Counter */
138 DMA_HandleTypeDef *hdmatx; /* IRDA Tx DMA Handle parameters */
140 DMA_HandleTypeDef *hdmarx; /* IRDA Rx DMA Handle parameters */
142 HAL_LockTypeDef Lock; /* Locking object */
144 __IO HAL_IRDA_StateTypeDef State; /* IRDA communication state */
146 __IO HAL_IRDA_ErrorTypeDef ErrorCode; /* IRDA Error code */
150 /* Exported constants --------------------------------------------------------*/
151 /** @defgroup IRDA_Exported_Constants
155 /** @defgroup IRDA_Word_Length
158 #define IRDA_WORDLENGTH_8B ((uint32_t)0x00000000)
159 #define IRDA_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
160 #define IS_IRDA_WORD_LENGTH(LENGTH) (((LENGTH) == IRDA_WORDLENGTH_8B) || \
161 ((LENGTH) == IRDA_WORDLENGTH_9B))
167 /** @defgroup IRDA_Parity
170 #define IRDA_PARITY_NONE ((uint32_t)0x00000000)
171 #define IRDA_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
172 #define IRDA_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
173 #define IS_IRDA_PARITY(PARITY) (((PARITY) == IRDA_PARITY_NONE) || \
174 ((PARITY) == IRDA_PARITY_EVEN) || \
175 ((PARITY) == IRDA_PARITY_ODD))
181 /** @defgroup IRDA_Mode
184 #define IRDA_MODE_RX ((uint32_t)USART_CR1_RE)
185 #define IRDA_MODE_TX ((uint32_t)USART_CR1_TE)
186 #define IRDA_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
187 #define IS_IRDA_MODE(MODE) ((((MODE) & (uint32_t)0x0000FFF3) == 0x00) && ((MODE) != (uint32_t)0x000000))
192 /** @defgroup IrDA_Low_Power
195 #define IRDA_POWERMODE_LOWPOWER ((uint32_t)USART_CR3_IRLP)
196 #define IRDA_POWERMODE_NORMAL ((uint32_t)0x00000000)
197 #define IS_IRDA_POWERMODE(MODE) (((MODE) == IRDA_POWERMODE_LOWPOWER) || \
198 ((MODE) == IRDA_POWERMODE_NORMAL))
203 /** @defgroup IRDA_Flags
204 * Elements values convention: 0xXXXX
205 * - 0xXXXX : Flag mask in the SR register
208 #define IRDA_FLAG_TXE ((uint32_t)0x00000080)
209 #define IRDA_FLAG_TC ((uint32_t)0x00000040)
210 #define IRDA_FLAG_RXNE ((uint32_t)0x00000020)
211 #define IRDA_FLAG_IDLE ((uint32_t)0x00000010)
212 #define IRDA_FLAG_ORE ((uint32_t)0x00000008)
213 #define IRDA_FLAG_NE ((uint32_t)0x00000004)
214 #define IRDA_FLAG_FE ((uint32_t)0x00000002)
215 #define IRDA_FLAG_PE ((uint32_t)0x00000001)
220 /** @defgroup IRDA_Interrupt_definition
221 * Elements values convention: 0xY000XXXX
222 * - XXXX : Interrupt mask in the XX register
223 * - Y : Interrupt source register (2bits)
231 #define IRDA_IT_PE ((uint32_t)0x10000100)
232 #define IRDA_IT_TXE ((uint32_t)0x10000080)
233 #define IRDA_IT_TC ((uint32_t)0x10000040)
234 #define IRDA_IT_RXNE ((uint32_t)0x10000020)
235 #define IRDA_IT_IDLE ((uint32_t)0x10000010)
237 #define IRDA_IT_LBD ((uint32_t)0x20000040)
239 #define IRDA_IT_CTS ((uint32_t)0x30000400)
240 #define IRDA_IT_ERR ((uint32_t)0x30000001)
250 /* Exported macro ------------------------------------------------------------*/
252 /** @brief Reset IRDA handle state
253 * @param __HANDLE__: specifies the USART Handle.
254 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
258 #define __HAL_IRDA_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_IRDA_STATE_RESET)
260 /** @brief Flushs the IRDA DR register
261 * @param __HANDLE__: specifies the USART Handle.
262 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
265 #define __HAL_IRDA_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
267 /** @brief Checks whether the specified IRDA flag is set or not.
268 * @param __HANDLE__: specifies the USART Handle.
269 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
271 * @param __FLAG__: specifies the flag to check.
272 * This parameter can be one of the following values:
273 * @arg IRDA_FLAG_TXE: Transmit data register empty flag
274 * @arg IRDA_FLAG_TC: Transmission Complete flag
275 * @arg IRDA_FLAG_RXNE: Receive data register not empty flag
276 * @arg IRDA_FLAG_IDLE: Idle Line detection flag
277 * @arg IRDA_FLAG_ORE: OverRun Error flag
278 * @arg IRDA_FLAG_NE: Noise Error flag
279 * @arg IRDA_FLAG_FE: Framing Error flag
280 * @arg IRDA_FLAG_PE: Parity Error flag
281 * @retval The new state of __FLAG__ (TRUE or FALSE).
283 #define __HAL_IRDA_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
285 /** @brief Clears the specified IRDA pending flag.
286 * @param __HANDLE__: specifies the USART Handle.
287 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
289 * @param __FLAG__: specifies the flag to check.
290 * This parameter can be any combination of the following values:
291 * @arg IRDA_FLAG_TC: Transmission Complete flag.
292 * @arg IRDA_FLAG_RXNE: Receive data register not empty flag.
294 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
295 * error) and IDLE (Idle line detected) flags are cleared by software
296 * sequence: a read operation to USART_SR register followed by a read
297 * operation to USART_DR register.
298 * @note RXNE flag can be also cleared by a read to the USART_DR register.
299 * @note TC flag can be also cleared by software sequence: a read operation to
300 * USART_SR register followed by a write operation to USART_DR register.
301 * @note TXE flag is cleared only by a write to the USART_DR register.
305 #define __HAL_IRDA_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
307 /** @brief Clear the IRDA PE pending flag.
308 * @param __HANDLE__: specifies the USART Handle.
309 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
313 #define __HAL_IRDA_CLEAR_PEFLAG(__HANDLE__) do{(__HANDLE__)->Instance->SR;\
314 (__HANDLE__)->Instance->DR;}while(0)
315 /** @brief Clear the IRDA FE pending flag.
316 * @param __HANDLE__: specifies the USART Handle.
317 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
321 #define __HAL_IRDA_CLEAR_FEFLAG(__HANDLE__) __HAL_IRDA_CLEAR_PEFLAG(__HANDLE__)
323 /** @brief Clear the IRDA NE pending flag.
324 * @param __HANDLE__: specifies the USART Handle.
325 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
329 #define __HAL_IRDA_CLEAR_NEFLAG(__HANDLE__) __HAL_IRDA_CLEAR_PEFLAG(__HANDLE__)
331 /** @brief Clear the IRDA ORE pending flag.
332 * @param __HANDLE__: specifies the USART Handle.
333 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
337 #define __HAL_IRDA_CLEAR_OREFLAG(__HANDLE__) __HAL_IRDA_CLEAR_PEFLAG(__HANDLE__)
339 /** @brief Clear the IRDA IDLE pending flag.
340 * @param __HANDLE__: specifies the USART Handle.
341 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
345 #define __HAL_IRDA_CLEAR_IDLEFLAG(__HANDLE__) __HAL_IRDA_CLEAR_PEFLAG(__HANDLE__)
347 /** @brief Enables or disables the specified IRDA interrupt.
348 * @param __HANDLE__: specifies the USART Handle.
349 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
351 * @param __INTERRUPT__: specifies the IRDA interrupt source to check.
352 * This parameter can be one of the following values:
353 * @arg IRDA_IT_TXE: Transmit Data Register empty interrupt
354 * @arg IRDA_IT_TC: Transmission complete interrupt
355 * @arg IRDA_IT_RXNE: Receive Data register not empty interrupt
356 * @arg IRDA_IT_IDLE: Idle line detection interrupt
357 * @arg IRDA_IT_PE: Parity Error interrupt
358 * @arg IRDA_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
359 * @param NewState: new state of the specified IRDA interrupt.
360 * This parameter can be: ENABLE or DISABLE.
363 #define IRDA_IT_MASK ((uint32_t)0x0000FFFF)
364 #define __HAL_IRDA_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == 1)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & IRDA_IT_MASK)): \
365 (((__INTERRUPT__) >> 28) == 2)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & IRDA_IT_MASK)): \
366 ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & IRDA_IT_MASK)))
367 #define __HAL_IRDA_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == 1)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & IRDA_IT_MASK)): \
368 (((__INTERRUPT__) >> 28) == 2)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & IRDA_IT_MASK)): \
369 ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & IRDA_IT_MASK)))
371 /** @brief Checks whether the specified IRDA interrupt has occurred or not.
372 * @param __HANDLE__: specifies the USART Handle.
373 * This parameter can be USARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
375 * @param __IT__: specifies the IRDA interrupt source to check.
376 * This parameter can be one of the following values:
377 * @arg IRDA_IT_TXE: Transmit Data Register empty interrupt
378 * @arg IRDA_IT_TC: Transmission complete interrupt
379 * @arg IRDA_IT_RXNE: Receive Data register not empty interrupt
380 * @arg IRDA_IT_IDLE: Idle line detection interrupt
381 * @arg USART_IT_ERR: Error interrupt
382 * @arg IRDA_IT_PE: Parity Error interrupt
383 * @retval The new state of __IT__ (TRUE or FALSE).
385 #define __HAL_IRDA_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == 1)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28) == 2)? \
386 (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & IRDA_IT_MASK))
390 #define __IRDA_ENABLE(__HANDLE__) ( (__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
391 #define __IRDA_DISABLE(__HANDLE__) ( (__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
393 #define __DIV(_PCLK_, _BAUD_) (((_PCLK_)*25)/(4*(_BAUD_)))
394 #define __DIVMANT(_PCLK_, _BAUD_) (__DIV((_PCLK_), (_BAUD_))/100)
395 #define __DIVFRAQ(_PCLK_, _BAUD_) (((__DIV((_PCLK_), (_BAUD_)) - (__DIVMANT((_PCLK_), (_BAUD_)) * 100)) * 16 + 50) / 100)
396 #define __IRDA_BRR(_PCLK_, _BAUD_) ((__DIVMANT((_PCLK_), (_BAUD_)) << 4)|(__DIVFRAQ((_PCLK_), (_BAUD_)) & 0x0F))
398 #define IS_IRDA_BAUDRATE(BAUDRATE) ((BAUDRATE) < 115201)
401 /* Exported functions --------------------------------------------------------*/
402 /* Initialization/de-initialization functions **********************************/
403 HAL_StatusTypeDef HAL_IRDA_Init(IRDA_HandleTypeDef *hirda);
404 HAL_StatusTypeDef HAL_IRDA_DeInit(IRDA_HandleTypeDef *hirda);
405 void HAL_IRDA_MspInit(IRDA_HandleTypeDef *hirda);
406 void HAL_IRDA_MspDeInit(IRDA_HandleTypeDef *hirda);
408 /* IO operation functions *******************************************************/
409 HAL_StatusTypeDef HAL_IRDA_Transmit(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout);
410 HAL_StatusTypeDef HAL_IRDA_Receive(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size, uint32_t Timeout);
411 HAL_StatusTypeDef HAL_IRDA_Transmit_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size);
412 HAL_StatusTypeDef HAL_IRDA_Receive_IT(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size);
413 HAL_StatusTypeDef HAL_IRDA_Transmit_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size);
414 HAL_StatusTypeDef HAL_IRDA_Receive_DMA(IRDA_HandleTypeDef *hirda, uint8_t *pData, uint16_t Size);
415 HAL_StatusTypeDef HAL_IRDA_DMAPause(IRDA_HandleTypeDef *hirda);
416 HAL_StatusTypeDef HAL_IRDA_DMAResume(IRDA_HandleTypeDef *hirda);
417 HAL_StatusTypeDef HAL_IRDA_DMAStop(IRDA_HandleTypeDef *hirda);
418 void HAL_IRDA_IRQHandler(IRDA_HandleTypeDef *hirda);
419 void HAL_IRDA_TxCpltCallback(IRDA_HandleTypeDef *hirda);
420 void HAL_IRDA_RxCpltCallback(IRDA_HandleTypeDef *hirda);
421 void HAL_IRDA_TxHalfCpltCallback(IRDA_HandleTypeDef *hirda);
422 void HAL_IRDA_RxHalfCpltCallback(IRDA_HandleTypeDef *hirda);
423 void HAL_IRDA_ErrorCallback(IRDA_HandleTypeDef *hirda);
425 /* Peripheral State functions **************************************************/
426 HAL_IRDA_StateTypeDef HAL_IRDA_GetState(IRDA_HandleTypeDef *hirda);
427 uint32_t HAL_IRDA_GetError(IRDA_HandleTypeDef *hirda);
441 #endif /* __STM32F4xx_HAL_IRDA_H */
443 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/