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