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