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