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