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