mbed library sources
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_NUCLEO_F411RE/stm32f4xx_hal_usart.c@308:59cc78a25982, 2014-09-05 (annotated)
- Committer:
- todotani
- Date:
- Fri Sep 05 14:20:33 2014 +0000
- Revision:
- 308:59cc78a25982
- Parent:
- 235:685d5f11838f
BLE_Health_Thermometer for mbed HRM1017 with BLE library 0.1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 235:685d5f11838f | 1 | /** |
mbed_official | 235:685d5f11838f | 2 | ****************************************************************************** |
mbed_official | 235:685d5f11838f | 3 | * @file stm32f4xx_hal_usart.c |
mbed_official | 235:685d5f11838f | 4 | * @author MCD Application Team |
mbed_official | 235:685d5f11838f | 5 | * @version V1.1.0 |
mbed_official | 235:685d5f11838f | 6 | * @date 19-June-2014 |
mbed_official | 235:685d5f11838f | 7 | * @brief USART HAL module driver. |
mbed_official | 235:685d5f11838f | 8 | * This file provides firmware functions to manage the following |
mbed_official | 235:685d5f11838f | 9 | * functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral: |
mbed_official | 235:685d5f11838f | 10 | * + Initialization and de-initialization functions |
mbed_official | 235:685d5f11838f | 11 | * + IO operation functions |
mbed_official | 235:685d5f11838f | 12 | * + Peripheral Control functions |
mbed_official | 235:685d5f11838f | 13 | @verbatim |
mbed_official | 235:685d5f11838f | 14 | ============================================================================== |
mbed_official | 235:685d5f11838f | 15 | ##### How to use this driver ##### |
mbed_official | 235:685d5f11838f | 16 | ============================================================================== |
mbed_official | 235:685d5f11838f | 17 | [..] |
mbed_official | 235:685d5f11838f | 18 | The USART HAL driver can be used as follows: |
mbed_official | 235:685d5f11838f | 19 | |
mbed_official | 235:685d5f11838f | 20 | (#) Declare a USART_HandleTypeDef handle structure. |
mbed_official | 235:685d5f11838f | 21 | (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit () API: |
mbed_official | 235:685d5f11838f | 22 | (##) Enable the USARTx interface clock. |
mbed_official | 235:685d5f11838f | 23 | (##) USART pins configuration: |
mbed_official | 235:685d5f11838f | 24 | (+++) Enable the clock for the USART GPIOs. |
mbed_official | 235:685d5f11838f | 25 | (+++) Configure these USART pins as alternate function pull-up. |
mbed_official | 235:685d5f11838f | 26 | (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(), |
mbed_official | 235:685d5f11838f | 27 | HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): |
mbed_official | 235:685d5f11838f | 28 | (+++) Configure the USARTx interrupt priority. |
mbed_official | 235:685d5f11838f | 29 | (+++) Enable the NVIC USART IRQ handle. |
mbed_official | 235:685d5f11838f | 30 | (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA() |
mbed_official | 235:685d5f11838f | 31 | HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): |
mbed_official | 235:685d5f11838f | 32 | (+++) Declare a DMA handle structure for the Tx/Rx stream. |
mbed_official | 235:685d5f11838f | 33 | (+++) Enable the DMAx interface clock. |
mbed_official | 235:685d5f11838f | 34 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
mbed_official | 235:685d5f11838f | 35 | (+++) Configure the DMA Tx/Rx Stream. |
mbed_official | 235:685d5f11838f | 36 | (+++) Associate the initilalized DMA handle to the USART DMA Tx/Rx handle. |
mbed_official | 235:685d5f11838f | 37 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream. |
mbed_official | 235:685d5f11838f | 38 | |
mbed_official | 235:685d5f11838f | 39 | (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware |
mbed_official | 235:685d5f11838f | 40 | flow control and Mode(Receiver/Transmitter) in the husart Init structure. |
mbed_official | 235:685d5f11838f | 41 | |
mbed_official | 235:685d5f11838f | 42 | (#) Initialize the USART registers by calling the HAL_USART_Init() API: |
mbed_official | 235:685d5f11838f | 43 | (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) |
mbed_official | 235:685d5f11838f | 44 | by calling the customed HAL_USART_MspInit(&husart) API. |
mbed_official | 235:685d5f11838f | 45 | |
mbed_official | 235:685d5f11838f | 46 | -@@- The specific USART interrupts (Transmission complete interrupt, |
mbed_official | 235:685d5f11838f | 47 | RXNE interrupt and Error Interrupts) will be managed using the macros |
mbed_official | 235:685d5f11838f | 48 | __USART_ENABLE_IT() and __USART_DISABLE_IT() inside the transmit and receive process. |
mbed_official | 235:685d5f11838f | 49 | |
mbed_official | 235:685d5f11838f | 50 | (#) Three operation modes are available within this driver : |
mbed_official | 235:685d5f11838f | 51 | |
mbed_official | 235:685d5f11838f | 52 | *** Polling mode IO operation *** |
mbed_official | 235:685d5f11838f | 53 | ================================= |
mbed_official | 235:685d5f11838f | 54 | [..] |
mbed_official | 235:685d5f11838f | 55 | (+) Send an amount of data in blocking mode using HAL_USART_Transmit() |
mbed_official | 235:685d5f11838f | 56 | (+) Receive an amount of data in blocking mode using HAL_USART_Receive() |
mbed_official | 235:685d5f11838f | 57 | |
mbed_official | 235:685d5f11838f | 58 | *** Interrupt mode IO operation *** |
mbed_official | 235:685d5f11838f | 59 | =================================== |
mbed_official | 235:685d5f11838f | 60 | [..] |
mbed_official | 235:685d5f11838f | 61 | (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT() |
mbed_official | 235:685d5f11838f | 62 | (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 63 | add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback |
mbed_official | 235:685d5f11838f | 64 | (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 65 | add his own code by customization of function pointer HAL_USART_TxCpltCallback |
mbed_official | 235:685d5f11838f | 66 | (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT() |
mbed_official | 235:685d5f11838f | 67 | (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 68 | add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback |
mbed_official | 235:685d5f11838f | 69 | (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 70 | add his own code by customization of function pointer HAL_USART_RxCpltCallback |
mbed_official | 235:685d5f11838f | 71 | (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can |
mbed_official | 235:685d5f11838f | 72 | add his own code by customization of function pointer HAL_USART_ErrorCallback |
mbed_official | 235:685d5f11838f | 73 | |
mbed_official | 235:685d5f11838f | 74 | *** DMA mode IO operation *** |
mbed_official | 235:685d5f11838f | 75 | ============================== |
mbed_official | 235:685d5f11838f | 76 | [..] |
mbed_official | 235:685d5f11838f | 77 | (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA() |
mbed_official | 235:685d5f11838f | 78 | (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 79 | add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback |
mbed_official | 235:685d5f11838f | 80 | (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 81 | add his own code by customization of function pointer HAL_USART_TxCpltCallback |
mbed_official | 235:685d5f11838f | 82 | (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA() |
mbed_official | 235:685d5f11838f | 83 | (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 84 | add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback |
mbed_official | 235:685d5f11838f | 85 | (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can |
mbed_official | 235:685d5f11838f | 86 | add his own code by customization of function pointer HAL_USART_RxCpltCallback |
mbed_official | 235:685d5f11838f | 87 | (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can |
mbed_official | 235:685d5f11838f | 88 | add his own code by customization of function pointer HAL_USART_ErrorCallback |
mbed_official | 235:685d5f11838f | 89 | (+) Pause the DMA Transfer using HAL_USART_DMAPause() |
mbed_official | 235:685d5f11838f | 90 | (+) Resume the DMA Transfer using HAL_USART_DMAResume() |
mbed_official | 235:685d5f11838f | 91 | (+) Stop the DMA Transfer using HAL_USART_DMAStop() |
mbed_official | 235:685d5f11838f | 92 | |
mbed_official | 235:685d5f11838f | 93 | *** USART HAL driver macros list *** |
mbed_official | 235:685d5f11838f | 94 | ============================================= |
mbed_official | 235:685d5f11838f | 95 | [..] |
mbed_official | 235:685d5f11838f | 96 | Below the list of most used macros in USART HAL driver. |
mbed_official | 235:685d5f11838f | 97 | |
mbed_official | 235:685d5f11838f | 98 | (+) __HAL_USART_ENABLE: Enable the USART peripheral |
mbed_official | 235:685d5f11838f | 99 | (+) __HAL_USART_DISABLE: Disable the USART peripheral |
mbed_official | 235:685d5f11838f | 100 | (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not |
mbed_official | 235:685d5f11838f | 101 | (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag |
mbed_official | 235:685d5f11838f | 102 | (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt |
mbed_official | 235:685d5f11838f | 103 | (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt |
mbed_official | 235:685d5f11838f | 104 | |
mbed_official | 235:685d5f11838f | 105 | [..] |
mbed_official | 235:685d5f11838f | 106 | (@) You can refer to the USART HAL driver header file for more useful macros |
mbed_official | 235:685d5f11838f | 107 | |
mbed_official | 235:685d5f11838f | 108 | @endverbatim |
mbed_official | 235:685d5f11838f | 109 | ****************************************************************************** |
mbed_official | 235:685d5f11838f | 110 | * @attention |
mbed_official | 235:685d5f11838f | 111 | * |
mbed_official | 235:685d5f11838f | 112 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
mbed_official | 235:685d5f11838f | 113 | * |
mbed_official | 235:685d5f11838f | 114 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 235:685d5f11838f | 115 | * are permitted provided that the following conditions are met: |
mbed_official | 235:685d5f11838f | 116 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 235:685d5f11838f | 117 | * this list of conditions and the following disclaimer. |
mbed_official | 235:685d5f11838f | 118 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 235:685d5f11838f | 119 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 235:685d5f11838f | 120 | * and/or other materials provided with the distribution. |
mbed_official | 235:685d5f11838f | 121 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 235:685d5f11838f | 122 | * may be used to endorse or promote products derived from this software |
mbed_official | 235:685d5f11838f | 123 | * without specific prior written permission. |
mbed_official | 235:685d5f11838f | 124 | * |
mbed_official | 235:685d5f11838f | 125 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 235:685d5f11838f | 126 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 235:685d5f11838f | 127 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 235:685d5f11838f | 128 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 235:685d5f11838f | 129 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 235:685d5f11838f | 130 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 235:685d5f11838f | 131 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 235:685d5f11838f | 132 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 235:685d5f11838f | 133 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 235:685d5f11838f | 134 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 235:685d5f11838f | 135 | * |
mbed_official | 235:685d5f11838f | 136 | ****************************************************************************** |
mbed_official | 235:685d5f11838f | 137 | */ |
mbed_official | 235:685d5f11838f | 138 | |
mbed_official | 235:685d5f11838f | 139 | /* Includes ------------------------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 140 | #include "stm32f4xx_hal.h" |
mbed_official | 235:685d5f11838f | 141 | |
mbed_official | 235:685d5f11838f | 142 | /** @addtogroup STM32F4xx_HAL_Driver |
mbed_official | 235:685d5f11838f | 143 | * @{ |
mbed_official | 235:685d5f11838f | 144 | */ |
mbed_official | 235:685d5f11838f | 145 | |
mbed_official | 235:685d5f11838f | 146 | /** @defgroup USART |
mbed_official | 235:685d5f11838f | 147 | * @brief HAL USART Synchronous module driver |
mbed_official | 235:685d5f11838f | 148 | * @{ |
mbed_official | 235:685d5f11838f | 149 | */ |
mbed_official | 235:685d5f11838f | 150 | #ifdef HAL_USART_MODULE_ENABLED |
mbed_official | 235:685d5f11838f | 151 | /* Private typedef -----------------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 152 | /* Private define ------------------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 153 | #define DUMMY_DATA 0xFFFF |
mbed_official | 235:685d5f11838f | 154 | #define USART_TIMEOUT_VALUE 22000 |
mbed_official | 235:685d5f11838f | 155 | /* Private macro -------------------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 156 | /* Private variables ---------------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 157 | /* Private function prototypes -----------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 158 | static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart); |
mbed_official | 235:685d5f11838f | 159 | static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart); |
mbed_official | 235:685d5f11838f | 160 | static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart); |
mbed_official | 235:685d5f11838f | 161 | static void USART_SetConfig (USART_HandleTypeDef *husart); |
mbed_official | 235:685d5f11838f | 162 | static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 235:685d5f11838f | 163 | static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 235:685d5f11838f | 164 | static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 235:685d5f11838f | 165 | static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 235:685d5f11838f | 166 | static void USART_DMAError(DMA_HandleTypeDef *hdma); |
mbed_official | 235:685d5f11838f | 167 | static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout); |
mbed_official | 235:685d5f11838f | 168 | /* Private functions ---------------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 169 | |
mbed_official | 235:685d5f11838f | 170 | |
mbed_official | 235:685d5f11838f | 171 | /** @defgroup USART_Private_Functions |
mbed_official | 235:685d5f11838f | 172 | * @{ |
mbed_official | 235:685d5f11838f | 173 | */ |
mbed_official | 235:685d5f11838f | 174 | |
mbed_official | 235:685d5f11838f | 175 | /** @defgroup USART_Group1 USART Initialization and de-initialization functions |
mbed_official | 235:685d5f11838f | 176 | * @brief Initialization and Configuration functions |
mbed_official | 235:685d5f11838f | 177 | * |
mbed_official | 235:685d5f11838f | 178 | @verbatim |
mbed_official | 235:685d5f11838f | 179 | ============================================================================== |
mbed_official | 235:685d5f11838f | 180 | ##### Initialization and Configuration functions ##### |
mbed_official | 235:685d5f11838f | 181 | ============================================================================== |
mbed_official | 235:685d5f11838f | 182 | [..] |
mbed_official | 235:685d5f11838f | 183 | This subsection provides a set of functions allowing to initialize the USART |
mbed_official | 235:685d5f11838f | 184 | in asynchronous and in synchronous modes. |
mbed_official | 235:685d5f11838f | 185 | (+) For the asynchronous mode only these parameters can be configured: |
mbed_official | 235:685d5f11838f | 186 | (++) Baud Rate |
mbed_official | 235:685d5f11838f | 187 | (++) Word Length |
mbed_official | 235:685d5f11838f | 188 | (++) Stop Bit |
mbed_official | 235:685d5f11838f | 189 | (++) Parity: If the parity is enabled, then the MSB bit of the data written |
mbed_official | 235:685d5f11838f | 190 | in the data register is transmitted but is changed by the parity bit. |
mbed_official | 235:685d5f11838f | 191 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
mbed_official | 235:685d5f11838f | 192 | please refer to Reference manual for possible USART frame formats. |
mbed_official | 235:685d5f11838f | 193 | (++) USART polarity |
mbed_official | 235:685d5f11838f | 194 | (++) USART phase |
mbed_official | 235:685d5f11838f | 195 | (++) USART LastBit |
mbed_official | 235:685d5f11838f | 196 | (++) Receiver/transmitter modes |
mbed_official | 235:685d5f11838f | 197 | |
mbed_official | 235:685d5f11838f | 198 | [..] |
mbed_official | 235:685d5f11838f | 199 | The HAL_USART_Init() function follows the USART synchronous configuration |
mbed_official | 235:685d5f11838f | 200 | procedure (details for the procedure are available in reference manual (RM0329)). |
mbed_official | 235:685d5f11838f | 201 | |
mbed_official | 235:685d5f11838f | 202 | @endverbatim |
mbed_official | 235:685d5f11838f | 203 | * @{ |
mbed_official | 235:685d5f11838f | 204 | */ |
mbed_official | 235:685d5f11838f | 205 | |
mbed_official | 235:685d5f11838f | 206 | /** |
mbed_official | 235:685d5f11838f | 207 | * @brief Initializes the USART mode according to the specified |
mbed_official | 235:685d5f11838f | 208 | * parameters in the USART_InitTypeDef and create the associated handle. |
mbed_official | 235:685d5f11838f | 209 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 210 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 211 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 212 | */ |
mbed_official | 235:685d5f11838f | 213 | HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 214 | { |
mbed_official | 235:685d5f11838f | 215 | /* Check the USART handle allocation */ |
mbed_official | 235:685d5f11838f | 216 | if(husart == NULL) |
mbed_official | 235:685d5f11838f | 217 | { |
mbed_official | 235:685d5f11838f | 218 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 219 | } |
mbed_official | 235:685d5f11838f | 220 | |
mbed_official | 235:685d5f11838f | 221 | /* Check the parameters */ |
mbed_official | 235:685d5f11838f | 222 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
mbed_official | 235:685d5f11838f | 223 | |
mbed_official | 235:685d5f11838f | 224 | if(husart->State == HAL_USART_STATE_RESET) |
mbed_official | 235:685d5f11838f | 225 | { |
mbed_official | 235:685d5f11838f | 226 | /* Init the low level hardware */ |
mbed_official | 235:685d5f11838f | 227 | HAL_USART_MspInit(husart); |
mbed_official | 235:685d5f11838f | 228 | } |
mbed_official | 235:685d5f11838f | 229 | |
mbed_official | 235:685d5f11838f | 230 | husart->State = HAL_USART_STATE_BUSY; |
mbed_official | 235:685d5f11838f | 231 | |
mbed_official | 235:685d5f11838f | 232 | /* Set the USART Communication parameters */ |
mbed_official | 235:685d5f11838f | 233 | USART_SetConfig(husart); |
mbed_official | 235:685d5f11838f | 234 | |
mbed_official | 235:685d5f11838f | 235 | /* In USART mode, the following bits must be kept cleared: |
mbed_official | 235:685d5f11838f | 236 | - LINEN bit in the USART_CR2 register |
mbed_official | 235:685d5f11838f | 237 | - HDSEL, SCEN and IREN bits in the USART_CR3 register */ |
mbed_official | 235:685d5f11838f | 238 | husart->Instance->CR2 &= ~USART_CR2_LINEN; |
mbed_official | 235:685d5f11838f | 239 | husart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN | USART_CR3_HDSEL); |
mbed_official | 235:685d5f11838f | 240 | |
mbed_official | 235:685d5f11838f | 241 | /* Enable the Peripheral */ |
mbed_official | 235:685d5f11838f | 242 | __USART_ENABLE(husart); |
mbed_official | 235:685d5f11838f | 243 | |
mbed_official | 235:685d5f11838f | 244 | /* Initialize the USART state */ |
mbed_official | 235:685d5f11838f | 245 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 246 | husart->State= HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 247 | |
mbed_official | 235:685d5f11838f | 248 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 249 | } |
mbed_official | 235:685d5f11838f | 250 | |
mbed_official | 235:685d5f11838f | 251 | /** |
mbed_official | 235:685d5f11838f | 252 | * @brief DeInitializes the USART peripheral. |
mbed_official | 235:685d5f11838f | 253 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 254 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 255 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 256 | */ |
mbed_official | 235:685d5f11838f | 257 | HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 258 | { |
mbed_official | 235:685d5f11838f | 259 | /* Check the USART handle allocation */ |
mbed_official | 235:685d5f11838f | 260 | if(husart == NULL) |
mbed_official | 235:685d5f11838f | 261 | { |
mbed_official | 235:685d5f11838f | 262 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 263 | } |
mbed_official | 235:685d5f11838f | 264 | |
mbed_official | 235:685d5f11838f | 265 | /* Check the parameters */ |
mbed_official | 235:685d5f11838f | 266 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
mbed_official | 235:685d5f11838f | 267 | |
mbed_official | 235:685d5f11838f | 268 | husart->State = HAL_USART_STATE_BUSY; |
mbed_official | 235:685d5f11838f | 269 | |
mbed_official | 235:685d5f11838f | 270 | /* DeInit the low level hardware */ |
mbed_official | 235:685d5f11838f | 271 | HAL_USART_MspDeInit(husart); |
mbed_official | 235:685d5f11838f | 272 | |
mbed_official | 235:685d5f11838f | 273 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 274 | husart->State = HAL_USART_STATE_RESET; |
mbed_official | 235:685d5f11838f | 275 | |
mbed_official | 235:685d5f11838f | 276 | /* Release Lock */ |
mbed_official | 235:685d5f11838f | 277 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 278 | |
mbed_official | 235:685d5f11838f | 279 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 280 | } |
mbed_official | 235:685d5f11838f | 281 | |
mbed_official | 235:685d5f11838f | 282 | /** |
mbed_official | 235:685d5f11838f | 283 | * @brief USART MSP Init. |
mbed_official | 235:685d5f11838f | 284 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 285 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 286 | * @retval None |
mbed_official | 235:685d5f11838f | 287 | */ |
mbed_official | 235:685d5f11838f | 288 | __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 289 | { |
mbed_official | 235:685d5f11838f | 290 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 291 | the HAL_USART_MspInit could be implenetd in the user file |
mbed_official | 235:685d5f11838f | 292 | */ |
mbed_official | 235:685d5f11838f | 293 | } |
mbed_official | 235:685d5f11838f | 294 | |
mbed_official | 235:685d5f11838f | 295 | /** |
mbed_official | 235:685d5f11838f | 296 | * @brief USART MSP DeInit. |
mbed_official | 235:685d5f11838f | 297 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 298 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 299 | * @retval None |
mbed_official | 235:685d5f11838f | 300 | */ |
mbed_official | 235:685d5f11838f | 301 | __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 302 | { |
mbed_official | 235:685d5f11838f | 303 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 304 | the HAL_USART_MspDeInit could be implenetd in the user file |
mbed_official | 235:685d5f11838f | 305 | */ |
mbed_official | 235:685d5f11838f | 306 | } |
mbed_official | 235:685d5f11838f | 307 | |
mbed_official | 235:685d5f11838f | 308 | /** |
mbed_official | 235:685d5f11838f | 309 | * @} |
mbed_official | 235:685d5f11838f | 310 | */ |
mbed_official | 235:685d5f11838f | 311 | |
mbed_official | 235:685d5f11838f | 312 | /** @defgroup USART_Group2 IO operation functions |
mbed_official | 235:685d5f11838f | 313 | * @brief USART Transmit and Receive functions |
mbed_official | 235:685d5f11838f | 314 | * |
mbed_official | 235:685d5f11838f | 315 | @verbatim |
mbed_official | 235:685d5f11838f | 316 | ============================================================================== |
mbed_official | 235:685d5f11838f | 317 | ##### IO operation functions ##### |
mbed_official | 235:685d5f11838f | 318 | ============================================================================== |
mbed_official | 235:685d5f11838f | 319 | [..] |
mbed_official | 235:685d5f11838f | 320 | This subsection provides a set of functions allowing to manage the USART synchronous |
mbed_official | 235:685d5f11838f | 321 | data transfers. |
mbed_official | 235:685d5f11838f | 322 | |
mbed_official | 235:685d5f11838f | 323 | [..] |
mbed_official | 235:685d5f11838f | 324 | The USART supports master mode only: it cannot receive or send data related to an input |
mbed_official | 235:685d5f11838f | 325 | clock (SCLK is always an output). |
mbed_official | 235:685d5f11838f | 326 | |
mbed_official | 235:685d5f11838f | 327 | (#) There are two modes of transfer: |
mbed_official | 235:685d5f11838f | 328 | (++) Blocking mode: The communication is performed in polling mode. |
mbed_official | 235:685d5f11838f | 329 | The HAL status of all data processing is returned by the same function |
mbed_official | 235:685d5f11838f | 330 | after finishing transfer. |
mbed_official | 235:685d5f11838f | 331 | (++) No-Blocking mode: The communication is performed using Interrupts |
mbed_official | 235:685d5f11838f | 332 | or DMA, These API's return the HAL status. |
mbed_official | 235:685d5f11838f | 333 | The end of the data processing will be indicated through the |
mbed_official | 235:685d5f11838f | 334 | dedicated USART IRQ when using Interrupt mode or the DMA IRQ when |
mbed_official | 235:685d5f11838f | 335 | using DMA mode. |
mbed_official | 235:685d5f11838f | 336 | The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() |
mbed_official | 235:685d5f11838f | 337 | user callbacks |
mbed_official | 235:685d5f11838f | 338 | will be executed respectivelly at the end of the transmit or Receive process |
mbed_official | 235:685d5f11838f | 339 | The HAL_USART_ErrorCallback() user callback will be executed when a communication |
mbed_official | 235:685d5f11838f | 340 | error is detected |
mbed_official | 235:685d5f11838f | 341 | |
mbed_official | 235:685d5f11838f | 342 | (#) Blocking mode APIs are : |
mbed_official | 235:685d5f11838f | 343 | (++) HAL_USART_Transmit() in simplex mode |
mbed_official | 235:685d5f11838f | 344 | (++) HAL_USART_Receive() in full duplex receive only |
mbed_official | 235:685d5f11838f | 345 | (++) HAL_USART_TransmitReceive() in full duplex mode |
mbed_official | 235:685d5f11838f | 346 | |
mbed_official | 235:685d5f11838f | 347 | (#) Non Blocking mode APIs with Interrupt are : |
mbed_official | 235:685d5f11838f | 348 | (++) HAL_USART_Transmit_IT()in simplex mode |
mbed_official | 235:685d5f11838f | 349 | (++) HAL_USART_Receive_IT() in full duplex receive only |
mbed_official | 235:685d5f11838f | 350 | (++) HAL_USART_TransmitReceive_IT() in full duplex mode |
mbed_official | 235:685d5f11838f | 351 | (++) HAL_USART_IRQHandler() |
mbed_official | 235:685d5f11838f | 352 | |
mbed_official | 235:685d5f11838f | 353 | (#) Non Blocking mode functions with DMA are : |
mbed_official | 235:685d5f11838f | 354 | (++) HAL_USART_Transmit_DMA()in simplex mode |
mbed_official | 235:685d5f11838f | 355 | (++) HAL_USART_Receive_DMA() in full duplex receive only |
mbed_official | 235:685d5f11838f | 356 | (++) HAL_USART_TransmitReceie_DMA() in full duplex mode |
mbed_official | 235:685d5f11838f | 357 | |
mbed_official | 235:685d5f11838f | 358 | (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: |
mbed_official | 235:685d5f11838f | 359 | (++) HAL_USART_TxCpltCallback() |
mbed_official | 235:685d5f11838f | 360 | (++) HAL_USART_RxCpltCallback() |
mbed_official | 235:685d5f11838f | 361 | (++) HAL_USART_ErrorCallback() |
mbed_official | 235:685d5f11838f | 362 | (++) HAL_USART_TxRxCpltCallback() |
mbed_official | 235:685d5f11838f | 363 | |
mbed_official | 235:685d5f11838f | 364 | @endverbatim |
mbed_official | 235:685d5f11838f | 365 | * @{ |
mbed_official | 235:685d5f11838f | 366 | */ |
mbed_official | 235:685d5f11838f | 367 | |
mbed_official | 235:685d5f11838f | 368 | /** |
mbed_official | 235:685d5f11838f | 369 | * @brief Simplex Send an amount of data in blocking mode. |
mbed_official | 235:685d5f11838f | 370 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 371 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 372 | * @param pTxData: Pointer to data buffer |
mbed_official | 235:685d5f11838f | 373 | * @param Size: Amount of data to be sent |
mbed_official | 235:685d5f11838f | 374 | * @param Timeout: Timeout duration |
mbed_official | 235:685d5f11838f | 375 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 376 | */ |
mbed_official | 235:685d5f11838f | 377 | HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout) |
mbed_official | 235:685d5f11838f | 378 | { |
mbed_official | 235:685d5f11838f | 379 | uint16_t* tmp; |
mbed_official | 235:685d5f11838f | 380 | |
mbed_official | 235:685d5f11838f | 381 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 382 | { |
mbed_official | 235:685d5f11838f | 383 | if((pTxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 384 | { |
mbed_official | 235:685d5f11838f | 385 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 386 | } |
mbed_official | 235:685d5f11838f | 387 | |
mbed_official | 235:685d5f11838f | 388 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 389 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 390 | |
mbed_official | 235:685d5f11838f | 391 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 392 | husart->State = HAL_USART_STATE_BUSY_TX; |
mbed_official | 235:685d5f11838f | 393 | |
mbed_official | 235:685d5f11838f | 394 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 395 | husart->TxXferCount = Size; |
mbed_official | 235:685d5f11838f | 396 | while(husart->TxXferCount > 0) |
mbed_official | 235:685d5f11838f | 397 | { |
mbed_official | 235:685d5f11838f | 398 | husart->TxXferCount--; |
mbed_official | 235:685d5f11838f | 399 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 400 | { |
mbed_official | 235:685d5f11838f | 401 | /* Wait for TC flag in order to write data in DR */ |
mbed_official | 235:685d5f11838f | 402 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 403 | { |
mbed_official | 235:685d5f11838f | 404 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 405 | } |
mbed_official | 235:685d5f11838f | 406 | tmp = (uint16_t*) pTxData; |
mbed_official | 235:685d5f11838f | 407 | husart->Instance->DR = (*tmp & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 408 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 409 | { |
mbed_official | 235:685d5f11838f | 410 | pTxData += 2; |
mbed_official | 235:685d5f11838f | 411 | } |
mbed_official | 235:685d5f11838f | 412 | else |
mbed_official | 235:685d5f11838f | 413 | { |
mbed_official | 235:685d5f11838f | 414 | pTxData += 1; |
mbed_official | 235:685d5f11838f | 415 | } |
mbed_official | 235:685d5f11838f | 416 | } |
mbed_official | 235:685d5f11838f | 417 | else |
mbed_official | 235:685d5f11838f | 418 | { |
mbed_official | 235:685d5f11838f | 419 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 420 | { |
mbed_official | 235:685d5f11838f | 421 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 422 | } |
mbed_official | 235:685d5f11838f | 423 | husart->Instance->DR = (*pTxData++ & (uint8_t)0xFF); |
mbed_official | 235:685d5f11838f | 424 | } |
mbed_official | 235:685d5f11838f | 425 | } |
mbed_official | 235:685d5f11838f | 426 | |
mbed_official | 235:685d5f11838f | 427 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 428 | { |
mbed_official | 235:685d5f11838f | 429 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 430 | } |
mbed_official | 235:685d5f11838f | 431 | |
mbed_official | 235:685d5f11838f | 432 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 433 | |
mbed_official | 235:685d5f11838f | 434 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 435 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 436 | |
mbed_official | 235:685d5f11838f | 437 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 438 | } |
mbed_official | 235:685d5f11838f | 439 | else |
mbed_official | 235:685d5f11838f | 440 | { |
mbed_official | 235:685d5f11838f | 441 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 442 | } |
mbed_official | 235:685d5f11838f | 443 | } |
mbed_official | 235:685d5f11838f | 444 | |
mbed_official | 235:685d5f11838f | 445 | /** |
mbed_official | 235:685d5f11838f | 446 | * @brief Full-Duplex Receive an amount of data in blocking mode. |
mbed_official | 235:685d5f11838f | 447 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 448 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 449 | * @param pRxData: Pointer to data buffer |
mbed_official | 235:685d5f11838f | 450 | * @param Size: Amount of data to be received |
mbed_official | 235:685d5f11838f | 451 | * @param Timeout: Timeout duration |
mbed_official | 235:685d5f11838f | 452 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 453 | */ |
mbed_official | 235:685d5f11838f | 454 | HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
mbed_official | 235:685d5f11838f | 455 | { |
mbed_official | 235:685d5f11838f | 456 | uint16_t* tmp; |
mbed_official | 235:685d5f11838f | 457 | |
mbed_official | 235:685d5f11838f | 458 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 459 | { |
mbed_official | 235:685d5f11838f | 460 | if((pRxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 461 | { |
mbed_official | 235:685d5f11838f | 462 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 463 | } |
mbed_official | 235:685d5f11838f | 464 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 465 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 466 | |
mbed_official | 235:685d5f11838f | 467 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 468 | husart->State = HAL_USART_STATE_BUSY_RX; |
mbed_official | 235:685d5f11838f | 469 | |
mbed_official | 235:685d5f11838f | 470 | husart->RxXferSize = Size; |
mbed_official | 235:685d5f11838f | 471 | husart->RxXferCount = Size; |
mbed_official | 235:685d5f11838f | 472 | /* Check the remain data to be received */ |
mbed_official | 235:685d5f11838f | 473 | while(husart->RxXferCount > 0) |
mbed_official | 235:685d5f11838f | 474 | { |
mbed_official | 235:685d5f11838f | 475 | husart->RxXferCount--; |
mbed_official | 235:685d5f11838f | 476 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 477 | { |
mbed_official | 235:685d5f11838f | 478 | /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */ |
mbed_official | 235:685d5f11838f | 479 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 480 | { |
mbed_official | 235:685d5f11838f | 481 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 482 | } |
mbed_official | 235:685d5f11838f | 483 | /* Send dummy byte in order to generate clock */ |
mbed_official | 235:685d5f11838f | 484 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 485 | |
mbed_official | 235:685d5f11838f | 486 | /* Wait for RXNE Flag */ |
mbed_official | 235:685d5f11838f | 487 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 488 | { |
mbed_official | 235:685d5f11838f | 489 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 490 | } |
mbed_official | 235:685d5f11838f | 491 | tmp = (uint16_t*) pRxData ; |
mbed_official | 235:685d5f11838f | 492 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 493 | { |
mbed_official | 235:685d5f11838f | 494 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 495 | pRxData +=2; |
mbed_official | 235:685d5f11838f | 496 | } |
mbed_official | 235:685d5f11838f | 497 | else |
mbed_official | 235:685d5f11838f | 498 | { |
mbed_official | 235:685d5f11838f | 499 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
mbed_official | 235:685d5f11838f | 500 | pRxData +=1; |
mbed_official | 235:685d5f11838f | 501 | } |
mbed_official | 235:685d5f11838f | 502 | } |
mbed_official | 235:685d5f11838f | 503 | else |
mbed_official | 235:685d5f11838f | 504 | { |
mbed_official | 235:685d5f11838f | 505 | /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */ |
mbed_official | 235:685d5f11838f | 506 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 507 | { |
mbed_official | 235:685d5f11838f | 508 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 509 | } |
mbed_official | 235:685d5f11838f | 510 | |
mbed_official | 235:685d5f11838f | 511 | /* Send Dummy Byte in order to generate clock */ |
mbed_official | 235:685d5f11838f | 512 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF); |
mbed_official | 235:685d5f11838f | 513 | |
mbed_official | 235:685d5f11838f | 514 | /* Wait until RXNE flag is set to receive the byte */ |
mbed_official | 235:685d5f11838f | 515 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 516 | { |
mbed_official | 235:685d5f11838f | 517 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 518 | } |
mbed_official | 235:685d5f11838f | 519 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 520 | { |
mbed_official | 235:685d5f11838f | 521 | /* Receive data */ |
mbed_official | 235:685d5f11838f | 522 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 523 | } |
mbed_official | 235:685d5f11838f | 524 | else |
mbed_official | 235:685d5f11838f | 525 | { |
mbed_official | 235:685d5f11838f | 526 | /* Receive data */ |
mbed_official | 235:685d5f11838f | 527 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
mbed_official | 235:685d5f11838f | 528 | } |
mbed_official | 235:685d5f11838f | 529 | |
mbed_official | 235:685d5f11838f | 530 | } |
mbed_official | 235:685d5f11838f | 531 | } |
mbed_official | 235:685d5f11838f | 532 | |
mbed_official | 235:685d5f11838f | 533 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 534 | |
mbed_official | 235:685d5f11838f | 535 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 536 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 537 | |
mbed_official | 235:685d5f11838f | 538 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 539 | } |
mbed_official | 235:685d5f11838f | 540 | else |
mbed_official | 235:685d5f11838f | 541 | { |
mbed_official | 235:685d5f11838f | 542 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 543 | } |
mbed_official | 235:685d5f11838f | 544 | } |
mbed_official | 235:685d5f11838f | 545 | |
mbed_official | 235:685d5f11838f | 546 | /** |
mbed_official | 235:685d5f11838f | 547 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (blocking mode). |
mbed_official | 235:685d5f11838f | 548 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 549 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 550 | * @param pTxData: Pointer to data transmitted buffer |
mbed_official | 235:685d5f11838f | 551 | * @param pRxData: Pointer to data received buffer |
mbed_official | 235:685d5f11838f | 552 | * @param Size: Amount of data to be sent |
mbed_official | 235:685d5f11838f | 553 | * @param Timeout: Timeout duration |
mbed_official | 235:685d5f11838f | 554 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 555 | */ |
mbed_official | 235:685d5f11838f | 556 | HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
mbed_official | 235:685d5f11838f | 557 | { |
mbed_official | 235:685d5f11838f | 558 | uint16_t* tmp; |
mbed_official | 235:685d5f11838f | 559 | |
mbed_official | 235:685d5f11838f | 560 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 561 | { |
mbed_official | 235:685d5f11838f | 562 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 563 | { |
mbed_official | 235:685d5f11838f | 564 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 565 | } |
mbed_official | 235:685d5f11838f | 566 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 567 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 568 | |
mbed_official | 235:685d5f11838f | 569 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 570 | husart->State = HAL_USART_STATE_BUSY_RX; |
mbed_official | 235:685d5f11838f | 571 | |
mbed_official | 235:685d5f11838f | 572 | husart->RxXferSize = Size; |
mbed_official | 235:685d5f11838f | 573 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 574 | husart->TxXferCount = Size; |
mbed_official | 235:685d5f11838f | 575 | husart->RxXferCount = Size; |
mbed_official | 235:685d5f11838f | 576 | |
mbed_official | 235:685d5f11838f | 577 | /* Check the remain data to be received */ |
mbed_official | 235:685d5f11838f | 578 | while(husart->TxXferCount > 0) |
mbed_official | 235:685d5f11838f | 579 | { |
mbed_official | 235:685d5f11838f | 580 | husart->TxXferCount--; |
mbed_official | 235:685d5f11838f | 581 | husart->RxXferCount--; |
mbed_official | 235:685d5f11838f | 582 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 583 | { |
mbed_official | 235:685d5f11838f | 584 | /* Wait for TC flag in order to write data in DR */ |
mbed_official | 235:685d5f11838f | 585 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 586 | { |
mbed_official | 235:685d5f11838f | 587 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 588 | } |
mbed_official | 235:685d5f11838f | 589 | tmp = (uint16_t*) pTxData; |
mbed_official | 235:685d5f11838f | 590 | husart->Instance->DR = (*tmp & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 591 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 592 | { |
mbed_official | 235:685d5f11838f | 593 | pTxData += 2; |
mbed_official | 235:685d5f11838f | 594 | } |
mbed_official | 235:685d5f11838f | 595 | else |
mbed_official | 235:685d5f11838f | 596 | { |
mbed_official | 235:685d5f11838f | 597 | pTxData += 1; |
mbed_official | 235:685d5f11838f | 598 | } |
mbed_official | 235:685d5f11838f | 599 | |
mbed_official | 235:685d5f11838f | 600 | /* Wait for RXNE Flag */ |
mbed_official | 235:685d5f11838f | 601 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 602 | { |
mbed_official | 235:685d5f11838f | 603 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 604 | } |
mbed_official | 235:685d5f11838f | 605 | tmp = (uint16_t*) pRxData ; |
mbed_official | 235:685d5f11838f | 606 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 607 | { |
mbed_official | 235:685d5f11838f | 608 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 609 | pRxData += 2; |
mbed_official | 235:685d5f11838f | 610 | } |
mbed_official | 235:685d5f11838f | 611 | else |
mbed_official | 235:685d5f11838f | 612 | { |
mbed_official | 235:685d5f11838f | 613 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
mbed_official | 235:685d5f11838f | 614 | pRxData += 1; |
mbed_official | 235:685d5f11838f | 615 | } |
mbed_official | 235:685d5f11838f | 616 | } |
mbed_official | 235:685d5f11838f | 617 | else |
mbed_official | 235:685d5f11838f | 618 | { |
mbed_official | 235:685d5f11838f | 619 | /* Wait for TC flag in order to write data in DR */ |
mbed_official | 235:685d5f11838f | 620 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 621 | { |
mbed_official | 235:685d5f11838f | 622 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 623 | } |
mbed_official | 235:685d5f11838f | 624 | husart->Instance->DR = (*pTxData++ & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 625 | |
mbed_official | 235:685d5f11838f | 626 | /* Wait for RXNE Flag */ |
mbed_official | 235:685d5f11838f | 627 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK) |
mbed_official | 235:685d5f11838f | 628 | { |
mbed_official | 235:685d5f11838f | 629 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 630 | } |
mbed_official | 235:685d5f11838f | 631 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 632 | { |
mbed_official | 235:685d5f11838f | 633 | /* Receive data */ |
mbed_official | 235:685d5f11838f | 634 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 635 | } |
mbed_official | 235:685d5f11838f | 636 | else |
mbed_official | 235:685d5f11838f | 637 | { |
mbed_official | 235:685d5f11838f | 638 | /* Receive data */ |
mbed_official | 235:685d5f11838f | 639 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
mbed_official | 235:685d5f11838f | 640 | } |
mbed_official | 235:685d5f11838f | 641 | } |
mbed_official | 235:685d5f11838f | 642 | } |
mbed_official | 235:685d5f11838f | 643 | |
mbed_official | 235:685d5f11838f | 644 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 645 | |
mbed_official | 235:685d5f11838f | 646 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 647 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 648 | |
mbed_official | 235:685d5f11838f | 649 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 650 | } |
mbed_official | 235:685d5f11838f | 651 | else |
mbed_official | 235:685d5f11838f | 652 | { |
mbed_official | 235:685d5f11838f | 653 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 654 | } |
mbed_official | 235:685d5f11838f | 655 | } |
mbed_official | 235:685d5f11838f | 656 | |
mbed_official | 235:685d5f11838f | 657 | /** |
mbed_official | 235:685d5f11838f | 658 | * @brief Simplex Send an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 659 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 660 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 661 | * @param pTxData: Pointer to data buffer |
mbed_official | 235:685d5f11838f | 662 | * @param Size: Amount of data to be sent |
mbed_official | 235:685d5f11838f | 663 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 664 | * @note The USART errors are not managed to avoid the overrun error. |
mbed_official | 235:685d5f11838f | 665 | */ |
mbed_official | 235:685d5f11838f | 666 | HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size) |
mbed_official | 235:685d5f11838f | 667 | { |
mbed_official | 235:685d5f11838f | 668 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 669 | { |
mbed_official | 235:685d5f11838f | 670 | if((pTxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 671 | { |
mbed_official | 235:685d5f11838f | 672 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 673 | } |
mbed_official | 235:685d5f11838f | 674 | |
mbed_official | 235:685d5f11838f | 675 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 676 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 677 | |
mbed_official | 235:685d5f11838f | 678 | husart->pTxBuffPtr = pTxData; |
mbed_official | 235:685d5f11838f | 679 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 680 | husart->TxXferCount = Size; |
mbed_official | 235:685d5f11838f | 681 | |
mbed_official | 235:685d5f11838f | 682 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 683 | husart->State = HAL_USART_STATE_BUSY_TX; |
mbed_official | 235:685d5f11838f | 684 | |
mbed_official | 235:685d5f11838f | 685 | /* The USART Error Interrupts: (Frame error, Noise error, Overrun error) |
mbed_official | 235:685d5f11838f | 686 | are not managed by the USART transmit process to avoid the overrun interrupt |
mbed_official | 235:685d5f11838f | 687 | when the USART mode is configured for transmit and receive "USART_MODE_TX_RX" |
mbed_official | 235:685d5f11838f | 688 | to benefit for the frame error and noise interrupts the USART mode should be |
mbed_official | 235:685d5f11838f | 689 | configured only for transmit "USART_MODE_TX" |
mbed_official | 235:685d5f11838f | 690 | The __USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error, |
mbed_official | 235:685d5f11838f | 691 | Noise error interrupt */ |
mbed_official | 235:685d5f11838f | 692 | |
mbed_official | 235:685d5f11838f | 693 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 694 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 695 | |
mbed_official | 235:685d5f11838f | 696 | /* Enable the USART Transmit Data Register Empty Interrupt */ |
mbed_official | 235:685d5f11838f | 697 | __USART_ENABLE_IT(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 698 | |
mbed_official | 235:685d5f11838f | 699 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 700 | } |
mbed_official | 235:685d5f11838f | 701 | else |
mbed_official | 235:685d5f11838f | 702 | { |
mbed_official | 235:685d5f11838f | 703 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 704 | } |
mbed_official | 235:685d5f11838f | 705 | } |
mbed_official | 235:685d5f11838f | 706 | |
mbed_official | 235:685d5f11838f | 707 | /** |
mbed_official | 235:685d5f11838f | 708 | * @brief Simplex Receive an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 709 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 710 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 711 | * @param pRxData: Pointer to data buffer |
mbed_official | 235:685d5f11838f | 712 | * @param Size: Amount of data to be received |
mbed_official | 235:685d5f11838f | 713 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 714 | */ |
mbed_official | 235:685d5f11838f | 715 | HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) |
mbed_official | 235:685d5f11838f | 716 | { |
mbed_official | 235:685d5f11838f | 717 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 718 | { |
mbed_official | 235:685d5f11838f | 719 | if((pRxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 720 | { |
mbed_official | 235:685d5f11838f | 721 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 722 | } |
mbed_official | 235:685d5f11838f | 723 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 724 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 725 | |
mbed_official | 235:685d5f11838f | 726 | husart->pRxBuffPtr = pRxData; |
mbed_official | 235:685d5f11838f | 727 | husart->RxXferSize = Size; |
mbed_official | 235:685d5f11838f | 728 | husart->RxXferCount = Size; |
mbed_official | 235:685d5f11838f | 729 | |
mbed_official | 235:685d5f11838f | 730 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 731 | husart->State = HAL_USART_STATE_BUSY_RX; |
mbed_official | 235:685d5f11838f | 732 | |
mbed_official | 235:685d5f11838f | 733 | /* Enable the USART Data Register not empty Interrupt */ |
mbed_official | 235:685d5f11838f | 734 | __USART_ENABLE_IT(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 735 | |
mbed_official | 235:685d5f11838f | 736 | /* Enable the USART Parity Error Interrupt */ |
mbed_official | 235:685d5f11838f | 737 | __USART_ENABLE_IT(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 738 | |
mbed_official | 235:685d5f11838f | 739 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
mbed_official | 235:685d5f11838f | 740 | __USART_ENABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 741 | |
mbed_official | 235:685d5f11838f | 742 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 743 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 744 | |
mbed_official | 235:685d5f11838f | 745 | /* Send dummy byte in order to generate the clock for the slave to send data */ |
mbed_official | 235:685d5f11838f | 746 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 747 | |
mbed_official | 235:685d5f11838f | 748 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 749 | } |
mbed_official | 235:685d5f11838f | 750 | else |
mbed_official | 235:685d5f11838f | 751 | { |
mbed_official | 235:685d5f11838f | 752 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 753 | } |
mbed_official | 235:685d5f11838f | 754 | } |
mbed_official | 235:685d5f11838f | 755 | |
mbed_official | 235:685d5f11838f | 756 | /** |
mbed_official | 235:685d5f11838f | 757 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking). |
mbed_official | 235:685d5f11838f | 758 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 759 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 760 | * @param pTxData: Pointer to data transmitted buffer |
mbed_official | 235:685d5f11838f | 761 | * @param pRxData: Pointer to data received buffer |
mbed_official | 235:685d5f11838f | 762 | * @param Size: Amount of data to be received |
mbed_official | 235:685d5f11838f | 763 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 764 | */ |
mbed_official | 235:685d5f11838f | 765 | HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
mbed_official | 235:685d5f11838f | 766 | { |
mbed_official | 235:685d5f11838f | 767 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 768 | { |
mbed_official | 235:685d5f11838f | 769 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 770 | { |
mbed_official | 235:685d5f11838f | 771 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 772 | } |
mbed_official | 235:685d5f11838f | 773 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 774 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 775 | |
mbed_official | 235:685d5f11838f | 776 | husart->pRxBuffPtr = pRxData; |
mbed_official | 235:685d5f11838f | 777 | husart->RxXferSize = Size; |
mbed_official | 235:685d5f11838f | 778 | husart->RxXferCount = Size; |
mbed_official | 235:685d5f11838f | 779 | husart->pTxBuffPtr = pTxData; |
mbed_official | 235:685d5f11838f | 780 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 781 | husart->TxXferCount = Size; |
mbed_official | 235:685d5f11838f | 782 | |
mbed_official | 235:685d5f11838f | 783 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 784 | husart->State = HAL_USART_STATE_BUSY_TX_RX; |
mbed_official | 235:685d5f11838f | 785 | |
mbed_official | 235:685d5f11838f | 786 | /* Enable the USART Data Register not empty Interrupt */ |
mbed_official | 235:685d5f11838f | 787 | __USART_ENABLE_IT(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 788 | |
mbed_official | 235:685d5f11838f | 789 | /* Enable the USART Parity Error Interrupt */ |
mbed_official | 235:685d5f11838f | 790 | __USART_ENABLE_IT(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 791 | |
mbed_official | 235:685d5f11838f | 792 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
mbed_official | 235:685d5f11838f | 793 | __USART_ENABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 794 | |
mbed_official | 235:685d5f11838f | 795 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 796 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 797 | |
mbed_official | 235:685d5f11838f | 798 | /* Enable the USART Transmit Data Register Empty Interrupt */ |
mbed_official | 235:685d5f11838f | 799 | __USART_ENABLE_IT(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 800 | |
mbed_official | 235:685d5f11838f | 801 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 802 | } |
mbed_official | 235:685d5f11838f | 803 | else |
mbed_official | 235:685d5f11838f | 804 | { |
mbed_official | 235:685d5f11838f | 805 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 806 | } |
mbed_official | 235:685d5f11838f | 807 | } |
mbed_official | 235:685d5f11838f | 808 | |
mbed_official | 235:685d5f11838f | 809 | /** |
mbed_official | 235:685d5f11838f | 810 | * @brief Simplex Send an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 811 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 812 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 813 | * @param pTxData: Pointer to data buffer |
mbed_official | 235:685d5f11838f | 814 | * @param Size: Amount of data to be sent |
mbed_official | 235:685d5f11838f | 815 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 816 | */ |
mbed_official | 235:685d5f11838f | 817 | HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size) |
mbed_official | 235:685d5f11838f | 818 | { |
mbed_official | 235:685d5f11838f | 819 | uint32_t *tmp; |
mbed_official | 235:685d5f11838f | 820 | |
mbed_official | 235:685d5f11838f | 821 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 822 | { |
mbed_official | 235:685d5f11838f | 823 | if((pTxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 824 | { |
mbed_official | 235:685d5f11838f | 825 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 826 | } |
mbed_official | 235:685d5f11838f | 827 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 828 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 829 | |
mbed_official | 235:685d5f11838f | 830 | husart->pTxBuffPtr = pTxData; |
mbed_official | 235:685d5f11838f | 831 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 832 | husart->TxXferCount = Size; |
mbed_official | 235:685d5f11838f | 833 | |
mbed_official | 235:685d5f11838f | 834 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 835 | husart->State = HAL_USART_STATE_BUSY_TX; |
mbed_official | 235:685d5f11838f | 836 | |
mbed_official | 235:685d5f11838f | 837 | /* Set the USART DMA transfer complete callback */ |
mbed_official | 235:685d5f11838f | 838 | husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; |
mbed_official | 235:685d5f11838f | 839 | |
mbed_official | 235:685d5f11838f | 840 | /* Set the USART DMA Half transfer complete callback */ |
mbed_official | 235:685d5f11838f | 841 | husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; |
mbed_official | 235:685d5f11838f | 842 | |
mbed_official | 235:685d5f11838f | 843 | /* Set the DMA error callback */ |
mbed_official | 235:685d5f11838f | 844 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
mbed_official | 235:685d5f11838f | 845 | |
mbed_official | 235:685d5f11838f | 846 | /* Enable the USART transmit DMA Stream */ |
mbed_official | 235:685d5f11838f | 847 | tmp = (uint32_t*)&pTxData; |
mbed_official | 235:685d5f11838f | 848 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size); |
mbed_official | 235:685d5f11838f | 849 | |
mbed_official | 235:685d5f11838f | 850 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
mbed_official | 235:685d5f11838f | 851 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 852 | husart->Instance->CR3 |= USART_CR3_DMAT; |
mbed_official | 235:685d5f11838f | 853 | |
mbed_official | 235:685d5f11838f | 854 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 855 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 856 | |
mbed_official | 235:685d5f11838f | 857 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 858 | } |
mbed_official | 235:685d5f11838f | 859 | else |
mbed_official | 235:685d5f11838f | 860 | { |
mbed_official | 235:685d5f11838f | 861 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 862 | } |
mbed_official | 235:685d5f11838f | 863 | } |
mbed_official | 235:685d5f11838f | 864 | |
mbed_official | 235:685d5f11838f | 865 | /** |
mbed_official | 235:685d5f11838f | 866 | * @brief Full-Duplex Receive an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 867 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 868 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 869 | * @param pRxData: Pointer to data buffer |
mbed_official | 235:685d5f11838f | 870 | * @param Size: Amount of data to be received |
mbed_official | 235:685d5f11838f | 871 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 872 | * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave. |
mbed_official | 235:685d5f11838f | 873 | * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. |
mbed_official | 235:685d5f11838f | 874 | */ |
mbed_official | 235:685d5f11838f | 875 | HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) |
mbed_official | 235:685d5f11838f | 876 | { |
mbed_official | 235:685d5f11838f | 877 | uint32_t *tmp; |
mbed_official | 235:685d5f11838f | 878 | |
mbed_official | 235:685d5f11838f | 879 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 880 | { |
mbed_official | 235:685d5f11838f | 881 | if((pRxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 882 | { |
mbed_official | 235:685d5f11838f | 883 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 884 | } |
mbed_official | 235:685d5f11838f | 885 | |
mbed_official | 235:685d5f11838f | 886 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 887 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 888 | |
mbed_official | 235:685d5f11838f | 889 | husart->pRxBuffPtr = pRxData; |
mbed_official | 235:685d5f11838f | 890 | husart->RxXferSize = Size; |
mbed_official | 235:685d5f11838f | 891 | husart->pTxBuffPtr = pRxData; |
mbed_official | 235:685d5f11838f | 892 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 893 | |
mbed_official | 235:685d5f11838f | 894 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 895 | husart->State = HAL_USART_STATE_BUSY_RX; |
mbed_official | 235:685d5f11838f | 896 | |
mbed_official | 235:685d5f11838f | 897 | /* Set the USART DMA Rx transfer complete callback */ |
mbed_official | 235:685d5f11838f | 898 | husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; |
mbed_official | 235:685d5f11838f | 899 | |
mbed_official | 235:685d5f11838f | 900 | /* Set the USART DMA Half transfer complete callback */ |
mbed_official | 235:685d5f11838f | 901 | husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; |
mbed_official | 235:685d5f11838f | 902 | |
mbed_official | 235:685d5f11838f | 903 | /* Set the USART DMA Rx transfer error callback */ |
mbed_official | 235:685d5f11838f | 904 | husart->hdmarx->XferErrorCallback = USART_DMAError; |
mbed_official | 235:685d5f11838f | 905 | |
mbed_official | 235:685d5f11838f | 906 | /* Enable the USART receive DMA Stream */ |
mbed_official | 235:685d5f11838f | 907 | tmp = (uint32_t*)&pRxData; |
mbed_official | 235:685d5f11838f | 908 | HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size); |
mbed_official | 235:685d5f11838f | 909 | |
mbed_official | 235:685d5f11838f | 910 | /* Enable the USART transmit DMA Stream: the transmit stream is used in order |
mbed_official | 235:685d5f11838f | 911 | to generate in the non-blocking mode the clock to the slave device, |
mbed_official | 235:685d5f11838f | 912 | this mode isn't a simplex receive mode but a full-duplex receive one */ |
mbed_official | 235:685d5f11838f | 913 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size); |
mbed_official | 235:685d5f11838f | 914 | |
mbed_official | 235:685d5f11838f | 915 | /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer |
mbed_official | 235:685d5f11838f | 916 | when using the USART in circular mode */ |
mbed_official | 235:685d5f11838f | 917 | __HAL_USART_CLEAR_OREFLAG(husart); |
mbed_official | 235:685d5f11838f | 918 | |
mbed_official | 235:685d5f11838f | 919 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
mbed_official | 235:685d5f11838f | 920 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 921 | husart->Instance->CR3 |= USART_CR3_DMAR; |
mbed_official | 235:685d5f11838f | 922 | |
mbed_official | 235:685d5f11838f | 923 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
mbed_official | 235:685d5f11838f | 924 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 925 | husart->Instance->CR3 |= USART_CR3_DMAT; |
mbed_official | 235:685d5f11838f | 926 | |
mbed_official | 235:685d5f11838f | 927 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 928 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 929 | |
mbed_official | 235:685d5f11838f | 930 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 931 | } |
mbed_official | 235:685d5f11838f | 932 | else |
mbed_official | 235:685d5f11838f | 933 | { |
mbed_official | 235:685d5f11838f | 934 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 935 | } |
mbed_official | 235:685d5f11838f | 936 | } |
mbed_official | 235:685d5f11838f | 937 | |
mbed_official | 235:685d5f11838f | 938 | /** |
mbed_official | 235:685d5f11838f | 939 | * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 940 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 941 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 942 | * @param pTxData: Pointer to data transmitted buffer |
mbed_official | 235:685d5f11838f | 943 | * @param pRxData: Pointer to data received buffer |
mbed_official | 235:685d5f11838f | 944 | * @param Size: Amount of data to be received |
mbed_official | 235:685d5f11838f | 945 | * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. |
mbed_official | 235:685d5f11838f | 946 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 947 | */ |
mbed_official | 235:685d5f11838f | 948 | HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
mbed_official | 235:685d5f11838f | 949 | { |
mbed_official | 235:685d5f11838f | 950 | uint32_t *tmp; |
mbed_official | 235:685d5f11838f | 951 | |
mbed_official | 235:685d5f11838f | 952 | if(husart->State == HAL_USART_STATE_READY) |
mbed_official | 235:685d5f11838f | 953 | { |
mbed_official | 235:685d5f11838f | 954 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
mbed_official | 235:685d5f11838f | 955 | { |
mbed_official | 235:685d5f11838f | 956 | return HAL_ERROR; |
mbed_official | 235:685d5f11838f | 957 | } |
mbed_official | 235:685d5f11838f | 958 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 959 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 960 | |
mbed_official | 235:685d5f11838f | 961 | husart->pRxBuffPtr = pRxData; |
mbed_official | 235:685d5f11838f | 962 | husart->RxXferSize = Size; |
mbed_official | 235:685d5f11838f | 963 | husart->pTxBuffPtr = pTxData; |
mbed_official | 235:685d5f11838f | 964 | husart->TxXferSize = Size; |
mbed_official | 235:685d5f11838f | 965 | |
mbed_official | 235:685d5f11838f | 966 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
mbed_official | 235:685d5f11838f | 967 | husart->State = HAL_USART_STATE_BUSY_TX_RX; |
mbed_official | 235:685d5f11838f | 968 | |
mbed_official | 235:685d5f11838f | 969 | /* Set the USART DMA Rx transfer complete callback */ |
mbed_official | 235:685d5f11838f | 970 | husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; |
mbed_official | 235:685d5f11838f | 971 | |
mbed_official | 235:685d5f11838f | 972 | /* Set the USART DMA Half transfer complete callback */ |
mbed_official | 235:685d5f11838f | 973 | husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; |
mbed_official | 235:685d5f11838f | 974 | |
mbed_official | 235:685d5f11838f | 975 | /* Set the USART DMA Tx transfer complete callback */ |
mbed_official | 235:685d5f11838f | 976 | husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; |
mbed_official | 235:685d5f11838f | 977 | |
mbed_official | 235:685d5f11838f | 978 | /* Set the USART DMA Half transfer complete callback */ |
mbed_official | 235:685d5f11838f | 979 | husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; |
mbed_official | 235:685d5f11838f | 980 | |
mbed_official | 235:685d5f11838f | 981 | /* Set the USART DMA Tx transfer error callback */ |
mbed_official | 235:685d5f11838f | 982 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
mbed_official | 235:685d5f11838f | 983 | |
mbed_official | 235:685d5f11838f | 984 | /* Set the USART DMA Rx transfer error callback */ |
mbed_official | 235:685d5f11838f | 985 | husart->hdmarx->XferErrorCallback = USART_DMAError; |
mbed_official | 235:685d5f11838f | 986 | |
mbed_official | 235:685d5f11838f | 987 | /* Enable the USART receive DMA Stream */ |
mbed_official | 235:685d5f11838f | 988 | tmp = (uint32_t*)&pRxData; |
mbed_official | 235:685d5f11838f | 989 | HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size); |
mbed_official | 235:685d5f11838f | 990 | |
mbed_official | 235:685d5f11838f | 991 | /* Enable the USART transmit DMA Stream */ |
mbed_official | 235:685d5f11838f | 992 | tmp = (uint32_t*)&pTxData; |
mbed_official | 235:685d5f11838f | 993 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size); |
mbed_official | 235:685d5f11838f | 994 | |
mbed_official | 235:685d5f11838f | 995 | /* Clear the Overrun flag: mandatory for the second transfer in circular mode */ |
mbed_official | 235:685d5f11838f | 996 | __HAL_USART_CLEAR_OREFLAG(husart); |
mbed_official | 235:685d5f11838f | 997 | |
mbed_official | 235:685d5f11838f | 998 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
mbed_official | 235:685d5f11838f | 999 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 1000 | husart->Instance->CR3 |= USART_CR3_DMAR; |
mbed_official | 235:685d5f11838f | 1001 | |
mbed_official | 235:685d5f11838f | 1002 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
mbed_official | 235:685d5f11838f | 1003 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 1004 | husart->Instance->CR3 |= USART_CR3_DMAT; |
mbed_official | 235:685d5f11838f | 1005 | |
mbed_official | 235:685d5f11838f | 1006 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 1007 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 1008 | |
mbed_official | 235:685d5f11838f | 1009 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1010 | } |
mbed_official | 235:685d5f11838f | 1011 | else |
mbed_official | 235:685d5f11838f | 1012 | { |
mbed_official | 235:685d5f11838f | 1013 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 1014 | } |
mbed_official | 235:685d5f11838f | 1015 | } |
mbed_official | 235:685d5f11838f | 1016 | |
mbed_official | 235:685d5f11838f | 1017 | /** |
mbed_official | 235:685d5f11838f | 1018 | * @brief Pauses the DMA Transfer. |
mbed_official | 235:685d5f11838f | 1019 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1020 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1021 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1022 | */ |
mbed_official | 235:685d5f11838f | 1023 | HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1024 | { |
mbed_official | 235:685d5f11838f | 1025 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 1026 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 1027 | |
mbed_official | 235:685d5f11838f | 1028 | /* Disable the USART DMA Tx request */ |
mbed_official | 235:685d5f11838f | 1029 | husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT); |
mbed_official | 235:685d5f11838f | 1030 | |
mbed_official | 235:685d5f11838f | 1031 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 1032 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 1033 | |
mbed_official | 235:685d5f11838f | 1034 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1035 | } |
mbed_official | 235:685d5f11838f | 1036 | |
mbed_official | 235:685d5f11838f | 1037 | /** |
mbed_official | 235:685d5f11838f | 1038 | * @brief Resumes the DMA Transfer. |
mbed_official | 235:685d5f11838f | 1039 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1040 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1041 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1042 | */ |
mbed_official | 235:685d5f11838f | 1043 | HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1044 | { |
mbed_official | 235:685d5f11838f | 1045 | /* Process Locked */ |
mbed_official | 235:685d5f11838f | 1046 | __HAL_LOCK(husart); |
mbed_official | 235:685d5f11838f | 1047 | |
mbed_official | 235:685d5f11838f | 1048 | /* Enable the USART DMA Tx request */ |
mbed_official | 235:685d5f11838f | 1049 | husart->Instance->CR3 |= USART_CR3_DMAT; |
mbed_official | 235:685d5f11838f | 1050 | |
mbed_official | 235:685d5f11838f | 1051 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 1052 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 1053 | |
mbed_official | 235:685d5f11838f | 1054 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1055 | } |
mbed_official | 235:685d5f11838f | 1056 | |
mbed_official | 235:685d5f11838f | 1057 | /** |
mbed_official | 235:685d5f11838f | 1058 | * @brief Stops the DMA Transfer. |
mbed_official | 235:685d5f11838f | 1059 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1060 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1061 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1062 | */ |
mbed_official | 235:685d5f11838f | 1063 | HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1064 | { |
mbed_official | 235:685d5f11838f | 1065 | /* The Lock is not implemented on this API to allow the user application |
mbed_official | 235:685d5f11838f | 1066 | to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback(): |
mbed_official | 235:685d5f11838f | 1067 | when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated |
mbed_official | 235:685d5f11838f | 1068 | and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() |
mbed_official | 235:685d5f11838f | 1069 | */ |
mbed_official | 235:685d5f11838f | 1070 | |
mbed_official | 235:685d5f11838f | 1071 | /* Abort the USART DMA Tx Stream */ |
mbed_official | 235:685d5f11838f | 1072 | if(husart->hdmatx != NULL) |
mbed_official | 235:685d5f11838f | 1073 | { |
mbed_official | 235:685d5f11838f | 1074 | HAL_DMA_Abort(husart->hdmatx); |
mbed_official | 235:685d5f11838f | 1075 | } |
mbed_official | 235:685d5f11838f | 1076 | /* Abort the USART DMA Rx Stream */ |
mbed_official | 235:685d5f11838f | 1077 | if(husart->hdmarx != NULL) |
mbed_official | 235:685d5f11838f | 1078 | { |
mbed_official | 235:685d5f11838f | 1079 | HAL_DMA_Abort(husart->hdmarx); |
mbed_official | 235:685d5f11838f | 1080 | } |
mbed_official | 235:685d5f11838f | 1081 | |
mbed_official | 235:685d5f11838f | 1082 | /* Disable the USART Tx/Rx DMA requests */ |
mbed_official | 235:685d5f11838f | 1083 | husart->Instance->CR3 &= ~USART_CR3_DMAT; |
mbed_official | 235:685d5f11838f | 1084 | husart->Instance->CR3 &= ~USART_CR3_DMAR; |
mbed_official | 235:685d5f11838f | 1085 | |
mbed_official | 235:685d5f11838f | 1086 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1087 | |
mbed_official | 235:685d5f11838f | 1088 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1089 | } |
mbed_official | 235:685d5f11838f | 1090 | |
mbed_official | 235:685d5f11838f | 1091 | /** |
mbed_official | 235:685d5f11838f | 1092 | * @brief This function handles USART interrupt request. |
mbed_official | 235:685d5f11838f | 1093 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1094 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1095 | * @retval None |
mbed_official | 235:685d5f11838f | 1096 | */ |
mbed_official | 235:685d5f11838f | 1097 | void HAL_USART_IRQHandler(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1098 | { |
mbed_official | 235:685d5f11838f | 1099 | uint32_t tmp1 = 0, tmp2 = 0; |
mbed_official | 235:685d5f11838f | 1100 | |
mbed_official | 235:685d5f11838f | 1101 | tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_PE); |
mbed_official | 235:685d5f11838f | 1102 | tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 1103 | /* USART parity error interrupt occurred -----------------------------------*/ |
mbed_official | 235:685d5f11838f | 1104 | if((tmp1 != RESET) && (tmp2 != RESET)) |
mbed_official | 235:685d5f11838f | 1105 | { |
mbed_official | 235:685d5f11838f | 1106 | __HAL_USART_CLEAR_PEFLAG(husart); |
mbed_official | 235:685d5f11838f | 1107 | husart->ErrorCode |= HAL_USART_ERROR_PE; |
mbed_official | 235:685d5f11838f | 1108 | } |
mbed_official | 235:685d5f11838f | 1109 | |
mbed_official | 235:685d5f11838f | 1110 | tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_FE); |
mbed_official | 235:685d5f11838f | 1111 | tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1112 | /* USART frame error interrupt occurred ------------------------------------*/ |
mbed_official | 235:685d5f11838f | 1113 | if((tmp1 != RESET) && (tmp2 != RESET)) |
mbed_official | 235:685d5f11838f | 1114 | { |
mbed_official | 235:685d5f11838f | 1115 | __HAL_USART_CLEAR_FEFLAG(husart); |
mbed_official | 235:685d5f11838f | 1116 | husart->ErrorCode |= HAL_USART_ERROR_FE; |
mbed_official | 235:685d5f11838f | 1117 | } |
mbed_official | 235:685d5f11838f | 1118 | |
mbed_official | 235:685d5f11838f | 1119 | tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_NE); |
mbed_official | 235:685d5f11838f | 1120 | tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1121 | /* USART noise error interrupt occurred ------------------------------------*/ |
mbed_official | 235:685d5f11838f | 1122 | if((tmp1 != RESET) && (tmp2 != RESET)) |
mbed_official | 235:685d5f11838f | 1123 | { |
mbed_official | 235:685d5f11838f | 1124 | __HAL_USART_CLEAR_NEFLAG(husart); |
mbed_official | 235:685d5f11838f | 1125 | husart->ErrorCode |= HAL_USART_ERROR_NE; |
mbed_official | 235:685d5f11838f | 1126 | } |
mbed_official | 235:685d5f11838f | 1127 | |
mbed_official | 235:685d5f11838f | 1128 | tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_ORE); |
mbed_official | 235:685d5f11838f | 1129 | tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1130 | /* USART Over-Run interrupt occurred ---------------------------------------*/ |
mbed_official | 235:685d5f11838f | 1131 | if((tmp1 != RESET) && (tmp2 != RESET)) |
mbed_official | 235:685d5f11838f | 1132 | { |
mbed_official | 235:685d5f11838f | 1133 | __HAL_USART_CLEAR_OREFLAG(husart); |
mbed_official | 235:685d5f11838f | 1134 | husart->ErrorCode |= HAL_USART_ERROR_ORE; |
mbed_official | 235:685d5f11838f | 1135 | } |
mbed_official | 235:685d5f11838f | 1136 | |
mbed_official | 235:685d5f11838f | 1137 | if(husart->ErrorCode != HAL_USART_ERROR_NONE) |
mbed_official | 235:685d5f11838f | 1138 | { |
mbed_official | 235:685d5f11838f | 1139 | /* Set the USART state ready to be able to start again the process */ |
mbed_official | 235:685d5f11838f | 1140 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1141 | |
mbed_official | 235:685d5f11838f | 1142 | HAL_USART_ErrorCallback(husart); |
mbed_official | 235:685d5f11838f | 1143 | } |
mbed_official | 235:685d5f11838f | 1144 | |
mbed_official | 235:685d5f11838f | 1145 | tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE); |
mbed_official | 235:685d5f11838f | 1146 | tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 1147 | /* USART in mode Receiver --------------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 1148 | if((tmp1 != RESET) && (tmp2 != RESET)) |
mbed_official | 235:685d5f11838f | 1149 | { |
mbed_official | 235:685d5f11838f | 1150 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
mbed_official | 235:685d5f11838f | 1151 | { |
mbed_official | 235:685d5f11838f | 1152 | USART_Receive_IT(husart); |
mbed_official | 235:685d5f11838f | 1153 | } |
mbed_official | 235:685d5f11838f | 1154 | else |
mbed_official | 235:685d5f11838f | 1155 | { |
mbed_official | 235:685d5f11838f | 1156 | USART_TransmitReceive_IT(husart); |
mbed_official | 235:685d5f11838f | 1157 | } |
mbed_official | 235:685d5f11838f | 1158 | } |
mbed_official | 235:685d5f11838f | 1159 | |
mbed_official | 235:685d5f11838f | 1160 | tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_TXE); |
mbed_official | 235:685d5f11838f | 1161 | tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 1162 | /* USART in mode Transmitter -----------------------------------------------*/ |
mbed_official | 235:685d5f11838f | 1163 | if((tmp1 != RESET) && (tmp2 != RESET)) |
mbed_official | 235:685d5f11838f | 1164 | { |
mbed_official | 235:685d5f11838f | 1165 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
mbed_official | 235:685d5f11838f | 1166 | { |
mbed_official | 235:685d5f11838f | 1167 | USART_Transmit_IT(husart); |
mbed_official | 235:685d5f11838f | 1168 | } |
mbed_official | 235:685d5f11838f | 1169 | else |
mbed_official | 235:685d5f11838f | 1170 | { |
mbed_official | 235:685d5f11838f | 1171 | USART_TransmitReceive_IT(husart); |
mbed_official | 235:685d5f11838f | 1172 | } |
mbed_official | 235:685d5f11838f | 1173 | } |
mbed_official | 235:685d5f11838f | 1174 | } |
mbed_official | 235:685d5f11838f | 1175 | |
mbed_official | 235:685d5f11838f | 1176 | /** |
mbed_official | 235:685d5f11838f | 1177 | * @brief Tx Transfer completed callbacks. |
mbed_official | 235:685d5f11838f | 1178 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1179 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1180 | * @retval None |
mbed_official | 235:685d5f11838f | 1181 | */ |
mbed_official | 235:685d5f11838f | 1182 | __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1183 | { |
mbed_official | 235:685d5f11838f | 1184 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 1185 | the HAL_USART_TxCpltCallback could be implemented in the user file |
mbed_official | 235:685d5f11838f | 1186 | */ |
mbed_official | 235:685d5f11838f | 1187 | } |
mbed_official | 235:685d5f11838f | 1188 | |
mbed_official | 235:685d5f11838f | 1189 | /** |
mbed_official | 235:685d5f11838f | 1190 | * @brief Tx Half Transfer completed callbacks. |
mbed_official | 235:685d5f11838f | 1191 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1192 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1193 | * @retval None |
mbed_official | 235:685d5f11838f | 1194 | */ |
mbed_official | 235:685d5f11838f | 1195 | __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1196 | { |
mbed_official | 235:685d5f11838f | 1197 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 1198 | the HAL_USART_TxCpltCallback could be implemented in the user file |
mbed_official | 235:685d5f11838f | 1199 | */ |
mbed_official | 235:685d5f11838f | 1200 | } |
mbed_official | 235:685d5f11838f | 1201 | |
mbed_official | 235:685d5f11838f | 1202 | /** |
mbed_official | 235:685d5f11838f | 1203 | * @brief Rx Transfer completed callbacks. |
mbed_official | 235:685d5f11838f | 1204 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1205 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1206 | * @retval None |
mbed_official | 235:685d5f11838f | 1207 | */ |
mbed_official | 235:685d5f11838f | 1208 | __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1209 | { |
mbed_official | 235:685d5f11838f | 1210 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 1211 | the HAL_USART_TxCpltCallback could be implemented in the user file |
mbed_official | 235:685d5f11838f | 1212 | */ |
mbed_official | 235:685d5f11838f | 1213 | } |
mbed_official | 235:685d5f11838f | 1214 | |
mbed_official | 235:685d5f11838f | 1215 | /** |
mbed_official | 235:685d5f11838f | 1216 | * @brief Rx Half Transfer completed callbacks. |
mbed_official | 235:685d5f11838f | 1217 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1218 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1219 | * @retval None |
mbed_official | 235:685d5f11838f | 1220 | */ |
mbed_official | 235:685d5f11838f | 1221 | __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1222 | { |
mbed_official | 235:685d5f11838f | 1223 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 1224 | the HAL_USART_TxCpltCallback could be implemented in the user file |
mbed_official | 235:685d5f11838f | 1225 | */ |
mbed_official | 235:685d5f11838f | 1226 | } |
mbed_official | 235:685d5f11838f | 1227 | |
mbed_official | 235:685d5f11838f | 1228 | /** |
mbed_official | 235:685d5f11838f | 1229 | * @brief Tx/Rx Transfers completed callback for the non-blocking process. |
mbed_official | 235:685d5f11838f | 1230 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1231 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1232 | * @retval None |
mbed_official | 235:685d5f11838f | 1233 | */ |
mbed_official | 235:685d5f11838f | 1234 | __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1235 | { |
mbed_official | 235:685d5f11838f | 1236 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 1237 | the HAL_USART_TxCpltCallback could be implemented in the user file |
mbed_official | 235:685d5f11838f | 1238 | */ |
mbed_official | 235:685d5f11838f | 1239 | } |
mbed_official | 235:685d5f11838f | 1240 | |
mbed_official | 235:685d5f11838f | 1241 | /** |
mbed_official | 235:685d5f11838f | 1242 | * @brief USART error callbacks. |
mbed_official | 235:685d5f11838f | 1243 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1244 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1245 | * @retval None |
mbed_official | 235:685d5f11838f | 1246 | */ |
mbed_official | 235:685d5f11838f | 1247 | __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1248 | { |
mbed_official | 235:685d5f11838f | 1249 | /* NOTE: This function Should not be modified, when the callback is needed, |
mbed_official | 235:685d5f11838f | 1250 | the HAL_USART_ErrorCallback could be implemented in the user file |
mbed_official | 235:685d5f11838f | 1251 | */ |
mbed_official | 235:685d5f11838f | 1252 | } |
mbed_official | 235:685d5f11838f | 1253 | |
mbed_official | 235:685d5f11838f | 1254 | /** |
mbed_official | 235:685d5f11838f | 1255 | * @} |
mbed_official | 235:685d5f11838f | 1256 | */ |
mbed_official | 235:685d5f11838f | 1257 | |
mbed_official | 235:685d5f11838f | 1258 | /** @defgroup USART_Group3 Peripheral State and Errors functions |
mbed_official | 235:685d5f11838f | 1259 | * @brief USART State and Errors functions |
mbed_official | 235:685d5f11838f | 1260 | * |
mbed_official | 235:685d5f11838f | 1261 | @verbatim |
mbed_official | 235:685d5f11838f | 1262 | ============================================================================== |
mbed_official | 235:685d5f11838f | 1263 | ##### Peripheral State and Errors functions ##### |
mbed_official | 235:685d5f11838f | 1264 | ============================================================================== |
mbed_official | 235:685d5f11838f | 1265 | [..] |
mbed_official | 235:685d5f11838f | 1266 | This subsection provides a set of functions allowing to return the State of |
mbed_official | 235:685d5f11838f | 1267 | USART communication |
mbed_official | 235:685d5f11838f | 1268 | process, return Peripheral Errors occurred during communication process |
mbed_official | 235:685d5f11838f | 1269 | (+) HAL_USART_GetState() API can be helpful to check in run-time the state |
mbed_official | 235:685d5f11838f | 1270 | of the USART peripheral. |
mbed_official | 235:685d5f11838f | 1271 | (+) HAL_USART_GetError() check in run-time errors that could be occurred during |
mbed_official | 235:685d5f11838f | 1272 | communication. |
mbed_official | 235:685d5f11838f | 1273 | @endverbatim |
mbed_official | 235:685d5f11838f | 1274 | * @{ |
mbed_official | 235:685d5f11838f | 1275 | */ |
mbed_official | 235:685d5f11838f | 1276 | |
mbed_official | 235:685d5f11838f | 1277 | /** |
mbed_official | 235:685d5f11838f | 1278 | * @brief Returns the USART state. |
mbed_official | 235:685d5f11838f | 1279 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1280 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1281 | * @retval HAL state |
mbed_official | 235:685d5f11838f | 1282 | */ |
mbed_official | 235:685d5f11838f | 1283 | HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1284 | { |
mbed_official | 235:685d5f11838f | 1285 | return husart->State; |
mbed_official | 235:685d5f11838f | 1286 | } |
mbed_official | 235:685d5f11838f | 1287 | |
mbed_official | 235:685d5f11838f | 1288 | /** |
mbed_official | 235:685d5f11838f | 1289 | * @brief Return the USART error code |
mbed_official | 235:685d5f11838f | 1290 | * @param husart : pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1291 | * the configuration information for the specified USART. |
mbed_official | 235:685d5f11838f | 1292 | * @retval USART Error Code |
mbed_official | 235:685d5f11838f | 1293 | */ |
mbed_official | 235:685d5f11838f | 1294 | uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1295 | { |
mbed_official | 235:685d5f11838f | 1296 | return husart->ErrorCode; |
mbed_official | 235:685d5f11838f | 1297 | } |
mbed_official | 235:685d5f11838f | 1298 | |
mbed_official | 235:685d5f11838f | 1299 | /** |
mbed_official | 235:685d5f11838f | 1300 | * @} |
mbed_official | 235:685d5f11838f | 1301 | */ |
mbed_official | 235:685d5f11838f | 1302 | |
mbed_official | 235:685d5f11838f | 1303 | |
mbed_official | 235:685d5f11838f | 1304 | /** |
mbed_official | 235:685d5f11838f | 1305 | * @brief DMA USART transmit process complete callback. |
mbed_official | 235:685d5f11838f | 1306 | * @param hdma: DMA handle |
mbed_official | 235:685d5f11838f | 1307 | * @retval None |
mbed_official | 235:685d5f11838f | 1308 | */ |
mbed_official | 235:685d5f11838f | 1309 | static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 235:685d5f11838f | 1310 | { |
mbed_official | 235:685d5f11838f | 1311 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
mbed_official | 235:685d5f11838f | 1312 | /* DMA Normal mode */ |
mbed_official | 235:685d5f11838f | 1313 | if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0) |
mbed_official | 235:685d5f11838f | 1314 | { |
mbed_official | 235:685d5f11838f | 1315 | husart->TxXferCount = 0; |
mbed_official | 235:685d5f11838f | 1316 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
mbed_official | 235:685d5f11838f | 1317 | { |
mbed_official | 235:685d5f11838f | 1318 | /* Wait for USART TC Flag */ |
mbed_official | 235:685d5f11838f | 1319 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, USART_TIMEOUT_VALUE) != HAL_OK) |
mbed_official | 235:685d5f11838f | 1320 | { |
mbed_official | 235:685d5f11838f | 1321 | /* Timeout occurred */ |
mbed_official | 235:685d5f11838f | 1322 | husart->State = HAL_USART_STATE_TIMEOUT; |
mbed_official | 235:685d5f11838f | 1323 | HAL_USART_ErrorCallback(husart); |
mbed_official | 235:685d5f11838f | 1324 | } |
mbed_official | 235:685d5f11838f | 1325 | else |
mbed_official | 235:685d5f11838f | 1326 | { |
mbed_official | 235:685d5f11838f | 1327 | /* No Timeout */ |
mbed_official | 235:685d5f11838f | 1328 | /* Disable the DMA transfer for transmit request by setting the DMAT bit |
mbed_official | 235:685d5f11838f | 1329 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 1330 | husart->Instance->CR3 &= ~(USART_CR3_DMAT); |
mbed_official | 235:685d5f11838f | 1331 | husart->State= HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1332 | HAL_USART_TxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1333 | } |
mbed_official | 235:685d5f11838f | 1334 | } |
mbed_official | 235:685d5f11838f | 1335 | } |
mbed_official | 235:685d5f11838f | 1336 | /* DMA Circular mode */ |
mbed_official | 235:685d5f11838f | 1337 | else |
mbed_official | 235:685d5f11838f | 1338 | { |
mbed_official | 235:685d5f11838f | 1339 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
mbed_official | 235:685d5f11838f | 1340 | { |
mbed_official | 235:685d5f11838f | 1341 | HAL_USART_TxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1342 | } |
mbed_official | 235:685d5f11838f | 1343 | } |
mbed_official | 235:685d5f11838f | 1344 | } |
mbed_official | 235:685d5f11838f | 1345 | |
mbed_official | 235:685d5f11838f | 1346 | /** |
mbed_official | 235:685d5f11838f | 1347 | * @brief DMA USART transmit process half complete callback |
mbed_official | 235:685d5f11838f | 1348 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1349 | * the configuration information for the specified DMA module. |
mbed_official | 235:685d5f11838f | 1350 | * @retval None |
mbed_official | 235:685d5f11838f | 1351 | */ |
mbed_official | 235:685d5f11838f | 1352 | static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 235:685d5f11838f | 1353 | { |
mbed_official | 235:685d5f11838f | 1354 | USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 235:685d5f11838f | 1355 | |
mbed_official | 235:685d5f11838f | 1356 | HAL_USART_TxHalfCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1357 | } |
mbed_official | 235:685d5f11838f | 1358 | |
mbed_official | 235:685d5f11838f | 1359 | /** |
mbed_official | 235:685d5f11838f | 1360 | * @brief DMA USART receive process complete callback. |
mbed_official | 235:685d5f11838f | 1361 | * @param hdma: DMA handle |
mbed_official | 235:685d5f11838f | 1362 | * @retval None |
mbed_official | 235:685d5f11838f | 1363 | */ |
mbed_official | 235:685d5f11838f | 1364 | static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 235:685d5f11838f | 1365 | { |
mbed_official | 235:685d5f11838f | 1366 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
mbed_official | 235:685d5f11838f | 1367 | /* DMA Normal mode */ |
mbed_official | 235:685d5f11838f | 1368 | if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0) |
mbed_official | 235:685d5f11838f | 1369 | { |
mbed_official | 235:685d5f11838f | 1370 | husart->RxXferCount = 0; |
mbed_official | 235:685d5f11838f | 1371 | husart->State= HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1372 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
mbed_official | 235:685d5f11838f | 1373 | { |
mbed_official | 235:685d5f11838f | 1374 | /* Disable the DMA transfer for the Transmit/receiver requests by setting the DMAT/DMAR bit |
mbed_official | 235:685d5f11838f | 1375 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 1376 | husart->Instance->CR3 &= ~(USART_CR3_DMAR); |
mbed_official | 235:685d5f11838f | 1377 | |
mbed_official | 235:685d5f11838f | 1378 | HAL_USART_RxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1379 | } |
mbed_official | 235:685d5f11838f | 1380 | /* the usart state is HAL_USART_STATE_BUSY_TX_RX*/ |
mbed_official | 235:685d5f11838f | 1381 | else |
mbed_official | 235:685d5f11838f | 1382 | { |
mbed_official | 235:685d5f11838f | 1383 | /* Disable the DMA transfer for the Transmit/receiver requests by setting the DMAT/DMAR bit |
mbed_official | 235:685d5f11838f | 1384 | in the USART CR3 register */ |
mbed_official | 235:685d5f11838f | 1385 | husart->Instance->CR3 &= ~(USART_CR3_DMAR); |
mbed_official | 235:685d5f11838f | 1386 | husart->Instance->CR3 &= ~(USART_CR3_DMAT); |
mbed_official | 235:685d5f11838f | 1387 | |
mbed_official | 235:685d5f11838f | 1388 | HAL_USART_TxRxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1389 | } |
mbed_official | 235:685d5f11838f | 1390 | } |
mbed_official | 235:685d5f11838f | 1391 | /* DMA circular mode */ |
mbed_official | 235:685d5f11838f | 1392 | else |
mbed_official | 235:685d5f11838f | 1393 | { |
mbed_official | 235:685d5f11838f | 1394 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
mbed_official | 235:685d5f11838f | 1395 | { |
mbed_official | 235:685d5f11838f | 1396 | HAL_USART_RxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1397 | } |
mbed_official | 235:685d5f11838f | 1398 | /* the usart state is HAL_USART_STATE_BUSY_TX_RX*/ |
mbed_official | 235:685d5f11838f | 1399 | else |
mbed_official | 235:685d5f11838f | 1400 | { |
mbed_official | 235:685d5f11838f | 1401 | HAL_USART_TxRxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1402 | } |
mbed_official | 235:685d5f11838f | 1403 | } |
mbed_official | 235:685d5f11838f | 1404 | } |
mbed_official | 235:685d5f11838f | 1405 | |
mbed_official | 235:685d5f11838f | 1406 | /** |
mbed_official | 235:685d5f11838f | 1407 | * @brief DMA USART receive process half complete callback |
mbed_official | 235:685d5f11838f | 1408 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1409 | * the configuration information for the specified DMA module. |
mbed_official | 235:685d5f11838f | 1410 | * @retval None |
mbed_official | 235:685d5f11838f | 1411 | */ |
mbed_official | 235:685d5f11838f | 1412 | static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 235:685d5f11838f | 1413 | { |
mbed_official | 235:685d5f11838f | 1414 | USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 235:685d5f11838f | 1415 | |
mbed_official | 235:685d5f11838f | 1416 | HAL_USART_RxHalfCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1417 | } |
mbed_official | 235:685d5f11838f | 1418 | |
mbed_official | 235:685d5f11838f | 1419 | /** |
mbed_official | 235:685d5f11838f | 1420 | * @brief DMA USART communication error callback. |
mbed_official | 235:685d5f11838f | 1421 | * @param hdma: DMA handle |
mbed_official | 235:685d5f11838f | 1422 | * @retval None |
mbed_official | 235:685d5f11838f | 1423 | */ |
mbed_official | 235:685d5f11838f | 1424 | static void USART_DMAError(DMA_HandleTypeDef *hdma) |
mbed_official | 235:685d5f11838f | 1425 | { |
mbed_official | 235:685d5f11838f | 1426 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
mbed_official | 235:685d5f11838f | 1427 | |
mbed_official | 235:685d5f11838f | 1428 | husart->RxXferCount = 0; |
mbed_official | 235:685d5f11838f | 1429 | husart->TxXferCount = 0; |
mbed_official | 235:685d5f11838f | 1430 | husart->ErrorCode |= HAL_USART_ERROR_DMA; |
mbed_official | 235:685d5f11838f | 1431 | husart->State= HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1432 | |
mbed_official | 235:685d5f11838f | 1433 | HAL_USART_ErrorCallback(husart); |
mbed_official | 235:685d5f11838f | 1434 | } |
mbed_official | 235:685d5f11838f | 1435 | |
mbed_official | 235:685d5f11838f | 1436 | /** |
mbed_official | 235:685d5f11838f | 1437 | * @brief This function handles USART Communication Timeout. |
mbed_official | 235:685d5f11838f | 1438 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1439 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1440 | * @param Flag: specifies the USART flag to check. |
mbed_official | 235:685d5f11838f | 1441 | * @param Status: The new Flag status (SET or RESET). |
mbed_official | 235:685d5f11838f | 1442 | * @param Timeout: Timeout duration |
mbed_official | 235:685d5f11838f | 1443 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1444 | */ |
mbed_official | 235:685d5f11838f | 1445 | static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout) |
mbed_official | 235:685d5f11838f | 1446 | { |
mbed_official | 235:685d5f11838f | 1447 | uint32_t tickstart = 0; |
mbed_official | 235:685d5f11838f | 1448 | |
mbed_official | 235:685d5f11838f | 1449 | /* Get tick */ |
mbed_official | 235:685d5f11838f | 1450 | tickstart = HAL_GetTick(); |
mbed_official | 235:685d5f11838f | 1451 | |
mbed_official | 235:685d5f11838f | 1452 | /* Wait until flag is set */ |
mbed_official | 235:685d5f11838f | 1453 | if(Status == RESET) |
mbed_official | 235:685d5f11838f | 1454 | { |
mbed_official | 235:685d5f11838f | 1455 | while(__HAL_USART_GET_FLAG(husart, Flag) == RESET) |
mbed_official | 235:685d5f11838f | 1456 | { |
mbed_official | 235:685d5f11838f | 1457 | /* Check for the Timeout */ |
mbed_official | 235:685d5f11838f | 1458 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 235:685d5f11838f | 1459 | { |
mbed_official | 235:685d5f11838f | 1460 | if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout)) |
mbed_official | 235:685d5f11838f | 1461 | { |
mbed_official | 235:685d5f11838f | 1462 | /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ |
mbed_official | 235:685d5f11838f | 1463 | __USART_DISABLE_IT(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 1464 | __USART_DISABLE_IT(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 1465 | __USART_DISABLE_IT(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 1466 | __USART_DISABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1467 | |
mbed_official | 235:685d5f11838f | 1468 | husart->State= HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1469 | |
mbed_official | 235:685d5f11838f | 1470 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 1471 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 1472 | |
mbed_official | 235:685d5f11838f | 1473 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 1474 | } |
mbed_official | 235:685d5f11838f | 1475 | } |
mbed_official | 235:685d5f11838f | 1476 | } |
mbed_official | 235:685d5f11838f | 1477 | } |
mbed_official | 235:685d5f11838f | 1478 | else |
mbed_official | 235:685d5f11838f | 1479 | { |
mbed_official | 235:685d5f11838f | 1480 | while(__HAL_USART_GET_FLAG(husart, Flag) != RESET) |
mbed_official | 235:685d5f11838f | 1481 | { |
mbed_official | 235:685d5f11838f | 1482 | /* Check for the Timeout */ |
mbed_official | 235:685d5f11838f | 1483 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 235:685d5f11838f | 1484 | { |
mbed_official | 235:685d5f11838f | 1485 | if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout)) |
mbed_official | 235:685d5f11838f | 1486 | { |
mbed_official | 235:685d5f11838f | 1487 | /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ |
mbed_official | 235:685d5f11838f | 1488 | __USART_DISABLE_IT(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 1489 | __USART_DISABLE_IT(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 1490 | __USART_DISABLE_IT(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 1491 | __USART_DISABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1492 | |
mbed_official | 235:685d5f11838f | 1493 | husart->State= HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1494 | |
mbed_official | 235:685d5f11838f | 1495 | /* Process Unlocked */ |
mbed_official | 235:685d5f11838f | 1496 | __HAL_UNLOCK(husart); |
mbed_official | 235:685d5f11838f | 1497 | |
mbed_official | 235:685d5f11838f | 1498 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 1499 | } |
mbed_official | 235:685d5f11838f | 1500 | } |
mbed_official | 235:685d5f11838f | 1501 | } |
mbed_official | 235:685d5f11838f | 1502 | } |
mbed_official | 235:685d5f11838f | 1503 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1504 | } |
mbed_official | 235:685d5f11838f | 1505 | |
mbed_official | 235:685d5f11838f | 1506 | |
mbed_official | 235:685d5f11838f | 1507 | /** |
mbed_official | 235:685d5f11838f | 1508 | * @brief Simplex Send an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 1509 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1510 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1511 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1512 | * @note The USART errors are not managed to avoid the overrun error. |
mbed_official | 235:685d5f11838f | 1513 | */ |
mbed_official | 235:685d5f11838f | 1514 | static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1515 | { |
mbed_official | 235:685d5f11838f | 1516 | uint16_t* tmp; |
mbed_official | 235:685d5f11838f | 1517 | |
mbed_official | 235:685d5f11838f | 1518 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
mbed_official | 235:685d5f11838f | 1519 | { |
mbed_official | 235:685d5f11838f | 1520 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 1521 | { |
mbed_official | 235:685d5f11838f | 1522 | tmp = (uint16_t*) husart->pTxBuffPtr; |
mbed_official | 235:685d5f11838f | 1523 | husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 1524 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 1525 | { |
mbed_official | 235:685d5f11838f | 1526 | husart->pTxBuffPtr += 2; |
mbed_official | 235:685d5f11838f | 1527 | } |
mbed_official | 235:685d5f11838f | 1528 | else |
mbed_official | 235:685d5f11838f | 1529 | { |
mbed_official | 235:685d5f11838f | 1530 | husart->pTxBuffPtr += 1; |
mbed_official | 235:685d5f11838f | 1531 | } |
mbed_official | 235:685d5f11838f | 1532 | } |
mbed_official | 235:685d5f11838f | 1533 | else |
mbed_official | 235:685d5f11838f | 1534 | { |
mbed_official | 235:685d5f11838f | 1535 | husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1536 | } |
mbed_official | 235:685d5f11838f | 1537 | |
mbed_official | 235:685d5f11838f | 1538 | if(--husart->TxXferCount == 0) |
mbed_official | 235:685d5f11838f | 1539 | { |
mbed_official | 235:685d5f11838f | 1540 | /* Disable the USART Transmit data register empty Interrupt */ |
mbed_official | 235:685d5f11838f | 1541 | __USART_DISABLE_IT(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 1542 | |
mbed_official | 235:685d5f11838f | 1543 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
mbed_official | 235:685d5f11838f | 1544 | __USART_DISABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1545 | |
mbed_official | 235:685d5f11838f | 1546 | |
mbed_official | 235:685d5f11838f | 1547 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, USART_TIMEOUT_VALUE) != HAL_OK) |
mbed_official | 235:685d5f11838f | 1548 | { |
mbed_official | 235:685d5f11838f | 1549 | return HAL_TIMEOUT; |
mbed_official | 235:685d5f11838f | 1550 | } |
mbed_official | 235:685d5f11838f | 1551 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1552 | |
mbed_official | 235:685d5f11838f | 1553 | HAL_USART_TxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1554 | |
mbed_official | 235:685d5f11838f | 1555 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1556 | } |
mbed_official | 235:685d5f11838f | 1557 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1558 | } |
mbed_official | 235:685d5f11838f | 1559 | else |
mbed_official | 235:685d5f11838f | 1560 | { |
mbed_official | 235:685d5f11838f | 1561 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 1562 | } |
mbed_official | 235:685d5f11838f | 1563 | } |
mbed_official | 235:685d5f11838f | 1564 | |
mbed_official | 235:685d5f11838f | 1565 | /** |
mbed_official | 235:685d5f11838f | 1566 | * @brief Simplex Receive an amount of data in non-blocking mode. |
mbed_official | 235:685d5f11838f | 1567 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1568 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1569 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1570 | */ |
mbed_official | 235:685d5f11838f | 1571 | static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1572 | { |
mbed_official | 235:685d5f11838f | 1573 | uint16_t* tmp; |
mbed_official | 235:685d5f11838f | 1574 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
mbed_official | 235:685d5f11838f | 1575 | { |
mbed_official | 235:685d5f11838f | 1576 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 1577 | { |
mbed_official | 235:685d5f11838f | 1578 | tmp = (uint16_t*) husart->pRxBuffPtr; |
mbed_official | 235:685d5f11838f | 1579 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 1580 | { |
mbed_official | 235:685d5f11838f | 1581 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 1582 | husart->pRxBuffPtr += 2; |
mbed_official | 235:685d5f11838f | 1583 | } |
mbed_official | 235:685d5f11838f | 1584 | else |
mbed_official | 235:685d5f11838f | 1585 | { |
mbed_official | 235:685d5f11838f | 1586 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1587 | husart->pRxBuffPtr += 1; |
mbed_official | 235:685d5f11838f | 1588 | } |
mbed_official | 235:685d5f11838f | 1589 | if(--husart->RxXferCount != 0x00) |
mbed_official | 235:685d5f11838f | 1590 | { |
mbed_official | 235:685d5f11838f | 1591 | /* Send dummy byte in order to generate the clock for the slave to send the next data */ |
mbed_official | 235:685d5f11838f | 1592 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 1593 | } |
mbed_official | 235:685d5f11838f | 1594 | } |
mbed_official | 235:685d5f11838f | 1595 | else |
mbed_official | 235:685d5f11838f | 1596 | { |
mbed_official | 235:685d5f11838f | 1597 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 1598 | { |
mbed_official | 235:685d5f11838f | 1599 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1600 | } |
mbed_official | 235:685d5f11838f | 1601 | else |
mbed_official | 235:685d5f11838f | 1602 | { |
mbed_official | 235:685d5f11838f | 1603 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
mbed_official | 235:685d5f11838f | 1604 | } |
mbed_official | 235:685d5f11838f | 1605 | |
mbed_official | 235:685d5f11838f | 1606 | if(--husart->RxXferCount != 0x00) |
mbed_official | 235:685d5f11838f | 1607 | { |
mbed_official | 235:685d5f11838f | 1608 | /* Send dummy byte in order to generate the clock for the slave to send the next data */ |
mbed_official | 235:685d5f11838f | 1609 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1610 | } |
mbed_official | 235:685d5f11838f | 1611 | } |
mbed_official | 235:685d5f11838f | 1612 | |
mbed_official | 235:685d5f11838f | 1613 | if(husart->RxXferCount == 0) |
mbed_official | 235:685d5f11838f | 1614 | { |
mbed_official | 235:685d5f11838f | 1615 | /* Disable the USART RXNE Interrupt */ |
mbed_official | 235:685d5f11838f | 1616 | __USART_DISABLE_IT(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 1617 | |
mbed_official | 235:685d5f11838f | 1618 | /* Disable the USART Parity Error Interrupt */ |
mbed_official | 235:685d5f11838f | 1619 | __USART_DISABLE_IT(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 1620 | |
mbed_official | 235:685d5f11838f | 1621 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
mbed_official | 235:685d5f11838f | 1622 | __USART_DISABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1623 | |
mbed_official | 235:685d5f11838f | 1624 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1625 | HAL_USART_RxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1626 | |
mbed_official | 235:685d5f11838f | 1627 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1628 | } |
mbed_official | 235:685d5f11838f | 1629 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1630 | } |
mbed_official | 235:685d5f11838f | 1631 | else |
mbed_official | 235:685d5f11838f | 1632 | { |
mbed_official | 235:685d5f11838f | 1633 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 1634 | } |
mbed_official | 235:685d5f11838f | 1635 | } |
mbed_official | 235:685d5f11838f | 1636 | |
mbed_official | 235:685d5f11838f | 1637 | /** |
mbed_official | 235:685d5f11838f | 1638 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking). |
mbed_official | 235:685d5f11838f | 1639 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1640 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1641 | * @retval HAL status |
mbed_official | 235:685d5f11838f | 1642 | */ |
mbed_official | 235:685d5f11838f | 1643 | static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1644 | { |
mbed_official | 235:685d5f11838f | 1645 | uint16_t* tmp; |
mbed_official | 235:685d5f11838f | 1646 | |
mbed_official | 235:685d5f11838f | 1647 | if(husart->State == HAL_USART_STATE_BUSY_TX_RX) |
mbed_official | 235:685d5f11838f | 1648 | { |
mbed_official | 235:685d5f11838f | 1649 | if(husart->TxXferCount != 0x00) |
mbed_official | 235:685d5f11838f | 1650 | { |
mbed_official | 235:685d5f11838f | 1651 | if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET) |
mbed_official | 235:685d5f11838f | 1652 | { |
mbed_official | 235:685d5f11838f | 1653 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 1654 | { |
mbed_official | 235:685d5f11838f | 1655 | tmp = (uint16_t*) husart->pTxBuffPtr; |
mbed_official | 235:685d5f11838f | 1656 | husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 1657 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 1658 | { |
mbed_official | 235:685d5f11838f | 1659 | husart->pTxBuffPtr += 2; |
mbed_official | 235:685d5f11838f | 1660 | } |
mbed_official | 235:685d5f11838f | 1661 | else |
mbed_official | 235:685d5f11838f | 1662 | { |
mbed_official | 235:685d5f11838f | 1663 | husart->pTxBuffPtr += 1; |
mbed_official | 235:685d5f11838f | 1664 | } |
mbed_official | 235:685d5f11838f | 1665 | } |
mbed_official | 235:685d5f11838f | 1666 | else |
mbed_official | 235:685d5f11838f | 1667 | { |
mbed_official | 235:685d5f11838f | 1668 | husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1669 | } |
mbed_official | 235:685d5f11838f | 1670 | husart->TxXferCount--; |
mbed_official | 235:685d5f11838f | 1671 | |
mbed_official | 235:685d5f11838f | 1672 | /* Check the latest data transmitted */ |
mbed_official | 235:685d5f11838f | 1673 | if(husart->TxXferCount == 0) |
mbed_official | 235:685d5f11838f | 1674 | { |
mbed_official | 235:685d5f11838f | 1675 | __USART_DISABLE_IT(husart, USART_IT_TXE); |
mbed_official | 235:685d5f11838f | 1676 | } |
mbed_official | 235:685d5f11838f | 1677 | } |
mbed_official | 235:685d5f11838f | 1678 | } |
mbed_official | 235:685d5f11838f | 1679 | |
mbed_official | 235:685d5f11838f | 1680 | if(husart->RxXferCount != 0x00) |
mbed_official | 235:685d5f11838f | 1681 | { |
mbed_official | 235:685d5f11838f | 1682 | if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET) |
mbed_official | 235:685d5f11838f | 1683 | { |
mbed_official | 235:685d5f11838f | 1684 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
mbed_official | 235:685d5f11838f | 1685 | { |
mbed_official | 235:685d5f11838f | 1686 | tmp = (uint16_t*) husart->pRxBuffPtr; |
mbed_official | 235:685d5f11838f | 1687 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 1688 | { |
mbed_official | 235:685d5f11838f | 1689 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
mbed_official | 235:685d5f11838f | 1690 | husart->pRxBuffPtr += 2; |
mbed_official | 235:685d5f11838f | 1691 | } |
mbed_official | 235:685d5f11838f | 1692 | else |
mbed_official | 235:685d5f11838f | 1693 | { |
mbed_official | 235:685d5f11838f | 1694 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1695 | husart->pRxBuffPtr += 1; |
mbed_official | 235:685d5f11838f | 1696 | } |
mbed_official | 235:685d5f11838f | 1697 | } |
mbed_official | 235:685d5f11838f | 1698 | else |
mbed_official | 235:685d5f11838f | 1699 | { |
mbed_official | 235:685d5f11838f | 1700 | if(husart->Init.Parity == USART_PARITY_NONE) |
mbed_official | 235:685d5f11838f | 1701 | { |
mbed_official | 235:685d5f11838f | 1702 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
mbed_official | 235:685d5f11838f | 1703 | } |
mbed_official | 235:685d5f11838f | 1704 | else |
mbed_official | 235:685d5f11838f | 1705 | { |
mbed_official | 235:685d5f11838f | 1706 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
mbed_official | 235:685d5f11838f | 1707 | } |
mbed_official | 235:685d5f11838f | 1708 | } |
mbed_official | 235:685d5f11838f | 1709 | husart->RxXferCount--; |
mbed_official | 235:685d5f11838f | 1710 | } |
mbed_official | 235:685d5f11838f | 1711 | } |
mbed_official | 235:685d5f11838f | 1712 | |
mbed_official | 235:685d5f11838f | 1713 | /* Check the latest data received */ |
mbed_official | 235:685d5f11838f | 1714 | if(husart->RxXferCount == 0) |
mbed_official | 235:685d5f11838f | 1715 | { |
mbed_official | 235:685d5f11838f | 1716 | __USART_DISABLE_IT(husart, USART_IT_RXNE); |
mbed_official | 235:685d5f11838f | 1717 | |
mbed_official | 235:685d5f11838f | 1718 | /* Disable the USART Parity Error Interrupt */ |
mbed_official | 235:685d5f11838f | 1719 | __USART_DISABLE_IT(husart, USART_IT_PE); |
mbed_official | 235:685d5f11838f | 1720 | |
mbed_official | 235:685d5f11838f | 1721 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
mbed_official | 235:685d5f11838f | 1722 | __USART_DISABLE_IT(husart, USART_IT_ERR); |
mbed_official | 235:685d5f11838f | 1723 | |
mbed_official | 235:685d5f11838f | 1724 | husart->State = HAL_USART_STATE_READY; |
mbed_official | 235:685d5f11838f | 1725 | |
mbed_official | 235:685d5f11838f | 1726 | HAL_USART_TxRxCpltCallback(husart); |
mbed_official | 235:685d5f11838f | 1727 | |
mbed_official | 235:685d5f11838f | 1728 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1729 | } |
mbed_official | 235:685d5f11838f | 1730 | |
mbed_official | 235:685d5f11838f | 1731 | return HAL_OK; |
mbed_official | 235:685d5f11838f | 1732 | } |
mbed_official | 235:685d5f11838f | 1733 | else |
mbed_official | 235:685d5f11838f | 1734 | { |
mbed_official | 235:685d5f11838f | 1735 | return HAL_BUSY; |
mbed_official | 235:685d5f11838f | 1736 | } |
mbed_official | 235:685d5f11838f | 1737 | } |
mbed_official | 235:685d5f11838f | 1738 | |
mbed_official | 235:685d5f11838f | 1739 | /** |
mbed_official | 235:685d5f11838f | 1740 | * @brief Configures the USART peripheral. |
mbed_official | 235:685d5f11838f | 1741 | * @param husart: pointer to a USART_HandleTypeDef structure that contains |
mbed_official | 235:685d5f11838f | 1742 | * the configuration information for the specified USART module. |
mbed_official | 235:685d5f11838f | 1743 | * @retval None |
mbed_official | 235:685d5f11838f | 1744 | */ |
mbed_official | 235:685d5f11838f | 1745 | static void USART_SetConfig(USART_HandleTypeDef *husart) |
mbed_official | 235:685d5f11838f | 1746 | { |
mbed_official | 235:685d5f11838f | 1747 | uint32_t tmpreg = 0x00; |
mbed_official | 235:685d5f11838f | 1748 | |
mbed_official | 235:685d5f11838f | 1749 | /* Check the parameters */ |
mbed_official | 235:685d5f11838f | 1750 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
mbed_official | 235:685d5f11838f | 1751 | assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity)); |
mbed_official | 235:685d5f11838f | 1752 | assert_param(IS_USART_PHASE(husart->Init.CLKPhase)); |
mbed_official | 235:685d5f11838f | 1753 | assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit)); |
mbed_official | 235:685d5f11838f | 1754 | assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate)); |
mbed_official | 235:685d5f11838f | 1755 | assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength)); |
mbed_official | 235:685d5f11838f | 1756 | assert_param(IS_USART_STOPBITS(husart->Init.StopBits)); |
mbed_official | 235:685d5f11838f | 1757 | assert_param(IS_USART_PARITY(husart->Init.Parity)); |
mbed_official | 235:685d5f11838f | 1758 | assert_param(IS_USART_MODE(husart->Init.Mode)); |
mbed_official | 235:685d5f11838f | 1759 | |
mbed_official | 235:685d5f11838f | 1760 | /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the |
mbed_official | 235:685d5f11838f | 1761 | receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */ |
mbed_official | 235:685d5f11838f | 1762 | husart->Instance->CR1 &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE)); |
mbed_official | 235:685d5f11838f | 1763 | |
mbed_official | 235:685d5f11838f | 1764 | /*---------------------------- USART CR2 Configuration ---------------------*/ |
mbed_official | 235:685d5f11838f | 1765 | tmpreg = husart->Instance->CR2; |
mbed_official | 235:685d5f11838f | 1766 | /* Clear CLKEN, CPOL, CPHA and LBCL bits */ |
mbed_official | 235:685d5f11838f | 1767 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)); |
mbed_official | 235:685d5f11838f | 1768 | /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/ |
mbed_official | 235:685d5f11838f | 1769 | /* Set CPOL bit according to husart->Init.CLKPolarity value */ |
mbed_official | 235:685d5f11838f | 1770 | /* Set CPHA bit according to husart->Init.CLKPhase value */ |
mbed_official | 235:685d5f11838f | 1771 | /* Set LBCL bit according to husart->Init.CLKLastBit value */ |
mbed_official | 235:685d5f11838f | 1772 | /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */ |
mbed_official | 235:685d5f11838f | 1773 | tmpreg |= (uint32_t)(USART_CLOCK_ENABLED| husart->Init.CLKPolarity | |
mbed_official | 235:685d5f11838f | 1774 | husart->Init.CLKPhase| husart->Init.CLKLastBit | husart->Init.StopBits); |
mbed_official | 235:685d5f11838f | 1775 | /* Write to USART CR2 */ |
mbed_official | 235:685d5f11838f | 1776 | husart->Instance->CR2 = (uint32_t)tmpreg; |
mbed_official | 235:685d5f11838f | 1777 | |
mbed_official | 235:685d5f11838f | 1778 | /*-------------------------- USART CR1 Configuration -----------------------*/ |
mbed_official | 235:685d5f11838f | 1779 | tmpreg = husart->Instance->CR1; |
mbed_official | 235:685d5f11838f | 1780 | |
mbed_official | 235:685d5f11838f | 1781 | /* Clear M, PCE, PS, TE and RE bits */ |
mbed_official | 235:685d5f11838f | 1782 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ |
mbed_official | 235:685d5f11838f | 1783 | USART_CR1_RE)); |
mbed_official | 235:685d5f11838f | 1784 | |
mbed_official | 235:685d5f11838f | 1785 | /* Configure the USART Word Length, Parity and mode: |
mbed_official | 235:685d5f11838f | 1786 | Set the M bits according to husart->Init.WordLength value |
mbed_official | 235:685d5f11838f | 1787 | Set PCE and PS bits according to husart->Init.Parity value |
mbed_official | 235:685d5f11838f | 1788 | Set TE and RE bits according to husart->Init.Mode value */ |
mbed_official | 235:685d5f11838f | 1789 | tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode; |
mbed_official | 235:685d5f11838f | 1790 | |
mbed_official | 235:685d5f11838f | 1791 | /* Write to USART CR1 */ |
mbed_official | 235:685d5f11838f | 1792 | husart->Instance->CR1 = (uint32_t)tmpreg; |
mbed_official | 235:685d5f11838f | 1793 | |
mbed_official | 235:685d5f11838f | 1794 | /*-------------------------- USART CR3 Configuration -----------------------*/ |
mbed_official | 235:685d5f11838f | 1795 | /* Clear CTSE and RTSE bits */ |
mbed_official | 235:685d5f11838f | 1796 | husart->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE)); |
mbed_official | 235:685d5f11838f | 1797 | |
mbed_official | 235:685d5f11838f | 1798 | /*-------------------------- USART BRR Configuration -----------------------*/ |
mbed_official | 235:685d5f11838f | 1799 | if((husart->Instance == USART1) || (husart->Instance == USART6)) |
mbed_official | 235:685d5f11838f | 1800 | { |
mbed_official | 235:685d5f11838f | 1801 | husart->Instance->BRR = __USART_BRR(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate); |
mbed_official | 235:685d5f11838f | 1802 | } |
mbed_official | 235:685d5f11838f | 1803 | else |
mbed_official | 235:685d5f11838f | 1804 | { |
mbed_official | 235:685d5f11838f | 1805 | husart->Instance->BRR = __USART_BRR(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate); |
mbed_official | 235:685d5f11838f | 1806 | } |
mbed_official | 235:685d5f11838f | 1807 | } |
mbed_official | 235:685d5f11838f | 1808 | |
mbed_official | 235:685d5f11838f | 1809 | /** |
mbed_official | 235:685d5f11838f | 1810 | * @} |
mbed_official | 235:685d5f11838f | 1811 | */ |
mbed_official | 235:685d5f11838f | 1812 | |
mbed_official | 235:685d5f11838f | 1813 | #endif /* HAL_USART_MODULE_ENABLED */ |
mbed_official | 235:685d5f11838f | 1814 | /** |
mbed_official | 235:685d5f11838f | 1815 | * @} |
mbed_official | 235:685d5f11838f | 1816 | */ |
mbed_official | 235:685d5f11838f | 1817 | |
mbed_official | 235:685d5f11838f | 1818 | /** |
mbed_official | 235:685d5f11838f | 1819 | * @} |
mbed_official | 235:685d5f11838f | 1820 | */ |
mbed_official | 235:685d5f11838f | 1821 | |
mbed_official | 235:685d5f11838f | 1822 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |