Mercurial > public > ostc4
comparison Small_CPU/Src/baseCPU2.c @ 981:c6c781a2e85b default
Merge into default
| author | heinrichsweikamp |
|---|---|
| date | Tue, 11 Feb 2025 18:12:00 +0100 |
| parents | 81049905d829 |
| children | d9290c76b840 |
comparison
equal
deleted
inserted
replaced
| 871:f7318457df4d | 981:c6c781a2e85b |
|---|---|
| 133 #include "dma.h" | 133 #include "dma.h" |
| 134 #include "i2c.h" | 134 #include "i2c.h" |
| 135 #include "spi.h" | 135 #include "spi.h" |
| 136 #include "rtc.h" | 136 #include "rtc.h" |
| 137 #include "adc.h" | 137 #include "adc.h" |
| 138 #include "gpio.h" | |
| 138 #include "compass.h" | 139 #include "compass.h" |
| 139 #include "pressure.h" | 140 #include "pressure.h" |
| 140 #include "batteryGasGauge.h" | 141 #include "batteryGasGauge.h" |
| 141 #include "batteryCharger.h" | 142 #include "batteryCharger.h" |
| 142 #include "scheduler.h" | 143 #include "scheduler.h" |
| 143 #include "tm_stm32f4_otp.h" | 144 #include "tm_stm32f4_otp.h" |
| 144 #include "externalInterface.h" | 145 #include "externalInterface.h" |
| 145 #include "uart.h" | 146 #include "uart.h" |
| 147 #include "uart_Internal.h" | |
| 148 #include "uartProtocol_GNSS.h" | |
| 149 #include "GNSS.h" | |
| 150 #include "configuration.h" | |
| 151 | |
| 146 | 152 |
| 147 // From Common/Inc: | 153 // From Common/Inc: |
| 148 #include "calc_crush.h" | 154 #include "calc_crush.h" |
| 149 #include "decom.h" | 155 #include "decom.h" |
| 150 #include "FirmwareData.h" | 156 #include "FirmwareData.h" |
| 151 | 157 |
| 152 // From Common/Drivers/ | 158 // From Common/Drivers/ |
| 153 #include "stm32f4xx_hal.h" | |
| 154 #include <stdio.h> | 159 #include <stdio.h> |
| 160 | |
| 155 | 161 |
| 156 uint8_t coldstart __attribute__((section (".noinit"))); | 162 uint8_t coldstart __attribute__((section (".noinit"))); |
| 157 | 163 |
| 158 uint8_t hasExternalClock(void) { | 164 uint8_t hasExternalClock(void) { |
| 159 if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF)) | 165 if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF)) |
| 164 | 170 |
| 165 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000. | 171 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000. |
| 166 // See CPU2-RTE.ld | 172 // See CPU2-RTE.ld |
| 167 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") ))= { | 173 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") ))= { |
| 168 .versionFirst = 3, | 174 .versionFirst = 3, |
| 169 .versionSecond = 2, | 175 .versionSecond = 4, |
| 170 .versionThird = 0, | 176 .versionThird = 0, |
| 171 .versionBeta = 0, | 177 .versionBeta = 0, |
| 172 | 178 |
| 173 /* 4 bytes with trailing 0 */ | 179 /* 4 bytes with trailing 0 */ |
| 174 .signature = "mh", | 180 .signature = "mh", |
| 175 | 181 |
| 176 .release_year = 23, | 182 .release_year = 25, |
| 177 .release_month = 9, | 183 .release_month = 1, |
| 178 .release_day = 14, | 184 .release_day = 18, |
| 179 .release_sub = 0, | 185 .release_sub = 0, |
| 180 | 186 |
| 181 /* max 48 with trailing 0 */ | 187 /* max 48 with trailing 0 */ |
| 182 //release_info ="12345678901234567890123456789012345678901" | 188 //release_info ="12345678901234567890123456789012345678901" |
| 183 .release_info = "stable April 2023", | 189 .release_info = "stable April 2023", |
| 210 #define BUTTON_OSTC_GPIO_PIN GPIO_PIN_0 | 216 #define BUTTON_OSTC_GPIO_PIN GPIO_PIN_0 |
| 211 #define BUTTON_OSTC_GPIO_PORT GPIOA | 217 #define BUTTON_OSTC_GPIO_PORT GPIOA |
| 212 #define BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() | 218 #define BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() |
| 213 #define BUTTON_OSTC_IRQn EXTI0_IRQn | 219 #define BUTTON_OSTC_IRQn EXTI0_IRQn |
| 214 | 220 |
| 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 | |
| 234 | |
| 235 #define LED_CONTROL_PIN GPIO_PIN_3 /* PortC */ | |
| 236 #define MAINCPU_CONTROL_PIN GPIO_PIN_0 /* PortC */ | |
| 237 | 221 |
| 238 /* Private macro -------------------------------------------------------------*/ | 222 /* Private macro -------------------------------------------------------------*/ |
| 239 | 223 |
| 240 /* Private variables ---------------------------------------------------------*/ | 224 /* Private variables ---------------------------------------------------------*/ |
| 241 uint32_t global_test_time_counter = 0; | 225 uint32_t global_test_time_counter = 0; |
| 242 SBackup backup; | 226 SBackup backup; |
| 243 | 227 |
| 244 /* Private function prototypes -----------------------------------------------*/ | 228 /* Private function prototypes -----------------------------------------------*/ |
| 245 static void EXTI_Wakeup_Button_Init(void); | 229 static void EXTI_Wakeup_Button_Init(void); |
| 246 static void EXTI_Wakeup_Button_DeInit(void); | 230 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 | 231 |
| 256 #ifdef DEBUG_I2C_LINES | 232 #ifdef DEBUG_I2C_LINES |
| 257 void GPIO_test_I2C_lines(void); | 233 void GPIO_test_I2C_lines(void); |
| 258 #endif | 234 #endif |
| 259 | 235 |
| 296 RTC_TimeTypeDef Stime; | 272 RTC_TimeTypeDef Stime; |
| 297 uint8_t measurementindex = 0; | 273 uint8_t measurementindex = 0; |
| 298 uint8_t lastsecond = 0xFF; | 274 uint8_t lastsecond = 0xFF; |
| 299 #endif | 275 #endif |
| 300 | 276 |
| 277 uint8_t extInterfaceActive = 0; | |
| 278 uint32_t shutdownTick = 0; | |
| 279 | |
| 301 HAL_Init(); | 280 HAL_Init(); |
| 302 SystemClock_Config(); | 281 SystemClock_Config(); |
| 303 | 282 |
| 304 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); | 283 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); |
| 305 HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK); | 284 HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK); |
| 306 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); | 285 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); |
| 307 | 286 |
| 308 MX_RTC_init(); | 287 MX_RTC_init(); |
| 309 GPIO_LED_Init(); | 288 GPIO_LEDs_VIBRATION_Init(); |
| 289 GPIO_GNSS_Init(); | |
| 310 GPIO_new_DEBUG_Init(); // added 170322 hw | 290 GPIO_new_DEBUG_Init(); // added 170322 hw |
| 311 initGlobals(); | 291 initGlobals(); |
| 312 | 292 |
| 313 /* printf("CPU2-RTE running...\n"); */ | 293 /* printf("CPU2-RTE running...\n"); */ |
| 314 | 294 |
| 401 | 381 |
| 402 ADCx_Init(); | 382 ADCx_Init(); |
| 403 GPIO_Power_MainCPU_Init(); | 383 GPIO_Power_MainCPU_Init(); |
| 404 global.mode = MODE_TEST; | 384 global.mode = MODE_TEST; |
| 405 #endif | 385 #endif |
| 386 | |
| 387 GNSS_Handle.alive = 0; /* only init at startup (outside init function) */ | |
| 388 GNSS_Handle.last_fLat = 0.0; | |
| 389 GNSS_Handle.last_fLon = 0.0; | |
| 390 GNSS_Handle.last_hour = 0; | |
| 391 | |
| 406 while (1) { | 392 while (1) { |
| 407 /* printf("Global mode = %d\n", global.mode); */ | 393 /* printf("Global mode = %d\n", global.mode); */ |
| 408 | 394 |
| 409 switch (global.mode) { | 395 switch (global.mode) { |
| 410 case MODE_POWERUP: | 396 case MODE_POWERUP: |
| 414 accelerator_init(); | 400 accelerator_init(); |
| 415 externalInterface_Init(); | 401 externalInterface_Init(); |
| 416 | 402 |
| 417 if (global.mode == MODE_BOOT) { | 403 if (global.mode == MODE_BOOT) { |
| 418 GPIO_Power_MainCPU_OFF(); | 404 GPIO_Power_MainCPU_OFF(); |
| 405 #ifdef ENABLE_GPIO_V2 | |
| 406 GPIO_LED_GREEN_ON(); | |
| 407 #endif | |
| 419 HAL_Delay(100); // for GPIO_Power_MainCPU_ON(); | 408 HAL_Delay(100); // for GPIO_Power_MainCPU_ON(); |
| 420 GPIO_Power_MainCPU_ON(); | 409 GPIO_Power_MainCPU_ON(); |
| 410 #ifdef ENABLE_GPIO_V2 | |
| 411 GPIO_LED_GREEN_OFF(); | |
| 412 | |
| 413 GPIO_LED_RED_ON(); | |
| 414 GPIO_VIBRATION_ON(); | |
| 415 #endif | |
| 416 HAL_Delay(100); | |
| 417 #ifdef ENABLE_GPIO_V2 | |
| 418 GPIO_LED_RED_OFF(); | |
| 419 GPIO_VIBRATION_OFF(); | |
| 420 #endif | |
| 421 } | 421 } |
| 422 #ifdef ENABLE_GPIO_V2 | |
| 423 GPIO_LED_RED_OFF(); | |
| 424 GPIO_LED_GREEN_OFF(); | |
| 425 GPIO_VIBRATION_OFF(); | |
| 426 #endif | |
| 422 SPI_synchronize_with_Master(); | 427 SPI_synchronize_with_Master(); |
| 423 MX_DMA_Init(); | 428 MX_DMA_Init(); |
| 424 MX_SPI1_Init(); | 429 MX_SPI1_Init(); |
| 425 SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */ | 430 SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */ |
| 426 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); | 431 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); |
| 427 EXTI_Test_Button_Init(); | 432 |
| 433 #ifdef ENABLE_GPIO_V2 | |
| 434 // GNSS tests | |
| 435 GNSS_IO_init(); | |
| 436 GPIO_GPS_ON(); | |
| 437 GPIO_GPS_BCKP_ON(); | |
| 438 MX_USART6_UART_Init(); | |
| 439 GNSS_Init(&GNSS_Handle, &huart6); | |
| 440 #else | |
| 441 #ifdef ENABLE_GNSS_SUPPORT | |
| 442 GNSS_Init(&GNSS_Handle, &huart1); | |
| 443 #endif | |
| 444 #endif | |
| 445 | |
| 428 global.mode = MODE_SURFACE; | 446 global.mode = MODE_SURFACE; |
| 429 break; | 447 break; |
| 430 | 448 |
| 431 case MODE_CALIB: | 449 case MODE_CALIB: |
| 432 scheduleCompassCalibrationMode(); | 450 scheduleCompassCalibrationMode(); |
| 446 vpm_init( &global.vpm, global.conservatism, global.repetitive_dive, | 464 vpm_init( &global.vpm, global.conservatism, global.repetitive_dive, |
| 447 global.seconds_since_last_dive ); | 465 global.seconds_since_last_dive ); |
| 448 global.no_fly_time_minutes = 0; | 466 global.no_fly_time_minutes = 0; |
| 449 global.lifeData.dive_time_seconds = 0; | 467 global.lifeData.dive_time_seconds = 0; |
| 450 global.lifeData.dive_time_seconds_without_surface_time = 0; | 468 global.lifeData.dive_time_seconds_without_surface_time = 0; |
| 469 #if defined ENABLE_GNSS_SUPPORT || defined ENABLE_GPIO_V2 | |
| 470 uartGnss_ReqPowerDown(1); | |
| 471 #endif | |
| 451 scheduleDiveMode(); | 472 scheduleDiveMode(); |
| 452 // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1; | 473 // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1; |
| 453 | 474 |
| 454 if( global.lifeData.dive_time_seconds > 60 ) | 475 if( global.lifeData.dive_time_seconds > 60 ) |
| 455 { | 476 { |
| 471 global.lifeData.dive_time_seconds_without_surface_time = 0; | 492 global.lifeData.dive_time_seconds_without_surface_time = 0; |
| 472 global.lifeData.counterSecondsShallowDepth = 0; | 493 global.lifeData.counterSecondsShallowDepth = 0; |
| 473 | 494 |
| 474 backup.no_fly_time_minutes = 0; | 495 backup.no_fly_time_minutes = 0; |
| 475 backup.seconds_since_last_dive = 0; | 496 backup.seconds_since_last_dive = 0; |
| 497 #if defined ENABLE_GNSS_SUPPORT || defined ENABLE_GPIO_V2 | |
| 498 uartGnss_ReqPowerDown(0); | |
| 499 #endif | |
| 476 break; | 500 break; |
| 477 | 501 |
| 478 case MODE_SHUTDOWN: | 502 case MODE_SHUTDOWN: |
| 479 HAL_Delay(200); | 503 HAL_Delay(200); |
| 504 | |
| 505 MX_SPI3_Init(); | |
| 506 | |
| 507 #if defined ENABLE_GNSS_SUPPORT || defined ENABLE_GPIO_V2 | |
| 508 if(shutdownTick == 0) | |
| 509 { | |
| 510 shutdownTick = HAL_GetTick(); | |
| 511 uartGnss_ReqPowerDown(1); | |
| 512 } | |
| 513 #ifdef ENABLE_GNSS_SUPPORT | |
| 514 externalInterface_HandleUART(); | |
| 515 #else | |
| 516 UART6_HandleUART(); | |
| 517 #endif | |
| 518 if((uartGnss_GetState() == UART_GNSS_INACTIVE) || (time_elapsed_ms(shutdownTick,HAL_GetTick()) > 3000)) | |
| 519 { | |
| 520 global.mode = MODE_SLEEP; | |
| 521 uartGnss_ReqPowerDown(0); /* release power down request */ | |
| 522 } | |
| 523 #else | |
| 480 global.mode = MODE_SLEEP; | 524 global.mode = MODE_SLEEP; |
| 481 MX_SPI3_Init(); | 525 #endif |
| 526 | |
| 527 | |
| 482 break; | 528 break; |
| 483 | 529 |
| 484 case MODE_SLEEP: | 530 case MODE_SLEEP: |
| 485 /* | 531 extInterfaceActive = externalInterface_isEnabledPower33(); |
| 486 sleep_prepare(); | 532 externalInterface_SwitchUART(EXT_INTERFACE_UART_OFF); |
| 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 */ | |
| 495 | |
| 496 EXTI_Test_Button_DeInit(); | |
| 497 externalInterface_SwitchUART(0); | |
| 498 externalInterface_SwitchPower33(false); | 533 externalInterface_SwitchPower33(false); |
| 499 if (hasExternalClock()) | 534 if (hasExternalClock()) |
| 500 SystemClock_Config_HSI(); | 535 SystemClock_Config_HSI(); |
| 536 GPIO_LEDs_VIBRATION_Init(); | |
| 501 sleep_prepare(); | 537 sleep_prepare(); |
| 502 | 538 |
| 503 GPIO_LED_Init(); | 539 while(time_elapsed_ms(shutdownTick,HAL_GetTick()) < 1000 ) /* delay shutdown till shutdown animation is finished */ |
| 504 | 540 { |
| 541 HAL_Delay(10); | |
| 542 } | |
| 543 shutdownTick = 0; | |
| 505 scheduleSleepMode(); | 544 scheduleSleepMode(); |
| 506 if (hasExternalClock()) | 545 if (hasExternalClock()) |
| 507 SystemClock_Config_HSE(); | 546 SystemClock_Config_HSE(); |
| 508 GPIO_LED_Init(); | |
| 509 EXTI_Wakeup_Button_DeInit(); | 547 EXTI_Wakeup_Button_DeInit(); |
| 510 ADCx_Init(); | 548 ADCx_Init(); |
| 511 GPIO_Power_MainCPU_Init(); | 549 GPIO_Power_MainCPU_Init(); |
| 512 GPIO_Power_MainCPU_ON(); | 550 GPIO_Power_MainCPU_ON(); |
| 513 compass_init(0, 7); | 551 compass_init(0, 7); |
| 515 SPI_synchronize_with_Master(); | 553 SPI_synchronize_with_Master(); |
| 516 MX_DMA_Init(); | 554 MX_DMA_Init(); |
| 517 MX_SPI1_Init(); | 555 MX_SPI1_Init(); |
| 518 SPI_Start_single_TxRx_with_Master(); | 556 SPI_Start_single_TxRx_with_Master(); |
| 519 | 557 |
| 520 if(externalInterface_isEnabledPower33()) | 558 if(extInterfaceActive) |
| 521 { | 559 { |
| 522 externalInterface_SwitchPower33(true); | 560 externalInterface_SwitchPower33(true); |
| 523 } | 561 } |
| 524 externalInterface_InitDatastruct(); | 562 externalInterface_InitDatastruct(); |
| 525 // EXTILine0_Button_DeInit(); not now, later after testing | 563 // EXTILine0_Button_DeInit(); not now, later after testing |
| 559 if (global.mode == MODE_SLEEP) { | 597 if (global.mode == MODE_SLEEP) { |
| 560 global.mode = MODE_BOOT; | 598 global.mode = MODE_BOOT; |
| 561 } | 599 } |
| 562 } | 600 } |
| 563 else | 601 else |
| 564 if (GPIO_Pin == BUTTON_TEST_GPIO_PIN) { | 602 { |
| 565 if (!global.demo_mode && (global.mode == MODE_SURFACE)) { | 603 |
| 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 } | 604 } |
| 575 } | 605 } |
| 576 | 606 |
| 577 /** | 607 /** |
| 578 * @brief System Clock Configuration | 608 * @brief System Clock Configuration |
| 611 RCC_ClkInitTypeDef RCC_ClkInitStruct; | 641 RCC_ClkInitTypeDef RCC_ClkInitStruct; |
| 612 // RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; | 642 // RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
| 613 | 643 |
| 614 __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE(); | 644 __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE(); |
| 615 | 645 |
| 616 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); | 646 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 617 | 647 |
| 618 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE; | 648 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE; |
| 619 RCC_OscInitStruct.HSEState = RCC_HSE_ON; | 649 RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
| 620 //RCC_OscInitStruct.LSEState = RCC_LSE_ON; | 650 //RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
| 621 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | 651 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 654 __HAL_RCC_PWR_CLK_ENABLE(); | 684 __HAL_RCC_PWR_CLK_ENABLE(); |
| 655 | 685 |
| 656 /* The voltage scaling allows optimizing the power consumption when the device is | 686 /* 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 | 687 clocked below the maximum system frequency, to update the voltage scaling value |
| 658 regarding system frequency refer to product datasheet. */ | 688 regarding system frequency refer to product datasheet. */ |
| 659 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); | 689 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 660 | 690 |
| 661 /* Enable HSI Oscillator and activate PLL with HSI as source */ | 691 /* Enable HSI Oscillator and activate PLL with HSI as source */ |
| 662 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; | 692 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| 663 RCC_OscInitStruct.HSIState = RCC_HSI_ON; | 693 RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| 664 RCC_OscInitStruct.HSICalibrationValue = 0x10; | 694 RCC_OscInitStruct.HSICalibrationValue = 0x10; |
| 678 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | 708 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
| 679 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; | 709 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
| 680 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | 710 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 681 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); | 711 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); |
| 682 } | 712 } |
| 683 /* | 713 |
| 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 | 714 |
| 722 /** | 715 /** |
| 723 * @brief Configures system clock after wake-up from STOP: enable HSI, PLL | 716 * @brief Configures system clock after wake-up from STOP: enable HSI, PLL |
| 724 * and select PLL as system clock source. | 717 * and select PLL as system clock source. |
| 725 * @param None | 718 * @param None |
| 809 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); | 802 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); |
| 810 HAL_Delay(10); | 803 HAL_Delay(10); |
| 811 } | 804 } |
| 812 */ | 805 */ |
| 813 | 806 |
| 814 static void GPIO_LED_Init(void) { | |
| 815 GPIO_InitTypeDef GPIO_InitStructure; | |
| 816 | |
| 817 __GPIOC_CLK_ENABLE(); | |
| 818 GPIO_InitStructure.Pin = LED_CONTROL_PIN; | |
| 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); | |
| 823 } | |
| 824 | |
| 825 void GPIO_new_DEBUG_Init(void) { | 807 void GPIO_new_DEBUG_Init(void) { |
| 826 #ifdef DEBUG_PIN_ACTIVE | 808 #ifdef DEBUG_PIN_ACTIVE |
| 827 GPIO_InitTypeDef GPIO_InitStructure; | 809 GPIO_InitTypeDef GPIO_InitStructure; |
| 828 | 810 |
| 829 __GPIOC_CLK_ENABLE(); | 811 __GPIOC_CLK_ENABLE(); |
| 845 #ifdef DEBUG_PIN_ACTIVE | 827 #ifdef DEBUG_PIN_ACTIVE |
| 846 HAL_GPIO_WritePin(GPIOC,LED_CONTROL_PIN,GPIO_PIN_SET); | 828 HAL_GPIO_WritePin(GPIOC,LED_CONTROL_PIN,GPIO_PIN_SET); |
| 847 #endif | 829 #endif |
| 848 } | 830 } |
| 849 | 831 |
| 850 static void GPIO_Power_MainCPU_Init(void) { | |
| 851 GPIO_InitTypeDef GPIO_InitStructure; | |
| 852 __GPIOC_CLK_ENABLE(); | |
| 853 GPIO_InitStructure.Pin = MAINCPU_CONTROL_PIN; | |
| 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); | |
| 858 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_RESET); | |
| 859 } | |
| 860 | |
| 861 static void GPIO_Power_MainCPU_ON(void) { | |
| 862 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_RESET); | |
| 863 } | |
| 864 | |
| 865 static void GPIO_Power_MainCPU_OFF(void) { | |
| 866 HAL_GPIO_WritePin( GPIOC, MAINCPU_CONTROL_PIN, GPIO_PIN_SET); | |
| 867 } | |
| 868 | |
| 869 /** | 832 /** |
| 870 * @brief Configures EXTI Line0 (connected to PA0 + PA1 pin) in interrupt mode | 833 * @brief Configures EXTI Line0 (connected to PA0 + PA1 pin) in interrupt mode |
| 871 * @param None | 834 * @param None |
| 872 * @retval None | 835 * @retval None |
| 873 */ | 836 */ |
| 896 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; | 859 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; |
| 897 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure); | 860 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure); |
| 898 HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn); | 861 HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn); |
| 899 } | 862 } |
| 900 | 863 |
| 901 static void EXTI_Test_Button_Init(void) { | |
| 902 GPIO_InitTypeDef GPIO_InitStructure; | |
| 903 | |
| 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); | |
| 911 } | |
| 912 | |
| 913 static void EXTI_Test_Button_DeInit(void) { | |
| 914 GPIO_InitTypeDef GPIO_InitStructure; | |
| 915 | |
| 916 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; | |
| 917 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
| 918 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
| 919 | |
| 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); | |
| 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 /** | 864 /** |
| 936 * @brief Wake Up Timer callback | 865 * @brief Wake Up Timer callback |
| 937 * @param hrtc: RTC handle | 866 * @param hrtc: RTC handle |
| 938 * @retval None | 867 * @retval None |
| 939 */ | 868 */ |
| 977 __HAL_RCC_GPIOB_CLK_ENABLE(); | 906 __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 978 __HAL_RCC_GPIOC_CLK_ENABLE(); | 907 __HAL_RCC_GPIOC_CLK_ENABLE(); |
| 979 __HAL_RCC_GPIOH_CLK_ENABLE(); | 908 __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 980 | 909 |
| 981 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; | 910 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
| 982 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | 911 GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
| 983 GPIO_InitStruct.Pull = GPIO_NOPULL; | 912 GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 984 GPIO_InitStruct.Pin = GPIO_PIN_All; | 913 GPIO_InitStruct.Pin = GPIO_PIN_All; |
| 985 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | 914 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); |
| 986 #ifdef DEBUGMODE | 915 #ifdef ENABLE_SLEEP_DEBUG |
| 987 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */ | 916 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */ |
| 988 #endif | 917 #endif |
| 918 | |
| 919 /* | |
| 920 GPIO_InitStruct.Pin = GPIO_PIN_All ^ (GPS_POWER_CONTROL_PIN | GPS_BCKP_CONTROL_PIN); | |
| 989 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct); | 921 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct); |
| 990 | 922 */ |
| 991 GPIO_InitStruct.Pin = | 923 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( MAINCPU_CONTROL_PIN | CHARGE_OUT_PIN | EXT33V_CONTROL_PIN); /* power off & charger in & charge out & OSC32 & ext33Volt */ |
| 992 GPIO_PIN_All | |
| 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 | 924 |
| 995 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct); | 925 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct); |
| 996 | 926 |
| 997 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0); | 927 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | VIBRATION_CONTROL_PIN | LED_CONTROL_PIN_RED | LED_CONTROL_PIN_GREEN); |
| 998 #ifdef DEBUGMODE | 928 #ifdef ENABLE_SLEEP_DEBUG |
| 999 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */ | 929 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */ |
| 1000 #endif | 930 #endif |
| 1001 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct); | 931 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct); |
| 1002 | 932 |
| 1003 GPIO_InitStruct.Pin = GPIO_PIN_All; | 933 GPIO_InitStruct.Pin = GPIO_PIN_All; |
| 1004 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); | 934 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct); |
| 1005 | 935 |
| 1006 GPIO_Power_MainCPU_OFF(); | 936 GPIO_Power_MainCPU_OFF(); |
| 1007 | 937 #ifdef ENABLE_GPIO_V2 |
| 1008 #ifndef DEBUGMODE | 938 GPIO_LED_GREEN_OFF(); |
| 939 GPIO_LED_RED_OFF(); | |
| 940 GPIO_VIBRATION_OFF(); | |
| 941 GPIO_GPS_BCKP_ON(); // mH : costs 100µA in sleep - beware | |
| 942 /* GPIO_GPS_OFF(); will be done in transition sleep => deep sleep */ | |
| 943 | |
| 944 MX_USART6_UART_DeInit(); | |
| 945 #endif | |
| 946 #ifndef ENABLE_SLEEP_DEBUG | |
| 947 /* | |
| 1009 __HAL_RCC_GPIOB_CLK_DISABLE(); | 948 __HAL_RCC_GPIOB_CLK_DISABLE(); |
| 949 */ | |
| 1010 #endif | 950 #endif |
| 1011 __HAL_RCC_GPIOH_CLK_DISABLE(); | 951 __HAL_RCC_GPIOH_CLK_DISABLE(); |
| 1012 | |
| 1013 HAL_Delay(1000); | |
| 1014 } | 952 } |
| 1015 | 953 |
| 1016 /* | 954 /* |
| 1017 void sleep_test(void) | 955 void sleep_test(void) |
| 1018 { | 956 { |
