comparison Small_CPU/Src/externalInterface.c @ 921:eb4109d7d1e9 Evo_2_23

Improved mix mode ADC conversion: Activity of UART sensors may have an impact to ADC measurement. To avoid the ADC trigger was moved into a time window ~300ms after last UART command request => After UART sensor performed measurement but before next measurement is requested. In addition the general ADC measurement cycle has been changed to one second to avoid jitter in the value updates on the display.
author Ideenmodellierer
date Sun, 03 Nov 2024 18:19:51 +0100
parents f72613a152dd
children effadaa3a1f7
comparison
equal deleted inserted replaced
920:c4c9850a2039 921:eb4109d7d1e9
38 #include "uartProtocol_GNSS.h" 38 #include "uartProtocol_GNSS.h"
39 39
40 extern SGlobal global; 40 extern SGlobal global;
41 extern UART_HandleTypeDef huart1; 41 extern UART_HandleTypeDef huart1;
42 42
43 #define ADC_ANSWER_LENGTH (5u) /* 3424 will provide addr + 4 data bytes */ 43 #define ADC_ANSWER_LENGTH (5u) /* 3424 will provide addr + 4 data bytes */
44 #define ADC_TIMEOUT (10u) /* conversion stuck for unknown reason => restart */ 44 #define ADC_TIMEOUT (10u) /* conversion stuck for unknown reason => restart */
45 #define ADC_REF_VOLTAGE_MV (2048.0f) /* reference voltage of MPC3424*/ 45 #define ADC_REF_VOLTAGE_MV (2048.0f) /* reference voltage of MPC3424*/
46 #define ADC_CYCLE_INTERVAL_MS (1000u) /* start adc read out once per second*/
46 47
47 #define ADC_START_CONVERSION (0x80) 48 #define ADC_START_CONVERSION (0x80)
48 #define ADC_GAIN_4 (0x02) 49 #define ADC_GAIN_4 (0x02)
49 #define ADC_GAIN_4_VALUE (4.0f) 50 #define ADC_GAIN_4_VALUE (4.0f)
50 #define ADC_GAIN_8 (0x03) 51 #define ADC_GAIN_8 (0x03)
68 static uint8_t activeChannel = 0; /* channel which is in request */ 69 static uint8_t activeChannel = 0; /* channel which is in request */
69 static uint8_t recBuf[ADC_ANSWER_LENGTH]; 70 static uint8_t recBuf[ADC_ANSWER_LENGTH];
70 static uint8_t timeoutCnt = 0; 71 static uint8_t timeoutCnt = 0;
71 static uint8_t externalInterfacePresent = 0; 72 static uint8_t externalInterfacePresent = 0;
72 static uint8_t delayAdcConversion = 0; 73 static uint8_t delayAdcConversion = 0;
74 static uint32_t startTickADC = 0;
73 75
74 float externalChannel_mV[MAX_ADC_CHANNEL]; 76 float externalChannel_mV[MAX_ADC_CHANNEL];
75 static uint8_t externalV33_On = 0; 77 static uint8_t externalV33_On = 0;
76 static uint8_t externalADC_On = 0; 78 static uint8_t externalADC_On = 0;
77 static uint8_t externalUART_Protocol = 0; 79 static uint8_t externalUART_Protocol = 0;
169 uint8_t nextChannel; 171 uint8_t nextChannel;
170 uint8_t* psensorMap = externalInterface_GetSensorMapPointer(0); 172 uint8_t* psensorMap = externalInterface_GetSensorMapPointer(0);
171 173
172 if(externalADC_On) 174 if(externalADC_On)
173 { 175 {
174 if(delayAdcConversion) 176 if(time_elapsed_ms(startTickADC, HAL_GetTick()) > ADC_CYCLE_INTERVAL_MS)
175 { 177 {
176 if(UART_isComActive(activeUartChannel) == 0) 178 if(delayAdcConversion)
177 { 179 {
178 externalInterface_StartConversion(activeChannel); 180 if(UART_isComActive(activeUartChannel) == 0)
179 delayAdcConversion = 0;
180 }
181 }
182 else if(I2C_Master_Receive(DEVICE_EXTERNAL_ADC, recBuf, ADC_ANSWER_LENGTH) == HAL_OK)
183 {
184 if((recBuf[ANSWER_CONFBYTE_INDEX] & ADC_START_CONVERSION) == 0) /* !ready set => received data contains new value */
185 {
186 retval = activeChannel; /* return channel number providing new data */
187 nextChannel = activeChannel + 1;
188 if(nextChannel == MAX_ADC_CHANNEL)
189 { 181 {
190 nextChannel = 0; 182 externalInterface_StartConversion(activeChannel);
183 delayAdcConversion = 0;
191 } 184 }
192 185 }
193 while((psensorMap[nextChannel] != SENSOR_ANALOG) && (nextChannel != activeChannel)) 186 else if(I2C_Master_Receive(DEVICE_EXTERNAL_ADC, recBuf, ADC_ANSWER_LENGTH) == HAL_OK)
187 {
188 if((recBuf[ANSWER_CONFBYTE_INDEX] & ADC_START_CONVERSION) == 0) /* !ready set => received data contains new value */
194 { 189 {
190 retval = activeChannel; /* return channel number providing new data */
191 nextChannel = activeChannel + 1;
195 if(nextChannel == MAX_ADC_CHANNEL) 192 if(nextChannel == MAX_ADC_CHANNEL)
196 { 193 {
197 nextChannel = 0; 194 nextChannel = 0;
198 } 195 }
196
197 while((psensorMap[nextChannel] != SENSOR_ANALOG) && (nextChannel != activeChannel))
198 {
199 if(nextChannel == MAX_ADC_CHANNEL)
200 {
201 nextChannel = 0;
202 startTickADC = HAL_GetTick();
203 }
204 else
205 {
206 nextChannel++;
207 }
208 }
209
210 activeChannel = nextChannel;
211 if(activeChannel == 0)
212 {
213 delayAdcConversion = 1; /* wait for next cycle interval */
214 }
199 else 215 else
200 { 216 {
201 nextChannel++; 217 if(UART_isComActive(activeUartChannel) == 0)
218 {
219 externalInterface_StartConversion(activeChannel);
220 }
221 else
222 {
223 delayAdcConversion = 1;
224 }
202 } 225 }
226 timeoutCnt = 0;
203 } 227 }
204 228 }
205 activeChannel = nextChannel; 229
206 if(UART_isComActive(activeUartChannel) == 0)
207 {
208 externalInterface_StartConversion(activeChannel);
209 }
210 else
211 {
212 delayAdcConversion = 1;
213 }
214 timeoutCnt = 0;
215 }
216 }
217 if(timeoutCnt++ >= ADC_TIMEOUT) 230 if(timeoutCnt++ >= ADC_TIMEOUT)
218 { 231 {
219 externalInterface_StartConversion(activeChannel); 232 externalInterface_StartConversion(activeChannel);
220 delayAdcConversion = 0; 233 delayAdcConversion = 0;
221 timeoutCnt = 0; 234 timeoutCnt = 0;
235 }
222 } 236 }
223 } 237 }
224 return retval; 238 return retval;
225 } 239 }
226 float externalInterface_CalculateADCValue(uint8_t channel) 240 float externalInterface_CalculateADCValue(uint8_t channel)
350 uint8_t loop = 0; 364 uint8_t loop = 0;
351 if((state) && (externalInterfacePresent)) 365 if((state) && (externalInterfacePresent))
352 { 366 {
353 if(externalADC_On == 0) 367 if(externalADC_On == 0)
354 { 368 {
369 startTickADC = HAL_GetTick();
355 activeChannel = 0; 370 activeChannel = 0;
356 externalInterface_StartConversion(activeChannel); 371 externalInterface_StartConversion(activeChannel);
357 externalADC_On = 1; 372 externalADC_On = 1;
358 } 373 }
359 } 374 }
1225 #endif 1240 #endif
1226 default: 1241 default:
1227 break; 1242 break;
1228 } 1243 }
1229 } 1244 }
1230 } 1245 #if 0
1231 1246 else
1232 1247 {
1248 if(((time_elapsed_ms(lastRequestTick,tick) > (externalInterfaceMuxReqIntervall - 100))
1249 && externalInterface_SensorState[activeSensorId] = UART_COMMON_IDLE))
1250 {
1251 externalInterface_ReadAndSwitch();
1252 }
1253 }
1254 #endif
1255 }
1233 1256
1234 #if 0 1257 #if 0
1235 #ifdef ENABLE_SENTINEL_MODE 1258 #ifdef ENABLE_SENTINEL_MODE
1236 if(externalInterface_GetUARTProtocol() & (EXT_INTERFACE_UART_SENTINEL)) 1259 if(externalInterface_GetUARTProtocol() & (EXT_INTERFACE_UART_SENTINEL))
1237 { 1260 {
1238 UART_HandleSentinelData(); 1261 UART_HandleSentinelData();
1239 } 1262 }
1240 #endif 1263 #endif
1241 #endif 1264 #endif
1242 1265 }
1243
1244 }