comparison Discovery/Src/base.c @ 422:b67327177159 ImprovmentNVM_2

Moved Timeout handling out of 100ms callback: Handling of the timeout is not timecritical and may be done in the main loop. This was needed because writing of flash data shall be done before going to sleep. Having this function in the timer callback would raise the risk of flash inconsitencies due to parallel calls out of the main loop => Created a separate function containing the old timeout detection functionality
author ideenmodellierer
date Mon, 10 Feb 2020 19:29:12 +0100
parents b3ab85121e4d
children b1091e183d52
comparison
equal deleted inserted replaced
421:3f7d80f37bfc 422:b67327177159
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)))
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 */
1048 set_globalState(StStop); 923 set_globalState(StStop);
1049 } 924 }
1050 925
1051 926
1052 // ----------------------------- 927 // -----------------------------
1752 { 1627 {
1753 __HAL_RCC_CLEAR_RESET_FLAGS(); 1628 __HAL_RCC_CLEAR_RESET_FLAGS();
1754 HAL_NVIC_SystemReset(); 1629 HAL_NVIC_SystemReset();
1755 } 1630 }
1756 1631
1632 static void TimeoutControlRequestModechange(void)
1633 {
1634 RequestModeChange = 1;
1635 }
1636
1637 static void TimeoutControl(void)
1638 {
1639 static uint8_t last_base;
1640
1641 SStateList status;
1642 uint32_t timeout_in_seconds;
1643 uint32_t timeout_limit_Surface_in_seconds;
1644 _Bool InDiveMode = 0;
1645
1646 get_globalStateList(&status);
1647
1648 if(stateUsed->mode == MODE_DIVE)
1649 {
1650 InDiveMode = 1;
1651 }
1652 else
1653 {
1654 InDiveMode = 0;
1655 }
1656 /* timeout control */
1657 if(RequestModeChange) ///< from RTE, set in data_exchange_main.c
1658 time_without_button_pressed_deciseconds = (settingsGetPointer()->timeoutSurfacemode / 4) * 3;
1659 if(status.base != last_base)
1660 time_without_button_pressed_deciseconds = 0;
1661 last_base = status.base;
1662 timeout_in_seconds = time_without_button_pressed_deciseconds / 10;
1663 time_without_button_pressed_deciseconds += 1;
1664 if(RequestModeChange || (timeout_in_seconds != time_without_button_pressed_deciseconds / 10))
1665 {
1666 #ifdef NO_TIMEOUT
1667 timeout_in_seconds = 0;
1668 #else
1669 timeout_in_seconds += 1;
1670 #endif
1671
1672 if(InDiveMode)
1673 {
1674 switch(status.base)
1675 {
1676 case BaseHome:
1677 if((status.line != 0) && (timeout_in_seconds >= settingsGetPointer()->timeoutEnterButtonSelectDive))
1678 {
1679 set_globalState(StD);
1680 timeout_in_seconds = 0;
1681 }
1682 break;
1683
1684 case BaseMenu:
1685 if((status.line == 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuDive) || RequestModeChange))
1686 {
1687 exitMenu();
1688 timeout_in_seconds = 0;
1689 }
1690 if((status.line != 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuEdit) || RequestModeChange))
1691 {
1692 exitMenuEdit_to_Home();
1693 timeout_in_seconds = 0;
1694 }
1695 break;
1696 default:
1697 break;
1698 }
1699 }
1700 else /* surface mode */
1701 {
1702 switch(status.base)
1703 {
1704 case BaseHome:
1705 // added hw 161027
1706 if(!(stateRealGetPointer()->warnings.lowBattery) && (stateRealGetPointer()->lifeData.battery_charge > 9))
1707 {
1708 stateRealGetPointerWrite()->lastKnownBatteryPercentage = stateRealGetPointer()->lifeData.battery_charge;
1709 }
1710 else if((wasFirmwareUpdateCheckBattery) && (timeout_in_seconds > 3))
1711 {
1712 wasFirmwareUpdateCheckBattery = 0;
1713 setButtonResponsiveness(settingsGetPointer()->ButtonResponsiveness); // added 170306
1714 if( (settingsGetPointer()->lastKnownBatteryPercentage > 0)
1715 && (settingsGetPointer()->lastKnownBatteryPercentage <= 100)
1716 && (stateRealGetPointer()->warnings.lowBattery))
1717 {
1718 setBatteryPercentage(settingsGetPointer()->lastKnownBatteryPercentage);
1719 }
1720 }
1721 // stuff before and new @161121 CCR-sensor limit 10 minutes
1722 if((settingsGetPointer()->dive_mode == DIVEMODE_CCR) && (settingsGetPointer()->CCR_Mode == CCRMODE_Sensors))
1723 {
1724 timeout_limit_Surface_in_seconds = settingsGetPointer()->timeoutSurfacemodeWithSensors;
1725 }
1726 else
1727 {
1728 timeout_limit_Surface_in_seconds = settingsGetPointer()->timeoutSurfacemode;
1729 }
1730 if(timeout_in_seconds >= timeout_limit_Surface_in_seconds)
1731 {
1732 gotoSleep();
1733 }
1734 break;
1735 case BaseMenu:
1736 if((status.line == 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuSurface) || RequestModeChange))
1737 {
1738 exitMenu();
1739 timeout_in_seconds = 0;
1740 }
1741 if((status.line != 0) && ((timeout_in_seconds >= settingsGetPointer()->timeoutMenuEdit) || RequestModeChange))
1742 {
1743 if((status.page != (uint8_t)((StMPLAN >> 24) & 0x0F)) || (timeout_in_seconds >= 10*(settingsGetPointer()->timeoutMenuEdit)))
1744 {
1745 exitMenuEdit_to_Home();
1746 timeout_in_seconds = 0;
1747 }
1748 }
1749 break;
1750
1751 case BaseInfo:
1752 if((timeout_in_seconds >= settingsGetPointer()->timeoutInfo) || RequestModeChange)
1753 {
1754 if(status.page == InfoPageLogList)
1755 {
1756 exitLog();
1757 timeout_in_seconds = 0;
1758 }
1759 else
1760 if(status.page == InfoPageLogShow)
1761 {
1762 show_logbook_exit();
1763 exitLog();
1764 timeout_in_seconds = 0;
1765 }
1766 else
1767 if(status.page != InfoPageCompass)
1768 {
1769 exitInfo();
1770 timeout_in_seconds = 0;
1771 }
1772 }
1773 break;
1774 default:
1775 break;
1776 }
1777 }
1778 }
1779 RequestModeChange = 0;
1780 }
1757 // debugging by https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/ 1781 // debugging by https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
1758 1782
1759 /* 1783 /*
1760 void printErrorMsg(const char * errMsg) 1784 void printErrorMsg(const char * errMsg)
1761 { 1785 {