Mercurial > public > ostc4
comparison Small_CPU/Src/spi.c @ 136:6ae8ba5683d6 FlipDisplay
Introduces abort of communication in case of a out of sync DMA transfer
author | Ideenmodellierer |
---|---|
date | Tue, 19 Feb 2019 18:20:45 +0100 |
parents | 4b355396557a |
children | 466c8d9c5e43 |
comparison
equal
deleted
inserted
replaced
135:5df4f1a315cb | 136:6ae8ba5683d6 |
---|---|
268 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12); | 268 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12); |
269 } | 269 } |
270 } | 270 } |
271 | 271 |
272 void SPI_synchronize_with_Master(void) { | 272 void SPI_synchronize_with_Master(void) { |
273 // GPIO_InitTypeDef GPIO_InitStruct; | 273 GPIO_InitTypeDef GPIO_InitStruct; |
274 // | 274 // |
275 // __GPIOA_CLK_ENABLE(); | 275 #if 0 |
276 // /**SPI1 GPIO Configuration | 276 __GPIOA_CLK_ENABLE(); |
277 // PA5 ------> SPI1_SCK | 277 /**SPI1 GPIO Configuration |
278 // */ | 278 PA5 ------> SPI1_SCK |
279 // GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; | 279 */ |
280 // GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | 280 GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
281 // GPIO_InitStruct.Pull = GPIO_PULLUP; | 281 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
282 // GPIO_InitStruct.Speed = GPIO_SPEED_LOW; | 282 GPIO_InitStruct.Pull = GPIO_PULLUP; |
283 // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | 283 GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
284 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
284 // | 285 // |
285 // HAL_Delay(10); | 286 HAL_Delay(10); |
286 // while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0); | 287 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0); |
287 // HAL_Delay(10); | 288 HAL_Delay(10); |
288 // while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1); | 289 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1); |
289 // HAL_Delay(20); | 290 HAL_Delay(20); |
291 #endif | |
290 } | 292 } |
291 | 293 |
292 void SPI_Start_single_TxRx_with_Master(void) { | 294 void SPI_Start_single_TxRx_with_Master(void) { |
293 uint8_t * pOutput; | 295 uint8_t * pOutput; |
296 HAL_StatusTypeDef retval; | |
294 | 297 |
295 if (global.dataSendToSlave.getDeviceDataNow) { | 298 if (global.dataSendToSlave.getDeviceDataNow) { |
296 global.dataSendToSlave.getDeviceDataNow = 0; | 299 global.dataSendToSlave.getDeviceDataNow = 0; |
297 pOutput = (uint8_t*) &(global.deviceDataSendToMaster); | 300 pOutput = (uint8_t*) &(global.deviceDataSendToMaster); |
298 } else { | 301 } else { |
299 pOutput = (uint8_t*) &(global.dataSendToMaster); | 302 pOutput = (uint8_t*) &(global.dataSendToMaster); |
300 } | 303 } |
301 | 304 retval = HAL_SPI_TransmitReceive_DMA(&hspi1, pOutput,(uint8_t*) &(global.dataSendToSlave), EXCHANGE_BUFFERSIZE); |
302 if (HAL_SPI_TransmitReceive_DMA(&hspi1, pOutput,(uint8_t*) &(global.dataSendToSlave), EXCHANGE_BUFFERSIZE)!= HAL_OK) { | 305 if ( retval!= HAL_OK) { |
303 // Transfer error in transmission process | |
304 SPI_Error_Handler(); | 306 SPI_Error_Handler(); |
305 } | 307 } |
306 } | 308 } |
307 | 309 |
308 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { | 310 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { |
309 /* restart SPI */ | 311 /* restart SPI */ |
310 if (hspi == &hspi1) { | 312 if (hspi == &hspi1) |
311 | 313 { |
312 global.check_sync_not_running = 0; | 314 global.check_sync_not_running = 0; |
313 /* stop data exchange? */ | 315 /* stop data exchange? */ |
314 if (global.mode == MODE_SHUTDOWN) { | 316 if (global.mode == MODE_SHUTDOWN) { |
315 global.mode = MODE_SLEEP; | 317 global.mode = MODE_SLEEP; |
316 global.dataSendToSlavePending = 0; | 318 global.dataSendToSlavePending = 0; |
324 // GPIO_new_DEBUG_HIGH(); //For debug. | 326 // GPIO_new_DEBUG_HIGH(); //For debug. |
325 global.dataSendToSlaveIsValid = 1; | 327 global.dataSendToSlaveIsValid = 1; |
326 global.dataSendToSlaveIsNotValidCount = 0; | 328 global.dataSendToSlaveIsNotValidCount = 0; |
327 } else { | 329 } else { |
328 // GPIO_new_DEBUG_LOW(); //For debug. | 330 // GPIO_new_DEBUG_LOW(); //For debug. |
329 global.dataSendToSlaveIsValid = 0; | 331 global.dataSendToSlaveIsValid = 0; |
330 global.dataSendToSlaveIsNotValidCount++; | 332 global.dataSendToSlaveIsNotValidCount++; |
331 MX_SPI_DeInit(); | 333 HAL_SPI_Abort_IT(&hspi1); |
332 HAL_Delay(30); | 334 global.dataSendToSlaveIsNotValidCount = 1; |
333 MX_DMA_Init(); | 335 } |
334 MX_SPI1_Init(); | 336 } |
335 } | |
336 global.dataSendToMaster.power_on_reset = 0; | 337 global.dataSendToMaster.power_on_reset = 0; |
337 global.deviceDataSendToMaster.power_on_reset = 0; | 338 global.deviceDataSendToMaster.power_on_reset = 0; |
338 | 339 |
339 //TODO:REMOVE | 340 //TODO:REMOVE |
340 // if ( !global.dataSendToSlaveStopEval ) { | 341 // if ( !global.dataSendToSlaveStopEval ) { |
341 // scheduleSpecial_Evaluate_DataSendToSlave(); | 342 // scheduleSpecial_Evaluate_DataSendToSlave(); |
342 // } | 343 // } |
343 scheduleSpecial_Evaluate_DataSendToSlave(); | 344 scheduleSpecial_Evaluate_DataSendToSlave(); |
344 SPI_Start_single_TxRx_with_Master(); //Send data always. | 345 |
345 } | 346 SPI_Start_single_TxRx_with_Master(); //Send data always. |
346 } | 347 } |
348 | |
349 | |
347 | 350 |
348 static uint8_t SPI_check_header_and_footer_ok(void) { | 351 static uint8_t SPI_check_header_and_footer_ok(void) { |
349 if (global.dataSendToSlave.header.checkCode[0] != 0xBB) | 352 if (global.dataSendToSlave.header.checkCode[0] != 0xBB) |
350 return 0; | 353 return 0; |
351 if (global.dataSendToSlave.header.checkCode[1] != 0x01) | 354 if (global.dataSendToSlave.header.checkCode[1] != 0x01) |