Initial commit
mbed-dev-master/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_usart.c@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /** |
lypinator | 0:bb348c97df44 | 2 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 3 | * @file stm32f4xx_hal_usart.c |
lypinator | 0:bb348c97df44 | 4 | * @author MCD Application Team |
lypinator | 0:bb348c97df44 | 5 | * @brief USART HAL module driver. |
lypinator | 0:bb348c97df44 | 6 | * This file provides firmware functions to manage the following |
lypinator | 0:bb348c97df44 | 7 | * functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral: |
lypinator | 0:bb348c97df44 | 8 | * + Initialization and de-initialization functions |
lypinator | 0:bb348c97df44 | 9 | * + IO operation functions |
lypinator | 0:bb348c97df44 | 10 | * + Peripheral Control functions |
lypinator | 0:bb348c97df44 | 11 | @verbatim |
lypinator | 0:bb348c97df44 | 12 | ============================================================================== |
lypinator | 0:bb348c97df44 | 13 | ##### How to use this driver ##### |
lypinator | 0:bb348c97df44 | 14 | ============================================================================== |
lypinator | 0:bb348c97df44 | 15 | [..] |
lypinator | 0:bb348c97df44 | 16 | The USART HAL driver can be used as follows: |
lypinator | 0:bb348c97df44 | 17 | |
lypinator | 0:bb348c97df44 | 18 | (#) Declare a USART_HandleTypeDef handle structure. |
lypinator | 0:bb348c97df44 | 19 | (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit () API: |
lypinator | 0:bb348c97df44 | 20 | (##) Enable the USARTx interface clock. |
lypinator | 0:bb348c97df44 | 21 | (##) USART pins configuration: |
lypinator | 0:bb348c97df44 | 22 | (+++) Enable the clock for the USART GPIOs. |
lypinator | 0:bb348c97df44 | 23 | (+++) Configure these USART pins as alternate function pull-up. |
lypinator | 0:bb348c97df44 | 24 | (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(), |
lypinator | 0:bb348c97df44 | 25 | HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): |
lypinator | 0:bb348c97df44 | 26 | (+++) Configure the USARTx interrupt priority. |
lypinator | 0:bb348c97df44 | 27 | (+++) Enable the NVIC USART IRQ handle. |
lypinator | 0:bb348c97df44 | 28 | (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA() |
lypinator | 0:bb348c97df44 | 29 | HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): |
lypinator | 0:bb348c97df44 | 30 | (+++) Declare a DMA handle structure for the Tx/Rx stream. |
lypinator | 0:bb348c97df44 | 31 | (+++) Enable the DMAx interface clock. |
lypinator | 0:bb348c97df44 | 32 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
lypinator | 0:bb348c97df44 | 33 | (+++) Configure the DMA Tx/Rx Stream. |
lypinator | 0:bb348c97df44 | 34 | (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle. |
lypinator | 0:bb348c97df44 | 35 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream. |
lypinator | 0:bb348c97df44 | 36 | |
lypinator | 0:bb348c97df44 | 37 | (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware |
lypinator | 0:bb348c97df44 | 38 | flow control and Mode(Receiver/Transmitter) in the husart Init structure. |
lypinator | 0:bb348c97df44 | 39 | |
lypinator | 0:bb348c97df44 | 40 | (#) Initialize the USART registers by calling the HAL_USART_Init() API: |
lypinator | 0:bb348c97df44 | 41 | (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) |
lypinator | 0:bb348c97df44 | 42 | by calling the customized HAL_USART_MspInit(&husart) API. |
lypinator | 0:bb348c97df44 | 43 | |
lypinator | 0:bb348c97df44 | 44 | -@@- The specific USART interrupts (Transmission complete interrupt, |
lypinator | 0:bb348c97df44 | 45 | RXNE interrupt and Error Interrupts) will be managed using the macros |
lypinator | 0:bb348c97df44 | 46 | __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process. |
lypinator | 0:bb348c97df44 | 47 | |
lypinator | 0:bb348c97df44 | 48 | (#) Three operation modes are available within this driver : |
lypinator | 0:bb348c97df44 | 49 | |
lypinator | 0:bb348c97df44 | 50 | *** Polling mode IO operation *** |
lypinator | 0:bb348c97df44 | 51 | ================================= |
lypinator | 0:bb348c97df44 | 52 | [..] |
lypinator | 0:bb348c97df44 | 53 | (+) Send an amount of data in blocking mode using HAL_USART_Transmit() |
lypinator | 0:bb348c97df44 | 54 | (+) Receive an amount of data in blocking mode using HAL_USART_Receive() |
lypinator | 0:bb348c97df44 | 55 | |
lypinator | 0:bb348c97df44 | 56 | *** Interrupt mode IO operation *** |
lypinator | 0:bb348c97df44 | 57 | =================================== |
lypinator | 0:bb348c97df44 | 58 | [..] |
lypinator | 0:bb348c97df44 | 59 | (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT() |
lypinator | 0:bb348c97df44 | 60 | (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can |
lypinator | 0:bb348c97df44 | 61 | add his own code by customization of function pointer HAL_USART_TxCpltCallback |
lypinator | 0:bb348c97df44 | 62 | (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT() |
lypinator | 0:bb348c97df44 | 63 | (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can |
lypinator | 0:bb348c97df44 | 64 | add his own code by customization of function pointer HAL_USART_RxCpltCallback |
lypinator | 0:bb348c97df44 | 65 | (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can |
lypinator | 0:bb348c97df44 | 66 | add his own code by customization of function pointer HAL_USART_ErrorCallback |
lypinator | 0:bb348c97df44 | 67 | |
lypinator | 0:bb348c97df44 | 68 | *** DMA mode IO operation *** |
lypinator | 0:bb348c97df44 | 69 | ============================== |
lypinator | 0:bb348c97df44 | 70 | [..] |
lypinator | 0:bb348c97df44 | 71 | (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA() |
lypinator | 0:bb348c97df44 | 72 | (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can |
lypinator | 0:bb348c97df44 | 73 | add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback |
lypinator | 0:bb348c97df44 | 74 | (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can |
lypinator | 0:bb348c97df44 | 75 | add his own code by customization of function pointer HAL_USART_TxCpltCallback |
lypinator | 0:bb348c97df44 | 76 | (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA() |
lypinator | 0:bb348c97df44 | 77 | (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can |
lypinator | 0:bb348c97df44 | 78 | add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback |
lypinator | 0:bb348c97df44 | 79 | (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can |
lypinator | 0:bb348c97df44 | 80 | add his own code by customization of function pointer HAL_USART_RxCpltCallback |
lypinator | 0:bb348c97df44 | 81 | (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can |
lypinator | 0:bb348c97df44 | 82 | add his own code by customization of function pointer HAL_USART_ErrorCallback |
lypinator | 0:bb348c97df44 | 83 | (+) Pause the DMA Transfer using HAL_USART_DMAPause() |
lypinator | 0:bb348c97df44 | 84 | (+) Resume the DMA Transfer using HAL_USART_DMAResume() |
lypinator | 0:bb348c97df44 | 85 | (+) Stop the DMA Transfer using HAL_USART_DMAStop() |
lypinator | 0:bb348c97df44 | 86 | |
lypinator | 0:bb348c97df44 | 87 | *** USART HAL driver macros list *** |
lypinator | 0:bb348c97df44 | 88 | ============================================= |
lypinator | 0:bb348c97df44 | 89 | [..] |
lypinator | 0:bb348c97df44 | 90 | Below the list of most used macros in USART HAL driver. |
lypinator | 0:bb348c97df44 | 91 | |
lypinator | 0:bb348c97df44 | 92 | (+) __HAL_USART_ENABLE: Enable the USART peripheral |
lypinator | 0:bb348c97df44 | 93 | (+) __HAL_USART_DISABLE: Disable the USART peripheral |
lypinator | 0:bb348c97df44 | 94 | (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not |
lypinator | 0:bb348c97df44 | 95 | (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag |
lypinator | 0:bb348c97df44 | 96 | (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt |
lypinator | 0:bb348c97df44 | 97 | (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt |
lypinator | 0:bb348c97df44 | 98 | |
lypinator | 0:bb348c97df44 | 99 | [..] |
lypinator | 0:bb348c97df44 | 100 | (@) You can refer to the USART HAL driver header file for more useful macros |
lypinator | 0:bb348c97df44 | 101 | |
lypinator | 0:bb348c97df44 | 102 | @endverbatim |
lypinator | 0:bb348c97df44 | 103 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 104 | * @attention |
lypinator | 0:bb348c97df44 | 105 | * |
lypinator | 0:bb348c97df44 | 106 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
lypinator | 0:bb348c97df44 | 107 | * |
lypinator | 0:bb348c97df44 | 108 | * Redistribution and use in source and binary forms, with or without modification, |
lypinator | 0:bb348c97df44 | 109 | * are permitted provided that the following conditions are met: |
lypinator | 0:bb348c97df44 | 110 | * 1. Redistributions of source code must retain the above copyright notice, |
lypinator | 0:bb348c97df44 | 111 | * this list of conditions and the following disclaimer. |
lypinator | 0:bb348c97df44 | 112 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
lypinator | 0:bb348c97df44 | 113 | * this list of conditions and the following disclaimer in the documentation |
lypinator | 0:bb348c97df44 | 114 | * and/or other materials provided with the distribution. |
lypinator | 0:bb348c97df44 | 115 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
lypinator | 0:bb348c97df44 | 116 | * may be used to endorse or promote products derived from this software |
lypinator | 0:bb348c97df44 | 117 | * without specific prior written permission. |
lypinator | 0:bb348c97df44 | 118 | * |
lypinator | 0:bb348c97df44 | 119 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
lypinator | 0:bb348c97df44 | 120 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
lypinator | 0:bb348c97df44 | 121 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lypinator | 0:bb348c97df44 | 122 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
lypinator | 0:bb348c97df44 | 123 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
lypinator | 0:bb348c97df44 | 124 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
lypinator | 0:bb348c97df44 | 125 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
lypinator | 0:bb348c97df44 | 126 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
lypinator | 0:bb348c97df44 | 127 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
lypinator | 0:bb348c97df44 | 128 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lypinator | 0:bb348c97df44 | 129 | * |
lypinator | 0:bb348c97df44 | 130 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 131 | */ |
lypinator | 0:bb348c97df44 | 132 | |
lypinator | 0:bb348c97df44 | 133 | /* Includes ------------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 134 | #include "stm32f4xx_hal.h" |
lypinator | 0:bb348c97df44 | 135 | |
lypinator | 0:bb348c97df44 | 136 | /** @addtogroup STM32F4xx_HAL_Driver |
lypinator | 0:bb348c97df44 | 137 | * @{ |
lypinator | 0:bb348c97df44 | 138 | */ |
lypinator | 0:bb348c97df44 | 139 | |
lypinator | 0:bb348c97df44 | 140 | /** @defgroup USART USART |
lypinator | 0:bb348c97df44 | 141 | * @brief HAL USART Synchronous module driver |
lypinator | 0:bb348c97df44 | 142 | * @{ |
lypinator | 0:bb348c97df44 | 143 | */ |
lypinator | 0:bb348c97df44 | 144 | #ifdef HAL_USART_MODULE_ENABLED |
lypinator | 0:bb348c97df44 | 145 | /* Private typedef -----------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 146 | /* Private define ------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 147 | /** @addtogroup USART_Private_Constants |
lypinator | 0:bb348c97df44 | 148 | * @{ |
lypinator | 0:bb348c97df44 | 149 | */ |
lypinator | 0:bb348c97df44 | 150 | #define DUMMY_DATA 0xFFFFU |
lypinator | 0:bb348c97df44 | 151 | #define USART_TIMEOUT_VALUE 22000U |
lypinator | 0:bb348c97df44 | 152 | /** |
lypinator | 0:bb348c97df44 | 153 | * @} |
lypinator | 0:bb348c97df44 | 154 | */ |
lypinator | 0:bb348c97df44 | 155 | /* Private macro -------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 156 | /* Private variables ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 157 | /* Private function prototypes -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 158 | /* Private functions ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 159 | /** @addtogroup USART_Private_Functions |
lypinator | 0:bb348c97df44 | 160 | * @{ |
lypinator | 0:bb348c97df44 | 161 | */ |
lypinator | 0:bb348c97df44 | 162 | static void USART_EndTxTransfer(USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 163 | static void USART_EndRxTransfer(USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 164 | static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 165 | static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 166 | static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 167 | static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 168 | static void USART_SetConfig (USART_HandleTypeDef *husart); |
lypinator | 0:bb348c97df44 | 169 | static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 170 | static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 171 | static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 172 | static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 173 | static void USART_DMAError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 174 | static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 175 | static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 176 | static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 177 | |
lypinator | 0:bb348c97df44 | 178 | static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout); |
lypinator | 0:bb348c97df44 | 179 | /** |
lypinator | 0:bb348c97df44 | 180 | * @} |
lypinator | 0:bb348c97df44 | 181 | */ |
lypinator | 0:bb348c97df44 | 182 | |
lypinator | 0:bb348c97df44 | 183 | /* Exported functions --------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 184 | /** @defgroup USART_Exported_Functions USART Exported Functions |
lypinator | 0:bb348c97df44 | 185 | * @{ |
lypinator | 0:bb348c97df44 | 186 | */ |
lypinator | 0:bb348c97df44 | 187 | |
lypinator | 0:bb348c97df44 | 188 | /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions |
lypinator | 0:bb348c97df44 | 189 | * @brief Initialization and Configuration functions |
lypinator | 0:bb348c97df44 | 190 | * |
lypinator | 0:bb348c97df44 | 191 | @verbatim |
lypinator | 0:bb348c97df44 | 192 | ============================================================================== |
lypinator | 0:bb348c97df44 | 193 | ##### Initialization and Configuration functions ##### |
lypinator | 0:bb348c97df44 | 194 | ============================================================================== |
lypinator | 0:bb348c97df44 | 195 | [..] |
lypinator | 0:bb348c97df44 | 196 | This subsection provides a set of functions allowing to initialize the USART |
lypinator | 0:bb348c97df44 | 197 | in asynchronous and in synchronous modes. |
lypinator | 0:bb348c97df44 | 198 | (+) For the asynchronous mode only these parameters can be configured: |
lypinator | 0:bb348c97df44 | 199 | (++) Baud Rate |
lypinator | 0:bb348c97df44 | 200 | (++) Word Length |
lypinator | 0:bb348c97df44 | 201 | (++) Stop Bit |
lypinator | 0:bb348c97df44 | 202 | (++) Parity: If the parity is enabled, then the MSB bit of the data written |
lypinator | 0:bb348c97df44 | 203 | in the data register is transmitted but is changed by the parity bit. |
lypinator | 0:bb348c97df44 | 204 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
lypinator | 0:bb348c97df44 | 205 | please refer to Reference manual for possible USART frame formats. |
lypinator | 0:bb348c97df44 | 206 | (++) USART polarity |
lypinator | 0:bb348c97df44 | 207 | (++) USART phase |
lypinator | 0:bb348c97df44 | 208 | (++) USART LastBit |
lypinator | 0:bb348c97df44 | 209 | (++) Receiver/transmitter modes |
lypinator | 0:bb348c97df44 | 210 | |
lypinator | 0:bb348c97df44 | 211 | [..] |
lypinator | 0:bb348c97df44 | 212 | The HAL_USART_Init() function follows the USART synchronous configuration |
lypinator | 0:bb348c97df44 | 213 | procedure (details for the procedure are available in reference manual (RM0329)). |
lypinator | 0:bb348c97df44 | 214 | |
lypinator | 0:bb348c97df44 | 215 | @endverbatim |
lypinator | 0:bb348c97df44 | 216 | * @{ |
lypinator | 0:bb348c97df44 | 217 | */ |
lypinator | 0:bb348c97df44 | 218 | |
lypinator | 0:bb348c97df44 | 219 | /** |
lypinator | 0:bb348c97df44 | 220 | * @brief Initializes the USART mode according to the specified |
lypinator | 0:bb348c97df44 | 221 | * parameters in the USART_InitTypeDef and create the associated handle. |
lypinator | 0:bb348c97df44 | 222 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 223 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 224 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 225 | */ |
lypinator | 0:bb348c97df44 | 226 | HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 227 | { |
lypinator | 0:bb348c97df44 | 228 | /* Check the USART handle allocation */ |
lypinator | 0:bb348c97df44 | 229 | if(husart == NULL) |
lypinator | 0:bb348c97df44 | 230 | { |
lypinator | 0:bb348c97df44 | 231 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 232 | } |
lypinator | 0:bb348c97df44 | 233 | |
lypinator | 0:bb348c97df44 | 234 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 235 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
lypinator | 0:bb348c97df44 | 236 | |
lypinator | 0:bb348c97df44 | 237 | if(husart->State == HAL_USART_STATE_RESET) |
lypinator | 0:bb348c97df44 | 238 | { |
lypinator | 0:bb348c97df44 | 239 | /* Allocate lock resource and initialize it */ |
lypinator | 0:bb348c97df44 | 240 | husart->Lock = HAL_UNLOCKED; |
lypinator | 0:bb348c97df44 | 241 | /* Init the low level hardware */ |
lypinator | 0:bb348c97df44 | 242 | HAL_USART_MspInit(husart); |
lypinator | 0:bb348c97df44 | 243 | } |
lypinator | 0:bb348c97df44 | 244 | |
lypinator | 0:bb348c97df44 | 245 | husart->State = HAL_USART_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 246 | |
lypinator | 0:bb348c97df44 | 247 | /* Set the USART Communication parameters */ |
lypinator | 0:bb348c97df44 | 248 | USART_SetConfig(husart); |
lypinator | 0:bb348c97df44 | 249 | |
lypinator | 0:bb348c97df44 | 250 | /* In USART mode, the following bits must be kept cleared: |
lypinator | 0:bb348c97df44 | 251 | - LINEN bit in the USART_CR2 register |
lypinator | 0:bb348c97df44 | 252 | - HDSEL, SCEN and IREN bits in the USART_CR3 register */ |
lypinator | 0:bb348c97df44 | 253 | CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN); |
lypinator | 0:bb348c97df44 | 254 | CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); |
lypinator | 0:bb348c97df44 | 255 | |
lypinator | 0:bb348c97df44 | 256 | /* Enable the Peripheral */ |
lypinator | 0:bb348c97df44 | 257 | __HAL_USART_ENABLE(husart); |
lypinator | 0:bb348c97df44 | 258 | |
lypinator | 0:bb348c97df44 | 259 | /* Initialize the USART state */ |
lypinator | 0:bb348c97df44 | 260 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 261 | husart->State= HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 262 | |
lypinator | 0:bb348c97df44 | 263 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 264 | } |
lypinator | 0:bb348c97df44 | 265 | |
lypinator | 0:bb348c97df44 | 266 | /** |
lypinator | 0:bb348c97df44 | 267 | * @brief DeInitializes the USART peripheral. |
lypinator | 0:bb348c97df44 | 268 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 269 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 270 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 271 | */ |
lypinator | 0:bb348c97df44 | 272 | HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 273 | { |
lypinator | 0:bb348c97df44 | 274 | /* Check the USART handle allocation */ |
lypinator | 0:bb348c97df44 | 275 | if(husart == NULL) |
lypinator | 0:bb348c97df44 | 276 | { |
lypinator | 0:bb348c97df44 | 277 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 278 | } |
lypinator | 0:bb348c97df44 | 279 | |
lypinator | 0:bb348c97df44 | 280 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 281 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
lypinator | 0:bb348c97df44 | 282 | |
lypinator | 0:bb348c97df44 | 283 | husart->State = HAL_USART_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 284 | |
lypinator | 0:bb348c97df44 | 285 | /* Disable the Peripheral */ |
lypinator | 0:bb348c97df44 | 286 | __HAL_USART_DISABLE(husart); |
lypinator | 0:bb348c97df44 | 287 | |
lypinator | 0:bb348c97df44 | 288 | /* DeInit the low level hardware */ |
lypinator | 0:bb348c97df44 | 289 | HAL_USART_MspDeInit(husart); |
lypinator | 0:bb348c97df44 | 290 | |
lypinator | 0:bb348c97df44 | 291 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 292 | husart->State = HAL_USART_STATE_RESET; |
lypinator | 0:bb348c97df44 | 293 | |
lypinator | 0:bb348c97df44 | 294 | /* Release Lock */ |
lypinator | 0:bb348c97df44 | 295 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 296 | |
lypinator | 0:bb348c97df44 | 297 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 298 | } |
lypinator | 0:bb348c97df44 | 299 | |
lypinator | 0:bb348c97df44 | 300 | /** |
lypinator | 0:bb348c97df44 | 301 | * @brief USART MSP Init. |
lypinator | 0:bb348c97df44 | 302 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 303 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 304 | * @retval None |
lypinator | 0:bb348c97df44 | 305 | */ |
lypinator | 0:bb348c97df44 | 306 | __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 307 | { |
lypinator | 0:bb348c97df44 | 308 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 309 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 310 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 311 | the HAL_USART_MspInit could be implemented in the user file |
lypinator | 0:bb348c97df44 | 312 | */ |
lypinator | 0:bb348c97df44 | 313 | } |
lypinator | 0:bb348c97df44 | 314 | |
lypinator | 0:bb348c97df44 | 315 | /** |
lypinator | 0:bb348c97df44 | 316 | * @brief USART MSP DeInit. |
lypinator | 0:bb348c97df44 | 317 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 318 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 319 | * @retval None |
lypinator | 0:bb348c97df44 | 320 | */ |
lypinator | 0:bb348c97df44 | 321 | __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 322 | { |
lypinator | 0:bb348c97df44 | 323 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 324 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 325 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 326 | the HAL_USART_MspDeInit could be implemented in the user file |
lypinator | 0:bb348c97df44 | 327 | */ |
lypinator | 0:bb348c97df44 | 328 | } |
lypinator | 0:bb348c97df44 | 329 | |
lypinator | 0:bb348c97df44 | 330 | /** |
lypinator | 0:bb348c97df44 | 331 | * @} |
lypinator | 0:bb348c97df44 | 332 | */ |
lypinator | 0:bb348c97df44 | 333 | |
lypinator | 0:bb348c97df44 | 334 | /** @defgroup USART_Exported_Functions_Group2 IO operation functions |
lypinator | 0:bb348c97df44 | 335 | * @brief USART Transmit and Receive functions |
lypinator | 0:bb348c97df44 | 336 | * |
lypinator | 0:bb348c97df44 | 337 | @verbatim |
lypinator | 0:bb348c97df44 | 338 | ============================================================================== |
lypinator | 0:bb348c97df44 | 339 | ##### IO operation functions ##### |
lypinator | 0:bb348c97df44 | 340 | ============================================================================== |
lypinator | 0:bb348c97df44 | 341 | [..] |
lypinator | 0:bb348c97df44 | 342 | This subsection provides a set of functions allowing to manage the USART synchronous |
lypinator | 0:bb348c97df44 | 343 | data transfers. |
lypinator | 0:bb348c97df44 | 344 | |
lypinator | 0:bb348c97df44 | 345 | [..] |
lypinator | 0:bb348c97df44 | 346 | The USART supports master mode only: it cannot receive or send data related to an input |
lypinator | 0:bb348c97df44 | 347 | clock (SCLK is always an output). |
lypinator | 0:bb348c97df44 | 348 | |
lypinator | 0:bb348c97df44 | 349 | (#) There are two modes of transfer: |
lypinator | 0:bb348c97df44 | 350 | (++) Blocking mode: The communication is performed in polling mode. |
lypinator | 0:bb348c97df44 | 351 | The HAL status of all data processing is returned by the same function |
lypinator | 0:bb348c97df44 | 352 | after finishing transfer. |
lypinator | 0:bb348c97df44 | 353 | (++) No-Blocking mode: The communication is performed using Interrupts |
lypinator | 0:bb348c97df44 | 354 | or DMA, These API's return the HAL status. |
lypinator | 0:bb348c97df44 | 355 | The end of the data processing will be indicated through the |
lypinator | 0:bb348c97df44 | 356 | dedicated USART IRQ when using Interrupt mode or the DMA IRQ when |
lypinator | 0:bb348c97df44 | 357 | using DMA mode. |
lypinator | 0:bb348c97df44 | 358 | The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() |
lypinator | 0:bb348c97df44 | 359 | user callbacks |
lypinator | 0:bb348c97df44 | 360 | will be executed respectively at the end of the transmit or Receive process |
lypinator | 0:bb348c97df44 | 361 | The HAL_USART_ErrorCallback() user callback will be executed when a communication |
lypinator | 0:bb348c97df44 | 362 | error is detected |
lypinator | 0:bb348c97df44 | 363 | |
lypinator | 0:bb348c97df44 | 364 | (#) Blocking mode APIs are : |
lypinator | 0:bb348c97df44 | 365 | (++) HAL_USART_Transmit() in simplex mode |
lypinator | 0:bb348c97df44 | 366 | (++) HAL_USART_Receive() in full duplex receive only |
lypinator | 0:bb348c97df44 | 367 | (++) HAL_USART_TransmitReceive() in full duplex mode |
lypinator | 0:bb348c97df44 | 368 | |
lypinator | 0:bb348c97df44 | 369 | (#) Non Blocking mode APIs with Interrupt are : |
lypinator | 0:bb348c97df44 | 370 | (++) HAL_USART_Transmit_IT()in simplex mode |
lypinator | 0:bb348c97df44 | 371 | (++) HAL_USART_Receive_IT() in full duplex receive only |
lypinator | 0:bb348c97df44 | 372 | (++) HAL_USART_TransmitReceive_IT() in full duplex mode |
lypinator | 0:bb348c97df44 | 373 | (++) HAL_USART_IRQHandler() |
lypinator | 0:bb348c97df44 | 374 | |
lypinator | 0:bb348c97df44 | 375 | (#) Non Blocking mode functions with DMA are : |
lypinator | 0:bb348c97df44 | 376 | (++) HAL_USART_Transmit_DMA()in simplex mode |
lypinator | 0:bb348c97df44 | 377 | (++) HAL_USART_Receive_DMA() in full duplex receive only |
lypinator | 0:bb348c97df44 | 378 | (++) HAL_USART_TransmitReceie_DMA() in full duplex mode |
lypinator | 0:bb348c97df44 | 379 | (++) HAL_USART_DMAPause() |
lypinator | 0:bb348c97df44 | 380 | (++) HAL_USART_DMAResume() |
lypinator | 0:bb348c97df44 | 381 | (++) HAL_USART_DMAStop() |
lypinator | 0:bb348c97df44 | 382 | |
lypinator | 0:bb348c97df44 | 383 | (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: |
lypinator | 0:bb348c97df44 | 384 | (++) HAL_USART_TxHalfCpltCallback() |
lypinator | 0:bb348c97df44 | 385 | (++) HAL_USART_TxCpltCallback() |
lypinator | 0:bb348c97df44 | 386 | (++) HAL_USART_RxHalfCpltCallback() |
lypinator | 0:bb348c97df44 | 387 | (++) HAL_USART_RxCpltCallback() |
lypinator | 0:bb348c97df44 | 388 | (++) HAL_USART_ErrorCallback() |
lypinator | 0:bb348c97df44 | 389 | (++) HAL_USART_TxRxCpltCallback() |
lypinator | 0:bb348c97df44 | 390 | |
lypinator | 0:bb348c97df44 | 391 | @endverbatim |
lypinator | 0:bb348c97df44 | 392 | * @{ |
lypinator | 0:bb348c97df44 | 393 | */ |
lypinator | 0:bb348c97df44 | 394 | |
lypinator | 0:bb348c97df44 | 395 | /** |
lypinator | 0:bb348c97df44 | 396 | * @brief Simplex Send an amount of data in blocking mode. |
lypinator | 0:bb348c97df44 | 397 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 398 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 399 | * @param pTxData Pointer to data buffer |
lypinator | 0:bb348c97df44 | 400 | * @param Size Amount of data to be sent |
lypinator | 0:bb348c97df44 | 401 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 402 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 403 | */ |
lypinator | 0:bb348c97df44 | 404 | HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 405 | { |
lypinator | 0:bb348c97df44 | 406 | uint16_t* tmp; |
lypinator | 0:bb348c97df44 | 407 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 408 | |
lypinator | 0:bb348c97df44 | 409 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 410 | { |
lypinator | 0:bb348c97df44 | 411 | if((pTxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 412 | { |
lypinator | 0:bb348c97df44 | 413 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 414 | } |
lypinator | 0:bb348c97df44 | 415 | |
lypinator | 0:bb348c97df44 | 416 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 417 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 418 | |
lypinator | 0:bb348c97df44 | 419 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 420 | husart->State = HAL_USART_STATE_BUSY_TX; |
lypinator | 0:bb348c97df44 | 421 | |
lypinator | 0:bb348c97df44 | 422 | /* Init tickstart for timeout managment */ |
lypinator | 0:bb348c97df44 | 423 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 424 | |
lypinator | 0:bb348c97df44 | 425 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 426 | husart->TxXferCount = Size; |
lypinator | 0:bb348c97df44 | 427 | while(husart->TxXferCount > 0U) |
lypinator | 0:bb348c97df44 | 428 | { |
lypinator | 0:bb348c97df44 | 429 | husart->TxXferCount--; |
lypinator | 0:bb348c97df44 | 430 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 431 | { |
lypinator | 0:bb348c97df44 | 432 | /* Wait for TC flag in order to write data in DR */ |
lypinator | 0:bb348c97df44 | 433 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 434 | { |
lypinator | 0:bb348c97df44 | 435 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 436 | } |
lypinator | 0:bb348c97df44 | 437 | tmp = (uint16_t*) pTxData; |
lypinator | 0:bb348c97df44 | 438 | husart->Instance->DR = (*tmp & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 439 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 440 | { |
lypinator | 0:bb348c97df44 | 441 | pTxData += 2U; |
lypinator | 0:bb348c97df44 | 442 | } |
lypinator | 0:bb348c97df44 | 443 | else |
lypinator | 0:bb348c97df44 | 444 | { |
lypinator | 0:bb348c97df44 | 445 | pTxData += 1U; |
lypinator | 0:bb348c97df44 | 446 | } |
lypinator | 0:bb348c97df44 | 447 | } |
lypinator | 0:bb348c97df44 | 448 | else |
lypinator | 0:bb348c97df44 | 449 | { |
lypinator | 0:bb348c97df44 | 450 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 451 | { |
lypinator | 0:bb348c97df44 | 452 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 453 | } |
lypinator | 0:bb348c97df44 | 454 | husart->Instance->DR = (*pTxData++ & (uint8_t)0xFF); |
lypinator | 0:bb348c97df44 | 455 | } |
lypinator | 0:bb348c97df44 | 456 | } |
lypinator | 0:bb348c97df44 | 457 | |
lypinator | 0:bb348c97df44 | 458 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 459 | { |
lypinator | 0:bb348c97df44 | 460 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 461 | } |
lypinator | 0:bb348c97df44 | 462 | |
lypinator | 0:bb348c97df44 | 463 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 464 | |
lypinator | 0:bb348c97df44 | 465 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 466 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 467 | |
lypinator | 0:bb348c97df44 | 468 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 469 | } |
lypinator | 0:bb348c97df44 | 470 | else |
lypinator | 0:bb348c97df44 | 471 | { |
lypinator | 0:bb348c97df44 | 472 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 473 | } |
lypinator | 0:bb348c97df44 | 474 | } |
lypinator | 0:bb348c97df44 | 475 | |
lypinator | 0:bb348c97df44 | 476 | /** |
lypinator | 0:bb348c97df44 | 477 | * @brief Full-Duplex Receive an amount of data in blocking mode. |
lypinator | 0:bb348c97df44 | 478 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 479 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 480 | * @param pRxData Pointer to data buffer |
lypinator | 0:bb348c97df44 | 481 | * @param Size Amount of data to be received |
lypinator | 0:bb348c97df44 | 482 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 483 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 484 | */ |
lypinator | 0:bb348c97df44 | 485 | HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 486 | { |
lypinator | 0:bb348c97df44 | 487 | uint16_t* tmp; |
lypinator | 0:bb348c97df44 | 488 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 489 | |
lypinator | 0:bb348c97df44 | 490 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 491 | { |
lypinator | 0:bb348c97df44 | 492 | if((pRxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 493 | { |
lypinator | 0:bb348c97df44 | 494 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 495 | } |
lypinator | 0:bb348c97df44 | 496 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 497 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 498 | |
lypinator | 0:bb348c97df44 | 499 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 500 | husart->State = HAL_USART_STATE_BUSY_RX; |
lypinator | 0:bb348c97df44 | 501 | |
lypinator | 0:bb348c97df44 | 502 | /* Init tickstart for timeout managment */ |
lypinator | 0:bb348c97df44 | 503 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 504 | |
lypinator | 0:bb348c97df44 | 505 | husart->RxXferSize = Size; |
lypinator | 0:bb348c97df44 | 506 | husart->RxXferCount = Size; |
lypinator | 0:bb348c97df44 | 507 | /* Check the remain data to be received */ |
lypinator | 0:bb348c97df44 | 508 | while(husart->RxXferCount > 0U) |
lypinator | 0:bb348c97df44 | 509 | { |
lypinator | 0:bb348c97df44 | 510 | husart->RxXferCount--; |
lypinator | 0:bb348c97df44 | 511 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 512 | { |
lypinator | 0:bb348c97df44 | 513 | /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */ |
lypinator | 0:bb348c97df44 | 514 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 515 | { |
lypinator | 0:bb348c97df44 | 516 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 517 | } |
lypinator | 0:bb348c97df44 | 518 | /* Send dummy byte in order to generate clock */ |
lypinator | 0:bb348c97df44 | 519 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 520 | |
lypinator | 0:bb348c97df44 | 521 | /* Wait for RXNE Flag */ |
lypinator | 0:bb348c97df44 | 522 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 523 | { |
lypinator | 0:bb348c97df44 | 524 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 525 | } |
lypinator | 0:bb348c97df44 | 526 | tmp = (uint16_t*) pRxData ; |
lypinator | 0:bb348c97df44 | 527 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 528 | { |
lypinator | 0:bb348c97df44 | 529 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 530 | pRxData +=2U; |
lypinator | 0:bb348c97df44 | 531 | } |
lypinator | 0:bb348c97df44 | 532 | else |
lypinator | 0:bb348c97df44 | 533 | { |
lypinator | 0:bb348c97df44 | 534 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
lypinator | 0:bb348c97df44 | 535 | pRxData +=1U; |
lypinator | 0:bb348c97df44 | 536 | } |
lypinator | 0:bb348c97df44 | 537 | } |
lypinator | 0:bb348c97df44 | 538 | else |
lypinator | 0:bb348c97df44 | 539 | { |
lypinator | 0:bb348c97df44 | 540 | /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */ |
lypinator | 0:bb348c97df44 | 541 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 542 | { |
lypinator | 0:bb348c97df44 | 543 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 544 | } |
lypinator | 0:bb348c97df44 | 545 | |
lypinator | 0:bb348c97df44 | 546 | /* Send Dummy Byte in order to generate clock */ |
lypinator | 0:bb348c97df44 | 547 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF); |
lypinator | 0:bb348c97df44 | 548 | |
lypinator | 0:bb348c97df44 | 549 | /* Wait until RXNE flag is set to receive the byte */ |
lypinator | 0:bb348c97df44 | 550 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 551 | { |
lypinator | 0:bb348c97df44 | 552 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 553 | } |
lypinator | 0:bb348c97df44 | 554 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 555 | { |
lypinator | 0:bb348c97df44 | 556 | /* Receive data */ |
lypinator | 0:bb348c97df44 | 557 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 558 | } |
lypinator | 0:bb348c97df44 | 559 | else |
lypinator | 0:bb348c97df44 | 560 | { |
lypinator | 0:bb348c97df44 | 561 | /* Receive data */ |
lypinator | 0:bb348c97df44 | 562 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
lypinator | 0:bb348c97df44 | 563 | } |
lypinator | 0:bb348c97df44 | 564 | |
lypinator | 0:bb348c97df44 | 565 | } |
lypinator | 0:bb348c97df44 | 566 | } |
lypinator | 0:bb348c97df44 | 567 | |
lypinator | 0:bb348c97df44 | 568 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 569 | |
lypinator | 0:bb348c97df44 | 570 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 571 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 572 | |
lypinator | 0:bb348c97df44 | 573 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 574 | } |
lypinator | 0:bb348c97df44 | 575 | else |
lypinator | 0:bb348c97df44 | 576 | { |
lypinator | 0:bb348c97df44 | 577 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 578 | } |
lypinator | 0:bb348c97df44 | 579 | } |
lypinator | 0:bb348c97df44 | 580 | |
lypinator | 0:bb348c97df44 | 581 | /** |
lypinator | 0:bb348c97df44 | 582 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (blocking mode). |
lypinator | 0:bb348c97df44 | 583 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 584 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 585 | * @param pTxData Pointer to data transmitted buffer |
lypinator | 0:bb348c97df44 | 586 | * @param pRxData Pointer to data received buffer |
lypinator | 0:bb348c97df44 | 587 | * @param Size Amount of data to be sent |
lypinator | 0:bb348c97df44 | 588 | * @param Timeout Timeout duration |
lypinator | 0:bb348c97df44 | 589 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 590 | */ |
lypinator | 0:bb348c97df44 | 591 | HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 592 | { |
lypinator | 0:bb348c97df44 | 593 | uint16_t* tmp; |
lypinator | 0:bb348c97df44 | 594 | uint32_t tickstart = 0U; |
lypinator | 0:bb348c97df44 | 595 | |
lypinator | 0:bb348c97df44 | 596 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 597 | { |
lypinator | 0:bb348c97df44 | 598 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 599 | { |
lypinator | 0:bb348c97df44 | 600 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 601 | } |
lypinator | 0:bb348c97df44 | 602 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 603 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 604 | |
lypinator | 0:bb348c97df44 | 605 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 606 | husart->State = HAL_USART_STATE_BUSY_RX; |
lypinator | 0:bb348c97df44 | 607 | |
lypinator | 0:bb348c97df44 | 608 | /* Init tickstart for timeout managment */ |
lypinator | 0:bb348c97df44 | 609 | tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 610 | |
lypinator | 0:bb348c97df44 | 611 | husart->RxXferSize = Size; |
lypinator | 0:bb348c97df44 | 612 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 613 | husart->TxXferCount = Size; |
lypinator | 0:bb348c97df44 | 614 | husart->RxXferCount = Size; |
lypinator | 0:bb348c97df44 | 615 | |
lypinator | 0:bb348c97df44 | 616 | /* Check the remain data to be received */ |
lypinator | 0:bb348c97df44 | 617 | while(husart->TxXferCount > 0U) |
lypinator | 0:bb348c97df44 | 618 | { |
lypinator | 0:bb348c97df44 | 619 | husart->TxXferCount--; |
lypinator | 0:bb348c97df44 | 620 | husart->RxXferCount--; |
lypinator | 0:bb348c97df44 | 621 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 622 | { |
lypinator | 0:bb348c97df44 | 623 | /* Wait for TC flag in order to write data in DR */ |
lypinator | 0:bb348c97df44 | 624 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 625 | { |
lypinator | 0:bb348c97df44 | 626 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 627 | } |
lypinator | 0:bb348c97df44 | 628 | tmp = (uint16_t*) pTxData; |
lypinator | 0:bb348c97df44 | 629 | husart->Instance->DR = (*tmp & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 630 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 631 | { |
lypinator | 0:bb348c97df44 | 632 | pTxData += 2U; |
lypinator | 0:bb348c97df44 | 633 | } |
lypinator | 0:bb348c97df44 | 634 | else |
lypinator | 0:bb348c97df44 | 635 | { |
lypinator | 0:bb348c97df44 | 636 | pTxData += 1U; |
lypinator | 0:bb348c97df44 | 637 | } |
lypinator | 0:bb348c97df44 | 638 | |
lypinator | 0:bb348c97df44 | 639 | /* Wait for RXNE Flag */ |
lypinator | 0:bb348c97df44 | 640 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 641 | { |
lypinator | 0:bb348c97df44 | 642 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 643 | } |
lypinator | 0:bb348c97df44 | 644 | tmp = (uint16_t*) pRxData ; |
lypinator | 0:bb348c97df44 | 645 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 646 | { |
lypinator | 0:bb348c97df44 | 647 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 648 | pRxData += 2U; |
lypinator | 0:bb348c97df44 | 649 | } |
lypinator | 0:bb348c97df44 | 650 | else |
lypinator | 0:bb348c97df44 | 651 | { |
lypinator | 0:bb348c97df44 | 652 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
lypinator | 0:bb348c97df44 | 653 | pRxData += 1U; |
lypinator | 0:bb348c97df44 | 654 | } |
lypinator | 0:bb348c97df44 | 655 | } |
lypinator | 0:bb348c97df44 | 656 | else |
lypinator | 0:bb348c97df44 | 657 | { |
lypinator | 0:bb348c97df44 | 658 | /* Wait for TC flag in order to write data in DR */ |
lypinator | 0:bb348c97df44 | 659 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 660 | { |
lypinator | 0:bb348c97df44 | 661 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 662 | } |
lypinator | 0:bb348c97df44 | 663 | husart->Instance->DR = (*pTxData++ & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 664 | |
lypinator | 0:bb348c97df44 | 665 | /* Wait for RXNE Flag */ |
lypinator | 0:bb348c97df44 | 666 | if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
lypinator | 0:bb348c97df44 | 667 | { |
lypinator | 0:bb348c97df44 | 668 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 669 | } |
lypinator | 0:bb348c97df44 | 670 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 671 | { |
lypinator | 0:bb348c97df44 | 672 | /* Receive data */ |
lypinator | 0:bb348c97df44 | 673 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 674 | } |
lypinator | 0:bb348c97df44 | 675 | else |
lypinator | 0:bb348c97df44 | 676 | { |
lypinator | 0:bb348c97df44 | 677 | /* Receive data */ |
lypinator | 0:bb348c97df44 | 678 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
lypinator | 0:bb348c97df44 | 679 | } |
lypinator | 0:bb348c97df44 | 680 | } |
lypinator | 0:bb348c97df44 | 681 | } |
lypinator | 0:bb348c97df44 | 682 | |
lypinator | 0:bb348c97df44 | 683 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 684 | |
lypinator | 0:bb348c97df44 | 685 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 686 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 687 | |
lypinator | 0:bb348c97df44 | 688 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 689 | } |
lypinator | 0:bb348c97df44 | 690 | else |
lypinator | 0:bb348c97df44 | 691 | { |
lypinator | 0:bb348c97df44 | 692 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 693 | } |
lypinator | 0:bb348c97df44 | 694 | } |
lypinator | 0:bb348c97df44 | 695 | |
lypinator | 0:bb348c97df44 | 696 | /** |
lypinator | 0:bb348c97df44 | 697 | * @brief Simplex Send an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 698 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 699 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 700 | * @param pTxData Pointer to data buffer |
lypinator | 0:bb348c97df44 | 701 | * @param Size Amount of data to be sent |
lypinator | 0:bb348c97df44 | 702 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 703 | * @note The USART errors are not managed to avoid the overrun error. |
lypinator | 0:bb348c97df44 | 704 | */ |
lypinator | 0:bb348c97df44 | 705 | HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size) |
lypinator | 0:bb348c97df44 | 706 | { |
lypinator | 0:bb348c97df44 | 707 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 708 | { |
lypinator | 0:bb348c97df44 | 709 | if((pTxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 710 | { |
lypinator | 0:bb348c97df44 | 711 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 712 | } |
lypinator | 0:bb348c97df44 | 713 | |
lypinator | 0:bb348c97df44 | 714 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 715 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 716 | |
lypinator | 0:bb348c97df44 | 717 | husart->pTxBuffPtr = pTxData; |
lypinator | 0:bb348c97df44 | 718 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 719 | husart->TxXferCount = Size; |
lypinator | 0:bb348c97df44 | 720 | |
lypinator | 0:bb348c97df44 | 721 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 722 | husart->State = HAL_USART_STATE_BUSY_TX; |
lypinator | 0:bb348c97df44 | 723 | |
lypinator | 0:bb348c97df44 | 724 | /* The USART Error Interrupts: (Frame error, Noise error, Overrun error) |
lypinator | 0:bb348c97df44 | 725 | are not managed by the USART transmit process to avoid the overrun interrupt |
lypinator | 0:bb348c97df44 | 726 | when the USART mode is configured for transmit and receive "USART_MODE_TX_RX" |
lypinator | 0:bb348c97df44 | 727 | to benefit for the frame error and noise interrupts the USART mode should be |
lypinator | 0:bb348c97df44 | 728 | configured only for transmit "USART_MODE_TX" |
lypinator | 0:bb348c97df44 | 729 | The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error, |
lypinator | 0:bb348c97df44 | 730 | Noise error interrupt */ |
lypinator | 0:bb348c97df44 | 731 | |
lypinator | 0:bb348c97df44 | 732 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 733 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 734 | |
lypinator | 0:bb348c97df44 | 735 | /* Enable the USART Transmit Data Register Empty Interrupt */ |
lypinator | 0:bb348c97df44 | 736 | SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
lypinator | 0:bb348c97df44 | 737 | |
lypinator | 0:bb348c97df44 | 738 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 739 | } |
lypinator | 0:bb348c97df44 | 740 | else |
lypinator | 0:bb348c97df44 | 741 | { |
lypinator | 0:bb348c97df44 | 742 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 743 | } |
lypinator | 0:bb348c97df44 | 744 | } |
lypinator | 0:bb348c97df44 | 745 | |
lypinator | 0:bb348c97df44 | 746 | /** |
lypinator | 0:bb348c97df44 | 747 | * @brief Simplex Receive an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 748 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 749 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 750 | * @param pRxData Pointer to data buffer |
lypinator | 0:bb348c97df44 | 751 | * @param Size Amount of data to be received |
lypinator | 0:bb348c97df44 | 752 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 753 | */ |
lypinator | 0:bb348c97df44 | 754 | HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) |
lypinator | 0:bb348c97df44 | 755 | { |
lypinator | 0:bb348c97df44 | 756 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 757 | { |
lypinator | 0:bb348c97df44 | 758 | if((pRxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 759 | { |
lypinator | 0:bb348c97df44 | 760 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 761 | } |
lypinator | 0:bb348c97df44 | 762 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 763 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 764 | |
lypinator | 0:bb348c97df44 | 765 | husart->pRxBuffPtr = pRxData; |
lypinator | 0:bb348c97df44 | 766 | husart->RxXferSize = Size; |
lypinator | 0:bb348c97df44 | 767 | husart->RxXferCount = Size; |
lypinator | 0:bb348c97df44 | 768 | |
lypinator | 0:bb348c97df44 | 769 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 770 | husart->State = HAL_USART_STATE_BUSY_RX; |
lypinator | 0:bb348c97df44 | 771 | |
lypinator | 0:bb348c97df44 | 772 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 773 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 774 | |
lypinator | 0:bb348c97df44 | 775 | /* Enable the USART Parity Error and Data Register not empty Interrupts */ |
lypinator | 0:bb348c97df44 | 776 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE); |
lypinator | 0:bb348c97df44 | 777 | |
lypinator | 0:bb348c97df44 | 778 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 779 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 780 | |
lypinator | 0:bb348c97df44 | 781 | /* Send dummy byte in order to generate the clock for the slave to send data */ |
lypinator | 0:bb348c97df44 | 782 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 783 | |
lypinator | 0:bb348c97df44 | 784 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 785 | } |
lypinator | 0:bb348c97df44 | 786 | else |
lypinator | 0:bb348c97df44 | 787 | { |
lypinator | 0:bb348c97df44 | 788 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 789 | } |
lypinator | 0:bb348c97df44 | 790 | } |
lypinator | 0:bb348c97df44 | 791 | |
lypinator | 0:bb348c97df44 | 792 | /** |
lypinator | 0:bb348c97df44 | 793 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking). |
lypinator | 0:bb348c97df44 | 794 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 795 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 796 | * @param pTxData Pointer to data transmitted buffer |
lypinator | 0:bb348c97df44 | 797 | * @param pRxData Pointer to data received buffer |
lypinator | 0:bb348c97df44 | 798 | * @param Size Amount of data to be received |
lypinator | 0:bb348c97df44 | 799 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 800 | */ |
lypinator | 0:bb348c97df44 | 801 | HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
lypinator | 0:bb348c97df44 | 802 | { |
lypinator | 0:bb348c97df44 | 803 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 804 | { |
lypinator | 0:bb348c97df44 | 805 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 806 | { |
lypinator | 0:bb348c97df44 | 807 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 808 | } |
lypinator | 0:bb348c97df44 | 809 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 810 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 811 | |
lypinator | 0:bb348c97df44 | 812 | husart->pRxBuffPtr = pRxData; |
lypinator | 0:bb348c97df44 | 813 | husart->RxXferSize = Size; |
lypinator | 0:bb348c97df44 | 814 | husart->RxXferCount = Size; |
lypinator | 0:bb348c97df44 | 815 | husart->pTxBuffPtr = pTxData; |
lypinator | 0:bb348c97df44 | 816 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 817 | husart->TxXferCount = Size; |
lypinator | 0:bb348c97df44 | 818 | |
lypinator | 0:bb348c97df44 | 819 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 820 | husart->State = HAL_USART_STATE_BUSY_TX_RX; |
lypinator | 0:bb348c97df44 | 821 | |
lypinator | 0:bb348c97df44 | 822 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 823 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 824 | |
lypinator | 0:bb348c97df44 | 825 | /* Enable the USART Data Register not empty Interrupt */ |
lypinator | 0:bb348c97df44 | 826 | SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
lypinator | 0:bb348c97df44 | 827 | |
lypinator | 0:bb348c97df44 | 828 | /* Enable the USART Parity Error Interrupt */ |
lypinator | 0:bb348c97df44 | 829 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 830 | |
lypinator | 0:bb348c97df44 | 831 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 832 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 833 | |
lypinator | 0:bb348c97df44 | 834 | /* Enable the USART Transmit Data Register Empty Interrupt */ |
lypinator | 0:bb348c97df44 | 835 | SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
lypinator | 0:bb348c97df44 | 836 | |
lypinator | 0:bb348c97df44 | 837 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 838 | } |
lypinator | 0:bb348c97df44 | 839 | else |
lypinator | 0:bb348c97df44 | 840 | { |
lypinator | 0:bb348c97df44 | 841 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 842 | } |
lypinator | 0:bb348c97df44 | 843 | } |
lypinator | 0:bb348c97df44 | 844 | |
lypinator | 0:bb348c97df44 | 845 | /** |
lypinator | 0:bb348c97df44 | 846 | * @brief Simplex Send an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 847 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 848 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 849 | * @param pTxData Pointer to data buffer |
lypinator | 0:bb348c97df44 | 850 | * @param Size Amount of data to be sent |
lypinator | 0:bb348c97df44 | 851 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 852 | */ |
lypinator | 0:bb348c97df44 | 853 | HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size) |
lypinator | 0:bb348c97df44 | 854 | { |
lypinator | 0:bb348c97df44 | 855 | uint32_t *tmp; |
lypinator | 0:bb348c97df44 | 856 | |
lypinator | 0:bb348c97df44 | 857 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 858 | { |
lypinator | 0:bb348c97df44 | 859 | if((pTxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 860 | { |
lypinator | 0:bb348c97df44 | 861 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 862 | } |
lypinator | 0:bb348c97df44 | 863 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 864 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 865 | |
lypinator | 0:bb348c97df44 | 866 | husart->pTxBuffPtr = pTxData; |
lypinator | 0:bb348c97df44 | 867 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 868 | husart->TxXferCount = Size; |
lypinator | 0:bb348c97df44 | 869 | |
lypinator | 0:bb348c97df44 | 870 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 871 | husart->State = HAL_USART_STATE_BUSY_TX; |
lypinator | 0:bb348c97df44 | 872 | |
lypinator | 0:bb348c97df44 | 873 | /* Set the USART DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 874 | husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; |
lypinator | 0:bb348c97df44 | 875 | |
lypinator | 0:bb348c97df44 | 876 | /* Set the USART DMA Half transfer complete callback */ |
lypinator | 0:bb348c97df44 | 877 | husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; |
lypinator | 0:bb348c97df44 | 878 | |
lypinator | 0:bb348c97df44 | 879 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 880 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
lypinator | 0:bb348c97df44 | 881 | |
lypinator | 0:bb348c97df44 | 882 | /* Set the DMA abort callback */ |
lypinator | 0:bb348c97df44 | 883 | husart->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 884 | |
lypinator | 0:bb348c97df44 | 885 | /* Enable the USART transmit DMA Stream */ |
lypinator | 0:bb348c97df44 | 886 | tmp = (uint32_t*)&pTxData; |
lypinator | 0:bb348c97df44 | 887 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size); |
lypinator | 0:bb348c97df44 | 888 | |
lypinator | 0:bb348c97df44 | 889 | /* Clear the TC flag in the SR register by writing 0 to it */ |
lypinator | 0:bb348c97df44 | 890 | __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC); |
lypinator | 0:bb348c97df44 | 891 | |
lypinator | 0:bb348c97df44 | 892 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 893 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 894 | |
lypinator | 0:bb348c97df44 | 895 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
lypinator | 0:bb348c97df44 | 896 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 897 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 898 | |
lypinator | 0:bb348c97df44 | 899 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 900 | } |
lypinator | 0:bb348c97df44 | 901 | else |
lypinator | 0:bb348c97df44 | 902 | { |
lypinator | 0:bb348c97df44 | 903 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 904 | } |
lypinator | 0:bb348c97df44 | 905 | } |
lypinator | 0:bb348c97df44 | 906 | |
lypinator | 0:bb348c97df44 | 907 | /** |
lypinator | 0:bb348c97df44 | 908 | * @brief Full-Duplex Receive an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 909 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 910 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 911 | * @param pRxData Pointer to data buffer |
lypinator | 0:bb348c97df44 | 912 | * @param Size Amount of data to be received |
lypinator | 0:bb348c97df44 | 913 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 914 | * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave. |
lypinator | 0:bb348c97df44 | 915 | * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. |
lypinator | 0:bb348c97df44 | 916 | */ |
lypinator | 0:bb348c97df44 | 917 | HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) |
lypinator | 0:bb348c97df44 | 918 | { |
lypinator | 0:bb348c97df44 | 919 | uint32_t *tmp; |
lypinator | 0:bb348c97df44 | 920 | |
lypinator | 0:bb348c97df44 | 921 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 922 | { |
lypinator | 0:bb348c97df44 | 923 | if((pRxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 924 | { |
lypinator | 0:bb348c97df44 | 925 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 926 | } |
lypinator | 0:bb348c97df44 | 927 | |
lypinator | 0:bb348c97df44 | 928 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 929 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 930 | |
lypinator | 0:bb348c97df44 | 931 | husart->pRxBuffPtr = pRxData; |
lypinator | 0:bb348c97df44 | 932 | husart->RxXferSize = Size; |
lypinator | 0:bb348c97df44 | 933 | husart->pTxBuffPtr = pRxData; |
lypinator | 0:bb348c97df44 | 934 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 935 | |
lypinator | 0:bb348c97df44 | 936 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 937 | husart->State = HAL_USART_STATE_BUSY_RX; |
lypinator | 0:bb348c97df44 | 938 | |
lypinator | 0:bb348c97df44 | 939 | /* Set the USART DMA Rx transfer complete callback */ |
lypinator | 0:bb348c97df44 | 940 | husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; |
lypinator | 0:bb348c97df44 | 941 | |
lypinator | 0:bb348c97df44 | 942 | /* Set the USART DMA Half transfer complete callback */ |
lypinator | 0:bb348c97df44 | 943 | husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; |
lypinator | 0:bb348c97df44 | 944 | |
lypinator | 0:bb348c97df44 | 945 | /* Set the USART DMA Rx transfer error callback */ |
lypinator | 0:bb348c97df44 | 946 | husart->hdmarx->XferErrorCallback = USART_DMAError; |
lypinator | 0:bb348c97df44 | 947 | |
lypinator | 0:bb348c97df44 | 948 | /* Set the DMA abort callback */ |
lypinator | 0:bb348c97df44 | 949 | husart->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 950 | |
lypinator | 0:bb348c97df44 | 951 | /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing |
lypinator | 0:bb348c97df44 | 952 | is performed in DMA reception complete callback */ |
lypinator | 0:bb348c97df44 | 953 | husart->hdmatx->XferHalfCpltCallback = NULL; |
lypinator | 0:bb348c97df44 | 954 | husart->hdmatx->XferCpltCallback = NULL; |
lypinator | 0:bb348c97df44 | 955 | |
lypinator | 0:bb348c97df44 | 956 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 957 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
lypinator | 0:bb348c97df44 | 958 | |
lypinator | 0:bb348c97df44 | 959 | /* Set the DMA AbortCpltCallback */ |
lypinator | 0:bb348c97df44 | 960 | husart->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 961 | |
lypinator | 0:bb348c97df44 | 962 | /* Enable the USART receive DMA Stream */ |
lypinator | 0:bb348c97df44 | 963 | tmp = (uint32_t*)&pRxData; |
lypinator | 0:bb348c97df44 | 964 | HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size); |
lypinator | 0:bb348c97df44 | 965 | |
lypinator | 0:bb348c97df44 | 966 | /* Enable the USART transmit DMA Stream: the transmit stream is used in order |
lypinator | 0:bb348c97df44 | 967 | to generate in the non-blocking mode the clock to the slave device, |
lypinator | 0:bb348c97df44 | 968 | this mode isn't a simplex receive mode but a full-duplex receive one */ |
lypinator | 0:bb348c97df44 | 969 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size); |
lypinator | 0:bb348c97df44 | 970 | |
lypinator | 0:bb348c97df44 | 971 | /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */ |
lypinator | 0:bb348c97df44 | 972 | __HAL_USART_CLEAR_OREFLAG(husart); |
lypinator | 0:bb348c97df44 | 973 | |
lypinator | 0:bb348c97df44 | 974 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 975 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 976 | |
lypinator | 0:bb348c97df44 | 977 | /* Enable the USART Parity Error Interrupt */ |
lypinator | 0:bb348c97df44 | 978 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 979 | |
lypinator | 0:bb348c97df44 | 980 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 981 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 982 | |
lypinator | 0:bb348c97df44 | 983 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
lypinator | 0:bb348c97df44 | 984 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 985 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 986 | |
lypinator | 0:bb348c97df44 | 987 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
lypinator | 0:bb348c97df44 | 988 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 989 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 990 | |
lypinator | 0:bb348c97df44 | 991 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 992 | } |
lypinator | 0:bb348c97df44 | 993 | else |
lypinator | 0:bb348c97df44 | 994 | { |
lypinator | 0:bb348c97df44 | 995 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 996 | } |
lypinator | 0:bb348c97df44 | 997 | } |
lypinator | 0:bb348c97df44 | 998 | |
lypinator | 0:bb348c97df44 | 999 | /** |
lypinator | 0:bb348c97df44 | 1000 | * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 1001 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1002 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1003 | * @param pTxData Pointer to data transmitted buffer |
lypinator | 0:bb348c97df44 | 1004 | * @param pRxData Pointer to data received buffer |
lypinator | 0:bb348c97df44 | 1005 | * @param Size Amount of data to be received |
lypinator | 0:bb348c97df44 | 1006 | * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. |
lypinator | 0:bb348c97df44 | 1007 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1008 | */ |
lypinator | 0:bb348c97df44 | 1009 | HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
lypinator | 0:bb348c97df44 | 1010 | { |
lypinator | 0:bb348c97df44 | 1011 | uint32_t *tmp; |
lypinator | 0:bb348c97df44 | 1012 | |
lypinator | 0:bb348c97df44 | 1013 | if(husart->State == HAL_USART_STATE_READY) |
lypinator | 0:bb348c97df44 | 1014 | { |
lypinator | 0:bb348c97df44 | 1015 | if((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
lypinator | 0:bb348c97df44 | 1016 | { |
lypinator | 0:bb348c97df44 | 1017 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1018 | } |
lypinator | 0:bb348c97df44 | 1019 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1020 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 1021 | |
lypinator | 0:bb348c97df44 | 1022 | husart->pRxBuffPtr = pRxData; |
lypinator | 0:bb348c97df44 | 1023 | husart->RxXferSize = Size; |
lypinator | 0:bb348c97df44 | 1024 | husart->pTxBuffPtr = pTxData; |
lypinator | 0:bb348c97df44 | 1025 | husart->TxXferSize = Size; |
lypinator | 0:bb348c97df44 | 1026 | |
lypinator | 0:bb348c97df44 | 1027 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1028 | husart->State = HAL_USART_STATE_BUSY_TX_RX; |
lypinator | 0:bb348c97df44 | 1029 | |
lypinator | 0:bb348c97df44 | 1030 | /* Set the USART DMA Rx transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1031 | husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; |
lypinator | 0:bb348c97df44 | 1032 | |
lypinator | 0:bb348c97df44 | 1033 | /* Set the USART DMA Half transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1034 | husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; |
lypinator | 0:bb348c97df44 | 1035 | |
lypinator | 0:bb348c97df44 | 1036 | /* Set the USART DMA Tx transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1037 | husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; |
lypinator | 0:bb348c97df44 | 1038 | |
lypinator | 0:bb348c97df44 | 1039 | /* Set the USART DMA Half transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1040 | husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; |
lypinator | 0:bb348c97df44 | 1041 | |
lypinator | 0:bb348c97df44 | 1042 | /* Set the USART DMA Tx transfer error callback */ |
lypinator | 0:bb348c97df44 | 1043 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
lypinator | 0:bb348c97df44 | 1044 | |
lypinator | 0:bb348c97df44 | 1045 | /* Set the USART DMA Rx transfer error callback */ |
lypinator | 0:bb348c97df44 | 1046 | husart->hdmarx->XferErrorCallback = USART_DMAError; |
lypinator | 0:bb348c97df44 | 1047 | |
lypinator | 0:bb348c97df44 | 1048 | /* Set the DMA abort callback */ |
lypinator | 0:bb348c97df44 | 1049 | husart->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1050 | |
lypinator | 0:bb348c97df44 | 1051 | /* Enable the USART receive DMA Stream */ |
lypinator | 0:bb348c97df44 | 1052 | tmp = (uint32_t*)&pRxData; |
lypinator | 0:bb348c97df44 | 1053 | HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size); |
lypinator | 0:bb348c97df44 | 1054 | |
lypinator | 0:bb348c97df44 | 1055 | /* Enable the USART transmit DMA Stream */ |
lypinator | 0:bb348c97df44 | 1056 | tmp = (uint32_t*)&pTxData; |
lypinator | 0:bb348c97df44 | 1057 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size); |
lypinator | 0:bb348c97df44 | 1058 | |
lypinator | 0:bb348c97df44 | 1059 | /* Clear the TC flag in the SR register by writing 0 to it */ |
lypinator | 0:bb348c97df44 | 1060 | __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC); |
lypinator | 0:bb348c97df44 | 1061 | |
lypinator | 0:bb348c97df44 | 1062 | /* Clear the Overrun flag: mandatory for the second transfer in circular mode */ |
lypinator | 0:bb348c97df44 | 1063 | __HAL_USART_CLEAR_OREFLAG(husart); |
lypinator | 0:bb348c97df44 | 1064 | |
lypinator | 0:bb348c97df44 | 1065 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1066 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 1067 | |
lypinator | 0:bb348c97df44 | 1068 | /* Enable the USART Parity Error Interrupt */ |
lypinator | 0:bb348c97df44 | 1069 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 1070 | |
lypinator | 0:bb348c97df44 | 1071 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 1072 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 1073 | |
lypinator | 0:bb348c97df44 | 1074 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
lypinator | 0:bb348c97df44 | 1075 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 1076 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1077 | |
lypinator | 0:bb348c97df44 | 1078 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
lypinator | 0:bb348c97df44 | 1079 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 1080 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1081 | |
lypinator | 0:bb348c97df44 | 1082 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1083 | } |
lypinator | 0:bb348c97df44 | 1084 | else |
lypinator | 0:bb348c97df44 | 1085 | { |
lypinator | 0:bb348c97df44 | 1086 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 1087 | } |
lypinator | 0:bb348c97df44 | 1088 | } |
lypinator | 0:bb348c97df44 | 1089 | |
lypinator | 0:bb348c97df44 | 1090 | /** |
lypinator | 0:bb348c97df44 | 1091 | * @brief Pauses the DMA Transfer. |
lypinator | 0:bb348c97df44 | 1092 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1093 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1094 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1095 | */ |
lypinator | 0:bb348c97df44 | 1096 | HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1097 | { |
lypinator | 0:bb348c97df44 | 1098 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1099 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 1100 | |
lypinator | 0:bb348c97df44 | 1101 | /* Disable the USART DMA Tx request */ |
lypinator | 0:bb348c97df44 | 1102 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1103 | |
lypinator | 0:bb348c97df44 | 1104 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1105 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 1106 | |
lypinator | 0:bb348c97df44 | 1107 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1108 | } |
lypinator | 0:bb348c97df44 | 1109 | |
lypinator | 0:bb348c97df44 | 1110 | /** |
lypinator | 0:bb348c97df44 | 1111 | * @brief Resumes the DMA Transfer. |
lypinator | 0:bb348c97df44 | 1112 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1113 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1114 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1115 | */ |
lypinator | 0:bb348c97df44 | 1116 | HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1117 | { |
lypinator | 0:bb348c97df44 | 1118 | /* Process Locked */ |
lypinator | 0:bb348c97df44 | 1119 | __HAL_LOCK(husart); |
lypinator | 0:bb348c97df44 | 1120 | |
lypinator | 0:bb348c97df44 | 1121 | /* Enable the USART DMA Tx request */ |
lypinator | 0:bb348c97df44 | 1122 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1123 | |
lypinator | 0:bb348c97df44 | 1124 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1125 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 1126 | |
lypinator | 0:bb348c97df44 | 1127 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1128 | } |
lypinator | 0:bb348c97df44 | 1129 | |
lypinator | 0:bb348c97df44 | 1130 | /** |
lypinator | 0:bb348c97df44 | 1131 | * @brief Stops the DMA Transfer. |
lypinator | 0:bb348c97df44 | 1132 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1133 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1134 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1135 | */ |
lypinator | 0:bb348c97df44 | 1136 | HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1137 | { |
lypinator | 0:bb348c97df44 | 1138 | uint32_t dmarequest = 0x00U; |
lypinator | 0:bb348c97df44 | 1139 | /* The Lock is not implemented on this API to allow the user application |
lypinator | 0:bb348c97df44 | 1140 | to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback(): |
lypinator | 0:bb348c97df44 | 1141 | when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated |
lypinator | 0:bb348c97df44 | 1142 | and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() |
lypinator | 0:bb348c97df44 | 1143 | */ |
lypinator | 0:bb348c97df44 | 1144 | |
lypinator | 0:bb348c97df44 | 1145 | /* Stop USART DMA Tx request if ongoing */ |
lypinator | 0:bb348c97df44 | 1146 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1147 | if((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest) |
lypinator | 0:bb348c97df44 | 1148 | { |
lypinator | 0:bb348c97df44 | 1149 | USART_EndTxTransfer(husart); |
lypinator | 0:bb348c97df44 | 1150 | |
lypinator | 0:bb348c97df44 | 1151 | /* Abort the USART DMA Tx channel */ |
lypinator | 0:bb348c97df44 | 1152 | if(husart->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1153 | { |
lypinator | 0:bb348c97df44 | 1154 | HAL_DMA_Abort(husart->hdmatx); |
lypinator | 0:bb348c97df44 | 1155 | } |
lypinator | 0:bb348c97df44 | 1156 | |
lypinator | 0:bb348c97df44 | 1157 | /* Disable the USART Tx DMA request */ |
lypinator | 0:bb348c97df44 | 1158 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1159 | } |
lypinator | 0:bb348c97df44 | 1160 | |
lypinator | 0:bb348c97df44 | 1161 | /* Stop USART DMA Rx request if ongoing */ |
lypinator | 0:bb348c97df44 | 1162 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1163 | if((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest) |
lypinator | 0:bb348c97df44 | 1164 | { |
lypinator | 0:bb348c97df44 | 1165 | USART_EndRxTransfer(husart); |
lypinator | 0:bb348c97df44 | 1166 | |
lypinator | 0:bb348c97df44 | 1167 | /* Abort the USART DMA Rx channel */ |
lypinator | 0:bb348c97df44 | 1168 | if(husart->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1169 | { |
lypinator | 0:bb348c97df44 | 1170 | HAL_DMA_Abort(husart->hdmarx); |
lypinator | 0:bb348c97df44 | 1171 | } |
lypinator | 0:bb348c97df44 | 1172 | |
lypinator | 0:bb348c97df44 | 1173 | /* Disable the USART Rx DMA request */ |
lypinator | 0:bb348c97df44 | 1174 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1175 | } |
lypinator | 0:bb348c97df44 | 1176 | |
lypinator | 0:bb348c97df44 | 1177 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1178 | } |
lypinator | 0:bb348c97df44 | 1179 | |
lypinator | 0:bb348c97df44 | 1180 | /** |
lypinator | 0:bb348c97df44 | 1181 | * @brief Abort ongoing transfer (blocking mode). |
lypinator | 0:bb348c97df44 | 1182 | * @param husart USART handle. |
lypinator | 0:bb348c97df44 | 1183 | * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx, |
lypinator | 0:bb348c97df44 | 1184 | * as described by TransferType parameter) started in Interrupt or DMA mode. |
lypinator | 0:bb348c97df44 | 1185 | * This procedure performs following operations : |
lypinator | 0:bb348c97df44 | 1186 | * - Disable PPP Interrupts (depending of transfer direction) |
lypinator | 0:bb348c97df44 | 1187 | * - Disable the DMA transfer in the peripheral register (if enabled) |
lypinator | 0:bb348c97df44 | 1188 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
lypinator | 0:bb348c97df44 | 1189 | * - Set handle State to READY |
lypinator | 0:bb348c97df44 | 1190 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
lypinator | 0:bb348c97df44 | 1191 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1192 | */ |
lypinator | 0:bb348c97df44 | 1193 | HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1194 | { |
lypinator | 0:bb348c97df44 | 1195 | /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
lypinator | 0:bb348c97df44 | 1196 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); |
lypinator | 0:bb348c97df44 | 1197 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 1198 | |
lypinator | 0:bb348c97df44 | 1199 | /* Disable the USART DMA Tx request if enabled */ |
lypinator | 0:bb348c97df44 | 1200 | if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) |
lypinator | 0:bb348c97df44 | 1201 | { |
lypinator | 0:bb348c97df44 | 1202 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1203 | |
lypinator | 0:bb348c97df44 | 1204 | /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */ |
lypinator | 0:bb348c97df44 | 1205 | if(husart->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1206 | { |
lypinator | 0:bb348c97df44 | 1207 | /* Set the USART DMA Abort callback to Null. |
lypinator | 0:bb348c97df44 | 1208 | No call back execution at end of DMA abort procedure */ |
lypinator | 0:bb348c97df44 | 1209 | husart->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1210 | |
lypinator | 0:bb348c97df44 | 1211 | HAL_DMA_Abort(husart->hdmatx); |
lypinator | 0:bb348c97df44 | 1212 | } |
lypinator | 0:bb348c97df44 | 1213 | } |
lypinator | 0:bb348c97df44 | 1214 | |
lypinator | 0:bb348c97df44 | 1215 | /* Disable the USART DMA Rx request if enabled */ |
lypinator | 0:bb348c97df44 | 1216 | if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
lypinator | 0:bb348c97df44 | 1217 | { |
lypinator | 0:bb348c97df44 | 1218 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1219 | |
lypinator | 0:bb348c97df44 | 1220 | /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */ |
lypinator | 0:bb348c97df44 | 1221 | if(husart->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1222 | { |
lypinator | 0:bb348c97df44 | 1223 | /* Set the USART DMA Abort callback to Null. |
lypinator | 0:bb348c97df44 | 1224 | No call back execution at end of DMA abort procedure */ |
lypinator | 0:bb348c97df44 | 1225 | husart->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1226 | |
lypinator | 0:bb348c97df44 | 1227 | HAL_DMA_Abort(husart->hdmarx); |
lypinator | 0:bb348c97df44 | 1228 | } |
lypinator | 0:bb348c97df44 | 1229 | } |
lypinator | 0:bb348c97df44 | 1230 | |
lypinator | 0:bb348c97df44 | 1231 | /* Reset Tx and Rx transfer counters */ |
lypinator | 0:bb348c97df44 | 1232 | husart->TxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1233 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1234 | |
lypinator | 0:bb348c97df44 | 1235 | /* Restore husart->State to Ready */ |
lypinator | 0:bb348c97df44 | 1236 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1237 | |
lypinator | 0:bb348c97df44 | 1238 | /* Reset Handle ErrorCode to No Error */ |
lypinator | 0:bb348c97df44 | 1239 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1240 | |
lypinator | 0:bb348c97df44 | 1241 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1242 | } |
lypinator | 0:bb348c97df44 | 1243 | |
lypinator | 0:bb348c97df44 | 1244 | /** |
lypinator | 0:bb348c97df44 | 1245 | * @brief Abort ongoing transfer (Interrupt mode). |
lypinator | 0:bb348c97df44 | 1246 | * @param husart USART handle. |
lypinator | 0:bb348c97df44 | 1247 | * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx, |
lypinator | 0:bb348c97df44 | 1248 | * as described by TransferType parameter) started in Interrupt or DMA mode. |
lypinator | 0:bb348c97df44 | 1249 | * This procedure performs following operations : |
lypinator | 0:bb348c97df44 | 1250 | * - Disable PPP Interrupts (depending of transfer direction) |
lypinator | 0:bb348c97df44 | 1251 | * - Disable the DMA transfer in the peripheral register (if enabled) |
lypinator | 0:bb348c97df44 | 1252 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
lypinator | 0:bb348c97df44 | 1253 | * - Set handle State to READY |
lypinator | 0:bb348c97df44 | 1254 | * - At abort completion, call user abort complete callback |
lypinator | 0:bb348c97df44 | 1255 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
lypinator | 0:bb348c97df44 | 1256 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
lypinator | 0:bb348c97df44 | 1257 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1258 | */ |
lypinator | 0:bb348c97df44 | 1259 | HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1260 | { |
lypinator | 0:bb348c97df44 | 1261 | uint32_t AbortCplt = 0x01U; |
lypinator | 0:bb348c97df44 | 1262 | |
lypinator | 0:bb348c97df44 | 1263 | /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
lypinator | 0:bb348c97df44 | 1264 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); |
lypinator | 0:bb348c97df44 | 1265 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 1266 | |
lypinator | 0:bb348c97df44 | 1267 | /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised |
lypinator | 0:bb348c97df44 | 1268 | before any call to DMA Abort functions */ |
lypinator | 0:bb348c97df44 | 1269 | /* DMA Tx Handle is valid */ |
lypinator | 0:bb348c97df44 | 1270 | if(husart->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1271 | { |
lypinator | 0:bb348c97df44 | 1272 | /* Set DMA Abort Complete callback if USART DMA Tx request if enabled. |
lypinator | 0:bb348c97df44 | 1273 | Otherwise, set it to NULL */ |
lypinator | 0:bb348c97df44 | 1274 | if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) |
lypinator | 0:bb348c97df44 | 1275 | { |
lypinator | 0:bb348c97df44 | 1276 | husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback; |
lypinator | 0:bb348c97df44 | 1277 | } |
lypinator | 0:bb348c97df44 | 1278 | else |
lypinator | 0:bb348c97df44 | 1279 | { |
lypinator | 0:bb348c97df44 | 1280 | husart->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1281 | } |
lypinator | 0:bb348c97df44 | 1282 | } |
lypinator | 0:bb348c97df44 | 1283 | /* DMA Rx Handle is valid */ |
lypinator | 0:bb348c97df44 | 1284 | if(husart->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1285 | { |
lypinator | 0:bb348c97df44 | 1286 | /* Set DMA Abort Complete callback if USART DMA Rx request if enabled. |
lypinator | 0:bb348c97df44 | 1287 | Otherwise, set it to NULL */ |
lypinator | 0:bb348c97df44 | 1288 | if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
lypinator | 0:bb348c97df44 | 1289 | { |
lypinator | 0:bb348c97df44 | 1290 | husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback; |
lypinator | 0:bb348c97df44 | 1291 | } |
lypinator | 0:bb348c97df44 | 1292 | else |
lypinator | 0:bb348c97df44 | 1293 | { |
lypinator | 0:bb348c97df44 | 1294 | husart->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1295 | } |
lypinator | 0:bb348c97df44 | 1296 | } |
lypinator | 0:bb348c97df44 | 1297 | |
lypinator | 0:bb348c97df44 | 1298 | /* Disable the USART DMA Tx request if enabled */ |
lypinator | 0:bb348c97df44 | 1299 | if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) |
lypinator | 0:bb348c97df44 | 1300 | { |
lypinator | 0:bb348c97df44 | 1301 | /* Disable DMA Tx at USART level */ |
lypinator | 0:bb348c97df44 | 1302 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1303 | |
lypinator | 0:bb348c97df44 | 1304 | /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */ |
lypinator | 0:bb348c97df44 | 1305 | if(husart->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1306 | { |
lypinator | 0:bb348c97df44 | 1307 | /* USART Tx DMA Abort callback has already been initialised : |
lypinator | 0:bb348c97df44 | 1308 | will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */ |
lypinator | 0:bb348c97df44 | 1309 | |
lypinator | 0:bb348c97df44 | 1310 | /* Abort DMA TX */ |
lypinator | 0:bb348c97df44 | 1311 | if(HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1312 | { |
lypinator | 0:bb348c97df44 | 1313 | husart->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1314 | } |
lypinator | 0:bb348c97df44 | 1315 | else |
lypinator | 0:bb348c97df44 | 1316 | { |
lypinator | 0:bb348c97df44 | 1317 | AbortCplt = 0x00U; |
lypinator | 0:bb348c97df44 | 1318 | } |
lypinator | 0:bb348c97df44 | 1319 | } |
lypinator | 0:bb348c97df44 | 1320 | } |
lypinator | 0:bb348c97df44 | 1321 | |
lypinator | 0:bb348c97df44 | 1322 | /* Disable the USART DMA Rx request if enabled */ |
lypinator | 0:bb348c97df44 | 1323 | if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
lypinator | 0:bb348c97df44 | 1324 | { |
lypinator | 0:bb348c97df44 | 1325 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1326 | |
lypinator | 0:bb348c97df44 | 1327 | /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */ |
lypinator | 0:bb348c97df44 | 1328 | if(husart->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1329 | { |
lypinator | 0:bb348c97df44 | 1330 | /* USART Rx DMA Abort callback has already been initialised : |
lypinator | 0:bb348c97df44 | 1331 | will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */ |
lypinator | 0:bb348c97df44 | 1332 | |
lypinator | 0:bb348c97df44 | 1333 | /* Abort DMA RX */ |
lypinator | 0:bb348c97df44 | 1334 | if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1335 | { |
lypinator | 0:bb348c97df44 | 1336 | husart->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1337 | AbortCplt = 0x01U; |
lypinator | 0:bb348c97df44 | 1338 | } |
lypinator | 0:bb348c97df44 | 1339 | else |
lypinator | 0:bb348c97df44 | 1340 | { |
lypinator | 0:bb348c97df44 | 1341 | AbortCplt = 0x00U; |
lypinator | 0:bb348c97df44 | 1342 | } |
lypinator | 0:bb348c97df44 | 1343 | } |
lypinator | 0:bb348c97df44 | 1344 | } |
lypinator | 0:bb348c97df44 | 1345 | |
lypinator | 0:bb348c97df44 | 1346 | /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ |
lypinator | 0:bb348c97df44 | 1347 | if(AbortCplt == 0x01U) |
lypinator | 0:bb348c97df44 | 1348 | { |
lypinator | 0:bb348c97df44 | 1349 | /* Reset Tx and Rx transfer counters */ |
lypinator | 0:bb348c97df44 | 1350 | husart->TxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1351 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1352 | |
lypinator | 0:bb348c97df44 | 1353 | /* Reset errorCode */ |
lypinator | 0:bb348c97df44 | 1354 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1355 | |
lypinator | 0:bb348c97df44 | 1356 | /* Restore husart->State to Ready */ |
lypinator | 0:bb348c97df44 | 1357 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1358 | |
lypinator | 0:bb348c97df44 | 1359 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
lypinator | 0:bb348c97df44 | 1360 | HAL_USART_AbortCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1361 | } |
lypinator | 0:bb348c97df44 | 1362 | |
lypinator | 0:bb348c97df44 | 1363 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1364 | } |
lypinator | 0:bb348c97df44 | 1365 | |
lypinator | 0:bb348c97df44 | 1366 | /** |
lypinator | 0:bb348c97df44 | 1367 | * @brief This function handles USART interrupt request. |
lypinator | 0:bb348c97df44 | 1368 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1369 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1370 | * @retval None |
lypinator | 0:bb348c97df44 | 1371 | */ |
lypinator | 0:bb348c97df44 | 1372 | void HAL_USART_IRQHandler(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1373 | { |
lypinator | 0:bb348c97df44 | 1374 | uint32_t isrflags = READ_REG(husart->Instance->SR); |
lypinator | 0:bb348c97df44 | 1375 | uint32_t cr1its = READ_REG(husart->Instance->CR1); |
lypinator | 0:bb348c97df44 | 1376 | uint32_t cr3its = READ_REG(husart->Instance->CR3); |
lypinator | 0:bb348c97df44 | 1377 | uint32_t errorflags = 0x00U; |
lypinator | 0:bb348c97df44 | 1378 | uint32_t dmarequest = 0x00U; |
lypinator | 0:bb348c97df44 | 1379 | |
lypinator | 0:bb348c97df44 | 1380 | /* If no error occurs */ |
lypinator | 0:bb348c97df44 | 1381 | errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE)); |
lypinator | 0:bb348c97df44 | 1382 | if(errorflags == RESET) |
lypinator | 0:bb348c97df44 | 1383 | { |
lypinator | 0:bb348c97df44 | 1384 | /* USART in mode Receiver -------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 1385 | if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1386 | { |
lypinator | 0:bb348c97df44 | 1387 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
lypinator | 0:bb348c97df44 | 1388 | { |
lypinator | 0:bb348c97df44 | 1389 | USART_Receive_IT(husart); |
lypinator | 0:bb348c97df44 | 1390 | } |
lypinator | 0:bb348c97df44 | 1391 | else |
lypinator | 0:bb348c97df44 | 1392 | { |
lypinator | 0:bb348c97df44 | 1393 | USART_TransmitReceive_IT(husart); |
lypinator | 0:bb348c97df44 | 1394 | } |
lypinator | 0:bb348c97df44 | 1395 | return; |
lypinator | 0:bb348c97df44 | 1396 | } |
lypinator | 0:bb348c97df44 | 1397 | } |
lypinator | 0:bb348c97df44 | 1398 | /* If some errors occur */ |
lypinator | 0:bb348c97df44 | 1399 | if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET))) |
lypinator | 0:bb348c97df44 | 1400 | { |
lypinator | 0:bb348c97df44 | 1401 | /* USART parity error interrupt occurred ----------------------------------*/ |
lypinator | 0:bb348c97df44 | 1402 | if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1403 | { |
lypinator | 0:bb348c97df44 | 1404 | husart->ErrorCode |= HAL_USART_ERROR_PE; |
lypinator | 0:bb348c97df44 | 1405 | } |
lypinator | 0:bb348c97df44 | 1406 | |
lypinator | 0:bb348c97df44 | 1407 | /* USART noise error interrupt occurred --------------------------------*/ |
lypinator | 0:bb348c97df44 | 1408 | if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1409 | { |
lypinator | 0:bb348c97df44 | 1410 | husart->ErrorCode |= HAL_USART_ERROR_NE; |
lypinator | 0:bb348c97df44 | 1411 | } |
lypinator | 0:bb348c97df44 | 1412 | |
lypinator | 0:bb348c97df44 | 1413 | /* USART frame error interrupt occurred --------------------------------*/ |
lypinator | 0:bb348c97df44 | 1414 | if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1415 | { |
lypinator | 0:bb348c97df44 | 1416 | husart->ErrorCode |= HAL_USART_ERROR_FE; |
lypinator | 0:bb348c97df44 | 1417 | } |
lypinator | 0:bb348c97df44 | 1418 | |
lypinator | 0:bb348c97df44 | 1419 | /* USART Over-Run interrupt occurred -----------------------------------*/ |
lypinator | 0:bb348c97df44 | 1420 | if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1421 | { |
lypinator | 0:bb348c97df44 | 1422 | husart->ErrorCode |= HAL_USART_ERROR_ORE; |
lypinator | 0:bb348c97df44 | 1423 | } |
lypinator | 0:bb348c97df44 | 1424 | |
lypinator | 0:bb348c97df44 | 1425 | if(husart->ErrorCode != HAL_USART_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1426 | { |
lypinator | 0:bb348c97df44 | 1427 | /* USART in mode Receiver -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 1428 | if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1429 | { |
lypinator | 0:bb348c97df44 | 1430 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
lypinator | 0:bb348c97df44 | 1431 | { |
lypinator | 0:bb348c97df44 | 1432 | USART_Receive_IT(husart); |
lypinator | 0:bb348c97df44 | 1433 | } |
lypinator | 0:bb348c97df44 | 1434 | else |
lypinator | 0:bb348c97df44 | 1435 | { |
lypinator | 0:bb348c97df44 | 1436 | USART_TransmitReceive_IT(husart); |
lypinator | 0:bb348c97df44 | 1437 | } |
lypinator | 0:bb348c97df44 | 1438 | } |
lypinator | 0:bb348c97df44 | 1439 | /* If Overrun error occurs, or if any error occurs in DMA mode reception, |
lypinator | 0:bb348c97df44 | 1440 | consider error as blocking */ |
lypinator | 0:bb348c97df44 | 1441 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1442 | if(((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest) |
lypinator | 0:bb348c97df44 | 1443 | { |
lypinator | 0:bb348c97df44 | 1444 | /* Set the USART state ready to be able to start again the process, |
lypinator | 0:bb348c97df44 | 1445 | Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ |
lypinator | 0:bb348c97df44 | 1446 | USART_EndRxTransfer(husart); |
lypinator | 0:bb348c97df44 | 1447 | |
lypinator | 0:bb348c97df44 | 1448 | /* Disable the USART DMA Rx request if enabled */ |
lypinator | 0:bb348c97df44 | 1449 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
lypinator | 0:bb348c97df44 | 1450 | { |
lypinator | 0:bb348c97df44 | 1451 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1452 | |
lypinator | 0:bb348c97df44 | 1453 | /* Abort the USART DMA Rx channel */ |
lypinator | 0:bb348c97df44 | 1454 | if(husart->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1455 | { |
lypinator | 0:bb348c97df44 | 1456 | /* Set the USART DMA Abort callback : |
lypinator | 0:bb348c97df44 | 1457 | will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */ |
lypinator | 0:bb348c97df44 | 1458 | husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError; |
lypinator | 0:bb348c97df44 | 1459 | |
lypinator | 0:bb348c97df44 | 1460 | if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1461 | { |
lypinator | 0:bb348c97df44 | 1462 | /* Call Directly XferAbortCallback function in case of error */ |
lypinator | 0:bb348c97df44 | 1463 | husart->hdmarx->XferAbortCallback(husart->hdmarx); |
lypinator | 0:bb348c97df44 | 1464 | } |
lypinator | 0:bb348c97df44 | 1465 | } |
lypinator | 0:bb348c97df44 | 1466 | else |
lypinator | 0:bb348c97df44 | 1467 | { |
lypinator | 0:bb348c97df44 | 1468 | /* Call user error callback */ |
lypinator | 0:bb348c97df44 | 1469 | HAL_USART_ErrorCallback(husart); |
lypinator | 0:bb348c97df44 | 1470 | } |
lypinator | 0:bb348c97df44 | 1471 | } |
lypinator | 0:bb348c97df44 | 1472 | else |
lypinator | 0:bb348c97df44 | 1473 | { |
lypinator | 0:bb348c97df44 | 1474 | /* Call user error callback */ |
lypinator | 0:bb348c97df44 | 1475 | HAL_USART_ErrorCallback(husart); |
lypinator | 0:bb348c97df44 | 1476 | } |
lypinator | 0:bb348c97df44 | 1477 | } |
lypinator | 0:bb348c97df44 | 1478 | else |
lypinator | 0:bb348c97df44 | 1479 | { |
lypinator | 0:bb348c97df44 | 1480 | /* Call user error callback */ |
lypinator | 0:bb348c97df44 | 1481 | HAL_USART_ErrorCallback(husart); |
lypinator | 0:bb348c97df44 | 1482 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1483 | } |
lypinator | 0:bb348c97df44 | 1484 | } |
lypinator | 0:bb348c97df44 | 1485 | return; |
lypinator | 0:bb348c97df44 | 1486 | } |
lypinator | 0:bb348c97df44 | 1487 | |
lypinator | 0:bb348c97df44 | 1488 | /* USART in mode Transmitter -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 1489 | if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1490 | { |
lypinator | 0:bb348c97df44 | 1491 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
lypinator | 0:bb348c97df44 | 1492 | { |
lypinator | 0:bb348c97df44 | 1493 | USART_Transmit_IT(husart); |
lypinator | 0:bb348c97df44 | 1494 | } |
lypinator | 0:bb348c97df44 | 1495 | else |
lypinator | 0:bb348c97df44 | 1496 | { |
lypinator | 0:bb348c97df44 | 1497 | USART_TransmitReceive_IT(husart); |
lypinator | 0:bb348c97df44 | 1498 | } |
lypinator | 0:bb348c97df44 | 1499 | return; |
lypinator | 0:bb348c97df44 | 1500 | } |
lypinator | 0:bb348c97df44 | 1501 | |
lypinator | 0:bb348c97df44 | 1502 | /* USART in mode Transmitter (transmission end) ----------------------------*/ |
lypinator | 0:bb348c97df44 | 1503 | if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) |
lypinator | 0:bb348c97df44 | 1504 | { |
lypinator | 0:bb348c97df44 | 1505 | USART_EndTransmit_IT(husart); |
lypinator | 0:bb348c97df44 | 1506 | return; |
lypinator | 0:bb348c97df44 | 1507 | } |
lypinator | 0:bb348c97df44 | 1508 | } |
lypinator | 0:bb348c97df44 | 1509 | |
lypinator | 0:bb348c97df44 | 1510 | /** |
lypinator | 0:bb348c97df44 | 1511 | * @brief Tx Transfer completed callbacks. |
lypinator | 0:bb348c97df44 | 1512 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1513 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1514 | * @retval None |
lypinator | 0:bb348c97df44 | 1515 | */ |
lypinator | 0:bb348c97df44 | 1516 | __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1517 | { |
lypinator | 0:bb348c97df44 | 1518 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1519 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1520 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1521 | the HAL_USART_TxCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 1522 | */ |
lypinator | 0:bb348c97df44 | 1523 | } |
lypinator | 0:bb348c97df44 | 1524 | |
lypinator | 0:bb348c97df44 | 1525 | /** |
lypinator | 0:bb348c97df44 | 1526 | * @brief Tx Half Transfer completed callbacks. |
lypinator | 0:bb348c97df44 | 1527 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1528 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1529 | * @retval None |
lypinator | 0:bb348c97df44 | 1530 | */ |
lypinator | 0:bb348c97df44 | 1531 | __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1532 | { |
lypinator | 0:bb348c97df44 | 1533 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1534 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1535 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1536 | the HAL_USART_TxCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 1537 | */ |
lypinator | 0:bb348c97df44 | 1538 | } |
lypinator | 0:bb348c97df44 | 1539 | |
lypinator | 0:bb348c97df44 | 1540 | /** |
lypinator | 0:bb348c97df44 | 1541 | * @brief Rx Transfer completed callbacks. |
lypinator | 0:bb348c97df44 | 1542 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1543 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1544 | * @retval None |
lypinator | 0:bb348c97df44 | 1545 | */ |
lypinator | 0:bb348c97df44 | 1546 | __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1547 | { |
lypinator | 0:bb348c97df44 | 1548 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1549 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1550 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1551 | the HAL_USART_TxCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 1552 | */ |
lypinator | 0:bb348c97df44 | 1553 | } |
lypinator | 0:bb348c97df44 | 1554 | |
lypinator | 0:bb348c97df44 | 1555 | /** |
lypinator | 0:bb348c97df44 | 1556 | * @brief Rx Half Transfer completed callbacks. |
lypinator | 0:bb348c97df44 | 1557 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1558 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1559 | * @retval None |
lypinator | 0:bb348c97df44 | 1560 | */ |
lypinator | 0:bb348c97df44 | 1561 | __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1562 | { |
lypinator | 0:bb348c97df44 | 1563 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1564 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1565 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1566 | the HAL_USART_TxCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 1567 | */ |
lypinator | 0:bb348c97df44 | 1568 | } |
lypinator | 0:bb348c97df44 | 1569 | |
lypinator | 0:bb348c97df44 | 1570 | /** |
lypinator | 0:bb348c97df44 | 1571 | * @brief Tx/Rx Transfers completed callback for the non-blocking process. |
lypinator | 0:bb348c97df44 | 1572 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1573 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1574 | * @retval None |
lypinator | 0:bb348c97df44 | 1575 | */ |
lypinator | 0:bb348c97df44 | 1576 | __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1577 | { |
lypinator | 0:bb348c97df44 | 1578 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1579 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1580 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1581 | the HAL_USART_TxCpltCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 1582 | */ |
lypinator | 0:bb348c97df44 | 1583 | } |
lypinator | 0:bb348c97df44 | 1584 | |
lypinator | 0:bb348c97df44 | 1585 | /** |
lypinator | 0:bb348c97df44 | 1586 | * @brief USART error callbacks. |
lypinator | 0:bb348c97df44 | 1587 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1588 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1589 | * @retval None |
lypinator | 0:bb348c97df44 | 1590 | */ |
lypinator | 0:bb348c97df44 | 1591 | __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1592 | { |
lypinator | 0:bb348c97df44 | 1593 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1594 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1595 | /* NOTE: This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1596 | the HAL_USART_ErrorCallback could be implemented in the user file |
lypinator | 0:bb348c97df44 | 1597 | */ |
lypinator | 0:bb348c97df44 | 1598 | } |
lypinator | 0:bb348c97df44 | 1599 | |
lypinator | 0:bb348c97df44 | 1600 | /** |
lypinator | 0:bb348c97df44 | 1601 | * @brief USART Abort Complete callback. |
lypinator | 0:bb348c97df44 | 1602 | * @param husart USART handle. |
lypinator | 0:bb348c97df44 | 1603 | * @retval None |
lypinator | 0:bb348c97df44 | 1604 | */ |
lypinator | 0:bb348c97df44 | 1605 | __weak void HAL_USART_AbortCpltCallback (USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1606 | { |
lypinator | 0:bb348c97df44 | 1607 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1608 | UNUSED(husart); |
lypinator | 0:bb348c97df44 | 1609 | |
lypinator | 0:bb348c97df44 | 1610 | /* NOTE : This function should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1611 | the HAL_USART_AbortCpltCallback can be implemented in the user file. |
lypinator | 0:bb348c97df44 | 1612 | */ |
lypinator | 0:bb348c97df44 | 1613 | } |
lypinator | 0:bb348c97df44 | 1614 | |
lypinator | 0:bb348c97df44 | 1615 | /** |
lypinator | 0:bb348c97df44 | 1616 | * @} |
lypinator | 0:bb348c97df44 | 1617 | */ |
lypinator | 0:bb348c97df44 | 1618 | |
lypinator | 0:bb348c97df44 | 1619 | /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions |
lypinator | 0:bb348c97df44 | 1620 | * @brief USART State and Errors functions |
lypinator | 0:bb348c97df44 | 1621 | * |
lypinator | 0:bb348c97df44 | 1622 | @verbatim |
lypinator | 0:bb348c97df44 | 1623 | ============================================================================== |
lypinator | 0:bb348c97df44 | 1624 | ##### Peripheral State and Errors functions ##### |
lypinator | 0:bb348c97df44 | 1625 | ============================================================================== |
lypinator | 0:bb348c97df44 | 1626 | [..] |
lypinator | 0:bb348c97df44 | 1627 | This subsection provides a set of functions allowing to return the State of |
lypinator | 0:bb348c97df44 | 1628 | USART communication |
lypinator | 0:bb348c97df44 | 1629 | process, return Peripheral Errors occurred during communication process |
lypinator | 0:bb348c97df44 | 1630 | (+) HAL_USART_GetState() API can be helpful to check in run-time the state |
lypinator | 0:bb348c97df44 | 1631 | of the USART peripheral. |
lypinator | 0:bb348c97df44 | 1632 | (+) HAL_USART_GetError() check in run-time errors that could be occurred during |
lypinator | 0:bb348c97df44 | 1633 | communication. |
lypinator | 0:bb348c97df44 | 1634 | @endverbatim |
lypinator | 0:bb348c97df44 | 1635 | * @{ |
lypinator | 0:bb348c97df44 | 1636 | */ |
lypinator | 0:bb348c97df44 | 1637 | |
lypinator | 0:bb348c97df44 | 1638 | /** |
lypinator | 0:bb348c97df44 | 1639 | * @brief Returns the USART state. |
lypinator | 0:bb348c97df44 | 1640 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1641 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1642 | * @retval HAL state |
lypinator | 0:bb348c97df44 | 1643 | */ |
lypinator | 0:bb348c97df44 | 1644 | HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1645 | { |
lypinator | 0:bb348c97df44 | 1646 | return husart->State; |
lypinator | 0:bb348c97df44 | 1647 | } |
lypinator | 0:bb348c97df44 | 1648 | |
lypinator | 0:bb348c97df44 | 1649 | /** |
lypinator | 0:bb348c97df44 | 1650 | * @brief Return the USART error code |
lypinator | 0:bb348c97df44 | 1651 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1652 | * the configuration information for the specified USART. |
lypinator | 0:bb348c97df44 | 1653 | * @retval USART Error Code |
lypinator | 0:bb348c97df44 | 1654 | */ |
lypinator | 0:bb348c97df44 | 1655 | uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1656 | { |
lypinator | 0:bb348c97df44 | 1657 | return husart->ErrorCode; |
lypinator | 0:bb348c97df44 | 1658 | } |
lypinator | 0:bb348c97df44 | 1659 | |
lypinator | 0:bb348c97df44 | 1660 | /** |
lypinator | 0:bb348c97df44 | 1661 | * @} |
lypinator | 0:bb348c97df44 | 1662 | */ |
lypinator | 0:bb348c97df44 | 1663 | |
lypinator | 0:bb348c97df44 | 1664 | /** |
lypinator | 0:bb348c97df44 | 1665 | * @brief DMA USART transmit process complete callback. |
lypinator | 0:bb348c97df44 | 1666 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 1667 | * @retval None |
lypinator | 0:bb348c97df44 | 1668 | */ |
lypinator | 0:bb348c97df44 | 1669 | static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1670 | { |
lypinator | 0:bb348c97df44 | 1671 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1672 | /* DMA Normal mode */ |
lypinator | 0:bb348c97df44 | 1673 | if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U) |
lypinator | 0:bb348c97df44 | 1674 | { |
lypinator | 0:bb348c97df44 | 1675 | husart->TxXferCount = 0U; |
lypinator | 0:bb348c97df44 | 1676 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
lypinator | 0:bb348c97df44 | 1677 | { |
lypinator | 0:bb348c97df44 | 1678 | /* Disable the DMA transfer for transmit request by resetting the DMAT bit |
lypinator | 0:bb348c97df44 | 1679 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 1680 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1681 | |
lypinator | 0:bb348c97df44 | 1682 | /* Enable the USART Transmit Complete Interrupt */ |
lypinator | 0:bb348c97df44 | 1683 | SET_BIT(husart->Instance->CR1, USART_CR1_TCIE); |
lypinator | 0:bb348c97df44 | 1684 | } |
lypinator | 0:bb348c97df44 | 1685 | } |
lypinator | 0:bb348c97df44 | 1686 | /* DMA Circular mode */ |
lypinator | 0:bb348c97df44 | 1687 | else |
lypinator | 0:bb348c97df44 | 1688 | { |
lypinator | 0:bb348c97df44 | 1689 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
lypinator | 0:bb348c97df44 | 1690 | { |
lypinator | 0:bb348c97df44 | 1691 | HAL_USART_TxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1692 | } |
lypinator | 0:bb348c97df44 | 1693 | } |
lypinator | 0:bb348c97df44 | 1694 | } |
lypinator | 0:bb348c97df44 | 1695 | |
lypinator | 0:bb348c97df44 | 1696 | /** |
lypinator | 0:bb348c97df44 | 1697 | * @brief DMA USART transmit process half complete callback |
lypinator | 0:bb348c97df44 | 1698 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1699 | * the configuration information for the specified DMA module. |
lypinator | 0:bb348c97df44 | 1700 | * @retval None |
lypinator | 0:bb348c97df44 | 1701 | */ |
lypinator | 0:bb348c97df44 | 1702 | static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1703 | { |
lypinator | 0:bb348c97df44 | 1704 | USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1705 | |
lypinator | 0:bb348c97df44 | 1706 | HAL_USART_TxHalfCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1707 | } |
lypinator | 0:bb348c97df44 | 1708 | |
lypinator | 0:bb348c97df44 | 1709 | /** |
lypinator | 0:bb348c97df44 | 1710 | * @brief DMA USART receive process complete callback. |
lypinator | 0:bb348c97df44 | 1711 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 1712 | * @retval None |
lypinator | 0:bb348c97df44 | 1713 | */ |
lypinator | 0:bb348c97df44 | 1714 | static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1715 | { |
lypinator | 0:bb348c97df44 | 1716 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1717 | /* DMA Normal mode */ |
lypinator | 0:bb348c97df44 | 1718 | if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U) |
lypinator | 0:bb348c97df44 | 1719 | { |
lypinator | 0:bb348c97df44 | 1720 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1721 | |
lypinator | 0:bb348c97df44 | 1722 | /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
lypinator | 0:bb348c97df44 | 1723 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 1724 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 1725 | |
lypinator | 0:bb348c97df44 | 1726 | /* Disable the DMA transfer for the Transmit/receiver request by clearing the DMAT/DMAR bit |
lypinator | 0:bb348c97df44 | 1727 | in the USART CR3 register */ |
lypinator | 0:bb348c97df44 | 1728 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1729 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1730 | |
lypinator | 0:bb348c97df44 | 1731 | husart->State= HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1732 | |
lypinator | 0:bb348c97df44 | 1733 | /* The USART state is HAL_USART_STATE_BUSY_RX */ |
lypinator | 0:bb348c97df44 | 1734 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
lypinator | 0:bb348c97df44 | 1735 | { |
lypinator | 0:bb348c97df44 | 1736 | HAL_USART_RxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1737 | } |
lypinator | 0:bb348c97df44 | 1738 | /* The USART state is HAL_USART_STATE_BUSY_TX_RX */ |
lypinator | 0:bb348c97df44 | 1739 | else |
lypinator | 0:bb348c97df44 | 1740 | { |
lypinator | 0:bb348c97df44 | 1741 | HAL_USART_TxRxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1742 | } |
lypinator | 0:bb348c97df44 | 1743 | } |
lypinator | 0:bb348c97df44 | 1744 | /* DMA circular mode */ |
lypinator | 0:bb348c97df44 | 1745 | else |
lypinator | 0:bb348c97df44 | 1746 | { |
lypinator | 0:bb348c97df44 | 1747 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
lypinator | 0:bb348c97df44 | 1748 | { |
lypinator | 0:bb348c97df44 | 1749 | HAL_USART_RxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1750 | } |
lypinator | 0:bb348c97df44 | 1751 | /* The USART state is HAL_USART_STATE_BUSY_TX_RX */ |
lypinator | 0:bb348c97df44 | 1752 | else |
lypinator | 0:bb348c97df44 | 1753 | { |
lypinator | 0:bb348c97df44 | 1754 | HAL_USART_TxRxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1755 | } |
lypinator | 0:bb348c97df44 | 1756 | } |
lypinator | 0:bb348c97df44 | 1757 | } |
lypinator | 0:bb348c97df44 | 1758 | |
lypinator | 0:bb348c97df44 | 1759 | /** |
lypinator | 0:bb348c97df44 | 1760 | * @brief DMA USART receive process half complete callback |
lypinator | 0:bb348c97df44 | 1761 | * @param hdma pointer to a DMA_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1762 | * the configuration information for the specified DMA module. |
lypinator | 0:bb348c97df44 | 1763 | * @retval None |
lypinator | 0:bb348c97df44 | 1764 | */ |
lypinator | 0:bb348c97df44 | 1765 | static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1766 | { |
lypinator | 0:bb348c97df44 | 1767 | USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1768 | |
lypinator | 0:bb348c97df44 | 1769 | HAL_USART_RxHalfCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1770 | } |
lypinator | 0:bb348c97df44 | 1771 | |
lypinator | 0:bb348c97df44 | 1772 | /** |
lypinator | 0:bb348c97df44 | 1773 | * @brief DMA USART communication error callback. |
lypinator | 0:bb348c97df44 | 1774 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 1775 | * @retval None |
lypinator | 0:bb348c97df44 | 1776 | */ |
lypinator | 0:bb348c97df44 | 1777 | static void USART_DMAError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1778 | { |
lypinator | 0:bb348c97df44 | 1779 | uint32_t dmarequest = 0x00U; |
lypinator | 0:bb348c97df44 | 1780 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1781 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1782 | husart->TxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1783 | |
lypinator | 0:bb348c97df44 | 1784 | /* Stop USART DMA Tx request if ongoing */ |
lypinator | 0:bb348c97df44 | 1785 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT); |
lypinator | 0:bb348c97df44 | 1786 | if((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest) |
lypinator | 0:bb348c97df44 | 1787 | { |
lypinator | 0:bb348c97df44 | 1788 | USART_EndTxTransfer(husart); |
lypinator | 0:bb348c97df44 | 1789 | } |
lypinator | 0:bb348c97df44 | 1790 | |
lypinator | 0:bb348c97df44 | 1791 | /* Stop USART DMA Rx request if ongoing */ |
lypinator | 0:bb348c97df44 | 1792 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR); |
lypinator | 0:bb348c97df44 | 1793 | if((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest) |
lypinator | 0:bb348c97df44 | 1794 | { |
lypinator | 0:bb348c97df44 | 1795 | USART_EndRxTransfer(husart); |
lypinator | 0:bb348c97df44 | 1796 | } |
lypinator | 0:bb348c97df44 | 1797 | |
lypinator | 0:bb348c97df44 | 1798 | husart->ErrorCode |= HAL_USART_ERROR_DMA; |
lypinator | 0:bb348c97df44 | 1799 | husart->State= HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1800 | |
lypinator | 0:bb348c97df44 | 1801 | HAL_USART_ErrorCallback(husart); |
lypinator | 0:bb348c97df44 | 1802 | } |
lypinator | 0:bb348c97df44 | 1803 | |
lypinator | 0:bb348c97df44 | 1804 | /** |
lypinator | 0:bb348c97df44 | 1805 | * @brief This function handles USART Communication Timeout. |
lypinator | 0:bb348c97df44 | 1806 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1807 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1808 | * @param Flag specifies the USART flag to check. |
lypinator | 0:bb348c97df44 | 1809 | * @param Status The new Flag status (SET or RESET). |
lypinator | 0:bb348c97df44 | 1810 | * @param Tickstart Tick start value. |
lypinator | 0:bb348c97df44 | 1811 | * @param Timeout Timeout duration. |
lypinator | 0:bb348c97df44 | 1812 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1813 | */ |
lypinator | 0:bb348c97df44 | 1814 | static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 1815 | { |
lypinator | 0:bb348c97df44 | 1816 | /* Wait until flag is set */ |
lypinator | 0:bb348c97df44 | 1817 | while((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status) |
lypinator | 0:bb348c97df44 | 1818 | { |
lypinator | 0:bb348c97df44 | 1819 | /* Check for the Timeout */ |
lypinator | 0:bb348c97df44 | 1820 | if(Timeout != HAL_MAX_DELAY) |
lypinator | 0:bb348c97df44 | 1821 | { |
lypinator | 0:bb348c97df44 | 1822 | if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout)) |
lypinator | 0:bb348c97df44 | 1823 | { |
lypinator | 0:bb348c97df44 | 1824 | /* Disable the USART Transmit Complete Interrupt */ |
lypinator | 0:bb348c97df44 | 1825 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
lypinator | 0:bb348c97df44 | 1826 | |
lypinator | 0:bb348c97df44 | 1827 | /* Disable the USART RXNE Interrupt */ |
lypinator | 0:bb348c97df44 | 1828 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
lypinator | 0:bb348c97df44 | 1829 | |
lypinator | 0:bb348c97df44 | 1830 | /* Disable the USART Parity Error Interrupt */ |
lypinator | 0:bb348c97df44 | 1831 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 1832 | |
lypinator | 0:bb348c97df44 | 1833 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 1834 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 1835 | |
lypinator | 0:bb348c97df44 | 1836 | husart->State= HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1837 | |
lypinator | 0:bb348c97df44 | 1838 | /* Process Unlocked */ |
lypinator | 0:bb348c97df44 | 1839 | __HAL_UNLOCK(husart); |
lypinator | 0:bb348c97df44 | 1840 | |
lypinator | 0:bb348c97df44 | 1841 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1842 | } |
lypinator | 0:bb348c97df44 | 1843 | } |
lypinator | 0:bb348c97df44 | 1844 | } |
lypinator | 0:bb348c97df44 | 1845 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1846 | } |
lypinator | 0:bb348c97df44 | 1847 | |
lypinator | 0:bb348c97df44 | 1848 | /** |
lypinator | 0:bb348c97df44 | 1849 | * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion). |
lypinator | 0:bb348c97df44 | 1850 | * @param husart USART handle. |
lypinator | 0:bb348c97df44 | 1851 | * @retval None |
lypinator | 0:bb348c97df44 | 1852 | */ |
lypinator | 0:bb348c97df44 | 1853 | static void USART_EndTxTransfer(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1854 | { |
lypinator | 0:bb348c97df44 | 1855 | /* Disable TXEIE and TCIE interrupts */ |
lypinator | 0:bb348c97df44 | 1856 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); |
lypinator | 0:bb348c97df44 | 1857 | |
lypinator | 0:bb348c97df44 | 1858 | /* At end of Tx process, restore husart->State to Ready */ |
lypinator | 0:bb348c97df44 | 1859 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1860 | } |
lypinator | 0:bb348c97df44 | 1861 | |
lypinator | 0:bb348c97df44 | 1862 | /** |
lypinator | 0:bb348c97df44 | 1863 | * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion). |
lypinator | 0:bb348c97df44 | 1864 | * @param husart USART handle. |
lypinator | 0:bb348c97df44 | 1865 | * @retval None |
lypinator | 0:bb348c97df44 | 1866 | */ |
lypinator | 0:bb348c97df44 | 1867 | static void USART_EndRxTransfer(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1868 | { |
lypinator | 0:bb348c97df44 | 1869 | /* Disable RXNE, PE and ERR interrupts */ |
lypinator | 0:bb348c97df44 | 1870 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); |
lypinator | 0:bb348c97df44 | 1871 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 1872 | |
lypinator | 0:bb348c97df44 | 1873 | /* At end of Rx process, restore husart->State to Ready */ |
lypinator | 0:bb348c97df44 | 1874 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1875 | } |
lypinator | 0:bb348c97df44 | 1876 | |
lypinator | 0:bb348c97df44 | 1877 | /** |
lypinator | 0:bb348c97df44 | 1878 | * @brief DMA USART communication abort callback, when initiated by HAL services on Error |
lypinator | 0:bb348c97df44 | 1879 | * (To be called at end of DMA Abort procedure following error occurrence). |
lypinator | 0:bb348c97df44 | 1880 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 1881 | * @retval None |
lypinator | 0:bb348c97df44 | 1882 | */ |
lypinator | 0:bb348c97df44 | 1883 | static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1884 | { |
lypinator | 0:bb348c97df44 | 1885 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1886 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1887 | husart->TxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1888 | |
lypinator | 0:bb348c97df44 | 1889 | HAL_USART_ErrorCallback(husart); |
lypinator | 0:bb348c97df44 | 1890 | } |
lypinator | 0:bb348c97df44 | 1891 | |
lypinator | 0:bb348c97df44 | 1892 | /** |
lypinator | 0:bb348c97df44 | 1893 | * @brief DMA USART Tx communication abort callback, when initiated by user |
lypinator | 0:bb348c97df44 | 1894 | * (To be called at end of DMA Tx Abort procedure following user abort request). |
lypinator | 0:bb348c97df44 | 1895 | * @note When this callback is executed, User Abort complete call back is called only if no |
lypinator | 0:bb348c97df44 | 1896 | * Abort still ongoing for Rx DMA Handle. |
lypinator | 0:bb348c97df44 | 1897 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 1898 | * @retval None |
lypinator | 0:bb348c97df44 | 1899 | */ |
lypinator | 0:bb348c97df44 | 1900 | static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1901 | { |
lypinator | 0:bb348c97df44 | 1902 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1903 | |
lypinator | 0:bb348c97df44 | 1904 | husart->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1905 | |
lypinator | 0:bb348c97df44 | 1906 | /* Check if an Abort process is still ongoing */ |
lypinator | 0:bb348c97df44 | 1907 | if(husart->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1908 | { |
lypinator | 0:bb348c97df44 | 1909 | if(husart->hdmarx->XferAbortCallback != NULL) |
lypinator | 0:bb348c97df44 | 1910 | { |
lypinator | 0:bb348c97df44 | 1911 | return; |
lypinator | 0:bb348c97df44 | 1912 | } |
lypinator | 0:bb348c97df44 | 1913 | } |
lypinator | 0:bb348c97df44 | 1914 | |
lypinator | 0:bb348c97df44 | 1915 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
lypinator | 0:bb348c97df44 | 1916 | husart->TxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1917 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1918 | |
lypinator | 0:bb348c97df44 | 1919 | /* Reset errorCode */ |
lypinator | 0:bb348c97df44 | 1920 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1921 | |
lypinator | 0:bb348c97df44 | 1922 | /* Restore husart->State to Ready */ |
lypinator | 0:bb348c97df44 | 1923 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1924 | |
lypinator | 0:bb348c97df44 | 1925 | /* Call user Abort complete callback */ |
lypinator | 0:bb348c97df44 | 1926 | HAL_USART_AbortCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1927 | } |
lypinator | 0:bb348c97df44 | 1928 | |
lypinator | 0:bb348c97df44 | 1929 | /** |
lypinator | 0:bb348c97df44 | 1930 | * @brief DMA USART Rx communication abort callback, when initiated by user |
lypinator | 0:bb348c97df44 | 1931 | * (To be called at end of DMA Rx Abort procedure following user abort request). |
lypinator | 0:bb348c97df44 | 1932 | * @note When this callback is executed, User Abort complete call back is called only if no |
lypinator | 0:bb348c97df44 | 1933 | * Abort still ongoing for Tx DMA Handle. |
lypinator | 0:bb348c97df44 | 1934 | * @param hdma DMA handle. |
lypinator | 0:bb348c97df44 | 1935 | * @retval None |
lypinator | 0:bb348c97df44 | 1936 | */ |
lypinator | 0:bb348c97df44 | 1937 | static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 1938 | { |
lypinator | 0:bb348c97df44 | 1939 | USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
lypinator | 0:bb348c97df44 | 1940 | |
lypinator | 0:bb348c97df44 | 1941 | husart->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1942 | |
lypinator | 0:bb348c97df44 | 1943 | /* Check if an Abort process is still ongoing */ |
lypinator | 0:bb348c97df44 | 1944 | if(husart->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1945 | { |
lypinator | 0:bb348c97df44 | 1946 | if(husart->hdmatx->XferAbortCallback != NULL) |
lypinator | 0:bb348c97df44 | 1947 | { |
lypinator | 0:bb348c97df44 | 1948 | return; |
lypinator | 0:bb348c97df44 | 1949 | } |
lypinator | 0:bb348c97df44 | 1950 | } |
lypinator | 0:bb348c97df44 | 1951 | |
lypinator | 0:bb348c97df44 | 1952 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
lypinator | 0:bb348c97df44 | 1953 | husart->TxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1954 | husart->RxXferCount = 0x00U; |
lypinator | 0:bb348c97df44 | 1955 | |
lypinator | 0:bb348c97df44 | 1956 | /* Reset errorCode */ |
lypinator | 0:bb348c97df44 | 1957 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1958 | |
lypinator | 0:bb348c97df44 | 1959 | /* Restore husart->State to Ready */ |
lypinator | 0:bb348c97df44 | 1960 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 1961 | |
lypinator | 0:bb348c97df44 | 1962 | /* Call user Abort complete callback */ |
lypinator | 0:bb348c97df44 | 1963 | HAL_USART_AbortCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 1964 | } |
lypinator | 0:bb348c97df44 | 1965 | |
lypinator | 0:bb348c97df44 | 1966 | /** |
lypinator | 0:bb348c97df44 | 1967 | * @brief Simplex Send an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 1968 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1969 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 1970 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1971 | * @note The USART errors are not managed to avoid the overrun error. |
lypinator | 0:bb348c97df44 | 1972 | */ |
lypinator | 0:bb348c97df44 | 1973 | static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 1974 | { |
lypinator | 0:bb348c97df44 | 1975 | uint16_t* tmp; |
lypinator | 0:bb348c97df44 | 1976 | |
lypinator | 0:bb348c97df44 | 1977 | if(husart->State == HAL_USART_STATE_BUSY_TX) |
lypinator | 0:bb348c97df44 | 1978 | { |
lypinator | 0:bb348c97df44 | 1979 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 1980 | { |
lypinator | 0:bb348c97df44 | 1981 | tmp = (uint16_t*) husart->pTxBuffPtr; |
lypinator | 0:bb348c97df44 | 1982 | husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 1983 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 1984 | { |
lypinator | 0:bb348c97df44 | 1985 | husart->pTxBuffPtr += 2U; |
lypinator | 0:bb348c97df44 | 1986 | } |
lypinator | 0:bb348c97df44 | 1987 | else |
lypinator | 0:bb348c97df44 | 1988 | { |
lypinator | 0:bb348c97df44 | 1989 | husart->pTxBuffPtr += 1U; |
lypinator | 0:bb348c97df44 | 1990 | } |
lypinator | 0:bb348c97df44 | 1991 | } |
lypinator | 0:bb348c97df44 | 1992 | else |
lypinator | 0:bb348c97df44 | 1993 | { |
lypinator | 0:bb348c97df44 | 1994 | husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 1995 | } |
lypinator | 0:bb348c97df44 | 1996 | |
lypinator | 0:bb348c97df44 | 1997 | if(--husart->TxXferCount == 0U) |
lypinator | 0:bb348c97df44 | 1998 | { |
lypinator | 0:bb348c97df44 | 1999 | /* Disable the USART Transmit data register empty Interrupt */ |
lypinator | 0:bb348c97df44 | 2000 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
lypinator | 0:bb348c97df44 | 2001 | |
lypinator | 0:bb348c97df44 | 2002 | /* Enable the USART Transmit Complete Interrupt */ |
lypinator | 0:bb348c97df44 | 2003 | SET_BIT(husart->Instance->CR1, USART_CR1_TCIE); |
lypinator | 0:bb348c97df44 | 2004 | } |
lypinator | 0:bb348c97df44 | 2005 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2006 | } |
lypinator | 0:bb348c97df44 | 2007 | else |
lypinator | 0:bb348c97df44 | 2008 | { |
lypinator | 0:bb348c97df44 | 2009 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 2010 | } |
lypinator | 0:bb348c97df44 | 2011 | } |
lypinator | 0:bb348c97df44 | 2012 | |
lypinator | 0:bb348c97df44 | 2013 | /** |
lypinator | 0:bb348c97df44 | 2014 | * @brief Wraps up transmission in non blocking mode. |
lypinator | 0:bb348c97df44 | 2015 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2016 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 2017 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2018 | */ |
lypinator | 0:bb348c97df44 | 2019 | static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 2020 | { |
lypinator | 0:bb348c97df44 | 2021 | /* Disable the USART Transmit Complete Interrupt */ |
lypinator | 0:bb348c97df44 | 2022 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE); |
lypinator | 0:bb348c97df44 | 2023 | |
lypinator | 0:bb348c97df44 | 2024 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 2025 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 2026 | |
lypinator | 0:bb348c97df44 | 2027 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 2028 | |
lypinator | 0:bb348c97df44 | 2029 | HAL_USART_TxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 2030 | |
lypinator | 0:bb348c97df44 | 2031 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2032 | } |
lypinator | 0:bb348c97df44 | 2033 | |
lypinator | 0:bb348c97df44 | 2034 | /** |
lypinator | 0:bb348c97df44 | 2035 | * @brief Simplex Receive an amount of data in non-blocking mode. |
lypinator | 0:bb348c97df44 | 2036 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2037 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 2038 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2039 | */ |
lypinator | 0:bb348c97df44 | 2040 | static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 2041 | { |
lypinator | 0:bb348c97df44 | 2042 | uint16_t* tmp; |
lypinator | 0:bb348c97df44 | 2043 | if(husart->State == HAL_USART_STATE_BUSY_RX) |
lypinator | 0:bb348c97df44 | 2044 | { |
lypinator | 0:bb348c97df44 | 2045 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 2046 | { |
lypinator | 0:bb348c97df44 | 2047 | tmp = (uint16_t*) husart->pRxBuffPtr; |
lypinator | 0:bb348c97df44 | 2048 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 2049 | { |
lypinator | 0:bb348c97df44 | 2050 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 2051 | husart->pRxBuffPtr += 2U; |
lypinator | 0:bb348c97df44 | 2052 | } |
lypinator | 0:bb348c97df44 | 2053 | else |
lypinator | 0:bb348c97df44 | 2054 | { |
lypinator | 0:bb348c97df44 | 2055 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
lypinator | 0:bb348c97df44 | 2056 | husart->pRxBuffPtr += 1U; |
lypinator | 0:bb348c97df44 | 2057 | } |
lypinator | 0:bb348c97df44 | 2058 | if(--husart->RxXferCount != 0x00U) |
lypinator | 0:bb348c97df44 | 2059 | { |
lypinator | 0:bb348c97df44 | 2060 | /* Send dummy byte in order to generate the clock for the slave to send the next data */ |
lypinator | 0:bb348c97df44 | 2061 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 2062 | } |
lypinator | 0:bb348c97df44 | 2063 | } |
lypinator | 0:bb348c97df44 | 2064 | else |
lypinator | 0:bb348c97df44 | 2065 | { |
lypinator | 0:bb348c97df44 | 2066 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 2067 | { |
lypinator | 0:bb348c97df44 | 2068 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 2069 | } |
lypinator | 0:bb348c97df44 | 2070 | else |
lypinator | 0:bb348c97df44 | 2071 | { |
lypinator | 0:bb348c97df44 | 2072 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
lypinator | 0:bb348c97df44 | 2073 | } |
lypinator | 0:bb348c97df44 | 2074 | |
lypinator | 0:bb348c97df44 | 2075 | if(--husart->RxXferCount != 0x00U) |
lypinator | 0:bb348c97df44 | 2076 | { |
lypinator | 0:bb348c97df44 | 2077 | /* Send dummy byte in order to generate the clock for the slave to send the next data */ |
lypinator | 0:bb348c97df44 | 2078 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF); |
lypinator | 0:bb348c97df44 | 2079 | } |
lypinator | 0:bb348c97df44 | 2080 | } |
lypinator | 0:bb348c97df44 | 2081 | |
lypinator | 0:bb348c97df44 | 2082 | if(husart->RxXferCount == 0U) |
lypinator | 0:bb348c97df44 | 2083 | { |
lypinator | 0:bb348c97df44 | 2084 | /* Disable the USART RXNE Interrupt */ |
lypinator | 0:bb348c97df44 | 2085 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
lypinator | 0:bb348c97df44 | 2086 | |
lypinator | 0:bb348c97df44 | 2087 | /* Disable the USART Parity Error Interrupt */ |
lypinator | 0:bb348c97df44 | 2088 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 2089 | |
lypinator | 0:bb348c97df44 | 2090 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 2091 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 2092 | |
lypinator | 0:bb348c97df44 | 2093 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 2094 | HAL_USART_RxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 2095 | |
lypinator | 0:bb348c97df44 | 2096 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2097 | } |
lypinator | 0:bb348c97df44 | 2098 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2099 | } |
lypinator | 0:bb348c97df44 | 2100 | else |
lypinator | 0:bb348c97df44 | 2101 | { |
lypinator | 0:bb348c97df44 | 2102 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 2103 | } |
lypinator | 0:bb348c97df44 | 2104 | } |
lypinator | 0:bb348c97df44 | 2105 | |
lypinator | 0:bb348c97df44 | 2106 | /** |
lypinator | 0:bb348c97df44 | 2107 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking). |
lypinator | 0:bb348c97df44 | 2108 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2109 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 2110 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2111 | */ |
lypinator | 0:bb348c97df44 | 2112 | static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 2113 | { |
lypinator | 0:bb348c97df44 | 2114 | uint16_t* tmp; |
lypinator | 0:bb348c97df44 | 2115 | |
lypinator | 0:bb348c97df44 | 2116 | if(husart->State == HAL_USART_STATE_BUSY_TX_RX) |
lypinator | 0:bb348c97df44 | 2117 | { |
lypinator | 0:bb348c97df44 | 2118 | if(husart->TxXferCount != 0x00U) |
lypinator | 0:bb348c97df44 | 2119 | { |
lypinator | 0:bb348c97df44 | 2120 | if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET) |
lypinator | 0:bb348c97df44 | 2121 | { |
lypinator | 0:bb348c97df44 | 2122 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 2123 | { |
lypinator | 0:bb348c97df44 | 2124 | tmp = (uint16_t*) husart->pTxBuffPtr; |
lypinator | 0:bb348c97df44 | 2125 | husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 2126 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 2127 | { |
lypinator | 0:bb348c97df44 | 2128 | husart->pTxBuffPtr += 2U; |
lypinator | 0:bb348c97df44 | 2129 | } |
lypinator | 0:bb348c97df44 | 2130 | else |
lypinator | 0:bb348c97df44 | 2131 | { |
lypinator | 0:bb348c97df44 | 2132 | husart->pTxBuffPtr += 1U; |
lypinator | 0:bb348c97df44 | 2133 | } |
lypinator | 0:bb348c97df44 | 2134 | } |
lypinator | 0:bb348c97df44 | 2135 | else |
lypinator | 0:bb348c97df44 | 2136 | { |
lypinator | 0:bb348c97df44 | 2137 | husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 2138 | } |
lypinator | 0:bb348c97df44 | 2139 | husart->TxXferCount--; |
lypinator | 0:bb348c97df44 | 2140 | |
lypinator | 0:bb348c97df44 | 2141 | /* Check the latest data transmitted */ |
lypinator | 0:bb348c97df44 | 2142 | if(husart->TxXferCount == 0U) |
lypinator | 0:bb348c97df44 | 2143 | { |
lypinator | 0:bb348c97df44 | 2144 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
lypinator | 0:bb348c97df44 | 2145 | } |
lypinator | 0:bb348c97df44 | 2146 | } |
lypinator | 0:bb348c97df44 | 2147 | } |
lypinator | 0:bb348c97df44 | 2148 | |
lypinator | 0:bb348c97df44 | 2149 | if(husart->RxXferCount != 0x00U) |
lypinator | 0:bb348c97df44 | 2150 | { |
lypinator | 0:bb348c97df44 | 2151 | if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET) |
lypinator | 0:bb348c97df44 | 2152 | { |
lypinator | 0:bb348c97df44 | 2153 | if(husart->Init.WordLength == USART_WORDLENGTH_9B) |
lypinator | 0:bb348c97df44 | 2154 | { |
lypinator | 0:bb348c97df44 | 2155 | tmp = (uint16_t*) husart->pRxBuffPtr; |
lypinator | 0:bb348c97df44 | 2156 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 2157 | { |
lypinator | 0:bb348c97df44 | 2158 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
lypinator | 0:bb348c97df44 | 2159 | husart->pRxBuffPtr += 2U; |
lypinator | 0:bb348c97df44 | 2160 | } |
lypinator | 0:bb348c97df44 | 2161 | else |
lypinator | 0:bb348c97df44 | 2162 | { |
lypinator | 0:bb348c97df44 | 2163 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
lypinator | 0:bb348c97df44 | 2164 | husart->pRxBuffPtr += 1U; |
lypinator | 0:bb348c97df44 | 2165 | } |
lypinator | 0:bb348c97df44 | 2166 | } |
lypinator | 0:bb348c97df44 | 2167 | else |
lypinator | 0:bb348c97df44 | 2168 | { |
lypinator | 0:bb348c97df44 | 2169 | if(husart->Init.Parity == USART_PARITY_NONE) |
lypinator | 0:bb348c97df44 | 2170 | { |
lypinator | 0:bb348c97df44 | 2171 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
lypinator | 0:bb348c97df44 | 2172 | } |
lypinator | 0:bb348c97df44 | 2173 | else |
lypinator | 0:bb348c97df44 | 2174 | { |
lypinator | 0:bb348c97df44 | 2175 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
lypinator | 0:bb348c97df44 | 2176 | } |
lypinator | 0:bb348c97df44 | 2177 | } |
lypinator | 0:bb348c97df44 | 2178 | husart->RxXferCount--; |
lypinator | 0:bb348c97df44 | 2179 | } |
lypinator | 0:bb348c97df44 | 2180 | } |
lypinator | 0:bb348c97df44 | 2181 | |
lypinator | 0:bb348c97df44 | 2182 | /* Check the latest data received */ |
lypinator | 0:bb348c97df44 | 2183 | if(husart->RxXferCount == 0U) |
lypinator | 0:bb348c97df44 | 2184 | { |
lypinator | 0:bb348c97df44 | 2185 | /* Disable the USART RXNE Interrupt */ |
lypinator | 0:bb348c97df44 | 2186 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
lypinator | 0:bb348c97df44 | 2187 | |
lypinator | 0:bb348c97df44 | 2188 | /* Disable the USART Parity Error Interrupt */ |
lypinator | 0:bb348c97df44 | 2189 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
lypinator | 0:bb348c97df44 | 2190 | |
lypinator | 0:bb348c97df44 | 2191 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
lypinator | 0:bb348c97df44 | 2192 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
lypinator | 0:bb348c97df44 | 2193 | |
lypinator | 0:bb348c97df44 | 2194 | husart->State = HAL_USART_STATE_READY; |
lypinator | 0:bb348c97df44 | 2195 | |
lypinator | 0:bb348c97df44 | 2196 | HAL_USART_TxRxCpltCallback(husart); |
lypinator | 0:bb348c97df44 | 2197 | |
lypinator | 0:bb348c97df44 | 2198 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2199 | } |
lypinator | 0:bb348c97df44 | 2200 | |
lypinator | 0:bb348c97df44 | 2201 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2202 | } |
lypinator | 0:bb348c97df44 | 2203 | else |
lypinator | 0:bb348c97df44 | 2204 | { |
lypinator | 0:bb348c97df44 | 2205 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 2206 | } |
lypinator | 0:bb348c97df44 | 2207 | } |
lypinator | 0:bb348c97df44 | 2208 | |
lypinator | 0:bb348c97df44 | 2209 | /** |
lypinator | 0:bb348c97df44 | 2210 | * @brief Configures the USART pferipheral. |
lypinator | 0:bb348c97df44 | 2211 | * @param husart pointer to a USART_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2212 | * the configuration information for the specified USART module. |
lypinator | 0:bb348c97df44 | 2213 | * @retval None |
lypinator | 0:bb348c97df44 | 2214 | */ |
lypinator | 0:bb348c97df44 | 2215 | static void USART_SetConfig(USART_HandleTypeDef *husart) |
lypinator | 0:bb348c97df44 | 2216 | { |
lypinator | 0:bb348c97df44 | 2217 | uint32_t tmpreg = 0x00U; |
lypinator | 0:bb348c97df44 | 2218 | |
lypinator | 0:bb348c97df44 | 2219 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 2220 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
lypinator | 0:bb348c97df44 | 2221 | assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity)); |
lypinator | 0:bb348c97df44 | 2222 | assert_param(IS_USART_PHASE(husart->Init.CLKPhase)); |
lypinator | 0:bb348c97df44 | 2223 | assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit)); |
lypinator | 0:bb348c97df44 | 2224 | assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate)); |
lypinator | 0:bb348c97df44 | 2225 | assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength)); |
lypinator | 0:bb348c97df44 | 2226 | assert_param(IS_USART_STOPBITS(husart->Init.StopBits)); |
lypinator | 0:bb348c97df44 | 2227 | assert_param(IS_USART_PARITY(husart->Init.Parity)); |
lypinator | 0:bb348c97df44 | 2228 | assert_param(IS_USART_MODE(husart->Init.Mode)); |
lypinator | 0:bb348c97df44 | 2229 | |
lypinator | 0:bb348c97df44 | 2230 | /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the |
lypinator | 0:bb348c97df44 | 2231 | receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */ |
lypinator | 0:bb348c97df44 | 2232 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE)); |
lypinator | 0:bb348c97df44 | 2233 | |
lypinator | 0:bb348c97df44 | 2234 | /*---------------------------- USART CR2 Configuration ---------------------*/ |
lypinator | 0:bb348c97df44 | 2235 | tmpreg = husart->Instance->CR2; |
lypinator | 0:bb348c97df44 | 2236 | /* Clear CLKEN, CPOL, CPHA and LBCL bits */ |
lypinator | 0:bb348c97df44 | 2237 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)); |
lypinator | 0:bb348c97df44 | 2238 | /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/ |
lypinator | 0:bb348c97df44 | 2239 | /* Set CPOL bit according to husart->Init.CLKPolarity value */ |
lypinator | 0:bb348c97df44 | 2240 | /* Set CPHA bit according to husart->Init.CLKPhase value */ |
lypinator | 0:bb348c97df44 | 2241 | /* Set LBCL bit according to husart->Init.CLKLastBit value */ |
lypinator | 0:bb348c97df44 | 2242 | /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */ |
lypinator | 0:bb348c97df44 | 2243 | tmpreg |= (uint32_t)(USART_CLOCK_ENABLE| husart->Init.CLKPolarity | |
lypinator | 0:bb348c97df44 | 2244 | husart->Init.CLKPhase| husart->Init.CLKLastBit | husart->Init.StopBits); |
lypinator | 0:bb348c97df44 | 2245 | /* Write to USART CR2 */ |
lypinator | 0:bb348c97df44 | 2246 | WRITE_REG(husart->Instance->CR2, (uint32_t)tmpreg); |
lypinator | 0:bb348c97df44 | 2247 | |
lypinator | 0:bb348c97df44 | 2248 | /*-------------------------- USART CR1 Configuration -----------------------*/ |
lypinator | 0:bb348c97df44 | 2249 | tmpreg = husart->Instance->CR1; |
lypinator | 0:bb348c97df44 | 2250 | |
lypinator | 0:bb348c97df44 | 2251 | /* Clear M, PCE, PS, TE, RE and OVER8 bits */ |
lypinator | 0:bb348c97df44 | 2252 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ |
lypinator | 0:bb348c97df44 | 2253 | USART_CR1_RE | USART_CR1_OVER8)); |
lypinator | 0:bb348c97df44 | 2254 | |
lypinator | 0:bb348c97df44 | 2255 | /* Configure the USART Word Length, Parity and mode: |
lypinator | 0:bb348c97df44 | 2256 | Set the M bits according to husart->Init.WordLength value |
lypinator | 0:bb348c97df44 | 2257 | Set PCE and PS bits according to husart->Init.Parity value |
lypinator | 0:bb348c97df44 | 2258 | Set TE and RE bits according to husart->Init.Mode value |
lypinator | 0:bb348c97df44 | 2259 | Force OVER8 bit to 1 in order to reach the max USART frequencies */ |
lypinator | 0:bb348c97df44 | 2260 | tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8; |
lypinator | 0:bb348c97df44 | 2261 | |
lypinator | 0:bb348c97df44 | 2262 | /* Write to USART CR1 */ |
lypinator | 0:bb348c97df44 | 2263 | WRITE_REG(husart->Instance->CR1, (uint32_t)tmpreg); |
lypinator | 0:bb348c97df44 | 2264 | |
lypinator | 0:bb348c97df44 | 2265 | /*-------------------------- USART CR3 Configuration -----------------------*/ |
lypinator | 0:bb348c97df44 | 2266 | /* Clear CTSE and RTSE bits */ |
lypinator | 0:bb348c97df44 | 2267 | CLEAR_BIT(husart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE)); |
lypinator | 0:bb348c97df44 | 2268 | |
lypinator | 0:bb348c97df44 | 2269 | /*-------------------------- USART BRR Configuration -----------------------*/ |
lypinator | 0:bb348c97df44 | 2270 | #if defined(USART6) |
lypinator | 0:bb348c97df44 | 2271 | if((husart->Instance == USART1) || (husart->Instance == USART6)) |
lypinator | 0:bb348c97df44 | 2272 | { |
lypinator | 0:bb348c97df44 | 2273 | husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate); |
lypinator | 0:bb348c97df44 | 2274 | } |
lypinator | 0:bb348c97df44 | 2275 | #else |
lypinator | 0:bb348c97df44 | 2276 | if(husart->Instance == USART1) |
lypinator | 0:bb348c97df44 | 2277 | { |
lypinator | 0:bb348c97df44 | 2278 | husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate); |
lypinator | 0:bb348c97df44 | 2279 | } |
lypinator | 0:bb348c97df44 | 2280 | #endif /* USART6 */ |
lypinator | 0:bb348c97df44 | 2281 | else |
lypinator | 0:bb348c97df44 | 2282 | { |
lypinator | 0:bb348c97df44 | 2283 | husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate); |
lypinator | 0:bb348c97df44 | 2284 | } |
lypinator | 0:bb348c97df44 | 2285 | } |
lypinator | 0:bb348c97df44 | 2286 | |
lypinator | 0:bb348c97df44 | 2287 | /** |
lypinator | 0:bb348c97df44 | 2288 | * @} |
lypinator | 0:bb348c97df44 | 2289 | */ |
lypinator | 0:bb348c97df44 | 2290 | |
lypinator | 0:bb348c97df44 | 2291 | #endif /* HAL_USART_MODULE_ENABLED */ |
lypinator | 0:bb348c97df44 | 2292 | /** |
lypinator | 0:bb348c97df44 | 2293 | * @} |
lypinator | 0:bb348c97df44 | 2294 | */ |
lypinator | 0:bb348c97df44 | 2295 | |
lypinator | 0:bb348c97df44 | 2296 | /** |
lypinator | 0:bb348c97df44 | 2297 | * @} |
lypinator | 0:bb348c97df44 | 2298 | */ |
lypinator | 0:bb348c97df44 | 2299 | |
lypinator | 0:bb348c97df44 | 2300 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |