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