comparison Small_CPU/Src/spi.c @ 89:ff7775cc34c4 kittz

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