comparison Small_CPU/Src/spi.c @ 148:ee744c7160ce FlipDisplay

Use SPI TX callback to synchronize to main CPU
author Ideenmodellierer
date Sat, 02 Mar 2019 17:01:06 +0100
parents 466c8d9c5e43
children 9fc06e1e0f66
comparison
equal deleted inserted replaced
147:14e4c83a7559 148:ee744c7160ce
135 hspi1.Init.Direction = SPI_DIRECTION_2LINES; 135 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
136 hspi1.Init.DataSize = SPI_DATASIZE_8BIT; 136 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
137 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; 137 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
138 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; 138 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
139 hspi1.Init.NSS = SPI_NSS_HARD_INPUT; //SPI_NSS_SOFT; 139 hspi1.Init.NSS = SPI_NSS_HARD_INPUT; //SPI_NSS_SOFT;
140 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; 140 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
141 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; 141 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
142 hspi1.Init.TIMode = SPI_TIMODE_DISABLED; 142 hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
143 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //_DISABLED; _ENABLED; 143 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //_DISABLED; _ENABLED;
144 hspi1.Init.CRCPolynomial = 7; 144 hspi1.Init.CRCPolynomial = 7;
145 HAL_SPI_Init(&hspi1); 145 HAL_SPI_Init(&hspi1);
152 void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { 152 void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
153 153
154 GPIO_InitTypeDef GPIO_InitStruct; 154 GPIO_InitTypeDef GPIO_InitStruct;
155 155
156 if (hspi->Instance == SPI1) { 156 if (hspi->Instance == SPI1) {
157 SPIDataRX = 0;
157 // Peripheral clock enable 158 // Peripheral clock enable
158 __SPI1_CLK_ENABLE(); 159 __SPI1_CLK_ENABLE();
159 __GPIOA_CLK_ENABLE(); 160 __GPIOA_CLK_ENABLE();
160 //SPI1 GPIO Configuration 161 //SPI1 GPIO Configuration
161 //PA4 ------> SPI1_CS 162 //PA4 ------> SPI1_CS
278 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12); 279 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
279 } 280 }
280 } 281 }
281 282
282 void SPI_synchronize_with_Master(void) { 283 void SPI_synchronize_with_Master(void) {
284 #ifdef USE_OLD_SYNC_METHOD
283 GPIO_InitTypeDef GPIO_InitStruct; 285 GPIO_InitTypeDef GPIO_InitStruct;
284 // 286 //
285 #if 0
286 __GPIOA_CLK_ENABLE(); 287 __GPIOA_CLK_ENABLE();
287 /**SPI1 GPIO Configuration 288 /**SPI1 GPIO Configuration
288 PA5 ------> SPI1_SCK 289 PA5 ------> SPI1_SCK
289 */ 290 */
290 GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; 291 GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
291 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 292 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
292 GPIO_InitStruct.Pull = GPIO_PULLUP; 293 GPIO_InitStruct.Pull = GPIO_PULLUP;
293 GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 294 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
294 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 295 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
295 // 296 //
296 HAL_Delay(10); 297 HAL_Delay(10);
297 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0); 298 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0);
298 HAL_Delay(10); 299 HAL_Delay(10);
299 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1); 300 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1);
300 HAL_Delay(20); 301 HAL_Delay(50);
301 #endif 302 #endif
302 } 303 }
303 304
304 void SPI_Start_single_TxRx_with_Master(void) { 305 void SPI_Start_single_TxRx_with_Master(void) {
305 uint8_t * pOutput; 306 uint8_t * pOutput;
319 320
320 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { 321 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
321 /* restart SPI */ 322 /* restart SPI */
322 if (hspi == &hspi1) 323 if (hspi == &hspi1)
323 { 324 {
325 HardSyncToSPI();
324 SPIDataRX = 1; 326 SPIDataRX = 1;
325 327
326 global.check_sync_not_running = 0; 328 global.check_sync_not_running = 0;
327 /* stop data exchange? */ 329 /* stop data exchange? */
328 if (global.mode == MODE_SHUTDOWN) { 330 if (global.mode == MODE_SHUTDOWN) {
329 global.mode = MODE_SLEEP; 331 global.mode = MODE_SLEEP;
330 global.dataSendToSlavePending = 0; 332 global.dataSendToSlavePending = 0;
331 global.dataSendToSlaveIsValid = 1; 333 global.dataSendToSlaveIsValid = 1;
332 global.dataSendToSlaveIsNotValidCount = 0; 334 global.dataSendToSlaveIsNotValidCount = 0;
333 return;
334 } 335 }
335 } 336 }
336 } 337 }
337 338
338 void SPI_Evaluate_RX_Data() 339 void SPI_Evaluate_RX_Data()
349 if(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] > 0x7F) 350 if(global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] > 0x7F)
350 { 351 {
351 HAL_SPI_Abort_IT(&hspi1); 352 HAL_SPI_Abort_IT(&hspi1);
352 global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER]; 353 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 global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_SLAVE] = 0;
354 return;
355 } 355 }
356 else 356 else
357 { 357 {
358 global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER]; 358 global.dataSendToMaster.header.checkCode[SPI_HEADER_INDEX_SLAVE] = global.dataSendToSlave.header.checkCode[SPI_HEADER_INDEX_MASTER];
359 } 359 }
360 HardSyncToSPI();
361 } else { 360 } else {
362 // GPIO_new_DEBUG_LOW(); //For debug. 361 // GPIO_new_DEBUG_LOW(); //For debug.
363 global.dataSendToSlaveIsValid = 0; 362 global.dataSendToSlaveIsValid = 0;
364 global.dataSendToSlaveIsNotValidCount++; 363 global.dataSendToSlaveIsNotValidCount++;
365 if(DataEX_check_header_and_footer_shifted()) 364 if(DataEX_check_header_and_footer_shifted())
385 } 384 }
386 385
387 static uint8_t SPI_check_header_and_footer_ok(void) { 386 static uint8_t SPI_check_header_and_footer_ok(void) {
388 if (global.dataSendToSlave.header.checkCode[0] != 0xBB) 387 if (global.dataSendToSlave.header.checkCode[0] != 0xBB)
389 return 0; 388 return 0;
390 #if USE_OLD_HEADER_FORMAT 389 #ifdef USE_OLD_HEADER_FORMAT
391 if (global.dataSendToSlave.header.checkCode[1] != 0x01) 390 if (global.dataSendToSlave.header.checkCode[1] != 0x01)
392 return 0; 391 return 0;
393 if (global.dataSendToSlave.header.checkCode[2] != 0x01) 392 if (global.dataSendToSlave.header.checkCode[2] != 0x01)
394 return 0; 393 return 0;
395 #endif 394 #endif