Advertisement
Forzaferrarileo

tim_sync main.c

Apr 21st, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.99 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2016 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f4xx_hal.h"
  35.  
  36. /* USER CODE BEGIN Includes */
  37.  
  38. /* USER CODE END Includes */
  39.  
  40. /* Private variables ---------------------------------------------------------*/
  41. TIM_HandleTypeDef htim3;
  42. TIM_HandleTypeDef htim4;
  43.  
  44. /* USER CODE BEGIN PV */
  45. /* Private variables ---------------------------------------------------------*/
  46.  
  47. /* USER CODE END PV */
  48.  
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. static void MX_GPIO_Init(void);
  52. static void MX_TIM3_Init(void);
  53. static void MX_TIM4_Init(void);
  54.  
  55. /* USER CODE BEGIN PFP */
  56. /* Private function prototypes -----------------------------------------------*/
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* USER CODE BEGIN 0 */
  61.  
  62. /* USER CODE END 0 */
  63.  
  64. int main(void)
  65. {
  66.  
  67.   /* USER CODE BEGIN 1 */
  68.  
  69.   /* USER CODE END 1 */
  70.  
  71.   /* MCU Configuration----------------------------------------------------------*/
  72.  
  73.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  74.   HAL_Init();
  75.  
  76.   /* Configure the system clock */
  77.   SystemClock_Config();
  78.  
  79.   /* Initialize all configured peripherals */
  80.   MX_GPIO_Init();
  81.   MX_TIM3_Init();
  82.   MX_TIM4_Init();
  83.  
  84.   /* USER CODE BEGIN 2 */
  85.  
  86.   /* USER CODE END 2 */
  87.  
  88.   /* Infinite loop */
  89.   /* USER CODE BEGIN WHILE */
  90.   while (1)
  91.   {
  92.   /* USER CODE END WHILE */
  93.  
  94.   /* USER CODE BEGIN 3 */
  95.  
  96.   }
  97.   /* USER CODE END 3 */
  98.  
  99. }
  100.  
  101. /** System Clock Configuration
  102. */
  103. void SystemClock_Config(void)
  104. {
  105.  
  106.   RCC_OscInitTypeDef RCC_OscInitStruct;
  107.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  108.  
  109.   __PWR_CLK_ENABLE();
  110.  
  111.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  112.  
  113.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  114.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  115.   RCC_OscInitStruct.HSICalibrationValue = 16;
  116.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  117.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  118.   RCC_OscInitStruct.PLL.PLLM = 16;
  119.   RCC_OscInitStruct.PLL.PLLN = 336;
  120.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  121.   RCC_OscInitStruct.PLL.PLLQ = 7;
  122.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  123.  
  124.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  125.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  126.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  127.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  128.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  129.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  130.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  131.  
  132.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  133.  
  134.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  135.  
  136.   /* SysTick_IRQn interrupt configuration */
  137.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  138. }
  139.  
  140. /* TIM3 init function */
  141. void MX_TIM3_Init(void)
  142. {
  143.  
  144.   TIM_ClockConfigTypeDef sClockSourceConfig;
  145.   TIM_MasterConfigTypeDef sMasterConfig;
  146.  
  147.   htim3.Instance = TIM3;
  148.   htim3.Init.Prescaler = 1000;
  149.   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  150.   htim3.Init.Period = 65535;
  151.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  152.   HAL_TIM_Base_Init(&htim3);
  153.  
  154.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  155.   HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
  156.  
  157.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  158.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
  159.   HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
  160.  
  161. }
  162.  
  163. /* TIM4 init function */
  164. void MX_TIM4_Init(void)
  165. {
  166.  
  167.   TIM_SlaveConfigTypeDef sSlaveConfig;
  168.   TIM_MasterConfigTypeDef sMasterConfig;
  169.  
  170.   htim4.Instance = TIM4;
  171.   htim4.Init.Prescaler = 0;
  172.   htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  173.   htim4.Init.Period = 10;
  174.   htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  175.   HAL_TIM_Base_Init(&htim4);
  176.   HAL_TIM_Base_Start_IT(&htim4);
  177.  
  178.   sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  179.   sSlaveConfig.InputTrigger = TIM_TS_ITR2;
  180.   HAL_TIM_SlaveConfigSynchronization_IT(&htim4, &sSlaveConfig);
  181.  
  182. }
  183.  
  184. /** Configure pins as
  185.         * Analog
  186.         * Input
  187.         * Output
  188.         * EVENT_OUT
  189.         * EXTI
  190.      PA2   ------> USART2_TX
  191.      PA3   ------> USART2_RX
  192. */
  193. void MX_GPIO_Init(void)
  194. {
  195.  
  196.   GPIO_InitTypeDef GPIO_InitStruct;
  197.  
  198.   /* GPIO Ports Clock Enable */
  199.   __GPIOC_CLK_ENABLE();
  200.   __GPIOH_CLK_ENABLE();
  201.   __GPIOA_CLK_ENABLE();
  202.   __GPIOB_CLK_ENABLE();
  203.  
  204.   /*Configure GPIO pin : B1_Pin */
  205.   GPIO_InitStruct.Pin = B1_Pin;
  206.   GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  207.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  208.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  209.  
  210.   /*Configure GPIO pins : USART_TX_Pin USART_RX_Pin */
  211.   GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin;
  212.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  213.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  214.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  215.   GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  216.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  217.  
  218.   /*Configure GPIO pin : LD2_Pin */
  219.   GPIO_InitStruct.Pin = LD2_Pin;
  220.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  221.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  222.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  223.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  224.  
  225.   /*Configure GPIO pin Output Level */
  226.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  227.  
  228. }
  229.  
  230. void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim){
  231.  
  232.         HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
  233.  
  234. }
  235.  
  236. /* USER CODE BEGIN 4 */
  237.  
  238. /* USER CODE END 4 */
  239.  
  240. #ifdef USE_FULL_ASSERT
  241.  
  242. /**
  243.    * @brief Reports the name of the source file and the source line number
  244.    * where the assert_param error has occurred.
  245.    * @param file: pointer to the source file name
  246.    * @param line: assert_param error line source number
  247.    * @retval None
  248.    */
  249. void assert_failed(uint8_t* file, uint32_t line)
  250. {
  251.   /* USER CODE BEGIN 6 */
  252.   /* User can add his own implementation to report the file name and line number,
  253.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  254.   /* USER CODE END 6 */
  255.  
  256. }
  257.  
  258. #endif
  259.  
  260. /**
  261.   * @}
  262.   */
  263.  
  264. /**
  265.   * @}
  266. */
  267.  
  268. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement