comparison Discovery/Src/data_exchange_main.c @ 208:9fc06e1e0f66 ImprovmentSPI

Update SPI error display and handling For easier identification of a communication problem the RX state of Main and RTE is displayed in the debug view. Also error reactions are now handles based on this state. E.g. RTE resets its DMA incase Main reports a data shift which can not be resolved by Main itself In addition the timeout for error detection has been decreased to have a faster reaction
author ideenmodellierer
date Sun, 24 Mar 2019 22:57:28 +0100
parents 878dc9e0dbc5
children f0069f002c55
comparison
equal deleted inserted replaced
207:b95741467355 208:9fc06e1e0f66
143 data_old__lost_connection_to_slave_counter_total = 0; 143 data_old__lost_connection_to_slave_counter_total = 0;
144 144
145 memset((void *)&dataOut, 0, sizeof(SDataReceiveFromMaster)); 145 memset((void *)&dataOut, 0, sizeof(SDataReceiveFromMaster));
146 146
147 dataOut.header.checkCode[0] = 0xBB; 147 dataOut.header.checkCode[0] = 0xBB;
148 dataOut.header.checkCode[1] = 0x01; 148 dataOut.header.checkCode[1] = SPI_RX_STATE_OK;
149 dataOut.header.checkCode[2] = 0x01; 149 dataOut.header.checkCode[2] = SPI_RX_STATE_OK;
150 dataOut.header.checkCode[3] = 0xBB; 150 dataOut.header.checkCode[3] = 0xBB;
151 151
152 dataOut.footer.checkCode[0] = 0xF4; 152 dataOut.footer.checkCode[0] = 0xF4;
153 dataOut.footer.checkCode[1] = 0xF3; 153 dataOut.footer.checkCode[1] = 0xF3;
154 dataOut.footer.checkCode[2] = 0xF2; 154 dataOut.footer.checkCode[2] = 0xF2;
256 } 256 }
257 257
258 258
259 uint8_t DataEX_call(void) 259 uint8_t DataEX_call(void)
260 { 260 {
261 static uint32_t RTEOfflineCnt = 0;
262 static uint8_t SusppressCom = 0;
263
261 uint8_t SPI_DMA_answer = 0; 264 uint8_t SPI_DMA_answer = 0;
262 265
263 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET); 266 if(SusppressCom)
264 267 {
265 if(data_old__lost_connection_to_slave_counter_temp >= 3) 268 SusppressCom--;
266 { 269 }
267 data_old__lost_connection_to_slave_counter_temp = 0;
268 if((DataEX_check_header_and_footer_shifted()) && (data_old__lost_connection_to_slave_counter_retry == 0))
269 {
270 HAL_SPI_Abort_IT(&cpu2DmaSpi);
271 }
272 /* reset of own DMA does not work ==> request reset of slave dma */
273 if((DataEX_check_header_and_footer_shifted()) && (data_old__lost_connection_to_slave_counter_retry == 2))
274 {
275 dataOut.header.checkCode[SPI_HEADER_INDEX_SLAVE] = 0xA5;
276 }
277 data_old__lost_connection_to_slave_counter_retry++;
278 }
279 #if USE_OLD_SYNC_METHOD
280 /* one cycle with NotChipSelect true to clear slave spi buffer */
281 else 270 else
282 { 271 {
283 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET); 272 if(data_old__lost_connection_to_slave_counter_temp >= 2) /* error reaction is triggered whenever communication could not be reestablishen within two cycles */
284 } 273 {
285 #endif 274 data_old__lost_connection_to_slave_counter_temp = 0;
286 275 if(DataEX_check_header_and_footer_shifted())
287 DataEx_call_helper_requests(); 276 {
288 277 if(RTEOfflineCnt > 1) /* RTE restarted communication after a longer silent time => restart error handling to recover */
289 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */ 278 {
290 279 data_old__lost_connection_to_slave_counter_retry = 0;
291 280 RTEOfflineCnt = 0;
292 SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE); 281 }
293 if(SPI_DMA_answer != HAL_OK) 282
294 { 283 /* We received shifted data. Step one. Reset DMA to see if the problem is located at main */
295 DataEX_Error_Handler(SPI_DMA_answer); 284 if (data_old__lost_connection_to_slave_counter_retry == 0)
296 } 285 {
286 HAL_SPI_Abort_IT(&cpu2DmaSpi);
287 }
288 /* reset of own DMA does not work ==> request reset of slave dma by indicating shifted receiption */
289 if (data_old__lost_connection_to_slave_counter_retry == 1)
290 {
291 dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_SHIFTED;
292 }
293
294 /* stop communication with RTE to trigger RTE timeout reaction */
295 if (data_old__lost_connection_to_slave_counter_retry == 2)
296 {
297 SusppressCom = 3;
298 }
299
300 data_old__lost_connection_to_slave_counter_retry++;
301 }
302 else
303 {
304 RTEOfflineCnt++; /* based on footer status the RTE does not seem to provide data in time */
305 dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OFFLINE;
306 }
307 }
308 #if USE_OLD_SYNC_METHOD
309 /* one cycle with NotChipSelect true to clear slave spi buffer */
310 else
311 {
312 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
313 }
314 #endif
315
316 DataEx_call_helper_requests();
317
318 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */
319
320 if(SusppressCom == 0)
321 {
322 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
323
324 SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE);
325 if(SPI_DMA_answer != HAL_OK)
326 {
327 DataEX_Error_Handler(SPI_DMA_answer);
328 }
329 }
330 }
297 // HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET); 331 // HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET);
298 //HAL_Delay(3); 332 //HAL_Delay(3);
299 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_SET); /* only for testing with Oscilloscope */ 333 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_SET); /* only for testing with Oscilloscope */
300 334
301 return 1; 335 return 1;
308 } 342 }
309 343
310 SDataExchangeSlaveToMaster* get_dataInPointer(void){ 344 SDataExchangeSlaveToMaster* get_dataInPointer(void){
311 return &dataIn; 345 return &dataIn;
312 } 346 }
313
314 347
315 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) 348 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
316 { 349 {
317 if(hspi == &cpu2DmaSpi) 350 if(hspi == &cpu2DmaSpi)
318 { 351 {
669 if(DataEX_check_header_and_footer_devicedata()) 702 if(DataEX_check_header_and_footer_devicedata())
670 { 703 {
671 data_old__lost_connection_to_slave_counter_retry = 0; 704 data_old__lost_connection_to_slave_counter_retry = 0;
672 data_old__lost_connection_to_slave_counter_temp = 0; 705 data_old__lost_connection_to_slave_counter_temp = 0;
673 stateRealGetPointerWrite()->data_old__lost_connection_to_slave = 0; 706 stateRealGetPointerWrite()->data_old__lost_connection_to_slave = 0;
707 dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
674 } 708 }
675 else 709 else
676 { 710 {
677 stateRealGetPointerWrite()->data_old__lost_connection_to_slave = 1; 711 stateRealGetPointerWrite()->data_old__lost_connection_to_slave = 1;
678 data_old__lost_connection_to_slave_counter_temp += 1; 712 data_old__lost_connection_to_slave_counter_temp += 1;
755 DataEX_merge_DeviceData_and_store(); 789 DataEX_merge_DeviceData_and_store();
756 DataEX_copy_to_VpmRepetitiveData(); 790 DataEX_copy_to_VpmRepetitiveData();
757 data_old__lost_connection_to_slave_counter_temp = 0; 791 data_old__lost_connection_to_slave_counter_temp = 0;
758 data_old__lost_connection_to_slave_counter_retry = 0; 792 data_old__lost_connection_to_slave_counter_retry = 0;
759 pStateReal->data_old__lost_connection_to_slave = 0; 793 pStateReal->data_old__lost_connection_to_slave = 0;
794 dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
760 } 795 }
761 else 796 else
762 { 797 {
763 pStateReal->data_old__lost_connection_to_slave = 1; 798 pStateReal->data_old__lost_connection_to_slave = 1;
764 data_old__lost_connection_to_slave_counter_temp += 1; 799 data_old__lost_connection_to_slave_counter_temp += 1;
769 else /* RX data OK */ 804 else /* RX data OK */
770 { 805 {
771 data_old__lost_connection_to_slave_counter_temp = 0; 806 data_old__lost_connection_to_slave_counter_temp = 0;
772 data_old__lost_connection_to_slave_counter_retry = 0; 807 data_old__lost_connection_to_slave_counter_retry = 0;
773 pStateReal->data_old__lost_connection_to_slave = 0; 808 pStateReal->data_old__lost_connection_to_slave = 0;
774 } 809 dataOut.header.checkCode[SPI_HEADER_INDEX_RX_STATE] = SPI_RX_STATE_OK;
775 810 }
776 /* update SPI communication tokens */ 811
777 dataOut.header.checkCode[SPI_HEADER_INDEX_SLAVE] = dataIn.header.checkCode[SPI_HEADER_INDEX_SLAVE];
778 dataOut.header.checkCode[SPI_HEADER_INDEX_MASTER] = (dataOut.header.checkCode[SPI_HEADER_INDEX_MASTER] + 1) & 0x7F;
779
780 if(getDeviceDataAfterStartOfMainCPU) 812 if(getDeviceDataAfterStartOfMainCPU)
781 { 813 {
782 getDeviceDataAfterStartOfMainCPU--; 814 getDeviceDataAfterStartOfMainCPU--;
783 if(getDeviceDataAfterStartOfMainCPU == 0) 815 if(getDeviceDataAfterStartOfMainCPU == 0)
784 { 816 {