Rev 14 | Rev 17 | 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 |
14 | mjames | 71 | uint16_t ADC_Samples[6]; |
8 | mjames | 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); |
||
13 | mjames | 94 | static void MX_USART2_UART_Init(void); |
2 | mjames | 95 | static void MX_USART1_UART_Init(void); |
96 | |||
97 | /* USER CODE BEGIN PFP */ |
||
98 | /* Private function prototypes -----------------------------------------------*/ |
||
99 | |||
9 | mjames | 100 | /* USER CODE END PFP */ |
7 | mjames | 101 | |
9 | mjames | 102 | /* USER CODE BEGIN 0 */ |
7 | mjames | 103 | |
9 | mjames | 104 | void plx_sendword(int x) { |
105 | PutCharSerial(&uc1, ((x) >> 6) & 0x3F); |
||
106 | PutCharSerial(&uc1, (x) & 0x3F); |
||
107 | } |
||
2 | mjames | 108 | |
9 | mjames | 109 | void ProcessRPM(int instance) { |
110 | // compute the timer values |
||
111 | // snapshot timers |
||
112 | unsigned long RPM_Pulsewidth; |
||
113 | unsigned long RPM_Count_Val; |
||
114 | __disable_irq(); // copy the counter value |
||
115 | RPM_Count_Val = RPM_Count; |
||
116 | __enable_irq(); |
||
117 | // do calculations |
||
118 | // if there is only one entry, cannot get difference |
||
119 | if (RPM_Count_Latch != RPM_Count_Val) { |
||
120 | while (1) { |
||
121 | unsigned int base_time; |
||
122 | unsigned int new_time; |
||
123 | // if we are at N-1, stop. |
||
124 | unsigned int next_count = RPM_Count_Latch + 1; |
||
125 | if (next_count == RPM_SAMPLES) { |
||
126 | next_count = 0; |
||
127 | } |
||
128 | if (next_count == RPM_Count_Val) { |
||
129 | break; |
||
130 | } |
||
131 | base_time = RPM_Time[RPM_Count_Latch]; |
||
132 | new_time = RPM_Time[next_count]; |
||
133 | RPM_Count_Latch = next_count; |
||
134 | if (new_time > base_time) { |
||
135 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
||
136 | } else { |
||
13 | mjames | 137 | RPM_Pulsewidth = new_time - base_time + 65536; // deal with wrapping |
9 | mjames | 138 | } |
2 | mjames | 139 | |
9 | mjames | 140 | RPM_Diff += RPM_Pulsewidth; |
141 | // need to check if this is a long pulse. If it is, keep the answer |
||
142 | if (RPM_Pulsewidth > BREAKER_MIN) { |
||
143 | RPM_Pulsecount++; // count one pulse |
||
144 | RPM_FilteredWidth += RPM_Diff; // add its width to the accumulator |
||
145 | RPM_Diff = 0; // reset accumulator of all the narrow widths |
||
146 | } |
||
147 | } |
||
148 | |||
149 | } |
||
150 | |||
151 | if (RPM_Pulsecount > 0) { |
||
152 | |||
153 | // now have time for N pulses in clocks |
||
154 | // need to scale by 19.55: one unit is 19.55 RPM |
||
155 | // 1Hz is 60 RPM |
||
156 | Coded_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE) |
||
157 | / (RPM_FilteredWidth) + 0.5; |
||
158 | #if !defined MY_DEBUG |
||
159 | // reset here unless we want to debug |
||
160 | RPM_Pulsecount = 0; |
||
161 | RPM_FilteredWidth = 0; |
||
162 | #endif |
||
163 | } |
||
164 | |||
165 | // send the current RPM calculation |
||
166 | plx_sendword(PLX_RPM); |
||
167 | PutCharSerial(&uc1, instance); |
||
168 | plx_sendword(Coded_RPM); |
||
169 | } |
||
170 | |||
11 | mjames | 171 | uint8_t CHT_Timer = 0; |
9 | mjames | 172 | |
173 | // this uses a MAX6675 which is a simple 16 bit read |
||
174 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
||
11 | mjames | 175 | // must wait > 0.22 seconds between conversion attempts as this is the measurement time |
176 | // |
||
9 | mjames | 177 | void ProcessCHT(int instance) |
178 | { |
||
179 | uint8_t buffer[2]; |
||
11 | mjames | 180 | CHT_Timer++; |
181 | if(CHT_Timer >= 3) // every 300 milliseconds |
||
182 | |||
183 | { |
||
184 | CHT_Timer=0; |
||
185 | |||
9 | mjames | 186 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_RESET); |
187 | |||
188 | |||
11 | mjames | 189 | HAL_Delay(1); |
190 | |||
9 | mjames | 191 | HAL_SPI_Receive(&hspi1, buffer, 2, 2); |
192 | |||
193 | |||
194 | |||
195 | uint16_t obs = (buffer[0]<<8)| buffer[1]; |
||
196 | |||
197 | uint8_t good = (obs & 4)==0; |
||
198 | if(good) |
||
199 | { |
||
10 | mjames | 200 | Coded_CHT = obs>>5; |
9 | mjames | 201 | } |
10 | mjames | 202 | else |
203 | { |
||
204 | Coded_CHT= 1000; // signal fail |
||
205 | } |
||
11 | mjames | 206 | } |
207 | |||
16 | mjames | 208 | plx_sendword(PLX_X_CHT); |
9 | mjames | 209 | PutCharSerial(&uc1, instance); |
210 | plx_sendword(Coded_CHT); |
||
12 | mjames | 211 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET); |
9 | mjames | 212 | |
213 | } |
||
214 | |||
12 | mjames | 215 | void ProcessBatteryVoltage(int instance) |
216 | { |
||
217 | plx_sendword(PLX_Volts); |
||
218 | PutCharSerial(&uc1, instance); |
||
219 | plx_sendword(ADC_Samples[instance]); |
||
220 | |||
221 | |||
222 | |||
223 | } |
||
224 | |||
225 | |||
16 | mjames | 226 | void ProcessTiming(int instance) |
227 | { |
||
228 | plx_sendword(PLX_Timing); |
||
229 | PutCharSerial(&uc1, instance); |
||
230 | plx_sendword(64-15); // make it negative |
||
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(); |
||
13 | mjames | 257 | MX_USART2_UART_Init(); |
2 | mjames | 258 | MX_USART1_UART_Init(); |
259 | |||
260 | /* USER CODE BEGIN 2 */ |
||
13 | mjames | 261 | HAL_MspInit(); |
2 | mjames | 262 | |
13 | mjames | 263 | // Not using HAL USART code |
9 | mjames | 264 | __HAL_RCC_USART1_CLK_ENABLE() |
265 | ; // PLX comms port |
||
266 | __HAL_RCC_USART2_CLK_ENABLE() |
||
267 | ; // Debug comms port |
||
7 | mjames | 268 | /* setup the USART control blocks */ |
269 | init_usart_ctl(&uc1, huart1.Instance); |
||
270 | init_usart_ctl(&uc2, huart2.Instance); |
||
271 | |||
272 | EnableSerialRxInterrupt(&uc1); |
||
273 | EnableSerialRxInterrupt(&uc2); |
||
274 | |||
13 | mjames | 275 | |
276 | HAL_SPI_MspInit(&hspi1); |
||
277 | |||
278 | HAL_ADC_MspInit(&hadc); |
||
14 | mjames | 279 | |
13 | mjames | 280 | HAL_ADC_Start_DMA(&hadc, ADC_Samples, 6); |
281 | |||
14 | mjames | 282 | HAL_ADC_Start_IT(&hadc); |
13 | mjames | 283 | |
284 | HAL_TIM_Base_MspInit(&htim6); |
||
9 | mjames | 285 | HAL_TIM_Base_Start_IT(&htim6); |
13 | mjames | 286 | |
287 | // initialise all the STMCubeMX stuff |
||
288 | HAL_TIM_Base_MspInit(&htim2); |
||
289 | // Start the counter |
||
12 | mjames | 290 | HAL_TIM_Base_Start(&htim2); |
13 | mjames | 291 | // Start the input capture and the interrupt |
292 | HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1); |
||
8 | mjames | 293 | |
7 | mjames | 294 | |
2 | mjames | 295 | /* USER CODE END 2 */ |
296 | |||
297 | /* Infinite loop */ |
||
298 | /* USER CODE BEGIN WHILE */ |
||
9 | mjames | 299 | while (1) { |
2 | mjames | 300 | /* USER CODE END WHILE */ |
301 | |||
302 | /* USER CODE BEGIN 3 */ |
||
9 | mjames | 303 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
304 | int c; |
||
305 | char send = 0; |
||
2 | mjames | 306 | |
9 | mjames | 307 | // poll the input for a stop bit or timeout |
308 | if(PollSerial(&uc1)) |
||
309 | { |
||
310 | c = GetCharSerial(&uc1); |
||
311 | if (c != PLX_Stop) |
||
312 | { |
||
313 | PutCharSerial(&uc1,c); // echo all but the stop bit |
||
314 | } else { // must be a stop character |
||
315 | send = 1; // start our sending process. |
||
316 | } |
||
317 | } |
||
318 | |||
319 | // sort out auto-sending |
||
320 | if (TimerFlag) |
||
321 | { |
||
10 | mjames | 322 | TimerFlag = 0; |
9 | mjames | 323 | if (NoSerialIn) |
324 | { |
||
325 | PutCharSerial(&uc1,PLX_Start); |
||
326 | send = 1; |
||
327 | } |
||
328 | } |
||
329 | if (send) |
||
330 | { |
||
331 | send = 0; |
||
332 | |||
12 | mjames | 333 | uint16_t val; |
13 | mjames | 334 | val = __HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_1); |
12 | mjames | 335 | PutCharSerial(&uc2,(val&31) + 32); |
336 | |||
13 | mjames | 337 | |
338 | |||
12 | mjames | 339 | // send the observations |
9 | mjames | 340 | ProcessRPM(0); |
341 | ProcessCHT(0); |
||
14 | mjames | 342 | ProcessBatteryVoltage(3); |
343 | ProcessBatteryVoltage(1); |
||
9 | mjames | 344 | |
16 | mjames | 345 | ProcessTiming(0); |
346 | |||
9 | mjames | 347 | PutCharSerial(&uc1,PLX_Stop); |
348 | } |
||
349 | |||
350 | } |
||
2 | mjames | 351 | /* USER CODE END 3 */ |
352 | |||
353 | } |
||
354 | |||
355 | /** System Clock Configuration |
||
356 | */ |
||
357 | void SystemClock_Config(void) |
||
358 | { |
||
359 | |||
360 | RCC_OscInitTypeDef RCC_OscInitStruct; |
||
361 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
||
362 | |||
363 | __HAL_RCC_PWR_CLK_ENABLE(); |
||
364 | |||
365 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
||
366 | |||
367 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
||
368 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
369 | RCC_OscInitStruct.HSICalibrationValue = 16; |
||
370 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
371 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
||
372 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; |
||
373 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
||
374 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
||
375 | { |
||
376 | Error_Handler(); |
||
377 | } |
||
378 | |||
379 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
380 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
381 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
||
382 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
383 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
||
384 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
385 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
||
386 | { |
||
387 | Error_Handler(); |
||
388 | } |
||
389 | |||
390 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
||
391 | |||
392 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
||
393 | |||
394 | /* SysTick_IRQn interrupt configuration */ |
||
395 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); |
||
396 | } |
||
397 | |||
398 | /* ADC init function */ |
||
399 | static void MX_ADC_Init(void) |
||
400 | { |
||
401 | |||
402 | ADC_ChannelConfTypeDef sConfig; |
||
403 | |||
404 | /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) |
||
405 | */ |
||
406 | hadc.Instance = ADC1; |
||
407 | hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; |
||
408 | hadc.Init.Resolution = ADC_RESOLUTION_12B; |
||
409 | hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
||
410 | hadc.Init.ScanConvMode = ADC_SCAN_ENABLE; |
||
411 | hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV; |
||
412 | hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE; |
||
413 | hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE; |
||
414 | hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A; |
||
415 | hadc.Init.ContinuousConvMode = DISABLE; |
||
416 | hadc.Init.NbrOfConversion = 6; |
||
417 | hadc.Init.DiscontinuousConvMode = DISABLE; |
||
418 | hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO; |
||
419 | hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; |
||
14 | mjames | 420 | hadc.Init.DMAContinuousRequests = ENABLE; |
2 | mjames | 421 | if (HAL_ADC_Init(&hadc) != HAL_OK) |
422 | { |
||
423 | Error_Handler(); |
||
424 | } |
||
425 | |||
426 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
427 | */ |
||
6 | mjames | 428 | sConfig.Channel = ADC_CHANNEL_10; |
2 | mjames | 429 | sConfig.Rank = 1; |
14 | mjames | 430 | sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES; |
2 | mjames | 431 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
432 | { |
||
433 | Error_Handler(); |
||
434 | } |
||
435 | |||
436 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
437 | */ |
||
6 | mjames | 438 | sConfig.Channel = ADC_CHANNEL_11; |
2 | mjames | 439 | sConfig.Rank = 2; |
440 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
441 | { |
||
442 | Error_Handler(); |
||
443 | } |
||
444 | |||
445 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
446 | */ |
||
6 | mjames | 447 | sConfig.Channel = ADC_CHANNEL_12; |
2 | mjames | 448 | sConfig.Rank = 3; |
449 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
450 | { |
||
451 | Error_Handler(); |
||
452 | } |
||
453 | |||
454 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
455 | */ |
||
6 | mjames | 456 | sConfig.Channel = ADC_CHANNEL_13; |
2 | mjames | 457 | sConfig.Rank = 4; |
458 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
459 | { |
||
460 | Error_Handler(); |
||
461 | } |
||
462 | |||
463 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
464 | */ |
||
465 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
||
466 | sConfig.Rank = 5; |
||
467 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
468 | { |
||
469 | Error_Handler(); |
||
470 | } |
||
471 | |||
472 | /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
||
473 | */ |
||
474 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
||
475 | sConfig.Rank = 6; |
||
476 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
477 | { |
||
478 | Error_Handler(); |
||
479 | } |
||
480 | |||
481 | } |
||
482 | |||
483 | /* SPI1 init function */ |
||
484 | static void MX_SPI1_Init(void) |
||
485 | { |
||
486 | |||
487 | hspi1.Instance = SPI1; |
||
488 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
3 | mjames | 489 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
2 | mjames | 490 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
491 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
492 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
493 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
10 | mjames | 494 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; |
2 | mjames | 495 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
496 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
497 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
498 | hspi1.Init.CRCPolynomial = 10; |
||
499 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
500 | { |
||
501 | Error_Handler(); |
||
502 | } |
||
503 | |||
504 | } |
||
505 | |||
506 | /* TIM2 init function */ |
||
507 | static void MX_TIM2_Init(void) |
||
508 | { |
||
509 | |||
12 | mjames | 510 | TIM_ClockConfigTypeDef sClockSourceConfig; |
2 | mjames | 511 | TIM_MasterConfigTypeDef sMasterConfig; |
512 | TIM_IC_InitTypeDef sConfigIC; |
||
513 | |||
514 | htim2.Instance = TIM2; |
||
515 | htim2.Init.Prescaler = 320; |
||
516 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
13 | mjames | 517 | htim2.Init.Period = 65535; |
2 | mjames | 518 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
12 | mjames | 519 | if (HAL_TIM_Base_Init(&htim2) != HAL_OK) |
520 | { |
||
521 | Error_Handler(); |
||
522 | } |
||
523 | |||
524 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
525 | if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) |
||
526 | { |
||
527 | Error_Handler(); |
||
528 | } |
||
529 | |||
2 | mjames | 530 | if (HAL_TIM_IC_Init(&htim2) != HAL_OK) |
531 | { |
||
532 | Error_Handler(); |
||
533 | } |
||
534 | |||
13 | mjames | 535 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
2 | mjames | 536 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
537 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) |
||
538 | { |
||
539 | Error_Handler(); |
||
540 | } |
||
541 | |||
542 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; |
||
543 | sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; |
||
544 | sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; |
||
545 | sConfigIC.ICFilter = 0; |
||
546 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) |
||
547 | { |
||
548 | Error_Handler(); |
||
549 | } |
||
550 | |||
551 | } |
||
552 | |||
553 | /* TIM6 init function */ |
||
554 | static void MX_TIM6_Init(void) |
||
555 | { |
||
556 | |||
557 | TIM_MasterConfigTypeDef sMasterConfig; |
||
558 | |||
559 | htim6.Instance = TIM6; |
||
13 | mjames | 560 | htim6.Init.Prescaler = 320; |
2 | mjames | 561 | htim6.Init.CounterMode = TIM_COUNTERMODE_UP; |
13 | mjames | 562 | htim6.Init.Period = 9999; |
2 | mjames | 563 | if (HAL_TIM_Base_Init(&htim6) != HAL_OK) |
564 | { |
||
565 | Error_Handler(); |
||
566 | } |
||
567 | |||
568 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
569 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
570 | if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) |
||
571 | { |
||
572 | Error_Handler(); |
||
573 | } |
||
574 | |||
575 | } |
||
576 | |||
577 | /* USART1 init function */ |
||
578 | static void MX_USART1_UART_Init(void) |
||
579 | { |
||
580 | |||
581 | huart1.Instance = USART1; |
||
582 | huart1.Init.BaudRate = 19200; |
||
583 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
584 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
585 | huart1.Init.Parity = UART_PARITY_NONE; |
||
586 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
587 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
588 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
589 | if (HAL_UART_Init(&huart1) != HAL_OK) |
||
590 | { |
||
591 | Error_Handler(); |
||
592 | } |
||
593 | |||
594 | } |
||
595 | |||
6 | mjames | 596 | /* USART2 init function */ |
597 | static void MX_USART2_UART_Init(void) |
||
598 | { |
||
599 | |||
600 | huart2.Instance = USART2; |
||
601 | huart2.Init.BaudRate = 115200; |
||
602 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
603 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
604 | huart2.Init.Parity = UART_PARITY_NONE; |
||
605 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
606 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
607 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
608 | if (HAL_UART_Init(&huart2) != HAL_OK) |
||
609 | { |
||
610 | Error_Handler(); |
||
611 | } |
||
612 | |||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Enable DMA controller clock |
||
617 | */ |
||
618 | static void MX_DMA_Init(void) |
||
619 | { |
||
620 | /* DMA controller clock enable */ |
||
621 | __HAL_RCC_DMA1_CLK_ENABLE(); |
||
622 | |||
623 | /* DMA interrupt init */ |
||
624 | /* DMA1_Channel1_IRQn interrupt configuration */ |
||
625 | HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); |
||
626 | HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); |
||
627 | |||
628 | } |
||
629 | |||
2 | mjames | 630 | /** Configure pins as |
631 | * Analog |
||
632 | * Input |
||
633 | * Output |
||
634 | * EVENT_OUT |
||
635 | * EXTI |
||
5 | mjames | 636 | * Free pins are configured automatically as Analog (this feature is enabled through |
637 | * the Code Generation settings) |
||
2 | mjames | 638 | */ |
639 | static void MX_GPIO_Init(void) |
||
640 | { |
||
641 | |||
642 | GPIO_InitTypeDef GPIO_InitStruct; |
||
643 | |||
644 | /* GPIO Ports Clock Enable */ |
||
5 | mjames | 645 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
646 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
||
2 | mjames | 647 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
648 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
5 | mjames | 649 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
2 | mjames | 650 | |
6 | mjames | 651 | /*Configure GPIO pins : PC13 PC14 PC15 PC6 |
5 | mjames | 652 | PC7 PC8 PC9 PC10 |
653 | PC11 PC12 */ |
||
6 | mjames | 654 | GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_6 |
5 | mjames | 655 | |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10 |
656 | |GPIO_PIN_11|GPIO_PIN_12; |
||
657 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
658 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
659 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
||
2 | mjames | 660 | |
5 | mjames | 661 | /*Configure GPIO pins : PH0 PH1 */ |
662 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; |
||
663 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
664 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
665 | HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); |
||
3 | mjames | 666 | |
6 | mjames | 667 | /*Configure GPIO pins : PA0 PA1 PA8 PA11 |
7 | mjames | 668 | PA12 */ |
6 | mjames | 669 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_11 |
7 | mjames | 670 | |GPIO_PIN_12; |
6 | mjames | 671 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
672 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
673 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
||
674 | |||
7 | mjames | 675 | /*Configure GPIO pin : LED_Blink_Pin */ |
676 | GPIO_InitStruct.Pin = LED_Blink_Pin; |
||
2 | mjames | 677 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
678 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
7 | mjames | 679 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
680 | HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct); |
||
2 | mjames | 681 | |
3 | mjames | 682 | /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */ |
683 | GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin; |
||
684 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
685 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
686 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
687 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
||
688 | |||
7 | mjames | 689 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NS_Temp_Pin */ |
690 | GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NS_Temp_Pin; |
||
3 | mjames | 691 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
692 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
693 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
7 | mjames | 694 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
3 | mjames | 695 | |
13 | mjames | 696 | /*Configure GPIO pins : PB2 PB10 PB11 PB12 |
697 | PB13 PB14 PB15 PB3 |
||
698 | PB4 PB5 PB6 PB7 |
||
699 | PB8 PB9 */ |
||
700 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12 |
||
701 | |GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_3 |
||
702 | |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 |
||
703 | |GPIO_PIN_8|GPIO_PIN_9; |
||
5 | mjames | 704 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
705 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
706 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
707 | |||
708 | /*Configure GPIO pin : PD2 */ |
||
709 | GPIO_InitStruct.Pin = GPIO_PIN_2; |
||
710 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
||
711 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
712 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
||
713 | |||
714 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 715 | HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET); |
5 | mjames | 716 | |
717 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 718 | HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET); |
5 | mjames | 719 | |
720 | /*Configure GPIO pin Output Level */ |
||
7 | mjames | 721 | HAL_GPIO_WritePin(SPI1CD_GPIO_Port, SPI1CD_Pin, GPIO_PIN_RESET); |
722 | |||
723 | /*Configure GPIO pin Output Level */ |
||
5 | mjames | 724 | HAL_GPIO_WritePin(SPI_RESET_GPIO_Port, SPI_RESET_Pin, GPIO_PIN_RESET); |
725 | |||
7 | mjames | 726 | /*Configure GPIO pin Output Level */ |
727 | HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET); |
||
728 | |||
2 | mjames | 729 | } |
730 | |||
731 | /* USER CODE BEGIN 4 */ |
||
732 | |||
733 | /* USER CODE END 4 */ |
||
734 | |||
735 | /** |
||
736 | * @brief This function is executed in case of error occurrence. |
||
737 | * @param None |
||
738 | * @retval None |
||
739 | */ |
||
740 | void Error_Handler(void) |
||
741 | { |
||
742 | /* USER CODE BEGIN Error_Handler */ |
||
9 | mjames | 743 | /* User can add his own implementation to report the HAL error return state */ |
744 | while (1) { |
||
745 | } |
||
2 | mjames | 746 | /* USER CODE END Error_Handler */ |
747 | } |
||
748 | |||
749 | #ifdef USE_FULL_ASSERT |
||
750 | |||
751 | /** |
||
752 | * @brief Reports the name of the source file and the source line number |
||
753 | * where the assert_param error has occurred. |
||
754 | * @param file: pointer to the source file name |
||
755 | * @param line: assert_param error line source number |
||
756 | * @retval None |
||
757 | */ |
||
758 | void assert_failed(uint8_t* file, uint32_t line) |
||
759 | { |
||
760 | /* USER CODE BEGIN 6 */ |
||
9 | mjames | 761 | /* User can add his own implementation to report the file name and line number, |
762 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
2 | mjames | 763 | /* USER CODE END 6 */ |
764 | |||
765 | } |
||
766 | |||
767 | #endif |
||
768 | |||
769 | /** |
||
770 | * @} |
||
771 | */ |
||
772 | |||
773 | /** |
||
774 | * @} |
||
775 | */ |
||
776 | |||
777 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |