Rev 20 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 20 | Rev 21 | ||
|---|---|---|---|
| Line 17... | Line 17... | ||
| 17 | */ |
17 | */ |
| 18 | /* USER CODE END Header */ |
18 | /* USER CODE END Header */ |
| 19 | /* Includes ------------------------------------------------------------------*/ |
19 | /* Includes ------------------------------------------------------------------*/ |
| 20 | #include "main.h" |
20 | #include "main.h" |
| 21 | 21 | ||
| - | 22 | // #define TEST_CODE |
|
| - | 23 | ||
| 22 | /* Private includes ----------------------------------------------------------*/ |
24 | /* Private includes ----------------------------------------------------------*/ |
| 23 | /* USER CODE BEGIN Includes */ |
25 | /* USER CODE BEGIN Includes */ |
| 24 | #include "memory.h" |
26 | #include "memory.h" |
| 25 | #include "display.h" |
27 | #include "display.h" |
| 26 | #include "bmp280driver.h" |
28 | #include "bmp280driver.h" |
| Line 79... | Line 81... | ||
| 79 | // 6 degrees error in timing wheel this time .. |
81 | // 6 degrees error in timing wheel this time .. |
| 80 | int const TIMING_OFFSET = -6 * TIMING_SCALE; |
82 | int const TIMING_OFFSET = -6 * TIMING_SCALE; |
| 81 | 83 | ||
| 82 | // Switch over to double sparking |
84 | // Switch over to double sparking |
| 83 | int const DOUBLE_SPARK_RPM = 1200; |
85 | int const DOUBLE_SPARK_RPM = 1200; |
| 84 | // default atmospheric pressure |
86 | // default atmospheric pressure - should be 1014 |
| 85 | uint32_t const DEFAULT_ATMOSPHERIC_PRESSURE = 1014 * 100; |
87 | uint32_t const DEFAULT_ATMOSPHERIC_PRESSURE = 1014 * 100; |
| 86 | 88 | ||
| 87 | uint32_t const DEFAULT_ATMOSPHERIC_TEMPERATURE = 25 * 100; |
89 | uint32_t const DEFAULT_ATMOSPHERIC_TEMPERATURE = 25 * 100; |
| 88 | 90 | ||
| 89 | // Serial buffers |
91 | // Serial buffers |
| Line 110... | Line 112... | ||
| 110 | void libPLXcallbackRecievedData(PLX_SensorInfo *info) |
112 | void libPLXcallbackRecievedData(PLX_SensorInfo *info) |
| 111 | { |
113 | { |
| 112 | (void)info; |
114 | (void)info; |
| 113 | } |
115 | } |
| 114 | 116 | ||
| 115 | void libPLXcallbackSendUserData() |
117 | void libPLXcallbackSendUserData(struct usart_ctl *handle) |
| 116 | { |
118 | { |
| 117 | // send MAP |
119 | // send MAP |
| 118 | PLX_SensorInfo info; |
120 | PLX_SensorInfo info; |
| 119 | ConvToPLXInstance(libPLXgetNextInstance(PLX_MAP), &info); |
121 | ConvToPLXInstance(libPLXgetNextInstance(PLX_MAP), &info); |
| 120 | ConvToPLXAddr(PLX_MAP, &info); |
122 | ConvToPLXAddr(PLX_MAP, &info); |
| 121 | ConvToPLXReading(ConveriMFDData2Raw(PLX_MAP, PRESSURE_kPa, (float)(compensatedManifoldPressure) / 100.0), &info); |
123 | ConvToPLXReading(ConveriMFDData2Raw(PLX_MAP, PRESSURE_kPa, (float)(compensatedManifoldPressure) / 100.0), &info); |
| 122 | sendInfo(&uc2, &info); |
124 | sendInfo(handle, &info); |
| 123 | 125 | ||
| - | 126 | // send timing |
|
| 124 | ConvToPLXInstance(libPLXgetNextInstance(PLX_Timing), &info); |
127 | ConvToPLXInstance(libPLXgetNextInstance(PLX_Timing), &info); |
| 125 | ConvToPLXAddr(PLX_Timing, &info); |
128 | ConvToPLXAddr(PLX_Timing, &info); |
| 126 | ConvToPLXReading(ConveriMFDData2Raw(PLX_Timing, 0, (float)(timing) / TIMING_SCALE), &info); |
129 | ConvToPLXReading(ConveriMFDData2Raw(PLX_Timing, 0, (float)(timing) / TIMING_SCALE), &info); |
| 127 | sendInfo(&uc2, &info); |
130 | sendInfo(handle, &info); |
| - | 131 | ||
| - | 132 | // send temperature |
|
| - | 133 | ConvToPLXInstance(libPLXgetNextInstance(PLX_AIT), &info); |
|
| - | 134 | ConvToPLXAddr(PLX_AIT, &info); |
|
| - | 135 | ConvToPLXReading(ConveriMFDData2Raw(PLX_AIT, 0, (float)(compensatedTemperature) / 100.0), &info); |
|
| - | 136 | sendInfo(handle, &info); |
|
| 128 | } |
137 | } |
| 129 | 138 | ||
| 130 | void triggerSAW() |
139 | void triggerSAW() |
| 131 | { |
140 | { |
| 132 | // trigger SAW timer, timer 1##pragma endregion |
141 | // trigger SAW timer, timer 1##pragma endregion |
| Line 206... | Line 215... | ||
| 206 | 215 | ||
| 207 | __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 5); // delay of 5 uS |
216 | __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 5); // delay of 5 uS |
| 208 | 217 | ||
| 209 | // HAL_I2C_ClearBusyFlagErrata_2_14_7(&hi2c1); |
218 | // HAL_I2C_ClearBusyFlagErrata_2_14_7(&hi2c1); |
| 210 | MX_I2C1_Init(); |
219 | MX_I2C1_Init(); |
| 211 | init_bmp(&hi2c1, &bmp); |
220 | init_bmp(&hi2c1, &bmpManifold, &confManifold); |
| 212 | init_bmp(&hi2c1, &bmpAtm); |
221 | init_bmp(&hi2c1, &bmpAtmosphere, &confAtmosphere); |
| 213 | 222 | ||
| 214 | uint32_t lastTick = HAL_GetTick(); |
223 | uint32_t nextTick = HAL_GetTick(); |
| 215 | - | ||
| 216 | uint32_t displayOff = lastTick + 10000; |
224 | uint32_t displayOff = nextTick + 10000; |
| 217 | uint32_t displayReinitialise = lastTick + DISPLAY_REINITIALISE; // every minute, reinitialise display because of risk of noise |
225 | uint32_t displayReinitialise = nextTick + DISPLAY_REINITIALISE; // every minute, reinitialise display because of risk of noise |
| 218 | 226 | ||
| 219 | uint8_t intensity = 2; |
227 | uint8_t intensity = 2; |
| 220 | 228 | ||
| 221 | ResetRxBuffer(&uc2); |
229 | ResetRxBuffer(&uc2); |
| 222 | 230 | ||
| Line 233... | Line 241... | ||
| 233 | int button = HAL_GPIO_ReadPin(PUSHBUTTON_GPIO_Port, PUSHBUTTON_Pin) == GPIO_PIN_RESET; |
241 | int button = HAL_GPIO_ReadPin(PUSHBUTTON_GPIO_Port, PUSHBUTTON_Pin) == GPIO_PIN_RESET; |
| 234 | 242 | ||
| 235 | if (button) |
243 | if (button) |
| 236 | { |
244 | { |
| 237 | intensity = 2; |
245 | intensity = 2; |
| 238 | displayOff = lastTick + 30000; |
246 | displayOff = HAL_GetTick() + 30000; |
| 239 | } |
247 | } |
| 240 | 248 | ||
| 241 | switch (intensity) |
249 | switch (intensity) |
| 242 | { |
250 | { |
| 243 | case 2: |
251 | case 2: |
| 244 | if (HAL_GetTick() > displayOff) |
252 | if (HAL_GetTick() > displayOff) |
| 245 | { |
253 | { |
| 246 | intensity = 1; |
254 | intensity = 1; |
| 247 | displayOff = lastTick + 60000; |
255 | displayOff += 60000; |
| 248 | } |
256 | } |
| 249 | 257 | ||
| 250 | break; |
258 | break; |
| 251 | case 1: |
259 | case 1: |
| 252 | if (HAL_GetTick() > displayOff) |
260 | if (HAL_GetTick() > displayOff) |
| Line 263... | Line 271... | ||
| 263 | cc_display(0, intensity, 1); |
271 | cc_display(0, intensity, 1); |
| 264 | } |
272 | } |
| 265 | else |
273 | else |
| 266 | cc_display(0, intensity, 0); |
274 | cc_display(0, intensity, 0); |
| 267 | 275 | ||
| 268 | if (HAL_GetTick() - lastTick > T100MS) |
276 | if (HAL_GetTick() > nextTick) |
| 269 | { |
277 | { |
| 270 | lastTick = HAL_GetTick(); |
278 | nextTick = HAL_GetTick() + T100MS; |
| 271 | /* Reading the raw data from sensor */ |
- | |
| 272 | struct bmp280_uncomp_data ucomp_data; |
- | |
| 273 | - | ||
| 274 | uint8_t rslt = bmp280_get_uncomp_data(&ucomp_data, &bmp); |
- | |
| 275 | - | ||
| 276 | uint8_t manifoldStatus = 1; |
279 | uint8_t manifoldStatus = 1; |
| 277 | uint8_t temperatureStatus = 1; |
280 | uint8_t temperatureStatus = 1; |
| 278 | uint8_t atmosphericStatus = 1; |
281 | uint8_t atmosphericStatus = 1; |
| - | 282 | /* Reading the raw data from manifold sensor */ |
|
| - | 283 | struct bmp280_uncomp_data ucomp_data; |
|
| - | 284 | ||
| - | 285 | uint8_t rslt = bmp280_get_uncomp_data(&ucomp_data, &bmpManifold); |
|
| 279 | 286 | ||
| 280 | if (rslt == BMP280_OK) |
287 | if (rslt == BMP280_OK) |
| 281 | { |
288 | { |
| 282 | manifoldStatus = bmp280_get_comp_pres_32bit(&compensatedManifoldPressure, ucomp_data.uncomp_press, &bmp); |
289 | // always calculate temperature then pressure so the pressure is correctly calibrated |
| 283 | - | ||
| 284 | temperatureStatus = bmp280_get_comp_temp_32bit(&compensatedTemperature, ucomp_data.uncomp_temp, &bmp); |
290 | temperatureStatus = bmp280_get_comp_temp_32bit(&compensatedTemperature, ucomp_data.uncomp_temp, &bmpManifold); |
| 285 | - | ||
| 286 | #if defined TEST_CODE |
- | |
| 287 | compensatedManifoldPressure = 100000; |
291 | manifoldStatus = bmp280_get_comp_pres_32bit(&compensatedManifoldPressure, ucomp_data.uncomp_press, &bmpManifold); |
| 288 | compensatedTemperature = 4000; |
- | |
| 289 | #endif |
- | |
| 290 | } |
292 | } |
| 291 | // get atmospheric data |
293 | // get atmospheric data |
| - | 294 | rslt = bmp280_get_uncomp_data(&ucomp_data, &bmpAtmosphere); |
|
| - | 295 | // now to read the environmental pressure |
|
| 292 | if (rslt == BMP280_OK) |
296 | if (rslt == BMP280_OK) |
| 293 | { |
297 | { |
| 294 | rslt = bmp280_get_uncomp_data(&ucomp_data, &bmpAtm); |
298 | // always calculate temperature then pressure so the pressure is correctly calibrated |
| 295 | // now to read the environmental pressure |
299 | temperatureStatus = bmp280_get_comp_temp_32bit(&compensatedTemperature, ucomp_data.uncomp_temp, &bmpAtmosphere); |
| 296 | if (rslt == BMP280_OK) |
- | |
| 297 | atmosphericStatus = bmp280_get_comp_pres_32bit(&compensatedAtmosphericPressure, ucomp_data.uncomp_press, &bmpAtm); |
300 | atmosphericStatus = bmp280_get_comp_pres_32bit(&compensatedAtmosphericPressure, ucomp_data.uncomp_press, &bmpAtmosphere); |
| 298 | } |
301 | } |
| 299 | if (manifoldStatus != BMP280_OK) |
302 | if (manifoldStatus != BMP280_OK) |
| 300 | compensatedManifoldPressure = DEFAULT_ATMOSPHERIC_PRESSURE; |
303 | compensatedManifoldPressure = DEFAULT_ATMOSPHERIC_PRESSURE; |
| 301 | if (temperatureStatus != BMP280_OK) |
304 | if (temperatureStatus != BMP280_OK) |
| 302 | compensatedTemperature = DEFAULT_ATMOSPHERIC_TEMPERATURE; |
305 | compensatedTemperature = DEFAULT_ATMOSPHERIC_TEMPERATURE; |
| 303 | if (atmosphericStatus != BMP280_OK) |
306 | if (atmosphericStatus != BMP280_OK) |
| 304 | compensatedAtmosphericPressure = DEFAULT_ATMOSPHERIC_PRESSURE; |
307 | compensatedAtmosphericPressure = DEFAULT_ATMOSPHERIC_PRESSURE; |
| - | 308 | #if defined TEST_CODE |
|
| - | 309 | compensatedManifoldPressure = 100000; |
|
| - | 310 | compensatedTemperature = 4000; |
|
| - | 311 | #endif |
|
| 305 | 312 | ||
| 306 | uint32_t vacuum = compensatedAtmosphericPressure - compensatedManifoldPressure; |
313 | int32_t vacuum = compensatedAtmosphericPressure - compensatedManifoldPressure; |
| - | 314 | if (vacuum < 0) |
|
| - | 315 | vacuum = 0; |
|
| 307 | // if the BMP280 pressure is good, then allow it through, otherwise drop to |
316 | // if the BMP280 pressure is good, then allow it through, otherwise drop to |
| 308 | // centrifugal advance only. |
317 | // centrifugal advance only. |
| 309 | // feed difference and add default pressure |
318 | // feed difference and add default pressure |
| 310 | cc_feed_env(vacuum + DEFAULT_ATMOSPHERIC_PRESSURE, compensatedTemperature); |
319 | cc_feed_env(compensatedAtmosphericPressure, 100000 - vacuum, compensatedTemperature); |
| 311 | 320 | ||
| 312 | // compute RPM value, feed to display |
321 | // compute RPM value, feed to display |
| 313 | #if defined TEST_CODE |
322 | #if defined TEST_CODE |
| 314 | int rpm = 1000; |
323 | int rpm = 1000; |
| 315 | #else |
324 | #else |
| Line 317... | Line 326... | ||
| 317 | #endif |
326 | #endif |
| 318 | if (rpm > 0) |
327 | if (rpm > 0) |
| 319 | { |
328 | { |
| 320 | cc_feed_rpm(rpm); |
329 | cc_feed_rpm(rpm); |
| 321 | // compute timing value, feed to display |
330 | // compute timing value, feed to display |
| 322 | timing = mapTiming(rpm, 1000 - vacuum / 100); |
331 | timing = mapTiming(rpm, vacuum / 100); |
| 323 | cc_feed_timing(timing); |
332 | cc_feed_timing(timing); |
| 324 | // enable double spark below 1200 rpm |
333 | // enable double spark below 1200 rpm |
| 325 | int microsecs = mapTimingToMicroseconds(timing + TIMING_OFFSET, rpm > DOUBLE_SPARK_RPM); |
334 | int microsecs = mapTimingToMicroseconds(timing + TIMING_OFFSET, rpm < DOUBLE_SPARK_RPM); |
| 326 | __HAL_TIM_SET_AUTORELOAD(&htim1, microsecs + SAW_DELAY); |
335 | __HAL_TIM_SET_AUTORELOAD(&htim1, microsecs + SAW_DELAY); |
| 327 | } |
336 | } |
| 328 | } |
337 | } |
| 329 | 338 | ||
| 330 | // Handle PLX |
339 | // Handle PLX |