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