Mercurial > public > ostc4
changeset 264:b3685fbada3b IPC_Sync_Improvment_2
Sync to Main 100ms time stamp & added Reinitialization of globals after startup
Added function to synchronize RTE to the timestamp provided by Main 100ms callback. This was introduced to compensated small shifts in execution and program flow shifts (for example during long operation times)
author | ideenmodellierer |
---|---|
date | Sun, 14 Apr 2019 11:43:29 +0200 |
parents | a6c0375bc950 |
children | a91d99265884 |
files | Small_CPU/Inc/scheduler.h Small_CPU/Inc/spi.h Small_CPU/Src/scheduler.c Small_CPU/Src/spi.c |
diffstat | 4 files changed, 63 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/Small_CPU/Inc/scheduler.h Sun Apr 14 11:38:14 2019 +0200 +++ b/Small_CPU/Inc/scheduler.h Sun Apr 14 11:43:29 2019 +0200 @@ -96,6 +96,7 @@ /* Function prototypes -----------------------------------------------*/ void initGlobals(void); +void reinitGlobals(void); void scheduleSurfaceMode(void); void scheduleDiveMode(void); @@ -109,7 +110,7 @@ void scheduleUpdateDeviceDataChargerCharging(void); void Scheduler_Request_sync_with_SPI(uint8_t SyncMethod); -void Scheduler_SyncToSPI(void); +void Scheduler_SyncToSPI(uint8_t TXtick); uint8_t scheduleSetButtonResponsiveness(void);
--- a/Small_CPU/Inc/spi.h Sun Apr 14 11:38:14 2019 +0200 +++ b/Small_CPU/Inc/spi.h Sun Apr 14 11:43:29 2019 +0200 @@ -30,7 +30,7 @@ //void SPI_Start_single_TxRx_with_Master_and_Stop_ChipSelectControl(void); void SPI_Start_single_TxRx_with_Master(void); void SPI_synchronize_with_Master(void); -void SPI_Evaluate_RX_Data(void); /*process the data received during last 100ms cycle */ +uint8_t SPI_Evaluate_RX_Data(void); /*process the data received during last 100ms cycle */ void MX_SPI_DeInit(void);
--- a/Small_CPU/Src/scheduler.c Sun Apr 14 11:38:14 2019 +0200 +++ b/Small_CPU/Src/scheduler.c Sun Apr 14 11:43:29 2019 +0200 @@ -88,7 +88,6 @@ _Bool scheduleCheck_pressure_reached_dive_mode_level(void); void scheduleSetDate(SDeviceLine *line); -extern void SPI_Evaluate_RX_Data(); /* Exported functions --------------------------------------------------------*/ void initGlobals(void) @@ -136,7 +135,7 @@ global.dataSendToMaster.power_on_reset = 1; global.dataSendToMaster.header.checkCode[0] = 0xA1; - global.dataSendToMaster.header.checkCode[1] = 0xA2; + global.dataSendToMaster.header.checkCode[1] = SPI_RX_STATE_OFFLINE; global.dataSendToMaster.header.checkCode[2] = 0xA3; global.dataSendToMaster.header.checkCode[3] = 0xA4; global.dataSendToMaster.footer.checkCode[3] = 0xE4; @@ -176,6 +175,14 @@ Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); } +void reinitGlobals(void) +{ + global.dataSendToSlavePending = 0; + global.dataSendToSlaveIsValid = 0; + global.dataSendToSlaveIsNotValidCount = 0; + global.sync_error_count = 0; + global.check_sync_not_running = 0; +} void scheduleSpecial_Evaluate_DataSendToSlave(void) { @@ -276,7 +283,7 @@ memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice)); deviceDataFlashValid = 1; - +#if 0 //TODO: Temporary placed here. Duration ~210 ms. if (global.I2C_SystemStatus != HAL_OK) { MX_I2C1_TestAndClear(); @@ -285,6 +292,7 @@ // compass_init(0, 7); // accelerator_init(); } +#endif /* already called once a second */ } @@ -402,7 +410,7 @@ void schedule_check_resync(void) { /* counter is incremented in cyclic 100ms loop and reset to 0 if the transmission complete callback is called */ - if((global.check_sync_not_running >= 3)) + if((global.check_sync_not_running >= 5)) { // global.dataSendToSlaveIsNotValidCount = 0; global.check_sync_not_running = 0; @@ -411,8 +419,8 @@ /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the * function error handler */ - Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT); SPI_Start_single_TxRx_with_Master(); + Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_HARD); } } @@ -434,7 +442,7 @@ global.dataSendToMaster.mode = MODE_DIVE; global.deviceDataSendToMaster.mode = MODE_DIVE; uint8_t counter_exit = 0; - + Scheduler.counterSPIdata100msec = 0; Scheduler.counterCompass100msec = 0; Scheduler.counterPressure100msec = 0; @@ -739,8 +747,10 @@ /* Evaluate received data at 10 ms, 110 ms, 210 ms,... duration ~<1ms */ if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) { - SPI_Evaluate_RX_Data(); - Scheduler.counterSPIdata100msec++; + if(SPI_Evaluate_RX_Data()!=0) /* did we receive something ? */ + { + Scheduler.counterSPIdata100msec++; + } } /* Evaluate pressure at 20 ms, 120 ms, 220 ms,... duration ~22ms] */ @@ -862,15 +872,16 @@ } } -void Scheduler_SyncToSPI() +void Scheduler_SyncToSPI(uint8_t TXtick) { uint32_t deltatick = 0; + int8_t TXcompensation; switch(dospisync) { case SPI_SYNC_METHOD_HARD: //Set back tick counter - Scheduler.tickstart = HAL_GetTick(); + Scheduler.tickstart = HAL_GetTick() - 4; /* consider 4ms offset for transfer */ Scheduler.counterSPIdata100msec = 0; Scheduler.counterCompass100msec = 0; Scheduler.counterPressure100msec = 0; @@ -890,7 +901,27 @@ } dospisync = SPI_SYNC_METHOD_NONE; break; - default: + default: /* continous sync activity */ + if(TXtick < 100) /* do not handle unexpected jump length > 100ms */ + { + TXtick += 4; /* add 4ms TX time to offset of 100ms time stamp */ + deltatick = time_elapsed_ms(Scheduler.tickstart,HAL_GetTick()); + deltatick %= 100; + if(deltatick > 50) + { + TXcompensation = deltatick - 100; /* neg drift */ + } + else + { + TXcompensation = deltatick; /* pos drift */ + } + TXcompensation = TXtick - TXcompensation; + Scheduler.tickstart -= TXcompensation; + } + else + { + Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT); /* A large shift in 100ms cycle occured => clip to 100ms in next sync call */ + } break; } } @@ -1000,6 +1031,7 @@ scheduleUpdateLifeData(-1); clearDecoNow = 0; setButtonsNow = 0; + reinitGlobals(); }
--- a/Small_CPU/Src/spi.c Sun Apr 14 11:38:14 2019 +0200 +++ b/Small_CPU/Src/spi.c Sun Apr 14 11:43:29 2019 +0200 @@ -317,7 +317,15 @@ /* restart SPI */ if (hspi == &hspi1) { - Scheduler_SyncToSPI(); + if(SPI_check_header_and_footer_ok()) /* process timestamp provided by main */ + { + Scheduler_SyncToSPI(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_TX_TICK]); + } + else + { + Scheduler_SyncToSPI(0); /* => no async will be calculated */ + } + SPIDataRX = 1; /* stop data exchange? */ @@ -330,9 +338,10 @@ } } -void SPI_Evaluate_RX_Data() +uint8_t SPI_Evaluate_RX_Data() { uint8_t resettimeout = 1; + uint8_t ret = SPIDataRX; if ((global.mode != MODE_SHUTDOWN) && ( global.mode != MODE_SLEEP) && (SPIDataRX)) { @@ -385,12 +394,13 @@ scheduleSpecial_Evaluate_DataSendToSlave(); SPI_Start_single_TxRx_with_Master(); + + if(resettimeout) + { + global.check_sync_not_running = 0; + } } - - if(resettimeout) - { - global.check_sync_not_running = 0; - } + return ret; } static uint8_t SPI_check_header_and_footer_ok(void) {