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