comparison Small_CPU/Src/uartProtocol_O2.c @ 795:d4083ac09b5d

Moved DiveO2 sensor specific funtionality into separated file: Having the protocol functions of all sensors within one file made the code hard to ready and created aa risk of interference between the protocols. In furture every (new) sensor should be maintained in its own source file. The ols UART.c does only contain the function which are used by all UART protocols (common functions).
author Ideenmodellierer
date Mon, 31 Jul 2023 19:50:36 +0200
parents
children e9eba334b942
comparison
equal deleted inserted replaced
794:bb37d4f3e50e 795:d4083ac09b5d
1 /**
2 ******************************************************************************
3 * @file uartProtocol_O2.c
4 * @author heinrichs weikamp gmbh
5 * @version V0.0.1
6 * @date 16-Jun-2023
7 * @brief Interface functionality to external, UART based O2 sensors
8 *
9 @verbatim
10
11
12 @endverbatim
13 ******************************************************************************
14 * @attention
15 *
16 * <h2><center>&copy; COPYRIGHT(c) 2023 heinrichs weikamp</center></h2>
17 *
18 ******************************************************************************
19 */
20 /* Includes ------------------------------------------------------------------*/
21
22 #include <string.h>
23 #include "uart.h"
24 #include "uartProtocol_O2.h"
25 #include "externalInterface.h"
26
27
28 const uint8_t errorStr[] = "#ERRO";
29 static uint32_t lastReceiveTick = 0;
30 static uartO2RxState_t rxState = O2RX_IDLE;
31 static uint8_t digO2Connected = 0; /* Binary indicator if a sensor is connected or not */
32 static SSensorDataDiveO2 tmpSensorDataDiveO2; /* intermediate storage for additional sensor data */
33
34 static uint8_t activeSensor = 0;
35 static uint8_t respondErrorDetected = 0;
36
37 void uartO2_InitData()
38 {
39 digO2Connected = 0;
40 }
41
42 void uartO2_SetupCmd(uint8_t O2State, uint8_t *cmdString, uint8_t *cmdLength)
43 {
44 switch (O2State)
45 {
46 case UART_O2_CHECK: *cmdLength = snprintf((char*)cmdString, 10, "#LOGO");
47 break;
48 case UART_O2_REQ_INFO: *cmdLength = snprintf((char*)cmdString, 10, "#VERS");
49 break;
50 case UART_O2_REQ_ID: *cmdLength = snprintf((char*)cmdString, 10, "#IDNR");
51 break;
52 case UART_O2_REQ_O2: *cmdLength = snprintf((char*)cmdString, 10, "#DOXY");
53 break;
54 case UART_O2_REQ_RAW: *cmdLength = snprintf((char*)cmdString, 10, "#DRAW");
55 break;
56 default: *cmdLength = 0;
57 break;
58 }
59 if(*cmdLength != 0)
60 {
61 cmdString[*cmdLength] = 0x0D;
62 *cmdLength = *cmdLength + 1;
63 cmdString[*cmdLength] = 0x0A;
64 *cmdLength = *cmdLength + 1;
65 cmdString[*cmdLength] = 0;
66 *cmdLength = *cmdLength + 1;
67 }
68 }
69
70 static uint8_t cmdLength = 0;
71 static uint8_t cmdString[10];
72
73 void uartO2_Control(void)
74 {
75 static uint8_t lastComState = 0;
76 static uint8_t lastActiveSensor = 0xFF;
77
78 uint8_t activeSensor = externalInterface_GetActiveUartSensor();
79
80 uartO2Status_t localComState = externalInterface_GetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET);
81 externalInterface_GetSensorData(activeSensor + EXT_INTERFACE_MUX_OFFSET, (uint8_t*)&tmpSensorDataDiveO2);
82
83
84 if(lastActiveSensor != activeSensor)
85 {
86 lastActiveSensor = activeSensor;
87 if(localComState != UART_O2_ERROR)
88 {
89 lastComState = localComState;
90 }
91 else
92 {
93 lastComState = UART_O2_IDLE;
94 }
95 if(localComState == UART_O2_CHECK)
96 {
97 localComState = UART_O2_IDLE;
98 }
99 UART_FlushRxBuffer();
100 }
101
102 if(localComState == UART_O2_INIT)
103 {
104 memset((char*) &tmpSensorDataDiveO2, 0, sizeof(tmpSensorDataDiveO2));
105 externalInterface_SetSensorData(0xFF,(uint8_t*)&tmpSensorDataDiveO2);
106
107 localComState = UART_O2_CHECK;
108 lastComState = UART_O2_CHECK;
109 uartO2_SetupCmd(localComState,cmdString,&cmdLength);
110
111 rxState = O2RX_CONFIRM;
112 respondErrorDetected = 0;
113 digO2Connected = 0;
114
115 UART_StartDMA_Receiption();
116 }
117 else
118 {
119 if(localComState == UART_O2_ERROR)
120 {
121 localComState = lastComState;
122 }
123 lastComState = localComState;
124 if(localComState == UART_O2_IDLE) /* cyclic request of o2 value */
125 {
126 if((activeSensor != MAX_MUX_CHANNEL) && (tmpSensorDataDiveO2.sensorId == 0))
127 {
128 localComState = UART_O2_REQ_ID;
129 }
130 else
131 {
132 localComState = UART_O2_REQ_RAW;
133 }
134 }
135 rxState = O2RX_CONFIRM;
136 uartO2_SetupCmd(localComState,cmdString,&cmdLength);
137 UART_SendCmdString(cmdString);
138 }
139 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState);
140 }
141
142 void uartO2_ProcessData(uint8_t data)
143 {
144 static uint8_t cmdReadIndex = 0;
145 static uint8_t errorReadIndex = 0;
146 static char tmpRxBuf[30];
147 static uint8_t tmpRxIdx = 0;
148
149 uint32_t tmpO2 = 0;
150 uint32_t tmpData = 0;
151
152 uint32_t tick = HAL_GetTick();
153
154 uartO2Status_t localComState = externalInterface_GetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET);
155
156 lastReceiveTick = tick;
157 switch(rxState)
158 {
159 case O2RX_CONFIRM: if(data == '#')
160 {
161 cmdReadIndex = 0;
162 errorReadIndex = 0;
163 }
164 if(errorReadIndex < sizeof(errorStr)-1)
165 {
166 if(data == errorStr[errorReadIndex])
167 {
168 errorReadIndex++;
169 }
170 else
171 {
172 errorReadIndex = 0;
173 }
174 }
175 else
176 {
177 respondErrorDetected = 1;
178 errorReadIndex = 0;
179 if(localComState != UART_O2_IDLE)
180 {
181 localComState = UART_O2_ERROR;
182 }
183 }
184 if(data == cmdString[cmdReadIndex])
185 {
186 cmdReadIndex++;
187 if(cmdReadIndex == cmdLength - 3)
188 {
189 errorReadIndex = 0;
190 if((activeSensor == MAX_MUX_CHANNEL))
191 {
192 if(respondErrorDetected)
193 {
194 digO2Connected = 0; /* the multiplexer mirrors the incoming message and does not generate an error information => no mux connected */
195 }
196 else
197 {
198 digO2Connected = 1;
199 }
200 }
201 else /* handle sensors which should respond with an error message after channel switch */
202 {
203 digO2Connected = 1;
204 }
205 tmpRxIdx = 0;
206 memset((char*) tmpRxBuf, 0, sizeof(tmpRxBuf));
207 cmdReadIndex = 0;
208 switch (localComState)
209 {
210 case UART_O2_CHECK: localComState = UART_O2_IDLE;
211 rxState = O2RX_IDLE;
212 break;
213 case UART_O2_REQ_ID: rxState = O2RX_GETNR;
214 break;
215 case UART_O2_REQ_INFO: rxState = O2RX_GETTYPE;
216 break;
217 case UART_O2_REQ_RAW:
218 case UART_O2_REQ_O2: rxState = O2RX_GETO2;
219 break;
220 default: localComState = UART_O2_IDLE;
221 rxState = O2RX_IDLE;
222 break;
223 }
224 }
225 }
226 else
227 {
228 cmdReadIndex = 0;
229 }
230 break;
231
232 case O2RX_GETSTATUS:
233 case O2RX_GETTEMP:
234 case O2RX_GETTYPE:
235 case O2RX_GETVERSION:
236 case O2RX_GETCHANNEL:
237 case O2RX_GETSUBSENSORS:
238 case O2RX_GETO2:
239 case O2RX_GETNR:
240 case O2RX_GETDPHI:
241 case O2RX_INTENSITY:
242 case O2RX_AMBIENTLIGHT:
243 case O2RX_PRESSURE:
244 case O2RX_HUMIDITY:
245 if(data != 0x0D)
246 {
247 if(data != ' ') /* the following data entities are placed within the data stream => no need to store data at the end */
248 {
249 tmpRxBuf[tmpRxIdx++] = data;
250 }
251 else
252 {
253 if(tmpRxIdx != 0)
254 {
255 switch(rxState)
256 {
257 case O2RX_GETCHANNEL: StringToInt(tmpRxBuf,&tmpData);
258 rxState = O2RX_GETVERSION;
259 break;
260 case O2RX_GETVERSION: StringToInt(tmpRxBuf,&tmpData);
261 rxState = O2RX_GETSUBSENSORS;
262 break;
263 case O2RX_GETTYPE: StringToInt(tmpRxBuf,&tmpData);
264 rxState = O2RX_GETCHANNEL;
265 break;
266
267 case O2RX_GETO2: StringToInt(tmpRxBuf,&tmpO2);
268
269 setExternalInterfaceChannel(activeSensor + EXT_INTERFACE_MUX_OFFSET,(float)(tmpO2 / 10000.0));
270 rxState = O2RX_GETTEMP;
271 break;
272 case O2RX_GETTEMP: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.temperature);
273 rxState = O2RX_GETSTATUS;
274 break;
275 case O2RX_GETSTATUS: StringToInt(tmpRxBuf,&tmpSensorDataDiveO2.status); /* raw data cycle */
276 rxState = O2RX_GETDPHI;
277 break;
278 case O2RX_GETDPHI: /* ignored to save memory and most likly irrelevant for diver */
279 rxState = O2RX_INTENSITY;
280 break;
281 case O2RX_INTENSITY: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.intensity); /* raw data cycle */
282 rxState = O2RX_AMBIENTLIGHT;
283 break;
284 case O2RX_AMBIENTLIGHT: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.ambient); /* raw data cycle */
285 rxState = O2RX_PRESSURE;
286 break;
287 case O2RX_PRESSURE: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.pressure); /* raw data cycle */
288 rxState = O2RX_HUMIDITY;
289 break;
290 default:
291 break;
292 }
293 memset((char*) tmpRxBuf, 0, tmpRxIdx);
294 tmpRxIdx = 0;
295 }
296 }
297 }
298 else
299 { /* the following data items are the last of a sensor respond => store temporal data */
300 switch (rxState)
301 {
302 case O2RX_GETSTATUS: StringToInt(tmpRxBuf,&tmpSensorDataDiveO2.status);
303 externalInterface_SetSensorData(activeSensor + EXT_INTERFACE_MUX_OFFSET,(uint8_t*)&tmpSensorDataDiveO2);
304 localComState = UART_O2_IDLE;
305 rxState = O2RX_IDLE;
306 break;
307 case O2RX_GETSUBSENSORS: StringToInt(tmpRxBuf,&tmpData);
308 localComState = UART_O2_IDLE;
309 rxState = O2RX_IDLE;
310 break;
311 case O2RX_HUMIDITY: StringToInt(tmpRxBuf,(uint32_t*)&tmpSensorDataDiveO2.humidity); /* raw data cycle */
312 externalInterface_SetSensorData(activeSensor + EXT_INTERFACE_MUX_OFFSET,(uint8_t*)&tmpSensorDataDiveO2);
313 localComState = UART_O2_IDLE;
314 rxState = O2RX_IDLE;
315 break;
316 case O2RX_GETNR: StringToUInt64((char*)tmpRxBuf,&tmpSensorDataDiveO2.sensorId);
317 externalInterface_SetSensorData(activeSensor + EXT_INTERFACE_MUX_OFFSET,(uint8_t*)&tmpSensorDataDiveO2);
318 localComState = UART_O2_IDLE;
319 rxState = O2RX_IDLE;
320 break;
321 default: localComState = UART_O2_IDLE;
322 rxState = O2RX_IDLE;
323 break;
324 }
325 }
326 break;
327 default: rxState = O2RX_IDLE;
328 break;
329
330 }
331 externalInterface_SetSensorState(activeSensor + EXT_INTERFACE_MUX_OFFSET,localComState);
332 }
333
334
335 uint8_t uartO2_isSensorConnected()
336 {
337 return digO2Connected;
338 }
339
340 void uartO2_SetChannel(uint8_t channel)
341 {
342 if(channel <= MAX_MUX_CHANNEL)
343 {
344 activeSensor = channel;
345 }
346 }
347