Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.47 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f4xx_hal.h"
  4.  
  5. /* USER CODE BEGIN Includes */
  6.  
  7. /* USER CODE END Includes */
  8.  
  9. /* Private variables ---------------------------------------------------------*/
  10. TIM_HandleTypeDef htim2;
  11. TIM_HandleTypeDef htim3;
  12. UART_HandleTypeDef huart2;
  13.  
  14. /* USER CODE BEGIN PV */
  15. /* Private variables ---------------------------------------------------------*/
  16.  
  17. /* USER CODE END PV */
  18.  
  19. /* Private function prototypes -----------------------------------------------*/
  20. void SystemClock_Config(void);
  21. static void MX_GPIO_Init(void);
  22. static void MX_USART2_UART_Init(void);
  23. static void MX_TIM2_Init(void);
  24.  
  25. /* USER CODE BEGIN PFP */
  26. /* Private function prototypes -----------------------------------------------*/
  27.  
  28. /* USER CODE END PFP */
  29.  
  30. /* USER CODE BEGIN 0 */
  31.  
  32. #define RC_CHANNEL1_PORT    GPIOA
  33. #define RC_CHANNEL1_PIN     GPIO_PIN_0
  34. #define RC_CHANNEL2_PORT    GPIOA
  35. #define RC_CHANNEL2_PIN     GPIO_PIN_1
  36. #define RC_CHANNEL3_PORT    GPIOB
  37. #define RC_CHANNEL3_PIN     GPIO_PIN_10
  38. #define RC_CHANNEL4_PORT    GPIOB
  39. #define RC_CHANNEL4_PIN     GPIO_PIN_1
  40.  
  41.  
  42. int32_t rc_cnt = 0;
  43. int32_t rc_acc[4] = {0};
  44.  
  45. int32_t rc_z_control_flag = 1;
  46.  
  47. extern int32_t rc_cal_flag;
  48. extern int32_t rc_enable_motor;
  49. extern int32_t fly_ready;
  50.  
  51. volatile int rc_timeout;    // R/C timeout counter
  52. char rc_connection_flag;    // R/C connection status
  53. char rc_flag[4];            // flag for received input capture interrupt count
  54. /* timer data for rising and falling edge and pulse width */
  55. int rc_t_rise[4], rc_t_fall[4], rc_t[4];
  56.  
  57. GPIO_TypeDef* RC_Channel_Ports[4] =
  58.     {
  59.       RC_CHANNEL1_PORT,
  60.       RC_CHANNEL2_PORT,
  61.       RC_CHANNEL3_PORT,
  62.       RC_CHANNEL4_PORT
  63.     };
  64.  
  65. uint16_t RC_Channel_Pins[4] =
  66.     {
  67.       RC_CHANNEL1_PIN,
  68.       RC_CHANNEL2_PIN,
  69.       RC_CHANNEL3_PIN,
  70.       RC_CHANNEL4_PIN
  71.     };
  72. /* USER CODE END 0 */
  73.  
  74. /**
  75.   * @brief  The application entry point.
  76.   *
  77.   * @retval None
  78.   */
  79. int main(void)
  80. {
  81.   /* USER CODE BEGIN 1 */
  82.  
  83.   /* USER CODE END 1 */
  84.  
  85.   /* MCU Configuration----------------------------------------------------------*/
  86.  
  87.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  88.   HAL_Init();
  89.  
  90.   /* USER CODE BEGIN Init */
  91.  
  92.   /* USER CODE END Init */
  93.  
  94.   /* Configure the system clock */
  95.   SystemClock_Config();
  96.  
  97.   /* USER CODE BEGIN SysInit */
  98.  
  99.   /* USER CODE END SysInit */
  100.  
  101.   /* Initialize all configured peripherals */
  102.   MX_GPIO_Init();
  103.   MX_USART2_UART_Init();
  104.   MX_TIM2_Init();
  105.   /* USER CODE BEGIN 2 */
  106.     HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_1);
  107.     HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_2);
  108.     HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_3);
  109.     HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_4);
  110.     int i;
  111.     for (i=0;i<4;i++)
  112.   {
  113.     rc_flag[i] = 0;
  114.     rc_t_rise[i] = 0;
  115.     rc_t_fall[i] = 0;
  116.     rc_t[i] = 0;
  117.   }
  118.   /* USER CODE END 2 */
  119.  
  120.   /* Infinite loop */
  121.   /* USER CODE BEGIN WHILE */
  122.   while (1)
  123.   {
  124.  
  125.   /* USER CODE END WHILE */
  126.  
  127.   /* USER CODE BEGIN 3 */
  128.  
  129.   }
  130.   /* USER CODE END 3 */
  131.  
  132. }
  133.  
  134. /**
  135.   * @brief System Clock Configuration
  136.   * @retval None
  137.   */
  138. void SystemClock_Config(void)
  139. {
  140.  
  141.   RCC_OscInitTypeDef RCC_OscInitStruct;
  142.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  143.  
  144.     /**Configure the main internal regulator output voltage
  145.     */
  146.   __HAL_RCC_PWR_CLK_ENABLE();
  147.  
  148.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  149.  
  150.     /**Initializes the CPU, AHB and APB busses clocks
  151.     */
  152.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  153.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  154.   RCC_OscInitStruct.HSICalibrationValue = 16;
  155.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  156.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  157.   RCC_OscInitStruct.PLL.PLLM = 16;
  158.   RCC_OscInitStruct.PLL.PLLN = 336;
  159.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  160.   RCC_OscInitStruct.PLL.PLLQ = 7;
  161.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  162.   {
  163.     _Error_Handler(__FILE__, __LINE__);
  164.   }
  165.  
  166.     /**Initializes the CPU, AHB and APB busses clocks
  167.     */
  168.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  169.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  170.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  171.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  172.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  173.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  174.  
  175.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  176.   {
  177.     _Error_Handler(__FILE__, __LINE__);
  178.   }
  179.  
  180.     /**Configure the Systick interrupt time
  181.     */
  182.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  183.  
  184.     /**Configure the Systick
  185.     */
  186.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  187.  
  188.   /* SysTick_IRQn interrupt configuration */
  189.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  190. }
  191.  
  192. /* TIM3 init function */
  193. //static void MX_TIM3_Init(void)
  194. //{
  195.  
  196. //  TIM_ClockConfigTypeDef sClockSourceConfig;
  197. //  TIM_MasterConfigTypeDef sMasterConfig;
  198. //  TIM_IC_InitTypeDef sConfigIC;
  199.  
  200. //  htim3.Instance = TIM3;
  201. //  htim3.Init.Prescaler = 48;
  202. //  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  203. //  htim3.Init.Period = 65535;
  204. //  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  205. //  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  206. //  {
  207. //    _Error_Handler(__FILE__, __LINE__);
  208. //  }
  209.  
  210. //  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  211. //  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  212. //  {
  213. //    _Error_Handler(__FILE__, __LINE__);
  214. //  }
  215.  
  216. //  if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
  217. //  {
  218. //    _Error_Handler(__FILE__, __LINE__);
  219. //  }
  220.  
  221. //  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  222. //  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  223. //  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  224. //  {
  225. //    _Error_Handler(__FILE__, __LINE__);
  226. //  }
  227.  
  228. //  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;
  229. //  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  230. //  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  231. //  sConfigIC.ICFilter = 0;
  232. //  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  233. //  {
  234. //    _Error_Handler(__FILE__, __LINE__);
  235. //  }
  236.  
  237. //  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
  238. //  {
  239. //    _Error_Handler(__FILE__, __LINE__);
  240. //  }
  241.  
  242. //  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_3) != HAL_OK)
  243. //  {
  244. //    _Error_Handler(__FILE__, __LINE__);
  245. //  }
  246.  
  247. //  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_4) != HAL_OK)
  248. //  {
  249. //    _Error_Handler(__FILE__, __LINE__);
  250. //  }
  251.  
  252. //}
  253.  
  254. void MX_TIM2_Init(void)
  255. {
  256.  
  257.   TIM_ClockConfigTypeDef sClockSourceConfig;
  258.   TIM_MasterConfigTypeDef sMasterConfig;
  259.   TIM_IC_InitTypeDef sConfigIC;
  260.  
  261.   htim2.Instance = TIM2;
  262.   htim2.Init.Prescaler = 20;
  263.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  264.   htim2.Init.Period = 32767;
  265.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  266.   HAL_TIM_Base_Init(&htim2);
  267.  
  268.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  269.   HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  270.  
  271.   HAL_TIM_IC_Init(&htim2);
  272.  
  273.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  274.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  275.   HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
  276.  
  277.   sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;
  278.   sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  279.   sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  280.   sConfigIC.ICFilter = 0;
  281.   HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);
  282.  
  283.   HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2);
  284.  
  285.   HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_3);
  286.  
  287.   HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_4);
  288.  
  289. }
  290.  
  291. /* USART2 init function */
  292. static void MX_USART2_UART_Init(void)
  293. {
  294.  
  295.   huart2.Instance = USART2;
  296.   huart2.Init.BaudRate = 115200;
  297.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  298.   huart2.Init.StopBits = UART_STOPBITS_1;
  299.   huart2.Init.Parity = UART_PARITY_NONE;
  300.   huart2.Init.Mode = UART_MODE_TX_RX;
  301.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  302.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  303.   if (HAL_UART_Init(&huart2) != HAL_OK)
  304.   {
  305.     _Error_Handler(__FILE__, __LINE__);
  306.   }
  307.  
  308. }
  309.  
  310. /** Configure pins as
  311.         * Analog
  312.         * Input
  313.         * Output
  314.         * EVENT_OUT
  315.         * EXTI
  316. */
  317. static void MX_GPIO_Init(void)
  318. {
  319.  
  320.   GPIO_InitTypeDef GPIO_InitStruct;
  321.  
  322.   /* GPIO Ports Clock Enable */
  323.   __HAL_RCC_GPIOC_CLK_ENABLE();
  324.   __HAL_RCC_GPIOH_CLK_ENABLE();
  325.   __HAL_RCC_GPIOA_CLK_ENABLE();
  326.   __HAL_RCC_GPIOB_CLK_ENABLE();
  327.  
  328.   /*Configure GPIO pin Output Level */
  329.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  330.  
  331.   /*Configure GPIO pin : B1_Pin */
  332.   GPIO_InitStruct.Pin = B1_Pin;
  333.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  334.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  335.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  336.  
  337.   /*Configure GPIO pin : LD2_Pin */
  338.   GPIO_InitStruct.Pin = LD2_Pin;
  339.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  340.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  341.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  342.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  343.  
  344. }
  345.  
  346. /* USER CODE BEGIN 4 */
  347.  
  348. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  349. {
  350.   int32_t timcnt, idx;
  351.  
  352.   // save the counter data
  353.   switch (htim->Channel)
  354.   {
  355.     case HAL_TIM_ACTIVE_CHANNEL_1: idx = 0; timcnt = htim->Instance->CCR1; break;
  356.     case HAL_TIM_ACTIVE_CHANNEL_2: idx = 1; timcnt = htim->Instance->CCR2; break;
  357.     case HAL_TIM_ACTIVE_CHANNEL_3: idx = 2; timcnt = htim->Instance->CCR3; break;
  358.     case HAL_TIM_ACTIVE_CHANNEL_4: idx = 3; timcnt = htim->Instance->CCR4; break;
  359.     default: idx = -1;
  360.   }
  361.  
  362.   if (idx != -1)
  363.   {
  364.     rc_timeout = 0;     // Reset timeout count
  365.     if (rc_flag[idx] < 2)
  366.       rc_flag[idx]++;
  367.     if (HAL_GPIO_ReadPin(RC_Channel_Ports[idx], RC_Channel_Pins[idx]) == GPIO_PIN_SET)
  368.     {   // rising edge
  369.       rc_t_rise[idx] = timcnt;
  370.     }
  371.     else
  372.     {   // falling edge
  373.       rc_t_fall[idx] = timcnt;
  374.       if (rc_flag[idx] >= 2)  // be sure to calculate signal width after at least a pair of interrupt
  375.       {
  376.         if (rc_t_fall[idx] > rc_t_rise[idx])
  377.           rc_t[idx] = rc_t_fall[idx] - rc_t_rise[idx];
  378.         else
  379.           rc_t[idx] = rc_t_fall[idx] - rc_t_rise[idx] + 32768;
  380.       }
  381.  
  382.     }
  383.   }
  384. }
  385.  
  386. /* USER CODE END 4 */
  387.  
  388. /**
  389.   * @brief  This function is executed in case of error occurrence.
  390.   * @param  file: The file name as string.
  391.   * @param  line: The line in file as a number.
  392.   * @retval None
  393.   */
  394. void _Error_Handler(char *file, int line)
  395. {
  396.   /* USER CODE BEGIN Error_Handler_Debug */
  397.   /* User can add his own implementation to report the HAL error return state */
  398.   while(1)
  399.   {
  400.   }
  401.   /* USER CODE END Error_Handler_Debug */
  402. }
  403.  
  404. #ifdef  USE_FULL_ASSERT
  405. /**
  406.   * @brief  Reports the name of the source file and the source line number
  407.   *         where the assert_param error has occurred.
  408.   * @param  file: pointer to the source file name
  409.   * @param  line: assert_param error line source number
  410.   * @retval None
  411.   */
  412. void assert_failed(uint8_t* file, uint32_t line)
  413. {
  414.   /* USER CODE BEGIN 6 */
  415.   /* User can add his own implementation to report the file name and line number,
  416.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  417.   /* USER CODE END 6 */
  418. }
  419. #endif /* USE_FULL_ASSERT */
  420.  
  421. /**
  422.   * @}
  423.   */
  424.  
  425. /**
  426.   * @}
  427.   */
  428.  
  429. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement