Mercurial > public > ostc4
annotate Small_CPU/Src/baseCPU2.c @ 170:5ca88855da87 cleanup-1
Fix accidentally pushed local code
Did I say that Mercurial is a pain for long time git users :-)
For the record: created a commit with 1 file, saw a spelling error
so, did a hg commit --amend ... which sneaked in a 2nd file. I did not
notice, created a pull request, and now this stupid commit undoing things
is the only way as all is in public phase now.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
| author | Jan Mulder <jlmulder@xs4all.nl> |
|---|---|
| date | Sun, 10 Mar 2019 10:46:48 +0100 |
| parents | ee744c7160ce |
| children | c659fda83e44 |
| 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 ============================================================================== | |
| 18 ##### What about hardware without 8 MHz oscilator ##### | |
| 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 ============================================================================== | |
| 36 [..] All realtime stuff und all what has to be done during sleep | |
| 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 | |
| 51 by I2C for example by setting global.dataSendToSlaveStopEval to 0. | |
| 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 ============================================================================== | |
| 67 [..] Wehe sie zickt statt tickt! | |
| 68 The RTC is a seperate part of hardware inside the CPU and is not affected | |
| 69 by reset. Only power-on reset does change something. | |
| 70 This is fine but the RTC is vital for the Sleep mode as Wakeuptimer. | |
| 71 This is the only date/time system in the OSTC. The main CPU is passive. | |
| 72 Data transfer is done with localtime_rtc_tr und localtime_rtc_dr | |
| 73 in HAL_RTC format to the main CPU and as HAL_RTC structs the way back for | |
| 74 setting the actual time and date. | |
| 75 The RTC unit has 20 Byte of V_bat powered SRAM. It could be used | |
| 76 for something usefull in both CPUs. | |
| 77 | |
| 78 ============================================================================== | |
| 79 ##### File system ##### | |
| 80 ============================================================================== | |
| 81 [..] some files are used for both CPUs, like decom.c/.h, data_central.h, ... | |
| 82 | |
| 83 | |
| 84 ============================================================================== | |
| 85 ##### Unique device ID register (96 bits) ##### | |
| 86 ============================================================================== | |
| 87 [..] some files are used for both CPUs, like decom.c/.h, data_central.h, ... | |
| 88 | |
| 89 | |
| 90 ============================================================================== | |
| 91 ##### I2C ##### | |
| 92 ============================================================================== | |
| 93 [..] used for pressure, compass, (accelerator) and battery gauge | |
| 94 main cpu and pic (button) is spi | |
| 95 | |
| 96 | |
| 97 ============================================================================== | |
| 98 ##### Firmware Update Info ##### | |
| 99 ============================================================================== | |
| 100 V0.85 160531 scheduleCheck_pressure_reached_dive_mode_level() changes | |
| 101 160606 global.no_fly_time_minutes ist at least 24h after the dive | |
| 102 160613 ambient light fixed | |
| 103 160720 compass calib to Flash (8000 writes max. as erase has problems) | |
| 104 160829 do not reset main CPU on power on! | |
| 105 V0.91 161018 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); | |
| 106 V0.92+ 161020 global.sensorError[MAX_SENSORS] | |
| 107 fix missing init_pressure(); at powerUp of RTE | |
| 108 added HAL_StatusTypeDef for many functions in pressure.c | |
| 109 161024 no_fly_time_minutes Backup FIX | |
| 110 seconds_since_last_dive now related to RTC clock | |
| 111 161121 close to surface starts at 1 meter below last known surface pressure | |
| 112 161121 in surface mode dive mode starts @1 mtr difference if surface 880 mbar instead of 700 mbar before | |
| 113 V0.97+ 170213 added global.dataSendToSlave.diveModeInfo for DIVEMODE_Apnea | |
| 114 added global.dataSendToSlave.setEndDive | |
| 115 DIVEMODE_Apnea special in scheduler.c (ticksdiff >= 1000) -> no tissue, cns, otu, no change in noFly Time etc. | |
| 116 V0.99 170320 new HAL Driver Repository | |
| 117 V1.01 170509 old HAL Driver Repository | |
| 118 | |
| 119 @endverbatim | |
| 120 ****************************************************************************** | |
| 121 * @attention | |
| 122 * | |
| 123 * <h2><center>© COPYRIGHT(c) 2017 heinrichs weikamp</center></h2> | |
| 124 * | |
| 125 ****************************************************************************** | |
| 126 */ | |
| 127 | |
| 128 //#define DEBUG_PIN_ACTIVE | |
| 129 /* Includes ------------------------------------------------------------------*/ | |
| 130 | |
| 131 #include "baseCPU2.h" | |
| 132 | |
| 133 // From Small_CPU/Inc: | |
| 134 #include "dma.h" | |
| 135 #include "i2c.h" | |
| 136 #include "spi.h" | |
| 137 #include "rtc.h" | |
| 138 #include "adc.h" | |
| 139 #include "compass.h" | |
| 140 #include "pressure.h" | |
| 141 #include "batteryGasGauge.h" | |
| 142 #include "batteryCharger.h" | |
| 143 #include "scheduler.h" | |
| 144 #include "wireless.h" | |
| 145 #include "tm_stm32f4_otp.h" | |
| 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 | |
| 89 | 156 uint8_t hasExternalClock(void) { |
| 157 if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF)) | |
| 158 return 1; | |
| 159 else | |
| 160 return 0; | |
| 38 | 161 } |
| 162 | |
| 163 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000. | |
| 164 // See CPU2-RTE.ld | |
| 100 | 165 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") ))= { |
| 166 .versionFirst = 1, | |
|
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
167 .versionSecond = 7, |
|
134
0586ae83a243
Removed cyclic (by timer) send on slave side
Ideenmodellierer
parents:
120
diff
changeset
|
168 .versionThird = 0, |
| 100 | 169 .versionBeta = 0, |
| 38 | 170 |
| 89 | 171 /* 4 bytes with trailing 0 */ |
| 100 | 172 .signature = "mh", |
| 38 | 173 |
| 100 | 174 .release_year = 18, |
| 175 .release_month = 7, | |
| 176 .release_day = 29, | |
| 177 .release_sub = 0, | |
| 38 | 178 |
| 89 | 179 /* max 48 with trailing 0 */ |
| 180 //release_info ="12345678901234567890123456789012345678901" | |
| 181 .release_info = "compass stuff", | |
| 38 | 182 |
| 89 | 183 /* for safety reasons and coming functions */ |
| 184 .magic[0] = FIRMWARE_MAGIC_FIRST, .magic[1] = FIRMWARE_MAGIC_SECOND, | |
| 185 .magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */ | |
| 186 .magic[3] = FIRMWARE_MAGIC_END }; | |
| 38 | 187 |
| 89 | 188 uint8_t firmwareVersionHigh(void) { |
| 189 return cpu2_FirmwareData.versionFirst; | |
| 38 | 190 } |
| 191 | |
| 89 | 192 uint8_t firmwareVersionLow(void) { |
| 193 return cpu2_FirmwareData.versionSecond; | |
| 38 | 194 } |
| 195 | |
| 196 /** @addtogroup OSTC4 | |
| 197 * @{ | |
| 198 */ | |
| 199 | |
| 200 /** @addtogroup CPU2 | |
| 201 * @{ | |
| 202 */ | |
| 203 | |
| 204 /* Private typedef -----------------------------------------------------------*/ | |
| 205 /* Private define ------------------------------------------------------------*/ | |
| 206 #define BUTTON_OSTC_GPIO_PIN GPIO_PIN_0 | |
| 207 #define BUTTON_OSTC_GPIO_PORT GPIOA | |
| 208 #define BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() | |
| 209 #define BUTTON_OSTC_IRQn EXTI0_IRQn | |
| 210 | |
| 211 #define BUTTON_TEST_GPIO_PIN GPIO_PIN_3 | |
| 212 #define BUTTON_TEST_GPIO_PORT GPIOA | |
| 213 #define BUTTON_TEST_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE() | |
| 214 #define BUTTON_TEST_IRQn EXTI3_IRQn | |
| 215 | |
| 216 #define WIRELSS_RISING_GPIO_PIN GPIO_PIN_1 | |
| 217 #define WIRELSS_RISING_GPIO_PORT GPIOA | |
| 218 #define WIRELSS_RISING_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() | |
| 219 #define WIRELSS_RISING_IRQn EXTI1_IRQn | |
| 220 | |
| 221 #define WIRELSS_FALLING_GPIO_PIN GPIO_PIN_2 | |
| 222 #define WIRELSS_FALLING_GPIO_PORT GPIOA | |
| 223 #define WIRELSS_FALLING_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() | |
| 224 #define WIRELSS_FALLING_IRQn EXTI2_IRQn | |
| 225 | |
| 226 #define WIRELSS_POWER_GPIO_PIN GPIO_PIN_12 | |
| 227 #define WIRELSS_POWER_GPIO_PORT GPIOB | |
| 228 #define WIRELSS_POWER_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE() | |
| 229 | |
| 230 /* Private macro -------------------------------------------------------------*/ | |
| 231 | |
| 232 /* Private variables ---------------------------------------------------------*/ | |
| 233 uint32_t global_test_time_counter = 0; | |
| 234 SBackup backup; | |
| 235 | |
| 236 /* Private function prototypes -----------------------------------------------*/ | |
| 237 static void EXTI_Wakeup_Button_Init(void); | |
| 238 static void EXTI_Wakeup_Button_DeInit(void); | |
| 239 | |
| 240 static void EXTI_Test_Button_Init(void); | |
| 241 static void EXTI_Test_Button_DeInit(void); | |
| 242 | |
| 243 static void MX_EXTI_wireless_Init(void); | |
| 244 static void MX_EXTI_wireless_DeInit(void); | |
| 245 | |
| 246 //static void EXTILine01_Button_DeInit(void); | |
| 247 static void GPIO_LED_Init(void); | |
| 248 static void GPIO_Power_MainCPU_Init(void); | |
| 249 static void GPIO_Power_MainCPU_ON(void); | |
| 250 static void GPIO_Power_MainCPU_OFF(void); | |
| 251 | |
| 252 void GPIO_test_I2C_lines(void); | |
| 253 | |
| 254 //void sleep_test(void); | |
| 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 |
| 89 | 310 MX_I2C1_Init(); |
| 311 if (global.I2C_SystemStatus != HAL_OK) { | |
| 312 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) { | |
| 313 MX_I2C1_TestAndClear(); // do it a second time | |
| 314 } | |
| 315 MX_I2C1_Init(); | |
| 316 } | |
| 38 | 317 |
| 89 | 318 //dangerous: TM_OTP_Write(0,0, 0x01); |
| 38 | 319 #ifdef REGULAR_RUN |
| 89 | 320 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); |
| 321 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; | |
| 322 if (global.I2C_SystemStatus != HAL_OK) { | |
| 323 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) { | |
| 324 MX_I2C1_TestAndClear(); // do it a second time | |
| 325 } | |
| 326 MX_I2C1_Init(); | |
| 327 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); | |
| 328 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; | |
| 329 } | |
| 38 | 330 |
| 89 | 331 global.dataSendToMaster.sensorErrors = |
| 332 global.sensorError[SENSOR_PRESSURE_ID]; | |
| 333 init_surface_ring(); | |
| 334 init_battery_gas_gauge(); | |
| 335 HAL_Delay(10); | |
| 336 battery_gas_gauge_get_data(); | |
| 38 | 337 // battery_gas_gauge_set(0); |
| 338 | |
| 89 | 339 global.lifeData.battery_voltage = get_voltage(); |
| 340 global.lifeData.battery_charge = get_charge(); | |
| 341 copyBatteryData(); | |
| 38 | 342 |
| 89 | 343 MX_SPI3_Init(); |
| 344 if (!scheduleSetButtonResponsiveness()) { | |
| 345 HAL_Delay(1); | |
| 346 scheduleSetButtonResponsiveness(); // init | |
| 347 HAL_Delay(1); | |
| 348 if (!scheduleSetButtonResponsiveness()) // send again, if problem it's not my problem here. | |
| 349 { | |
| 350 HAL_Delay(1); | |
| 351 scheduleSetButtonResponsiveness(); // init | |
| 352 HAL_Delay(1); | |
| 353 } | |
| 354 } | |
| 38 | 355 |
| 89 | 356 ADCx_Init(); |
| 357 GPIO_Power_MainCPU_Init(); | |
| 358 global.mode = MODE_POWERUP; | |
| 38 | 359 #else |
| 89 | 360 init_pressure(); |
| 361 init_surface_ring(); | |
| 38 | 362 |
| 89 | 363 ADCx_Init(); |
| 364 GPIO_Power_MainCPU_Init(); | |
| 365 global.mode = MODE_TEST; | |
| 38 | 366 #endif |
| 89 | 367 while (1) { |
| 120 | 368 /* printf("Global mode = %d\n", global.mode); */ |
| 38 | 369 |
| 89 | 370 switch (global.mode) { |
| 371 case MODE_POWERUP: | |
| 372 case MODE_BOOT: | |
| 373 // ReInit_battery_charger_status_pins(); | |
| 374 compass_init(0, 7); | |
| 375 accelerator_init(); | |
| 376 wireless_init(); | |
| 377 if (global.mode == MODE_BOOT) { | |
| 378 GPIO_Power_MainCPU_OFF(); | |
| 379 HAL_Delay(100); // for GPIO_Power_MainCPU_ON(); | |
| 380 GPIO_Power_MainCPU_ON(); | |
| 381 } | |
| 382 SPI_synchronize_with_Master(); | |
| 383 MX_DMA_Init(); | |
| 384 MX_SPI1_Init(); | |
| 385 MX_EXTI_wireless_Init(); | |
|
134
0586ae83a243
Removed cyclic (by timer) send on slave side
Ideenmodellierer
parents:
120
diff
changeset
|
386 SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */ |
|
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
387 dohardspisync = 1; |
| 89 | 388 EXTI_Test_Button_Init(); |
| 389 | |
| 390 /* | |
| 391 uint8_t dataWireless[64]; | |
| 392 while(1) | |
| 393 { | |
| 394 wireless_evaluate_and_debug(dataWireless,64); | |
| 395 } | |
| 396 */ | |
| 397 global.mode = MODE_SURFACE; | |
| 398 break; | |
| 38 | 399 |
| 89 | 400 case MODE_CALIB: |
| 90 | 401 scheduleCompassCalibrationMode(); |
| 89 | 402 break; |
| 403 | |
| 404 case MODE_SURFACE: | |
| 405 scheduleSurfaceMode(); | |
| 406 break; | |
| 38 | 407 |
| 89 | 408 case MODE_TEST: |
| 409 break; | |
| 38 | 410 |
| 89 | 411 case MODE_DIVE: |
| 90 | 412 backup.no_fly_time_minutes = global.no_fly_time_minutes; |
| 413 backup.seconds_since_last_dive = global.seconds_since_last_dive; | |
| 414 | |
| 415 vpm_init( &global.vpm, global.conservatism, global.repetitive_dive, | |
| 416 global.seconds_since_last_dive ); | |
| 417 global.no_fly_time_minutes = 0; | |
| 418 global.lifeData.dive_time_seconds = 0; | |
| 419 global.lifeData.dive_time_seconds_without_surface_time = 0; | |
| 420 scheduleDiveMode(); | |
| 421 // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1; | |
| 422 | |
| 423 if( global.lifeData.dive_time_seconds > 60 ) | |
| 424 { | |
| 425 //No Fly time 60% of desaturationtime after dive | |
| 426 global.no_fly_time_minutes = decom_calc_desaturation_time( | |
| 427 global.lifeData.tissue_nitrogen_bar, | |
| 428 global.lifeData.tissue_helium_bar, | |
| 429 global.lifeData.pressure_surface_bar ) * 60 / 100; | |
| 430 if( global.no_fly_time_minutes < (24 * 60) ) | |
| 431 global.no_fly_time_minutes = 24 * 60; | |
| 432 } | |
| 433 else | |
| 434 { | |
| 435 global.no_fly_time_minutes = backup.no_fly_time_minutes; | |
| 436 global.seconds_since_last_dive = backup.seconds_since_last_dive; | |
| 437 } | |
| 438 | |
| 439 global.lifeData.dive_time_seconds = 0; | |
| 440 global.lifeData.dive_time_seconds_without_surface_time = 0; | |
| 441 global.lifeData.counterSecondsShallowDepth = 0; | |
| 442 | |
| 443 backup.no_fly_time_minutes = 0; | |
| 444 backup.seconds_since_last_dive = 0; | |
| 89 | 445 break; |
| 38 | 446 |
| 89 | 447 case MODE_SHUTDOWN: |
| 448 HAL_Delay(200); | |
| 449 global.mode = MODE_SLEEP; | |
| 450 MX_SPI3_Init(); | |
| 451 break; | |
| 452 | |
| 453 case MODE_SLEEP: | |
| 454 /* | |
| 455 sleep_prepare(); | |
| 456 scheduleSleepMode_test(); | |
| 457 */ | |
| 458 /* | |
| 459 GPIO_Power_MainCPU_OFF(); | |
| 460 EXTI_Test_Button_DeInit(); | |
| 461 EXTI_Wakeup_Button_Init(); | |
| 462 NOT_USED_AT_THE_MOMENT_scheduleSleepMode(); | |
| 463 */ | |
| 464 EXTI_Test_Button_DeInit(); | |
| 465 MX_EXTI_wireless_DeInit(); | |
| 466 if (hasExternalClock()) | |
| 467 SystemClock_Config_HSI(); | |
| 468 sleep_prepare(); | |
| 38 | 469 |
| 89 | 470 GPIO_LED_Init(); |
| 38 | 471 |
| 89 | 472 scheduleSleepMode(); |
| 473 if (hasExternalClock()) | |
| 474 SystemClock_Config_HSE(); | |
| 475 GPIO_LED_Init(); | |
| 476 EXTI_Wakeup_Button_DeInit(); | |
| 477 ADCx_Init(); | |
| 478 GPIO_Power_MainCPU_Init(); | |
| 479 GPIO_Power_MainCPU_ON(); | |
| 480 compass_init(0, 7); | |
| 481 accelerator_init(); | |
| 482 wireless_init(); | |
|
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
483 SPI_synchronize_with_Master(); |
| 89 | 484 MX_DMA_Init(); |
| 485 MX_SPI1_Init(); | |
| 486 MX_EXTI_wireless_Init(); | |
|
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
134
diff
changeset
|
487 SPI_Start_single_TxRx_with_Master(); |
| 89 | 488 |
| 489 // EXTILine0_Button_DeInit(); not now, later after testing | |
| 490 break; | |
| 491 } | |
| 120 | 492 |
| 493 #ifdef DEBUG_RUNTIME | |
| 494 HAL_RTC_GetTime(&RTCHandle, &Stime, RTC_FORMAT_BCD); | |
| 495 | |
| 496 if(lastsecond == 0xFF) | |
| 497 { | |
| 498 measurementindex = 0; | |
| 499 loopcnt[measurementindex] = 0; | |
| 500 lastsecond = Stime.Seconds; | |
| 501 } | |
| 502 loopcnt[measurementindex]++; | |
| 503 | |
| 504 if(lastsecond != Stime.Seconds) | |
| 505 { | |
| 506 measurementindex++; | |
| 507 if (measurementindex == MEASURECNT) measurementindex = 0; | |
| 508 loopcnt[measurementindex] = 0; | |
| 509 lastsecond = Stime.Seconds; | |
| 510 if(measurementindex +1 < MEASURECNT) loopcnt[measurementindex +1] = 0xffff; /* helps to identify the latest value */ | |
| 511 } | |
| 512 #endif | |
| 89 | 513 } |
| 38 | 514 } |
| 515 | |
| 516 /** @brief Button feedback - EXTI line detection callbacks | |
| 517 * @param GPIO_Pin: Specifies the pins connected EXTI line | |
| 518 * @retval None | |
| 519 */ | |
| 89 | 520 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { |
| 38 | 521 |
| 89 | 522 if (GPIO_Pin == WIRELSS_RISING_GPIO_PIN) { |
| 523 wireless_trigger_RisingEdgeSilence(); | |
| 524 } | |
| 38 | 525 |
| 89 | 526 else |
| 38 | 527 |
| 89 | 528 if (GPIO_Pin == WIRELSS_FALLING_GPIO_PIN) { |
| 529 wireless_trigger_FallingEdgeSignalHigh(); | |
| 530 } | |
| 38 | 531 |
| 89 | 532 else |
| 38 | 533 |
| 89 | 534 if (GPIO_Pin == BUTTON_OSTC_GPIO_PIN) { |
| 535 if (global.mode == MODE_SLEEP) { | |
| 536 global.mode = MODE_BOOT; | |
| 537 } | |
| 538 } | |
| 38 | 539 |
| 89 | 540 else |
| 38 | 541 |
| 89 | 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 | |
| 89 | 903 static void MX_EXTI_wireless_Init(void) { |
| 904 GPIO_InitTypeDef GPIO_InitStructure; | |
| 38 | 905 |
| 89 | 906 WIRELSS_POWER_HAL_RCC_GPIO_CLK_ENABLE(); |
| 907 GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN; | |
| 908 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
| 909 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
| 910 HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure); | |
| 911 HAL_GPIO_WritePin( WIRELSS_POWER_GPIO_PORT, WIRELSS_POWER_GPIO_PIN, | |
| 912 GPIO_PIN_SET); | |
| 38 | 913 |
| 89 | 914 WIRELSS_RISING_HAL_RCC_GPIO_CLK_ENABLE(); |
| 915 GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN; | |
| 916 GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING; | |
| 917 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
| 918 HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure); | |
| 38 | 919 |
| 89 | 920 HAL_NVIC_SetPriority( WIRELSS_RISING_IRQn, 0x02, 0); |
| 921 HAL_NVIC_EnableIRQ( WIRELSS_RISING_IRQn); | |
| 38 | 922 |
| 89 | 923 WIRELSS_FALLING_HAL_RCC_GPIO_CLK_ENABLE(); |
| 924 GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN; | |
| 925 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; | |
| 926 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
| 927 HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure); | |
| 38 | 928 |
| 89 | 929 HAL_NVIC_SetPriority( WIRELSS_FALLING_IRQn, 0x02, 0); |
| 930 HAL_NVIC_EnableIRQ( WIRELSS_FALLING_IRQn); | |
| 38 | 931 |
| 932 } | |
| 933 | |
| 89 | 934 static void MX_EXTI_wireless_DeInit(void) { |
| 935 GPIO_InitTypeDef GPIO_InitStructure; | |
| 38 | 936 |
| 89 | 937 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; |
| 938 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
| 939 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
| 38 | 940 |
| 89 | 941 GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN; |
| 942 HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure); | |
| 38 | 943 |
| 89 | 944 GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN; |
| 945 HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure); | |
| 38 | 946 |
| 89 | 947 GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN; |
| 948 HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure); | |
| 38 | 949 |
| 89 | 950 HAL_NVIC_DisableIRQ( WIRELSS_RISING_IRQn); |
| 951 HAL_NVIC_DisableIRQ( WIRELSS_FALLING_IRQn); | |
| 38 | 952 } |
| 953 | |
| 954 /* NUCLEO C 13 | |
| 955 KEY_BUTTON_GPIO_CLK_ENABLE(); | |
| 956 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; | |
| 957 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
| 958 GPIO_InitStructure.Pin = KEY_BUTTON_PIN; | |
| 959 HAL_GPIO_Init(KEY_BUTTON_GPIO_PORT, &GPIO_InitStructure); | |
| 960 HAL_NVIC_SetPriority(KEY_BUTTON_EXTI_IRQn, 2, 0); | |
| 961 HAL_NVIC_EnableIRQ(KEY_BUTTON_EXTI_IRQn); | |
| 962 */ | |
| 963 | |
| 964 /** | |
| 965 * @brief Wake Up Timer callback | |
| 966 * @param hrtc: RTC handle | |
| 967 * @retval None | |
| 968 */ | |
| 969 | |
| 970 /* | |
| 971 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) | |
| 972 { | |
| 973 static uint8_t uwCounter = 0; | |
| 974 uwCounter = 1; | |
| 975 } | |
| 976 */ | |
| 977 | |
| 89 | 978 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle) { |
| 38 | 979 |
| 980 } | |
| 981 | |
| 89 | 982 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle) { |
| 38 | 983 |
| 984 } | |
| 985 | |
| 89 | 986 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle) { |
| 38 | 987 |
| 988 } | |
| 989 | |
| 89 | 990 void sleep_prepare(void) { |
| 991 EXTI_Wakeup_Button_Init(); | |
| 992 /* | |
| 993 GPIO_InitStruct.Pull = GPIO_PULLUP; | |
| 994 GPIO_InitStruct.Pin = GPIO_PIN_0; | |
| 995 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
| 996 */ | |
| 997 compass_sleep(); | |
| 998 HAL_Delay(100); | |
| 999 accelerator_sleep(); | |
| 1000 HAL_Delay(100); | |
| 38 | 1001 |
| 89 | 1002 I2C_DeInit(); |
| 1003 MX_SPI_DeInit(); | |
| 1004 MX_SPI3_DeInit(); | |
| 1005 ADCx_DeInit(); | |
| 38 | 1006 |
| 89 | 1007 GPIO_InitTypeDef GPIO_InitStruct; |
| 38 | 1008 |
| 89 | 1009 __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 1010 __HAL_RCC_GPIOB_CLK_ENABLE(); | |
| 1011 __HAL_RCC_GPIOC_CLK_ENABLE(); | |
| 1012 __HAL_RCC_GPIOH_CLK_ENABLE(); | |
| 38 | 1013 |
| 89 | 1014 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
| 1015 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | |
| 1016 GPIO_InitStruct.Pull = GPIO_NOPULL; | |
| 1017 GPIO_InitStruct.Pin = GPIO_PIN_All; | |
| 1018 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | |
| 38 | 1019 #ifdef DEBUGMODE |
| 89 | 1020 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */ |
| 38 | 1021 #endif |
| 89 | 1022 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct); |
| 38 | 1023 |
| 89 | 1024 GPIO_InitStruct.Pin = |
| 1025 GPIO_PIN_All | |
| 1026 ^ ( GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_14 | |
| 1027 | GPIO_PIN_15); /* power off & charger in & charge out & OSC32*/ | |
| 1028 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct); | |
| 38 | 1029 |
| 89 | 1030 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0); |
| 38 | 1031 #ifdef DEBUGMODE |
| 89 | 1032 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */ |
| 38 | 1033 #endif |
| 89 | 1034 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct); |
| 38 | 1035 |
| 89 | 1036 GPIO_InitStruct.Pin = GPIO_PIN_All; |
| 1037 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | |
| 38 | 1038 |
| 89 | 1039 GPIO_Power_MainCPU_OFF(); |
| 38 | 1040 |
| 1041 #ifndef DEBUGMODE | |
| 89 | 1042 __HAL_RCC_GPIOB_CLK_DISABLE(); |
| 38 | 1043 #endif |
| 89 | 1044 __HAL_RCC_GPIOH_CLK_DISABLE(); |
| 38 | 1045 |
| 89 | 1046 HAL_Delay(1000); |
| 38 | 1047 } |
| 1048 | |
| 1049 /* | |
| 1050 void sleep_test(void) | |
| 1051 { | |
| 1052 GPIO_InitTypeDef GPIO_InitStruct; | |
| 1053 | |
| 1054 __HAL_RCC_GPIOA_CLK_ENABLE(); | |
| 1055 __HAL_RCC_GPIOB_CLK_ENABLE(); | |
| 1056 __HAL_RCC_GPIOC_CLK_ENABLE(); | |
| 1057 __HAL_RCC_GPIOH_CLK_ENABLE(); | |
| 1058 | |
| 1059 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; | |
| 1060 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | |
| 1061 GPIO_InitStruct.Pull = GPIO_NOPULL; | |
| 1062 GPIO_InitStruct.Pin = GPIO_PIN_All; | |
| 1063 HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); | |
| 1064 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |
| 1065 | |
| 1066 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_15 | GPIO_PIN_14); | |
| 1067 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); | |
| 1068 | |
| 1069 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0); | |
| 1070 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
| 1071 | |
| 1072 GPIO_Power_MainCPU_OFF(); | |
| 1073 | |
| 1074 GPIO_InitStruct.Pull = GPIO_PULLUP; | |
| 1075 GPIO_InitStruct.Pin = GPIO_PIN_0; | |
| 1076 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
| 1077 | |
| 1078 // __HAL_RCC_GPIOA_CLK_DISABLE(); | |
| 1079 __HAL_RCC_GPIOB_CLK_DISABLE(); | |
| 1080 // __HAL_RCC_GPIOC_CLK_DISABLE(); | |
| 1081 __HAL_RCC_GPIOH_CLK_DISABLE(); | |
| 1082 | |
| 1083 | |
| 1084 HAL_Delay(5000); | |
| 1085 while(1) | |
| 1086 { | |
| 1087 RTC_StopMode_2seconds(); | |
| 1088 HAL_Delay(200); | |
| 1089 } | |
| 1090 } | |
| 1091 */ | |
| 1092 | |
| 1093 #ifdef USE_FULL_ASSERT | |
| 1094 | |
| 1095 /** | |
| 1096 * @brief Reports the name of the source file and the source line number | |
| 1097 * where the assert_param error has occurred. | |
| 1098 * @param file: pointer to the source file name | |
| 1099 * @param line: assert_param error line source number | |
| 1100 * @retval None | |
| 1101 */ | |
| 1102 void assert_failed(uint8_t* file, uint32_t line) | |
| 1103 { | |
| 89 | 1104 /* User can add his own implementation to report the file name and line number, |
| 1105 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ | |
| 38 | 1106 |
| 89 | 1107 /* Infinite loop */ |
| 1108 while (1) | |
| 1109 { | |
| 1110 } | |
| 38 | 1111 } |
| 1112 #endif | |
| 1113 | |
| 1114 /** | |
| 1115 * @} | |
| 1116 */ | |
| 104 | 1117 /** |
| 1118 * @brief This function handles SysTick Handler. | |
| 1119 * @param None | |
| 1120 * @retval None | |
| 1121 */ | |
| 38 | 1122 |
| 104 | 1123 /*TxRx only here. Every 100 ms.*/ |
| 1124 uint8_t ticks100ms=0; | |
| 1125 void SysTick_Handler(void) | |
| 1126 { | |
| 1127 HAL_IncTick(); | |
| 1128 if(ticks100ms<100){ | |
| 1129 ticks100ms++; | |
| 1130 }else | |
| 1131 { | |
| 1132 ticks100ms=0; | |
| 1133 } | |
| 1134 } | |
| 38 | 1135 /** |
| 1136 * @} | |
| 1137 */ | |
| 1138 | |
| 1139 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |
