Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f4xx_hal.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- TIM_HandleTypeDef htim2;
- TIM_HandleTypeDef htim3;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_USART2_UART_Init(void);
- static void MX_TIM2_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- #define RC_CHANNEL1_PORT GPIOA
- #define RC_CHANNEL1_PIN GPIO_PIN_0
- #define RC_CHANNEL2_PORT GPIOA
- #define RC_CHANNEL2_PIN GPIO_PIN_1
- #define RC_CHANNEL3_PORT GPIOB
- #define RC_CHANNEL3_PIN GPIO_PIN_10
- #define RC_CHANNEL4_PORT GPIOB
- #define RC_CHANNEL4_PIN GPIO_PIN_1
- int32_t rc_cnt = 0;
- int32_t rc_acc[4] = {0};
- int32_t rc_z_control_flag = 1;
- extern int32_t rc_cal_flag;
- extern int32_t rc_enable_motor;
- extern int32_t fly_ready;
- volatile int rc_timeout; // R/C timeout counter
- char rc_connection_flag; // R/C connection status
- char rc_flag[4]; // flag for received input capture interrupt count
- /* timer data for rising and falling edge and pulse width */
- int rc_t_rise[4], rc_t_fall[4], rc_t[4];
- GPIO_TypeDef* RC_Channel_Ports[4] =
- {
- RC_CHANNEL1_PORT,
- RC_CHANNEL2_PORT,
- RC_CHANNEL3_PORT,
- RC_CHANNEL4_PORT
- };
- uint16_t RC_Channel_Pins[4] =
- {
- RC_CHANNEL1_PIN,
- RC_CHANNEL2_PIN,
- RC_CHANNEL3_PIN,
- RC_CHANNEL4_PIN
- };
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART2_UART_Init();
- MX_TIM2_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_1);
- HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_2);
- HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_3);
- HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_4);
- int i;
- for (i=0;i<4;i++)
- {
- rc_flag[i] = 0;
- rc_t_rise[i] = 0;
- rc_t_fall[i] = 0;
- rc_t[i] = 0;
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 16;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* TIM3 init function */
- //static void MX_TIM3_Init(void)
- //{
- // TIM_ClockConfigTypeDef sClockSourceConfig;
- // TIM_MasterConfigTypeDef sMasterConfig;
- // TIM_IC_InitTypeDef sConfigIC;
- // htim3.Instance = TIM3;
- // htim3.Init.Prescaler = 48;
- // htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
- // htim3.Init.Period = 65535;
- // htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- // if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- // if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- // sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- // if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;
- // sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
- // sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
- // sConfigIC.ICFilter = 0;
- // if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_3) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- // if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_4) != HAL_OK)
- // {
- // _Error_Handler(__FILE__, __LINE__);
- // }
- //}
- void MX_TIM2_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig;
- TIM_MasterConfigTypeDef sMasterConfig;
- TIM_IC_InitTypeDef sConfigIC;
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = 20;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim2.Init.Period = 32767;
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- HAL_TIM_Base_Init(&htim2);
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
- HAL_TIM_IC_Init(&htim2);
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
- sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;
- sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
- sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
- sConfigIC.ICFilter = 0;
- HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);
- HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2);
- HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_3);
- HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_4);
- }
- /* USART2 init function */
- static void MX_USART2_UART_Init(void)
- {
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : LD2_Pin */
- GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
- {
- int32_t timcnt, idx;
- // save the counter data
- switch (htim->Channel)
- {
- case HAL_TIM_ACTIVE_CHANNEL_1: idx = 0; timcnt = htim->Instance->CCR1; break;
- case HAL_TIM_ACTIVE_CHANNEL_2: idx = 1; timcnt = htim->Instance->CCR2; break;
- case HAL_TIM_ACTIVE_CHANNEL_3: idx = 2; timcnt = htim->Instance->CCR3; break;
- case HAL_TIM_ACTIVE_CHANNEL_4: idx = 3; timcnt = htim->Instance->CCR4; break;
- default: idx = -1;
- }
- if (idx != -1)
- {
- rc_timeout = 0; // Reset timeout count
- if (rc_flag[idx] < 2)
- rc_flag[idx]++;
- if (HAL_GPIO_ReadPin(RC_Channel_Ports[idx], RC_Channel_Pins[idx]) == GPIO_PIN_SET)
- { // rising edge
- rc_t_rise[idx] = timcnt;
- }
- else
- { // falling edge
- rc_t_fall[idx] = timcnt;
- if (rc_flag[idx] >= 2) // be sure to calculate signal width after at least a pair of interrupt
- {
- if (rc_t_fall[idx] > rc_t_rise[idx])
- rc_t[idx] = rc_t_fall[idx] - rc_t_rise[idx];
- else
- rc_t[idx] = rc_t_fall[idx] - rc_t_rise[idx] + 32768;
- }
- }
- }
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement