Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /* USER CODE BEGIN Header */ |
| 2 | /** |
||
| 3 | mjames | 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 | */ |
||
| 2 | mjames | 19 | /* USER CODE END Header */ |
| 20 | /* Includes ------------------------------------------------------------------*/ |
||
| 21 | #include "main.h" |
||
| 22 | |||
| 23 | /* Private includes ----------------------------------------------------------*/ |
||
| 24 | /* USER CODE BEGIN Includes */ |
||
| 4 | mjames | 25 | #include "sendLeds.h" |
| 5 | mjames | 26 | #include "libLog/log.h" |
| 2 | mjames | 27 | /* USER CODE END Includes */ |
| 28 | |||
| 29 | /* Private typedef -----------------------------------------------------------*/ |
||
| 30 | /* USER CODE BEGIN PTD */ |
||
| 31 | |||
| 32 | /* USER CODE END PTD */ |
||
| 33 | |||
| 34 | /* Private define ------------------------------------------------------------*/ |
||
| 35 | /* USER CODE BEGIN PD */ |
||
| 36 | /* USER CODE END PD */ |
||
| 37 | |||
| 38 | /* Private macro -------------------------------------------------------------*/ |
||
| 39 | /* USER CODE BEGIN PM */ |
||
| 40 | |||
| 41 | /* USER CODE END PM */ |
||
| 42 | |||
| 43 | /* Private variables ---------------------------------------------------------*/ |
||
| 6 | mjames | 44 | ADC_HandleTypeDef hadc; |
| 2 | mjames | 45 | DMA_HandleTypeDef hdma_adc; |
| 46 | |||
| 3 | mjames | 47 | SPI_HandleTypeDef hspi1; |
| 48 | DMA_HandleTypeDef hdma_spi1_tx; |
||
| 49 | |||
| 2 | mjames | 50 | TIM_HandleTypeDef htim1; |
| 51 | TIM_HandleTypeDef htim3; |
||
| 6 | mjames | 52 | TIM_HandleTypeDef htim14; |
| 2 | mjames | 53 | |
| 54 | UART_HandleTypeDef huart1; |
||
| 55 | |||
| 56 | /* USER CODE BEGIN PV */ |
||
| 57 | #define NUM_SAMPLES 8 |
||
| 3 | mjames | 58 | uint32_t ADC_Samples[NUM_SAMPLES]; |
| 2 | mjames | 59 | |
| 3 | mjames | 60 | #define NUM_SPI_BYTES 20 |
| 61 | uint8_t SPI_Buffer[NUM_SPI_BYTES]; |
||
| 2 | mjames | 62 | |
| 63 | /* USER CODE END PV */ |
||
| 64 | |||
| 65 | /* Private function prototypes -----------------------------------------------*/ |
||
| 66 | void SystemClock_Config(void); |
||
| 67 | static void MX_GPIO_Init(void); |
||
| 68 | static void MX_DMA_Init(void); |
||
| 69 | static void MX_ADC_Init(void); |
||
| 70 | static void MX_TIM1_Init(void); |
||
| 71 | static void MX_USART1_UART_Init(void); |
||
| 72 | static void MX_TIM3_Init(void); |
||
| 3 | mjames | 73 | static void MX_SPI1_Init(void); |
| 6 | mjames | 74 | static void MX_TIM14_Init(void); |
| 2 | mjames | 75 | /* USER CODE BEGIN PFP */ |
| 76 | |||
| 77 | /* USER CODE END PFP */ |
||
| 78 | |||
| 79 | /* Private user code ---------------------------------------------------------*/ |
||
| 80 | /* USER CODE BEGIN 0 */ |
||
| 81 | |||
| 82 | void setDrive1(uint8_t bit) { |
||
| 83 | if (bit) { |
||
| 3 | mjames | 84 | HAL_GPIO_WritePin(step1P_GPIO_Port, step1P_Pin, GPIO_PIN_SET); |
| 85 | HAL_GPIO_WritePin(step1N_GPIO_Port, step1N_Pin, GPIO_PIN_RESET); |
||
| 2 | mjames | 86 | } else { |
| 3 | mjames | 87 | HAL_GPIO_WritePin(step1P_GPIO_Port, step1P_Pin, GPIO_PIN_RESET); |
| 88 | HAL_GPIO_WritePin(step1N_GPIO_Port, step1N_Pin, GPIO_PIN_SET); |
||
| 2 | mjames | 89 | } |
| 90 | } |
||
| 91 | |||
| 92 | void setDrive2(uint8_t bit) { |
||
| 93 | if (bit) { |
||
| 3 | mjames | 94 | HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin, GPIO_PIN_SET); |
| 95 | HAL_GPIO_WritePin(step2N_GPIO_Port, step2N_Pin, GPIO_PIN_RESET); |
||
| 2 | mjames | 96 | } else { |
| 3 | mjames | 97 | HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin, GPIO_PIN_RESET); |
| 98 | HAL_GPIO_WritePin(step2N_GPIO_Port, step2N_Pin, GPIO_PIN_SET); |
||
| 2 | mjames | 99 | } |
| 100 | } |
||
| 101 | |||
| 102 | // |
||
| 103 | int count = 0; |
||
| 104 | int origin = 0; |
||
| 105 | |||
| 3 | mjames | 106 | void moveGauge(int target) { |
| 2 | mjames | 107 | |
| 3 | mjames | 108 | unsigned const fast = 10; |
| 5 | mjames | 109 | unsigned const slow = 20; |
| 2 | mjames | 110 | unsigned const range = slow - fast; |
| 111 | unsigned del = fast; |
||
| 112 | int step = 1; |
||
| 113 | while (count != target) { |
||
| 114 | |||
| 115 | // use bit mask, so it works for negative count values .. |
||
| 116 | switch (count & 3) { |
||
| 117 | case 0: |
||
| 3 | mjames | 118 | setDrive1(0); |
| 119 | setDrive2(1); |
||
| 2 | mjames | 120 | break; |
| 121 | case 1: |
||
| 122 | setDrive1(1); |
||
| 123 | setDrive2(1); |
||
| 124 | break; |
||
| 125 | case 2: |
||
| 3 | mjames | 126 | setDrive1(1); |
| 127 | setDrive2(0); |
||
| 2 | mjames | 128 | break; |
| 129 | case 3: |
||
| 130 | setDrive1(0); |
||
| 131 | setDrive2(0); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | |||
| 135 | // all this calculates minimum distance from |
||
| 136 | // target or origin |
||
| 137 | int d1 = count - origin; |
||
| 138 | if (d1 < 0) |
||
| 139 | d1 = -d1; |
||
| 140 | int d2 = count - target; |
||
| 141 | if (d2 < 0) |
||
| 142 | d2 = -d2; |
||
| 143 | // finally, minimum distance |
||
| 144 | int dist = d1 < d2 ? d1 : d2; |
||
| 145 | |||
| 146 | del = fast; |
||
| 147 | if (dist < range) // inside lower bound of distance |
||
| 148 | { |
||
| 149 | del = slow - dist; |
||
| 150 | } |
||
| 151 | HAL_Delay(del); |
||
| 152 | |||
| 153 | if (count < target) { |
||
| 154 | step = 1; |
||
| 155 | } |
||
| 156 | if (count > target) { |
||
| 157 | step = -1; |
||
| 158 | } |
||
| 159 | if (count == target) { |
||
| 160 | step = 0; |
||
| 161 | } |
||
| 162 | count = count + step; |
||
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | } |
||
| 167 | |||
| 168 | // move gauge back to zero position |
||
| 3 | mjames | 169 | void resetGauge() { |
| 2 | mjames | 170 | moveGauge(-600); |
| 3 | mjames | 171 | count = 0; |
| 2 | mjames | 172 | origin = 0; |
| 173 | } |
||
| 174 | |||
| 5 | mjames | 175 | |
| 176 | uint16_t getADC(){ |
||
| 177 | uint16_t mean = 0; |
||
| 178 | for (int i =0; i< NUM_SAMPLES; i++) |
||
| 179 | { |
||
| 180 | mean+=ADC_Samples[i]; |
||
| 181 | } |
||
| 182 | return mean/NUM_SAMPLES; |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | |||
| 2 | mjames | 187 | /* USER CODE END 0 */ |
| 188 | |||
| 189 | /** |
||
| 190 | * @brief The application entry point. |
||
| 191 | * @retval int |
||
| 192 | */ |
||
| 193 | int main(void) |
||
| 194 | { |
||
| 195 | /* USER CODE BEGIN 1 */ |
||
| 196 | // half degree step 315 degree movement |
||
| 197 | #define GAUGE_MAX 315*2 |
||
| 198 | #define GAUGE_MIN 0 |
||
| 199 | |||
| 200 | /* USER CODE END 1 */ |
||
| 201 | |||
| 202 | /* MCU Configuration--------------------------------------------------------*/ |
||
| 203 | |||
| 204 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
| 205 | HAL_Init(); |
||
| 206 | |||
| 207 | /* USER CODE BEGIN Init */ |
||
| 208 | |||
| 209 | /* USER CODE END Init */ |
||
| 210 | |||
| 211 | /* Configure the system clock */ |
||
| 212 | SystemClock_Config(); |
||
| 213 | |||
| 214 | /* USER CODE BEGIN SysInit */ |
||
| 215 | |||
| 216 | /* USER CODE END SysInit */ |
||
| 217 | |||
| 218 | /* Initialize all configured peripherals */ |
||
| 219 | MX_GPIO_Init(); |
||
| 220 | MX_DMA_Init(); |
||
| 221 | MX_ADC_Init(); |
||
| 222 | MX_TIM1_Init(); |
||
| 223 | MX_USART1_UART_Init(); |
||
| 224 | MX_TIM3_Init(); |
||
| 3 | mjames | 225 | MX_SPI1_Init(); |
| 6 | mjames | 226 | MX_TIM14_Init(); |
| 2 | mjames | 227 | /* USER CODE BEGIN 2 */ |
| 228 | |||
| 3 | mjames | 229 | int i; |
| 230 | for (i = 0; i < NUM_SAMPLES; i++) |
||
| 231 | ADC_Samples[i] = 0; |
||
| 2 | mjames | 232 | |
| 3 | mjames | 233 | HAL_ADC_MspInit(&hadc); |
| 2 | mjames | 234 | |
| 3 | mjames | 235 | HAL_ADC_Start_DMA(&hadc, ADC_Samples, NUM_SAMPLES); |
| 2 | mjames | 236 | |
| 3 | mjames | 237 | HAL_ADC_Start_IT(&hadc); |
| 2 | mjames | 238 | |
| 3 | mjames | 239 | // timer 3 triggers the ADC |
| 240 | HAL_TIM_Base_MspInit(&htim3); |
||
| 241 | HAL_TIM_Base_Start_IT(&htim3); |
||
| 2 | mjames | 242 | |
| 6 | mjames | 243 | // start the timer 1 |
| 244 | |||
| 245 | HAL_TIM_Base_Start(&htim14); |
||
| 246 | |||
| 247 | __HAL_TIM_SET_COMPARE(&htim14,TIM_CHANNEL_1, 0); |
||
| 248 | |||
| 249 | HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); |
||
| 250 | |||
| 3 | mjames | 251 | resetGauge(); |
| 2 | mjames | 252 | |
| 3 | mjames | 253 | /* USER CODE END 2 */ |
| 2 | mjames | 254 | |
| 3 | mjames | 255 | /* Infinite loop */ |
| 256 | /* USER CODE BEGIN WHILE */ |
||
| 257 | while (1) { |
||
| 6 | mjames | 258 | sendLeds(); |
| 3 | mjames | 259 | HAL_Delay(10); |
| 2 | mjames | 260 | |
| 5 | mjames | 261 | |
| 262 | uint16_t val = log2fix ( getADC(), 8); |
||
| 263 | |||
| 264 | moveGauge(val * 4); |
||
| 265 | |||
| 266 | |||
| 2 | mjames | 267 | /* USER CODE END WHILE */ |
| 268 | |||
| 269 | /* USER CODE BEGIN 3 */ |
||
| 3 | mjames | 270 | } |
| 2 | mjames | 271 | /* USER CODE END 3 */ |
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @brief System Clock Configuration |
||
| 276 | * @retval None |
||
| 277 | */ |
||
| 278 | void SystemClock_Config(void) |
||
| 279 | { |
||
| 280 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
||
| 281 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
| 282 | RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; |
||
| 283 | |||
| 4 | mjames | 284 | /** Initializes the RCC Oscillators according to the specified parameters |
| 285 | * in the RCC_OscInitTypeDef structure. |
||
| 2 | mjames | 286 | */ |
| 287 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14; |
||
| 288 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
| 289 | RCC_OscInitStruct.HSI14State = RCC_HSI14_ON; |
||
| 290 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
||
| 291 | RCC_OscInitStruct.HSI14CalibrationValue = 16; |
||
| 292 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
| 293 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
||
| 4 | mjames | 294 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; |
| 2 | mjames | 295 | RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1; |
| 296 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
||
| 297 | { |
||
| 298 | Error_Handler(); |
||
| 299 | } |
||
| 6 | mjames | 300 | |
| 4 | mjames | 301 | /** Initializes the CPU, AHB and APB buses clocks |
| 2 | mjames | 302 | */ |
| 303 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
| 304 | |RCC_CLOCKTYPE_PCLK1; |
||
| 305 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
||
| 306 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
| 307 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
||
| 308 | |||
| 309 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
||
| 310 | { |
||
| 311 | Error_Handler(); |
||
| 312 | } |
||
| 313 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1; |
||
| 314 | PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1; |
||
| 315 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) |
||
| 316 | { |
||
| 317 | Error_Handler(); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @brief ADC Initialization Function |
||
| 323 | * @param None |
||
| 324 | * @retval None |
||
| 325 | */ |
||
| 326 | static void MX_ADC_Init(void) |
||
| 327 | { |
||
| 328 | |||
| 329 | /* USER CODE BEGIN ADC_Init 0 */ |
||
| 330 | |||
| 331 | /* USER CODE END ADC_Init 0 */ |
||
| 332 | |||
| 333 | ADC_ChannelConfTypeDef sConfig = {0}; |
||
| 334 | |||
| 335 | /* USER CODE BEGIN ADC_Init 1 */ |
||
| 336 | |||
| 337 | /* USER CODE END ADC_Init 1 */ |
||
| 6 | mjames | 338 | |
| 3 | mjames | 339 | /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) |
| 2 | mjames | 340 | */ |
| 341 | hadc.Instance = ADC1; |
||
| 342 | hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; |
||
| 343 | hadc.Init.Resolution = ADC_RESOLUTION_12B; |
||
| 344 | hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
||
| 345 | hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; |
||
| 346 | hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; |
||
| 347 | hadc.Init.LowPowerAutoWait = DISABLE; |
||
| 348 | hadc.Init.LowPowerAutoPowerOff = DISABLE; |
||
| 349 | hadc.Init.ContinuousConvMode = DISABLE; |
||
| 350 | hadc.Init.DiscontinuousConvMode = DISABLE; |
||
| 351 | hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO; |
||
| 352 | hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; |
||
| 353 | hadc.Init.DMAContinuousRequests = ENABLE; |
||
| 354 | hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED; |
||
| 355 | if (HAL_ADC_Init(&hadc) != HAL_OK) |
||
| 356 | { |
||
| 357 | Error_Handler(); |
||
| 358 | } |
||
| 6 | mjames | 359 | |
| 3 | mjames | 360 | /** Configure for the selected ADC regular channel to be converted. |
| 2 | mjames | 361 | */ |
| 362 | sConfig.Channel = ADC_CHANNEL_0; |
||
| 363 | sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; |
||
| 364 | sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5; |
||
| 365 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
| 366 | { |
||
| 367 | Error_Handler(); |
||
| 368 | } |
||
| 6 | mjames | 369 | |
| 370 | /** Configure for the selected ADC regular channel to be converted. |
||
| 371 | */ |
||
| 372 | sConfig.Channel = ADC_CHANNEL_1; |
||
| 373 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
||
| 374 | { |
||
| 375 | Error_Handler(); |
||
| 376 | } |
||
| 2 | mjames | 377 | /* USER CODE BEGIN ADC_Init 2 */ |
| 378 | |||
| 379 | /* USER CODE END ADC_Init 2 */ |
||
| 380 | |||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 3 | mjames | 384 | * @brief SPI1 Initialization Function |
| 385 | * @param None |
||
| 386 | * @retval None |
||
| 387 | */ |
||
| 388 | static void MX_SPI1_Init(void) |
||
| 389 | { |
||
| 390 | |||
| 391 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
| 392 | |||
| 393 | /* USER CODE END SPI1_Init 0 */ |
||
| 394 | |||
| 395 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
| 396 | |||
| 397 | /* USER CODE END SPI1_Init 1 */ |
||
| 398 | /* SPI1 parameter configuration*/ |
||
| 399 | hspi1.Instance = SPI1; |
||
| 400 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
| 401 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
||
| 402 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
| 403 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
| 404 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
| 405 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
| 4 | mjames | 406 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; |
| 407 | hspi1.Init.FirstBit = SPI_FIRSTBIT_LSB; |
||
| 3 | mjames | 408 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
| 409 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
| 410 | hspi1.Init.CRCPolynomial = 7; |
||
| 411 | hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; |
||
| 4 | mjames | 412 | hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; |
| 3 | mjames | 413 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
| 414 | { |
||
| 415 | Error_Handler(); |
||
| 416 | } |
||
| 417 | /* USER CODE BEGIN SPI1_Init 2 */ |
||
| 418 | |||
| 419 | /* USER CODE END SPI1_Init 2 */ |
||
| 420 | |||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 2 | mjames | 424 | * @brief TIM1 Initialization Function |
| 425 | * @param None |
||
| 426 | * @retval None |
||
| 427 | */ |
||
| 428 | static void MX_TIM1_Init(void) |
||
| 429 | { |
||
| 430 | |||
| 431 | /* USER CODE BEGIN TIM1_Init 0 */ |
||
| 432 | |||
| 433 | /* USER CODE END TIM1_Init 0 */ |
||
| 434 | |||
| 435 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
| 436 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
| 437 | |||
| 438 | /* USER CODE BEGIN TIM1_Init 1 */ |
||
| 439 | |||
| 440 | /* USER CODE END TIM1_Init 1 */ |
||
| 441 | htim1.Instance = TIM1; |
||
| 442 | htim1.Init.Prescaler = 0; |
||
| 443 | htim1.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 4 | mjames | 444 | htim1.Init.Period = 65535; |
| 2 | mjames | 445 | htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
| 446 | htim1.Init.RepetitionCounter = 0; |
||
| 447 | htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 448 | if (HAL_TIM_Base_Init(&htim1) != HAL_OK) |
||
| 449 | { |
||
| 450 | Error_Handler(); |
||
| 451 | } |
||
| 452 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
| 453 | if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) |
||
| 454 | { |
||
| 455 | Error_Handler(); |
||
| 456 | } |
||
| 457 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
||
| 458 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 459 | if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) |
||
| 460 | { |
||
| 461 | Error_Handler(); |
||
| 462 | } |
||
| 463 | /* USER CODE BEGIN TIM1_Init 2 */ |
||
| 464 | |||
| 465 | /* USER CODE END TIM1_Init 2 */ |
||
| 466 | |||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @brief TIM3 Initialization Function |
||
| 471 | * @param None |
||
| 472 | * @retval None |
||
| 473 | */ |
||
| 474 | static void MX_TIM3_Init(void) |
||
| 475 | { |
||
| 476 | |||
| 477 | /* USER CODE BEGIN TIM3_Init 0 */ |
||
| 478 | |||
| 479 | /* USER CODE END TIM3_Init 0 */ |
||
| 480 | |||
| 481 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
| 482 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
| 483 | |||
| 484 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
| 485 | |||
| 486 | /* USER CODE END TIM3_Init 1 */ |
||
| 487 | htim3.Instance = TIM3; |
||
| 488 | htim3.Init.Prescaler = 32; |
||
| 489 | htim3.Init.CounterMode = TIM_COUNTERMODE_DOWN; |
||
| 490 | htim3.Init.Period = 10000; |
||
| 491 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 492 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; |
||
| 493 | if (HAL_TIM_Base_Init(&htim3) != HAL_OK) |
||
| 494 | { |
||
| 495 | Error_Handler(); |
||
| 496 | } |
||
| 497 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
| 498 | if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) |
||
| 499 | { |
||
| 500 | Error_Handler(); |
||
| 501 | } |
||
| 502 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
| 503 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 504 | if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) |
||
| 505 | { |
||
| 506 | Error_Handler(); |
||
| 507 | } |
||
| 508 | /* USER CODE BEGIN TIM3_Init 2 */ |
||
| 509 | |||
| 510 | /* USER CODE END TIM3_Init 2 */ |
||
| 511 | |||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 6 | mjames | 515 | * @brief TIM14 Initialization Function |
| 516 | * @param None |
||
| 517 | * @retval None |
||
| 518 | */ |
||
| 519 | static void MX_TIM14_Init(void) |
||
| 520 | { |
||
| 521 | |||
| 522 | /* USER CODE BEGIN TIM14_Init 0 */ |
||
| 523 | |||
| 524 | /* USER CODE END TIM14_Init 0 */ |
||
| 525 | |||
| 526 | TIM_OC_InitTypeDef sConfigOC = {0}; |
||
| 527 | |||
| 528 | /* USER CODE BEGIN TIM14_Init 1 */ |
||
| 529 | |||
| 530 | /* USER CODE END TIM14_Init 1 */ |
||
| 531 | htim14.Instance = TIM14; |
||
| 532 | htim14.Init.Prescaler = 8; |
||
| 533 | htim14.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 534 | htim14.Init.Period = 65535; |
||
| 535 | htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 536 | htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 537 | if (HAL_TIM_Base_Init(&htim14) != HAL_OK) |
||
| 538 | { |
||
| 539 | Error_Handler(); |
||
| 540 | } |
||
| 541 | if (HAL_TIM_PWM_Init(&htim14) != HAL_OK) |
||
| 542 | { |
||
| 543 | Error_Handler(); |
||
| 544 | } |
||
| 545 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
||
| 546 | sConfigOC.Pulse = 0; |
||
| 547 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
||
| 548 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
| 549 | if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
||
| 550 | { |
||
| 551 | Error_Handler(); |
||
| 552 | } |
||
| 553 | /* USER CODE BEGIN TIM14_Init 2 */ |
||
| 554 | |||
| 555 | /* USER CODE END TIM14_Init 2 */ |
||
| 556 | HAL_TIM_MspPostInit(&htim14); |
||
| 557 | |||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 2 | mjames | 561 | * @brief USART1 Initialization Function |
| 562 | * @param None |
||
| 563 | * @retval None |
||
| 564 | */ |
||
| 565 | static void MX_USART1_UART_Init(void) |
||
| 566 | { |
||
| 567 | |||
| 568 | /* USER CODE BEGIN USART1_Init 0 */ |
||
| 569 | |||
| 570 | /* USER CODE END USART1_Init 0 */ |
||
| 571 | |||
| 572 | /* USER CODE BEGIN USART1_Init 1 */ |
||
| 573 | |||
| 574 | /* USER CODE END USART1_Init 1 */ |
||
| 575 | huart1.Instance = USART1; |
||
| 576 | huart1.Init.BaudRate = 38400; |
||
| 577 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 578 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
| 579 | huart1.Init.Parity = UART_PARITY_NONE; |
||
| 580 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
| 581 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 582 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 583 | huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; |
||
| 584 | huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; |
||
| 585 | if (HAL_UART_Init(&huart1) != HAL_OK) |
||
| 586 | { |
||
| 587 | Error_Handler(); |
||
| 588 | } |
||
| 589 | /* USER CODE BEGIN USART1_Init 2 */ |
||
| 590 | |||
| 591 | /* USER CODE END USART1_Init 2 */ |
||
| 592 | |||
| 593 | } |
||
| 594 | |||
| 3 | mjames | 595 | /** |
| 2 | mjames | 596 | * Enable DMA controller clock |
| 597 | */ |
||
| 3 | mjames | 598 | static void MX_DMA_Init(void) |
| 2 | mjames | 599 | { |
| 600 | |||
| 601 | /* DMA controller clock enable */ |
||
| 602 | __HAL_RCC_DMA1_CLK_ENABLE(); |
||
| 603 | |||
| 604 | /* DMA interrupt init */ |
||
| 605 | /* DMA1_Channel1_IRQn interrupt configuration */ |
||
| 606 | HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); |
||
| 607 | HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); |
||
| 3 | mjames | 608 | /* DMA1_Channel2_3_IRQn interrupt configuration */ |
| 609 | HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0); |
||
| 610 | HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn); |
||
| 2 | mjames | 611 | |
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @brief GPIO Initialization Function |
||
| 616 | * @param None |
||
| 617 | * @retval None |
||
| 618 | */ |
||
| 619 | static void MX_GPIO_Init(void) |
||
| 620 | { |
||
| 621 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
||
| 622 | |||
| 623 | /* GPIO Ports Clock Enable */ |
||
| 6 | mjames | 624 | __HAL_RCC_GPIOF_CLK_ENABLE(); |
| 2 | mjames | 625 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 626 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
| 627 | |||
| 628 | /*Configure GPIO pin Output Level */ |
||
| 6 | mjames | 629 | HAL_GPIO_WritePin(enable_current_GPIO_Port, enable_current_Pin, GPIO_PIN_RESET); |
| 2 | mjames | 630 | |
| 631 | /*Configure GPIO pin Output Level */ |
||
| 6 | mjames | 632 | HAL_GPIO_WritePin(GPIOA, step2N_Pin|step1N_Pin|step1P_Pin, GPIO_PIN_RESET); |
| 633 | |||
| 634 | /*Configure GPIO pin Output Level */ |
||
| 3 | mjames | 635 | HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin, GPIO_PIN_RESET); |
| 2 | mjames | 636 | |
| 6 | mjames | 637 | /*Configure GPIO pin : enable_current_Pin */ |
| 638 | GPIO_InitStruct.Pin = enable_current_Pin; |
||
| 2 | mjames | 639 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 640 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 641 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 6 | mjames | 642 | HAL_GPIO_Init(enable_current_GPIO_Port, &GPIO_InitStruct); |
| 643 | |||
| 644 | /*Configure GPIO pins : step2N_Pin step1N_Pin step1P_Pin */ |
||
| 645 | GPIO_InitStruct.Pin = step2N_Pin|step1N_Pin|step1P_Pin; |
||
| 646 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
| 647 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 648 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 2 | mjames | 649 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 650 | |||
| 3 | mjames | 651 | /*Configure GPIO pin : step2P_Pin */ |
| 652 | GPIO_InitStruct.Pin = step2P_Pin; |
||
| 2 | mjames | 653 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 654 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 655 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 3 | mjames | 656 | HAL_GPIO_Init(step2P_GPIO_Port, &GPIO_InitStruct); |
| 2 | mjames | 657 | |
| 658 | } |
||
| 659 | |||
| 660 | /* USER CODE BEGIN 4 */ |
||
| 661 | |||
| 662 | /* USER CODE END 4 */ |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @brief This function is executed in case of error occurrence. |
||
| 666 | * @retval None |
||
| 667 | */ |
||
| 668 | void Error_Handler(void) |
||
| 669 | { |
||
| 670 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
| 3 | mjames | 671 | /* User can add his own implementation to report the HAL error return state */ |
| 2 | mjames | 672 | |
| 673 | /* USER CODE END Error_Handler_Debug */ |
||
| 674 | } |
||
| 675 | |||
| 676 | #ifdef USE_FULL_ASSERT |
||
| 677 | /** |
||
| 678 | * @brief Reports the name of the source file and the source line number |
||
| 679 | * where the assert_param error has occurred. |
||
| 680 | * @param file: pointer to the source file name |
||
| 681 | * @param line: assert_param error line source number |
||
| 682 | * @retval None |
||
| 683 | */ |
||
| 4 | mjames | 684 | void assert_failed(uint8_t *file, uint32_t line) |
| 3 | mjames | 685 | { |
| 2 | mjames | 686 | /* USER CODE BEGIN 6 */ |
| 687 | /* User can add his own implementation to report the file name and line number, |
||
| 688 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
| 689 | /* USER CODE END 6 */ |
||
| 690 | } |
||
| 691 | #endif /* USE_FULL_ASSERT */ |