Subversion Repositories FuelGauge

Rev

Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; 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.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  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 ---------------------------------------------------------*/
  44. ADC_HandleTypeDef hadc;
  45. DMA_HandleTypeDef hdma_adc;
  46.  
  47. TIM_HandleTypeDef htim1;
  48. TIM_HandleTypeDef htim3;
  49.  
  50. UART_HandleTypeDef huart1;
  51.  
  52. /* USER CODE BEGIN PV */
  53. #define NUM_SAMPLES 8
  54. uint32_t ADC_Samples [ NUM_SAMPLES ];
  55.  
  56.  
  57. /* USER CODE END PV */
  58.  
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. static void MX_GPIO_Init(void);
  62. static void MX_DMA_Init(void);
  63. static void MX_ADC_Init(void);
  64. static void MX_TIM1_Init(void);
  65. static void MX_USART1_UART_Init(void);
  66. static void MX_TIM3_Init(void);
  67. /* USER CODE BEGIN PFP */
  68.  
  69. /* USER CODE END PFP */
  70.  
  71. /* Private user code ---------------------------------------------------------*/
  72. /* USER CODE BEGIN 0 */
  73.  
  74. void setDrive1(uint8_t bit) {
  75.         if (bit) {
  76.                 HAL_GPIO_WritePin(step1P_GPIO_Port, step1P_Pin,GPIO_PIN_SET);
  77.                 HAL_GPIO_WritePin(step1N_GPIO_Port, step1N_Pin,GPIO_PIN_RESET);
  78.         } else {
  79.                 HAL_GPIO_WritePin(step1P_GPIO_Port, step1P_Pin,GPIO_PIN_RESET);
  80.                 HAL_GPIO_WritePin(step1N_GPIO_Port,step1N_Pin,GPIO_PIN_SET);
  81.         }
  82. }
  83.  
  84. void setDrive2(uint8_t bit) {
  85.         if (bit) {
  86.                 HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin,GPIO_PIN_SET);
  87.                 HAL_GPIO_WritePin(step2N_GPIO_Port, step2N_Pin,GPIO_PIN_RESET);
  88.         } else {
  89.                 HAL_GPIO_WritePin(step2P_GPIO_Port, step2P_Pin,GPIO_PIN_RESET);
  90.                 HAL_GPIO_WritePin(step2N_GPIO_Port,step2N_Pin,GPIO_PIN_SET);
  91.         }
  92. }
  93.  
  94.  
  95. //
  96. int count = 0;
  97. int origin = 0;
  98.  
  99. void moveGauge(int target)
  100. {
  101.  
  102.         unsigned const fast = 3;
  103.         unsigned const slow = 20;
  104.         unsigned const range = slow - fast;
  105.         unsigned del = fast;
  106.         int step = 1;
  107.         while (count != target) {
  108.  
  109.                 // use bit mask, so it works for negative count values ..
  110.                 switch (count & 3) {
  111.                 case 0:
  112.                         setDrive1(1);
  113.                         setDrive2(0);
  114.                         break;
  115.                 case 1:
  116.                         setDrive1(1);
  117.                         setDrive2(1);
  118.                         break;
  119.                 case 2:
  120.                         setDrive1(0);
  121.                         setDrive2(1);
  122.                         break;
  123.                 case 3:
  124.                         setDrive1(0);
  125.                         setDrive2(0);
  126.                         break;
  127.                 }
  128.  
  129.                 // all this calculates minimum distance from
  130.                 // target or origin
  131.                 int d1 = count - origin;
  132.                 if (d1 < 0)
  133.                         d1 = -d1;
  134.                 int d2 = count - target;
  135.                 if (d2 < 0)
  136.                         d2 = -d2;
  137.                 // finally, minimum distance
  138.                 int dist = d1 < d2 ? d1 : d2;
  139.  
  140.                 del = fast;
  141.                 if (dist < range) // inside lower bound of distance
  142.                                 {
  143.                         del = slow - dist;
  144.                 }
  145.                 HAL_Delay(del);
  146.  
  147.                 if (count < target) {
  148.                         step = 1;
  149.                 }
  150.                 if (count > target) {
  151.                         step = -1;
  152.                 }
  153.                 if (count == target) {
  154.                         step = 0;
  155.                 }
  156.                 count = count + step;
  157.  
  158.         }
  159.  
  160. }
  161.  
  162. // move gauge back to zero position
  163. void resetGauge()
  164. {
  165.         moveGauge(-600);
  166.         count=0;
  167.         origin = 0;
  168. }
  169.  
  170. /* USER CODE END 0 */
  171.  
  172. /**
  173.   * @brief  The application entry point.
  174.   * @retval int
  175.   */
  176. int main(void)
  177. {
  178.   /* USER CODE BEGIN 1 */
  179. //  half degree step  315 degree movement
  180. #define GAUGE_MAX 315*2
  181. #define GAUGE_MIN 0
  182.  
  183.  
  184.   /* USER CODE END 1 */
  185.  
  186.  
  187.   /* MCU Configuration--------------------------------------------------------*/
  188.  
  189.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  190.   HAL_Init();
  191.  
  192.   /* USER CODE BEGIN Init */
  193.  
  194.   /* USER CODE END Init */
  195.  
  196.   /* Configure the system clock */
  197.   SystemClock_Config();
  198.  
  199.   /* USER CODE BEGIN SysInit */
  200.  
  201.   /* USER CODE END SysInit */
  202.  
  203.   /* Initialize all configured peripherals */
  204.   MX_GPIO_Init();
  205.   MX_DMA_Init();
  206.   MX_ADC_Init();
  207.   MX_TIM1_Init();
  208.   MX_USART1_UART_Init();
  209.   MX_TIM3_Init();
  210.   /* USER CODE BEGIN 2 */
  211.  
  212.  
  213.   int i;
  214.   for (i=0 ; i< NUM_SAMPLES; i++)
  215.           ADC_Samples[i] =0;
  216.  
  217.   HAL_ADC_MspInit(&hadc);
  218.  
  219.   HAL_ADC_Start_DMA(&hadc, ADC_Samples, NUM_SAMPLES);
  220.   HAL_ADC_Start_IT(&hadc);
  221.  
  222.      // timer 3 triggers the ADC
  223.      HAL_TIM_Base_MspInit(&htim3);
  224.         HAL_TIM_Base_Start_IT(&htim3);
  225.  
  226.   resetGauge();
  227.  
  228.  
  229.  
  230.  
  231.  
  232.   /* USER CODE END 2 */
  233.  
  234.  
  235.  
  236.   /* Infinite loop */
  237.   /* USER CODE BEGIN WHILE */
  238.   while (1)
  239.   {
  240.     /* USER CODE END WHILE */
  241.  
  242.     /* USER CODE BEGIN 3 */
  243.   }
  244.   /* USER CODE END 3 */
  245. }
  246.  
  247. /**
  248.   * @brief System Clock Configuration
  249.   * @retval None
  250.   */
  251. void SystemClock_Config(void)
  252. {
  253.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  254.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  255.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  256.  
  257.   /** Initializes the CPU, AHB and APB busses clocks
  258.   */
  259.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
  260.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  261.   RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  262.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  263.   RCC_OscInitStruct.HSI14CalibrationValue = 16;
  264.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  265.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  266.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
  267.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  268.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  269.   {
  270.     Error_Handler();
  271.   }
  272.   /** Initializes the CPU, AHB and APB busses clocks
  273.   */
  274.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  275.                               |RCC_CLOCKTYPE_PCLK1;
  276.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  277.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  278.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  279.  
  280.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  281.   {
  282.     Error_Handler();
  283.   }
  284.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  285.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  286.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  287.   {
  288.     Error_Handler();
  289.   }
  290. }
  291.  
  292. /**
  293.   * @brief ADC Initialization Function
  294.   * @param None
  295.   * @retval None
  296.   */
  297. static void MX_ADC_Init(void)
  298. {
  299.  
  300.   /* USER CODE BEGIN ADC_Init 0 */
  301.  
  302.   /* USER CODE END ADC_Init 0 */
  303.  
  304.   ADC_ChannelConfTypeDef sConfig = {0};
  305.  
  306.   /* USER CODE BEGIN ADC_Init 1 */
  307.  
  308.   /* USER CODE END ADC_Init 1 */
  309.   /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  310.   */
  311.   hadc.Instance = ADC1;
  312.   hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  313.   hadc.Init.Resolution = ADC_RESOLUTION_12B;
  314.   hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  315.   hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  316.   hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  317.   hadc.Init.LowPowerAutoWait = DISABLE;
  318.   hadc.Init.LowPowerAutoPowerOff = DISABLE;
  319.   hadc.Init.ContinuousConvMode = DISABLE;
  320.   hadc.Init.DiscontinuousConvMode = DISABLE;
  321.   hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
  322.   hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  323.   hadc.Init.DMAContinuousRequests = ENABLE;
  324.   hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  325.   if (HAL_ADC_Init(&hadc) != HAL_OK)
  326.   {
  327.     Error_Handler();
  328.   }
  329.   /** Configure for the selected ADC regular channel to be converted.
  330.   */
  331.   sConfig.Channel = ADC_CHANNEL_0;
  332.   sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  333.   sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
  334.   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  335.   {
  336.     Error_Handler();
  337.   }
  338.   /* USER CODE BEGIN ADC_Init 2 */
  339.  
  340.   /* USER CODE END ADC_Init 2 */
  341.  
  342. }
  343.  
  344. /**
  345.   * @brief TIM1 Initialization Function
  346.   * @param None
  347.   * @retval None
  348.   */
  349. static void MX_TIM1_Init(void)
  350. {
  351.  
  352.   /* USER CODE BEGIN TIM1_Init 0 */
  353.  
  354.   /* USER CODE END TIM1_Init 0 */
  355.  
  356.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  357.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  358.  
  359.   /* USER CODE BEGIN TIM1_Init 1 */
  360.  
  361.   /* USER CODE END TIM1_Init 1 */
  362.   htim1.Instance = TIM1;
  363.   htim1.Init.Prescaler = 0;
  364.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  365.   htim1.Init.Period = 0;
  366.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  367.   htim1.Init.RepetitionCounter = 0;
  368.   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  369.   if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  370.   {
  371.     Error_Handler();
  372.   }
  373.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  374.   if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  375.   {
  376.     Error_Handler();
  377.   }
  378.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  379.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  380.   if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  381.   {
  382.     Error_Handler();
  383.   }
  384.   /* USER CODE BEGIN TIM1_Init 2 */
  385.  
  386.   /* USER CODE END TIM1_Init 2 */
  387.  
  388. }
  389.  
  390. /**
  391.   * @brief TIM3 Initialization Function
  392.   * @param None
  393.   * @retval None
  394.   */
  395. static void MX_TIM3_Init(void)
  396. {
  397.  
  398.   /* USER CODE BEGIN TIM3_Init 0 */
  399.  
  400.   /* USER CODE END TIM3_Init 0 */
  401.  
  402.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  403.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  404.  
  405.   /* USER CODE BEGIN TIM3_Init 1 */
  406.  
  407.   /* USER CODE END TIM3_Init 1 */
  408.   htim3.Instance = TIM3;
  409.   htim3.Init.Prescaler = 32;
  410.   htim3.Init.CounterMode = TIM_COUNTERMODE_DOWN;
  411.   htim3.Init.Period = 10000;
  412.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  413.   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  414.   if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  415.   {
  416.     Error_Handler();
  417.   }
  418.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  419.   if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  420.   {
  421.     Error_Handler();
  422.   }
  423.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  424.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  425.   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  426.   {
  427.     Error_Handler();
  428.   }
  429.   /* USER CODE BEGIN TIM3_Init 2 */
  430.  
  431.   /* USER CODE END TIM3_Init 2 */
  432.  
  433. }
  434.  
  435. /**
  436.   * @brief USART1 Initialization Function
  437.   * @param None
  438.   * @retval None
  439.   */
  440. static void MX_USART1_UART_Init(void)
  441. {
  442.  
  443.   /* USER CODE BEGIN USART1_Init 0 */
  444.  
  445.   /* USER CODE END USART1_Init 0 */
  446.  
  447.   /* USER CODE BEGIN USART1_Init 1 */
  448.  
  449.   /* USER CODE END USART1_Init 1 */
  450.   huart1.Instance = USART1;
  451.   huart1.Init.BaudRate = 38400;
  452.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  453.   huart1.Init.StopBits = UART_STOPBITS_1;
  454.   huart1.Init.Parity = UART_PARITY_NONE;
  455.   huart1.Init.Mode = UART_MODE_TX_RX;
  456.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  457.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  458.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  459.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  460.   if (HAL_UART_Init(&huart1) != HAL_OK)
  461.   {
  462.     Error_Handler();
  463.   }
  464.   /* USER CODE BEGIN USART1_Init 2 */
  465.  
  466.   /* USER CODE END USART1_Init 2 */
  467.  
  468. }
  469.  
  470. /**
  471.   * Enable DMA controller clock
  472.   */
  473. static void MX_DMA_Init(void)
  474. {
  475.  
  476.   /* DMA controller clock enable */
  477.   __HAL_RCC_DMA1_CLK_ENABLE();
  478.  
  479.   /* DMA interrupt init */
  480.   /* DMA1_Channel1_IRQn interrupt configuration */
  481.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  482.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  483.  
  484. }
  485.  
  486. /**
  487.   * @brief GPIO Initialization Function
  488.   * @param None
  489.   * @retval None
  490.   */
  491. static void MX_GPIO_Init(void)
  492. {
  493.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  494.  
  495.   /* GPIO Ports Clock Enable */
  496.   __HAL_RCC_GPIOF_CLK_ENABLE();
  497.   __HAL_RCC_GPIOA_CLK_ENABLE();
  498.   __HAL_RCC_GPIOB_CLK_ENABLE();
  499.  
  500.   /*Configure GPIO pin Output Level */
  501.   HAL_GPIO_WritePin(GPIOA, enableCurrent_Pin|step2N_Pin|step2P_Pin|step1N_Pin, GPIO_PIN_RESET);
  502.  
  503.   /*Configure GPIO pin Output Level */
  504.   HAL_GPIO_WritePin(step1P_GPIO_Port, step1P_Pin, GPIO_PIN_RESET);
  505.  
  506.   /*Configure GPIO pins : enableCurrent_Pin step2N_Pin step2P_Pin step1N_Pin */
  507.   GPIO_InitStruct.Pin = enableCurrent_Pin|step2N_Pin|step2P_Pin|step1N_Pin;
  508.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  509.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  510.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  511.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  512.  
  513.   /*Configure GPIO pin : step1P_Pin */
  514.   GPIO_InitStruct.Pin = step1P_Pin;
  515.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  516.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  517.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  518.   HAL_GPIO_Init(step1P_GPIO_Port, &GPIO_InitStruct);
  519.  
  520. }
  521.  
  522. /* USER CODE BEGIN 4 */
  523.  
  524. /* USER CODE END 4 */
  525.  
  526. /**
  527.   * @brief  This function is executed in case of error occurrence.
  528.   * @retval None
  529.   */
  530. void Error_Handler(void)
  531. {
  532.   /* USER CODE BEGIN Error_Handler_Debug */
  533.   /* User can add his own implementation to report the HAL error return state */
  534.  
  535.   /* USER CODE END Error_Handler_Debug */
  536. }
  537.  
  538. #ifdef  USE_FULL_ASSERT
  539. /**
  540.   * @brief  Reports the name of the source file and the source line number
  541.   *         where the assert_param error has occurred.
  542.   * @param  file: pointer to the source file name
  543.   * @param  line: assert_param error line source number
  544.   * @retval None
  545.   */
  546. void assert_failed(char *file, uint32_t line)
  547. {
  548.   /* USER CODE BEGIN 6 */
  549.   /* User can add his own implementation to report the file name and line number,
  550.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  551.   /* USER CODE END 6 */
  552. }
  553. #endif /* USE_FULL_ASSERT */
  554.  
  555. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  556.