Subversion Repositories FuelGauge

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line 39... Line 39...
39
/* USER CODE BEGIN PM */
39
/* USER CODE BEGIN PM */
40
 
40
 
41
/* USER CODE END PM */
41
/* USER CODE END PM */
42
 
42
 
43
/* Private variables ---------------------------------------------------------*/
43
/* Private variables ---------------------------------------------------------*/
44
ADC_HandleTypeDef hadc;
44
 ADC_HandleTypeDef hadc;
45
DMA_HandleTypeDef hdma_adc;
45
DMA_HandleTypeDef hdma_adc;
46
 
46
 
47
SPI_HandleTypeDef hspi1;
47
SPI_HandleTypeDef hspi1;
48
DMA_HandleTypeDef hdma_spi1_tx;
48
DMA_HandleTypeDef hdma_spi1_tx;
49
 
49
 
50
TIM_HandleTypeDef htim1;
50
TIM_HandleTypeDef htim1;
51
TIM_HandleTypeDef htim3;
51
TIM_HandleTypeDef htim3;
-
 
52
TIM_HandleTypeDef htim14;
52
 
53
 
53
UART_HandleTypeDef huart1;
54
UART_HandleTypeDef huart1;
54
 
55
 
55
/* USER CODE BEGIN PV */
56
/* USER CODE BEGIN PV */
56
#define NUM_SAMPLES 8
57
#define NUM_SAMPLES 8
Line 68... Line 69...
68
static void MX_ADC_Init(void);
69
static void MX_ADC_Init(void);
69
static void MX_TIM1_Init(void);
70
static void MX_TIM1_Init(void);
70
static void MX_USART1_UART_Init(void);
71
static void MX_USART1_UART_Init(void);
71
static void MX_TIM3_Init(void);
72
static void MX_TIM3_Init(void);
72
static void MX_SPI1_Init(void);
73
static void MX_SPI1_Init(void);
-
 
74
static void MX_TIM14_Init(void);
73
/* USER CODE BEGIN PFP */
75
/* USER CODE BEGIN PFP */
74
 
76
 
75
/* USER CODE END PFP */
77
/* USER CODE END PFP */
76
 
78
 
77
/* Private user code ---------------------------------------------------------*/
79
/* Private user code ---------------------------------------------------------*/
Line 219... Line 221...
219
  MX_ADC_Init();
221
  MX_ADC_Init();
220
  MX_TIM1_Init();
222
  MX_TIM1_Init();
221
  MX_USART1_UART_Init();
223
  MX_USART1_UART_Init();
222
  MX_TIM3_Init();
224
  MX_TIM3_Init();
223
  MX_SPI1_Init();
225
  MX_SPI1_Init();
-
 
226
  MX_TIM14_Init();
224
  /* USER CODE BEGIN 2 */
227
  /* USER CODE BEGIN 2 */
225
 
228
 
226
        int i;
229
        int i;
227
        for (i = 0; i < NUM_SAMPLES; i++)
230
        for (i = 0; i < NUM_SAMPLES; i++)
228
                ADC_Samples[i] = 0;
231
                ADC_Samples[i] = 0;
Line 235... Line 238...
235
 
238
 
236
        // timer 3 triggers the ADC
239
        // timer 3 triggers the ADC
237
        HAL_TIM_Base_MspInit(&htim3);
240
        HAL_TIM_Base_MspInit(&htim3);
238
        HAL_TIM_Base_Start_IT(&htim3);
241
        HAL_TIM_Base_Start_IT(&htim3);
239
 
242
 
-
 
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
 
240
        resetGauge();
251
        resetGauge();
241
 
252
 
242
  /* USER CODE END 2 */
253
  /* USER CODE END 2 */
243
 
254
 
244
  /* Infinite loop */
255
  /* Infinite loop */
245
  /* USER CODE BEGIN WHILE */
256
  /* USER CODE BEGIN WHILE */
246
        while (1) {
257
        while (1) {
247
                sendLeds();
258
        sendLeds();
248
                HAL_Delay(10);
259
                HAL_Delay(10);
249
 
260
 
250
 
261
 
251
        uint16_t val = log2fix ( getADC(), 8);
262
        uint16_t val = log2fix ( getADC(), 8);
252
 
263
 
Line 284... Line 295...
284
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
295
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
285
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
296
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
286
  {
297
  {
287
    Error_Handler();
298
    Error_Handler();
288
  }
299
  }
-
 
300
 
289
  /** Initializes the CPU, AHB and APB buses clocks
301
  /** Initializes the CPU, AHB and APB buses clocks
290
  */
302
  */
291
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
303
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
292
                              |RCC_CLOCKTYPE_PCLK1;
304
                              |RCC_CLOCKTYPE_PCLK1;
293
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
305
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Line 321... Line 333...
321
  ADC_ChannelConfTypeDef sConfig = {0};
333
  ADC_ChannelConfTypeDef sConfig = {0};
322
 
334
 
323
  /* USER CODE BEGIN ADC_Init 1 */
335
  /* USER CODE BEGIN ADC_Init 1 */
324
 
336
 
325
  /* USER CODE END ADC_Init 1 */
337
  /* USER CODE END ADC_Init 1 */
-
 
338
 
326
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
339
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
327
  */
340
  */
328
  hadc.Instance = ADC1;
341
  hadc.Instance = ADC1;
329
  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
342
  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
330
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
343
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
Line 341... Line 354...
341
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
354
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
342
  if (HAL_ADC_Init(&hadc) != HAL_OK)
355
  if (HAL_ADC_Init(&hadc) != HAL_OK)
343
  {
356
  {
344
    Error_Handler();
357
    Error_Handler();
345
  }
358
  }
-
 
359
 
346
  /** Configure for the selected ADC regular channel to be converted.
360
  /** Configure for the selected ADC regular channel to be converted.
347
  */
361
  */
348
  sConfig.Channel = ADC_CHANNEL_0;
362
  sConfig.Channel = ADC_CHANNEL_0;
349
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
363
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
350
  sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
364
  sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
351
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
365
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
352
  {
366
  {
353
    Error_Handler();
367
    Error_Handler();
354
  }
368
  }
-
 
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
  }
355
  /* USER CODE BEGIN ADC_Init 2 */
377
  /* USER CODE BEGIN ADC_Init 2 */
356
 
378
 
357
  /* USER CODE END ADC_Init 2 */
379
  /* USER CODE END ADC_Init 2 */
358
 
380
 
359
}
381
}
Line 488... Line 510...
488
  /* USER CODE END TIM3_Init 2 */
510
  /* USER CODE END TIM3_Init 2 */
489
 
511
 
490
}
512
}
491
 
513
 
492
/**
514
/**
-
 
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
/**
493
  * @brief USART1 Initialization Function
561
  * @brief USART1 Initialization Function
494
  * @param None
562
  * @param None
495
  * @retval None
563
  * @retval None
496
  */
564
  */
497
static void MX_USART1_UART_Init(void)
565
static void MX_USART1_UART_Init(void)
Line 551... Line 619...
551
static void MX_GPIO_Init(void)
619
static void MX_GPIO_Init(void)
552
{
620
{
553
  GPIO_InitTypeDef GPIO_InitStruct = {0};
621
  GPIO_InitTypeDef GPIO_InitStruct = {0};
554
 
622
 
555
  /* GPIO Ports Clock Enable */
623
  /* GPIO Ports Clock Enable */
-
 
624
  __HAL_RCC_GPIOF_CLK_ENABLE();
556
  __HAL_RCC_GPIOA_CLK_ENABLE();
625
  __HAL_RCC_GPIOA_CLK_ENABLE();
557
  __HAL_RCC_GPIOB_CLK_ENABLE();
626
  __HAL_RCC_GPIOB_CLK_ENABLE();
558
 
627
 
559
  /*Configure GPIO pin Output Level */
628
  /*Configure GPIO pin Output Level */
-
 
629
  HAL_GPIO_WritePin(enable_current_GPIO_Port, enable_current_Pin, GPIO_PIN_RESET);
-
 
630
 
-
 
631
  /*Configure GPIO pin Output Level */
560
  HAL_GPIO_WritePin(GPIOA, enableCurrent_Pin|step2N_Pin|step1N_Pin|step1P_Pin, GPIO_PIN_RESET);
632
  HAL_GPIO_WritePin(GPIOA, step2N_Pin|step1N_Pin|step1P_Pin, GPIO_PIN_RESET);
561
 
633
 
562
  /*Configure GPIO pin Output Level */
634
  /*Configure GPIO pin Output Level */
563
  HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin, GPIO_PIN_RESET);
635
  HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin, GPIO_PIN_RESET);
564
 
636
 
-
 
637
  /*Configure GPIO pin : enable_current_Pin */
-
 
638
  GPIO_InitStruct.Pin = enable_current_Pin;
-
 
639
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-
 
640
  GPIO_InitStruct.Pull = GPIO_NOPULL;
-
 
641
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
-
 
642
  HAL_GPIO_Init(enable_current_GPIO_Port, &GPIO_InitStruct);
-
 
643
 
565
  /*Configure GPIO pins : enableCurrent_Pin step2N_Pin step1N_Pin step1P_Pin */
644
  /*Configure GPIO pins : step2N_Pin step1N_Pin step1P_Pin */
566
  GPIO_InitStruct.Pin = enableCurrent_Pin|step2N_Pin|step1N_Pin|step1P_Pin;
645
  GPIO_InitStruct.Pin = step2N_Pin|step1N_Pin|step1P_Pin;
567
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
646
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
568
  GPIO_InitStruct.Pull = GPIO_NOPULL;
647
  GPIO_InitStruct.Pull = GPIO_NOPULL;
569
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
648
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
570
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
649
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
571
 
650
 
Line 608... Line 687...
608
  /* User can add his own implementation to report the file name and line number,
687
  /* User can add his own implementation to report the file name and line number,
609
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
688
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
610
  /* USER CODE END 6 */
689
  /* USER CODE END 6 */
611
}
690
}
612
#endif /* USE_FULL_ASSERT */
691
#endif /* USE_FULL_ASSERT */
613
 
-
 
614
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
-