922
+ − 1 /**
+ − 2 ******************************************************************************
+ − 3 * @file uart_Internal.c
+ − 4 * @author heinrichs weikamp gmbh
+ − 5 * @version V0.0.1
+ − 6 * @date 03-November-2044
+ − 7 * @brief Control functions for devices connected to the internal UART
+ − 8 *
+ − 9 @verbatim
+ − 10 ==============================================================================
+ − 11 ##### How to use #####
+ − 12 ==============================================================================
+ − 13 @endverbatim
+ − 14 ******************************************************************************
+ − 15 * @attention
+ − 16 *
+ − 17 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2>
+ − 18 *
+ − 19 ******************************************************************************
+ − 20 */
+ − 21 /* Includes ------------------------------------------------------------------*/
+ − 22 #include "uart.h"
+ − 23 #include "uart_Internal.h"
+ − 24 #include "uartProtocol_GNSS.h"
+ − 25 #include "GNSS.h"
+ − 26 #include "externalInterface.h"
+ − 27 #include "data_exchange.h"
+ − 28 #include <string.h> /* memset */
+ − 29
+ − 30
+ − 31 /* Private variables ---------------------------------------------------------*/
+ − 32
+ − 33 #define REQUEST_INT_SENSOR_MS (1500) /* Minimum time interval for cyclic sensor data requests per sensor (UART mux) */
+ − 34 #define COMMAND_TX_DELAY (30u) /* The time the sensor needs to recover from a invalid command request */
+ − 35 #define TIMEOUT_SENSOR_ANSWER (300) /* Time till a request is repeated if no answer was received */
+ − 36
+ − 37 DMA_HandleTypeDef hdma_usart6_rx, hdma_usart6_tx;
+ − 38
+ − 39 uint8_t tx6Buffer[CHUNK_SIZE]; /* tx uses less bytes */
+ − 40
+ − 41 uint8_t rxBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */
+ − 42 uint8_t txBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */
+ − 43
932
+ − 44 sUartComCtrl Uart6Ctrl;
922
+ − 45
+ − 46 /* Exported functions --------------------------------------------------------*/
+ − 47
+ − 48 void GNSS_IO_init() {
+ − 49
+ − 50 GPIO_InitTypeDef GPIO_InitStruct = { 0 };
+ − 51 /* Peripheral clock enable */
+ − 52 __HAL_RCC_USART6_CLK_ENABLE()
+ − 53 ;
+ − 54
+ − 55 __HAL_RCC_GPIOA_CLK_ENABLE()
+ − 56 ;
+ − 57 /**USART6 GPIO Configuration
+ − 58 PA11 ------> USART6_TX
+ − 59 PA12 ------> USART6_RX
+ − 60 */
+ − 61 GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;
+ − 62 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ − 63 GPIO_InitStruct.Pull = GPIO_NOPULL;
+ − 64 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
+ − 65 GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
+ − 66 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+ − 67
+ − 68 /* USART6 DMA Init */
+ − 69 /* USART6_RX Init */
923
+ − 70 hdma_usart6_rx.Instance = DMA2_Stream2;
922
+ − 71 hdma_usart6_rx.Init.Channel = DMA_CHANNEL_5;
+ − 72 hdma_usart6_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
+ − 73 hdma_usart6_rx.Init.PeriphInc = DMA_PINC_DISABLE;
+ − 74 hdma_usart6_rx.Init.MemInc = DMA_MINC_ENABLE;
+ − 75 hdma_usart6_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
+ − 76 hdma_usart6_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+ − 77 hdma_usart6_rx.Init.Mode = DMA_NORMAL;
+ − 78 hdma_usart6_rx.Init.Priority = DMA_PRIORITY_LOW;
+ − 79 hdma_usart6_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+ − 80 HAL_DMA_Init(&hdma_usart6_rx);
+ − 81
+ − 82 __HAL_LINKDMA(&huart6, hdmarx, hdma_usart6_rx);
+ − 83
+ − 84 /* USART6_TX Init */
+ − 85 hdma_usart6_tx.Instance = DMA2_Stream6;
+ − 86 hdma_usart6_tx.Init.Channel = DMA_CHANNEL_5;
+ − 87 hdma_usart6_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
+ − 88 hdma_usart6_tx.Init.PeriphInc = DMA_PINC_DISABLE;
+ − 89 hdma_usart6_tx.Init.MemInc = DMA_MINC_ENABLE;
+ − 90 hdma_usart6_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
+ − 91 hdma_usart6_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+ − 92 hdma_usart6_tx.Init.Mode = DMA_NORMAL;
+ − 93 hdma_usart6_tx.Init.Priority = DMA_PRIORITY_LOW;
+ − 94 hdma_usart6_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+ − 95 HAL_DMA_Init(&hdma_usart6_tx);
+ − 96
+ − 97 __HAL_LINKDMA(&huart6, hdmatx, hdma_usart6_tx);
+ − 98
+ − 99 /* USART6 interrupt Init */
+ − 100 HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
+ − 101 HAL_NVIC_EnableIRQ(USART6_IRQn);
+ − 102
+ − 103 MX_USART6_DMA_Init();
+ − 104
+ − 105 }
+ − 106
+ − 107 void MX_USART6_DMA_Init() {
+ − 108 /* DMA controller clock enable */
+ − 109 __HAL_RCC_DMA2_CLK_ENABLE();
+ − 110
+ − 111 /* DMA interrupt init */
+ − 112 /* DMA2_Stream2_IRQn interrupt configuration */
+ − 113 HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
+ − 114 HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
+ − 115 /* DMA2_Stream6_IRQn interrupt configuration */
+ − 116 HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 0, 0);
+ − 117 HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);
+ − 118 }
+ − 119
+ − 120
+ − 121 void MX_USART6_UART_DeInit(void)
+ − 122 {
+ − 123 HAL_DMA_Abort(&hdma_usart6_rx);
+ − 124 HAL_DMA_DeInit(&hdma_usart6_rx);
+ − 125 HAL_DMA_Abort(&hdma_usart6_tx);
+ − 126 HAL_DMA_DeInit(&hdma_usart6_tx);
+ − 127 HAL_UART_DeInit(&huart6);
+ − 128 HAL_UART_DeInit(&huart6);
+ − 129 }
+ − 130
+ − 131 void MX_USART6_UART_Init(void) {
+ − 132 huart6.Instance = USART6;
+ − 133 huart6.Init.BaudRate = 9600;
+ − 134 huart6.Init.WordLength = UART_WORDLENGTH_8B;
+ − 135 huart6.Init.StopBits = UART_STOPBITS_1;
+ − 136 huart6.Init.Parity = UART_PARITY_NONE;
+ − 137 huart6.Init.Mode = UART_MODE_TX_RX;
+ − 138 huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ − 139 huart6.Init.OverSampling = UART_OVERSAMPLING_16;
+ − 140 HAL_UART_Init(&huart6);
923
+ − 141
932
+ − 142 UART_clearRxBuffer(&Uart6Ctrl);
922
+ − 143
932
+ − 144 Uart6Ctrl.pHandle = &huart6;
+ − 145 Uart6Ctrl.dmaRxActive = 0;
+ − 146 Uart6Ctrl.dmaTxActive = 0;
+ − 147 Uart6Ctrl.pRxBuffer = rxBufferUart6;
+ − 148 Uart6Ctrl.pTxBuffer = txBufferUart6;
+ − 149 Uart6Ctrl.rxReadIndex = 0;
+ − 150 Uart6Ctrl.rxWriteIndex = 0;
+ − 151 Uart6Ctrl.txBufferQueLen = 0;
931
+ − 152
932
+ − 153 UART_SetGnssCtrl(&Uart6Ctrl);
922
+ − 154 }
+ − 155
+ − 156 void UART6_HandleUART()
+ − 157 {
+ − 158 static uint8_t retryRequest = 0;
+ − 159 static uint32_t lastRequestTick = 0;
+ − 160 static uint32_t TriggerTick = 0;
939
+ − 161 static uint16_t timeToTrigger = 0;
922
+ − 162 uint32_t tick = HAL_GetTick();
+ − 163
932
+ − 164 uartGnssStatus_t gnssState = uartGnss_GetState();
+ − 165
922
+ − 166 if(gnssState != UART_GNSS_INIT)
+ − 167 {
932
+ − 168 UART_ReadData(SENSOR_GNSS);
+ − 169 UART_WriteData(&Uart6Ctrl);
922
+ − 170 }
+ − 171 if(gnssState == UART_GNSS_INIT)
+ − 172 {
+ − 173 lastRequestTick = tick;
+ − 174 TriggerTick = tick - 10; /* just to make sure control is triggered */
+ − 175 timeToTrigger = 1;
+ − 176 retryRequest = 0;
+ − 177 }
939
+ − 178 else if((gnssState == UART_GNSS_INACTIVE) && (!uartGnss_isPowerDownRequested())) /* send dummy bytes to wakeup receiver */
+ − 179 {
+ − 180 txBufferUart6[0] = 0xFF;
+ − 181 txBufferUart6[1] = 0xFF;
+ − 182 HAL_UART_Transmit_DMA(Uart6Ctrl.pHandle, Uart6Ctrl.pTxBuffer,2);
+ − 183 timeToTrigger = 500; /* receiver needs 500ms for wakeup */
+ − 184 lastRequestTick = tick;
+ − 185 gnssState = UART_GNSS_PWRUP;
+ − 186 uartGnss_SetState(gnssState);
+ − 187 }
922
+ − 188 else if(((retryRequest == 0) /* timeout or error */
+ − 189 && (((time_elapsed_ms(lastRequestTick,tick) > (TIMEOUT_SENSOR_ANSWER)) && (gnssState != UART_GNSS_IDLE)) /* retry if no answer after half request interval */
+ − 190 || (gnssState == UART_GNSS_ERROR))))
+ − 191 {
+ − 192 /* The channel switch will cause the sensor to respond with an error message. */
+ − 193 /* The sensor needs ~30ms to recover before he is ready to receive the next command => transmission delay needed */
+ − 194
+ − 195 TriggerTick = tick;
+ − 196 timeToTrigger = COMMAND_TX_DELAY;
+ − 197 retryRequest = 1;
+ − 198 }
+ − 199
+ − 200 else if(time_elapsed_ms(lastRequestTick,tick) > 1000) /* switch sensor and / or trigger next request */
+ − 201 {
+ − 202 lastRequestTick = tick;
+ − 203 TriggerTick = tick;
+ − 204 retryRequest = 0;
+ − 205 timeToTrigger = 1;
+ − 206
939
+ − 207 if((gnssState == UART_GNSS_GET_SAT) || (gnssState == UART_GNSS_GET_PVT) || (gnssState == UART_GNSS_PWRUP)) /* timeout */
922
+ − 208 {
+ − 209 gnssState = UART_GNSS_IDLE;
932
+ − 210 uartGnss_SetState(gnssState);
922
+ − 211 }
+ − 212 timeToTrigger = 1;
+ − 213 }
+ − 214 if((timeToTrigger != 0) && (time_elapsed_ms(TriggerTick,tick) > timeToTrigger))
+ − 215 {
+ − 216 timeToTrigger = 0;
932
+ − 217 uartGnss_Control();
922
+ − 218 }
+ − 219
+ − 220 }
+ − 221
+ − 222
+ − 223 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/