38
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file spi.c
|
|
4 * @author heinrichs weikamp gmbh
|
|
5 * @version V0.0.1
|
|
6 * @date 16-Sept-2014
|
|
7 * @brief Source code for spi control
|
|
8 *
|
|
9 @verbatim
|
|
10 ==============================================================================
|
|
11 ##### How to use #####
|
|
12 ==============================================================================
|
|
13 @endverbatim
|
|
14 ******************************************************************************
|
|
15 * @attention
|
|
16 *
|
|
17 * <h2><center>© COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
|
|
18 *
|
|
19 ******************************************************************************
|
|
20 */
|
|
21
|
|
22 /* Includes ------------------------------------------------------------------*/
|
|
23 #include "spi.h"
|
|
24 //#include "gpio.h"
|
|
25
|
|
26 /* USER CODE BEGIN 0 */
|
|
27 #include "scheduler.h"
|
|
28
|
|
29 extern void GPIO_new_DEBUG_LOW(void);
|
|
30 extern void GPIO_new_DEBUG_HIGH(void);
|
|
31
|
|
32
|
|
33 uint8_t data_error = 0;
|
|
34 uint32_t data_error_time = 0;
|
|
35
|
|
36 static void SPI_Error_Handler(void);
|
|
37
|
|
38 /* USER CODE END 0 */
|
|
39
|
|
40 static uint8_t SPI_check_header_and_footer_ok(void);
|
|
41
|
|
42 SPI_HandleTypeDef hspi1;
|
|
43 SPI_HandleTypeDef hspi3;
|
|
44
|
|
45 DMA_HandleTypeDef hdma_tx;
|
|
46 DMA_HandleTypeDef hdma_rx;
|
|
47
|
|
48 // SPI3 init function
|
|
49 void MX_SPI3_Init(void)
|
|
50 {
|
|
51 hspi3.Instance = SPI3;
|
|
52 hspi3.Init.Mode = SPI_MODE_MASTER;
|
|
53 hspi3.Init.Direction = SPI_DIRECTION_2LINES;
|
|
54 hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
55 hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
|
56 hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
|
|
57 hspi3.Init.NSS = SPI_NSS_SOFT;
|
|
58 hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
|
|
59 hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
60 hspi3.Init.TIMode = SPI_TIMODE_DISABLED;
|
|
61 hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
|
|
62 hspi3.Init.CRCPolynomial = 7;
|
|
63 HAL_SPI_Init(&hspi3);
|
|
64 }
|
|
65
|
|
66 void MX_SPI3_DeInit(void)
|
|
67 {
|
|
68 HAL_SPI_DeInit(&hspi3);
|
|
69 }
|
|
70
|
|
71 uint8_t SPI3_ButtonAdjust(uint8_t *arrayInput, uint8_t *arrayOutput)
|
|
72 {
|
|
73 HAL_StatusTypeDef status;
|
|
74 uint8_t answer[10];
|
|
75 uint8_t rework[10];
|
|
76
|
|
77 rework[0] = 0xFF;
|
|
78 for(int i = 0; i < 3; i++)
|
|
79 {
|
|
80 // limiter
|
|
81 if(arrayInput[i] == 0xFF)
|
|
82 arrayInput[i] = 0xFE;
|
|
83 if(arrayInput[i] >= 15)
|
|
84 {
|
|
85 // copy - auslöse-schwelle
|
|
86 rework[i+1] = arrayInput[i];
|
|
87 // wieder-scharf-schalte-schwelle
|
|
88 rework[i+3+1] = arrayInput[i] - 10;
|
|
89 }
|
|
90 else
|
|
91 if(arrayInput[i] >= 10)
|
|
92 {
|
|
93 // copy - auslöse-schwelle
|
|
94 rework[i+1] = arrayInput[i];
|
|
95 // wieder-scharf-schalte-schwelle
|
|
96 rework[i+3+1] = arrayInput[i] - 5;
|
|
97 }
|
|
98 else
|
|
99 {
|
|
100 // copy - auslöse-schwelle
|
|
101 rework[i+1] = 7;
|
|
102 // wieder-scharf-schalte-schwelle
|
|
103 rework[i+3+1] = 6;
|
|
104 }
|
|
105 }
|
|
106
|
|
107 status = HAL_OK; /* = 0 */
|
|
108 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_SET);
|
|
109 for(int i=0;i<7;i++)
|
|
110 {
|
63
|
111 HAL_Delay(10);
|
38
|
112 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_RESET);
|
63
|
113 HAL_Delay(10);
|
38
|
114 status += HAL_SPI_TransmitReceive(&hspi3, &rework[i], &answer[i], 1,20);
|
63
|
115 HAL_Delay(10);
|
38
|
116 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_SET);
|
|
117 }
|
|
118
|
|
119 if(status == HAL_OK)
|
|
120 {
|
|
121 for(int i = 0; i < 3; i++)
|
|
122 {
|
|
123 arrayOutput[i] = answer[i+2]; // first not, return of 0xFF not
|
|
124 }
|
|
125 return 1;
|
|
126 }
|
|
127 else
|
|
128
|
|
129 return 0;
|
|
130 }
|
|
131
|
|
132
|
|
133 // SPI5 init function
|
|
134 void MX_SPI1_Init(void)
|
|
135 {
|
|
136 hspi1.Instance = SPI1;
|
|
137 hspi1.Init.Mode = SPI_MODE_SLAVE;
|
|
138 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
139 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
140 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
|
141 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
|
142 hspi1.Init.NSS = SPI_NSS_HARD_INPUT;//SPI_NSS_SOFT;
|
|
143 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
|
|
144 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
145 hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
|
|
146 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;//_DISABLED; _ENABLED;
|
|
147 hspi1.Init.CRCPolynomial = 7;
|
|
148 HAL_SPI_Init(&hspi1);
|
|
149 }
|
|
150
|
|
151 void MX_SPI_DeInit(void)
|
|
152 {
|
|
153 HAL_SPI_DeInit(&hspi1);
|
|
154 }
|
|
155
|
|
156
|
|
157 void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
|
|
158 {
|
|
159
|
|
160 GPIO_InitTypeDef GPIO_InitStruct;
|
|
161
|
|
162 if(hspi->Instance==SPI1)
|
|
163 {
|
|
164 // Peripheral clock enable
|
|
165 __SPI1_CLK_ENABLE();
|
|
166 __GPIOA_CLK_ENABLE();
|
|
167 //SPI1 GPIO Configuration
|
|
168 //PA4 ------> SPI1_CS
|
|
169 //PA5 ------> SPI1_SCK
|
|
170 //PA6 ------> SPI1_MISO
|
|
171 //PA7 ------> SPI1_MOSI
|
|
172
|
|
173 GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
|
|
174 // GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
|
|
175 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
176 GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
177 GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
|
|
178 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
|
|
179 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
180
|
|
181 //##-3- Configure the DMA streams ##########################################
|
|
182 // Configure the DMA handler for Transmission process
|
|
183 hdma_tx.Instance = DMA2_Stream3;
|
|
184 hdma_tx.Init.Channel = DMA_CHANNEL_3;
|
|
185 hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
186 hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
187 hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
188 hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
189 hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
190 hdma_tx.Init.Mode = DMA_NORMAL;
|
|
191 hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
|
|
192 hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
193 hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
|
194 hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
|
|
195 hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
|
|
196
|
|
197 HAL_DMA_Init(&hdma_tx);
|
|
198
|
|
199 // Associate the initialized DMA handle to the the SPI handle
|
|
200 __HAL_LINKDMA(hspi, hdmatx, hdma_tx);
|
|
201
|
|
202 // Configure the DMA handler for Transmission process
|
|
203 hdma_rx.Instance = DMA2_Stream0;
|
|
204 hdma_rx.Init.Channel = DMA_CHANNEL_3;
|
|
205 hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
206 hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
207 hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
208 hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
209 hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
210 hdma_rx.Init.Mode = DMA_NORMAL;
|
|
211 hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
|
|
212 hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
213 hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
|
214 hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
|
|
215 hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
|
|
216
|
|
217 HAL_DMA_Init(&hdma_rx);
|
|
218
|
|
219 // Associate the initialized DMA handle to the the SPI handle
|
|
220 __HAL_LINKDMA(hspi, hdmarx, hdma_rx);
|
|
221
|
|
222 //##-4- Configure the NVIC for DMA #########################################
|
|
223 //NVIC configuration for DMA transfer complete interrupt (SPI3_RX)
|
|
224 HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 1, 0);
|
|
225 HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
|
|
226
|
|
227 // NVIC configuration for DMA transfer complete interrupt (SPI1_TX)
|
|
228 HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 1, 1);
|
|
229 HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
|
|
230 }
|
|
231 else
|
|
232 if(hspi->Instance==SPI3)
|
|
233 {
|
|
234 __GPIOC_CLK_ENABLE();
|
|
235 __SPI3_CLK_ENABLE();
|
|
236
|
|
237 //SPI1 GPIO Configuration
|
|
238 //PC10 ------> SPI3_SCK
|
|
239 //PC11 ------> SPI3_MISO
|
|
240 //PC12 ------> SPI3_MOSI
|
|
241 //PA15 ------> SPI3_NSS (official)
|
|
242 //PC9 ------> SPI3_NSS (hw)
|
|
243
|
|
244 GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
|
|
245 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
246 GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
247 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
|
|
248 GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
|
|
249 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
250
|
|
251 GPIO_InitStruct.Pin = GPIO_PIN_9;
|
|
252 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
253 GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
254 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
|
|
255 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
256
|
|
257 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_SET);
|
|
258 }
|
|
259 }
|
|
260
|
|
261 void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi){
|
|
262 if(hspi->Instance==SPI1)
|
|
263 {
|
|
264 __SPI1_FORCE_RESET();
|
|
265 __SPI1_RELEASE_RESET();
|
|
266
|
|
267 //SPI1 GPIO Configuration
|
|
268 //PA5 ------> SPI1_SCK
|
|
269 //PA6 ------> SPI1_MISO
|
|
270 //PA7 ------> SPI1_MOSI
|
|
271
|
|
272 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
|
|
273
|
|
274 HAL_DMA_DeInit(&hdma_tx);
|
|
275 HAL_DMA_DeInit(&hdma_rx);
|
|
276
|
|
277 HAL_NVIC_DisableIRQ(DMA2_Stream3_IRQn);
|
|
278 HAL_NVIC_DisableIRQ(DMA2_Stream0_IRQn);
|
|
279 }
|
|
280 else
|
|
281 if(hspi->Instance==SPI3)
|
|
282 {
|
|
283 __SPI3_FORCE_RESET();
|
|
284 __SPI3_RELEASE_RESET();
|
|
285
|
|
286 //SPI1 GPIO Configuration
|
|
287 //PC10 ------> SPI3_SCK
|
|
288 //PC11 ------> SPI3_MISO
|
|
289 //PC12 ------> SPI3_MOSI
|
|
290 //PA15 ------> SPI3_NSS (official)
|
|
291 //PC9 ------> SPI3_NSS (hw)
|
|
292 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12);
|
|
293 }
|
|
294 }
|
|
295
|
|
296 void SPI_synchronize_with_Master(void)
|
|
297 {
|
|
298 GPIO_InitTypeDef GPIO_InitStruct;
|
|
299
|
|
300 __GPIOA_CLK_ENABLE();
|
|
301 /**SPI1 GPIO Configuration
|
|
302 PA5 ------> SPI1_SCK
|
|
303 */
|
|
304 GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
|
|
305 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
306 GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
307 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
|
|
308 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
309
|
|
310 HAL_Delay(10);
|
|
311 while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0);
|
|
312 HAL_Delay(10);
|
|
313 while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1);
|
|
314 HAL_Delay(50);
|
|
315 }
|
|
316
|
|
317
|
|
318 void SPI_Start_single_TxRx_with_Master(void)
|
|
319 {
|
|
320 uint8_t * pOutput;
|
|
321
|
|
322 if(global.dataSendToSlave.getDeviceDataNow)
|
|
323 {
|
|
324 global.dataSendToSlave.getDeviceDataNow = 0;
|
|
325 pOutput = (uint8_t*)&(global.deviceDataSendToMaster);
|
|
326 }
|
|
327 else
|
|
328 {
|
|
329 pOutput = (uint8_t*)&(global.dataSendToMaster);
|
|
330 }
|
|
331
|
|
332 if(HAL_SPI_TransmitReceive_DMA(&hspi1,pOutput, (uint8_t*)&(global.dataSendToSlave), EXCHANGE_BUFFERSIZE+1) != HAL_OK)
|
|
333 {
|
|
334 // Transfer error in transmission process
|
|
335 SPI_Error_Handler();
|
|
336 }
|
|
337 }
|
|
338
|
|
339
|
|
340 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
|
|
341 {
|
|
342 global.check_sync_not_running = 0;
|
|
343 /* stop data exchange? */
|
|
344 if(global.mode == MODE_SHUTDOWN)
|
|
345 {
|
|
346 global.mode = MODE_SLEEP;
|
|
347 global.dataSendToSlavePending = 0;
|
|
348 global.dataSendToSlaveIsValid = 1;
|
|
349 global.dataSendToSlaveIsNotValidCount = 0;
|
|
350 return;
|
|
351 }
|
|
352
|
|
353 /* data consistent? */
|
|
354 if(SPI_check_header_and_footer_ok())
|
|
355 {
|
|
356 GPIO_new_DEBUG_HIGH();
|
|
357 global.dataSendToSlaveIsValid = 1;
|
|
358 global.dataSendToSlaveIsNotValidCount = 0;
|
|
359 }
|
|
360 else
|
|
361 {
|
|
362 GPIO_new_DEBUG_LOW();
|
|
363 global.dataSendToSlaveIsValid = 0;
|
|
364 global.dataSendToSlaveIsNotValidCount++;
|
|
365 }
|
|
366 global.dataSendToMaster.power_on_reset = 0;
|
|
367 global.deviceDataSendToMaster.power_on_reset = 0;
|
|
368 /* no i2c or other time critical threads? */
|
|
369 if(global.dataSendToSlaveIsValid)
|
|
370 {
|
|
371 if(!global.dataSendToSlaveStopEval)
|
|
372 {
|
|
373 scheduleSpecial_Evaluate_DataSendToSlave();
|
|
374 }
|
|
375 else
|
|
376 {
|
|
377 global.dataSendToSlavePending = 1;
|
|
378 }
|
|
379 }
|
|
380 else
|
|
381 {
|
|
382 global.dataSendToSlavePending = 0;
|
|
383 }
|
|
384
|
|
385 /* restart SPI */
|
|
386 if(hspi == &hspi1)
|
|
387 {
|
|
388 if(global.dataSendToSlaveIsValid)
|
|
389 SPI_Start_single_TxRx_with_Master();
|
|
390 }
|
|
391 }
|
|
392
|
|
393
|
|
394 static uint8_t SPI_check_header_and_footer_ok(void)
|
|
395 {
|
|
396 if(global.dataSendToSlave.header.checkCode[0] != 0xBB)
|
|
397 return 0;
|
|
398 if(global.dataSendToSlave.header.checkCode[1] != 0x01)
|
|
399 return 0;
|
|
400 if(global.dataSendToSlave.header.checkCode[2] != 0x01)
|
|
401 return 0;
|
|
402 if(global.dataSendToSlave.header.checkCode[3] != 0xBB)
|
|
403 return 0;
|
|
404 if(global.dataSendToSlave.footer.checkCode[0] != 0xF4)
|
|
405 return 0;
|
|
406 if(global.dataSendToSlave.footer.checkCode[1] != 0xF3)
|
|
407 return 0;
|
|
408 if(global.dataSendToSlave.footer.checkCode[2] != 0xF2)
|
|
409 return 0;
|
|
410 if(global.dataSendToSlave.footer.checkCode[3] != 0xF1)
|
|
411 return 0;
|
|
412
|
|
413 return 1;
|
|
414 }
|
|
415
|
|
416 static void SPI_Error_Handler(void)
|
|
417 {
|
|
418 while(1)
|
|
419 {
|
|
420 }
|
|
421 }
|
|
422
|
|
423 /**
|
|
424 * @}
|
|
425 */
|
|
426
|
|
427 /**
|
|
428 * @}
|
|
429 */
|
|
430
|
|
431 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|