changeset 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 0bb6a8e7be9d
children 9fc06e1e0f66
files Small_CPU/Inc/scheduler.h Small_CPU/Src/baseCPU2.c Small_CPU/Src/compass.c Small_CPU/Src/scheduler.c
diffstat 4 files changed, 50 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Small_CPU/Inc/scheduler.h	Sun Mar 24 22:51:15 2019 +0100
+++ b/Small_CPU/Inc/scheduler.h	Sun Mar 24 22:53:17 2019 +0100
@@ -32,6 +32,11 @@
 #define SENSOR_PRESSURE_ID 0
 #define MAX_SENSORS 1
  
+#define SPI_SYNC_METHOD_NONE	(0u)
+#define SPI_SYNC_METHOD_HARD	(1u)	/* Scheduler shall reset all counters to 0 */
+#define SPI_SYNC_METHOD_SOFT	(2u)	/* Scheduler shall reset adjust counters to 100ms SPI data exchange cycle */
+#define SPI_SYNC_METHOD_INVALID	(4u)
+
 typedef struct
 {
 	uint8_t mode;
@@ -83,7 +88,6 @@
 
 /* Variables ---------------------------------------------------------*/
 extern SGlobal global;
-extern uint8_t dohardspisync;
 
 
 /* Function prototypes -----------------------------------------------*/
@@ -101,6 +105,9 @@
 void scheduleUpdateDeviceDataChargerFull(void);
 void scheduleUpdateDeviceDataChargerCharging(void);
 
+void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod);
+void Scheduler_SyncToSPI(void);
+
 uint8_t scheduleSetButtonResponsiveness(void);
 
 void copyBatteryData(void);
--- a/Small_CPU/Src/baseCPU2.c	Sun Mar 24 22:51:15 2019 +0100
+++ b/Small_CPU/Src/baseCPU2.c	Sun Mar 24 22:53:17 2019 +0100
@@ -379,7 +379,7 @@
 			MX_DMA_Init();
 			MX_SPI1_Init();
 			SPI_Start_single_TxRx_with_Master(); /* be prepared for the first data exchange */
-			dohardspisync = 1;
+			Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 			EXTI_Test_Button_Init();
 			global.mode = MODE_SURFACE;
 			break;
--- a/Small_CPU/Src/compass.c	Sun Mar 24 22:51:15 2019 +0100
+++ b/Small_CPU/Src/compass.c	Sun Mar 24 22:53:17 2019 +0100
@@ -34,7 +34,6 @@
 #include "compass_LSM303D.h"
 #include "compass_LSM303DLHC.h"
 
-#include "spi.h"
 #include "i2c.h"
 #include "RTE_FlashAccess.h" // to store compass_calib_data
 
--- a/Small_CPU/Src/scheduler.c	Sun Mar 24 22:51:15 2019 +0100
+++ b/Small_CPU/Src/scheduler.c	Sun Mar 24 22:53:17 2019 +0100
@@ -57,7 +57,6 @@
 SDevice DeviceDataFlash;
 uint8_t deviceDataFlashValid = 0;
 uint8_t deviceDataSubSeconds = 0;
-uint8_t dohardspisync = 1;
 
 /* Private variables ---------------------------------------------------------*/
 /* can be lost while in sleep */
@@ -67,6 +66,8 @@
 /* has to be in SRAM2 */
 uint8_t secondsCount = 0;
 
+static uint8_t dospisync = SPI_SYNC_METHOD_NONE;
+
 SScheduleCtrl Scheduler;
  
 /* Private function prototypes -----------------------------------------------*/
@@ -180,7 +181,7 @@
 	global.deviceData.temperatureMinimum.value_int32 = INT32_MAX;
 	global.deviceData.voltageMinimum.value_int32 = INT32_MAX;
 
-	dohardspisync = 1;
+	Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD);
 }
 
 
@@ -440,10 +441,7 @@
 		 * function error handler
 		 */
 		SPI_Start_single_TxRx_with_Master();
-	}
-	if((global.check_sync_not_running == 10)) /* connection lost for about a second. Could be debugging or Firmware update */
-	{
-		dohardspisync = 1;
+		Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT);
 	}
 }
 
@@ -754,8 +752,6 @@
 
 	while(global.mode == MODE_SURFACE)
 	{
-	/*    printf("surface...\n"); */
-//	    SPI_Start_single_TxRx_with_Master();
 		schedule_check_resync();
 		lasttick = HAL_GetTick();
 		ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick);
@@ -766,7 +762,6 @@
 				setButtonsNow = 0;
 		}
 		
-
 		//Evaluate received data at 10 ms, 110 ms, 210 ms,...
 		if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10)
 		{
@@ -788,8 +783,7 @@
 					global.mode = MODE_DIVE;
 		}
 		
-		//evaluate compass data at 50 ms, 150 ms, 250 ms,...
-
+		//Evaluate compass data at 50 ms, 150 ms, 250 ms,...
 		if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50)
 		{
 			compass_read();
@@ -881,17 +875,44 @@
 	}
 }
 
-void HardSyncToSPI()
+inline void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod)
 {
-	if(dohardspisync)
+	if( SyncMethod < SPI_SYNC_METHOD_INVALID)
+	{
+		dospisync = SyncMethod;
+	}
+}
+
+void Scheduler_SyncToSPI()
+{
+	uint32_t deltatick = 0;
+
+	switch(dospisync)
 	{
-		//Set back tick counter
-		Scheduler.tickstart = HAL_GetTick();
-		Scheduler.counterSPIdata100msec = 0;
-		Scheduler.counterCompass100msec = 0;
-		Scheduler.counterPressure100msec = 0;
-		Scheduler.counterAmbientLight100msec = 0;
-		dohardspisync = 0;
+		case SPI_SYNC_METHOD_HARD:
+				//Set back tick counter
+				Scheduler.tickstart = HAL_GetTick();
+				Scheduler.counterSPIdata100msec = 0;
+				Scheduler.counterCompass100msec = 0;
+				Scheduler.counterPressure100msec = 0;
+				Scheduler.counterAmbientLight100msec = 0;
+				dospisync = SPI_SYNC_METHOD_NONE;
+			break;
+		case SPI_SYNC_METHOD_SOFT:
+				deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick());
+				deltatick %= 100;  						/* clip to 100ms window */
+				if(Scheduler.tickstart - deltatick >= 0) /* adjust start time to the next 100ms window */
+				{
+					Scheduler.tickstart -= deltatick;
+				}
+				else
+				{
+					Scheduler.tickstart = 0xFFFFFFFF- (deltatick - Scheduler.tickstart);
+				}
+				dospisync = SPI_SYNC_METHOD_NONE;
+			break;
+		default:
+			break;
 	}
 }