38
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file uart.c
|
|
4 * @author heinrichs weikamp gmbh
|
|
5 * @version V0.0.1
|
|
6 * @date 27-March-2014
|
|
7 * @brief button control
|
|
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"
|
662
|
23 #include "externalInterface.h"
|
|
24 #include "data_exchange.h"
|
38
|
25
|
|
26 /* Private variables ---------------------------------------------------------*/
|
|
27
|
662
|
28 #define CHUNK_SIZE (20u) /* the DMA will handle chunk size transfers */
|
|
29 #define CHUNKS_PER_BUFFER (3u)
|
|
30 UART_HandleTypeDef huart1;
|
|
31
|
|
32 DMA_HandleTypeDef hdma_usart1_rx;
|
38
|
33
|
662
|
34 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */
|
|
35 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */
|
|
36 static uint8_t rxReadIndex; /* Index at which new data is stared */
|
|
37 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */
|
|
38 static uint8_t dmaActive; /* Indicator if DMA receiption needs to be started */
|
38
|
39
|
662
|
40 float LED_Level = 0.0; /* Normalized LED value which may be used as indication for the health status of the sensor */
|
|
41 float LED_ZeroOffset = 0.0;
|
|
42 float pCO2 = 0.0;
|
38
|
43 /* Exported functions --------------------------------------------------------*/
|
|
44
|
662
|
45 void MX_USART1_UART_Init(void)
|
38
|
46 {
|
662
|
47 /* regular init */
|
|
48
|
|
49 huart1.Instance = USART1;
|
|
50 huart1.Init.BaudRate = 9600;
|
|
51 huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
52 huart1.Init.StopBits = UART_STOPBITS_1;
|
|
53 huart1.Init.Parity = UART_PARITY_NONE;
|
|
54 huart1.Init.Mode = UART_MODE_TX_RX;
|
|
55 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
56 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
57
|
|
58 HAL_UART_Init(&huart1);
|
|
59
|
|
60 rxReadIndex = 0;
|
|
61 lastCmdIndex = 0;
|
|
62 rxWriteIndex = 0;
|
|
63 dmaActive = 0;
|
|
64 }
|
38
|
65
|
662
|
66 void MX_USART1_UART_DeInit(void)
|
|
67 {
|
|
68 HAL_DMA_DeInit(&hdma_usart1_rx);
|
|
69 HAL_UART_DeInit(&huart1);
|
|
70 }
|
|
71
|
|
72 void MX_USART1_DMA_Init()
|
|
73 {
|
|
74 /* DMA controller clock enable */
|
|
75 __DMA2_CLK_ENABLE();
|
|
76
|
|
77 /* Peripheral DMA init*/
|
|
78 hdma_usart1_rx.Instance = DMA2_Stream5;
|
|
79 hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
|
|
80 hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; //DMA_MEMORY_TO_PERIPH;
|
|
81 hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
82 hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
83 hdma_usart1_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
84 hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
85 hdma_usart1_rx.Init.Mode = DMA_NORMAL;
|
|
86 hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
87 hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
88 HAL_DMA_Init(&hdma_usart1_rx);
|
|
89
|
|
90 __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx);
|
|
91
|
|
92 /* DMA interrupt init */
|
|
93 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
|
|
94 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
|
38
|
95 }
|
|
96
|
|
97
|
662
|
98 uint32_t dataValue = 0;
|
|
99
|
|
100 void HandleUARTData(void)
|
38
|
101 {
|
662
|
102 uint8_t localRX = rxReadIndex;
|
|
103 uint8_t dataType = 0;
|
|
104 static receiveState_t rxState = RX_Ready;
|
|
105 static uint32_t lastReceiveTick = 0;
|
|
106
|
|
107 while(localRX != rxWriteIndex)
|
|
108 {
|
|
109 lastReceiveTick = HAL_GetTick();
|
|
110 if(rxState == RX_Ready) /* identify data content */
|
|
111 {
|
|
112 switch(rxBuffer[localRX])
|
|
113 {
|
|
114 case 'l':
|
|
115 case 'D':
|
|
116 case 'Z':
|
|
117 dataType = rxBuffer[localRX];
|
|
118 rxState = RX_Data0;
|
|
119 dataValue = 0;
|
|
120 break;
|
|
121
|
|
122 default: /* unknown or corrupted => ignore */
|
|
123 break;
|
|
124 }
|
|
125 }
|
|
126 else if((rxState >= RX_Data0) && (rxState <= RX_Data4))
|
|
127 {
|
|
128 if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
|
|
129 {
|
|
130 dataValue = dataValue * 10 + (rxBuffer[localRX] - '0');
|
|
131 rxState++;
|
|
132 }
|
|
133 }
|
|
134 if((rxBuffer[localRX] == ' ') || (rxBuffer[localRX] == '\n')) /* Abort data detection */
|
|
135 {
|
|
136 if(rxState == RX_DataComplete)
|
|
137 {
|
|
138 if(externalInterface_GetCO2State() == 0)
|
|
139 {
|
|
140 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);
|
|
141 }
|
|
142 switch(dataType)
|
|
143 {
|
|
144 case 'D': externalInterface_SetCO2SignalStrength(dataValue);
|
|
145 break;
|
|
146 case 'l': LED_ZeroOffset = dataValue;
|
|
147 break;
|
|
148 case 'Z': externalInterface_SetCO2Value(dataValue);
|
|
149 break;
|
|
150 default: break;
|
|
151 }
|
|
152 }
|
|
153 if(rxState != RX_Data0) /* reset state machine because message in wrong format */
|
|
154 {
|
|
155 rxState = RX_Ready;
|
|
156 }
|
|
157 }
|
|
158
|
|
159 localRX++;
|
|
160 rxReadIndex++;
|
|
161 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
|
|
162 {
|
|
163 localRX = 0;
|
|
164 rxReadIndex = 0;
|
|
165 }
|
|
166 }
|
|
167
|
|
168 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 2000) /* check for communication timeout */
|
|
169 {
|
|
170 externalInterface_SetCO2State(0);
|
|
171 }
|
|
172
|
|
173 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */
|
|
174 {
|
|
175 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
|
|
176 {
|
|
177 dmaActive = 1;
|
|
178 }
|
|
179 }
|
38
|
180 }
|
|
181
|
662
|
182 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
38
|
183 {
|
662
|
184 if(huart == &huart1)
|
|
185 {
|
|
186 dmaActive = 0;
|
|
187 rxWriteIndex+=CHUNK_SIZE;
|
|
188 if(rxWriteIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
|
|
189 {
|
|
190 rxWriteIndex = 0;
|
|
191 }
|
|
192 if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE)) /* start next transfer if we did not catch up with read index */
|
|
193 {
|
|
194 if(externalInterface_isEnabledPower33())
|
|
195 {
|
|
196 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
|
|
197 {
|
|
198 dmaActive = 1;
|
|
199 }
|
|
200 }
|
|
201 }
|
|
202 }
|
38
|
203 }
|
|
204
|
|
205
|
662
|
206
|
|
207
|
|
208
|
|
209
|
|
210
|
|
211
|
38
|
212 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/
|