comparison Small_CPU/Src/scheduler.c @ 207:b95741467355 ImprovmentSPI

Introduce scheduler function The first implementation was only focussed on doing a hard sync and the new one uses an interface instead of global variables
author ideenmodellierer
date Sun, 24 Mar 2019 22:53:17 +0100
parents f11f0bf6ef2d
children e524a824d8f2
comparison
equal deleted inserted replaced
206:0bb6a8e7be9d 207:b95741467355
55 /* Exported variables --------------------------------------------------------*/ 55 /* Exported variables --------------------------------------------------------*/
56 SGlobal global; 56 SGlobal global;
57 SDevice DeviceDataFlash; 57 SDevice DeviceDataFlash;
58 uint8_t deviceDataFlashValid = 0; 58 uint8_t deviceDataFlashValid = 0;
59 uint8_t deviceDataSubSeconds = 0; 59 uint8_t deviceDataSubSeconds = 0;
60 uint8_t dohardspisync = 1;
61 60
62 /* Private variables ---------------------------------------------------------*/ 61 /* Private variables ---------------------------------------------------------*/
63 /* can be lost while in sleep */ 62 /* can be lost while in sleep */
64 uint8_t clearDecoNow = 0; 63 uint8_t clearDecoNow = 0;
65 uint8_t setButtonsNow = 0; 64 uint8_t setButtonsNow = 0;
66 65
67 /* has to be in SRAM2 */ 66 /* has to be in SRAM2 */
68 uint8_t secondsCount = 0; 67 uint8_t secondsCount = 0;
68
69 static uint8_t dospisync = SPI_SYNC_METHOD_NONE;
69 70
70 SScheduleCtrl Scheduler; 71 SScheduleCtrl Scheduler;
71 72
72 /* Private function prototypes -----------------------------------------------*/ 73 /* Private function prototypes -----------------------------------------------*/
73 74
178 global.deviceData.hoursOfOperation.value_int32 = 0; 179 global.deviceData.hoursOfOperation.value_int32 = 0;
179 global.deviceData.temperatureMaximum.value_int32 = INT32_MIN; 180 global.deviceData.temperatureMaximum.value_int32 = INT32_MIN;
180 global.deviceData.temperatureMinimum.value_int32 = INT32_MAX; 181 global.deviceData.temperatureMinimum.value_int32 = INT32_MAX;
181 global.deviceData.voltageMinimum.value_int32 = INT32_MAX; 182 global.deviceData.voltageMinimum.value_int32 = INT32_MAX;
182 183
183 dohardspisync = 1; 184 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
184 } 185 }
185 186
186 187
187 void scheduleSpecial_Evaluate_DataSendToSlave(void) 188 void scheduleSpecial_Evaluate_DataSendToSlave(void)
188 { 189 {
438 439
439 /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the 440 /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the
440 * function error handler 441 * function error handler
441 */ 442 */
442 SPI_Start_single_TxRx_with_Master(); 443 SPI_Start_single_TxRx_with_Master();
443 } 444 Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT);
444 if((global.check_sync_not_running == 10)) /* connection lost for about a second. Could be debugging or Firmware update */
445 {
446 dohardspisync = 1;
447 } 445 }
448 } 446 }
449 447
450 448
451 /** 449 /**
752 global.dataSendToMaster.mode = MODE_SURFACE; 750 global.dataSendToMaster.mode = MODE_SURFACE;
753 global.deviceDataSendToMaster.mode = MODE_SURFACE; 751 global.deviceDataSendToMaster.mode = MODE_SURFACE;
754 752
755 while(global.mode == MODE_SURFACE) 753 while(global.mode == MODE_SURFACE)
756 { 754 {
757 /* printf("surface...\n"); */
758 // SPI_Start_single_TxRx_with_Master();
759 schedule_check_resync(); 755 schedule_check_resync();
760 lasttick = HAL_GetTick(); 756 lasttick = HAL_GetTick();
761 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); 757 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick);
762 758
763 if(setButtonsNow == 1) 759 if(setButtonsNow == 1)
764 { 760 {
765 if(scheduleSetButtonResponsiveness()) 761 if(scheduleSetButtonResponsiveness())
766 setButtonsNow = 0; 762 setButtonsNow = 0;
767 } 763 }
768 764
769
770 //Evaluate received data at 10 ms, 110 ms, 210 ms,... 765 //Evaluate received data at 10 ms, 110 ms, 210 ms,...
771 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) 766 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
772 { 767 {
773 SPI_Evaluate_RX_Data(); 768 SPI_Evaluate_RX_Data();
774 Scheduler.counterSPIdata100msec++; 769 Scheduler.counterSPIdata100msec++;
786 781
787 if(scheduleCheck_pressure_reached_dive_mode_level()) 782 if(scheduleCheck_pressure_reached_dive_mode_level())
788 global.mode = MODE_DIVE; 783 global.mode = MODE_DIVE;
789 } 784 }
790 785
791 //evaluate compass data at 50 ms, 150 ms, 250 ms,... 786 //Evaluate compass data at 50 ms, 150 ms, 250 ms,...
792
793 if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50) 787 if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50)
794 { 788 {
795 compass_read(); 789 compass_read();
796 acceleration_read(); 790 acceleration_read();
797 compass_calc(); 791 compass_calc();
879 Scheduler.counterAmbientLight100msec = 0; 873 Scheduler.counterAmbientLight100msec = 0;
880 } 874 }
881 } 875 }
882 } 876 }
883 877
884 void HardSyncToSPI() 878 inline void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod)
885 { 879 {
886 if(dohardspisync) 880 if( SyncMethod < SPI_SYNC_METHOD_INVALID)
887 { 881 {
888 //Set back tick counter 882 dospisync = SyncMethod;
889 Scheduler.tickstart = HAL_GetTick(); 883 }
890 Scheduler.counterSPIdata100msec = 0; 884 }
891 Scheduler.counterCompass100msec = 0; 885
892 Scheduler.counterPressure100msec = 0; 886 void Scheduler_SyncToSPI()
893 Scheduler.counterAmbientLight100msec = 0; 887 {
894 dohardspisync = 0; 888 uint32_t deltatick = 0;
889
890 switch(dospisync)
891 {
892 case SPI_SYNC_METHOD_HARD:
893 //Set back tick counter
894 Scheduler.tickstart = HAL_GetTick();
895 Scheduler.counterSPIdata100msec = 0;
896 Scheduler.counterCompass100msec = 0;
897 Scheduler.counterPressure100msec = 0;
898 Scheduler.counterAmbientLight100msec = 0;
899 dospisync = SPI_SYNC_METHOD_NONE;
900 break;
901 case SPI_SYNC_METHOD_SOFT:
902 deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick());
903 deltatick %= 100; /* clip to 100ms window */
904 if(Scheduler.tickstart - deltatick >= 0) /* adjust start time to the next 100ms window */
905 {
906 Scheduler.tickstart -= deltatick;
907 }
908 else
909 {
910 Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart);
911 }
912 dospisync = SPI_SYNC_METHOD_NONE;
913 break;
914 default:
915 break;
895 } 916 }
896 } 917 }
897 918
898 /** 919 /**
899 ****************************************************************************** 920 ******************************************************************************