SHARE
TWEET

Untitled

a guest Dec 8th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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) 2019 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.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdbool.h>
  27. #include "ring_buffer.h"
  28. /* USER CODE END Includes */
  29.  
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32.  
  33. /* USER CODE END PTD */
  34.  
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. #define SET_LED(x) TIM2->CCR1 = x
  38. #define LED_ON SET_LED(0)
  39. #define LED_OFF SET_LED(31999)
  40. #define GET_LED TIM2->CCR1
  41. /* USER CODE END PD */
  42.  
  43. /* Private macro -------------------------------------------------------------*/
  44. /* USER CODE BEGIN PM */
  45.  
  46. /* USER CODE END PM */
  47.  
  48. /* Private variables ---------------------------------------------------------*/
  49. IWDG_HandleTypeDef hiwdg;
  50.  
  51. TIM_HandleTypeDef htim2;
  52.  
  53. UART_HandleTypeDef huart1;
  54.  
  55. /* USER CODE BEGIN PV */
  56. uint8_t receive; // Zmienna do której zapisujemy odebrany bajt
  57. ring_buffer_t rb_receive; // Struktura przechowująca odebrane dane
  58. ring_buffer_t rb_send; // Struktura przechowująca dane do wysłania
  59. uint8_t comm[25]; // Długość najdłuższej komendy + dodatkowe 4 miejsca
  60. int on_time; // Czas zapalonej diody
  61. int off_time; // Czas wyłączonej diody
  62. int blink_count; // Ilość mrugnięć diody
  63. uint16_t tail; // Zmienna tymczasowa
  64.  
  65.  
  66. uint16_t counter_tim = 1000;
  67. uint16_t brightness = 0;
  68. bool brightning = true;
  69. double i = 1;
  70.  
  71. enum State{normal, pulsing, blinking};
  72.  
  73. enum State state;
  74.  
  75. /* USER CODE END PV */
  76.  
  77. /* Private function prototypes -----------------------------------------------*/
  78. void SystemClock_Config(void);
  79. static void MX_GPIO_Init(void);
  80. static void MX_IWDG_Init(void);
  81. static void MX_TIM2_Init(void);
  82. static void MX_USART1_UART_Init(void);
  83. /* USER CODE BEGIN PFP */
  84.  
  85. void send_info(const char* data){
  86.     for (int i = 0; i < strlen(data); i++) {
  87.             RB_writeByte(&rb_send, data[i]);
  88.         }
  89.         tail = rb_send.tail;
  90.         if(huart1.gState == HAL_UART_STATE_READY){
  91.             __disable_irq();
  92.             if(RB_readByte(&rb_send))
  93.                 HAL_UART_Transmit_IT(&huart1, (uint8_t*)&rb_send.data[tail], 1);
  94.             __enable_irq();
  95.         }
  96. }
  97.  
  98. void parse(){
  99.     if(strcmp(comm, "BLINK") == 0){
  100.         LED_OFF;
  101.         state = blinking;
  102.         counter_tim = 0;
  103.         send_info("BLINKING");
  104.     }
  105.     else if(strcmp(comm, "PULSE") == 0){
  106.         state = pulsing;
  107.         counter_tim = 0;
  108.         send_info("PULSING");
  109.     }
  110.     else if(strstr(comm, "BLINK:")){
  111.         int parameters = sscanf(comm,  "BLINK:%i:%i:%i", &on_time, &off_time, &blink_count*2);
  112.         if(parameters != 3){
  113.             send_info("Niewlasciwa liczba argumentow!\r\n");
  114.         }
  115.         LED_OFF;
  116.         state = blinking;
  117.     }
  118.     else
  119.         send_info("blad: komenda nierozpoznana\r\n");
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. void set_led_brightness(double brightness){
  129.     uint32_t value = pow(brightness/1000, 2.2) * 31999;
  130.     SET_LED(value);
  131. }
  132.  
  133. void blinkLed(){
  134.     if (state == blinking && counter_tim == 0){
  135.  
  136.         if(blink_count == 1) state = normal;
  137.  
  138.         if (GET_LED == 31999){
  139.             LED_ON;
  140.             if(blink_count-- >= 1) counter_tim = on_time;
  141.             else counter_tim = 1000;
  142.         }
  143.         else if (GET_LED == 0){
  144.             LED_OFF;
  145.             if(blink_count-- >= 1) counter_tim = off_time;
  146.             else counter_tim = 1000;
  147.         }
  148.     }
  149. }
  150.  
  151. void pulseLed(){
  152.     if (state == pulsing && counter_tim == 0){
  153.               if (brightning == false && brightness > 0){
  154.                   set_led_brightness(brightness--);
  155.                   counter_tim = 1;
  156.  
  157.                   if (brightness == 0)
  158.                       brightning = true;
  159.               }
  160.               else if (brightning == true && brightness < 1000){
  161.                   set_led_brightness(brightness++);
  162.                   counter_tim = 1;
  163.  
  164.                   if (brightness == 1000)
  165.                       brightning = false;
  166.               }
  167.           }
  168. }
  169.  
  170. void IWDG_Refresh(){// Refresh IWDG: reload counter reset watchdoga
  171.     if(HAL_IWDG_Refresh(&hiwdg) != HAL_OK){
  172.         send_info("WATCHDOG CANT REFRESH\r\n");
  173.         Error_Handler();
  174.     }
  175. }
  176. /* USER CODE END PFP */
  177.  
  178. /* Private user code ---------------------------------------------------------*/
  179. /* USER CODE BEGIN 0 */
  180.  
  181. /* USER CODE END 0 */
  182.  
  183. /**
  184.   * @brief  The application entry point.
  185.   * @retval int
  186.   */
  187. int main(void)
  188. {
  189.   /* USER CODE BEGIN 1 */
  190.  
  191.     uint8_t counter = 0;
  192.     unsigned char x;
  193.  
  194.   /* USER CODE END 1 */
  195.  
  196.  
  197.   /* MCU Configuration--------------------------------------------------------*/
  198.  
  199.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  200.   HAL_Init();
  201.  
  202.   /* USER CODE BEGIN Init */
  203.  
  204.   RB_init(&rb_send);
  205.   RB_init(&rb_receive);
  206.  
  207.   /* USER CODE END Init */
  208.  
  209.   /* Configure the system clock */
  210.   SystemClock_Config();
  211.  
  212.   /* USER CODE BEGIN SysInit */
  213.  
  214.   /* USER CODE END SysInit */
  215.  
  216.   /* Initialize all configured peripherals */
  217.   MX_GPIO_Init();
  218.   MX_IWDG_Init();
  219.   MX_TIM2_Init();
  220.   MX_USART1_UART_Init();
  221.   /* USER CODE BEGIN 2 */
  222.  
  223.   if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET){
  224.       send_info("Program stuck in the loop\r\n");
  225.   }
  226.  
  227.   if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK)
  228.     {
  229.       send_info("PWM didn't start\r\n");
  230.     }
  231.  
  232.   HAL_TIM_Base_Start_IT(&htim2);
  233.  
  234.  
  235.   send_info("Hello, I am STM32 !!!\r\n");
  236.  
  237.   HAL_UART_Receive_IT(&huart1, &receive, 1);
  238.  
  239.   /* USER CODE END 2 */
  240.  
  241.   /* Infinite loop */
  242.   /* USER CODE BEGIN WHILE */
  243.   while (1)
  244.   {
  245.     /* USER CODE END WHILE */
  246.  
  247.     /* USER CODE BEGIN 3 */
  248.       if(x = RB_readByte(&rb_receive)){
  249.           if(x != ';'){
  250.               comm[counter] = x;
  251.               counter++;
  252.           }
  253.           else{
  254.               parse();
  255.               memset(comm, 0, 25);
  256.               counter = 0;
  257.           }
  258.       }
  259.       pulseLed();
  260.       blinkLed();
  261.  
  262.       IWDG_Refresh();
  263.   }
  264.   /* USER CODE END 3 */
  265. }
  266.  
  267. /**
  268.   * @brief System Clock Configuration
  269.   * @retval None
  270.   */
  271. void SystemClock_Config(void)
  272. {
  273.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  274.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  275.  
  276.   /** Initializes the CPU, AHB and APB busses clocks
  277.   */
  278.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  279.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  280.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  281.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  282.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  283.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  284.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
  285.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  286.   {
  287.     Error_Handler();
  288.   }
  289.   /** Initializes the CPU, AHB and APB busses clocks
  290.   */
  291.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  292.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  293.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  294.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  295.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  296.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  297.  
  298.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  299.   {
  300.     Error_Handler();
  301.   }
  302. }
  303.  
  304. /**
  305.   * @brief IWDG Initialization Function
  306.   * @param None
  307.   * @retval None
  308.   */
  309. static void MX_IWDG_Init(void)
  310. {
  311.  
  312.   /* USER CODE BEGIN IWDG_Init 0 */
  313.  
  314.   /* USER CODE END IWDG_Init 0 */
  315.  
  316.   /* USER CODE BEGIN IWDG_Init 1 */
  317.  
  318.   /* USER CODE END IWDG_Init 1 */
  319.   hiwdg.Instance = IWDG;
  320.   hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
  321.   hiwdg.Init.Reload = 4095;
  322.   if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  323.   {
  324.     Error_Handler();
  325.   }
  326.   /* USER CODE BEGIN IWDG_Init 2 */
  327.  
  328.   /* USER CODE END IWDG_Init 2 */
  329.  
  330. }
  331.  
  332. /**
  333.   * @brief TIM2 Initialization Function
  334.   * @param None
  335.   * @retval None
  336.   */
  337. static void MX_TIM2_Init(void)
  338. {
  339.  
  340.   /* USER CODE BEGIN TIM2_Init 0 */
  341.  
  342.   /* USER CODE END TIM2_Init 0 */
  343.  
  344.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  345.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  346.   TIM_OC_InitTypeDef sConfigOC = {0};
  347.  
  348.   /* USER CODE BEGIN TIM2_Init 1 */
  349.  
  350.   /* USER CODE END TIM2_Init 1 */
  351.   htim2.Instance = TIM2;
  352.   htim2.Init.Prescaler = 0;
  353.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  354.   htim2.Init.Period = 31999;
  355.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  356.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  357.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  358.   {
  359.     Error_Handler();
  360.   }
  361.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  362.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  363.   {
  364.     Error_Handler();
  365.   }
  366.   if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  367.   {
  368.     Error_Handler();
  369.   }
  370.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  371.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  372.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  373.   {
  374.     Error_Handler();
  375.   }
  376.   sConfigOC.OCMode = TIM_OCMODE_PWM1;
  377.   sConfigOC.Pulse = 31999;
  378.   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  379.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  380.   if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  381.   {
  382.     Error_Handler();
  383.   }
  384.   /* USER CODE BEGIN TIM2_Init 2 */
  385.  
  386.   /* USER CODE END TIM2_Init 2 */
  387.   HAL_TIM_MspPostInit(&htim2);
  388.  
  389. }
  390.  
  391. /**
  392.   * @brief USART1 Initialization Function
  393.   * @param None
  394.   * @retval None
  395.   */
  396. static void MX_USART1_UART_Init(void)
  397. {
  398.  
  399.   /* USER CODE BEGIN USART1_Init 0 */
  400.  
  401.   /* USER CODE END USART1_Init 0 */
  402.  
  403.   /* USER CODE BEGIN USART1_Init 1 */
  404.  
  405.   /* USER CODE END USART1_Init 1 */
  406.   huart1.Instance = USART1;
  407.   huart1.Init.BaudRate = 115200;
  408.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  409.   huart1.Init.StopBits = UART_STOPBITS_1;
  410.   huart1.Init.Parity = UART_PARITY_NONE;
  411.   huart1.Init.Mode = UART_MODE_TX_RX;
  412.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  413.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  414.   if (HAL_UART_Init(&huart1) != HAL_OK)
  415.   {
  416.     Error_Handler();
  417.   }
  418.   /* USER CODE BEGIN USART1_Init 2 */
  419.  
  420.   /* USER CODE END USART1_Init 2 */
  421.  
  422. }
  423.  
  424. /**
  425.   * @brief GPIO Initialization Function
  426.   * @param None
  427.   * @retval None
  428.   */
  429. static void MX_GPIO_Init(void)
  430. {
  431.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  432.  
  433.   /* GPIO Ports Clock Enable */
  434.   __HAL_RCC_GPIOC_CLK_ENABLE();
  435.   __HAL_RCC_GPIOA_CLK_ENABLE();
  436.  
  437.   /*Configure GPIO pin : LD_Pin */
  438.   GPIO_InitStruct.Pin = LD_Pin;
  439.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  440.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  441.   HAL_GPIO_Init(LD_GPIO_Port, &GPIO_InitStruct);
  442.  
  443. }
  444.  
  445. /* USER CODE BEGIN 4 */
  446.  
  447. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  448. {
  449.         if(counter_tim > 0 && htim->Instance == TIM2) counter_tim--;
  450. }
  451.  
  452. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
  453.     if(huart->Instance == USART1){
  454.         RB_writeByte(&rb_receive, receive);
  455.         HAL_UART_Receive_IT(&huart1, &receive, 1);
  456.     }
  457. }
  458. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart){
  459.     if(huart->Instance == USART1){
  460.         uint8_t CharToSend;
  461.         if(CharToSend = RB_readByte(&rb_send))
  462.             HAL_UART_Transmit_IT(&huart1, &CharToSend, 1);
  463.     }
  464. }
  465.  
  466. /* USER CODE END 4 */
  467.  
  468. /**
  469.   * @brief  This function is executed in case of error occurrence.
  470.   * @retval None
  471.   */
  472. void Error_Handler(void)
  473. {
  474.   /* USER CODE BEGIN Error_Handler_Debug */
  475.   /* User can add his own implementation to report the HAL error return state */
  476.  
  477.   /* USER CODE END Error_Handler_Debug */
  478. }
  479.  
  480. #ifdef  USE_FULL_ASSERT
  481. /**
  482.   * @brief  Reports the name of the source file and the source line number
  483.   *         where the assert_param error has occurred.
  484.   * @param  file: pointer to the source file name
  485.   * @param  line: assert_param error line source number
  486.   * @retval None
  487.   */
  488. void assert_failed(uint8_t *file, uint32_t line)
  489. {
  490.   /* USER CODE BEGIN 6 */
  491.   /* User can add his own implementation to report the file name and line number,
  492.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  493.   /* USER CODE END 6 */
  494. }
  495. #endif /* USE_FULL_ASSERT */
  496.  
  497. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top