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