Mercurial > public > ostc4
annotate Small_CPU/Src/baseCPU2.c @ 889:cf3967fe6924 Evo_2_23
GNSS work in progress
author | heinrichsweikamp |
---|---|
date | Fri, 06 Sep 2024 16:46:22 +0200 |
parents | 17f02ac9da67 |
children | 651d21777b61 |
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, |
843 | 171 .versionSecond = 3, |
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); | |
880 | 238 static void GPIO_LED_RED_OFF(void); |
239 static void GPIO_LED_RED_ON(void); | |
240 static void GPIO_LED_GREEN_OFF(void); | |
241 static void GPIO_LED_GREEN_ON(void); | |
242 static void GPIO_VIBRATION_OFF(void); | |
243 static void GPIO_VIBRATION_ON(void); | |
887 | 244 static void GPIO_GPS_OFF(void); |
245 static void GPIO_GPS_ON(void); | |
246 static void GPIO_GPS_BCKP_OFF(void); | |
247 static void GPIO_GPS_BCKP_ON(void); | |
38 | 248 |
183 | 249 #ifdef DEBUG_I2C_LINES |
38 | 250 void GPIO_test_I2C_lines(void); |
183 | 251 #endif |
38 | 252 |
253 void sleep_prepare(void); | |
254 | |
255 void SystemClock_Config(void); | |
256 void SystemClock_Config_HSI(void); | |
257 void SystemClock_Config_HSE(void); | |
258 void SYSCLKConfig_STOP_HSI(void); | |
259 void SYSCLKConfig_STOP_HSE(void); | |
260 | |
261 void GPIO_new_DEBUG_Init(void); | |
262 void GPIO_new_DEBUG_LOW(void); | |
263 void GPIO_new_DEBUG_HIGH(void); | |
264 | |
265 #define REGULAR_RUN | |
266 | |
89 | 267 int __io_putchar(int ch) { |
268 ITM_SendChar(ch); | |
269 return ch; | |
38 | 270 } |
271 | |
272 /* Private functions ---------------------------------------------------------*/ | |
273 | |
274 /** | |
275 * @brief Main program | |
276 * @param None | |
277 * @retval None | |
278 */ | |
120 | 279 /* #define DEBUG_RUNTIME TRUE */ |
280 #ifdef DEBUG_RUNTIME | |
281 #define MEASURECNT 60 /* number of measuremets to be stored */ | |
282 static uint32_t loopcnt[MEASURECNT]; | |
283 extern RTC_HandleTypeDef RTCHandle; | |
284 #endif | |
38 | 285 |
89 | 286 int main(void) { |
120 | 287 |
288 #ifdef DEBUG_RUNTIME | |
289 RTC_TimeTypeDef Stime; | |
290 uint8_t measurementindex = 0; | |
291 uint8_t lastsecond = 0xFF; | |
292 #endif | |
293 | |
89 | 294 HAL_Init(); |
295 SystemClock_Config(); | |
38 | 296 |
89 | 297 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); |
298 HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK); | |
299 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); | |
38 | 300 |
89 | 301 MX_RTC_init(); |
880 | 302 GPIO_LEDs_VIBRATION_Init(); |
89 | 303 GPIO_new_DEBUG_Init(); // added 170322 hw |
304 initGlobals(); | |
38 | 305 |
120 | 306 /* printf("CPU2-RTE running...\n"); */ |
38 | 307 |
329 | 308 HAL_Delay(100); |
309 | |
89 | 310 MX_I2C1_Init(); |
329 | 311 if (global.I2C_SystemStatus != HAL_OK) |
312 { | |
89 | 313 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) { |
314 MX_I2C1_TestAndClear(); // do it a second time | |
315 } | |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
316 HAL_Delay(100); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
317 I2C_DeInit(); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
318 HAL_Delay(100); |
89 | 319 MX_I2C1_Init(); |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
320 HAL_Delay(100); |
89 | 321 } |
38 | 322 |
329 | 323 |
324 | |
89 | 325 //dangerous: TM_OTP_Write(0,0, 0x01); |
38 | 326 #ifdef REGULAR_RUN |
89 | 327 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); |
328 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; | |
329 | 329 if (global.I2C_SystemStatus != HAL_OK) |
330 { | |
89 | 331 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) { |
332 MX_I2C1_TestAndClear(); // do it a second time | |
333 } | |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
334 HAL_Delay(100); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
335 I2C_DeInit(); |
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
336 HAL_Delay(100); |
89 | 337 MX_I2C1_Init(); |
488
9eeab3fead8f
Added "I2C_DeInit();" in hardware detection routines. It's the recommended way
heinrichsweikamp
parents:
473
diff
changeset
|
338 HAL_Delay(100); |
89 | 339 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); |
340 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; | |
341 } | |
38 | 342 |
89 | 343 global.dataSendToMaster.sensorErrors = |
344 global.sensorError[SENSOR_PRESSURE_ID]; | |
329 | 345 |
346 if(is_init_pressure_done()) | |
347 { | |
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
|
348 init_surface_ring(0); |
329 | 349 } |
89 | 350 init_battery_gas_gauge(); |
351 HAL_Delay(10); | |
352 battery_gas_gauge_get_data(); | |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
353 |
662 | 354 global.lifeData.battery_voltage = get_voltage(); |
355 global.lifeData.battery_charge = get_charge(); | |
356 copyBatteryData(); | |
357 | |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
358 MX_SPI3_Init(); |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
359 |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
360 if(coldstart != 0xA5) /* Not reading a 0xA5 means the memory cells has not been initialized before => cold start */ |
433 | 361 { |
362 coldstart = 0xA5; | |
662 | 363 |
364 set_charge_state(Charger_ColdStart); | |
365 | |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
366 global.dataSendToMaster.power_on_reset = 1; |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
367 global.deviceDataSendToMaster.power_on_reset = 1; |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
368 |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
369 if (!scheduleSetButtonResponsiveness()) |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
370 { |
763
aa6006975e76
increase HAL_Delay to 10ms for cold-start-button reset
heinrichsweikamp
parents:
762
diff
changeset
|
371 HAL_Delay(10); |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
372 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
|
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 scheduleSetButtonResponsiveness(); // init |
763
aa6006975e76
increase HAL_Delay to 10ms for cold-start-button reset
heinrichsweikamp
parents:
762
diff
changeset
|
376 HAL_Delay(10); |
473
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
377 } |
edfc1a464b42
Do not signal power on reset in case of a "warm" reset:
ideenmodellierer
parents:
439
diff
changeset
|
378 } |
433 | 379 } |
662 | 380 else |
381 { | |
382 set_charge_state(Charger_NotConnected); | |
383 } | |
38 | 384 |
89 | 385 ADCx_Init(); |
386 GPIO_Power_MainCPU_Init(); | |
662 | 387 |
388 externalInterface_InitPower33(); | |
389 | |
89 | 390 global.mode = MODE_POWERUP; |
38 | 391 #else |
89 | 392 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
|
393 init_surface_ring(0); |
38 | 394 |
89 | 395 ADCx_Init(); |
396 GPIO_Power_MainCPU_Init(); | |
397 global.mode = MODE_TEST; | |
38 | 398 #endif |
89 | 399 while (1) { |
120 | 400 /* printf("Global mode = %d\n", global.mode); */ |
38 | 401 |
89 | 402 switch (global.mode) { |
403 case MODE_POWERUP: | |
404 case MODE_BOOT: | |
405 // ReInit_battery_charger_status_pins(); | |
406 compass_init(0, 7); | |
407 accelerator_init(); | |
554
3328189786e7
Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
533
diff
changeset
|
408 externalInterface_Init(); |
181 | 409 |
89 | 410 if (global.mode == MODE_BOOT) { |
411 GPIO_Power_MainCPU_OFF(); | |
880 | 412 |
413 GPIO_LED_GREEN_ON(); | |
89 | 414 HAL_Delay(100); // for GPIO_Power_MainCPU_ON(); |
415 GPIO_Power_MainCPU_ON(); | |
880 | 416 |
417 GPIO_LED_GREEN_OFF(); | |
418 | |
419 GPIO_LED_RED_ON(); | |
889 | 420 //GPIO_VIBRATION_ON(); |
880 | 421 HAL_Delay(100); |
422 GPIO_LED_RED_OFF(); | |
423 GPIO_VIBRATION_OFF(); | |
887 | 424 GPIO_GPS_ON(); |
425 GPIO_GPS_BCKP_ON(); | |
89 | 426 } |
427 SPI_synchronize_with_Master(); | |
428 MX_DMA_Init(); | |
429 MX_SPI1_Init(); | |
134
0586ae83a243
Removed cyclic (by timer) send on slave side
Ideenmodellierer
parents:
120
diff
changeset
|
430 SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */ |
207 | 431 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); |
889 | 432 |
433 // GNSS tests | |
434 GNSS_IO_init(); | |
435 MX_USART6_UART_Init(); | |
436 GNSS_Init(&GNSS_Handle, &huart6); | |
437 HAL_Delay(1000); | |
438 GNSS_LoadConfig(&GNSS_Handle); | |
439 HAL_Delay(10); | |
440 GNSS_GetUniqID(&GNSS_Handle); | |
441 GNSS_ParseBuffer(&GNSS_Handle); | |
442 HAL_Delay(10); | |
443 GNSS_GetPVTData(&GNSS_Handle); | |
444 GNSS_ParseBuffer(&GNSS_Handle); | |
445 | |
446 | |
89 | 447 global.mode = MODE_SURFACE; |
448 break; | |
38 | 449 |
89 | 450 case MODE_CALIB: |
90 | 451 scheduleCompassCalibrationMode(); |
89 | 452 break; |
453 | |
454 case MODE_SURFACE: | |
455 scheduleSurfaceMode(); | |
456 break; | |
38 | 457 |
89 | 458 case MODE_TEST: |
459 break; | |
38 | 460 |
89 | 461 case MODE_DIVE: |
90 | 462 backup.no_fly_time_minutes = global.no_fly_time_minutes; |
463 backup.seconds_since_last_dive = global.seconds_since_last_dive; | |
464 | |
465 vpm_init( &global.vpm, global.conservatism, global.repetitive_dive, | |
466 global.seconds_since_last_dive ); | |
467 global.no_fly_time_minutes = 0; | |
468 global.lifeData.dive_time_seconds = 0; | |
469 global.lifeData.dive_time_seconds_without_surface_time = 0; | |
470 scheduleDiveMode(); | |
471 // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1; | |
472 | |
473 if( global.lifeData.dive_time_seconds > 60 ) | |
474 { | |
475 //No Fly time 60% of desaturationtime after dive | |
476 global.no_fly_time_minutes = decom_calc_desaturation_time( | |
477 global.lifeData.tissue_nitrogen_bar, | |
478 global.lifeData.tissue_helium_bar, | |
479 global.lifeData.pressure_surface_bar ) * 60 / 100; | |
480 if( global.no_fly_time_minutes < (24 * 60) ) | |
481 global.no_fly_time_minutes = 24 * 60; | |
482 } | |
483 else | |
484 { | |
485 global.no_fly_time_minutes = backup.no_fly_time_minutes; | |
486 global.seconds_since_last_dive = backup.seconds_since_last_dive; | |
487 } | |
488 | |
489 global.lifeData.dive_time_seconds = 0; | |
490 global.lifeData.dive_time_seconds_without_surface_time = 0; | |
491 global.lifeData.counterSecondsShallowDepth = 0; | |
492 | |
493 backup.no_fly_time_minutes = 0; | |
494 backup.seconds_since_last_dive = 0; | |
89 | 495 break; |
38 | 496 |
89 | 497 case MODE_SHUTDOWN: |
498 HAL_Delay(200); | |
499 global.mode = MODE_SLEEP; | |
500 MX_SPI3_Init(); | |
501 break; | |
502 | |
503 case MODE_SLEEP: | |
839 | 504 externalInterface_SwitchUART(EXT_INTERFACE_UART_OFF); |
662 | 505 externalInterface_SwitchPower33(false); |
89 | 506 if (hasExternalClock()) |
507 SystemClock_Config_HSI(); | |
887 | 508 GPIO_LEDs_VIBRATION_Init(); |
89 | 509 sleep_prepare(); |
38 | 510 |
89 | 511 scheduleSleepMode(); |
512 if (hasExternalClock()) | |
513 SystemClock_Config_HSE(); | |
514 EXTI_Wakeup_Button_DeInit(); | |
515 ADCx_Init(); | |
516 GPIO_Power_MainCPU_Init(); | |
517 GPIO_Power_MainCPU_ON(); | |
518 compass_init(0, 7); | |
519 accelerator_init(); | |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
520 SPI_synchronize_with_Master(); |
89 | 521 MX_DMA_Init(); |
522 MX_SPI1_Init(); | |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
523 SPI_Start_single_TxRx_with_Master(); |
89 | 524 |
662 | 525 if(externalInterface_isEnabledPower33()) |
526 { | |
527 externalInterface_SwitchPower33(true); | |
528 } | |
738
30717de00f3a
Added data init function for external interface:
Ideenmodellierer
parents:
728
diff
changeset
|
529 externalInterface_InitDatastruct(); |
89 | 530 // EXTILine0_Button_DeInit(); not now, later after testing |
531 break; | |
532 } | |
120 | 533 |
534 #ifdef DEBUG_RUNTIME | |
535 HAL_RTC_GetTime(&RTCHandle, &Stime, RTC_FORMAT_BCD); | |
536 | |
537 if(lastsecond == 0xFF) | |
538 { | |
539 measurementindex = 0; | |
540 loopcnt[measurementindex] = 0; | |
541 lastsecond = Stime.Seconds; | |
542 } | |
543 loopcnt[measurementindex]++; | |
544 | |
545 if(lastsecond != Stime.Seconds) | |
546 { | |
547 measurementindex++; | |
548 if (measurementindex == MEASURECNT) measurementindex = 0; | |
549 loopcnt[measurementindex] = 0; | |
550 lastsecond = Stime.Seconds; | |
551 if(measurementindex +1 < MEASURECNT) loopcnt[measurementindex +1] = 0xffff; /* helps to identify the latest value */ | |
552 } | |
553 #endif | |
89 | 554 } |
38 | 555 } |
556 | |
557 /** @brief Button feedback - EXTI line detection callbacks | |
558 * @param GPIO_Pin: Specifies the pins connected EXTI line | |
559 * @retval None | |
560 */ | |
89 | 561 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { |
38 | 562 |
89 | 563 if (GPIO_Pin == BUTTON_OSTC_GPIO_PIN) { |
564 if (global.mode == MODE_SLEEP) { | |
565 global.mode = MODE_BOOT; | |
566 } | |
567 } | |
568 else | |
880 | 569 { |
570 | |
89 | 571 } |
38 | 572 } |
573 | |
574 /** | |
575 * @brief System Clock Configuration | |
576 * The system Clock is configured as follow : | |
577 * System Clock source = PLL (HSI) | |
578 * SYSCLK(Hz) = 100 MHz | |
579 * HCLK(Hz) = 100 MHz | |
580 * AHB Prescaler = 1 | |
581 * APB1 Prescaler = 2 | |
582 * APB2 Prescaler = 1 | |
583 * HSI Frequency(Hz) = 16 MHz | |
584 * PLL_M = 16 | |
585 * PLL_N = 400 | |
586 * PLL_P = 4 | |
587 * PLL_Q = 7 // no USB | |
588 * VDD(V) = 3.3 | |
589 * Main regulator output voltage = Scale1 mode | |
590 * Flash Latency(WS) = 3 | |
591 * @param None | |
592 * @retval None | |
593 */ | |
594 | |
89 | 595 void SystemClock_Config(void) { |
596 if (hasExternalClock()) | |
597 SystemClock_Config_HSE(); | |
598 else | |
599 SystemClock_Config_HSI(); | |
38 | 600 } |
601 | |
89 | 602 void SYSCLKConfig_STOP(void) { |
603 SYSCLKConfig_STOP_HSI(); | |
38 | 604 } |
605 | |
89 | 606 void SystemClock_Config_HSE(void) { |
607 RCC_OscInitTypeDef RCC_OscInitStruct; | |
608 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
38 | 609 // RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
610 | |
89 | 611 __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE(); |
38 | 612 |
887 | 613 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
38 | 614 |
89 | 615 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE; |
616 RCC_OscInitStruct.HSEState = RCC_HSE_ON; | |
617 //RCC_OscInitStruct.LSEState = RCC_LSE_ON; | |
618 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
619 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; | |
620 RCC_OscInitStruct.PLL.PLLM = 8; | |
621 RCC_OscInitStruct.PLL.PLLN = 320; | |
622 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; | |
623 RCC_OscInitStruct.PLL.PLLQ = 4; | |
624 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 625 |
89 | 626 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
627 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; | |
628 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
629 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
630 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; | |
631 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | |
632 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); | |
38 | 633 |
634 // PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; | |
635 // PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; | |
636 // HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); | |
637 | |
638 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); | |
639 | |
640 // HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); | |
641 | |
89 | 642 /* SysTick_IRQn interrupt configuration */ |
38 | 643 // HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); |
644 } | |
645 | |
89 | 646 void SystemClock_Config_HSI(void) { |
647 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
648 RCC_OscInitTypeDef RCC_OscInitStruct; | |
38 | 649 |
89 | 650 /* Enable Power Control clock */ |
651 __HAL_RCC_PWR_CLK_ENABLE(); | |
38 | 652 |
89 | 653 /* The voltage scaling allows optimizing the power consumption when the device is |
654 clocked below the maximum system frequency, to update the voltage scaling value | |
655 regarding system frequency refer to product datasheet. */ | |
887 | 656 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
38 | 657 |
89 | 658 /* Enable HSI Oscillator and activate PLL with HSI as source */ |
659 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | |
660 RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
661 RCC_OscInitStruct.HSICalibrationValue = 0x10; | |
662 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
663 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; | |
664 RCC_OscInitStruct.PLL.PLLM = 16; | |
665 RCC_OscInitStruct.PLL.PLLN = 320; | |
666 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; | |
667 RCC_OscInitStruct.PLL.PLLQ = 4; | |
668 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 669 |
89 | 670 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
671 clocks dividers */ | |
672 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | |
673 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); | |
674 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
675 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
676 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; | |
677 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | |
678 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); | |
38 | 679 } |
680 | |
681 | |
682 /** | |
683 * @brief Configures system clock after wake-up from STOP: enable HSI, PLL | |
684 * and select PLL as system clock source. | |
685 * @param None | |
686 * @retval None | |
687 */ | |
89 | 688 void SYSCLKConfig_STOP_HSE(void) { |
689 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
690 RCC_OscInitTypeDef RCC_OscInitStruct; | |
691 uint32_t pFLatency = 0; | |
38 | 692 |
89 | 693 /* Get the Oscillators configuration according to the internal RCC registers */ |
694 HAL_RCC_GetOscConfig(&RCC_OscInitStruct); | |
38 | 695 |
89 | 696 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ |
697 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; | |
698 RCC_OscInitStruct.HSIState = RCC_HSE_ON; | |
699 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
700 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 701 |
89 | 702 /* Get the Clocks configuration according to the internal RCC registers */ |
703 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency); | |
38 | 704 |
89 | 705 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
706 clocks dividers */ | |
707 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; | |
708 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
709 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency); | |
38 | 710 } |
711 | |
89 | 712 void SYSCLKConfig_STOP_HSI(void) { |
713 RCC_ClkInitTypeDef RCC_ClkInitStruct; | |
714 RCC_OscInitTypeDef RCC_OscInitStruct; | |
715 uint32_t pFLatency = 0; | |
38 | 716 |
89 | 717 /* Get the Oscillators configuration according to the internal RCC registers */ |
718 HAL_RCC_GetOscConfig(&RCC_OscInitStruct); | |
38 | 719 |
89 | 720 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ |
721 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | |
722 RCC_OscInitStruct.HSIState = RCC_HSI_ON; | |
723 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
724 RCC_OscInitStruct.HSICalibrationValue = 0x10; | |
725 HAL_RCC_OscConfig(&RCC_OscInitStruct); | |
38 | 726 |
89 | 727 /* Get the Clocks configuration according to the internal RCC registers */ |
728 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency); | |
38 | 729 |
89 | 730 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 |
731 clocks dividers */ | |
732 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; | |
733 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
734 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency); | |
38 | 735 } |
736 | |
737 /** | |
738 * @brief SYSTICK callback | |
739 * @param None | |
740 * @retval None | |
741 */ | |
89 | 742 void HAL_SYSTICK_Callback(void) { |
743 HAL_IncTick(); | |
38 | 744 } |
745 | |
746 /** | |
747 * @brief Configures GPIO for LED | |
748 * Might move with STM32Cube usage | |
749 * @param None | |
750 * @retval None | |
751 */ | |
752 /* | |
753 void GPIO_test_I2C_lines(void) | |
754 { | |
755 GPIO_InitTypeDef GPIO_InitStructure; | |
756 __GPIOB_CLK_ENABLE(); | |
757 GPIO_InitStructure.Pin = GPIO_PIN_8; | |
758 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
759 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
760 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
761 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); | |
762 GPIO_InitStructure.Pin = GPIO_PIN_9; | |
763 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); | |
764 | |
765 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); | |
766 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET); | |
767 HAL_Delay(10); | |
768 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET); | |
769 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); | |
770 HAL_Delay(10); | |
771 } | |
772 */ | |
773 | |
880 | 774 static void GPIO_LEDs_VIBRATION_Init(void) { |
89 | 775 GPIO_InitTypeDef GPIO_InitStructure; |
38 | 776 |
880 | 777 __GPIOA_CLK_ENABLE(); |
778 GPIO_InitStructure.Pin = LED_CONTROL_PIN_RED; | |
89 | 779 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
780 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
880 | 781 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
782 HAL_GPIO_Init( GPIOA, &GPIO_InitStructure); | |
783 | |
784 GPIO_InitStructure.Pin = LED_CONTROL_PIN_GREEN; | |
785 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
786 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
787 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
788 HAL_GPIO_Init( GPIOA, &GPIO_InitStructure); | |
789 | |
790 GPIO_InitStructure.Pin = VIBRATION_CONTROL_PIN; | |
791 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
792 GPIO_InitStructure.Pull = GPIO_PULLDOWN; | |
793 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
794 HAL_GPIO_Init( GPIOA, &GPIO_InitStructure); | |
887 | 795 |
796 __GPIOB_CLK_ENABLE(); | |
797 GPIO_InitStructure.Pin = GPS_POWER_CONTROL_PIN; | |
798 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
799 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
800 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
801 HAL_GPIO_Init( GPIOB, &GPIO_InitStructure); | |
802 | |
803 GPIO_InitStructure.Pin = GPS_BCKP_CONTROL_PIN; | |
804 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
805 GPIO_InitStructure.Pull = GPIO_PULLDOWN; | |
806 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
807 HAL_GPIO_Init( GPIOB, &GPIO_InitStructure); | |
808 | |
38 | 809 } |
810 | |
89 | 811 void GPIO_new_DEBUG_Init(void) { |
38 | 812 #ifdef DEBUG_PIN_ACTIVE |
89 | 813 GPIO_InitTypeDef GPIO_InitStructure; |
38 | 814 |
89 | 815 __GPIOC_CLK_ENABLE(); |
662 | 816 GPIO_InitStructure.Pin = LED_CONTROL_PIN; |
89 | 817 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
818 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
819 GPIO_InitStructure.Speed = GPIO_SPEED_FAST; | |
820 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); | |
38 | 821 #endif |
822 } | |
823 | |
89 | 824 void GPIO_new_DEBUG_LOW(void) { |
38 | 825 #ifdef DEBUG_PIN_ACTIVE |
662 | 826 HAL_GPIO_WritePin(GPIOC,LED_CONTROL_PIN,GPIO_PIN_RESET); |
38 | 827 #endif |
828 } | |
829 | |
89 | 830 void GPIO_new_DEBUG_HIGH(void) { |
38 | 831 #ifdef DEBUG_PIN_ACTIVE |
662 | 832 HAL_GPIO_WritePin(GPIOC,LED_CONTROL_PIN,GPIO_PIN_SET); |
38 | 833 #endif |
834 } | |
835 | |
89 | 836 static void GPIO_Power_MainCPU_Init(void) { |
837 GPIO_InitTypeDef GPIO_InitStructure; | |
838 __GPIOC_CLK_ENABLE(); | |
662 | 839 GPIO_InitStructure.Pin = MAINCPU_CONTROL_PIN; |
89 | 840 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
841 GPIO_InitStructure.Pull = GPIO_PULLUP; | |
842 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
843 HAL_GPIO_Init( GPIOC, &GPIO_InitStructure); | |
662 | 844 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_RESET); |
38 | 845 } |
846 | |
89 | 847 static void GPIO_Power_MainCPU_ON(void) { |
662 | 848 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_RESET); |
38 | 849 } |
850 | |
89 | 851 static void GPIO_Power_MainCPU_OFF(void) { |
662 | 852 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_SET); |
38 | 853 } |
854 | |
880 | 855 static void GPIO_LED_GREEN_ON(void) { |
856 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_GREEN, GPIO_PIN_RESET); | |
857 } | |
858 | |
859 static void GPIO_LED_GREEN_OFF(void) { | |
860 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_GREEN, GPIO_PIN_SET); | |
861 } | |
862 | |
863 static void GPIO_LED_RED_ON(void) { | |
864 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_RED, GPIO_PIN_RESET); | |
865 } | |
866 | |
867 static void GPIO_LED_RED_OFF(void) { | |
868 HAL_GPIO_WritePin( GPIOA, LED_CONTROL_PIN_RED, GPIO_PIN_SET); | |
869 } | |
870 | |
871 static void GPIO_VIBRATION_ON(void) { | |
872 HAL_GPIO_WritePin( GPIOA, VIBRATION_CONTROL_PIN, GPIO_PIN_SET); | |
873 } | |
874 | |
875 static void GPIO_VIBRATION_OFF(void) { | |
876 HAL_GPIO_WritePin( GPIOA, VIBRATION_CONTROL_PIN, GPIO_PIN_RESET); | |
877 } | |
878 | |
887 | 879 static void GPIO_GPS_ON(void) { |
880 HAL_GPIO_WritePin( GPIOB, GPS_POWER_CONTROL_PIN, GPIO_PIN_RESET); | |
881 } | |
882 | |
883 static void GPIO_GPS_OFF(void) { | |
884 HAL_GPIO_WritePin( GPIOB, GPS_POWER_CONTROL_PIN, GPIO_PIN_SET); | |
885 } | |
886 | |
887 static void GPIO_GPS_BCKP_ON(void) { | |
888 HAL_GPIO_WritePin( GPIOB, GPS_BCKP_CONTROL_PIN, GPIO_PIN_SET); | |
889 } | |
890 | |
891 static void GPIO_GPS_BCKP_OFF(void) { | |
892 HAL_GPIO_WritePin( GPIOB, GPS_BCKP_CONTROL_PIN, GPIO_PIN_RESET); | |
893 } | |
894 | |
880 | 895 |
38 | 896 /** |
897 * @brief Configures EXTI Line0 (connected to PA0 + PA1 pin) in interrupt mode | |
898 * @param None | |
899 * @retval None | |
900 */ | |
901 | |
89 | 902 static void EXTI_Wakeup_Button_Init(void) { |
903 GPIO_InitTypeDef GPIO_InitStructure; | |
38 | 904 |
89 | 905 __HAL_RCC_GPIOA_CLK_ENABLE(); |
906 BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE(); | |
907 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; | |
908 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; | |
909 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
910 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure); | |
38 | 911 |
89 | 912 HAL_NVIC_SetPriority( BUTTON_OSTC_IRQn, 0x0F, 0); |
913 HAL_NVIC_EnableIRQ( BUTTON_OSTC_IRQn); | |
38 | 914 } |
915 | |
89 | 916 static void EXTI_Wakeup_Button_DeInit(void) { |
917 GPIO_InitTypeDef GPIO_InitStructure; | |
38 | 918 |
89 | 919 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; |
920 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
921 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
38 | 922 |
89 | 923 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; |
924 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure); | |
925 HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn); | |
38 | 926 } |
927 | |
928 /** | |
929 * @brief Wake Up Timer callback | |
930 * @param hrtc: RTC handle | |
931 * @retval None | |
932 */ | |
933 | |
934 /* | |
935 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) | |
936 { | |
937 static uint8_t uwCounter = 0; | |
938 uwCounter = 1; | |
939 } | |
940 */ | |
941 | |
89 | 942 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle) { |
38 | 943 |
944 } | |
945 | |
89 | 946 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle) { |
38 | 947 |
948 } | |
949 | |
89 | 950 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle) { |
38 | 951 |
952 } | |
953 | |
89 | 954 void sleep_prepare(void) { |
955 EXTI_Wakeup_Button_Init(); | |
662 | 956 |
89 | 957 compass_sleep(); |
958 HAL_Delay(100); | |
959 accelerator_sleep(); | |
960 HAL_Delay(100); | |
38 | 961 |
89 | 962 I2C_DeInit(); |
963 MX_SPI_DeInit(); | |
964 MX_SPI3_DeInit(); | |
965 ADCx_DeInit(); | |
38 | 966 |
89 | 967 GPIO_InitTypeDef GPIO_InitStruct; |
38 | 968 |
89 | 969 __HAL_RCC_GPIOA_CLK_ENABLE(); |
970 __HAL_RCC_GPIOB_CLK_ENABLE(); | |
971 __HAL_RCC_GPIOC_CLK_ENABLE(); | |
972 __HAL_RCC_GPIOH_CLK_ENABLE(); | |
38 | 973 |
89 | 974 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
887 | 975 GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
89 | 976 GPIO_InitStruct.Pull = GPIO_NOPULL; |
977 GPIO_InitStruct.Pin = GPIO_PIN_All; | |
978 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | |
38 | 979 #ifdef DEBUGMODE |
89 | 980 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */ |
38 | 981 #endif |
887 | 982 |
983 GPIO_InitStruct.Pin = GPIO_PIN_All ^ (GPS_POWER_CONTROL_PIN | GPS_BCKP_CONTROL_PIN); | |
89 | 984 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct); |
38 | 985 |
887 | 986 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( MAINCPU_CONTROL_PIN | CHARGE_OUT_PIN | EXT33V_CONTROL_PIN); /* power off & charger in & charge out & OSC32 & ext33Volt */ |
662 | 987 |
89 | 988 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct); |
38 | 989 |
887 | 990 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | VIBRATION_CONTROL_PIN | LED_CONTROL_PIN_RED | LED_CONTROL_PIN_GREEN); |
38 | 991 #ifdef DEBUGMODE |
89 | 992 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */ |
38 | 993 #endif |
89 | 994 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct); |
38 | 995 |
89 | 996 GPIO_InitStruct.Pin = GPIO_PIN_All; |
997 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | |
38 | 998 |
89 | 999 GPIO_Power_MainCPU_OFF(); |
880 | 1000 GPIO_LED_GREEN_OFF(); |
1001 GPIO_LED_RED_OFF(); | |
887 | 1002 GPIO_VIBRATION_OFF(); |
1003 GPIO_GPS_BCKP_ON(); // mH : costs 100µA in sleep - beware | |
1004 GPIO_GPS_OFF(); | |
880 | 1005 |
38 | 1006 |
1007 #ifndef DEBUGMODE | |
89 | 1008 __HAL_RCC_GPIOB_CLK_DISABLE(); |
38 | 1009 #endif |
89 | 1010 __HAL_RCC_GPIOH_CLK_DISABLE(); |
38 | 1011 |
89 | 1012 HAL_Delay(1000); |
38 | 1013 } |
1014 | |
1015 /* | |
1016 void sleep_test(void) | |
1017 { | |
1018 GPIO_InitTypeDef GPIO_InitStruct; | |
1019 | |
1020 __HAL_RCC_GPIOA_CLK_ENABLE(); | |
1021 __HAL_RCC_GPIOB_CLK_ENABLE(); | |
1022 __HAL_RCC_GPIOC_CLK_ENABLE(); | |
1023 __HAL_RCC_GPIOH_CLK_ENABLE(); | |
1024 | |
1025 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; | |
1026 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | |
1027 GPIO_InitStruct.Pull = GPIO_NOPULL; | |
1028 GPIO_InitStruct.Pin = GPIO_PIN_All; | |
1029 HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); | |
1030 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |
1031 | |
1032 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_15 | GPIO_PIN_14); | |
1033 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); | |
1034 | |
1035 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0); | |
1036 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
1037 | |
1038 GPIO_Power_MainCPU_OFF(); | |
1039 | |
1040 GPIO_InitStruct.Pull = GPIO_PULLUP; | |
1041 GPIO_InitStruct.Pin = GPIO_PIN_0; | |
1042 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
1043 | |
1044 // __HAL_RCC_GPIOA_CLK_DISABLE(); | |
1045 __HAL_RCC_GPIOB_CLK_DISABLE(); | |
1046 // __HAL_RCC_GPIOC_CLK_DISABLE(); | |
1047 __HAL_RCC_GPIOH_CLK_DISABLE(); | |
1048 | |
1049 | |
1050 HAL_Delay(5000); | |
1051 while(1) | |
1052 { | |
1053 RTC_StopMode_2seconds(); | |
1054 HAL_Delay(200); | |
1055 } | |
1056 } | |
1057 */ | |
1058 | |
1059 #ifdef USE_FULL_ASSERT | |
1060 | |
1061 /** | |
1062 * @brief Reports the name of the source file and the source line number | |
1063 * where the assert_param error has occurred. | |
1064 * @param file: pointer to the source file name | |
1065 * @param line: assert_param error line source number | |
1066 * @retval None | |
1067 */ | |
1068 void assert_failed(uint8_t* file, uint32_t line) | |
1069 { | |
89 | 1070 /* User can add his own implementation to report the file name and line number, |
1071 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
38 | 1072 |
89 | 1073 /* Infinite loop */ |
1074 while (1) | |
1075 { | |
1076 } | |
38 | 1077 } |
1078 #endif | |
1079 | |
1080 /** | |
1081 * @} | |
1082 */ | |
104 | 1083 /** |
1084 * @brief This function handles SysTick Handler. | |
1085 * @param None | |
1086 * @retval None | |
1087 */ | |
38 | 1088 |
104 | 1089 /*TxRx only here. Every 100 ms.*/ |
1090 uint8_t ticks100ms=0; | |
1091 void SysTick_Handler(void) | |
1092 { | |
1093 HAL_IncTick(); | |
1094 if(ticks100ms<100){ | |
1095 ticks100ms++; | |
1096 }else | |
1097 { | |
1098 ticks100ms=0; | |
1099 } | |
1100 } | |
38 | 1101 /** |
1102 * @} | |
1103 */ | |
1104 | |
1105 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |