Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* USER CODE BEGIN Header */ |
2 | /** |
||
3 | ****************************************************************************** |
||
4 | * @file : main.c |
||
5 | * @brief : Main program body |
||
6 | ****************************************************************************** |
||
7 | * @attention |
||
8 | * |
||
9 | * <h2><center>© Copyright (c) 2020 STMicroelectronics. |
||
10 | * All rights reserved.</center></h2> |
||
11 | * |
||
12 | * This software component is licensed by ST under BSD 3-Clause license, |
||
13 | * the "License"; You may not use this file except in compliance with the |
||
14 | * License. You may obtain a copy of the License at: |
||
15 | * opensource.org/licenses/BSD-3-Clause |
||
16 | * |
||
17 | ****************************************************************************** |
||
18 | */ |
||
19 | /* USER CODE END Header */ |
||
20 | /* Includes ------------------------------------------------------------------*/ |
||
21 | #include "main.h" |
||
22 | |||
23 | /* Private includes ----------------------------------------------------------*/ |
||
24 | /* USER CODE BEGIN Includes */ |
||
25 | #include "libSerial/serial.h" |
||
9 | mjames | 26 | |
6 | mjames | 27 | #include "display.h" |
2 | mjames | 28 | /* USER CODE END Includes */ |
29 | |||
30 | /* Private typedef -----------------------------------------------------------*/ |
||
31 | /* USER CODE BEGIN PTD */ |
||
32 | |||
33 | /* USER CODE END PTD */ |
||
34 | |||
35 | /* Private define ------------------------------------------------------------*/ |
||
36 | /* USER CODE BEGIN PD */ |
||
37 | /* USER CODE END PD */ |
||
38 | |||
39 | /* Private macro -------------------------------------------------------------*/ |
||
40 | /* USER CODE BEGIN PM */ |
||
41 | |||
42 | /* USER CODE END PM */ |
||
43 | |||
44 | /* Private variables ---------------------------------------------------------*/ |
||
45 | CAN_HandleTypeDef hcan; |
||
46 | |||
47 | SPI_HandleTypeDef hspi1; |
||
48 | |||
9 | mjames | 49 | TIM_HandleTypeDef htim3; |
2 | mjames | 50 | TIM_HandleTypeDef htim4; |
51 | |||
52 | UART_HandleTypeDef huart1; |
||
53 | |||
54 | /* USER CODE BEGIN PV */ |
||
55 | |||
56 | /* USER CODE END PV */ |
||
57 | |||
58 | /* Private function prototypes -----------------------------------------------*/ |
||
9 | mjames | 59 | void SystemClock_Config(void); |
60 | static void MX_GPIO_Init(void); |
||
61 | static void MX_CAN_Init(void); |
||
62 | static void MX_SPI1_Init(void); |
||
63 | static void MX_TIM4_Init(void); |
||
64 | static void MX_USART1_UART_Init(void); |
||
65 | static void MX_TIM3_Init(void); |
||
2 | mjames | 66 | /* USER CODE BEGIN PFP */ |
67 | |||
68 | /* USER CODE END PFP */ |
||
69 | |||
70 | /* Private user code ---------------------------------------------------------*/ |
||
71 | /* USER CODE BEGIN 0 */ |
||
72 | |||
73 | /* USER CODE END 0 */ |
||
74 | |||
75 | /** |
||
9 | mjames | 76 | * @brief The application entry point. |
77 | * @retval int |
||
78 | */ |
||
79 | int main(void) |
||
2 | mjames | 80 | { |
81 | /* USER CODE BEGIN 1 */ |
||
82 | |||
83 | /* USER CODE END 1 */ |
||
84 | |||
85 | /* MCU Configuration--------------------------------------------------------*/ |
||
86 | |||
87 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
9 | mjames | 88 | HAL_Init(); |
2 | mjames | 89 | |
90 | /* USER CODE BEGIN Init */ |
||
91 | |||
92 | /* USER CODE END Init */ |
||
93 | |||
94 | /* Configure the system clock */ |
||
9 | mjames | 95 | SystemClock_Config(); |
2 | mjames | 96 | |
97 | /* USER CODE BEGIN SysInit */ |
||
98 | |||
99 | /* USER CODE END SysInit */ |
||
100 | |||
101 | /* Initialize all configured peripherals */ |
||
9 | mjames | 102 | MX_GPIO_Init(); |
103 | MX_CAN_Init(); |
||
104 | MX_SPI1_Init(); |
||
105 | MX_TIM4_Init(); |
||
106 | MX_USART1_UART_Init(); |
||
107 | MX_TIM3_Init(); |
||
2 | mjames | 108 | /* USER CODE BEGIN 2 */ |
109 | __HAL_RCC_USART1_CLK_ENABLE(); |
||
110 | /* setup the USART control blocks */ |
||
6 | mjames | 111 | init_usart_ctl (&uc1, huart1.Instance); |
2 | mjames | 112 | |
113 | EnableSerialRxInterrupt (&uc1); |
||
114 | |||
6 | mjames | 115 | cc_init (); |
116 | HAL_Delay (1000); |
||
2 | mjames | 117 | /* USER CODE END 2 */ |
118 | |||
119 | /* Infinite loop */ |
||
120 | /* USER CODE BEGIN WHILE */ |
||
121 | while (1) |
||
122 | { |
||
6 | mjames | 123 | cc_run (); |
2 | mjames | 124 | |
125 | |||
9 | mjames | 126 | HAL_Delay (50); |
5 | mjames | 127 | |
9 | mjames | 128 | /* USER CODE END WHILE */ |
5 | mjames | 129 | |
9 | mjames | 130 | /* USER CODE BEGIN 3 */ |
2 | mjames | 131 | } |
132 | /* USER CODE END 3 */ |
||
133 | } |
||
134 | |||
135 | /** |
||
9 | mjames | 136 | * @brief System Clock Configuration |
137 | * @retval None |
||
138 | */ |
||
139 | void SystemClock_Config(void) |
||
2 | mjames | 140 | { |
9 | mjames | 141 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
142 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
2 | mjames | 143 | |
144 | /** Initializes the RCC Oscillators according to the specified parameters |
||
9 | mjames | 145 | * in the RCC_OscInitTypeDef structure. |
146 | */ |
||
2 | mjames | 147 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
148 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
||
149 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
150 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
151 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
152 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
153 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8; |
||
9 | mjames | 154 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
155 | { |
||
156 | Error_Handler(); |
||
157 | } |
||
2 | mjames | 158 | /** Initializes the CPU, AHB and APB buses clocks |
9 | mjames | 159 | */ |
160 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
161 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
2 | mjames | 162 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
163 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
164 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
165 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
166 | |||
9 | mjames | 167 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) |
168 | { |
||
169 | Error_Handler(); |
||
170 | } |
||
2 | mjames | 171 | } |
172 | |||
173 | /** |
||
9 | mjames | 174 | * @brief CAN Initialization Function |
175 | * @param None |
||
176 | * @retval None |
||
177 | */ |
||
178 | static void MX_CAN_Init(void) |
||
2 | mjames | 179 | { |
180 | |||
181 | /* USER CODE BEGIN CAN_Init 0 */ |
||
182 | |||
183 | /* USER CODE END CAN_Init 0 */ |
||
184 | |||
185 | /* USER CODE BEGIN CAN_Init 1 */ |
||
186 | |||
187 | /* USER CODE END CAN_Init 1 */ |
||
188 | hcan.Instance = CAN1; |
||
189 | hcan.Init.Prescaler = 16; |
||
190 | hcan.Init.Mode = CAN_MODE_NORMAL; |
||
191 | hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; |
||
192 | hcan.Init.TimeSeg1 = CAN_BS1_1TQ; |
||
193 | hcan.Init.TimeSeg2 = CAN_BS2_1TQ; |
||
194 | hcan.Init.TimeTriggeredMode = DISABLE; |
||
195 | hcan.Init.AutoBusOff = DISABLE; |
||
196 | hcan.Init.AutoWakeUp = DISABLE; |
||
197 | hcan.Init.AutoRetransmission = DISABLE; |
||
198 | hcan.Init.ReceiveFifoLocked = DISABLE; |
||
199 | hcan.Init.TransmitFifoPriority = DISABLE; |
||
9 | mjames | 200 | if (HAL_CAN_Init(&hcan) != HAL_OK) |
201 | { |
||
202 | Error_Handler(); |
||
203 | } |
||
2 | mjames | 204 | /* USER CODE BEGIN CAN_Init 2 */ |
205 | |||
206 | /* USER CODE END CAN_Init 2 */ |
||
207 | |||
208 | } |
||
209 | |||
210 | /** |
||
9 | mjames | 211 | * @brief SPI1 Initialization Function |
212 | * @param None |
||
213 | * @retval None |
||
214 | */ |
||
215 | static void MX_SPI1_Init(void) |
||
2 | mjames | 216 | { |
217 | |||
218 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
219 | |||
220 | /* USER CODE END SPI1_Init 0 */ |
||
221 | |||
222 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
223 | |||
224 | /* USER CODE END SPI1_Init 1 */ |
||
225 | /* SPI1 parameter configuration*/ |
||
226 | hspi1.Instance = SPI1; |
||
227 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
228 | hspi1.Init.Direction = SPI_DIRECTION_1LINE; |
||
229 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
5 | mjames | 230 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; |
231 | hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; |
||
2 | mjames | 232 | hspi1.Init.NSS = SPI_NSS_SOFT; |
233 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
||
234 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
235 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
236 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
237 | hspi1.Init.CRCPolynomial = 10; |
||
9 | mjames | 238 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
239 | { |
||
240 | Error_Handler(); |
||
241 | } |
||
2 | mjames | 242 | /* USER CODE BEGIN SPI1_Init 2 */ |
243 | |||
244 | /* USER CODE END SPI1_Init 2 */ |
||
245 | |||
246 | } |
||
247 | |||
248 | /** |
||
9 | mjames | 249 | * @brief TIM3 Initialization Function |
250 | * @param None |
||
251 | * @retval None |
||
252 | */ |
||
253 | static void MX_TIM3_Init(void) |
||
2 | mjames | 254 | { |
255 | |||
9 | mjames | 256 | /* USER CODE BEGIN TIM3_Init 0 */ |
257 | |||
258 | /* USER CODE END TIM3_Init 0 */ |
||
259 | |||
260 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
261 | TIM_OC_InitTypeDef sConfigOC = {0}; |
||
262 | |||
263 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
264 | |||
265 | /* USER CODE END TIM3_Init 1 */ |
||
266 | htim3.Instance = TIM3; |
||
267 | htim3.Init.Prescaler = 640; |
||
268 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
269 | htim3.Init.Period = 10000; |
||
270 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
271 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
272 | if (HAL_TIM_OC_Init(&htim3) != HAL_OK) |
||
273 | { |
||
274 | Error_Handler(); |
||
275 | } |
||
276 | if (HAL_TIM_OnePulse_Init(&htim3, TIM_OPMODE_SINGLE) != HAL_OK) |
||
277 | { |
||
278 | Error_Handler(); |
||
279 | } |
||
280 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; |
||
281 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
282 | if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) |
||
283 | { |
||
284 | Error_Handler(); |
||
285 | } |
||
286 | sConfigOC.OCMode = TIM_OCMODE_TIMING; |
||
287 | sConfigOC.Pulse = 9999; |
||
288 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
||
289 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
290 | if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
||
291 | { |
||
292 | Error_Handler(); |
||
293 | } |
||
294 | /* USER CODE BEGIN TIM3_Init 2 */ |
||
295 | |||
296 | /* USER CODE END TIM3_Init 2 */ |
||
297 | |||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @brief TIM4 Initialization Function |
||
302 | * @param None |
||
303 | * @retval None |
||
304 | */ |
||
305 | static void MX_TIM4_Init(void) |
||
306 | { |
||
307 | |||
2 | mjames | 308 | /* USER CODE BEGIN TIM4_Init 0 */ |
309 | |||
310 | /* USER CODE END TIM4_Init 0 */ |
||
311 | |||
9 | mjames | 312 | TIM_Encoder_InitTypeDef sConfig = {0}; |
313 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
2 | mjames | 314 | |
315 | /* USER CODE BEGIN TIM4_Init 1 */ |
||
316 | |||
317 | /* USER CODE END TIM4_Init 1 */ |
||
318 | htim4.Instance = TIM4; |
||
319 | htim4.Init.Prescaler = 0; |
||
320 | htim4.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
321 | htim4.Init.Period = 65535; |
||
322 | htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
323 | htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
324 | sConfig.EncoderMode = TIM_ENCODERMODE_TI12; |
||
325 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
||
326 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
||
327 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
328 | sConfig.IC1Filter = 8; |
||
329 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
||
330 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
||
331 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
332 | sConfig.IC2Filter = 8; |
||
9 | mjames | 333 | if (HAL_TIM_Encoder_Init(&htim4, &sConfig) != HAL_OK) |
334 | { |
||
335 | Error_Handler(); |
||
336 | } |
||
2 | mjames | 337 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
338 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
9 | mjames | 339 | if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) |
340 | { |
||
341 | Error_Handler(); |
||
342 | } |
||
2 | mjames | 343 | /* USER CODE BEGIN TIM4_Init 2 */ |
344 | |||
345 | /* USER CODE END TIM4_Init 2 */ |
||
346 | |||
347 | } |
||
348 | |||
349 | /** |
||
9 | mjames | 350 | * @brief USART1 Initialization Function |
351 | * @param None |
||
352 | * @retval None |
||
353 | */ |
||
354 | static void MX_USART1_UART_Init(void) |
||
2 | mjames | 355 | { |
356 | |||
357 | /* USER CODE BEGIN USART1_Init 0 */ |
||
358 | |||
359 | /* USER CODE END USART1_Init 0 */ |
||
360 | |||
361 | /* USER CODE BEGIN USART1_Init 1 */ |
||
362 | |||
363 | /* USER CODE END USART1_Init 1 */ |
||
364 | huart1.Instance = USART1; |
||
365 | huart1.Init.BaudRate = 115200; |
||
366 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
367 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
368 | huart1.Init.Parity = UART_PARITY_NONE; |
||
369 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
370 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
371 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
9 | mjames | 372 | if (HAL_UART_Init(&huart1) != HAL_OK) |
373 | { |
||
374 | Error_Handler(); |
||
375 | } |
||
2 | mjames | 376 | /* USER CODE BEGIN USART1_Init 2 */ |
377 | |||
378 | /* USER CODE END USART1_Init 2 */ |
||
379 | |||
380 | } |
||
381 | |||
382 | /** |
||
9 | mjames | 383 | * @brief GPIO Initialization Function |
384 | * @param None |
||
385 | * @retval None |
||
386 | */ |
||
387 | static void MX_GPIO_Init(void) |
||
2 | mjames | 388 | { |
9 | mjames | 389 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
2 | mjames | 390 | |
391 | /* GPIO Ports Clock Enable */ |
||
5 | mjames | 392 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
2 | mjames | 393 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
394 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
395 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
396 | |||
397 | /*Configure GPIO pin Output Level */ |
||
9 | mjames | 398 | HAL_GPIO_WritePin(GPIOC, SPI_CD_Pin|SPI_RESET_Pin, GPIO_PIN_RESET); |
2 | mjames | 399 | |
400 | /*Configure GPIO pin Output Level */ |
||
9 | mjames | 401 | HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_RESET); |
2 | mjames | 402 | |
5 | mjames | 403 | /*Configure GPIO pins : SPI_CD_Pin SPI_RESET_Pin */ |
9 | mjames | 404 | GPIO_InitStruct.Pin = SPI_CD_Pin|SPI_RESET_Pin; |
2 | mjames | 405 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
406 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
407 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
9 | mjames | 408 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
2 | mjames | 409 | |
5 | mjames | 410 | /*Configure GPIO pin : SPI_NSS1_Pin */ |
411 | GPIO_InitStruct.Pin = SPI_NSS1_Pin; |
||
2 | mjames | 412 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
413 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
414 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
9 | mjames | 415 | HAL_GPIO_Init(SPI_NSS1_GPIO_Port, &GPIO_InitStruct); |
2 | mjames | 416 | |
417 | } |
||
418 | |||
419 | /* USER CODE BEGIN 4 */ |
||
420 | |||
421 | /* USER CODE END 4 */ |
||
422 | |||
423 | /** |
||
9 | mjames | 424 | * @brief This function is executed in case of error occurrence. |
425 | * @retval None |
||
426 | */ |
||
427 | void Error_Handler(void) |
||
2 | mjames | 428 | { |
429 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
430 | /* User can add his own implementation to report the HAL error return state */ |
||
431 | |||
432 | /* USER CODE END Error_Handler_Debug */ |
||
433 | } |
||
434 | |||
435 | #ifdef USE_FULL_ASSERT |
||
436 | /** |
||
437 | * @brief Reports the name of the source file and the source line number |
||
438 | * where the assert_param error has occurred. |
||
439 | * @param file: pointer to the source file name |
||
440 | * @param line: assert_param error line source number |
||
441 | * @retval None |
||
442 | */ |
||
443 | void assert_failed(uint8_t *file, uint32_t line) |
||
444 | { |
||
445 | /* USER CODE BEGIN 6 */ |
||
446 | /* User can add his own implementation to report the file name and line number, |
||
447 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
448 | /* USER CODE END 6 */ |
||
449 | } |
||
450 | #endif /* USE_FULL_ASSERT */ |
||
451 | |||
452 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |