Fahad Mirza
/
Nucleo_HXC900
A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.
Driver/hw_usart.c@39:cb0e5a76ab15, 2019-01-24 (annotated)
- Committer:
- fahadmirza
- Date:
- Thu Jan 24 23:45:12 2019 +0000
- Revision:
- 39:cb0e5a76ab15
- Parent:
- 5:53302861bfea
Doxygen style header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fahadmirza | 0:a0c5877bd360 | 1 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 2 | * File : hw_usart.c |
fahadmirza | 0:a0c5877bd360 | 3 | * Author : Fahad Mirza (Haxiot) |
fahadmirza | 0:a0c5877bd360 | 4 | * Date : 08-January-2018 |
fahadmirza | 0:a0c5877bd360 | 5 | * Brief : Configuration of the USART instances. |
fahadmirza | 0:a0c5877bd360 | 6 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 7 | * @attention |
fahadmirza | 0:a0c5877bd360 | 8 | * |
fahadmirza | 0:a0c5877bd360 | 9 | * <h2><center>© Copyright (c) 2017 Haxiot. All rights reserved.</center></h2> |
fahadmirza | 0:a0c5877bd360 | 10 | * |
fahadmirza | 0:a0c5877bd360 | 11 | * Redistribution and use in source and binary forms, with or without |
fahadmirza | 0:a0c5877bd360 | 12 | * modification, are permitted, provided that the following conditions are met: |
fahadmirza | 0:a0c5877bd360 | 13 | * |
fahadmirza | 0:a0c5877bd360 | 14 | * 1. Redistribution of source code must retain the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 15 | * this list of conditions and the following disclaimer. |
fahadmirza | 0:a0c5877bd360 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fahadmirza | 0:a0c5877bd360 | 17 | * this list of conditions and the following disclaimer in the documentation |
fahadmirza | 0:a0c5877bd360 | 18 | * and/or other materials provided with the distribution. |
fahadmirza | 0:a0c5877bd360 | 19 | * 3. Neither the name of STMicroelectronics nor the names of other |
fahadmirza | 0:a0c5877bd360 | 20 | * contributors to this software may be used to endorse or promote products |
fahadmirza | 0:a0c5877bd360 | 21 | * derived from this software without specific written permission. |
fahadmirza | 0:a0c5877bd360 | 22 | * 4. Redistribution and use of this software other than as permitted under |
fahadmirza | 0:a0c5877bd360 | 23 | * this license is void and will automatically terminate your rights under |
fahadmirza | 0:a0c5877bd360 | 24 | * this license. |
fahadmirza | 0:a0c5877bd360 | 25 | * |
fahadmirza | 0:a0c5877bd360 | 26 | * THIS SOFTWARE IS PROVIDED BY HAXIOT AND CONTRIBUTORS "AS IS" |
fahadmirza | 0:a0c5877bd360 | 27 | * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
fahadmirza | 0:a0c5877bd360 | 29 | * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY |
fahadmirza | 0:a0c5877bd360 | 30 | * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT |
fahadmirza | 0:a0c5877bd360 | 31 | * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
fahadmirza | 0:a0c5877bd360 | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
fahadmirza | 0:a0c5877bd360 | 33 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
fahadmirza | 0:a0c5877bd360 | 34 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
fahadmirza | 0:a0c5877bd360 | 35 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
fahadmirza | 0:a0c5877bd360 | 36 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
fahadmirza | 0:a0c5877bd360 | 37 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fahadmirza | 0:a0c5877bd360 | 38 | * |
fahadmirza | 0:a0c5877bd360 | 39 | ****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 40 | */ |
fahadmirza | 0:a0c5877bd360 | 41 | |
fahadmirza | 0:a0c5877bd360 | 42 | /*---- Includes ----------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 43 | #include "hw.h" |
fahadmirza | 0:a0c5877bd360 | 44 | #include "low_power_manager.h" |
fahadmirza | 0:a0c5877bd360 | 45 | #include "time_server.h" |
fahadmirza | 0:a0c5877bd360 | 46 | |
fahadmirza | 0:a0c5877bd360 | 47 | /*---- Private variable --------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 48 | static struct |
fahadmirza | 0:a0c5877bd360 | 49 | { |
fahadmirza | 0:a0c5877bd360 | 50 | char buffTx[256]; /* structure have to be simplified*/ |
fahadmirza | 0:a0c5877bd360 | 51 | char buffRx[256]; |
fahadmirza | 0:a0c5877bd360 | 52 | int rx_idx_free; |
fahadmirza | 0:a0c5877bd360 | 53 | int rx_idx_toread; |
fahadmirza | 0:a0c5877bd360 | 54 | HW_LockTypeDef Lock; |
fahadmirza | 0:a0c5877bd360 | 55 | __IO HAL_UART_StateTypeDef gState; |
fahadmirza | 0:a0c5877bd360 | 56 | __IO HAL_UART_StateTypeDef RxState; |
fahadmirza | 0:a0c5877bd360 | 57 | } uart_context; |
fahadmirza | 0:a0c5877bd360 | 58 | |
fahadmirza | 0:a0c5877bd360 | 59 | /*---- private function --------------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 60 | static void receive(char rx); |
fahadmirza | 0:a0c5877bd360 | 61 | |
fahadmirza | 0:a0c5877bd360 | 62 | /*---- private Global variables ------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 63 | static UART_HandleTypeDef hxcUart; |
fahadmirza | 0:a0c5877bd360 | 64 | |
fahadmirza | 0:a0c5877bd360 | 65 | |
fahadmirza | 0:a0c5877bd360 | 66 | /*---- Function Definitions ----------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 67 | |
fahadmirza | 0:a0c5877bd360 | 68 | bool HW_UART_Modem_Init(uint32_t BaudRate) |
fahadmirza | 0:a0c5877bd360 | 69 | { |
fahadmirza | 0:a0c5877bd360 | 70 | hxcUart.Instance = HXC_USARTX; |
fahadmirza | 0:a0c5877bd360 | 71 | hxcUart.Init.BaudRate = BaudRate; |
fahadmirza | 0:a0c5877bd360 | 72 | hxcUart.Init.WordLength = UART_WORDLENGTH_8B; |
fahadmirza | 0:a0c5877bd360 | 73 | hxcUart.Init.StopBits = UART_STOPBITS_1; |
fahadmirza | 0:a0c5877bd360 | 74 | hxcUart.Init.Parity = UART_PARITY_NONE; |
fahadmirza | 0:a0c5877bd360 | 75 | hxcUart.Init.Mode = UART_MODE_TX_RX; |
fahadmirza | 0:a0c5877bd360 | 76 | hxcUart.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
fahadmirza | 0:a0c5877bd360 | 77 | hxcUart.Init.OverSampling = UART_OVERSAMPLING_16; |
fahadmirza | 0:a0c5877bd360 | 78 | hxcUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; |
fahadmirza | 0:a0c5877bd360 | 79 | hxcUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; |
fahadmirza | 0:a0c5877bd360 | 80 | hxcUart.gState = HAL_UART_STATE_RESET; |
fahadmirza | 0:a0c5877bd360 | 81 | |
fahadmirza | 0:a0c5877bd360 | 82 | |
fahadmirza | 0:a0c5877bd360 | 83 | if(HAL_UART_Init(&hxcUart) != HAL_OK) |
fahadmirza | 0:a0c5877bd360 | 84 | { |
fahadmirza | 0:a0c5877bd360 | 85 | // Initialization Error |
fahadmirza | 0:a0c5877bd360 | 86 | return false; |
fahadmirza | 0:a0c5877bd360 | 87 | } |
fahadmirza | 0:a0c5877bd360 | 88 | |
fahadmirza | 0:a0c5877bd360 | 89 | /* Computation of UART mask to apply to RDR register */ |
fahadmirza | 0:a0c5877bd360 | 90 | UART_MASK_COMPUTATION(&hxcUart); |
fahadmirza | 0:a0c5877bd360 | 91 | /*******************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 92 | /*see Application Note AN4991 : how to wake up an STM32L0 MCU from */ |
fahadmirza | 0:a0c5877bd360 | 93 | /*low power mode with the USART or the LPUART */ |
fahadmirza | 0:a0c5877bd360 | 94 | /*******************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 95 | |
fahadmirza | 0:a0c5877bd360 | 96 | /* Enable the UART Parity Error and Data Register not empty Interrupts */ |
fahadmirza | 0:a0c5877bd360 | 97 | SET_BIT(hxcUart.Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE); |
fahadmirza | 0:a0c5877bd360 | 98 | /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ |
fahadmirza | 0:a0c5877bd360 | 99 | SET_BIT(hxcUart.Instance->CR3, USART_CR3_EIE); |
fahadmirza | 0:a0c5877bd360 | 100 | |
fahadmirza | 0:a0c5877bd360 | 101 | /*Enable UART Stop Mode*/ |
fahadmirza | 0:a0c5877bd360 | 102 | HAL_UARTEx_EnableStopMode(&hxcUart); |
fahadmirza | 0:a0c5877bd360 | 103 | |
fahadmirza | 0:a0c5877bd360 | 104 | // Enable wakeup from stop mode active on start bit detection |
fahadmirza | 0:a0c5877bd360 | 105 | MODIFY_REG(hxcUart.Instance->CR3, USART_CR3_WUS, USART_CR3_WUS_1); |
fahadmirza | 0:a0c5877bd360 | 106 | |
fahadmirza | 0:a0c5877bd360 | 107 | // Enable Wake Up from Stop Mode Interrupt |
fahadmirza | 0:a0c5877bd360 | 108 | SET_BIT(hxcUart.Instance->CR3, USART_CR3_WUFIE); |
fahadmirza | 0:a0c5877bd360 | 109 | |
fahadmirza | 0:a0c5877bd360 | 110 | hxcUart.ErrorCode = HAL_UART_ERROR_NONE; |
fahadmirza | 0:a0c5877bd360 | 111 | hxcUart.RxState = HAL_UART_STATE_READY; |
fahadmirza | 0:a0c5877bd360 | 112 | |
fahadmirza | 0:a0c5877bd360 | 113 | return true; |
fahadmirza | 0:a0c5877bd360 | 114 | } |
fahadmirza | 0:a0c5877bd360 | 115 | |
fahadmirza | 0:a0c5877bd360 | 116 | void HAL_UART_MspInit(UART_HandleTypeDef* huart) |
fahadmirza | 0:a0c5877bd360 | 117 | { |
fahadmirza | 0:a0c5877bd360 | 118 | GPIO_InitTypeDef GPIO_InitStruct; |
fahadmirza | 0:a0c5877bd360 | 119 | /***************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 120 | /* GPIO Configuration for UART2 or for LPUART1 */ |
fahadmirza | 0:a0c5877bd360 | 121 | /* for USART1 use: */ |
fahadmirza | 0:a0c5877bd360 | 122 | /* PA10 ------> USART1_RX */ |
fahadmirza | 0:a0c5877bd360 | 123 | /* PA9 ------> USART1_TX */ |
fahadmirza | 0:a0c5877bd360 | 124 | /* else for USART2 use: */ |
fahadmirza | 0:a0c5877bd360 | 125 | /* PA2 ------> USART2_TX */ |
fahadmirza | 0:a0c5877bd360 | 126 | /* PA3 ------> USART2_RX */ |
fahadmirza | 0:a0c5877bd360 | 127 | /***************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 128 | |
fahadmirza | 0:a0c5877bd360 | 129 | /* We need both instances. One UART is used for HXC and |
fahadmirza | 0:a0c5877bd360 | 130 | * another one is used for debug printf. As HAL_UART_Init() |
fahadmirza | 0:a0c5877bd360 | 131 | * uses HAL_UART_MspInit(), we needed to cover both instances. |
fahadmirza | 0:a0c5877bd360 | 132 | */ |
fahadmirza | 0:a0c5877bd360 | 133 | if(huart->Instance == USART1) |
fahadmirza | 0:a0c5877bd360 | 134 | { |
fahadmirza | 0:a0c5877bd360 | 135 | // Peripheral clock enable |
fahadmirza | 0:a0c5877bd360 | 136 | __HAL_RCC_USART1_CLK_ENABLE(); |
fahadmirza | 0:a0c5877bd360 | 137 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
fahadmirza | 0:a0c5877bd360 | 138 | GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10; |
fahadmirza | 0:a0c5877bd360 | 139 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
fahadmirza | 0:a0c5877bd360 | 140 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
fahadmirza | 0:a0c5877bd360 | 141 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; |
fahadmirza | 0:a0c5877bd360 | 142 | GPIO_InitStruct.Alternate = GPIO_AF4_USART1; |
fahadmirza | 0:a0c5877bd360 | 143 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
fahadmirza | 0:a0c5877bd360 | 144 | } |
fahadmirza | 0:a0c5877bd360 | 145 | else if(huart->Instance == USART2) |
fahadmirza | 0:a0c5877bd360 | 146 | { |
fahadmirza | 0:a0c5877bd360 | 147 | // Peripheral clock enable |
fahadmirza | 0:a0c5877bd360 | 148 | __HAL_RCC_USART2_CLK_ENABLE(); |
fahadmirza | 0:a0c5877bd360 | 149 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
fahadmirza | 0:a0c5877bd360 | 150 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; |
fahadmirza | 0:a0c5877bd360 | 151 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
fahadmirza | 0:a0c5877bd360 | 152 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
fahadmirza | 0:a0c5877bd360 | 153 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; |
fahadmirza | 0:a0c5877bd360 | 154 | GPIO_InitStruct.Alternate = GPIO_AF4_USART2; |
fahadmirza | 0:a0c5877bd360 | 155 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
fahadmirza | 0:a0c5877bd360 | 156 | } |
fahadmirza | 0:a0c5877bd360 | 157 | if(huart->Instance == HXC_USARTX) |
fahadmirza | 0:a0c5877bd360 | 158 | { |
fahadmirza | 0:a0c5877bd360 | 159 | // Peripheral interrupt init |
fahadmirza | 0:a0c5877bd360 | 160 | HAL_NVIC_SetPriority(HXC_USARTX_IRQn, 0, 0); |
fahadmirza | 0:a0c5877bd360 | 161 | HAL_NVIC_EnableIRQ(HXC_USARTX_IRQn); |
fahadmirza | 0:a0c5877bd360 | 162 | } |
fahadmirza | 0:a0c5877bd360 | 163 | } |
fahadmirza | 0:a0c5877bd360 | 164 | |
fahadmirza | 0:a0c5877bd360 | 165 | void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) |
fahadmirza | 0:a0c5877bd360 | 166 | { |
fahadmirza | 0:a0c5877bd360 | 167 | |
fahadmirza | 0:a0c5877bd360 | 168 | if(huart->Instance == USART1) |
fahadmirza | 0:a0c5877bd360 | 169 | { |
fahadmirza | 0:a0c5877bd360 | 170 | /* Peripheral clock disable */ |
fahadmirza | 0:a0c5877bd360 | 171 | __HAL_RCC_USART1_CLK_DISABLE(); |
fahadmirza | 0:a0c5877bd360 | 172 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); |
fahadmirza | 0:a0c5877bd360 | 173 | } |
fahadmirza | 0:a0c5877bd360 | 174 | else if(huart->Instance == USART2) |
fahadmirza | 0:a0c5877bd360 | 175 | { |
fahadmirza | 0:a0c5877bd360 | 176 | /* Peripheral clock disable */ |
fahadmirza | 0:a0c5877bd360 | 177 | __HAL_RCC_USART2_CLK_DISABLE(); |
fahadmirza | 0:a0c5877bd360 | 178 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); |
fahadmirza | 0:a0c5877bd360 | 179 | } |
fahadmirza | 0:a0c5877bd360 | 180 | |
fahadmirza | 0:a0c5877bd360 | 181 | if(huart->Instance == HXC_USARTX) |
fahadmirza | 0:a0c5877bd360 | 182 | { |
fahadmirza | 0:a0c5877bd360 | 183 | /* Peripheral interrupt Deinit*/ |
fahadmirza | 0:a0c5877bd360 | 184 | HAL_NVIC_DisableIRQ(HXC_USARTX_IRQn); |
fahadmirza | 0:a0c5877bd360 | 185 | } |
fahadmirza | 0:a0c5877bd360 | 186 | } |
fahadmirza | 0:a0c5877bd360 | 187 | |
fahadmirza | 0:a0c5877bd360 | 188 | |
fahadmirza | 0:a0c5877bd360 | 189 | void HW_UART_Modem_DeInit(void) |
fahadmirza | 0:a0c5877bd360 | 190 | { |
fahadmirza | 0:a0c5877bd360 | 191 | HAL_UART_DeInit(&hxcUart); |
fahadmirza | 0:a0c5877bd360 | 192 | } |
fahadmirza | 0:a0c5877bd360 | 193 | |
fahadmirza | 0:a0c5877bd360 | 194 | |
fahadmirza | 0:a0c5877bd360 | 195 | bool HW_UART_Modem_Receive_IT (uint8_t *aRxBuffer) |
fahadmirza | 0:a0c5877bd360 | 196 | { |
fahadmirza | 0:a0c5877bd360 | 197 | return ( HAL_UART_Receive_IT(&hxcUart, aRxBuffer, 1) == HAL_OK ); |
fahadmirza | 0:a0c5877bd360 | 198 | } |
fahadmirza | 0:a0c5877bd360 | 199 | |
fahadmirza | 0:a0c5877bd360 | 200 | |
fahadmirza | 0:a0c5877bd360 | 201 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 202 | * @brief Handler on Rx IRQ |
fahadmirza | 0:a0c5877bd360 | 203 | * @param handle to the UART |
fahadmirza | 0:a0c5877bd360 | 204 | * @retval void |
fahadmirza | 0:a0c5877bd360 | 205 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 206 | void HW_UART_Modem_IRQHandler(void) |
fahadmirza | 0:a0c5877bd360 | 207 | { |
fahadmirza | 0:a0c5877bd360 | 208 | UART_HandleTypeDef *huart = &hxcUart; //ToDo |
fahadmirza | 0:a0c5877bd360 | 209 | uint32_t isrflags = READ_REG(huart->Instance->ISR); |
fahadmirza | 0:a0c5877bd360 | 210 | uint32_t cr1its = READ_REG(huart->Instance->CR1); |
fahadmirza | 0:a0c5877bd360 | 211 | uint32_t cr3its = READ_REG(huart->Instance->CR3);; |
fahadmirza | 0:a0c5877bd360 | 212 | uint32_t errorflags; |
fahadmirza | 0:a0c5877bd360 | 213 | char rxByte = '\0'; |
fahadmirza | 0:a0c5877bd360 | 214 | int rx_ready = 0; |
fahadmirza | 0:a0c5877bd360 | 215 | |
fahadmirza | 0:a0c5877bd360 | 216 | huart->RxState = HAL_UART_STATE_BUSY_RX; |
fahadmirza | 0:a0c5877bd360 | 217 | |
fahadmirza | 0:a0c5877bd360 | 218 | /* UART wakeup from Stop mode interrupt occurred ---------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 219 | if(((isrflags & USART_ISR_WUF) != RESET) && ((cr3its & USART_CR3_WUFIE) != RESET)) |
fahadmirza | 0:a0c5877bd360 | 220 | { |
fahadmirza | 0:a0c5877bd360 | 221 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_WUF); |
fahadmirza | 0:a0c5877bd360 | 222 | |
fahadmirza | 0:a0c5877bd360 | 223 | /* forbid stop mode */ |
fahadmirza | 0:a0c5877bd360 | 224 | LPM_SetStopMode(LPM_UART_RX_Id , LPM_Disable ); |
fahadmirza | 0:a0c5877bd360 | 225 | |
fahadmirza | 0:a0c5877bd360 | 226 | /* Enable the UART Data Register not empty Interrupts */ |
fahadmirza | 0:a0c5877bd360 | 227 | SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE); |
fahadmirza | 0:a0c5877bd360 | 228 | |
fahadmirza | 0:a0c5877bd360 | 229 | /* Set the UART state ready to be able to start again the process */ |
fahadmirza | 0:a0c5877bd360 | 230 | huart->gState = HAL_UART_STATE_READY; |
fahadmirza | 0:a0c5877bd360 | 231 | huart->RxState = HAL_UART_STATE_READY; |
fahadmirza | 0:a0c5877bd360 | 232 | } |
fahadmirza | 0:a0c5877bd360 | 233 | |
fahadmirza | 0:a0c5877bd360 | 234 | /* UART in mode Receiver ---------------------------------------------------*/ |
fahadmirza | 0:a0c5877bd360 | 235 | if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
fahadmirza | 0:a0c5877bd360 | 236 | { |
fahadmirza | 0:a0c5877bd360 | 237 | /* Check that a Rx process is ongoing */ |
fahadmirza | 0:a0c5877bd360 | 238 | if(huart->RxState == HAL_UART_STATE_BUSY_RX) |
fahadmirza | 0:a0c5877bd360 | 239 | { |
fahadmirza | 0:a0c5877bd360 | 240 | /*RXNE flag is auto cleared by reading the data*/ |
fahadmirza | 0:a0c5877bd360 | 241 | rxByte = (uint8_t)READ_REG(huart->Instance->RDR); |
fahadmirza | 0:a0c5877bd360 | 242 | |
fahadmirza | 0:a0c5877bd360 | 243 | /* allow stop mode*/ |
fahadmirza | 0:a0c5877bd360 | 244 | LPM_SetStopMode(LPM_UART_RX_Id , LPM_Enable ); |
fahadmirza | 0:a0c5877bd360 | 245 | |
fahadmirza | 0:a0c5877bd360 | 246 | huart->RxState = HAL_UART_STATE_READY; |
fahadmirza | 0:a0c5877bd360 | 247 | rx_ready = 1; /* not used RxTC callback*/ |
fahadmirza | 0:a0c5877bd360 | 248 | } |
fahadmirza | 0:a0c5877bd360 | 249 | else |
fahadmirza | 0:a0c5877bd360 | 250 | { |
fahadmirza | 0:a0c5877bd360 | 251 | // Clear RXNE interrupt flag |
fahadmirza | 0:a0c5877bd360 | 252 | __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); |
fahadmirza | 0:a0c5877bd360 | 253 | return; |
fahadmirza | 0:a0c5877bd360 | 254 | } |
fahadmirza | 0:a0c5877bd360 | 255 | } |
fahadmirza | 0:a0c5877bd360 | 256 | |
fahadmirza | 0:a0c5877bd360 | 257 | // If error occurs |
fahadmirza | 0:a0c5877bd360 | 258 | errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE)); |
fahadmirza | 0:a0c5877bd360 | 259 | if (errorflags != RESET) |
fahadmirza | 0:a0c5877bd360 | 260 | { |
fahadmirza | 0:a0c5877bd360 | 261 | // Error on receiving |
fahadmirza | 0:a0c5877bd360 | 262 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF); |
fahadmirza | 0:a0c5877bd360 | 263 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF); |
fahadmirza | 0:a0c5877bd360 | 264 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF); |
fahadmirza | 0:a0c5877bd360 | 265 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF); |
fahadmirza | 0:a0c5877bd360 | 266 | // we skip the overrun case |
fahadmirza | 0:a0c5877bd360 | 267 | rx_ready = 1; |
fahadmirza | 0:a0c5877bd360 | 268 | } |
fahadmirza | 0:a0c5877bd360 | 269 | |
fahadmirza | 0:a0c5877bd360 | 270 | if(rx_ready) |
fahadmirza | 0:a0c5877bd360 | 271 | { |
fahadmirza | 0:a0c5877bd360 | 272 | // Put received character in the ring buffer |
fahadmirza | 0:a0c5877bd360 | 273 | receive(rxByte); |
fahadmirza | 0:a0c5877bd360 | 274 | } |
fahadmirza | 0:a0c5877bd360 | 275 | } |
fahadmirza | 0:a0c5877bd360 | 276 | |
fahadmirza | 0:a0c5877bd360 | 277 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 278 | * @Brief : Read a complete string until <CR><LF> |
fahadmirza | 0:a0c5877bd360 | 279 | * @Param : rxBuffer - pointer for received characters |
fahadmirza | 0:a0c5877bd360 | 280 | * rxBufferSize - size of the rxBuffer |
fahadmirza | 0:a0c5877bd360 | 281 | * @Retval: Return the number of characters received |
fahadmirza | 0:a0c5877bd360 | 282 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 283 | uint8_t HW_UART_Modem_GetCharactersUntilNewLine(char *rxBuffer, uint8_t rxBufferSize, uint32_t timeout) |
fahadmirza | 0:a0c5877bd360 | 284 | { |
fahadmirza | 0:a0c5877bd360 | 285 | uint8_t len = 0; |
fahadmirza | 0:a0c5877bd360 | 286 | uint32_t currentTime = TimerGetCurrentTime(); |
fahadmirza | 0:a0c5877bd360 | 287 | |
fahadmirza | 0:a0c5877bd360 | 288 | while(len < (rxBufferSize - 1)) // Keep 1 byte for NULL |
fahadmirza | 0:a0c5877bd360 | 289 | { |
fahadmirza | 0:a0c5877bd360 | 290 | if(HW_UART_Modem_IsNewCharReceived() == false) |
fahadmirza | 0:a0c5877bd360 | 291 | { |
fahadmirza | 0:a0c5877bd360 | 292 | if(TimerGetElapsedTime(currentTime) > timeout) |
fahadmirza | 0:a0c5877bd360 | 293 | { |
fahadmirza | 0:a0c5877bd360 | 294 | break; |
fahadmirza | 0:a0c5877bd360 | 295 | } |
fahadmirza | 0:a0c5877bd360 | 296 | } |
fahadmirza | 0:a0c5877bd360 | 297 | else |
fahadmirza | 0:a0c5877bd360 | 298 | { |
fahadmirza | 0:a0c5877bd360 | 299 | rxBuffer[len++] = (char)HW_UART_Modem_GetNewChar(); |
fahadmirza | 0:a0c5877bd360 | 300 | if(rxBuffer[len - 1] == '\n') |
fahadmirza | 0:a0c5877bd360 | 301 | { |
fahadmirza | 0:a0c5877bd360 | 302 | break; |
fahadmirza | 0:a0c5877bd360 | 303 | } |
fahadmirza | 0:a0c5877bd360 | 304 | } |
fahadmirza | 0:a0c5877bd360 | 305 | } |
fahadmirza | 0:a0c5877bd360 | 306 | |
fahadmirza | 0:a0c5877bd360 | 307 | rxBuffer[len] = '\0'; |
fahadmirza | 0:a0c5877bd360 | 308 | return len; |
fahadmirza | 0:a0c5877bd360 | 309 | } |
fahadmirza | 0:a0c5877bd360 | 310 | |
fahadmirza | 0:a0c5877bd360 | 311 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 312 | * @brief To check if data has been received |
fahadmirza | 0:a0c5877bd360 | 313 | * @param none |
fahadmirza | 0:a0c5877bd360 | 314 | * @retval false no data / true data |
fahadmirza | 0:a0c5877bd360 | 315 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 316 | bool HW_UART_Modem_IsNewCharReceived(void) |
fahadmirza | 0:a0c5877bd360 | 317 | { |
fahadmirza | 0:a0c5877bd360 | 318 | bool status; |
fahadmirza | 0:a0c5877bd360 | 319 | // ToDo : why STM used separate PRIMASK()? |
fahadmirza | 0:a0c5877bd360 | 320 | |
fahadmirza | 0:a0c5877bd360 | 321 | //BACKUP_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 322 | uint32_t primask_bit= __get_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 323 | //DISABLE_IRQ(); |
fahadmirza | 0:a0c5877bd360 | 324 | __disable_irq(); |
fahadmirza | 0:a0c5877bd360 | 325 | |
fahadmirza | 0:a0c5877bd360 | 326 | status = ((uart_context.rx_idx_toread == uart_context.rx_idx_free) ? false : true); |
fahadmirza | 0:a0c5877bd360 | 327 | |
fahadmirza | 0:a0c5877bd360 | 328 | //RESTORE_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 329 | __set_PRIMASK(primask_bit); |
fahadmirza | 0:a0c5877bd360 | 330 | return status; |
fahadmirza | 0:a0c5877bd360 | 331 | } |
fahadmirza | 0:a0c5877bd360 | 332 | |
fahadmirza | 0:a0c5877bd360 | 333 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 334 | * @brief Get the received character |
fahadmirza | 0:a0c5877bd360 | 335 | * @param none |
fahadmirza | 0:a0c5877bd360 | 336 | * @retval Return the data received |
fahadmirza | 0:a0c5877bd360 | 337 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 338 | uint8_t HW_UART_Modem_GetNewChar(void) |
fahadmirza | 0:a0c5877bd360 | 339 | { |
fahadmirza | 0:a0c5877bd360 | 340 | uint8_t NewChar; |
fahadmirza | 0:a0c5877bd360 | 341 | |
fahadmirza | 0:a0c5877bd360 | 342 | // BACKUP_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 343 | uint32_t primask_bit= __get_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 344 | // DISABLE_IRQ(); |
fahadmirza | 0:a0c5877bd360 | 345 | __disable_irq(); |
fahadmirza | 0:a0c5877bd360 | 346 | |
fahadmirza | 0:a0c5877bd360 | 347 | NewChar = uart_context.buffRx[uart_context.rx_idx_toread]; |
fahadmirza | 0:a0c5877bd360 | 348 | uart_context.rx_idx_toread = (uart_context.rx_idx_toread + 1) % sizeof(uart_context.buffRx); |
fahadmirza | 0:a0c5877bd360 | 349 | |
fahadmirza | 0:a0c5877bd360 | 350 | // RESTORE_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 351 | __set_PRIMASK(primask_bit); |
fahadmirza | 0:a0c5877bd360 | 352 | return NewChar; |
fahadmirza | 0:a0c5877bd360 | 353 | } |
fahadmirza | 0:a0c5877bd360 | 354 | |
fahadmirza | 0:a0c5877bd360 | 355 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 356 | * @brief Reset read and write index of the circular buffer |
fahadmirza | 0:a0c5877bd360 | 357 | * @param none |
fahadmirza | 0:a0c5877bd360 | 358 | * @retval none |
fahadmirza | 0:a0c5877bd360 | 359 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 360 | void HW_UART_ResetBuffer(void) |
fahadmirza | 0:a0c5877bd360 | 361 | { |
fahadmirza | 0:a0c5877bd360 | 362 | BACKUP_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 363 | DISABLE_IRQ(); |
fahadmirza | 0:a0c5877bd360 | 364 | |
fahadmirza | 0:a0c5877bd360 | 365 | uart_context.rx_idx_toread = uart_context.rx_idx_free; |
fahadmirza | 0:a0c5877bd360 | 366 | |
fahadmirza | 0:a0c5877bd360 | 367 | RESTORE_PRIMASK(); |
fahadmirza | 0:a0c5877bd360 | 368 | } |
fahadmirza | 0:a0c5877bd360 | 369 | |
fahadmirza | 0:a0c5877bd360 | 370 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 371 | * @Brief : Send bytes |
fahadmirza | 0:a0c5877bd360 | 372 | * @param : ToDo |
fahadmirza | 0:a0c5877bd360 | 373 | * @Retval: True - if the operation is successful |
fahadmirza | 0:a0c5877bd360 | 374 | * False - if the operation isn't successful |
fahadmirza | 0:a0c5877bd360 | 375 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 376 | bool HW_UART_Modem_SendBytes(const char *pData, uint16_t size) |
fahadmirza | 0:a0c5877bd360 | 377 | { |
fahadmirza | 0:a0c5877bd360 | 378 | if (HAL_UART_Transmit(&hxcUart, (uint8_t *)pData, size, 5000) != HAL_OK) |
fahadmirza | 0:a0c5877bd360 | 379 | { |
fahadmirza | 0:a0c5877bd360 | 380 | return false; |
fahadmirza | 0:a0c5877bd360 | 381 | } |
fahadmirza | 0:a0c5877bd360 | 382 | |
fahadmirza | 0:a0c5877bd360 | 383 | return true; |
fahadmirza | 0:a0c5877bd360 | 384 | } |
fahadmirza | 0:a0c5877bd360 | 385 | |
fahadmirza | 0:a0c5877bd360 | 386 | |
fahadmirza | 0:a0c5877bd360 | 387 | void HW_UART_Modem_Ready(void) |
fahadmirza | 0:a0c5877bd360 | 388 | { |
fahadmirza | 0:a0c5877bd360 | 389 | hxcUart.gState = HAL_UART_STATE_READY; |
fahadmirza | 0:a0c5877bd360 | 390 | hxcUart.RxState = HAL_UART_STATE_READY; |
fahadmirza | 0:a0c5877bd360 | 391 | } |
fahadmirza | 0:a0c5877bd360 | 392 | |
fahadmirza | 0:a0c5877bd360 | 393 | |
fahadmirza | 0:a0c5877bd360 | 394 | /****************************************************************************** |
fahadmirza | 0:a0c5877bd360 | 395 | * @brief Store in ring buffer the received character |
fahadmirza | 0:a0c5877bd360 | 396 | * @param none |
fahadmirza | 0:a0c5877bd360 | 397 | * @retval none |
fahadmirza | 0:a0c5877bd360 | 398 | ******************************************************************************/ |
fahadmirza | 0:a0c5877bd360 | 399 | static void receive(char rx) |
fahadmirza | 0:a0c5877bd360 | 400 | { |
fahadmirza | 0:a0c5877bd360 | 401 | int next_free; |
fahadmirza | 0:a0c5877bd360 | 402 | |
fahadmirza | 0:a0c5877bd360 | 403 | /** no need to clear the RXNE flag because it is auto cleared by reading the data*/ |
fahadmirza | 0:a0c5877bd360 | 404 | uart_context.buffRx[uart_context.rx_idx_free] = rx; |
fahadmirza | 0:a0c5877bd360 | 405 | next_free = (uart_context.rx_idx_free + 1) % sizeof(uart_context.buffRx); |
fahadmirza | 0:a0c5877bd360 | 406 | if (next_free != uart_context.rx_idx_toread) |
fahadmirza | 0:a0c5877bd360 | 407 | { |
fahadmirza | 0:a0c5877bd360 | 408 | /* this is ok to read as there is no buffer overflow in input */ |
fahadmirza | 0:a0c5877bd360 | 409 | uart_context.rx_idx_free = next_free; |
fahadmirza | 0:a0c5877bd360 | 410 | } |
fahadmirza | 0:a0c5877bd360 | 411 | // else |
fahadmirza | 0:a0c5877bd360 | 412 | // { |
fahadmirza | 0:a0c5877bd360 | 413 | // /* force the end of a command in case of overflow so that we can process it */ |
fahadmirza | 0:a0c5877bd360 | 414 | // uart_context.buffRx[uart_context.rx_idx_free] = '\r'; |
fahadmirza | 0:a0c5877bd360 | 415 | // PRINTF("uart_context.buffRx buffer overflow %d\r\n"); |
fahadmirza | 0:a0c5877bd360 | 416 | // } |
fahadmirza | 0:a0c5877bd360 | 417 | } |
fahadmirza | 0:a0c5877bd360 | 418 | |
fahadmirza | 0:a0c5877bd360 | 419 | /** |
fahadmirza | 0:a0c5877bd360 | 420 | * @} |
fahadmirza | 0:a0c5877bd360 | 421 | */ |
fahadmirza | 0:a0c5877bd360 | 422 | |
fahadmirza | 0:a0c5877bd360 | 423 | /** |
fahadmirza | 0:a0c5877bd360 | 424 | * @} |
fahadmirza | 0:a0c5877bd360 | 425 | */ |
fahadmirza | 0:a0c5877bd360 | 426 | |
fahadmirza | 0:a0c5877bd360 | 427 | /************************ (C) COPYRIGHT Haxiot *****END OF FILE****/ |
fahadmirza | 0:a0c5877bd360 | 428 |