Mercurial > public > ostc4
annotate Small_CPU/Src/spi.c @ 85:923c4566a2a1 kittz
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
author | Dmitry Romanov <kitt@bk.ru> |
---|---|
date | Wed, 21 Nov 2018 12:49:54 +0300 |
parents | e6abbef57475 |
children | 3db7389d49cc |
rev | line source |
---|---|
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 { | |
82 | 85 // copy - ausl�se-schwelle |
38 | 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 { | |
82 | 93 // copy - ausl�se-schwelle |
38 | 94 rework[i+1] = arrayInput[i]; |
95 // wieder-scharf-schalte-schwelle | |
96 rework[i+3+1] = arrayInput[i] - 5; | |
97 } | |
98 else | |
99 { | |
82 | 100 // copy - ausl�se-schwelle |
38 | 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; | |
85
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
143 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; |
38 | 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; | |
85
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
191 hdma_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH; |
38 | 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 { | |
82 | 342 /* restart SPI */ |
343 if(hspi == &hspi1) | |
344 { | |
345 | |
38 | 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 { | |
82 | 360 // GPIO_new_DEBUG_HIGH(); //For debug. |
38 | 361 global.dataSendToSlaveIsValid = 1; |
362 global.dataSendToSlaveIsNotValidCount = 0; | |
363 } | |
364 else | |
365 { | |
82 | 366 // GPIO_new_DEBUG_LOW(); //For debug. |
38 | 367 global.dataSendToSlaveIsValid = 0; |
368 global.dataSendToSlaveIsNotValidCount++; | |
369 } | |
370 global.dataSendToMaster.power_on_reset = 0; | |
371 global.deviceDataSendToMaster.power_on_reset = 0; | |
85
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
372 // if(global.dataUpdateIsNeeded) |
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
373 // { |
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
374 scheduleSpecial_Evaluate_DataSendToSlave(); |
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
375 // global.dataUpdateIsNeeded=0; |
923c4566a2a1
increased interCPU baudrate, cpu2: i2c in SPI1 IRQ
Dmitry Romanov <kitt@bk.ru>
parents:
84
diff
changeset
|
376 // } |
82 | 377 SPI_Start_single_TxRx_with_Master(); |
378 } | |
38 | 379 } |
380 | |
381 | |
382 static uint8_t SPI_check_header_and_footer_ok(void) | |
383 { | |
384 if(global.dataSendToSlave.header.checkCode[0] != 0xBB) | |
385 return 0; | |
386 if(global.dataSendToSlave.header.checkCode[1] != 0x01) | |
387 return 0; | |
388 if(global.dataSendToSlave.header.checkCode[2] != 0x01) | |
389 return 0; | |
390 if(global.dataSendToSlave.header.checkCode[3] != 0xBB) | |
391 return 0; | |
392 if(global.dataSendToSlave.footer.checkCode[0] != 0xF4) | |
393 return 0; | |
394 if(global.dataSendToSlave.footer.checkCode[1] != 0xF3) | |
395 return 0; | |
396 if(global.dataSendToSlave.footer.checkCode[2] != 0xF2) | |
397 return 0; | |
398 if(global.dataSendToSlave.footer.checkCode[3] != 0xF1) | |
399 return 0; | |
400 | |
401 return 1; | |
402 } | |
403 | |
404 static void SPI_Error_Handler(void) | |
405 { | |
82 | 406 //The device is locks. Hard to recover. |
407 // while(1) | |
408 // { | |
409 // } | |
38 | 410 } |
411 | |
412 /** | |
413 * @} | |
414 */ | |
415 | |
416 /** | |
417 * @} | |
418 */ | |
419 | |
420 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |