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