Rev 10 | Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * File Name : main.c |
||
4 | * Description : Main program body |
||
5 | ****************************************************************************** |
||
6 | * |
||
7 | * COPYRIGHT(c) 2016 STMicroelectronics |
||
8 | * |
||
9 | * Redistribution and use in source and binary forms, with or without modification, |
||
10 | * are permitted provided that the following conditions are met: |
||
11 | * 1. Redistributions of source code must retain the above copyright notice, |
||
12 | * this list of conditions and the following disclaimer. |
||
13 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
14 | * this list of conditions and the following disclaimer in the documentation |
||
15 | * and/or other materials provided with the distribution. |
||
16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
||
17 | * may be used to endorse or promote products derived from this software |
||
18 | * without specific prior written permission. |
||
19 | * |
||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
30 | * |
||
31 | ****************************************************************************** |
||
32 | */ |
||
33 | /* Includes ------------------------------------------------------------------*/ |
||
34 | #include "stm32l1xx_hal.h" |
||
35 | |||
36 | /* USER CODE BEGIN Includes */ |
||
7 | mjames | 37 | #include "serial.h" |
9 | mjames | 38 | #include "plx.h" |
39 | #include "misc.h" |
||
2 | mjames | 40 | /* USER CODE END Includes */ |
41 | |||
42 | /* Private variables ---------------------------------------------------------*/ |
||
43 | ADC_HandleTypeDef hadc; |
||
6 | mjames | 44 | DMA_HandleTypeDef hdma_adc; |
2 | mjames | 45 | |
46 | SPI_HandleTypeDef hspi1; |
||
47 | |||
48 | TIM_HandleTypeDef htim2; |
||
49 | TIM_HandleTypeDef htim6; |
||
50 | |||
51 | UART_HandleTypeDef huart1; |
||
6 | mjames | 52 | UART_HandleTypeDef huart2; |
2 | mjames | 53 | |
54 | /* USER CODE BEGIN PV */ |
||
55 | /* Private variables ---------------------------------------------------------*/ |
||
56 | |||
8 | mjames | 57 | |
9 | mjames | 58 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
59 | // freq = 5000/60 * 2 = 166Hz. Because the breaker might bounce , we accept the first pulse longer than 1/300 of a second as being a proper closure . |
||
60 | // the TIM2 counter counts in 10uS increments, |
||
61 | |||
62 | #define BREAKER_MIN (RPM_COUNT_RATE/300) |
||
63 | |||
64 | |||
65 | volatile char TimerFlag = 0; |
||
66 | |||
67 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
||
68 | volatile char NoSerialIn = 0; |
||
69 | |||
8 | mjames | 70 | // storage for ADC |
71 | long ADC_samples[6]; |
||
72 | |||
9 | mjames | 73 | // Rev counter processing from original RevCounter Project |
74 | unsigned int RPM_Diff = 0; |
||
75 | unsigned int RPM_Count_Latch = 0; |
||
76 | // accumulators |
||
77 | unsigned int RPM_Pulsecount = 0; |
||
78 | unsigned int RPM_FilteredWidth = 0; |
||
79 | |||
80 | unsigned int Coded_RPM = 0; |
||
81 | unsigned int Coded_CHT = 0; |
||
82 | |||
2 | mjames | 83 | /* USER CODE END PV */ |
84 | |||
85 | /* Private function prototypes -----------------------------------------------*/ |
||
86 | void SystemClock_Config(void); |
||
87 | void Error_Handler(void); |
||
88 | static void MX_GPIO_Init(void); |
||
6 | mjames | 89 | static void MX_DMA_Init(void); |
2 | mjames | 90 | static void MX_ADC_Init(void); |
91 | static void MX_SPI1_Init(void); |
||
92 | static void MX_TIM2_Init(void); |
||
93 | static void MX_TIM6_Init(void); |
||
94 | static void MX_USART1_UART_Init(void); |
||
6 | mjames | 95 | static void MX_USART2_UART_Init(void); |
2 | mjames | 96 | |
97 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); |
||
98 | |||
99 | |||
100 | /* USER CODE BEGIN PFP */ |
||
101 | /* Private function prototypes -----------------------------------------------*/ |
||
102 | |||
9 | mjames | 103 | /* USER CODE END PFP */ |
7 | mjames | 104 | |
9 | mjames | 105 | /* USER CODE BEGIN 0 */ |
7 | mjames | 106 | |
9 | mjames | 107 | void ConfigureDMA(void) { |
108 | hdma_adc.Instance = DMA1_Channel1; |
||
109 | hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY; |
||
110 | hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE; |
||
111 | hdma_adc.Init.MemInc = DMA_MINC_ENABLE; |
||
112 | hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; |
||
113 | hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; |
||
114 | hdma_adc.Init.Mode = DMA_CIRCULAR; // make the DMA loop automatically |
||
115 | hdma_adc.Init.Priority = DMA_PRIORITY_LOW; |
||
116 | HAL_DMA_Init(&hdma_adc); |
||
117 | __HAL_LINKDMA(&hadc, DMA_Handle, hdma_adc); |
||
118 | |||
7 | mjames | 119 | } |
120 | |||
9 | mjames | 121 | void plx_sendword(int x) { |
122 | PutCharSerial(&uc1, ((x) >> 6) & 0x3F); |
||
123 | PutCharSerial(&uc1, (x) & 0x3F); |
||
124 | } |
||
2 | mjames | 125 | |
9 | mjames | 126 | void ProcessRPM(int instance) { |
127 | // compute the timer values |
||
128 | // snapshot timers |
||
129 | unsigned long RPM_Pulsewidth; |
||
130 | unsigned long RPM_Count_Val; |
||
131 | __disable_irq(); // copy the counter value |
||
132 | RPM_Count_Val = RPM_Count; |
||
133 | __enable_irq(); |
||
134 | // do calculations |
||
135 | // if there is only one entry, cannot get difference |
||
136 | if (RPM_Count_Latch != RPM_Count_Val) { |
||
137 | while (1) { |
||
138 | unsigned int base_time; |
||
139 | unsigned int new_time; |
||
140 | // if we are at N-1, stop. |
||
141 | unsigned int next_count = RPM_Count_Latch + 1; |
||
142 | if (next_count == RPM_SAMPLES) { |
||
143 | next_count = 0; |
||
144 | } |
||
145 | if (next_count == RPM_Count_Val) { |
||
146 | break; |
||
147 | } |
||
148 | base_time = RPM_Time[RPM_Count_Latch]; |
||
149 | new_time = RPM_Time[next_count]; |
||
150 | RPM_Count_Latch = next_count; |
||
151 | if (new_time > base_time) { |
||
152 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
||
153 | } else { |
||
154 | RPM_Pulsewidth = new_time + (~base_time) + 1; // deal with wrapping |
||
155 | } |
||
2 | mjames | 156 | |
9 | mjames | 157 | RPM_Diff += RPM_Pulsewidth; |
158 | // need to check if this is a long pulse. If it is, keep the answer |
||
159 | if (RPM_Pulsewidth > BREAKER_MIN) { |
||
160 | RPM_Pulsecount++; // count one pulse |
||
161 | RPM_FilteredWidth += RPM_Diff; // add its width to the accumulator |
||
162 | RPM_Diff = 0; // reset accumulator of all the narrow widths |
||
163 | } |
||
164 | } |
||
165 | |||
166 | } |
||
167 | |||
168 | if (RPM_Pulsecount > 0) { |
||
169 | |||
170 | // now have time for N pulses in clocks |
||
171 | // need to scale by 19.55: one unit is 19.55 RPM |
||
172 | // 1Hz is 60 RPM |
||
173 | Coded_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE) |
||
174 | / (RPM_FilteredWidth) + 0.5; |
||
175 | #if !defined MY_DEBUG |
||
176 | // reset here unless we want to debug |
||
177 | RPM_Pulsecount = 0; |
||
178 | RPM_FilteredWidth = 0; |
||
179 | #endif |
||
180 | } |
||
181 | |||
182 | // send the current RPM calculation |
||
183 | plx_sendword(PLX_RPM); |
||
184 | PutCharSerial(&uc1, instance); |
||
185 | plx_sendword(Coded_RPM); |
||
186 | } |
||
187 | |||
11 | mjames | 188 | uint8_t CHT_Timer = 0; |
9 | mjames | 189 | |
190 | // this uses a MAX6675 which is a simple 16 bit read |
||
191 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
||
11 | mjames | 192 | // must wait > 0.22 seconds between conversion attempts as this is the measurement time |
193 | // |
||
9 | mjames | 194 | void ProcessCHT(int instance) |
195 | { |
||
196 | uint8_t buffer[2]; |
||
11 | mjames | 197 | CHT_Timer++; |
198 | if(CHT_Timer >= 3) // every 300 milliseconds |
||
199 | |||
200 | { |
||
201 | CHT_Timer=0; |
||
202 | |||
9 | mjames | 203 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_RESET); |
204 | |||
205 | |||
11 | mjames | 206 | HAL_Delay(1); |
207 | |||
9 | mjames | 208 | HAL_SPI_Receive(&hspi1, buffer, 2, 2); |
209 | |||
210 | |||
211 | |||
212 | uint16_t obs = (buffer[0]<<8)| buffer[1]; |
||
213 | |||
214 | uint8_t good = (obs & 4)==0; |
||
215 | if(good) |
||
216 | { |
||
10 | mjames | 217 | Coded_CHT = obs>>5; |
9 | mjames | 218 | } |
10 | mjames | 219 | else |
220 | { |
||
221 | Coded_CHT= 1000; // signal fail |
||
222 | } |
||
11 | mjames | 223 | } |
224 | |||
9 | mjames | 225 | plx_sendword(PLX_EGT); |
226 | PutCharSerial(&uc1, instance); |
||
227 | plx_sendword(Coded_CHT); |
||
10 | mjames | 228 | PutCharSerial(&uc2,Coded_CHT + 32); |
229 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET); |
||
9 | mjames | 230 | |
231 | } |
||
232 | |||
2 | mjames | 233 | /* USER CODE END 0 */ |
234 | |||
235 | int main(void) |
||
236 | { |
||
237 | |||
238 | /* USER CODE BEGIN 1 */ |
||
239 | |||
240 | /* USER CODE END 1 */ |
||
241 | |||
242 | /* MCU Configuration----------------------------------------------------------*/ |
||
243 | |||
244 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
245 | HAL_Init(); |
||
246 | |||
247 | /* Configure the system clock */ |
||
248 | SystemClock_Config(); |
||
249 | |||
250 | /* Initialize all configured peripherals */ |
||
251 | MX_GPIO_Init(); |
||
6 | mjames | 252 | MX_DMA_Init(); |
2 | mjames | 253 | MX_ADC_Init(); |
254 | MX_SPI1_Init(); |
||
255 | MX_TIM2_Init(); |
||
256 | MX_TIM6_Init(); |
||
257 | MX_USART1_UART_Init(); |
||
6 | mjames | 258 | MX_USART2_UART_Init(); |
2 | mjames | 259 | |
260 | /* USER CODE BEGIN 2 */ |
||
261 | |||
9 | mjames | 262 | __HAL_RCC_SPI1_CLK_ENABLE() |
263 | ; // Temp sensor port |
||
264 | __HAL_RCC_USART1_CLK_ENABLE() |
||
265 | ; // PLX comms port |
||
266 | __HAL_RCC_USART2_CLK_ENABLE() |
||
267 | ; // Debug comms port |
||
7 | mjames | 268 | |
9 | mjames | 269 | __HAL_RCC_ADC1_CLK_ENABLE() |
270 | ; // enable the ADC |
||
7 | mjames | 271 | |
9 | mjames | 272 | __HAL_RCC_TIM6_CLK_ENABLE() |
273 | ; |
||
7 | mjames | 274 | |
9 | mjames | 275 | ConfigureDMA(); |
276 | // HAL_ADC_Start_DMA(&g_AdcHandle, g_ADCBuffer, ADC_BUFFER_LENGTH); |
||
8 | mjames | 277 | |
7 | mjames | 278 | /* setup the USART control blocks */ |
279 | init_usart_ctl(&uc1, huart1.Instance); |
||
280 | init_usart_ctl(&uc2, huart2.Instance); |
||
281 | |||
282 | EnableSerialRxInterrupt(&uc1); |
||
283 | EnableSerialRxInterrupt(&uc2); |
||
284 | |||
9 | mjames | 285 | HAL_TIM_Base_Start_IT(&htim6); |
8 | mjames | 286 | |
9 | mjames | 287 | PutCharSerial(&uc2, 'A'); |
7 | mjames | 288 | |
2 | mjames | 289 | /* USER CODE END 2 */ |
290 | |||
291 | /* Infinite loop */ |
||
292 | /* USER CODE BEGIN WHILE */ |
||
9 | mjames | 293 | while (1) { |
2 | mjames | 294 | /* USER CODE END WHILE */ |
295 | |||
296 | /* USER CODE BEGIN 3 */ |
||
9 | mjames | 297 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
298 | int c; |
||
299 | char send = 0; |
||
2 | mjames | 300 | |
9 | mjames | 301 | // poll the input for a stop bit or timeout |
302 | if(PollSerial(&uc1)) |
||
303 | { |
||
304 | c = GetCharSerial(&uc1); |
||
305 | if (c != PLX_Stop) |
||
306 | { |
||
307 | PutCharSerial(&uc1,c); // echo all but the stop bit |
||
308 | } else { // must be a stop character |
||
309 | send = 1; // start our sending process. |
||
310 | } |
||
311 | } |
||
312 | |||
313 | // sort out auto-sending |
||
314 | if (TimerFlag) |
||
315 | { |
||
10 | mjames | 316 | TimerFlag = 0; |
9 | mjames | 317 | if (NoSerialIn) |
318 | { |
||
319 | PutCharSerial(&uc1,PLX_Start); |
||
320 | send = 1; |
||
321 | } |
||
322 | } |
||
323 | if (send) |
||
324 | { |
||
325 | send = 0; |
||
326 | |||
327 | ProcessRPM(0); |
||
328 | ProcessCHT(0); |
||
329 | |||
330 | PutCharSerial(&uc1,PLX_Stop); |
||
331 | } |
||
332 | |||
333 | } |
||
2 | mjames | 334 | /* USER CODE END 3 */ |
335 | |||
336 | } |
||
337 | |||
338 | /** System Clock Configuration |
||
339 | */ |
||
340 | void SystemClock_Config(void) |
||
341 | { |
||
342 | |||
343 | RCC_OscInitTypeDef RCC_OscInitStruct; |
||
344 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
||
345 | |||
346 | __HAL_RCC_PWR_CLK_ENABLE(); |
||
347 | |||
348 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
||
349 | |||
350 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
||
351 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
352 | RCC_OscInitStruct.HSICalibrationValue = 16; |
||
353 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
354 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
||
355 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; |
||
356 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
||
357 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
||
358 | { |
||
359 | Error_Handler(); |
||
360 | } |
||
361 | |||
362 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
363 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
364 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
||
365 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
366 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
||
367 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
368 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
||
369 | { |
||
370 | Error_Handler(); |
||
371 | } |
||
372 | |||
373 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
||
374 | |||
375 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
||
376 | |||
377 | /* SysTick_IRQn interrupt configuration */ |
||
378 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); |
||
379 | } |
||
380 | |||
381 | /* ADC init function */ |
||
382 | static void MX_ADC_Init(void) |
||
383 | { |
||
384 | |||
385 | ADC_ChannelConfTypeDef sConfig; |
||
386 | |||
387 | /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) |
||
388 | */ |
||
389 | hadc.Instance = ADC1; |
||
390 | hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; |
||
391 | hadc.Init.Resolution = ADC_RESOLUTION_12B; |
||
392 | hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
||
393 | hadc.Init.ScanConvMode = ADC_SCAN_ENABLE; |
||
394 | hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV; |
||
395 | hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE; |
||
396 | hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE; |
||
397 | hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A; |
||
398 | hadc.Init.ContinuousConvMode = DISABLE; |
||
399 | hadc.Init.NbrOfConversion = 6; |
||
400 | hadc.Init.DiscontinuousConvMode = DISABLE; |
||
401 | hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO; |
||
402 | hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; |
||
403 | hadc.Init.DMAContinuousRequests = DISABLE; |
||
404 | if (HAL_ADC_Init(&hadc) != HAL_OK) |
||
405 | { |
||
406 | Error_Handler(); |
||
407 | } |
||
408 | |||
409 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
410 | */ |
||
6 | mjames | 411 | sConfig.Channel = ADC_CHANNEL_10; |
2 | mjames | 412 | sConfig.Rank = 1; |
413 | sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES; |
||
414 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
415 | { |
||
416 | Error_Handler(); |
||
417 | } |
||
418 | |||
419 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
420 | */ |
||
6 | mjames | 421 | sConfig.Channel = ADC_CHANNEL_11; |
2 | mjames | 422 | sConfig.Rank = 2; |
423 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
424 | { |
||
425 | Error_Handler(); |
||
426 | } |
||
427 | |||
428 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
429 | */ |
||
6 | mjames | 430 | sConfig.Channel = ADC_CHANNEL_12; |
2 | mjames | 431 | sConfig.Rank = 3; |
432 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
433 | { |
||
434 | Error_Handler(); |
||
435 | } |
||
436 | |||
437 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
438 | */ |
||
6 | mjames | 439 | sConfig.Channel = ADC_CHANNEL_13; |
2 | mjames | 440 | sConfig.Rank = 4; |
441 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
442 | { |
||
443 | Error_Handler(); |
||
444 | } |
||
445 | |||
446 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
447 | */ |
||
448 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
||
449 | sConfig.Rank = 5; |
||
450 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
451 | { |
||
452 | Error_Handler(); |
||
453 | } |
||
454 | |||
455 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
456 | */ |
||
457 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
||
458 | sConfig.Rank = 6; |
||
459 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
460 | { |
||
461 | Error_Handler(); |
||
462 | } |
||
463 | |||
464 | } |
||
465 | |||
466 | /* SPI1 init function */ |
||
467 | static void MX_SPI1_Init(void) |
||
468 | { |
||
469 | |||
470 | hspi1.Instance = SPI1; |
||
471 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
3 | mjames | 472 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
2 | mjames | 473 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
474 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
475 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
476 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
10 | mjames | 477 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; |
2 | mjames | 478 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
479 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
480 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
481 | hspi1.Init.CRCPolynomial = 10; |
||
482 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
483 | { |
||
484 | Error_Handler(); |
||
485 | } |
||
486 | |||
487 | } |
||
488 | |||
489 | /* TIM2 init function */ |
||
490 | static void MX_TIM2_Init(void) |
||
491 | { |
||
492 | |||
493 | TIM_MasterConfigTypeDef sMasterConfig; |
||
494 | TIM_IC_InitTypeDef sConfigIC; |
||
495 | TIM_OC_InitTypeDef sConfigOC; |
||
496 | |||
497 | htim2.Instance = TIM2; |
||
498 | htim2.Init.Prescaler = 320; |
||
499 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
500 | htim2.Init.Period = 0; |
||
501 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
502 | if (HAL_TIM_IC_Init(&htim2) != HAL_OK) |
||
503 | { |
||
504 | Error_Handler(); |
||
505 | } |
||
506 | |||
507 | if (HAL_TIM_OC_Init(&htim2) != HAL_OK) |
||
508 | { |
||
509 | Error_Handler(); |
||
510 | } |
||
511 | |||
512 | if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) |
||
513 | { |
||
514 | Error_Handler(); |
||
515 | } |
||
516 | |||
517 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
||
518 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
519 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) |
||
520 | { |
||
521 | Error_Handler(); |
||
522 | } |
||
523 | |||
524 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; |
||
525 | sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; |
||
526 | sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; |
||
527 | sConfigIC.ICFilter = 0; |
||
528 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) |
||
529 | { |
||
530 | Error_Handler(); |
||
531 | } |
||
532 | |||
533 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) |
||
534 | { |
||
535 | Error_Handler(); |
||
536 | } |
||
537 | |||
538 | sConfigOC.OCMode = TIM_OCMODE_TIMING; |
||
539 | sConfigOC.Pulse = 0; |
||
540 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
||
541 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
542 | if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) |
||
543 | { |
||
544 | Error_Handler(); |
||
545 | } |
||
546 | |||
547 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
||
548 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) |
||
549 | { |
||
550 | Error_Handler(); |
||
551 | } |
||
552 | |||
553 | HAL_TIM_MspPostInit(&htim2); |
||
554 | |||
555 | } |
||
556 | |||
557 | /* TIM6 init function */ |
||
558 | static void MX_TIM6_Init(void) |
||
559 | { |
||
560 | |||
561 | TIM_MasterConfigTypeDef sMasterConfig; |
||
562 | |||
563 | htim6.Instance = TIM6; |
||
564 | htim6.Init.Prescaler = 3200; |
||
565 | htim6.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
566 | htim6.Init.Period = 1000; |
||
567 | if (HAL_TIM_Base_Init(&htim6) != HAL_OK) |
||
568 | { |
||
569 | Error_Handler(); |
||
570 | } |
||
571 | |||
572 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
573 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
574 | if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) |
||
575 | { |
||
576 | Error_Handler(); |
||
577 | } |
||
578 | |||
579 | } |
||
580 | |||
581 | /* USART1 init function */ |
||
582 | static void MX_USART1_UART_Init(void) |
||
583 | { |
||
584 | |||
585 | huart1.Instance = USART1; |
||
586 | huart1.Init.BaudRate = 19200; |
||
587 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
588 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
589 | huart1.Init.Parity = UART_PARITY_NONE; |
||
590 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
591 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
592 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
593 | if (HAL_UART_Init(&huart1) != HAL_OK) |
||
594 | { |
||
595 | Error_Handler(); |
||
596 | } |
||
597 | |||
598 | } |
||
599 | |||
6 | mjames | 600 | /* USART2 init function */ |
601 | static void MX_USART2_UART_Init(void) |
||
602 | { |
||
603 | |||
604 | huart2.Instance = USART2; |
||
605 | huart2.Init.BaudRate = 115200; |
||
606 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
607 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
608 | huart2.Init.Parity = UART_PARITY_NONE; |
||
609 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
610 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
611 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
612 | if (HAL_UART_Init(&huart2) != HAL_OK) |
||
613 | { |
||
614 | Error_Handler(); |
||
615 | } |
||
616 | |||
617 | } |
||
618 | |||
619 | /** |
||
620 | * Enable DMA controller clock |
||
621 | */ |
||
622 | static void MX_DMA_Init(void) |
||
623 | { |
||
624 | /* DMA controller clock enable */ |
||
625 | __HAL_RCC_DMA1_CLK_ENABLE(); |
||
626 | |||
627 | /* DMA interrupt init */ |
||
628 | /* DMA1_Channel1_IRQn interrupt configuration */ |
||
629 | HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); |
||
630 | HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); |
||
631 | |||
632 | } |
||
633 | |||
2 | mjames | 634 | /** Configure pins as |
635 | * Analog |
||
636 | * Input |
||
637 | * Output |
||
638 | * EVENT_OUT |
||
639 | * EXTI |
||
5 | mjames | 640 | * Free pins are configured automatically as Analog (this feature is enabled through |
641 | * the Code Generation settings) |
||
2 | mjames | 642 | */ |
643 | static void MX_GPIO_Init(void) |
||
644 | { |
||
645 | |||
646 | GPIO_InitTypeDef GPIO_InitStruct; |
||
647 | |||
648 | /* GPIO Ports Clock Enable */ |
||
5 | mjames | 649 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
650 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
||
2 | mjames | 651 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
652 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
5 | mjames | 653 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
2 | mjames | 654 | |
6 | mjames | 655 | /*Configure GPIO pins : PC13 PC14 PC15 PC6 |
5 | mjames | 656 | PC7 PC8 PC9 PC10 |
657 | PC11 PC12 */ |
||
6 | mjames | 658 | GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_6 |
5 | mjames | 659 | |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10 |
660 | |GPIO_PIN_11|GPIO_PIN_12; |
||
661 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
662 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
663 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
||
2 | mjames | 664 | |
5 | mjames | 665 | /*Configure GPIO pins : PH0 PH1 */ |
666 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; |
||
667 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
668 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
669 | HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); |
||
3 | mjames | 670 | |
6 | mjames | 671 | /*Configure GPIO pins : PA0 PA1 PA8 PA11 |
7 | mjames | 672 | PA12 */ |
6 | mjames | 673 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_11 |
7 | mjames | 674 | |GPIO_PIN_12; |
6 | mjames | 675 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
676 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
677 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
||
678 | |||
7 | mjames | 679 | /*Configure GPIO pin : LED_Blink_Pin */ |
680 | GPIO_InitStruct.Pin = LED_Blink_Pin; |
||
2 | mjames | 681 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
682 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
7 | mjames | 683 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
684 | HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct); |
||
2 | mjames | 685 | |
3 | mjames | 686 | /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */ |
687 | GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin; |
||
688 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
689 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
690 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
691 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
||
692 | |||
7 | mjames | 693 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NS_Temp_Pin */ |
694 | GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NS_Temp_Pin; |
||
3 | mjames | 695 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
696 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
697 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
7 | mjames | 698 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
3 | mjames | 699 | |
7 | mjames | 700 | /*Configure GPIO pins : PB2 PB12 PB13 PB14 |
701 | PB15 PB4 PB5 PB6 |
||
702 | PB7 PB8 PB9 */ |
||
703 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14 |
||
704 | |GPIO_PIN_15|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 |
||
705 | |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9; |
||
5 | mjames | 706 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
707 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
708 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
709 | |||
710 | /*Configure GPIO pin : PD2 */ |
||
711 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
||
712 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
713 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
714 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
||
715 | |||
716 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 717 | HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET); |
5 | mjames | 718 | |
719 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 720 | HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET); |
5 | mjames | 721 | |
722 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 723 | HAL_GPIO_WritePin(SPI1CD_GPIO_Port, SPI1CD_Pin, GPIO_PIN_RESET); |
724 | |||
725 | /*Configure GPIO pin Output Level */ |
||
5 | mjames | 726 | HAL_GPIO_WritePin(SPI_RESET_GPIO_Port, SPI_RESET_Pin, GPIO_PIN_RESET); |
727 | |||
7 | mjames | 728 | /*Configure GPIO pin Output Level */ |
729 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET); |
||
730 | |||
2 | mjames | 731 | } |
732 | |||
733 | /* USER CODE BEGIN 4 */ |
||
734 | |||
735 | /* USER CODE END 4 */ |
||
736 | |||
737 | /** |
||
738 | * @brief This function is executed in case of error occurrence. |
||
739 | * @param None |
||
740 | * @retval None |
||
741 | */ |
||
742 | void Error_Handler(void) |
||
743 | { |
||
744 | /* USER CODE BEGIN Error_Handler */ |
||
9 | mjames | 745 | /* User can add his own implementation to report the HAL error return state */ |
746 | while (1) { |
||
747 | } |
||
2 | mjames | 748 | /* USER CODE END Error_Handler */ |
749 | } |
||
750 | |||
751 | #ifdef USE_FULL_ASSERT |
||
752 | |||
753 | /** |
||
754 | * @brief Reports the name of the source file and the source line number |
||
755 | * where the assert_param error has occurred. |
||
756 | * @param file: pointer to the source file name |
||
757 | * @param line: assert_param error line source number |
||
758 | * @retval None |
||
759 | */ |
||
760 | void assert_failed(uint8_t* file, uint32_t line) |
||
761 | { |
||
762 | /* USER CODE BEGIN 6 */ |
||
9 | mjames | 763 | /* User can add his own implementation to report the file name and line number, |
764 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
2 | mjames | 765 | /* USER CODE END 6 */ |
766 | |||
767 | } |
||
768 | |||
769 | #endif |
||
770 | |||
771 | /** |
||
772 | * @} |
||
773 | */ |
||
774 | |||
775 | /** |
||
776 | * @} |
||
777 | */ |
||
778 | |||
779 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |