Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.94 KB | None | 0 0
  1. /**
  2.  ******************************************************************************
  3.  * @file           : main.c
  4.  * @brief          : Main program body
  5.  ******************************************************************************
  6.  ** This notice applies to any and all portions of this file
  7.  * that are not between comment pairs USER CODE BEGIN and
  8.  * USER CODE END. Other portions of this file, whether
  9.  * inserted by the user or by software development tools
  10.  * are owned by their respective copyright owners.
  11.  *
  12.  * COPYRIGHT(c) 2018 STMicroelectronics
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without modification,
  15.  * are permitted provided that the following conditions are met:
  16.  *   1. Redistributions of source code must retain the above copyright notice,
  17.  *      this list of conditions and the following disclaimer.
  18.  *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.  *      this list of conditions and the following disclaimer in the documentation
  20.  *      and/or other materials provided with the distribution.
  21.  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.  *      may be used to endorse or promote products derived from this software
  23.  *      without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  *
  36.  ******************************************************************************
  37.  */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"
  41. #include "stdarg.h"
  42. #include "string.h"
  43.  
  44. /* USER CODE BEGIN Includes */
  45. #define BUF_SIZE 100
  46. /* USER CODE END Includes */
  47.  
  48. /* Private variables ---------------------------------------------------------*/
  49. UART_HandleTypeDef huart2;
  50.  
  51. /* USER CODE BEGIN PV */
  52. /* Private variables ---------------------------------------------------------*/
  53.  
  54. /* USER CODE END PV */
  55.  
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. static void MX_GPIO_Init(void);
  59. static void MX_USART2_UART_Init(void);
  60.  
  61. /* USER CODE BEGIN PFP */
  62. /* Private function prototypes -----------------------------------------------*/
  63.  
  64. /* USER CODE END PFP */
  65.  
  66. /* USER CODE BEGIN 0 */
  67. __IO uint8_t BUF_Rx[BUF_SIZE];
  68. __IO uint8_t BUF_Tx[BUF_SIZE];
  69.  
  70. __IO uint8_t BusyRx = 0;
  71. __IO uint8_t EmptyRx = 0;
  72. __IO uint8_t BusyTx = 0;
  73. __IO uint8_t EmptyTx = 0;
  74. __IO uint8_t tmp;
  75. __IO uint16_t timeon_led;
  76. __IO uint16_t timeoff;
  77. __IO uint16_t cnt;
  78. uint16_t count = 0;
  79. __IO uint16_t timer = 0;
  80. uint16_t frequency = 1000;
  81. uint8_t flag = 0;
  82. uint8_t led_on = 0;
  83. uint8_t received = 0;
  84.  
  85. void execute_command() {
  86.     uint8_t command = 0;
  87.     uint8_t i = 0;
  88.     char temp[20];
  89.     uint8_t idx = BusyRx;
  90.     while (idx != EmptyRx) {
  91.         temp[i++] = BUF_Rx[idx++];
  92.         if (idx >= BUF_SIZE) {
  93.             idx = 0;
  94.         }
  95.     }
  96.     BusyRx = idx;
  97.     temp[i - 1] = '\0';
  98.     if (strcmp(temp, "ON") == 0)
  99.         command = 1;
  100.     else if (strcmp(temp, "OFF") == 0)
  101.         command = 2;
  102.     else if (strcmp(temp, "BLINK") == 0)
  103.         command = 3;
  104.     else if (sscanf(temp, "BLINK:%d:%d:%d", &timeon_led, &timeoff, &cnt) == 3)
  105.         command = 4;
  106.  
  107.     switch (command) {
  108.     case 1:
  109.         flag = 0;
  110.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  111.         break;
  112.     case 2:
  113.         flag = 0;
  114.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  115.         break;
  116.     case 3:
  117.         flag = 1;
  118.         break;
  119.     case 4:
  120.         flag = 2;
  121.         count = 0;
  122.         break;
  123.     default:
  124.         send("Nieznana komenda!\r\n");
  125.         break;
  126.     }
  127.     received = 1;
  128. }
  129.  
  130. uint8_t check_buffer() {
  131.     if (EmptyRx == 0) {
  132.         if (BUF_Rx[BUF_SIZE - 1] == ';')
  133.             return 1;
  134.         else
  135.             return 0;
  136.     } else if (BUF_Rx[EmptyRx - 1] == ';')
  137.         return 1;
  138.     else
  139.         return 0;
  140. }
  141.  
  142. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  143.     if (huart->Instance == USART2) {
  144.         EmptyRx++;
  145.         if (EmptyRx >= BUF_SIZE) {
  146.             EmptyRx = 0;
  147.         }
  148.         received = 0;
  149.         HAL_UART_Receive_IT(&huart2, &BUF_Rx[EmptyRx], 1);
  150.     }
  151. }
  152.  
  153. void send(char format[], ...) {
  154.     char tmp_s[256];
  155.     va_list arglist;
  156.     va_start(arglist, format);
  157.     vsprintf(tmp_s, format, arglist);
  158.     va_end(arglist);
  159.     uint8_t idx = EmptyTx;
  160.     for (uint8_t i = 0; i < strlen(&tmp_s); i++) {
  161.         BUF_Tx[idx] = tmp_s[i];
  162.         idx++;
  163.         if (idx >= BUF_SIZE)
  164.             idx = 0;
  165.     }
  166.  
  167.     __disable_irq();
  168.  
  169.     if (BusyTx == EmptyTx
  170.             && __HAL_UART_GET_FLAG(&huart2,UART_FLAG_TXE) == SET) {
  171.         EmptyTx = idx;
  172.         tmp = BUF_Tx[BusyTx];
  173.         BusyTx++;
  174.         if (BusyTx >= BUF_SIZE)
  175.             BusyTx = 0;
  176.         HAL_UART_Transmit_IT(&huart2, &tmp, 1);
  177.     } else {
  178.         EmptyTx = idx;
  179.     }
  180.     __enable_irq();
  181. }
  182.  
  183. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
  184.     if (huart->Instance == USART2) {
  185.         if (BusyTx != EmptyTx) {
  186.             tmp = BUF_Tx[BusyTx++];
  187.             if (BusyTx >= BUF_SIZE)
  188.                 BusyTx = 0;
  189.             HAL_UART_Transmit_IT(&huart2, &tmp, 1);
  190.         }
  191.     }
  192. }
  193.  
  194. /* USER CODE END 0 */
  195.  
  196. /**
  197.  * @brief  The application entry point.
  198.  *
  199.  * @retval None
  200.  */
  201. int main(void) {
  202.     /* USER CODE BEGIN 1 */
  203.  
  204.     /* USER CODE END 1 */
  205.  
  206.     /* MCU Configuration----------------------------------------------------------*/
  207.  
  208.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  209.     HAL_Init();
  210.  
  211.     /* USER CODE BEGIN Init */
  212.  
  213.     /* USER CODE END Init */
  214.  
  215.     /* Configure the system clock */
  216.     SystemClock_Config();
  217.  
  218.     /* USER CODE BEGIN SysInit */
  219.  
  220.     /* USER CODE END SysInit */
  221.  
  222.     /* Initialize all configured peripherals */
  223.     MX_GPIO_Init();
  224.     MX_USART2_UART_Init();
  225.     /* USER CODE BEGIN 2 */
  226.     HAL_UART_Receive_IT(&huart2, &BUF_Rx[EmptyRx], 1);
  227.     /* USER CODE END 2 */
  228.  
  229.     /* Infinite loop */
  230.     /* USER CODE BEGIN WHILE */
  231.     while (1) {
  232.         if (received == 0)
  233.             if (check_buffer() == 1)
  234.                 execute_command();
  235.         if (flag == 1) {
  236.             if (timer >= frequency) {
  237.                 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  238.                 timer = 0;
  239.             }
  240.         } else if (flag == 2) {
  241.             if (count < cnt) {
  242.                 if (timer >= timeoff && led_on == 0) {
  243.                     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  244.                     timer = 0;
  245.                     led_on = 1;
  246.                 }
  247.                 if (timer >= timeon_led && led_on == 1) {
  248.                     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  249.                     timer = 0;
  250.                     led_on = 0;
  251.                     count++;
  252.                 }
  253.             }
  254.         }
  255.     }
  256.     /* USER CODE END WHILE */
  257.  
  258.     /* USER CODE BEGIN 3 */
  259.  
  260. }
  261. /* USER CODE END 3 */
  262.  
  263. /**
  264.  * @brief System Clock Configuration
  265.  * @retval None
  266.  */
  267. void SystemClock_Config(void) {
  268.  
  269.     RCC_OscInitTypeDef RCC_OscInitStruct;
  270.     RCC_ClkInitTypeDef RCC_ClkInitStruct;
  271.  
  272.     /**Initializes the CPU, AHB and APB busses clocks
  273.      */
  274.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  275.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  276.     RCC_OscInitStruct.HSICalibrationValue = 16;
  277.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  278.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  279.     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  280.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  281.         _Error_Handler(__FILE__, __LINE__);
  282.     }
  283.  
  284.     /**Initializes the CPU, AHB and APB busses clocks
  285.      */
  286.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  287.             | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  288.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  289.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  290.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  291.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  292.  
  293.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  294.         _Error_Handler(__FILE__, __LINE__);
  295.     }
  296.  
  297.     /**Configure the Systick interrupt time
  298.      */
  299.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
  300.  
  301.     /**Configure the Systick
  302.      */
  303.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  304.  
  305.     /* SysTick_IRQn interrupt configuration */
  306.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  307. }
  308.  
  309. /* USART2 init function */
  310. static void MX_USART2_UART_Init(void) {
  311.  
  312.     huart2.Instance = USART2;
  313.     huart2.Init.BaudRate = 9600;
  314.     huart2.Init.WordLength = UART_WORDLENGTH_8B;
  315.     huart2.Init.StopBits = UART_STOPBITS_1;
  316.     huart2.Init.Parity = UART_PARITY_NONE;
  317.     huart2.Init.Mode = UART_MODE_TX_RX;
  318.     huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  319.     huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  320.     if (HAL_UART_Init(&huart2) != HAL_OK) {
  321.         _Error_Handler(__FILE__, __LINE__);
  322.     }
  323.  
  324. }
  325.  
  326. /** Configure pins as
  327.  * Analog
  328.  * Input
  329.  * Output
  330.  * EVENT_OUT
  331.  * EXTI
  332.  */
  333. static void MX_GPIO_Init(void) {
  334.  
  335.     GPIO_InitTypeDef GPIO_InitStruct;
  336.  
  337.     /* GPIO Ports Clock Enable */
  338.     __HAL_RCC_GPIOC_CLK_ENABLE()
  339.     ;
  340.     __HAL_RCC_GPIOD_CLK_ENABLE()
  341.     ;
  342.     __HAL_RCC_GPIOA_CLK_ENABLE()
  343.     ;
  344.     __HAL_RCC_GPIOB_CLK_ENABLE()
  345.     ;
  346.  
  347.     /*Configure GPIO pin Output Level */
  348.     HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  349.  
  350.     /*Configure GPIO pin : B1_Pin */
  351.     GPIO_InitStruct.Pin = B1_Pin;
  352.     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  353.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  354.     HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  355.  
  356.     /*Configure GPIO pin : LD2_Pin */
  357.     GPIO_InitStruct.Pin = LD2_Pin;
  358.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  359.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  360.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  361.     HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  362.  
  363.     /* EXTI interrupt init*/
  364.     HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  365.     HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  366.  
  367. }
  368.  
  369. /* USER CODE BEGIN 4 */
  370.  
  371. /* USER CODE END 4 */
  372.  
  373. /**
  374.  * @brief  This function is executed in case of error occurrence.
  375.  * @param  file: The file name as string.
  376.  * @param  line: The line in file as a number.
  377.  * @retval None
  378.  */
  379. void _Error_Handler(char *file, int line) {
  380.     /* USER CODE BEGIN Error_Handler_Debug */
  381.     /* User can add his own implementation to report the HAL error return state */
  382.     while (1) {
  383.     }
  384.     /* USER CODE END Error_Handler_Debug */
  385. }
  386.  
  387. #ifdef  USE_FULL_ASSERT
  388. /**
  389.  * @brief  Reports the name of the source file and the source line number
  390.  *         where the assert_param error has occurred.
  391.  * @param  file: pointer to the source file name
  392.  * @param  line: assert_param error line source number
  393.  * @retval None
  394.  */
  395. void assert_failed(uint8_t* file, uint32_t line)
  396. {
  397.     /* USER CODE BEGIN 6 */
  398.     /* User can add his own implementation to report the file name and line number,
  399.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  400.     /* USER CODE END 6 */
  401. }
  402. #endif /* USE_FULL_ASSERT */
  403.  
  404. /**
  405.  * @}
  406.  */
  407.  
  408. /**
  409.  * @}
  410.  */
  411.  
  412. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement