comparison Small_CPU/Src/baseCPU2.c @ 89:ff7775cc34c4 kittz

temp! full cyclic SPI
author Dmitry Romanov <kitt@bk.ru>
date Fri, 23 Nov 2018 16:52:21 +0300
parents 3db7389d49cc
children 83857eb3b12b
comparison
equal deleted inserted replaced
88:3db7389d49cc 89:ff7775cc34c4
151 151
152 // From Common/Drivers/ 152 // From Common/Drivers/
153 #include "stm32f4xx_hal.h" 153 #include "stm32f4xx_hal.h"
154 #include <stdio.h> 154 #include <stdio.h>
155 155
156 uint8_t hasExternalClock(void) 156 uint8_t hasExternalClock(void) {
157 { 157 if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF))
158 if( (TM_OTP_Read( 0, 0 ) > 0) && (TM_OTP_Read( 0, 0 ) < 0xFF) ) 158 return 1;
159 return 1; 159 else
160 else 160 return 0;
161 return 0;
162 } 161 }
163 162
164 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000. 163 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000.
165 // See CPU2-RTE.ld 164 // See CPU2-RTE.ld
166 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") )) 165 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") ))
167 = 166 = { .versionFirst = 1, .versionSecond = 5, .versionThird = 2, .versionBeta = 0,
168 { 167
169 .versionFirst = 1, 168 /* 4 bytes with trailing 0 */
170 .versionSecond = 5, 169 .signature = "mh",
171 .versionThird = 2, 170
172 .versionBeta = 0, 171 .release_year = 18, .release_month = 7, .release_day = 29, .release_sub = 0,
173 172
174 /* 4 bytes with trailing 0 */ 173 /* max 48 with trailing 0 */
175 .signature = "mh", 174 //release_info ="12345678901234567890123456789012345678901"
176 175 .release_info = "compass stuff",
177 .release_year = 18, 176
178 .release_month = 7, 177 /* for safety reasons and coming functions */
179 .release_day = 29, 178 .magic[0] = FIRMWARE_MAGIC_FIRST, .magic[1] = FIRMWARE_MAGIC_SECOND,
180 .release_sub = 0, 179 .magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */
181 180 .magic[3] = FIRMWARE_MAGIC_END };
182 /* max 48 with trailing 0 */ 181
183 //release_info ="12345678901234567890123456789012345678901" 182 uint8_t firmwareVersionHigh(void) {
184 .release_info = "compass stuff", 183 return cpu2_FirmwareData.versionFirst;
185 184 }
186 /* for safety reasons and coming functions */ 185
187 .magic[0] = FIRMWARE_MAGIC_FIRST, 186 uint8_t firmwareVersionLow(void) {
188 .magic[1] = FIRMWARE_MAGIC_SECOND, 187 return cpu2_FirmwareData.versionSecond;
189 .magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */
190 .magic[3] = FIRMWARE_MAGIC_END
191 };
192
193 uint8_t firmwareVersionHigh(void)
194 {
195 return cpu2_FirmwareData.versionFirst;
196 }
197
198 uint8_t firmwareVersionLow(void)
199 {
200 return cpu2_FirmwareData.versionSecond;
201 } 188 }
202 189
203 /** @addtogroup OSTC4 190 /** @addtogroup OSTC4
204 * @{ 191 * @{
205 */ 192 */
271 void GPIO_new_DEBUG_LOW(void); 258 void GPIO_new_DEBUG_LOW(void);
272 void GPIO_new_DEBUG_HIGH(void); 259 void GPIO_new_DEBUG_HIGH(void);
273 260
274 #define REGULAR_RUN 261 #define REGULAR_RUN
275 262
276 int __io_putchar(int ch) 263 int __io_putchar(int ch) {
277 { 264 ITM_SendChar(ch);
278 ITM_SendChar( ch ); 265 return ch;
279 return ch;
280 } 266 }
281 267
282 /* Private functions ---------------------------------------------------------*/ 268 /* Private functions ---------------------------------------------------------*/
283 269
284 /** 270 /**
285 * @brief Main program 271 * @brief Main program
286 * @param None 272 * @param None
287 * @retval None 273 * @retval None
288 */ 274 */
289 275
290 int main(void) 276 int main(void) {
291 { 277 HAL_Init();
292 HAL_Init(); 278 SystemClock_Config();
293 SystemClock_Config(); 279
294 280 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
295 HAL_SYSTICK_Config( HAL_RCC_GetHCLKFreq() / 1000 ); 281 HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK);
296 HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK ); 282 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
297 HAL_NVIC_SetPriority( SysTick_IRQn, 0, 0 ); 283
298 284 MX_RTC_init();
299 MX_RTC_init(); 285 GPIO_LED_Init();
300 GPIO_LED_Init(); 286 GPIO_new_DEBUG_Init(); // added 170322 hw
301 GPIO_new_DEBUG_Init(); // added 170322 hw 287 initGlobals();
302 initGlobals(); 288
303 289 printf("CPU2-RTE running...\n");
304 printf("CPU2-RTE running...\n"); 290
305 291 MX_I2C1_Init();
306 MX_I2C1_Init(); 292 if (global.I2C_SystemStatus != HAL_OK) {
307 if( global.I2C_SystemStatus != HAL_OK ) 293 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) {
308 { 294 MX_I2C1_TestAndClear(); // do it a second time
309 if( MX_I2C1_TestAndClear() == GPIO_PIN_RESET ) 295 }
310 { 296 MX_I2C1_Init();
311 MX_I2C1_TestAndClear(); // do it a second time 297 }
312 } 298
313 MX_I2C1_Init(); 299 //dangerous: TM_OTP_Write(0,0, 0x01);
314 }
315
316 //dangerous: TM_OTP_Write(0,0, 0x01);
317 #ifdef REGULAR_RUN 300 #ifdef REGULAR_RUN
318 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); 301 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure();
319 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; 302 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID];
320 if( global.I2C_SystemStatus != HAL_OK ) 303 if (global.I2C_SystemStatus != HAL_OK) {
321 { 304 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) {
322 if( MX_I2C1_TestAndClear() == GPIO_PIN_RESET ) 305 MX_I2C1_TestAndClear(); // do it a second time
323 { 306 }
324 MX_I2C1_TestAndClear(); // do it a second time 307 MX_I2C1_Init();
325 } 308 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure();
326 MX_I2C1_Init(); 309 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID];
327 global.sensorError[SENSOR_PRESSURE_ID] = init_pressure(); 310 }
328 global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID]; 311
329 } 312 global.dataSendToMaster.sensorErrors =
330 313 global.sensorError[SENSOR_PRESSURE_ID];
331 global.dataSendToMaster.sensorErrors = 314 init_surface_ring();
332 global.sensorError[SENSOR_PRESSURE_ID]; 315 init_battery_gas_gauge();
333 init_surface_ring(); 316 HAL_Delay(10);
334 init_battery_gas_gauge(); 317 battery_gas_gauge_get_data();
335 HAL_Delay( 10 );
336 battery_gas_gauge_get_data();
337 // battery_gas_gauge_set(0); 318 // battery_gas_gauge_set(0);
338 319
339 global.lifeData.battery_voltage = get_voltage(); 320 global.lifeData.battery_voltage = get_voltage();
340 global.lifeData.battery_charge = get_charge(); 321 global.lifeData.battery_charge = get_charge();
341 copyBatteryData(); 322 copyBatteryData();
342 323
343 MX_SPI3_Init(); 324 MX_SPI3_Init();
344 if( !scheduleSetButtonResponsiveness() ) 325 if (!scheduleSetButtonResponsiveness()) {
345 { 326 HAL_Delay(1);
346 HAL_Delay( 1 ); 327 scheduleSetButtonResponsiveness(); // init
347 scheduleSetButtonResponsiveness(); // init 328 HAL_Delay(1);
348 HAL_Delay( 1 ); 329 if (!scheduleSetButtonResponsiveness()) // send again, if problem it's not my problem here.
349 if( !scheduleSetButtonResponsiveness() ) // send again, if problem it's not my problem here. 330 {
350 { 331 HAL_Delay(1);
351 HAL_Delay( 1 ); 332 scheduleSetButtonResponsiveness(); // init
352 scheduleSetButtonResponsiveness(); // init 333 HAL_Delay(1);
353 HAL_Delay( 1 ); 334 }
354 } 335 }
355 } 336
356 337 ADCx_Init();
357 ADCx_Init(); 338 GPIO_Power_MainCPU_Init();
358 GPIO_Power_MainCPU_Init(); 339 global.mode = MODE_POWERUP;
359 global.mode = MODE_POWERUP;
360 #else 340 #else
361 init_pressure(); 341 init_pressure();
362 init_surface_ring(); 342 init_surface_ring();
363 343
364 ADCx_Init(); 344 ADCx_Init();
365 GPIO_Power_MainCPU_Init(); 345 GPIO_Power_MainCPU_Init();
366 global.mode = MODE_TEST; 346 global.mode = MODE_TEST;
367 #endif 347 #endif
368 while( 1 ) 348 while (1) {
369 { 349 printf("Global mode = %d\n", global.mode);
370 printf("Global mode = %d\n", global.mode); 350
371 351 switch (global.mode) {
372 switch( global.mode ) 352 case MODE_POWERUP:
373 { 353 case MODE_BOOT:
374 case MODE_POWERUP: 354 // ReInit_battery_charger_status_pins();
375 case MODE_BOOT: 355 compass_init(0, 7);
376 // ReInit_battery_charger_status_pins(); 356 accelerator_init();
377 compass_init( 0, 7 ); 357 wireless_init();
378 accelerator_init(); 358 if (global.mode == MODE_BOOT) {
379 wireless_init(); 359 GPIO_Power_MainCPU_OFF();
380 if( global.mode == MODE_BOOT ) 360 HAL_Delay(100); // for GPIO_Power_MainCPU_ON();
381 { 361 GPIO_Power_MainCPU_ON();
382 GPIO_Power_MainCPU_OFF(); 362 }
383 HAL_Delay( 100 ); // for GPIO_Power_MainCPU_ON(); 363 SPI_synchronize_with_Master();
384 GPIO_Power_MainCPU_ON(); 364 MX_DMA_Init();
385 } 365 MX_SPI1_Init();
386 SPI_synchronize_with_Master(); 366 MX_EXTI_wireless_Init();
387 MX_DMA_Init(); 367 SPI_Start_single_TxRx_with_Master();
388 MX_SPI1_Init(); 368 EXTI_Test_Button_Init();
389 MX_EXTI_wireless_Init(); 369
390 SPI_Start_single_TxRx_with_Master(); 370 /*
391 EXTI_Test_Button_Init(); 371 uint8_t dataWireless[64];
392 372 while(1)
393 /* 373 {
394 uint8_t dataWireless[64]; 374 wireless_evaluate_and_debug(dataWireless,64);
395 while(1) 375 }
396 { 376 */
397 wireless_evaluate_and_debug(dataWireless,64); 377 global.mode = MODE_SURFACE;
398 } 378 break;
399 */ 379
400 global.mode = MODE_SURFACE; 380 case MODE_CALIB:
401 break; 381 // scheduleCompassCalibrationMode();
402 382 break;
403 case MODE_CALIB: 383
404 scheduleCompassCalibrationMode(); 384 case MODE_SURFACE:
405 break; 385
406 386
407 case MODE_SURFACE: 387 // uint32_t tickstart = 0;
408 scheduleSurfaceMode(); 388 // uint32_t ticksdiff = 0;
409 break; 389 // uint32_t lasttick = 0;
410 390 // tickstart = HAL_GetTick();
411 case MODE_TEST: 391 // uint8_t counterPressure100msec = 0;
412 GPIO_Power_MainCPU_ON(); 392 // uint8_t counterCompass100msec = 0;
413 SPI_synchronize_with_Master(); 393 // uint8_t counterAmbientLight100msec = 0;
414 MX_DMA_Init(); 394 // uint16_t counterWireless1msec = 0;
415 MX_SPI1_Init(); 395 // uint16_t counter1ms=0;
416 // MX_EXTI_wireless_Init(); 396 // while (global.mode == MODE_SURFACE) {
417 SPI_Start_single_TxRx_with_Master(); 397 //
418 // EXTI_Test_Button_Init(); 398 // lasttick = HAL_GetTick();
419 399 // ticksdiff = time_elapsed_ms(tickstart, lasttick);
420 scheduleTestMode(); 400 //
421 break; 401 // if (ticksdiff >= counter1ms) {
422 402 //
423 case MODE_DIVE: 403 // global.lifeData.ascent_rate_meter_per_min = 0;
424 backup.no_fly_time_minutes = global.no_fly_time_minutes; 404 // counter1ms++;
425 backup.seconds_since_last_dive = global.seconds_since_last_dive; 405 // if(counter1ms>1000){
426 406 // counter1ms=0;
427 vpm_init( &global.vpm, global.conservatism, global.repetitive_dive, 407 // global.check_sync_not_running++;
428 global.seconds_since_last_dive ); 408 //
429 global.no_fly_time_minutes = 0; 409 // }
430 global.lifeData.dive_time_seconds = 0; 410 //
431 global.lifeData.dive_time_seconds_without_surface_time = 0; 411 // }
432 scheduleDiveMode(); 412 //
433 // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1; 413 // }
434 414
435 if( global.lifeData.dive_time_seconds > 60 ) 415 // if(global.check_sync_not_running>10){
436 { 416 // SPI_Start_single_TxRx_with_Master();
437 //No Fly time 60% of desaturationtime after dive 417 // }
438 global.no_fly_time_minutes = decom_calc_desaturation_time( 418 // schedule_check_resync();
439 global.lifeData.tissue_nitrogen_bar, 419 // pressure_update();
440 global.lifeData.tissue_helium_bar, 420 // compass_read();
441 global.lifeData.pressure_surface_bar ) * 60 / 100; 421 // acceleration_read();
442 if( global.no_fly_time_minutes < (24 * 60) ) 422 // compass_calc();
443 global.no_fly_time_minutes = 24 * 60; 423 scheduleSurfaceMode();
444 } 424 break;
445 else 425
446 { 426 case MODE_TEST:
447 global.no_fly_time_minutes = backup.no_fly_time_minutes; 427 break;
448 global.seconds_since_last_dive = backup.seconds_since_last_dive; 428
449 } 429 case MODE_DIVE:
450 430 // backup.no_fly_time_minutes = global.no_fly_time_minutes;
451 global.lifeData.dive_time_seconds = 0; 431 // backup.seconds_since_last_dive = global.seconds_since_last_dive;
452 global.lifeData.dive_time_seconds_without_surface_time = 0; 432 //
453 global.lifeData.counterSecondsShallowDepth = 0; 433 // vpm_init( &global.vpm, global.conservatism, global.repetitive_dive,
454 434 // global.seconds_since_last_dive );
455 backup.no_fly_time_minutes = 0; 435 // global.no_fly_time_minutes = 0;
456 backup.seconds_since_last_dive = 0; 436 // global.lifeData.dive_time_seconds = 0;
457 break; 437 // global.lifeData.dive_time_seconds_without_surface_time = 0;
458 438 // scheduleDiveMode();
459 case MODE_SHUTDOWN: 439 // // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1;
460 HAL_Delay( 200 ); 440 //
461 global.mode = MODE_SLEEP; 441 // if( global.lifeData.dive_time_seconds > 60 )
462 MX_SPI3_Init(); 442 // {
463 break; 443 // //No Fly time 60% of desaturationtime after dive
464 444 // global.no_fly_time_minutes = decom_calc_desaturation_time(
465 case MODE_SLEEP: 445 // global.lifeData.tissue_nitrogen_bar,
466 /* 446 // global.lifeData.tissue_helium_bar,
467 sleep_prepare(); 447 // global.lifeData.pressure_surface_bar ) * 60 / 100;
468 scheduleSleepMode_test(); 448 // if( global.no_fly_time_minutes < (24 * 60) )
469 */ 449 // global.no_fly_time_minutes = 24 * 60;
470 /* 450 // }
471 GPIO_Power_MainCPU_OFF(); 451 // else
472 EXTI_Test_Button_DeInit(); 452 // {
473 EXTI_Wakeup_Button_Init(); 453 // global.no_fly_time_minutes = backup.no_fly_time_minutes;
474 NOT_USED_AT_THE_MOMENT_scheduleSleepMode(); 454 // global.seconds_since_last_dive = backup.seconds_since_last_dive;
475 */ 455 // }
476 EXTI_Test_Button_DeInit(); 456 //
477 MX_EXTI_wireless_DeInit(); 457 // global.lifeData.dive_time_seconds = 0;
478 if( hasExternalClock() ) 458 // global.lifeData.dive_time_seconds_without_surface_time = 0;
479 SystemClock_Config_HSI(); 459 // global.lifeData.counterSecondsShallowDepth = 0;
480 sleep_prepare(); 460 //
481 461 // backup.no_fly_time_minutes = 0;
482 GPIO_LED_Init(); 462 // backup.seconds_since_last_dive = 0;
483 463 break;
484 scheduleSleepMode(); 464
485 if( hasExternalClock() ) 465 case MODE_SHUTDOWN:
486 SystemClock_Config_HSE(); 466 HAL_Delay(200);
487 GPIO_LED_Init(); 467 global.mode = MODE_SLEEP;
488 EXTI_Wakeup_Button_DeInit(); 468 MX_SPI3_Init();
489 ADCx_Init(); 469 break;
490 GPIO_Power_MainCPU_Init(); 470
491 GPIO_Power_MainCPU_ON(); 471 case MODE_SLEEP:
492 compass_init( 0, 7 ); 472 /*
493 accelerator_init(); 473 sleep_prepare();
494 wireless_init(); 474 scheduleSleepMode_test();
495 SPI_synchronize_with_Master(); 475 */
496 MX_DMA_Init(); 476 /*
497 MX_SPI1_Init(); 477 GPIO_Power_MainCPU_OFF();
498 MX_EXTI_wireless_Init(); 478 EXTI_Test_Button_DeInit();
499 SPI_Start_single_TxRx_with_Master(); 479 EXTI_Wakeup_Button_Init();
500 480 NOT_USED_AT_THE_MOMENT_scheduleSleepMode();
501 // EXTILine0_Button_DeInit(); not now, later after testing 481 */
502 break; 482 EXTI_Test_Button_DeInit();
503 } 483 MX_EXTI_wireless_DeInit();
504 } 484 if (hasExternalClock())
485 SystemClock_Config_HSI();
486 sleep_prepare();
487
488 GPIO_LED_Init();
489
490 scheduleSleepMode();
491 if (hasExternalClock())
492 SystemClock_Config_HSE();
493 GPIO_LED_Init();
494 EXTI_Wakeup_Button_DeInit();
495 ADCx_Init();
496 GPIO_Power_MainCPU_Init();
497 GPIO_Power_MainCPU_ON();
498 compass_init(0, 7);
499 accelerator_init();
500 wireless_init();
501 SPI_synchronize_with_Master();
502 MX_DMA_Init();
503 MX_SPI1_Init();
504 MX_EXTI_wireless_Init();
505 SPI_Start_single_TxRx_with_Master();
506
507 // EXTILine0_Button_DeInit(); not now, later after testing
508 break;
509 }
510 }
505 } 511 }
506 512
507 /** @brief Button feedback - EXTI line detection callbacks 513 /** @brief Button feedback - EXTI line detection callbacks
508 * @param GPIO_Pin: Specifies the pins connected EXTI line 514 * @param GPIO_Pin: Specifies the pins connected EXTI line
509 * @retval None 515 * @retval None
510 */ 516 */
511 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) 517 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
512 { 518
513 519 if (GPIO_Pin == WIRELSS_RISING_GPIO_PIN) {
514 if( GPIO_Pin == WIRELSS_RISING_GPIO_PIN ) 520 wireless_trigger_RisingEdgeSilence();
515 { 521 }
516 wireless_trigger_RisingEdgeSilence(); 522
517 } 523 else
518 524
519 else 525 if (GPIO_Pin == WIRELSS_FALLING_GPIO_PIN) {
520 526 wireless_trigger_FallingEdgeSignalHigh();
521 if( GPIO_Pin == WIRELSS_FALLING_GPIO_PIN ) 527 }
522 { 528
523 wireless_trigger_FallingEdgeSignalHigh(); 529 else
524 } 530
525 531 if (GPIO_Pin == BUTTON_OSTC_GPIO_PIN) {
526 else 532 if (global.mode == MODE_SLEEP) {
527 533 global.mode = MODE_BOOT;
528 if( GPIO_Pin == BUTTON_OSTC_GPIO_PIN ) 534 }
529 { 535 }
530 if( global.mode == MODE_SLEEP ) 536
531 { 537 else
532 global.mode = MODE_BOOT; 538
533 } 539 if (GPIO_Pin == BUTTON_TEST_GPIO_PIN) {
534 } 540 if (!global.demo_mode && (global.mode == MODE_SURFACE)) {
535 541 global.demo_mode = 1;
536 else 542 global.mode = MODE_DIVE;
537 543 } else if (global.demo_mode && (global.mode == MODE_DIVE)
538 if( GPIO_Pin == BUTTON_TEST_GPIO_PIN ) 544 && (global.lifeData.dive_time_seconds > 10)) {
539 { 545 global.demo_mode = 0;
540 if( !global.demo_mode && (global.mode == MODE_SURFACE) ) 546 global.dataSendToMaster.mode = MODE_ENDDIVE;
541 { 547 global.deviceDataSendToMaster.mode = MODE_ENDDIVE;
542 global.demo_mode = 1; 548 }
543 global.mode = MODE_DIVE; 549 }
544 }
545 else if( global.demo_mode && (global.mode == MODE_DIVE)
546 && (global.lifeData.dive_time_seconds > 10) )
547 {
548 global.demo_mode = 0;
549 global.dataSendToMaster.mode = MODE_ENDDIVE;
550 global.deviceDataSendToMaster.mode = MODE_ENDDIVE;
551 }
552 }
553 } 550 }
554 551
555 /** 552 /**
556 * @brief System Clock Configuration 553 * @brief System Clock Configuration
557 * The system Clock is configured as follow : 554 * The system Clock is configured as follow :
571 * Flash Latency(WS) = 3 568 * Flash Latency(WS) = 3
572 * @param None 569 * @param None
573 * @retval None 570 * @retval None
574 */ 571 */
575 572
576 void SystemClock_Config(void) 573 void SystemClock_Config(void) {
577 { 574 if (hasExternalClock())
578 if( hasExternalClock() ) 575 SystemClock_Config_HSE();
579 SystemClock_Config_HSE(); 576 else
580 else 577 SystemClock_Config_HSI();
581 SystemClock_Config_HSI(); 578 }
582 } 579
583 580 void SYSCLKConfig_STOP(void) {
584 void SYSCLKConfig_STOP(void) 581 SYSCLKConfig_STOP_HSI();
585 { 582 }
586 SYSCLKConfig_STOP_HSI(); 583
587 } 584 void SystemClock_Config_HSE(void) {
588 585 RCC_OscInitTypeDef RCC_OscInitStruct;
589 void SystemClock_Config_HSE(void) 586 RCC_ClkInitTypeDef RCC_ClkInitStruct;
590 {
591 RCC_OscInitTypeDef RCC_OscInitStruct;
592 RCC_ClkInitTypeDef RCC_ClkInitStruct;
593 // RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; 587 // RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
594 588
595 __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE(); 589 __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE();
596 590
597 __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE2 ); 591 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
598 592
599 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE; 593 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE;
600 RCC_OscInitStruct.HSEState = RCC_HSE_ON; 594 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
601 //RCC_OscInitStruct.LSEState = RCC_LSE_ON; 595 //RCC_OscInitStruct.LSEState = RCC_LSE_ON;
602 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 596 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
603 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; 597 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
604 RCC_OscInitStruct.PLL.PLLM = 8; 598 RCC_OscInitStruct.PLL.PLLM = 8;
605 RCC_OscInitStruct.PLL.PLLN = 320; 599 RCC_OscInitStruct.PLL.PLLN = 320;
606 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; 600 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
607 RCC_OscInitStruct.PLL.PLLQ = 4; 601 RCC_OscInitStruct.PLL.PLLQ = 4;
608 HAL_RCC_OscConfig( &RCC_OscInitStruct ); 602 HAL_RCC_OscConfig(&RCC_OscInitStruct);
609 603
610 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK 604 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
611 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; 605 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
612 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 606 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
613 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 607 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
614 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 608 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
615 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 609 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
616 HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_2 ); 610 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
617 611
618 // PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; 612 // PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
619 // PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; 613 // PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
620 // HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); 614 // HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
621 615
622 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 616 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
623 617
624 // HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 618 // HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
625 619
626 /* SysTick_IRQn interrupt configuration */ 620 /* SysTick_IRQn interrupt configuration */
627 // HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 621 // HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
628 } 622 }
629 623
630 void SystemClock_Config_HSI(void) 624 void SystemClock_Config_HSI(void) {
631 { 625 RCC_ClkInitTypeDef RCC_ClkInitStruct;
632 RCC_ClkInitTypeDef RCC_ClkInitStruct; 626 RCC_OscInitTypeDef RCC_OscInitStruct;
633 RCC_OscInitTypeDef RCC_OscInitStruct; 627
634 628 /* Enable Power Control clock */
635 /* Enable Power Control clock */ 629 __HAL_RCC_PWR_CLK_ENABLE();
636 __HAL_RCC_PWR_CLK_ENABLE(); 630
637 631 /* The voltage scaling allows optimizing the power consumption when the device is
638 /* The voltage scaling allows optimizing the power consumption when the device is 632 clocked below the maximum system frequency, to update the voltage scaling value
639 clocked below the maximum system frequency, to update the voltage scaling value 633 regarding system frequency refer to product datasheet. */
640 regarding system frequency refer to product datasheet. */ 634 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
641 __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE2 ); 635
642 636 /* Enable HSI Oscillator and activate PLL with HSI as source */
643 /* Enable HSI Oscillator and activate PLL with HSI as source */ 637 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
644 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 638 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
645 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 639 RCC_OscInitStruct.HSICalibrationValue = 0x10;
646 RCC_OscInitStruct.HSICalibrationValue = 0x10; 640 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
647 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 641 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
648 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; 642 RCC_OscInitStruct.PLL.PLLM = 16;
649 RCC_OscInitStruct.PLL.PLLM = 16; 643 RCC_OscInitStruct.PLL.PLLN = 320;
650 RCC_OscInitStruct.PLL.PLLN = 320; 644 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
651 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; 645 RCC_OscInitStruct.PLL.PLLQ = 4;
652 RCC_OscInitStruct.PLL.PLLQ = 4; 646 HAL_RCC_OscConfig(&RCC_OscInitStruct);
653 HAL_RCC_OscConfig( &RCC_OscInitStruct ); 647
654 648 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
655 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 649 clocks dividers */
656 clocks dividers */ 650 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
657 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK 651 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
658 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); 652 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
659 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 653 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
660 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 654 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
661 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 655 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
662 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 656 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
663 HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_2 );
664 } 657 }
665 /* 658 /*
666 RCC_OscInitTypeDef RCC_OscInitStruct; 659 RCC_OscInitTypeDef RCC_OscInitStruct;
667 RCC_ClkInitTypeDef RCC_ClkInitStruct; 660 RCC_ClkInitTypeDef RCC_ClkInitStruct;
668 661
705 * @brief Configures system clock after wake-up from STOP: enable HSI, PLL 698 * @brief Configures system clock after wake-up from STOP: enable HSI, PLL
706 * and select PLL as system clock source. 699 * and select PLL as system clock source.
707 * @param None 700 * @param None
708 * @retval None 701 * @retval None
709 */ 702 */
710 void SYSCLKConfig_STOP_HSE(void) 703 void SYSCLKConfig_STOP_HSE(void) {
711 { 704 RCC_ClkInitTypeDef RCC_ClkInitStruct;
712 RCC_ClkInitTypeDef RCC_ClkInitStruct; 705 RCC_OscInitTypeDef RCC_OscInitStruct;
713 RCC_OscInitTypeDef RCC_OscInitStruct; 706 uint32_t pFLatency = 0;
714 uint32_t pFLatency = 0; 707
715 708 /* Get the Oscillators configuration according to the internal RCC registers */
716 /* Get the Oscillators configuration according to the internal RCC registers */ 709 HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
717 HAL_RCC_GetOscConfig( &RCC_OscInitStruct ); 710
718 711 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
719 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ 712 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
720 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; 713 RCC_OscInitStruct.HSIState = RCC_HSE_ON;
721 RCC_OscInitStruct.HSIState = RCC_HSE_ON; 714 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
722 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 715 HAL_RCC_OscConfig(&RCC_OscInitStruct);
723 HAL_RCC_OscConfig( &RCC_OscInitStruct ); 716
724 717 /* Get the Clocks configuration according to the internal RCC registers */
725 /* Get the Clocks configuration according to the internal RCC registers */ 718 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
726 HAL_RCC_GetClockConfig( &RCC_ClkInitStruct, &pFLatency ); 719
727 720 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
728 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 721 clocks dividers */
729 clocks dividers */ 722 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
730 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; 723 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
731 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 724 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency);
732 HAL_RCC_ClockConfig( &RCC_ClkInitStruct, pFLatency ); 725 }
733 } 726
734 727 void SYSCLKConfig_STOP_HSI(void) {
735 void SYSCLKConfig_STOP_HSI(void) 728 RCC_ClkInitTypeDef RCC_ClkInitStruct;
736 { 729 RCC_OscInitTypeDef RCC_OscInitStruct;
737 RCC_ClkInitTypeDef RCC_ClkInitStruct; 730 uint32_t pFLatency = 0;
738 RCC_OscInitTypeDef RCC_OscInitStruct; 731
739 uint32_t pFLatency = 0; 732 /* Get the Oscillators configuration according to the internal RCC registers */
740 733 HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
741 /* Get the Oscillators configuration according to the internal RCC registers */ 734
742 HAL_RCC_GetOscConfig( &RCC_OscInitStruct ); 735 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
743 736 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
744 /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */ 737 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
745 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 738 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
746 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 739 RCC_OscInitStruct.HSICalibrationValue = 0x10;
747 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 740 HAL_RCC_OscConfig(&RCC_OscInitStruct);
748 RCC_OscInitStruct.HSICalibrationValue = 0x10; 741
749 HAL_RCC_OscConfig( &RCC_OscInitStruct ); 742 /* Get the Clocks configuration according to the internal RCC registers */
750 743 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
751 /* Get the Clocks configuration according to the internal RCC registers */ 744
752 HAL_RCC_GetClockConfig( &RCC_ClkInitStruct, &pFLatency ); 745 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
753 746 clocks dividers */
754 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 747 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
755 clocks dividers */ 748 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
756 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; 749 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency);
757 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
758 HAL_RCC_ClockConfig( &RCC_ClkInitStruct, pFLatency );
759 } 750 }
760 751
761 /** 752 /**
762 * @brief SYSTICK callback 753 * @brief SYSTICK callback
763 * @param None 754 * @param None
764 * @retval None 755 * @retval None
765 */ 756 */
766 void HAL_SYSTICK_Callback(void) 757 void HAL_SYSTICK_Callback(void) {
767 { 758 HAL_IncTick();
768 HAL_IncTick();
769 } 759 }
770 760
771 /** 761 /**
772 * @brief Configures GPIO for LED 762 * @brief Configures GPIO for LED
773 * Might move with STM32Cube usage 763 * Might move with STM32Cube usage
794 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); 784 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
795 HAL_Delay(10); 785 HAL_Delay(10);
796 } 786 }
797 */ 787 */
798 788
799 static void GPIO_LED_Init(void) 789 static void GPIO_LED_Init(void) {
800 { 790 GPIO_InitTypeDef GPIO_InitStructure;
801 GPIO_InitTypeDef GPIO_InitStructure; 791
802 792 __GPIOC_CLK_ENABLE();
803 __GPIOC_CLK_ENABLE(); 793 GPIO_InitStructure.Pin = GPIO_PIN_3;
804 GPIO_InitStructure.Pin = GPIO_PIN_3; 794 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
805 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 795 GPIO_InitStructure.Pull = GPIO_PULLUP;
806 GPIO_InitStructure.Pull = GPIO_PULLUP; 796 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
807 GPIO_InitStructure.Speed = GPIO_SPEED_FAST; 797 HAL_GPIO_Init( GPIOC, &GPIO_InitStructure);
808 HAL_GPIO_Init( GPIOC, &GPIO_InitStructure ); 798 }
809 } 799
810 800 void GPIO_new_DEBUG_Init(void) {
811 void GPIO_new_DEBUG_Init(void)
812 {
813 #ifdef DEBUG_PIN_ACTIVE 801 #ifdef DEBUG_PIN_ACTIVE
814 GPIO_InitTypeDef GPIO_InitStructure; 802 GPIO_InitTypeDef GPIO_InitStructure;
815 803
816 __GPIOC_CLK_ENABLE(); 804 __GPIOC_CLK_ENABLE();
817 GPIO_InitStructure.Pin = GPIO_PIN_3; 805 GPIO_InitStructure.Pin = GPIO_PIN_3;
818 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 806 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
819 GPIO_InitStructure.Pull = GPIO_PULLUP; 807 GPIO_InitStructure.Pull = GPIO_PULLUP;
820 GPIO_InitStructure.Speed = GPIO_SPEED_FAST; 808 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
821 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); 809 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
822 #endif 810 #endif
823 } 811 }
824 812
825 void GPIO_new_DEBUG_LOW(void) 813 void GPIO_new_DEBUG_LOW(void) {
826 {
827 #ifdef DEBUG_PIN_ACTIVE 814 #ifdef DEBUG_PIN_ACTIVE
828 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET); 815 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET);
829 #endif 816 #endif
830 } 817 }
831 818
832 void GPIO_new_DEBUG_HIGH(void) 819 void GPIO_new_DEBUG_HIGH(void) {
833 {
834 #ifdef DEBUG_PIN_ACTIVE 820 #ifdef DEBUG_PIN_ACTIVE
835 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET); 821 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET);
836 #endif 822 #endif
837 } 823 }
838 824
839 static void GPIO_Power_MainCPU_Init(void) 825 static void GPIO_Power_MainCPU_Init(void) {
840 { 826 GPIO_InitTypeDef GPIO_InitStructure;
841 GPIO_InitTypeDef GPIO_InitStructure; 827 __GPIOC_CLK_ENABLE();
842 __GPIOC_CLK_ENABLE(); 828 GPIO_InitStructure.Pin = GPIO_PIN_0;
843 GPIO_InitStructure.Pin = GPIO_PIN_0; 829 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
844 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 830 GPIO_InitStructure.Pull = GPIO_PULLUP;
845 GPIO_InitStructure.Pull = GPIO_PULLUP; 831 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
846 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; 832 HAL_GPIO_Init( GPIOC, &GPIO_InitStructure);
847 HAL_GPIO_Init( GPIOC, &GPIO_InitStructure ); 833 HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
848 HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET ); 834 }
849 } 835
850 836 static void GPIO_Power_MainCPU_ON(void) {
851 static void GPIO_Power_MainCPU_ON(void) 837 HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
852 { 838 }
853 HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET ); 839
854 } 840 static void GPIO_Power_MainCPU_OFF(void) {
855 841 HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
856 static void GPIO_Power_MainCPU_OFF(void)
857 {
858 HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_SET );
859 } 842 }
860 843
861 /** 844 /**
862 * @brief Configures EXTI Line0 (connected to PA0 + PA1 pin) in interrupt mode 845 * @brief Configures EXTI Line0 (connected to PA0 + PA1 pin) in interrupt mode
863 * @param None 846 * @param None
864 * @retval None 847 * @retval None
865 */ 848 */
866 849
867 static void EXTI_Wakeup_Button_Init(void) 850 static void EXTI_Wakeup_Button_Init(void) {
868 { 851 GPIO_InitTypeDef GPIO_InitStructure;
869 GPIO_InitTypeDef GPIO_InitStructure; 852
870 853 __HAL_RCC_GPIOA_CLK_ENABLE();
871 __HAL_RCC_GPIOA_CLK_ENABLE(); 854 BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE();
872 BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE(); 855 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN;
873 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; 856 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
874 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; 857 GPIO_InitStructure.Pull = GPIO_NOPULL;
875 GPIO_InitStructure.Pull = GPIO_NOPULL; 858 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure);
876 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure ); 859
877 860 HAL_NVIC_SetPriority( BUTTON_OSTC_IRQn, 0x0F, 0);
878 HAL_NVIC_SetPriority( BUTTON_OSTC_IRQn, 0x0F, 0 ); 861 HAL_NVIC_EnableIRQ( BUTTON_OSTC_IRQn);
879 HAL_NVIC_EnableIRQ( BUTTON_OSTC_IRQn ); 862 }
880 } 863
881 864 static void EXTI_Wakeup_Button_DeInit(void) {
882 static void EXTI_Wakeup_Button_DeInit(void) 865 GPIO_InitTypeDef GPIO_InitStructure;
883 { 866
884 GPIO_InitTypeDef GPIO_InitStructure; 867 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
885 868 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
886 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; 869 GPIO_InitStructure.Pull = GPIO_NOPULL;
887 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; 870
888 GPIO_InitStructure.Pull = GPIO_NOPULL; 871 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN;
889 872 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure);
890 GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN; 873 HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn);
891 HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure ); 874 }
892 HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn ); 875
893 } 876 static void EXTI_Test_Button_Init(void) {
894 877 GPIO_InitTypeDef GPIO_InitStructure;
895 static void EXTI_Test_Button_Init(void) 878
896 { 879 BUTTON_TEST_GPIO_CLK_ENABLE();
897 GPIO_InitTypeDef GPIO_InitStructure; 880 GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN;
898 881 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
899 BUTTON_TEST_GPIO_CLK_ENABLE(); 882 GPIO_InitStructure.Pull = GPIO_PULLUP;
900 GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN; 883 HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure);
901 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; 884 HAL_NVIC_SetPriority( BUTTON_TEST_IRQn, 0x0F, 0);
902 GPIO_InitStructure.Pull = GPIO_PULLUP; 885 HAL_NVIC_EnableIRQ( BUTTON_TEST_IRQn);
903 HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure ); 886 }
904 HAL_NVIC_SetPriority( BUTTON_TEST_IRQn, 0x0F, 0 ); 887
905 HAL_NVIC_EnableIRQ( BUTTON_TEST_IRQn ); 888 static void EXTI_Test_Button_DeInit(void) {
906 } 889 GPIO_InitTypeDef GPIO_InitStructure;
907 890
908 static void EXTI_Test_Button_DeInit(void) 891 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
909 { 892 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
910 GPIO_InitTypeDef GPIO_InitStructure; 893 GPIO_InitStructure.Pull = GPIO_NOPULL;
911 894
912 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; 895 GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN;
913 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; 896 HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure);
914 GPIO_InitStructure.Pull = GPIO_NOPULL; 897 HAL_NVIC_DisableIRQ( BUTTON_TEST_IRQn);
915 898 }
916 GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN; 899
917 HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure ); 900 static void MX_EXTI_wireless_Init(void) {
918 HAL_NVIC_DisableIRQ( BUTTON_TEST_IRQn ); 901 GPIO_InitTypeDef GPIO_InitStructure;
919 } 902
920 903 WIRELSS_POWER_HAL_RCC_GPIO_CLK_ENABLE();
921 static void MX_EXTI_wireless_Init(void) 904 GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
922 { 905 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
923 GPIO_InitTypeDef GPIO_InitStructure; 906 GPIO_InitStructure.Pull = GPIO_NOPULL;
924 907 HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure);
925 WIRELSS_POWER_HAL_RCC_GPIO_CLK_ENABLE(); 908 HAL_GPIO_WritePin( WIRELSS_POWER_GPIO_PORT, WIRELSS_POWER_GPIO_PIN,
926 GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN; 909 GPIO_PIN_SET);
927 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 910
928 GPIO_InitStructure.Pull = GPIO_NOPULL; 911 WIRELSS_RISING_HAL_RCC_GPIO_CLK_ENABLE();
929 HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure ); 912 GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN;
930 HAL_GPIO_WritePin( WIRELSS_POWER_GPIO_PORT, WIRELSS_POWER_GPIO_PIN, 913 GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
931 GPIO_PIN_SET ); 914 GPIO_InitStructure.Pull = GPIO_NOPULL;
932 915 HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure);
933 WIRELSS_RISING_HAL_RCC_GPIO_CLK_ENABLE(); 916
934 GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN; 917 HAL_NVIC_SetPriority( WIRELSS_RISING_IRQn, 0x02, 0);
935 GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING; 918 HAL_NVIC_EnableIRQ( WIRELSS_RISING_IRQn);
936 GPIO_InitStructure.Pull = GPIO_NOPULL; 919
937 HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure ); 920 WIRELSS_FALLING_HAL_RCC_GPIO_CLK_ENABLE();
938 921 GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN;
939 HAL_NVIC_SetPriority( WIRELSS_RISING_IRQn, 0x02, 0 ); 922 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
940 HAL_NVIC_EnableIRQ( WIRELSS_RISING_IRQn ); 923 GPIO_InitStructure.Pull = GPIO_NOPULL;
941 924 HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure);
942 WIRELSS_FALLING_HAL_RCC_GPIO_CLK_ENABLE(); 925
943 GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN; 926 HAL_NVIC_SetPriority( WIRELSS_FALLING_IRQn, 0x02, 0);
944 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; 927 HAL_NVIC_EnableIRQ( WIRELSS_FALLING_IRQn);
945 GPIO_InitStructure.Pull = GPIO_NOPULL; 928
946 HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure ); 929 }
947 930
948 HAL_NVIC_SetPriority( WIRELSS_FALLING_IRQn, 0x02, 0 ); 931 static void MX_EXTI_wireless_DeInit(void) {
949 HAL_NVIC_EnableIRQ( WIRELSS_FALLING_IRQn ); 932 GPIO_InitTypeDef GPIO_InitStructure;
950 933
951 } 934 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
952 935 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
953 static void MX_EXTI_wireless_DeInit(void) 936 GPIO_InitStructure.Pull = GPIO_NOPULL;
954 { 937
955 GPIO_InitTypeDef GPIO_InitStructure; 938 GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN;
956 939 HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure);
957 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; 940
958 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; 941 GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN;
959 GPIO_InitStructure.Pull = GPIO_NOPULL; 942 HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure);
960 943
961 GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN; 944 GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
962 HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure ); 945 HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure);
963 946
964 GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN; 947 HAL_NVIC_DisableIRQ( WIRELSS_RISING_IRQn);
965 HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure ); 948 HAL_NVIC_DisableIRQ( WIRELSS_FALLING_IRQn);
966
967 GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
968 HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure );
969
970 HAL_NVIC_DisableIRQ( WIRELSS_RISING_IRQn );
971 HAL_NVIC_DisableIRQ( WIRELSS_FALLING_IRQn );
972 } 949 }
973 950
974 /* NUCLEO C 13 951 /* NUCLEO C 13
975 KEY_BUTTON_GPIO_CLK_ENABLE(); 952 KEY_BUTTON_GPIO_CLK_ENABLE();
976 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; 953 GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
993 static uint8_t uwCounter = 0; 970 static uint8_t uwCounter = 0;
994 uwCounter = 1; 971 uwCounter = 1;
995 } 972 }
996 */ 973 */
997 974
998 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle) 975 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle) {
999 { 976
1000 977 }
1001 } 978
1002 979 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle) {
1003 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle) 980
1004 { 981 }
1005 982
1006 } 983 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle) {
1007 984
1008 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle) 985 }
1009 { 986
1010 987 void sleep_prepare(void) {
1011 } 988 EXTI_Wakeup_Button_Init();
1012 989 /*
1013 void sleep_prepare(void) 990 GPIO_InitStruct.Pull = GPIO_PULLUP;
1014 { 991 GPIO_InitStruct.Pin = GPIO_PIN_0;
1015 EXTI_Wakeup_Button_Init(); 992 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
1016 /* 993 */
1017 GPIO_InitStruct.Pull = GPIO_PULLUP; 994 compass_sleep();
1018 GPIO_InitStruct.Pin = GPIO_PIN_0; 995 HAL_Delay(100);
1019 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 996 accelerator_sleep();
1020 */ 997 HAL_Delay(100);
1021 compass_sleep(); 998
1022 HAL_Delay( 100 ); 999 I2C_DeInit();
1023 accelerator_sleep(); 1000 MX_SPI_DeInit();
1024 HAL_Delay( 100 ); 1001 MX_SPI3_DeInit();
1025 1002 ADCx_DeInit();
1026 I2C_DeInit(); 1003
1027 MX_SPI_DeInit(); 1004 GPIO_InitTypeDef GPIO_InitStruct;
1028 MX_SPI3_DeInit(); 1005
1029 ADCx_DeInit(); 1006 __HAL_RCC_GPIOA_CLK_ENABLE();
1030 1007 __HAL_RCC_GPIOB_CLK_ENABLE();
1031 GPIO_InitTypeDef GPIO_InitStruct; 1008 __HAL_RCC_GPIOC_CLK_ENABLE();
1032 1009 __HAL_RCC_GPIOH_CLK_ENABLE();
1033 __HAL_RCC_GPIOA_CLK_ENABLE(); 1010
1034 __HAL_RCC_GPIOB_CLK_ENABLE(); 1011 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
1035 __HAL_RCC_GPIOC_CLK_ENABLE(); 1012 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
1036 __HAL_RCC_GPIOH_CLK_ENABLE(); 1013 GPIO_InitStruct.Pull = GPIO_NOPULL;
1037 1014 GPIO_InitStruct.Pin = GPIO_PIN_All;
1038 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 1015 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct);
1039 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
1040 GPIO_InitStruct.Pull = GPIO_NOPULL;
1041 GPIO_InitStruct.Pin = GPIO_PIN_All;
1042 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct );
1043 #ifdef DEBUGMODE 1016 #ifdef DEBUGMODE
1044 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */ 1017 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */
1045 #endif 1018 #endif
1046 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct ); 1019 HAL_GPIO_Init( GPIOB, &GPIO_InitStruct);
1047 1020
1048 GPIO_InitStruct.Pin = GPIO_PIN_All 1021 GPIO_InitStruct.Pin =
1049 ^ ( GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_14 | GPIO_PIN_15); /* power off & charger in & charge out & OSC32*/ 1022 GPIO_PIN_All
1050 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct ); 1023 ^ ( GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_14
1051 1024 | GPIO_PIN_15); /* power off & charger in & charge out & OSC32*/
1052 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0); 1025 HAL_GPIO_Init( GPIOC, &GPIO_InitStruct);
1026
1027 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0);
1053 #ifdef DEBUGMODE 1028 #ifdef DEBUGMODE
1054 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */ 1029 GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */
1055 #endif 1030 #endif
1056 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct ); 1031 HAL_GPIO_Init( GPIOA, &GPIO_InitStruct);
1057 1032
1058 GPIO_InitStruct.Pin = GPIO_PIN_All; 1033 GPIO_InitStruct.Pin = GPIO_PIN_All;
1059 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct ); 1034 HAL_GPIO_Init( GPIOH, &GPIO_InitStruct);
1060 1035
1061 GPIO_Power_MainCPU_OFF(); 1036 GPIO_Power_MainCPU_OFF();
1062 1037
1063 #ifndef DEBUGMODE 1038 #ifndef DEBUGMODE
1064 __HAL_RCC_GPIOB_CLK_DISABLE(); 1039 __HAL_RCC_GPIOB_CLK_DISABLE();
1065 #endif 1040 #endif
1066 __HAL_RCC_GPIOH_CLK_DISABLE(); 1041 __HAL_RCC_GPIOH_CLK_DISABLE();
1067 1042
1068 HAL_Delay( 1000 ); 1043 HAL_Delay(1000);
1069 } 1044 }
1070 1045
1071 /* 1046 /*
1072 void sleep_test(void) 1047 void sleep_test(void)
1073 { 1048 {
1121 * @param line: assert_param error line source number 1096 * @param line: assert_param error line source number
1122 * @retval None 1097 * @retval None
1123 */ 1098 */
1124 void assert_failed(uint8_t* file, uint32_t line) 1099 void assert_failed(uint8_t* file, uint32_t line)
1125 { 1100 {
1126 /* User can add his own implementation to report the file name and line number, 1101 /* User can add his own implementation to report the file name and line number,
1127 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 1102 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
1128 1103
1129 /* Infinite loop */ 1104 /* Infinite loop */
1130 while (1) 1105 while (1)
1131 { 1106 {
1132 } 1107 }
1133 } 1108 }
1134 #endif 1109 #endif
1135 1110
1136 /** 1111 /**
1137 * @} 1112 * @}