Mercurial > public > ostc4
annotate Small_CPU/Src/baseCPU2.c @ 919:c0553dd70608 Evo_2_23
GNSS support for external UART interface:
An ubox gps module may now be connected to the external UART. Per default the functionality is disabled using the compile switch ENABLE_GNSS_SUPPORT
author | Ideenmodellierer |
---|---|
date | Sun, 03 Nov 2024 15:43:04 +0100 |
parents | 0d6c4b40fae4 |
children | 7c996354b8ac |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @copyright heinrichs weikamp | |
4 * @file base.c including main() | |
5 * @author heinrichs weikamp gmbh | |
6 * @date 15-Aug-2014 | |
7 * @version V1.0.3 | |
8 * @since 21-Nov-2014 | |
9 * @brief The beginning of it all. main() is part of this. | |
10 * + Do the inits for hardware | |
11 * + Do the inits for sub-systems like menu, dive screen etc. | |
12 * + Start IRQs | |
13 * + Start MainTasks not in IRQs | |
14 * @bug | |
15 * @warning | |
16 @verbatim | |
17 ============================================================================== | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
18 ##### What about hardware without 8 MHz oscillator ##### |
38 | 19 ============================================================================== |
20 [..] modify OTP Byte 1 at 0x1FFF7800 with ST-Link utility | |
21 | |
22 ============================================================================== | |
23 ##### Where is the RTE Firmware version ##### | |
24 ============================================================================== | |
25 [..] in baseCPU2.c <just here below :-> | |
26 | |
27 ============================================================================== | |
28 ##### What to do with the RTE Firmware version ##### | |
29 ============================================================================== | |
30 [..] change the values RTErequiredHigh and RTErequiredLow in settings.c | |
31 to start warning via the firmware if not updated | |
32 | |
33 ============================================================================== | |
34 ##### What it does ##### | |
35 ============================================================================== | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
36 [..] All realtime stuff and all what has to be done during sleep |
38 | 37 |
38 [..] RealTimeClock. The entire time and date handling (including divetime) | |
39 [..] Hardware control for pressure sensor, compass, battery monitor | |
40 [..] Calculations of tissue load, critical radius, otu, cns | |
41 [..] Switching off the power of the main CPU after request from it. | |
42 | |
43 ============================================================================== | |
44 ##### IRQs ##### | |
45 ============================================================================== | |
46 [..] The IRQs are are only used for SystemTick and SPI TransferComplete after | |
47 DMA data reception. | |
48 | |
49 [..] HAL_SPI_TxRxCpltCallback() restarts DMA and will call | |
50 scheduleSpecial_Evaluate_DataSendToSlave() only if it is not blocked | |
239
e4207f0aaa4b
cleanup: factor out dataSendToSlaveStopEval
Jan Mulder <jlmulder@xs4all.nl>
parents:
207
diff
changeset
|
51 by I2C. |
38 | 52 If the evaluation is blocked it has to be tested and executed afterwards. |
53 I2C is executed _without_ the usage of interrupts. | |
54 | |
55 ============================================================================== | |
56 ##### Main loop ##### | |
57 ============================================================================== | |
58 [..] is a combination of the while loop below in main.c and code in scheduler.c | |
59 It is similar to the DR5 code / logic - in contrast to the main CPU | |
60 Switching the state is done via global.mode | |
61 The loops in scheduler all run in the main execution thread without | |
62 any job stacks (like it was in the DR5). | |
63 | |
64 ============================================================================== | |
65 ##### Real Time Clock ##### | |
66 ============================================================================== | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
67 The RTC is a separate part of hardware inside the CPU and is not affected |
38 | 68 by reset. Only power-on reset does change something. |
69 This is fine but the RTC is vital for the Sleep mode as Wakeuptimer. | |
70 This is the only date/time system in the OSTC. The main CPU is passive. | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
71 Data transfer is done with localtime_rtc_tr and localtime_rtc_dr |
38 | 72 in HAL_RTC format to the main CPU and as HAL_RTC structs the way back for |
73 setting the actual time and date. | |
74 The RTC unit has 20 Byte of V_bat powered SRAM. It could be used | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
75 for something useful in both CPUs. |
38 | 76 |
77 ============================================================================== | |
78 ##### File system ##### | |
79 ============================================================================== | |
80 [..] some files are used for both CPUs, like decom.c/.h, data_central.h, ... | |
81 | |
82 | |
83 ============================================================================== | |
84 ##### Unique device ID register (96 bits) ##### | |
85 ============================================================================== | |
86 [..] some files are used for both CPUs, like decom.c/.h, data_central.h, ... | |
87 | |
88 | |
89 ============================================================================== | |
90 ##### I2C ##### | |
91 ============================================================================== | |
92 [..] used for pressure, compass, (accelerator) and battery gauge | |
93 main cpu and pic (button) is spi | |
94 | |
95 | |
96 ============================================================================== | |
97 ##### Firmware Update Info ##### | |
98 ============================================================================== | |
99 V0.85 160531 scheduleCheck_pressure_reached_dive_mode_level() changes | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
100 160606 global.no_fly_time_minutes is at least 24h after the dive |
38 | 101 160613 ambient light fixed |
102 160720 compass calib to Flash (8000 writes max. as erase has problems) | |
103 160829 do not reset main CPU on power on! | |
104 V0.91 161018 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); | |
105 V0.92+ 161020 global.sensorError[MAX_SENSORS] | |
106 fix missing init_pressure(); at powerUp of RTE | |
107 added HAL_StatusTypeDef for many functions in pressure.c | |
108 161024 no_fly_time_minutes Backup FIX | |
109 seconds_since_last_dive now related to RTC clock | |
110 161121 close to surface starts at 1 meter below last known surface pressure | |
286
733877ab87d9
Minor: Some Texts fixed, allign RTErequired* values with RTE firmware
heinrichsweikamp
parents:
280
diff
changeset
|
111 161121 in surface mode dive mode starts @1 mtr difference if surface 880 hPa instead of 700 hPa before |
38 | 112 V0.97+ 170213 added global.dataSendToSlave.diveModeInfo for DIVEMODE_Apnea |
113 added global.dataSendToSlave.setEndDive | |
114 DIVEMODE_Apnea special in scheduler.c (ticksdiff >= 1000) -> no tissue, cns, otu, no change in noFly Time etc. | |
115 V0.99 170320 new HAL Driver Repository | |
116 V1.01 170509 old HAL Driver Repository | |
117 | |
118 @endverbatim | |
119 ****************************************************************************** | |
120 * @attention | |
121 * | |
122 * <h2><center>© COPYRIGHT(c) 2017 heinrichs weikamp</center></h2> | |
123 * | |
124 ****************************************************************************** | |
125 */ | |
126 | |
127 //#define DEBUG_PIN_ACTIVE | |
128 /* Includes ------------------------------------------------------------------*/ | |
129 | |
130 #include "baseCPU2.h" | |
131 | |
132 // From Small_CPU/Inc: | |
133 #include "dma.h" | |
134 #include "i2c.h" | |
135 #include "spi.h" | |
136 #include "rtc.h" | |
137 #include "adc.h" | |
138 #include "compass.h" | |
139 #include "pressure.h" | |
140 #include "batteryGasGauge.h" | |
141 #include "batteryCharger.h" | |
142 #include "scheduler.h" | |
143 #include "tm_stm32f4_otp.h" | |
554
3328189786e7
Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
533
diff
changeset
|
144 #include "externalInterface.h" |
662 | 145 #include "uart.h" |
889 | 146 #include "GNSS.h" |
147 | |
38 | 148 |
149 // From Common/Inc: | |
150 #include "calc_crush.h" | |
151 #include "decom.h" | |
152 #include "FirmwareData.h" | |
153 | |
154 // From Common/Drivers/ | |
155 #include <stdio.h> | |
156 | |
889 | 157 |
433 | 158 uint8_t coldstart __attribute__((section (".noinit"))); |
159 | |
89 | 160 uint8_t hasExternalClock(void) { |
161 if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF)) | |
162 return 1; | |
163 else | |
164 return 0; | |
38 | 165 } |
166 | |
167 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000. | |
168 // See CPU2-RTE.ld | |
100 | 169 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") ))= { |
728
5143e927219f
Added sensor map to Firmware <=> RTE inferface:
Ideenmodellierer
parents:
713
diff
changeset
|
170 .versionFirst = 3, |
899
2225c467f1e9
Added data path and visualization for position data:
Ideenmodellierer
parents:
890
diff
changeset
|
171 .versionSecond = 4, |
762 | 172 .versionThird = 0, |
843 | 173 .versionBeta = 1, |
38 | 174 |
89 | 175 /* 4 bytes with trailing 0 */ |
100 | 176 .signature = "mh", |
38 | 177 |
762 | 178 .release_year = 23, |
818 | 179 .release_month = 9, |
180 .release_day = 14, | |
100 | 181 .release_sub = 0, |
38 | 182 |
89 | 183 /* max 48 with trailing 0 */ |
184 //release_info ="12345678901234567890123456789012345678901" | |
768 | 185 .release_info = "stable April 2023", |
38 | 186 |
89 | 187 /* for safety reasons and coming functions */ |
188 .magic[0] = FIRMWARE_MAGIC_FIRST, .magic[1] = FIRMWARE_MAGIC_SECOND, | |
189 .magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */ | |
433 | 190 .magic[3] = FIRMWARE_MAGIC_END |
191 }; | |
192 | |
38 | 193 |
89 | 194 uint8_t firmwareVersionHigh(void) { |
195 return cpu2_FirmwareData.versionFirst; | |
38 | 196 } |
197 | |
89 | 198 uint8_t firmwareVersionLow(void) { |
199 return cpu2_FirmwareData.versionSecond; | |
38 | 200 } |
201 | |
202 /** @addtogroup OSTC4 | |
203 * @{ | |
204 */ | |
205 | |
206 /** @addtogroup CPU2 | |
207 * @{ | |
208 */ | |
209 | |
210 /* Private typedef -----------------------------------------------------------*/ | |
211 /* Private define ------------------------------------------------------------*/ | |
212 #define BUTTON_OSTC_GPIO_PIN GPIO_PIN_0 | |
213 #define BUTTON_OSTC_GPIO_PORT GPIOA | |
214 #define BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() | |
215 #define BUTTON_OSTC_IRQn EXTI0_IRQn | |
216 | |
880 | 217 #define VIBRATION_CONTROL_PIN GPIO_PIN_3 /* PortA */ |
218 #define LED_CONTROL_PIN_RED GPIO_PIN_2 /* PortA */ | |
219 #define LED_CONTROL_PIN_GREEN GPIO_PIN_1 /* PortA */ | |
662 | 220 #define MAINCPU_CONTROL_PIN GPIO_PIN_0 /* PortC */ |
887 | 221 #define GPS_POWER_CONTROL_PIN GPIO_PIN_15 /* PortB */ |
222 #define GPS_BCKP_CONTROL_PIN GPIO_PIN_14 /* PortB */ | |
662 | 223 |
38 | 224 /* Private macro -------------------------------------------------------------*/ |
225 | |
226 /* Private variables ---------------------------------------------------------*/ | |
227 uint32_t global_test_time_counter = 0; | |
228 SBackup backup; | |
229 | |
230 /* Private function prototypes -----------------------------------------------*/ | |
231 static void EXTI_Wakeup_Button_Init(void); | |
232 static void EXTI_Wakeup_Button_DeInit(void); | |
233 | |
880 | 234 static void GPIO_LEDs_VIBRATION_Init(void); |
38 | 235 static void GPIO_Power_MainCPU_Init(void); |
236 static void GPIO_Power_MainCPU_ON(void); | |
237 static void GPIO_Power_MainCPU_OFF(void); | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
238 #ifdef ENABLE_GNSS |
880 | 239 static void GPIO_LED_RED_OFF(void); |
240 static void GPIO_LED_RED_ON(void); | |
241 static void GPIO_LED_GREEN_OFF(void); | |
242 static void GPIO_LED_GREEN_ON(void); | |
243 static void GPIO_VIBRATION_OFF(void); | |
244 static void GPIO_VIBRATION_ON(void); | |
887 | 245 static void GPIO_GPS_OFF(void); |
246 static void GPIO_GPS_ON(void); | |
247 static void GPIO_GPS_BCKP_OFF(void); | |
248 static void GPIO_GPS_BCKP_ON(void); | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
249 #endif |
183 | 250 #ifdef DEBUG_I2C_LINES |
38 | 251 void GPIO_test_I2C_lines(void); |
183 | 252 #endif |
38 | 253 |
254 void sleep_prepare(void); | |
255 | |
256 void SystemClock_Config(void); | |
257 void SystemClock_Config_HSI(void); | |
258 void SystemClock_Config_HSE(void); | |
259 void SYSCLKConfig_STOP_HSI(void); | |
260 void SYSCLKConfig_STOP_HSE(void); | |
261 | |
262 void GPIO_new_DEBUG_Init(void); | |
263 void GPIO_new_DEBUG_LOW(void); | |
264 void GPIO_new_DEBUG_HIGH(void); | |
265 | |
266 #define REGULAR_RUN | |
267 | |
89 | 268 int __io_putchar(int ch) { |
269 ITM_SendChar(ch); | |
270 return ch; | |
38 | 271 } |
272 | |
273 /* Private functions ---------------------------------------------------------*/ | |
274 | |
275 /** | |
276 * @brief Main program | |
277 * @param None | |
278 * @retval None | |
279 */ | |
120 | 280 /* #define DEBUG_RUNTIME TRUE */ |
281 #ifdef DEBUG_RUNTIME | |
282 #define MEASURECNT 60 /* number of measuremets to be stored */ | |
283 static uint32_t loopcnt[MEASURECNT]; | |
284 extern RTC_HandleTypeDef RTCHandle; | |
285 #endif | |
38 | 286 |
89 | 287 int main(void) { |
120 | 288 |
289 #ifdef DEBUG_RUNTIME | |
290 RTC_TimeTypeDef Stime; | |
291 uint8_t measurementindex = 0; | |
292 uint8_t lastsecond = 0xFF; | |
293 #endif | |
294 | |
917
0d6c4b40fae4
Bugfix external interface power up after sleep:
Ideenmodellierer
parents:
909
diff
changeset
|
295 uint8_t extInterfaceActive = 0; |
0d6c4b40fae4
Bugfix external interface power up after sleep:
Ideenmodellierer
parents:
909
diff
changeset
|
296 |
89 | 297 HAL_Init(); |
298 SystemClock_Config(); | |
38 | 299 |
89 | 300 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); |
301 HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK); | |
302 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); | |
38 | 303 |
89 | 304 MX_RTC_init(); |
880 | 305 GPIO_LEDs_VIBRATION_Init(); |
89 | 306 GPIO_new_DEBUG_Init(); // added 170322 hw |
307 initGlobals(); | |
38 | 308 |
120 | 309 /* printf("CPU2-RTE running...\n"); */ |
38 | 310 |
329 | 311 HAL_Delay(100); |
312 | |
89 | 313 MX_I2C1_Init(); |
329 | 314 if (global.I2C_SystemStatus != HAL_OK) |
315 { | |
89 | 316 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) { |
317 MX_I2C1_TestAndClear(); // do it a second time | |
318 } | |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
319 HAL_Delay(100); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
320 I2C_DeInit(); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
321 HAL_Delay(100); |
89 | 322 MX_I2C1_Init(); |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
323 HAL_Delay(100); |
89 | 324 } |
38 | 325 |
329 | 326 |
327 | |
89 | 328 //dangerous: TM_OTP_Write(0,0, 0x01); |
38 | 329 #ifdef REGULAR_RUN |
89 | 330 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); |
331 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; | |
329 | 332 if (global.I2C_SystemStatus != HAL_OK) |
333 { | |
89 | 334 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) { |
335 MX_I2C1_TestAndClear(); // do it a second time | |
336 } | |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
337 HAL_Delay(100); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
338 I2C_DeInit(); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
339 HAL_Delay(100); |
89 | 340 MX_I2C1_Init(); |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
341 HAL_Delay(100); |
89 | 342 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); |
343 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; | |
344 } | |
38 | 345 |
89 | 346 global.dataSendToMaster.sensorErrors = |
347 global.sensorError[SENSOR_PRESSURE_ID]; | |
329 | 348 |
349 if(is_init_pressure_done()) | |
350 { | |
339
37f45300bc2e
Apply averaging to pressure measurement: In pre versions calculated pressure value jittered +/-10hPa. Since we measure the pressure several time a second but only use one value a second, calc average including not used values
ideenmodellierer
parents:
334
diff
changeset
|
351 init_surface_ring(0); |
329 | 352 } |
89 | 353 init_battery_gas_gauge(); |
354 HAL_Delay(10); | |
355 battery_gas_gauge_get_data(); | |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
356 |
662 | 357 global.lifeData.battery_voltage = get_voltage(); |
358 global.lifeData.battery_charge = get_charge(); | |
359 copyBatteryData(); | |
360 | |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
361 MX_SPI3_Init(); |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
362 |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
363 if(coldstart != 0xA5) /* Not reading a 0xA5 means the memory cells has not been initialized before => cold start */ |
433 | 364 { |
365 coldstart = 0xA5; | |
662 | 366 |
367 set_charge_state(Charger_ColdStart); | |
368 | |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
369 global.dataSendToMaster.power_on_reset = 1; |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
370 global.deviceDataSendToMaster.power_on_reset = 1; |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
371 |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
372 if (!scheduleSetButtonResponsiveness()) |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
373 { |
763
aa6006975e76
increase HAL_Delay to 10ms for cold-start-button reset
heinrichsweikamp
parents:
762
diff
changeset
|
374 HAL_Delay(10); |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
375 if (!scheduleSetButtonResponsiveness()) // send again, if problem it's not my problem here. |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
376 { |
763
aa6006975e76
increase HAL_Delay to 10ms for cold-start-button reset
heinrichsweikamp
parents:
762
diff
changeset
|
377 HAL_Delay(10); |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
378 scheduleSetButtonResponsiveness(); // init |
763
aa6006975e76
increase HAL_Delay to 10ms for cold-start-button reset
heinrichsweikamp
parents:
762
diff
changeset
|
379 HAL_Delay(10); |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
380 } |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
381 } |
433 | 382 } |
662 | 383 else |
384 { | |
385 set_charge_state(Charger_NotConnected); | |
386 } | |
38 | 387 |
89 | 388 ADCx_Init(); |
389 GPIO_Power_MainCPU_Init(); | |
662 | 390 |
391 externalInterface_InitPower33(); | |
392 | |
89 | 393 global.mode = MODE_POWERUP; |
38 | 394 #else |
89 | 395 init_pressure(); |
339
37f45300bc2e
Apply averaging to pressure measurement: In pre versions calculated pressure value jittered +/-10hPa. Since we measure the pressure several time a second but only use one value a second, calc average including not used values
ideenmodellierer
parents:
334
diff
changeset
|
396 init_surface_ring(0); |
38 | 397 |
89 | 398 ADCx_Init(); |
399 GPIO_Power_MainCPU_Init(); | |
400 global.mode = MODE_TEST; | |
38 | 401 #endif |
89 | 402 while (1) { |
120 | 403 /* printf("Global mode = %d\n", global.mode); */ |
38 | 404 |
89 | 405 switch (global.mode) { |
406 case MODE_POWERUP: | |
407 case MODE_BOOT: | |
408 // ReInit_battery_charger_status_pins(); | |
409 compass_init(0, 7); | |
410 accelerator_init(); | |
554
3328189786e7
Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
533
diff
changeset
|
411 externalInterface_Init(); |
181 | 412 |
89 | 413 if (global.mode == MODE_BOOT) { |
414 GPIO_Power_MainCPU_OFF(); | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
415 #ifdef ENABLE_GNSS |
880 | 416 GPIO_LED_GREEN_ON(); |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
417 #endif |
89 | 418 HAL_Delay(100); // for GPIO_Power_MainCPU_ON(); |
419 GPIO_Power_MainCPU_ON(); | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
420 #ifdef ENABLE_GNSS |
880 | 421 GPIO_LED_GREEN_OFF(); |
422 | |
423 GPIO_LED_RED_ON(); | |
900 | 424 GPIO_VIBRATION_ON(); |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
425 #endif |
880 | 426 HAL_Delay(100); |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
427 #ifdef ENABLE_GNSS |
880 | 428 GPIO_LED_RED_OFF(); |
429 GPIO_VIBRATION_OFF(); | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
430 #endif |
89 | 431 } |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
432 #ifdef ENABLE_GNSS |
900 | 433 GPIO_LED_RED_OFF(); |
434 GPIO_LED_GREEN_OFF(); | |
435 GPIO_VIBRATION_OFF(); | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
436 #endif |
89 | 437 SPI_synchronize_with_Master(); |
438 MX_DMA_Init(); | |
439 MX_SPI1_Init(); | |
134
0586ae83a243
Removed cyclic (by timer) send on slave side
Ideenmodellierer
parents:
120
diff
changeset
|
440 SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */ |
207 | 441 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); |
889 | 442 |
900 | 443 #ifdef ENABLE_GNSS |
889 | 444 // GNSS tests |
445 GNSS_IO_init(); | |
900 | 446 GPIO_GPS_ON(); |
447 GPIO_GPS_BCKP_ON(); | |
889 | 448 MX_USART6_UART_Init(); |
449 GNSS_Init(&GNSS_Handle, &huart6); | |
900 | 450 #endif |
899
2225c467f1e9
Added data path and visualization for position data:
Ideenmodellierer
parents:
890
diff
changeset
|
451 |
2225c467f1e9
Added data path and visualization for position data:
Ideenmodellierer
parents:
890
diff
changeset
|
452 #if 0 |
889 | 453 HAL_Delay(1000); |
454 GNSS_LoadConfig(&GNSS_Handle); | |
890
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
455 HAL_Delay(250); |
889 | 456 GNSS_GetUniqID(&GNSS_Handle); |
457 GNSS_ParseBuffer(&GNSS_Handle); | |
890
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
458 HAL_Delay(250); |
889 | 459 GNSS_GetPVTData(&GNSS_Handle); |
460 GNSS_ParseBuffer(&GNSS_Handle); | |
899
2225c467f1e9
Added data path and visualization for position data:
Ideenmodellierer
parents:
890
diff
changeset
|
461 #endif |
890
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
462 /* |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
463 * Demo code from SimpleMethod |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
464 * called 1/second |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
465 while (1) { |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
466 |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
467 if ((HAL_GetTick() - Timer) > 1000) { |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
468 GNSS_GetUniqID(&GNSS_Handle); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
469 GNSS_ParseBuffer(&GNSS_Handle); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
470 HAL_Delay(250); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
471 GNSS_GetPVTData(&GNSS_Handle); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
472 GNSS_ParseBuffer(&GNSS_Handle); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
473 printf("Day: %d-%d-%d \r\n", GNSS_Handle.day, GNSS_Handle.month,GNSS_Handle.year); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
474 printf("Time: %d:%d:%d \r\n", GNSS_Handle.hour, GNSS_Handle.min,GNSS_Handle.sec); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
475 printf("Status of fix: %d \r\n", GNSS_Handle.fixType); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
476 printf("Latitude: %f \r\n", GNSS_Handle.fLat); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
477 printf("Longitude: %f \r\n",(float) GNSS_Handle.lon / 10000000.0); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
478 printf("Height above ellipsoid: %d \r\n", GNSS_Handle.height); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
479 printf("Height above mean sea level: %d \r\n", GNSS_Handle.hMSL); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
480 printf("Ground Speed (2-D): %d \r\n", GNSS_Handle.gSpeed); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
481 printf("Unique ID: %04X %04X %04X %04X %04X \n\r", |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
482 GNSS_Handle.uniqueID[0], GNSS_Handle.uniqueID[1], |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
483 GNSS_Handle.uniqueID[2], GNSS_Handle.uniqueID[3], |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
484 GNSS_Handle.uniqueID[4], GNSS_Handle.uniqueID[5]); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
485 Timer = HAL_GetTick(); |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
486 } |
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
487 */ |
889 | 488 |
89 | 489 global.mode = MODE_SURFACE; |
490 break; | |
38 | 491 |
89 | 492 case MODE_CALIB: |
90 | 493 scheduleCompassCalibrationMode(); |
89 | 494 break; |
495 | |
496 case MODE_SURFACE: | |
497 scheduleSurfaceMode(); | |
498 break; | |
38 | 499 |
89 | 500 case MODE_TEST: |
501 break; | |
38 | 502 |
89 | 503 case MODE_DIVE: |
90 | 504 backup.no_fly_time_minutes = global.no_fly_time_minutes; |
505 backup.seconds_since_last_dive = global.seconds_since_last_dive; | |
506 | |
507 vpm_init( &global.vpm, global.conservatism, global.repetitive_dive, | |
508 global.seconds_since_last_dive ); | |
509 global.no_fly_time_minutes = 0; | |
510 global.lifeData.dive_time_seconds = 0; | |
511 global.lifeData.dive_time_seconds_without_surface_time = 0; | |
512 scheduleDiveMode(); | |
513 // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1; | |
514 | |
515 if( global.lifeData.dive_time_seconds > 60 ) | |
516 { | |
517 //No Fly time 60% of desaturationtime after dive | |
518 global.no_fly_time_minutes = decom_calc_desaturation_time( | |
519 global.lifeData.tissue_nitrogen_bar, | |
520 global.lifeData.tissue_helium_bar, | |
521 global.lifeData.pressure_surface_bar ) * 60 / 100; | |
522 if( global.no_fly_time_minutes < (24 * 60) ) | |
523 global.no_fly_time_minutes = 24 * 60; | |
524 } | |
525 else | |
526 { | |
527 global.no_fly_time_minutes = backup.no_fly_time_minutes; | |
528 global.seconds_since_last_dive = backup.seconds_since_last_dive; | |
529 } | |
530 | |
531 global.lifeData.dive_time_seconds = 0; | |
532 global.lifeData.dive_time_seconds_without_surface_time = 0; | |
533 global.lifeData.counterSecondsShallowDepth = 0; | |
534 | |
535 backup.no_fly_time_minutes = 0; | |
536 backup.seconds_since_last_dive = 0; | |
89 | 537 break; |
38 | 538 |
89 | 539 case MODE_SHUTDOWN: |
540 HAL_Delay(200); | |
541 global.mode = MODE_SLEEP; | |
542 MX_SPI3_Init(); | |
543 break; | |
544 | |
545 case MODE_SLEEP: | |
917
0d6c4b40fae4
Bugfix external interface power up after sleep:
Ideenmodellierer
parents:
909
diff
changeset
|
546 extInterfaceActive = externalInterface_isEnabledPower33(); |
839 | 547 externalInterface_SwitchUART(EXT_INTERFACE_UART_OFF); |
662 | 548 externalInterface_SwitchPower33(false); |
89 | 549 if (hasExternalClock()) |
550 SystemClock_Config_HSI(); | |
887 | 551 GPIO_LEDs_VIBRATION_Init(); |
89 | 552 sleep_prepare(); |
38 | 553 |
89 | 554 scheduleSleepMode(); |
555 if (hasExternalClock()) | |
556 SystemClock_Config_HSE(); | |
557 EXTI_Wakeup_Button_DeInit(); | |
558 ADCx_Init(); | |
559 GPIO_Power_MainCPU_Init(); | |
560 GPIO_Power_MainCPU_ON(); | |
561 compass_init(0, 7); | |
562 accelerator_init(); | |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
563 SPI_synchronize_with_Master(); |
89 | 564 MX_DMA_Init(); |
565 MX_SPI1_Init(); | |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
566 SPI_Start_single_TxRx_with_Master(); |
89 | 567 |
917
0d6c4b40fae4
Bugfix external interface power up after sleep:
Ideenmodellierer
parents:
909
diff
changeset
|
568 if(extInterfaceActive) |
662 | 569 { |
570 externalInterface_SwitchPower33(true); | |
571 } | |
738
30717de00f3a
Added data init function for external interface:
Ideenmodellierer
parents:
728
diff
changeset
|
572 externalInterface_InitDatastruct(); |
89 | 573 // EXTILine0_Button_DeInit(); not now, later after testing |
574 break; | |
575 } | |
120 | 576 |
577 #ifdef DEBUG_RUNTIME | |
578 HAL_RTC_GetTime(&RTCHandle, &Stime, RTC_FORMAT_BCD); | |
579 | |
580 if(lastsecond == 0xFF) | |
581 { | |
582 measurementindex = 0; | |
583 loopcnt[measurementindex] = 0; | |
584 lastsecond = Stime.Seconds; | |
585 } | |
586 loopcnt[measurementindex]++; | |
587 | |
588 if(lastsecond != Stime.Seconds) | |
589 { | |
590 measurementindex++; | |
591 if (measurementindex == MEASURECNT) measurementindex = 0; | |
592 loopcnt[measurementindex] = 0; | |
593 lastsecond = Stime.Seconds; | |
594 if(measurementindex +1 < MEASURECNT) loopcnt[measurementindex +1] = 0xffff; /* helps to identify the latest value */ | |
595 } | |
596 #endif | |
89 | 597 } |
38 | 598 } |
599 | |
600 /** @brief Button feedback - EXTI line detection callbacks | |
601 * @param GPIO_Pin: Specifies the pins connected EXTI line | |
602 * @retval None | |
603 */ | |
89 | 604 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { |
38 | 605 |
89 | 606 if (GPIO_Pin == BUTTON_OSTC_GPIO_PIN) { |
607 if (global.mode == MODE_SLEEP) { | |
608 global.mode = MODE_BOOT; | |
609 } | |
610 } | |
611 else | |
880 | 612 { |
613 | |
89 | 614 } |
38 | 615 } |
616 | |
617 /** | |
618 * @brief System Clock Configuration | |
619 * The system Clock is configured as follow : | |
620 * System Clock source = PLL (HSI) | |
621 * SYSCLK(Hz) = 100 MHz | |
622 * HCLK(Hz) = 100 MHz | |
623 * AHB Prescaler = 1 | |
624 * APB1 Prescaler = 2 | |
625 * APB2 Prescaler = 1 | |
626 * HSI Frequency(Hz) = 16 MHz | |
627 * PLL_M = 16 | |
628 * PLL_N = 400 | |
629 * PLL_P = 4 | |
630 * PLL_Q = 7 // no USB | |
631 * VDD(V) = 3.3 | |
632 * Main regulator output voltage = Scale1 mode | |
633 * Flash Latency(WS) = 3 | |
634 * @param None | |
635 * @retval None | |
636 */ | |
637 | |
89 | 638 void SystemClock_Config(void) { |
639 if (hasExternalClock()) | |
640 SystemClock_Config_HSE(); | |
641 else | |
642 SystemClock_Config_HSI(); | |
38 | 643 } |
644 | |
89 | 645 void SYSCLKConfig_STOP(void) { |
646 SYSCLKConfig_STOP_HSI(); | |
38 | 647 } |
648 | |
89 | 649 void SystemClock_Config_HSE(void) { |
650 RCC_OscInitTypeDef RCC_OscInitStruct; | |
651 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
38 | 652 // RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
653 | |
89 | 654 __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE(); |
38 | 655 |
887 | 656 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
38 | 657 |
89 | 658 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE; |
659 RCC_OscInitStruct.HSEState = RCC_HSE_ON; | |
660 //RCC_OscInitStruct.LSEState = RCC_LSE_ON; | |
661 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
662 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; | |
663 RCC_OscInitStruct.PLL.PLLM = 8; | |
664 RCC_OscInitStruct.PLL.PLLN = 320; | |
665 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; | |
666 RCC_OscInitStruct.PLL.PLLQ = 4; | |
667 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 668 |
89 | 669 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
670 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; | |
671 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
672 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
673 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; | |
674 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | |
675 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); | |
38 | 676 |
677 // PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; | |
678 // PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; | |
679 // HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); | |
680 | |
681 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); | |
682 | |
683 // HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); | |
684 | |
89 | 685 /* SysTick_IRQn interrupt configuration */ |
38 | 686 // HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); |
687 } | |
688 | |
89 | 689 void SystemClock_Config_HSI(void) { |
690 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
691 RCC_OscInitTypeDef RCC_OscInitStruct; | |
38 | 692 |
89 | 693 /* Enable Power Control clock */ |
694 __HAL_RCC_PWR_CLK_ENABLE(); | |
38 | 695 |
89 | 696 /* The voltage scaling allows optimizing the power consumption when the device is |
697 clocked below the maximum system frequency, to update the voltage scaling value | |
698 regarding system frequency refer to product datasheet. */ | |
887 | 699 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
38 | 700 |
89 | 701 /* Enable HSI Oscillator and activate PLL with HSI as source */ |
702 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | |
703 RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
704 RCC_OscInitStruct.HSICalibrationValue = 0x10; | |
705 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
706 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; | |
707 RCC_OscInitStruct.PLL.PLLM = 16; | |
708 RCC_OscInitStruct.PLL.PLLN = 320; | |
709 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; | |
710 RCC_OscInitStruct.PLL.PLLQ = 4; | |
711 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 712 |
89 | 713 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
714 clocks dividers */ | |
715 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | |
716 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); | |
717 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
718 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
719 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; | |
720 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | |
721 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); | |
38 | 722 } |
723 | |
724 | |
725 /** | |
726 * @brief Configures system clock after wake-up from STOP: enable HSI, PLL | |
727 * and select PLL as system clock source. | |
728 * @param None | |
729 * @retval None | |
730 */ | |
89 | 731 void SYSCLKConfig_STOP_HSE(void) { |
732 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
733 RCC_OscInitTypeDef RCC_OscInitStruct; | |
734 uint32_t pFLatency = 0; | |
38 | 735 |
89 | 736 /* Get the Oscillators configuration according to the internal RCC registers */ |
737 HAL_RCC_GetOscConfig(&RCC_OscInitStruct); | |
38 | 738 |
89 | 739 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ |
740 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; | |
741 RCC_OscInitStruct.HSIState = RCC_HSE_ON; | |
742 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
743 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 744 |
89 | 745 /* Get the Clocks configuration according to the internal RCC registers */ |
746 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency); | |
38 | 747 |
89 | 748 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
749 clocks dividers */ | |
750 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; | |
751 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
752 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency); | |
38 | 753 } |
754 | |
89 | 755 void SYSCLKConfig_STOP_HSI(void) { |
756 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
757 RCC_OscInitTypeDef RCC_OscInitStruct; | |
758 uint32_t pFLatency = 0; | |
38 | 759 |
89 | 760 /* Get the Oscillators configuration according to the internal RCC registers */ |
761 HAL_RCC_GetOscConfig(&RCC_OscInitStruct); | |
38 | 762 |
89 | 763 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ |
764 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | |
765 RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
766 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
767 RCC_OscInitStruct.HSICalibrationValue = 0x10; | |
768 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 769 |
89 | 770 /* Get the Clocks configuration according to the internal RCC registers */ |
771 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency); | |
38 | 772 |
89 | 773 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
774 clocks dividers */ | |
775 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; | |
776 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
777 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency); | |
38 | 778 } |
779 | |
780 /** | |
781 * @brief SYSTICK callback | |
782 * @param None | |
783 * @retval None | |
784 */ | |
89 | 785 void HAL_SYSTICK_Callback(void) { |
786 HAL_IncTick(); | |
38 | 787 } |
788 | |
789 /** | |
790 * @brief Configures GPIO for LED | |
791 * Might move with STM32Cube usage | |
792 * @param None | |
793 * @retval None | |
794 */ | |
795 /* | |
796 void GPIO_test_I2C_lines(void) | |
797 { | |
798 GPIO_InitTypeDef GPIO_InitStructure; | |
799 __GPIOB_CLK_ENABLE(); | |
800 GPIO_InitStructure.Pin = GPIO_PIN_8; | |
801 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
802 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
803 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
804 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); | |
805 GPIO_InitStructure.Pin = GPIO_PIN_9; | |
806 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); | |
807 | |
808 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); | |
809 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET); | |
810 HAL_Delay(10); | |
811 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET); | |
812 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); | |
813 HAL_Delay(10); | |
814 } | |
815 */ | |
816 | |
880 | 817 static void GPIO_LEDs_VIBRATION_Init(void) { |
89 | 818 GPIO_InitTypeDef GPIO_InitStructure; |
38 | 819 |
880 | 820 __GPIOA_CLK_ENABLE(); |
821 GPIO_InitStructure.Pin = LED_CONTROL_PIN_RED; | |
89 | 822 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
823 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
880 | 824 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
825 HAL_GPIO_Init( GPIOA, &GPIO_InitStructure); | |
909 | 826 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_RED, GPIO_PIN_SET); |
880 | 827 |
828 GPIO_InitStructure.Pin = LED_CONTROL_PIN_GREEN; | |
829 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
830 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
831 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
832 HAL_GPIO_Init( GPIOA, &GPIO_InitStructure); | |
909 | 833 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_GREEN, GPIO_PIN_SET); |
880 | 834 |
835 GPIO_InitStructure.Pin = VIBRATION_CONTROL_PIN; | |
836 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
837 GPIO_InitStructure.Pull = GPIO_PULLDOWN; | |
838 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
839 HAL_GPIO_Init( GPIOA, &GPIO_InitStructure); | |
909 | 840 HAL_GPIO_WritePin( GPIOA, VIBRATION_CONTROL_PIN, GPIO_PIN_RESET); |
887 | 841 |
842 __GPIOB_CLK_ENABLE(); | |
843 GPIO_InitStructure.Pin = GPS_POWER_CONTROL_PIN; | |
844 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
845 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
846 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
847 HAL_GPIO_Init( GPIOB, &GPIO_InitStructure); | |
909 | 848 HAL_GPIO_WritePin( GPIOB, GPS_POWER_CONTROL_PIN, GPIO_PIN_SET); |
887 | 849 |
850 GPIO_InitStructure.Pin = GPS_BCKP_CONTROL_PIN; | |
851 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
852 GPIO_InitStructure.Pull = GPIO_PULLDOWN; | |
853 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
854 HAL_GPIO_Init( GPIOB, &GPIO_InitStructure); | |
909 | 855 HAL_GPIO_WritePin( GPIOB, GPS_BCKP_CONTROL_PIN, GPIO_PIN_RESET); |
38 | 856 } |
857 | |
89 | 858 void GPIO_new_DEBUG_Init(void) { |
38 | 859 #ifdef DEBUG_PIN_ACTIVE |
89 | 860 GPIO_InitTypeDef GPIO_InitStructure; |
38 | 861 |
89 | 862 __GPIOC_CLK_ENABLE(); |
662 | 863 GPIO_InitStructure.Pin = LED_CONTROL_PIN; |
89 | 864 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
865 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
866 GPIO_InitStructure.Speed = GPIO_SPEED_FAST; | |
867 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); | |
38 | 868 #endif |
869 } | |
870 | |
89 | 871 void GPIO_new_DEBUG_LOW(void) { |
38 | 872 #ifdef DEBUG_PIN_ACTIVE |
662 | 873 HAL_GPIO_WritePin(GPIOC,LED_CONTROL_PIN,GPIO_PIN_RESET); |
38 | 874 #endif |
875 } | |
876 | |
89 | 877 void GPIO_new_DEBUG_HIGH(void) { |
38 | 878 #ifdef DEBUG_PIN_ACTIVE |
662 | 879 HAL_GPIO_WritePin(GPIOC,LED_CONTROL_PIN,GPIO_PIN_SET); |
38 | 880 #endif |
881 } | |
882 | |
89 | 883 static void GPIO_Power_MainCPU_Init(void) { |
884 GPIO_InitTypeDef GPIO_InitStructure; | |
885 __GPIOC_CLK_ENABLE(); | |
662 | 886 GPIO_InitStructure.Pin = MAINCPU_CONTROL_PIN; |
89 | 887 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
888 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
889 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
890 HAL_GPIO_Init( GPIOC, &GPIO_InitStructure); | |
662 | 891 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_RESET); |
38 | 892 } |
893 | |
89 | 894 static void GPIO_Power_MainCPU_ON(void) { |
662 | 895 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_RESET); |
38 | 896 } |
897 | |
89 | 898 static void GPIO_Power_MainCPU_OFF(void) { |
662 | 899 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_SET); |
38 | 900 } |
901 | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
902 #ifdef ENABLE_GNSS |
880 | 903 static void GPIO_LED_GREEN_ON(void) { |
904 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_GREEN, GPIO_PIN_RESET); | |
905 } | |
906 | |
907 static void GPIO_LED_GREEN_OFF(void) { | |
908 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_GREEN, GPIO_PIN_SET); | |
909 } | |
910 | |
911 static void GPIO_LED_RED_ON(void) { | |
912 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_RED, GPIO_PIN_RESET); | |
913 } | |
914 | |
915 static void GPIO_LED_RED_OFF(void) { | |
916 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_RED, GPIO_PIN_SET); | |
917 } | |
918 | |
919 static void GPIO_VIBRATION_ON(void) { | |
920 HAL_GPIO_WritePin( GPIOA, VIBRATION_CONTROL_PIN, GPIO_PIN_SET); | |
921 } | |
922 | |
923 static void GPIO_VIBRATION_OFF(void) { | |
924 HAL_GPIO_WritePin( GPIOA, VIBRATION_CONTROL_PIN, GPIO_PIN_RESET); | |
925 } | |
926 | |
887 | 927 static void GPIO_GPS_ON(void) { |
928 HAL_GPIO_WritePin( GPIOB, GPS_POWER_CONTROL_PIN, GPIO_PIN_RESET); | |
929 } | |
930 | |
931 static void GPIO_GPS_OFF(void) { | |
932 HAL_GPIO_WritePin( GPIOB, GPS_POWER_CONTROL_PIN, GPIO_PIN_SET); | |
933 } | |
934 | |
935 static void GPIO_GPS_BCKP_ON(void) { | |
936 HAL_GPIO_WritePin( GPIOB, GPS_BCKP_CONTROL_PIN, GPIO_PIN_SET); | |
937 } | |
938 | |
939 static void GPIO_GPS_BCKP_OFF(void) { | |
940 HAL_GPIO_WritePin( GPIOB, GPS_BCKP_CONTROL_PIN, GPIO_PIN_RESET); | |
941 } | |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
942 #endif |
880 | 943 |
38 | 944 /** |
945 * @brief Configures EXTI Line0 (connected to PA0 + PA1 pin) in interrupt mode | |
946 * @param None | |
947 * @retval None | |
948 */ | |
949 | |
89 | 950 static void EXTI_Wakeup_Button_Init(void) { |
951 GPIO_InitTypeDef GPIO_InitStructure; | |
38 | 952 |
89 | 953 __HAL_RCC_GPIOA_CLK_ENABLE(); |
954 BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE(); | |
955 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; | |
956 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; | |
957 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
958 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure); | |
38 | 959 |
89 | 960 HAL_NVIC_SetPriority( BUTTON_OSTC_IRQn, 0x0F, 0); |
961 HAL_NVIC_EnableIRQ( BUTTON_OSTC_IRQn); | |
38 | 962 } |
963 | |
89 | 964 static void EXTI_Wakeup_Button_DeInit(void) { |
965 GPIO_InitTypeDef GPIO_InitStructure; | |
38 | 966 |
89 | 967 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; |
968 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
969 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
38 | 970 |
89 | 971 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; |
972 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure); | |
973 HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn); | |
38 | 974 } |
975 | |
976 /** | |
977 * @brief Wake Up Timer callback | |
978 * @param hrtc: RTC handle | |
979 * @retval None | |
980 */ | |
981 | |
982 /* | |
983 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) | |
984 { | |
985 static uint8_t uwCounter = 0; | |
986 uwCounter = 1; | |
987 } | |
988 */ | |
989 | |
89 | 990 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle) { |
38 | 991 |
992 } | |
993 | |
89 | 994 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle) { |
38 | 995 |
996 } | |
997 | |
89 | 998 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle) { |
38 | 999 |
1000 } | |
1001 | |
89 | 1002 void sleep_prepare(void) { |
1003 EXTI_Wakeup_Button_Init(); | |
662 | 1004 |
89 | 1005 compass_sleep(); |
1006 HAL_Delay(100); | |
1007 accelerator_sleep(); | |
1008 HAL_Delay(100); | |
38 | 1009 |
89 | 1010 I2C_DeInit(); |
1011 MX_SPI_DeInit(); | |
1012 MX_SPI3_DeInit(); | |
1013 ADCx_DeInit(); | |
38 | 1014 |
89 | 1015 GPIO_InitTypeDef GPIO_InitStruct; |
38 | 1016 |
89 | 1017 __HAL_RCC_GPIOA_CLK_ENABLE(); |
1018 __HAL_RCC_GPIOB_CLK_ENABLE(); | |
1019 __HAL_RCC_GPIOC_CLK_ENABLE(); | |
1020 __HAL_RCC_GPIOH_CLK_ENABLE(); | |
38 | 1021 |
89 | 1022 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
887 | 1023 GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
89 | 1024 GPIO_InitStruct.Pull = GPIO_NOPULL; |
1025 GPIO_InitStruct.Pin = GPIO_PIN_All; | |
1026 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | |
38 | 1027 #ifdef DEBUGMODE |
89 | 1028 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */ |
38 | 1029 #endif |
887 | 1030 |
1031 GPIO_InitStruct.Pin = GPIO_PIN_All ^ (GPS_POWER_CONTROL_PIN | GPS_BCKP_CONTROL_PIN); | |
89 | 1032 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct); |
38 | 1033 |
887 | 1034 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( MAINCPU_CONTROL_PIN | CHARGE_OUT_PIN | EXT33V_CONTROL_PIN); /* power off & charger in & charge out & OSC32 & ext33Volt */ |
662 | 1035 |
89 | 1036 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct); |
38 | 1037 |
887 | 1038 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | VIBRATION_CONTROL_PIN | LED_CONTROL_PIN_RED | LED_CONTROL_PIN_GREEN); |
38 | 1039 #ifdef DEBUGMODE |
89 | 1040 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */ |
38 | 1041 #endif |
89 | 1042 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct); |
38 | 1043 |
89 | 1044 GPIO_InitStruct.Pin = GPIO_PIN_All; |
1045 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | |
38 | 1046 |
89 | 1047 GPIO_Power_MainCPU_OFF(); |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
1048 #ifdef ENABLE_GNSS |
880 | 1049 GPIO_LED_GREEN_OFF(); |
1050 GPIO_LED_RED_OFF(); | |
887 | 1051 GPIO_VIBRATION_OFF(); |
1052 GPIO_GPS_BCKP_ON(); // mH : costs 100µA in sleep - beware | |
1053 GPIO_GPS_OFF(); | |
880 | 1054 |
890
651d21777b61
cleanup and disable function for GNSS uart and DMA
heinrichsweikamp
parents:
889
diff
changeset
|
1055 MX_USART6_UART_DeInit(); |
903
0f2e5a166d40
Disable GNSS operations using compile switch:
Ideenmodellierer
parents:
900
diff
changeset
|
1056 #endif |
38 | 1057 #ifndef DEBUGMODE |
89 | 1058 __HAL_RCC_GPIOB_CLK_DISABLE(); |
38 | 1059 #endif |
89 | 1060 __HAL_RCC_GPIOH_CLK_DISABLE(); |
38 | 1061 |
89 | 1062 HAL_Delay(1000); |
38 | 1063 } |
1064 | |
1065 /* | |
1066 void sleep_test(void) | |
1067 { | |
1068 GPIO_InitTypeDef GPIO_InitStruct; | |
1069 | |
1070 __HAL_RCC_GPIOA_CLK_ENABLE(); | |
1071 __HAL_RCC_GPIOB_CLK_ENABLE(); | |
1072 __HAL_RCC_GPIOC_CLK_ENABLE(); | |
1073 __HAL_RCC_GPIOH_CLK_ENABLE(); | |
1074 | |
1075 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; | |
1076 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | |
1077 GPIO_InitStruct.Pull = GPIO_NOPULL; | |
1078 GPIO_InitStruct.Pin = GPIO_PIN_All; | |
1079 HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); | |
1080 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |
1081 | |
1082 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_15 | GPIO_PIN_14); | |
1083 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); | |
1084 | |
1085 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0); | |
1086 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
1087 | |
1088 GPIO_Power_MainCPU_OFF(); | |
1089 | |
1090 GPIO_InitStruct.Pull = GPIO_PULLUP; | |
1091 GPIO_InitStruct.Pin = GPIO_PIN_0; | |
1092 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
1093 | |
1094 // __HAL_RCC_GPIOA_CLK_DISABLE(); | |
1095 __HAL_RCC_GPIOB_CLK_DISABLE(); | |
1096 // __HAL_RCC_GPIOC_CLK_DISABLE(); | |
1097 __HAL_RCC_GPIOH_CLK_DISABLE(); | |
1098 | |
1099 | |
1100 HAL_Delay(5000); | |
1101 while(1) | |
1102 { | |
1103 RTC_StopMode_2seconds(); | |
1104 HAL_Delay(200); | |
1105 } | |
1106 } | |
1107 */ | |
1108 | |
1109 #ifdef USE_FULL_ASSERT | |
1110 | |
1111 /** | |
1112 * @brief Reports the name of the source file and the source line number | |
1113 * where the assert_param error has occurred. | |
1114 * @param file: pointer to the source file name | |
1115 * @param line: assert_param error line source number | |
1116 * @retval None | |
1117 */ | |
1118 void assert_failed(uint8_t* file, uint32_t line) | |
1119 { | |
89 | 1120 /* User can add his own implementation to report the file name and line number, |
1121 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
38 | 1122 |
89 | 1123 /* Infinite loop */ |
1124 while (1) | |
1125 { | |
1126 } | |
38 | 1127 } |
1128 #endif | |
1129 | |
1130 /** | |
1131 * @} | |
1132 */ | |
104 | 1133 /** |
1134 * @brief This function handles SysTick Handler. | |
1135 * @param None | |
1136 * @retval None | |
1137 */ | |
38 | 1138 |
104 | 1139 /*TxRx only here. Every 100 ms.*/ |
1140 uint8_t ticks100ms=0; | |
1141 void SysTick_Handler(void) | |
1142 { | |
1143 HAL_IncTick(); | |
1144 if(ticks100ms<100){ | |
1145 ticks100ms++; | |
1146 }else | |
1147 { | |
1148 ticks100ms=0; | |
1149 } | |
1150 } | |
38 | 1151 /** |
1152 * @} | |
1153 */ | |
1154 | |
1155 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |