comparison Small_CPU/Src/spi.c @ 143:466c8d9c5e43 FlipDisplay

Introduced Token for SPI data exchange Set Prescalar back to 128
author Ideenmodellierer
date Thu, 28 Feb 2019 19:38:36 +0100
parents 6ae8ba5683d6
children ee744c7160ce
comparison
equal deleted inserted replaced
142:69f4b8067daa 143:466c8d9c5e43
18 * 18 *
19 ****************************************************************************** 19 ******************************************************************************
20 */ 20 */
21 21
22 /* Includes ------------------------------------------------------------------*/ 22 /* Includes ------------------------------------------------------------------*/
23
24 #include "global_constants.h"
23 #include "spi.h" 25 #include "spi.h"
24 #include "dma.h" 26 #include "dma.h"
27
25 //#include "gpio.h" 28 //#include "gpio.h"
26 29
27 /* USER CODE BEGIN 0 */ 30 /* USER CODE BEGIN 0 */
28 #include "scheduler.h" 31 #include "scheduler.h"
29 32
30 #ifdef DEBUG_GPIO 33 #ifdef DEBUG_GPIO
31 extern void GPIO_new_DEBUG_LOW(void); 34 extern void GPIO_new_DEBUG_LOW(void);
32 extern void GPIO_new_DEBUG_HIGH(void); 35 extern void GPIO_new_DEBUG_HIGH(void);
33 #endif 36 #endif
34 37
38 // SPI header by index used for synchronization check (package sequence counter)
39 #define SPI_HEADER_INDEX_MASTER 1
40 #define SPI_HEADER_INDEX_SLAVE 2
41
35 uint8_t data_error = 0; 42 uint8_t data_error = 0;
36 uint32_t data_error_time = 0; 43 uint32_t data_error_time = 0;
37 44 uint8_t SPIDataRX = 0; /* Flag to signal that SPI RX callback has been triggered */
45
46 extern void HardSyncToSPI(void);
38 static void SPI_Error_Handler(void); 47 static void SPI_Error_Handler(void);
39 48
40 /* USER CODE END 0 */ 49 /* USER CODE END 0 */
41 50
42 static uint8_t SPI_check_header_and_footer_ok(void); 51 static uint8_t SPI_check_header_and_footer_ok(void);
52 static uint8_t DataEX_check_header_and_footer_shifted(void);
43 53
44 SPI_HandleTypeDef hspi1; 54 SPI_HandleTypeDef hspi1;
45 SPI_HandleTypeDef hspi3; 55 SPI_HandleTypeDef hspi3;
46 56
47 DMA_HandleTypeDef hdma_tx; 57 DMA_HandleTypeDef hdma_tx;
125 hspi1.Init.Direction = SPI_DIRECTION_2LINES; 135 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
126 hspi1.Init.DataSize = SPI_DATASIZE_8BIT; 136 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
127 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; 137 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
128 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; 138 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
129 hspi1.Init.NSS = SPI_NSS_HARD_INPUT; //SPI_NSS_SOFT; 139 hspi1.Init.NSS = SPI_NSS_HARD_INPUT; //SPI_NSS_SOFT;
130 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; 140 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
131 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; 141 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
132 hspi1.Init.TIMode = SPI_TIMODE_DISABLED; 142 hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
133 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //_DISABLED; _ENABLED; 143 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //_DISABLED; _ENABLED;
134 hspi1.Init.CRCPolynomial = 7; 144 hspi1.Init.CRCPolynomial = 7;
135 HAL_SPI_Init(&hspi1); 145 HAL_SPI_Init(&hspi1);
309 319
310 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { 320 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
311 /* restart SPI */ 321 /* restart SPI */
312 if (hspi == &hspi1) 322 if (hspi == &hspi1)
313 { 323 {
324 SPIDataRX = 1;
325
314 global.check_sync_not_running = 0; 326 global.check_sync_not_running = 0;
315 /* stop data exchange? */ 327 /* stop data exchange? */
316 if (global.mode == MODE_SHUTDOWN) { 328 if (global.mode == MODE_SHUTDOWN) {
317 global.mode = MODE_SLEEP; 329 global.mode = MODE_SLEEP;
318 global.dataSendToSlavePending = 0; 330 global.dataSendToSlavePending = 0;
319 global.dataSendToSlaveIsValid = 1; 331 global.dataSendToSlaveIsValid = 1;
320 global.dataSendToSlaveIsNotValidCount = 0; 332 global.dataSendToSlaveIsNotValidCount = 0;
321 return; 333 return;
322 } 334 }
323 335 }
336 }
337
338 void SPI_Evaluate_RX_Data()
339 {
340 if ((global.mode != MODE_SHUTDOWN) && ( global.mode != MODE_SLEEP) && (SPIDataRX))
341 {
342 SPIDataRX = 0;
324 /* data consistent? */ 343 /* data consistent? */
325 if (SPI_check_header_and_footer_ok()) { 344 if (SPI_check_header_and_footer_ok()) {
326 // GPIO_new_DEBUG_HIGH(); //For debug. 345 // GPIO_new_DEBUG_HIGH(); //For debug.
327 global.dataSendToSlaveIsValid = 1; 346 global.dataSendToSlaveIsValid = 1;
328 global.dataSendToSlaveIsNotValidCount = 0; 347 global.dataSendToSlaveIsNotValidCount = 0;
348 /* use sequence index from master to indicate correct reception */
349 if(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] > 0x7F)
350 {
351 HAL_SPI_Abort_IT(&hspi1);
352 global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER];
353 global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] = 0;
354 return;
355 }
356 else
357 {
358 global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER];
359 }
360 HardSyncToSPI();
329 } else { 361 } else {
330 // GPIO_new_DEBUG_LOW(); //For debug. 362 // GPIO_new_DEBUG_LOW(); //For debug.
331 global.dataSendToSlaveIsValid = 0; 363 global.dataSendToSlaveIsValid = 0;
332 global.dataSendToSlaveIsNotValidCount++; 364 global.dataSendToSlaveIsNotValidCount++;
333 HAL_SPI_Abort_IT(&hspi1); 365 if(DataEX_check_header_and_footer_shifted())
334 global.dataSendToSlaveIsNotValidCount = 1; 366 {
367 if (global.dataSendToSlaveIsNotValidCount == 1)
368 {
369 HAL_SPI_Abort_IT(&hspi1); /* reset DMA only once */
370 }
371 }
335 } 372 }
336 } 373
337 global.dataSendToMaster.power_on_reset = 0; 374 global.dataSendToMaster.power_on_reset = 0;
338 global.deviceDataSendToMaster.power_on_reset = 0; 375 global.deviceDataSendToMaster.power_on_reset = 0;
339 376
340 //TODO:REMOVE 377 //TODO:REMOVE
341 // if ( !global.dataSendToSlaveStopEval ) { 378 // if ( !global.dataSendToSlaveStopEval ) {
342 // scheduleSpecial_Evaluate_DataSendToSlave(); 379 // scheduleSpecial_Evaluate_DataSendToSlave();
343 // } 380 // }
344 scheduleSpecial_Evaluate_DataSendToSlave(); 381 scheduleSpecial_Evaluate_DataSendToSlave();
345 382
346 SPI_Start_single_TxRx_with_Master(); //Send data always. 383 SPI_Start_single_TxRx_with_Master(); //Send data always.
347 } 384 }
348 385 }
349
350 386
351 static uint8_t SPI_check_header_and_footer_ok(void) { 387 static uint8_t SPI_check_header_and_footer_ok(void) {
352 if (global.dataSendToSlave.header.checkCode[0] != 0xBB) 388 if (global.dataSendToSlave.header.checkCode[0] != 0xBB)
353 return 0; 389 return 0;
390 #if USE_OLD_HEADER_FORMAT
354 if (global.dataSendToSlave.header.checkCode[1] != 0x01) 391 if (global.dataSendToSlave.header.checkCode[1] != 0x01)
355 return 0; 392 return 0;
356 if (global.dataSendToSlave.header.checkCode[2] != 0x01) 393 if (global.dataSendToSlave.header.checkCode[2] != 0x01)
357 return 0; 394 return 0;
395 #endif
358 if (global.dataSendToSlave.header.checkCode[3] != 0xBB) 396 if (global.dataSendToSlave.header.checkCode[3] != 0xBB)
359 return 0; 397 return 0;
360 if (global.dataSendToSlave.footer.checkCode[0] != 0xF4) 398 if (global.dataSendToSlave.footer.checkCode[0] != 0xF4)
361 return 0; 399 return 0;
362 if (global.dataSendToSlave.footer.checkCode[1] != 0xF3) 400 if (global.dataSendToSlave.footer.checkCode[1] != 0xF3)
365 return 0; 403 return 0;
366 if (global.dataSendToSlave.footer.checkCode[3] != 0xF1) 404 if (global.dataSendToSlave.footer.checkCode[3] != 0xF1)
367 return 0; 405 return 0;
368 406
369 return 1; 407 return 1;
408 }
409
410
411 /* Check if there is an empty frame providec by RTE (all 0) or even no data provided by RTE (all 0xFF)
412 * If that is not the case the DMA is somehow not in sync
413 */
414 uint8_t DataEX_check_header_and_footer_shifted()
415 {
416 uint8_t ret = 1;
417 if((global.dataSendToSlave.footer.checkCode[0] == 0x00)
418 && (global.dataSendToSlave.footer.checkCode[1] == 0x00)
419 && (global.dataSendToSlave.footer.checkCode[2] == 0x00)
420 && (global.dataSendToSlave.footer.checkCode[3] == 0x00)) { ret = 0; }
421
422 if((global.dataSendToSlave.footer.checkCode[0] == 0xff)
423 && (global.dataSendToSlave.footer.checkCode[1] == 0xff)
424 && (global.dataSendToSlave.footer.checkCode[2] == 0xff)
425 && (global.dataSendToSlave.footer.checkCode[3] == 0xff)) { ret = 0; }
426
427 return ret;
370 } 428 }
371 429
372 static void SPI_Error_Handler(void) { 430 static void SPI_Error_Handler(void) {
373 //The device is locks. Hard to recover. 431 //The device is locks. Hard to recover.
374 // while(1) 432 // while(1)