comparison Discovery/Src/base.c @ 430:30580cf5ee77

Merged in Ideenmodellierer/ostc4/ImprovmentNVM_2 (pull request #39) ImprovmentNVM 2
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Tue, 18 Feb 2020 07:20:29 +0000
parents b1091e183d52
children f1257a32f2d4
comparison
equal deleted inserted replaced
419:c2264ce139cb 430:30580cf5ee77
228 #include "timer.h" 228 #include "timer.h"
229 #include "logbook_miniLive.h" 229 #include "logbook_miniLive.h"
230 #include "test_vpm.h" 230 #include "test_vpm.h"
231 #include "tDebug.h" 231 #include "tDebug.h"
232 #include "motion.h" 232 #include "motion.h"
233 #include "data_exchange_main.h"
233 234
234 #ifdef DEMOMODE 235 #ifdef DEMOMODE
235 #include "demo.h" 236 #include "demo.h"
236 static void TIM_DEMO_init(void); 237 static void TIM_DEMO_init(void);
237 #endif 238 #endif
259 TIM_HandleTypeDef TimHandle; /* used in stm32f4xx_it.c too */ 260 TIM_HandleTypeDef TimHandle; /* used in stm32f4xx_it.c too */
260 static TIM_HandleTypeDef TimBacklightHandle; 261 static TIM_HandleTypeDef TimBacklightHandle;
261 #ifdef DEMOMODE 262 #ifdef DEMOMODE
262 TIM_HandleTypeDef TimDemoHandle; /* used in stm32f4xx_it.c too */ 263 TIM_HandleTypeDef TimDemoHandle; /* used in stm32f4xx_it.c too */
263 #endif 264 #endif
265
266 static uint8_t RequestModeChange = 0;
264 267
265 static uint8_t LastButtonPressed; 268 static uint8_t LastButtonPressed;
266 static uint32_t LastButtonPressedTick; 269 static uint32_t LastButtonPressedTick;
267 static uint32_t BaseTick100ms; /* Tick at last 100ms cycle */ 270 static uint32_t BaseTick100ms; /* Tick at last 100ms cycle */
268 271
298 static void deco_loop(void); 301 static void deco_loop(void);
299 static void resetToFirmwareUpdate(void); 302 static void resetToFirmwareUpdate(void);
300 static void TriggerButtonAction(void); 303 static void TriggerButtonAction(void);
301 static void EvaluateButton(void); 304 static void EvaluateButton(void);
302 static void RefreshDisplay(void); 305 static void RefreshDisplay(void);
306 static void TimeoutControlRequestModechange(void);
307 static void TimeoutControl(void);
303 308
304 /* ITM Trace-------- ---------------------------------------------------------*/ 309 /* ITM Trace-------- ---------------------------------------------------------*/
305 /* 310 /*
306 #define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n))) 311 #define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
307 #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n))) 312 #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
448 settingsGetPointer()->totalDiveCounter = totalDiveCounterFound; 453 settingsGetPointer()->totalDiveCounter = totalDiveCounterFound;
449 454
450 if( settingsGetPointer()->debugModeOnStart ) 455 if( settingsGetPointer()->debugModeOnStart )
451 { 456 {
452 settingsGetPointer()->debugModeOnStart = 0; 457 settingsGetPointer()->debugModeOnStart = 0;
453 ext_flash_write_settings(); 458 ext_flash_write_settings(0);
454 setDebugMode(); 459 setDebugMode();
455 openInfo( StIDEBUG ); 460 openInfo( StIDEBUG );
456 } 461 }
457 InitMotionDetection(); 462 InitMotionDetection();
458 463
474 tCCR_control(); 479 tCCR_control();
475 if( tComm_control() )// will stop while loop if tComm Mode started until exit from UART 480 if( tComm_control() )// will stop while loop if tComm Mode started until exit from UART
476 { 481 {
477 createDiveSettings(); 482 createDiveSettings();
478 updateMenu(); 483 updateMenu();
479 ext_flash_write_settings(); 484 ext_flash_write_settings(0);
480 } 485 }
481 486
482 /* check if tasks depending on global state are pending */ 487 /* check if tasks depending on global state are pending */
483 get_globalStateList(&status); 488 get_globalStateList(&status);
484 if(status.base == BaseHome) 489 if(status.base == BaseHome)
485 { 490 {
486 tMenuEdit_writeSettingsToFlash(); // takes 900 ms!! 491 tMenuEdit_writeSettingsToFlash();
487 } 492 }
488 493
489 DataEX_merge_devicedata(); /* data is exchanged at startup and every 10 minutes => check if something changed */ 494 DataEX_merge_devicedata(); /* data is exchanged at startup and every 10 minutes => check if something changed */
490 495
491 deco_loop(); 496 deco_loop();
492 TriggerButtonAction(); 497 TriggerButtonAction();
493 if(DoDisplayRefresh) /* set every 100ms by timer interrupt */ 498 if(DoDisplayRefresh) /* set every 100ms by timer interrupt */
494 { 499 {
495 DoDisplayRefresh = 0; 500 DoDisplayRefresh = 0;
496 RefreshDisplay(); 501 RefreshDisplay();
502
503 TimeoutControl(); /* exit menus if needed */
497 504
498 if(stateUsed->mode == MODE_DIVE) /* handle motion events in divemode only */ 505 if(stateUsed->mode == MODE_DIVE) /* handle motion events in divemode only */
499 { 506 {
500 switch(settingsGetPointer()->MotionDetection) 507 switch(settingsGetPointer()->MotionDetection)
501 { 508 {
581 { 588 {
582 HAL_GPIO_EXTI_Callback(demoGetCommand()); 589 HAL_GPIO_EXTI_Callback(demoGetCommand());
583 return; 590 return;
584 } 591 }
585 #endif 592 #endif
586 static uint8_t last_base;
587
588 SStateList status; 593 SStateList status;
589 uint32_t timeout_in_seconds; 594 _Bool modeChange = 0;
590 uint32_t timeout_limit_Surface_in_seconds;
591
592 595
593 BaseTick100ms = HAL_GetTick(); /* store start of 100ms cycle */ 596 BaseTick100ms = HAL_GetTick(); /* store start of 100ms cycle */
594
595 _Bool InDiveMode = 0;
596 _Bool modeChange = 0; // to exit from menu and logbook
597
598 if(stateUsed->mode == MODE_DIVE)
599 InDiveMode = 1;
600 else
601 InDiveMode = 0;
602 597
603 EvaluateButton(); 598 EvaluateButton();
604 599
605 if(returnFromCommCleanUpRequest) 600 if(returnFromCommCleanUpRequest)
606 { 601 {
650 DataEX_copy_to_LifeData(0); 645 DataEX_copy_to_LifeData(0);
651 } 646 }
652 break; 647 break;
653 } 648 }
654 649
655 /* timeout control */
656 if(modeChange) ///< from RTE, set in data_exchange_main.c
657 time_without_button_pressed_deciseconds = (settingsGetPointer()->timeoutSurfacemode / 4) * 3;
658 if(status.base != last_base)
659 time_without_button_pressed_deciseconds = 0;
660 last_base = status.base;
661 timeout_in_seconds = time_without_button_pressed_deciseconds / 10;
662 time_without_button_pressed_deciseconds += 1;
663 if(modeChange || (timeout_in_seconds != time_without_button_pressed_deciseconds / 10))
664 {
665 #ifdef NO_TIMEOUT
666 timeout_in_seconds = 0;
667 #else
668 timeout_in_seconds += 1;
669 #endif
670
671 if(InDiveMode)
672 {
673 switch(status.base)
674 {
675 case BaseHome:
676 if((status.line != 0) && (timeout_in_seconds >= settingsGetPointer()->timeoutEnterButtonSelectDive))
677 {
678 set_globalState(StD);
679 timeout_in_seconds = 0;
680 }
681 break;
682
683 case BaseMenu:
684 if((status.line == 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuDive) || modeChange))
685 {
686 exitMenu();
687 timeout_in_seconds = 0;
688 }
689 if((status.line != 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuEdit) || modeChange))
690 {
691 exitMenuEdit_to_Home();
692 timeout_in_seconds = 0;
693 }
694 break;
695 default:
696 break;
697 }
698 }
699 else /* surface mode */
700 {
701 switch(status.base)
702 {
703 case BaseHome:
704 // added hw 161027
705 if(!(stateRealGetPointer()->warnings.lowBattery) && (stateRealGetPointer()->lifeData.battery_charge > 9))
706 {
707 stateRealGetPointerWrite()->lastKnownBatteryPercentage = stateRealGetPointer()->lifeData.battery_charge;
708 }
709 else if((wasFirmwareUpdateCheckBattery) && (timeout_in_seconds > 3))
710 {
711 wasFirmwareUpdateCheckBattery = 0;
712 setButtonResponsiveness(settingsGetPointer()->ButtonResponsiveness); // added 170306
713 if( (settingsGetPointer()->lastKnownBatteryPercentage > 0)
714 && (settingsGetPointer()->lastKnownBatteryPercentage <= 100)
715 && (stateRealGetPointer()->warnings.lowBattery))
716 {
717 setBatteryPercentage(settingsGetPointer()->lastKnownBatteryPercentage);
718 }
719 }
720 // stuff before and new @161121 CCR-sensor limit 10 minutes
721 if((settingsGetPointer()->dive_mode == DIVEMODE_CCR) && (settingsGetPointer()->CCR_Mode == CCRMODE_Sensors))
722 {
723 timeout_limit_Surface_in_seconds = settingsGetPointer()->timeoutSurfacemodeWithSensors;
724 }
725 else
726 {
727 timeout_limit_Surface_in_seconds = settingsGetPointer()->timeoutSurfacemode;
728 }
729 if(timeout_in_seconds >= timeout_limit_Surface_in_seconds)
730 {
731 gotoSleep();
732 }
733 break;
734 case BaseMenu:
735 if((status.line == 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuSurface) || modeChange))
736 {
737 exitMenu();
738 timeout_in_seconds = 0;
739 }
740 if((status.line != 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuEdit) || modeChange))
741 {
742 if((status.page != (uint8_t)((StMPLAN >> 24) & 0x0F)) || (timeout_in_seconds >= 10*(settingsGetPointer()->timeoutMenuEdit)))
743 {
744 exitMenuEdit_to_Home();
745 timeout_in_seconds = 0;
746 }
747 }
748 break;
749
750 case BaseInfo:
751 if((timeout_in_seconds >= settingsGetPointer()->timeoutInfo) || modeChange)
752 {
753 if(status.page == InfoPageLogList)
754 {
755 exitLog();
756 timeout_in_seconds = 0;
757 }
758 else
759 if(status.page == InfoPageLogShow)
760 {
761 show_logbook_exit();
762 exitLog();
763 timeout_in_seconds = 0;
764 }
765 else
766 if(status.page != InfoPageCompass)
767 {
768 exitInfo();
769 timeout_in_seconds = 0;
770 }
771 }
772 break;
773 default:
774 break;
775 }
776 }
777 }
778
779 get_globalStateList(&status); 650 get_globalStateList(&status);
780 651 if(modeChange)
652 {
653 TimeoutControlRequestModechange();
654 }
781 if(status.base == BaseComm) /* main loop not serviced in com mode */ 655 if(status.base == BaseComm) /* main loop not serviced in com mode */
782 { 656 {
783 tComm_refresh(); 657 tComm_refresh();
784 } 658 }
785 else 659 else
1043 static void gotoSleep(void) 917 static void gotoSleep(void)
1044 { 918 {
1045 /* not at the moment of testing */ 919 /* not at the moment of testing */
1046 // ext_flash_erase_firmware_if_not_empty(); 920 // ext_flash_erase_firmware_if_not_empty();
1047 GFX_logoAutoOff(); 921 GFX_logoAutoOff();
922 ext_flash_write_devicedata(true); /* write data at default position */
923 ext_flash_write_settings(true); /* write data at default position */
1048 set_globalState(StStop); 924 set_globalState(StStop);
1049 } 925 }
1050 926
1051 927
1052 // ----------------------------- 928 // -----------------------------
1752 { 1628 {
1753 __HAL_RCC_CLEAR_RESET_FLAGS(); 1629 __HAL_RCC_CLEAR_RESET_FLAGS();
1754 HAL_NVIC_SystemReset(); 1630 HAL_NVIC_SystemReset();
1755 } 1631 }
1756 1632
1633 static void TimeoutControlRequestModechange(void)
1634 {
1635 RequestModeChange = 1;
1636 }
1637
1638 static void TimeoutControl(void)
1639 {
1640 static uint8_t last_base;
1641
1642 SStateList status;
1643 uint32_t timeout_in_seconds;
1644 uint32_t timeout_limit_Surface_in_seconds;
1645 _Bool InDiveMode = 0;
1646
1647 get_globalStateList(&status);
1648
1649 if(stateUsed->mode == MODE_DIVE)
1650 {
1651 InDiveMode = 1;
1652 }
1653 else
1654 {
1655 InDiveMode = 0;
1656 }
1657 /* timeout control */
1658 if(RequestModeChange) ///< from RTE, set in data_exchange_main.c
1659 time_without_button_pressed_deciseconds = (settingsGetPointer()->timeoutSurfacemode / 4) * 3;
1660 if(status.base != last_base)
1661 time_without_button_pressed_deciseconds = 0;
1662 last_base = status.base;
1663 timeout_in_seconds = time_without_button_pressed_deciseconds / 10;
1664 time_without_button_pressed_deciseconds += 1;
1665 if(RequestModeChange || (timeout_in_seconds != time_without_button_pressed_deciseconds / 10))
1666 {
1667 #ifdef NO_TIMEOUT
1668 timeout_in_seconds = 0;
1669 #else
1670 timeout_in_seconds += 1;
1671 #endif
1672
1673 if(InDiveMode)
1674 {
1675 switch(status.base)
1676 {
1677 case BaseHome:
1678 if((status.line != 0) && (timeout_in_seconds >= settingsGetPointer()->timeoutEnterButtonSelectDive))
1679 {
1680 set_globalState(StD);
1681 timeout_in_seconds = 0;
1682 }
1683 break;
1684
1685 case BaseMenu:
1686 if((status.line == 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuDive) || RequestModeChange))
1687 {
1688 exitMenu();
1689 timeout_in_seconds = 0;
1690 }
1691 if((status.line != 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuEdit) || RequestModeChange))
1692 {
1693 exitMenuEdit_to_Home();
1694 timeout_in_seconds = 0;
1695 }
1696 break;
1697 default:
1698 break;
1699 }
1700 }
1701 else /* surface mode */
1702 {
1703 switch(status.base)
1704 {
1705 case BaseHome:
1706 // added hw 161027
1707 if(!(stateRealGetPointer()->warnings.lowBattery) && (stateRealGetPointer()->lifeData.battery_charge > 9))
1708 {
1709 stateRealGetPointerWrite()->lastKnownBatteryPercentage = stateRealGetPointer()->lifeData.battery_charge;
1710 }
1711 else if((wasFirmwareUpdateCheckBattery) && (timeout_in_seconds > 3))
1712 {
1713 wasFirmwareUpdateCheckBattery = 0;
1714 setButtonResponsiveness(settingsGetPointer()->ButtonResponsiveness); // added 170306
1715 if( (settingsGetPointer()->lastKnownBatteryPercentage > 0)
1716 && (settingsGetPointer()->lastKnownBatteryPercentage <= 100)
1717 && (stateRealGetPointer()->warnings.lowBattery))
1718 {
1719 setBatteryPercentage(settingsGetPointer()->lastKnownBatteryPercentage);
1720 }
1721 }
1722 // stuff before and new @161121 CCR-sensor limit 10 minutes
1723 if((settingsGetPointer()->dive_mode == DIVEMODE_CCR) && (settingsGetPointer()->CCR_Mode == CCRMODE_Sensors))
1724 {
1725 timeout_limit_Surface_in_seconds = settingsGetPointer()->timeoutSurfacemodeWithSensors;
1726 }
1727 else
1728 {
1729 timeout_limit_Surface_in_seconds = settingsGetPointer()->timeoutSurfacemode;
1730 }
1731 if(timeout_in_seconds >= timeout_limit_Surface_in_seconds)
1732 {
1733 gotoSleep();
1734 }
1735 break;
1736 case BaseMenu:
1737 if((status.line == 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuSurface) || RequestModeChange))
1738 {
1739 exitMenu();
1740 timeout_in_seconds = 0;
1741 }
1742 if((status.line != 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuEdit) || RequestModeChange))
1743 {
1744 if((status.page != (uint8_t)((StMPLAN >> 24) & 0x0F)) || (timeout_in_seconds >= 10*(settingsGetPointer()->timeoutMenuEdit)))
1745 {
1746 exitMenuEdit_to_Home();
1747 timeout_in_seconds = 0;
1748 }
1749 }
1750 break;
1751
1752 case BaseInfo:
1753 if((timeout_in_seconds >= settingsGetPointer()->timeoutInfo) || RequestModeChange)
1754 {
1755 if(status.page == InfoPageLogList)
1756 {
1757 exitLog();
1758 timeout_in_seconds = 0;
1759 }
1760 else
1761 if(status.page == InfoPageLogShow)
1762 {
1763 show_logbook_exit();
1764 exitLog();
1765 timeout_in_seconds = 0;
1766 }
1767 else
1768 if(status.page != InfoPageCompass)
1769 {
1770 exitInfo();
1771 timeout_in_seconds = 0;
1772 }
1773 }
1774 break;
1775 default:
1776 break;
1777 }
1778 }
1779 }
1780 RequestModeChange = 0;
1781 }
1757 // debugging by https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/ 1782 // debugging by https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
1758 1783
1759 /* 1784 /*
1760 void printErrorMsg(const char * errMsg) 1785 void printErrorMsg(const char * errMsg)
1761 { 1786 {