Subversion Repositories dualCDC

Rev

Rev 2 | 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. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "usb_device.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "libSerial/serial.h"
  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. UART_HandleTypeDef huart1;
  45. UART_HandleTypeDef huart2;
  46.  
  47. /* USER CODE BEGIN PV */
  48. usart_ctl uc1;
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void
  53. SystemClock_Config (void);
  54. static void
  55. MX_GPIO_Init (void);
  56. static void
  57. MX_USART1_UART_Init (void);
  58. static void
  59. MX_USART2_UART_Init (void);
  60. /* USER CODE BEGIN PFP */
  61.  
  62. /* USER CODE END PFP */
  63.  
  64. /* Private user code ---------------------------------------------------------*/
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70.  * @brief  The application entry point.
  71.  * @retval int
  72.  */
  73. int
  74. main (void)
  75. {
  76.   /* USER CODE BEGIN 1 */
  77.  
  78.   /* USER CODE END 1 */
  79.  
  80.   /* MCU Configuration--------------------------------------------------------*/
  81.  
  82.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83.   HAL_Init ();
  84.  
  85.   /* USER CODE BEGIN Init */
  86.  
  87.   /* USER CODE END Init */
  88.  
  89.   /* Configure the system clock */
  90.   SystemClock_Config ();
  91.  
  92.   /* USER CODE BEGIN SysInit */
  93.  
  94.   /* USER CODE END SysInit */
  95.  
  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init ();
  98.  
  99.   MX_USART1_UART_Init ();
  100.   MX_USART2_UART_Init ();
  101.   MX_USB_DEVICE_Init ();
  102.   /* USER CODE BEGIN 2 */
  103.   HAL_GPIO_WritePin (
  104.   USART1_TXEN_GPIO_Port,
  105.                      USART1_TXEN_Pin, GPIO_PIN_RESET);
  106.  
  107.   HAL_GPIO_WritePin ( USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET);
  108.  
  109.   HAL_Delay (1000);
  110.   HAL_GPIO_WritePin ( USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_SET);
  111.  
  112.   init_usart_ctl (&uc1, &huart1);
  113.  
  114.   HAL_NVIC_EnableIRQ (USART1_IRQn);
  115.  
  116.   /* USER CODE END 2 */
  117.  
  118.   /* Infinite loop */
  119.   /* USER CODE BEGIN WHILE */
  120.   uint8_t cnt = '0';
  121.   uint32_t tim_end = HAL_GetTick ();
  122.   while (1)
  123.     {
  124.       uint16_t chars = CDC_Poll_UART (); // poll current uart
  125.  
  126.       if (chars)
  127.         tim_end = HAL_GetTick () + 50;
  128.  
  129.       HAL_GPIO_WritePin (
  130.           GreenLED_GPIO_Port, GreenLED_Pin,
  131.           HAL_GetTick () > tim_end ? GPIO_PIN_SET : GPIO_PIN_RESET);
  132.  
  133.       HAL_Delay (10);
  134.  
  135.       /* USER CODE END WHILE */
  136.  
  137.       /* USER CODE BEGIN 3 */
  138.     }
  139.   /* USER CODE END 3 */
  140. }
  141.  
  142. /**
  143.  * @brief System Clock Configuration
  144.  * @retval None
  145.  */
  146. void
  147. SystemClock_Config (void)
  148. {
  149.   RCC_OscInitTypeDef RCC_OscInitStruct =
  150.     { 0 };
  151.   RCC_ClkInitTypeDef RCC_ClkInitStruct =
  152.     { 0 };
  153.   RCC_PeriphCLKInitTypeDef PeriphClkInit =
  154.     { 0 };
  155.  
  156.   /** Initializes the RCC Oscillators according to the specified parameters
  157.    * in the RCC_OscInitTypeDef structure.
  158.    */
  159.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  160.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  161.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  162.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  163.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  164.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  165.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  166.   if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK)
  167.     {
  168.       Error_Handler ();
  169.     }
  170.   /** Initializes the CPU, AHB and APB buses clocks
  171.    */
  172.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  173.       | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  174.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  175.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  176.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  177.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  178.  
  179.   if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  180.     {
  181.       Error_Handler ();
  182.     }
  183.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  184.   PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  185.   if (HAL_RCCEx_PeriphCLKConfig (&PeriphClkInit) != HAL_OK)
  186.     {
  187.       Error_Handler ();
  188.     }
  189. }
  190.  
  191. /**
  192.  * @brief USART1 Initialization Function
  193.  * @param None
  194.  * @retval None
  195.  */
  196. static void
  197. MX_USART1_UART_Init (void)
  198. {
  199.  
  200.   /* USER CODE BEGIN USART1_Init 0 */
  201.  
  202.   /* USER CODE END USART1_Init 0 */
  203.  
  204.   /* USER CODE BEGIN USART1_Init 1 */
  205.  
  206.   /* USER CODE END USART1_Init 1 */
  207.   huart1.Instance = USART1;
  208.   huart1.Init.BaudRate = 38400;
  209.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  210.   huart1.Init.StopBits = UART_STOPBITS_1;
  211.   huart1.Init.Parity = UART_PARITY_NONE;
  212.   huart1.Init.Mode = UART_MODE_TX_RX;
  213.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  214.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  215.   if (HAL_UART_Init (&huart1) != HAL_OK)
  216.     {
  217.       Error_Handler ();
  218.     }
  219.   /* USER CODE BEGIN USART1_Init 2 */
  220.  
  221.   /* USER CODE END USART1_Init 2 */
  222.  
  223. }
  224.  
  225. /**
  226.  * @brief USART2 Initialization Function
  227.  * @param None
  228.  * @retval None
  229.  */
  230. static void
  231. MX_USART2_UART_Init (void)
  232. {
  233.  
  234.   /* USER CODE BEGIN USART2_Init 0 */
  235.  
  236.   /* USER CODE END USART2_Init 0 */
  237.  
  238.   /* USER CODE BEGIN USART2_Init 1 */
  239.  
  240.   /* USER CODE END USART2_Init 1 */
  241.   huart2.Instance = USART2;
  242.   huart2.Init.BaudRate = 9600;
  243.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  244.   huart2.Init.StopBits = UART_STOPBITS_1;
  245.   huart2.Init.Parity = UART_PARITY_NONE;
  246.   huart2.Init.Mode = UART_MODE_TX_RX;
  247.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  248.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  249.   if (HAL_UART_Init (&huart2) != HAL_OK)
  250.     {
  251.       Error_Handler ();
  252.     }
  253.   /* USER CODE BEGIN USART2_Init 2 */
  254.  
  255.   /* USER CODE END USART2_Init 2 */
  256.  
  257. }
  258.  
  259. /**
  260.  * @brief GPIO Initialization Function
  261.  * @param None
  262.  * @retval None
  263.  */
  264. static void
  265. MX_GPIO_Init (void)
  266. {
  267.   GPIO_InitTypeDef GPIO_InitStruct =
  268.     { 0 };
  269.  
  270.   /* GPIO Ports Clock Enable */
  271.   __HAL_RCC_GPIOC_CLK_ENABLE();
  272.   __HAL_RCC_GPIOD_CLK_ENABLE();
  273.   __HAL_RCC_GPIOA_CLK_ENABLE();
  274.   __HAL_RCC_GPIOB_CLK_ENABLE();
  275.  
  276.   /*Configure GPIO pin Output Level */
  277.   HAL_GPIO_WritePin (GreenLED_GPIO_Port, GreenLED_Pin, GPIO_PIN_RESET);
  278.  
  279.   /*Configure GPIO pin Output Level */
  280.   HAL_GPIO_WritePin (USART1_TXEN_GPIO_Port, USART1_TXEN_Pin, GPIO_PIN_RESET);
  281.  
  282.   /*Configure GPIO pin Output Level */
  283.   HAL_GPIO_WritePin (USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET);
  284.  
  285.   /*Configure GPIO pin : GreenLED_Pin */
  286.   GPIO_InitStruct.Pin = GreenLED_Pin;
  287.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  288.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  289.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  290.   HAL_GPIO_Init (GreenLED_GPIO_Port, &GPIO_InitStruct);
  291.  
  292.   /*Configure GPIO pin : USART1_TXEN_Pin */
  293.   GPIO_InitStruct.Pin = USART1_TXEN_Pin;
  294.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  295.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  296.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  297.   HAL_GPIO_Init (USART1_TXEN_GPIO_Port, &GPIO_InitStruct);
  298.  
  299.   /*Configure GPIO pin : USB_PULLUP_Pin */
  300.   GPIO_InitStruct.Pin = USB_PULLUP_Pin;
  301.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  302.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  303.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  304.   HAL_GPIO_Init (USB_PULLUP_GPIO_Port, &GPIO_InitStruct);
  305.  
  306. }
  307.  
  308. /* USER CODE BEGIN 4 */
  309.  
  310. /* USER CODE END 4 */
  311.  
  312. /**
  313.  * @brief  This function is executed in case of error occurrence.
  314.  * @retval None
  315.  */
  316. void
  317. Error_Handler (void)
  318. {
  319.   /* USER CODE BEGIN Error_Handler_Debug */
  320.   /* User can add his own implementation to report the HAL error return state */
  321.  
  322.   /* USER CODE END Error_Handler_Debug */
  323. }
  324.  
  325. #ifdef  USE_FULL_ASSERT
  326. /**
  327.   * @brief  Reports the name of the source file and the source line number
  328.   *         where the assert_param error has occurred.
  329.   * @param  file: pointer to the source file name
  330.   * @param  line: assert_param error line source number
  331.   * @retval None
  332.   */
  333. void assert_failed(uint8_t *file, uint32_t line)
  334. {
  335.   /* USER CODE BEGIN 6 */
  336.   /* User can add his own implementation to report the file name and line number,
  337.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  338.   /* USER CODE END 6 */
  339. }
  340. #endif /* USE_FULL_ASSERT */
  341.  
  342. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  343.