mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
Diff: targets/cmsis/TARGET_STM/TARGET_NUCLEO_F401RE/stm32f4xx_hal_uart.c
- Revision:
- 226:b062af740e40
- Parent:
- 106:ced8cbb51063
--- a/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F401RE/stm32f4xx_hal_uart.c Wed Jun 11 09:30:07 2014 +0100 +++ b/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F401RE/stm32f4xx_hal_uart.c Wed Jun 11 09:45:09 2014 +0100 @@ -2,8 +2,8 @@ ****************************************************************************** * @file stm32f4xx_hal_uart.c * @author MCD Application Team - * @version V1.0.0 - * @date 18-February-2014 + * @version V1.1.0RC2 + * @date 14-May-2014 * @brief UART HAL module driver. * This file provides firmware functions to manage the following * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral: @@ -21,7 +21,7 @@ (#) Declare a UART_HandleTypeDef handle structure. - (#) Initialize the UART low level resources by implement the HAL_UART_MspInit() API: + (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API: (##) Enable the USARTx interface clock. (##) UART pins configuration: (+++) Enable the clock for the UART GPIOs. @@ -42,7 +42,7 @@ interrupt on the DMA Tx/Rx Stream. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware - flow control and Mode(Receiver/Transmitter) in the huart Init structure. + flow control and Mode(Receiver/Transmitter) in the Init structure. (#) For the UART asynchronous mode, initialize the UART registers by calling the HAL_UART_Init() API. @@ -55,16 +55,19 @@ (#) For the Multi-Processor mode, initialize the UART registers by calling the HAL_MultiProcessor_Init() API. - -@- The specific UART interrupts (Transmission complete interrupt, + [..] + (@) The specific UART interrupts (Transmission complete interrupt, RXNE interrupt and Error Interrupts) will be managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit and receive process. - -@- These API's(HAL_UART_Init() and HAL_HalfDuplex_Init()) configures also the + [..] + (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customed HAL_UART_MspInit() API. - (#) Three mode of operations are available within this driver : + [..] + Three operation modes are available within this driver : *** Polling mode IO operation *** ================================= @@ -114,10 +117,10 @@ (+) __HAL_UART_ENABLE: Enable the UART peripheral (+) __HAL_UART_DISABLE: Disable the UART peripheral - (+) __HAL_UART_GET_FLAG : Checks whether the specified UART flag is set or not - (+) __HAL_UART_CLEAR_FLAG : Clears the specified UART pending flag - (+) __HAL_UART_ENABLE_IT: Enables the specified UART interrupt - (+) __HAL_UART_DISABLE_IT: Disables the specified UART interrupt + (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not + (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag + (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt + (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt [..] (@) You can refer to the UART HAL driver header file for more useful macros @@ -205,18 +208,7 @@ (++) Parity: If the parity is enabled, then the MSB bit of the data written in the data register is transmitted but is changed by the parity bit. Depending on the frame length defined by the M bit (8-bits or 9-bits), - the possible UART frame formats are as listed in the following table: - +-------------------------------------------------------------+ - | M bit | PCE bit | UART frame | - |---------------------|---------------------------------------| - | 0 | 0 | | SB | 8 bit data | STB | | - |---------|-----------|---------------------------------------| - | 0 | 1 | | SB | 7 bit data | PB | STB | | - |---------|-----------|---------------------------------------| - | 1 | 0 | | SB | 9 bit data | STB | | - |---------|-----------|---------------------------------------| - | 1 | 1 | | SB | 8 bit data | PB | STB | | - +-------------------------------------------------------------+ + please refer to Reference manual for possible UART frame formats. (++) Hardware flow control (++) Receiver/transmitter modes (++) Over Sampling Methode @@ -232,7 +224,8 @@ /** * @brief Initializes the UART mode according to the specified parameters in * the UART_InitTypeDef and create the associated handle. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) @@ -259,9 +252,9 @@ /* Init the low level hardware */ HAL_UART_MspInit(huart); } - - huart->State = HAL_UART_STATE_BUSY; - + + huart->State = HAL_UART_STATE_BUSY; + /* Disable the peripheral */ __HAL_UART_DISABLE(huart); @@ -287,7 +280,8 @@ /** * @brief Initializes the half-duplex mode according to the specified * parameters in the UART_InitTypeDef and create the associated handle. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart) @@ -303,7 +297,9 @@ /* Init the low level hardware */ HAL_UART_MspInit(huart); } - + + huart->State = HAL_UART_STATE_BUSY; + /* Disable the peripheral */ __HAL_UART_DISABLE(huart); @@ -332,7 +328,8 @@ /** * @brief Initializes the LIN mode according to the specified * parameters in the UART_InitTypeDef and create the associated handle. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param BreakDetectLength: Specifies the LIN break detection length. * This parameter can be one of the following values: * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection @@ -354,7 +351,9 @@ /* Init the low level hardware */ HAL_UART_MspInit(huart); } - + + huart->State = HAL_UART_STATE_BUSY; + /* Disable the peripheral */ __HAL_UART_DISABLE(huart); @@ -387,7 +386,8 @@ /** * @brief Initializes the Multi-Processor mode according to the specified * parameters in the UART_InitTypeDef and create the associated handle. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param Address: USART address * @param WakeUpMethode: specifies the USART wakeup method. * This parameter can be one of the following values: @@ -412,7 +412,9 @@ /* Init the low level hardware */ HAL_UART_MspInit(huart); } - + + huart->State = HAL_UART_STATE_BUSY; + /* Disable the peripheral */ __HAL_UART_DISABLE(huart); @@ -446,7 +448,8 @@ /** * @brief DeInitializes the UART peripheral. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart) @@ -476,7 +479,8 @@ /** * @brief UART MSP Init. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) @@ -488,7 +492,8 @@ /** * @brief UART MSP DeInit. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) @@ -517,7 +522,7 @@ (++) Blocking mode: The communication is performed in polling mode. The HAL status of all data processing is returned by the same function after finishing transfer. - (++) Non-Blocking mode: The communication is performed using Interrupts + (++) Non blocking mode: The communication is performed using Interrupts or DMA, these APIs return the HAL status. The end of the data processing will be indicated through the dedicated UART IRQ when using Interrupt mode or the DMA IRQ when @@ -527,20 +532,20 @@ The HAL_UART_ErrorCallback() user callback will be executed when a communication error is detected. - (#) Blocking mode API's are: + (#) Blocking mode APIs are: (++) HAL_UART_Transmit() (++) HAL_UART_Receive() - (#) Non-Blocking mode API's with Interrupt are: + (#) Non Blocking mode APIs with Interrupt are: (++) HAL_UART_Transmit_IT() (++) HAL_UART_Receive_IT() (++) HAL_UART_IRQHandler() - (#) No-Blocking mode functions with DMA are: + (#) Non Blocking mode functions with DMA are: (++) HAL_UART_Transmit_DMA() (++) HAL_UART_Receive_DMA() - (#) A set of Transfer Complete Callbacks are provided in Non-Blocking mode: + (#) A set of Transfer Complete Callbacks are provided in non blocking mode: (++) HAL_UART_TxCpltCallback() (++) HAL_UART_RxCpltCallback() (++) HAL_UART_ErrorCallback() @@ -556,7 +561,8 @@ /** * @brief Sends an amount of data in blocking mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be sent * @param Timeout: Timeout duration @@ -649,7 +655,8 @@ /** * @brief Receives an amount of data in blocking mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be received * @param Timeout: Timeout duration @@ -748,7 +755,8 @@ /** * @brief Sends an amount of data in non blocking mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be sent * @retval HAL status @@ -805,7 +813,8 @@ /** * @brief Receives an amount of data in non blocking mode - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be received * @retval HAL status @@ -862,7 +871,8 @@ /** * @brief Sends an amount of data in non blocking mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be sent * @retval HAL status @@ -928,7 +938,8 @@ /** * @brief Receives an amount of data in non blocking mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be received * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit. @@ -954,7 +965,7 @@ huart->RxXferSize = Size; huart->ErrorCode = HAL_UART_ERROR_NONE; - /* Check if a transmit rocess is ongoing or not */ + /* Check if a transmit process is ongoing or not */ if(huart->State == HAL_UART_STATE_BUSY_TX) { huart->State = HAL_UART_STATE_BUSY_TX_RX; @@ -994,8 +1005,9 @@ /** * @brief Pauses the DMA Transfer. - * @param huart: UART handle - * @retval None + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. + * @retval HAL status */ HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart) { @@ -1027,8 +1039,9 @@ /** * @brief Resumes the DMA Transfer. - * @param huart: UART handle - * @retval None + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. + * @retval HAL status */ HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart) { @@ -1067,8 +1080,9 @@ /** * @brief Stops the DMA Transfer. - * @param huart: UART handle - * @retval None + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. + * @retval HAL status */ HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) { @@ -1102,7 +1116,8 @@ /** * @brief This function handles UART interrupt request. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ void HAL_UART_IRQHandler(UART_HandleTypeDef *huart) @@ -1178,7 +1193,8 @@ /** * @brief Tx Transfer completed callbacks. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) @@ -1190,7 +1206,8 @@ /** * @brief Tx Half Transfer completed callbacks. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) @@ -1202,7 +1219,8 @@ /** * @brief Rx Transfer completed callbacks. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) @@ -1214,7 +1232,8 @@ /** * @brief Rx Half Transfer completed callbacks. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) @@ -1226,7 +1245,8 @@ /** * @brief UART error callbacks. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) @@ -1259,7 +1279,8 @@ /** * @brief Transmits break characters. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart) @@ -1285,7 +1306,8 @@ /** * @brief Enters the UART in mute mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart) @@ -1311,7 +1333,8 @@ /** * @brief Exits the UART mute mode: wake up software. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart) @@ -1337,9 +1360,9 @@ /** * @brief Enables the UART transmitter and disables the UART receiver. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status - * @retval None */ HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart) { @@ -1372,7 +1395,8 @@ /** * @brief Enables the UART receiver and disables the UART transmitter. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart) @@ -1417,10 +1441,10 @@ ============================================================================== [..] This subsection provides a set of functions allowing to return the State of - UART communication process, return Peripheral Errors occured during communication + UART communication process, return Peripheral Errors occurred during communication process (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral. - (+) HAL_UART_GetError() check in run-time errors that could be occured durung communication. + (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication. @endverbatim * @{ @@ -1428,7 +1452,8 @@ /** * @brief Returns the UART state. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL state */ HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart) @@ -1468,7 +1493,7 @@ /* Wait for UART TC Flag */ if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, UART_TIMEOUT_VALUE) != HAL_OK) { - /* Timeout Occured */ + /* Timeout occurred */ huart->State = HAL_UART_STATE_TIMEOUT; HAL_UART_ErrorCallback(huart); } @@ -1490,7 +1515,8 @@ /** * @brief DMA UART transmit process half complete callback - * @param hdma : DMA handle + * @param hdma: pointer to a DMA_HandleTypeDef structure that contains + * the configuration information for the specified DMA module. * @retval None */ static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) @@ -1528,7 +1554,8 @@ /** * @brief DMA UART receive process half complete callback - * @param hdma : DMA handle + * @param hdma: pointer to a DMA_HandleTypeDef structure that contains + * the configuration information for the specified DMA module. * @retval None */ static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) @@ -1555,7 +1582,8 @@ /** * @brief This function handles UART Communication Timeout. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @param Flag: specifies the UART flag to check. * @param Status: The new Flag status (SET or RESET). * @param Timeout: Timeout duration @@ -1623,7 +1651,8 @@ /** * @brief Sends an amount of data in non blocking mode. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart) @@ -1634,9 +1663,6 @@ tmp1 = huart->State; if((tmp1 == HAL_UART_STATE_BUSY_TX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX)) { - /* Process Locked */ - __HAL_LOCK(huart); - if(huart->Init.WordLength == UART_WORDLENGTH_9B) { tmp = (uint16_t*) huart->pTxBuffPtr; @@ -1675,28 +1701,21 @@ huart->State = HAL_UART_STATE_READY; } - /* Call the Process Unlocked before calling the Tx callback API to give the possibility to - start again the Transmission under the Tx callback API */ - __HAL_UNLOCK(huart); - HAL_UART_TxCpltCallback(huart); return HAL_OK; } - - /* Process Unlocked */ - __HAL_UNLOCK(huart); - - return HAL_OK; + return HAL_OK; } else { - return HAL_BUSY; + return HAL_BUSY; } } /** * @brief Receives an amount of data in non blocking mode - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval HAL status */ static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart) @@ -1707,9 +1726,6 @@ tmp1 = huart->State; if((tmp1 == HAL_UART_STATE_BUSY_RX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX)) { - /* Process Locked */ - __HAL_LOCK(huart); - if(huart->Init.WordLength == UART_WORDLENGTH_9B) { tmp = (uint16_t*) huart->pRxBuffPtr; @@ -1738,8 +1754,9 @@ if(--huart->RxXferCount == 0) { - while(HAL_IS_BIT_SET(huart->Instance->SR, UART_FLAG_RXNE)) - { + if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, SET, UART_TIMEOUT_VALUE) != HAL_OK) + { + return HAL_TIMEOUT; } __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); @@ -1758,17 +1775,10 @@ huart->State = HAL_UART_STATE_READY; } - /* Call the Process Unlocked before calling the Rx callback API to give the possibility to - start again the reception under the Rx callback API */ - __HAL_UNLOCK(huart); - HAL_UART_RxCpltCallback(huart); return HAL_OK; } - /* Process Unlocked */ - __HAL_UNLOCK(huart); - return HAL_OK; } else @@ -1779,7 +1789,8 @@ /** * @brief Configures the UART peripheral. - * @param huart: UART handle + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. * @retval None */ static void UART_SetConfig(UART_HandleTypeDef *huart)