Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.49 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) 2019 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.  
  42. /* USER CODE BEGIN Includes */
  43. #include "stdarg.h"
  44. #include "string.h"
  45. #define BUF_SIZE 100
  46. /* USER CODE END Includes */
  47.  
  48. /* Private variables ---------------------------------------------------------*/
  49. IWDG_HandleTypeDef hiwdg;
  50.  
  51. UART_HandleTypeDef huart2;
  52.  
  53. /* USER CODE BEGIN PV */
  54. /* Private variables ---------------------------------------------------------*/
  55.  
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. static void MX_GPIO_Init(void);
  61. static void MX_USART2_UART_Init(void);
  62. static void MX_IWDG_Init(void);
  63.  
  64. /* USER CODE BEGIN PFP */
  65. /* Private function prototypes -----------------------------------------------*/
  66.  
  67. /* USER CODE END PFP */
  68.  
  69. /* USER CODE BEGIN 0 */
  70. __IO uint8_t BUF_Tx[BUF_SIZE];
  71.  
  72.  
  73. __IO uint8_t BusyTx = 0;
  74. __IO uint8_t EmptyTx = 0;
  75. __IO uint8_t tmp;
  76.  
  77. void IWDG_Refresh(void) {
  78.     if (HAL_IWDG_Refresh(&hiwdg) != HAL_OK) {
  79.         send("WATCHDOG CANT REFRESH\r\n");
  80.         Error_Handler();
  81.     }
  82. }
  83.  
  84. void send(char format[], ...) {
  85.     char tmp_s[256];
  86.     va_list arglist;
  87.     va_start(arglist, format);
  88.     vsprintf(tmp_s, format, arglist);
  89.     va_end(arglist);
  90.     uint8_t idx = EmptyTx;
  91.     for (uint8_t i = 0; i < strlen(&tmp_s); i++) {
  92.         BUF_Tx[idx] = tmp_s[i];
  93.         idx++;
  94.         if (idx >= BUF_SIZE)
  95.             idx = 0;
  96.     }
  97.  
  98.     __disable_irq();
  99.  
  100.     if (BusyTx == EmptyTx
  101.             && __HAL_UART_GET_FLAG(&huart2,UART_FLAG_TXE) == SET) {
  102.         EmptyTx = idx;
  103.         tmp = BUF_Tx[BusyTx];
  104.         BusyTx++;
  105.         if (BusyTx >= BUF_SIZE)
  106.             BusyTx = 0;
  107.         HAL_UART_Transmit_IT(&huart2, &tmp, 1);
  108.     } else {
  109.         EmptyTx = idx;
  110.     }
  111.     __enable_irq();
  112. }
  113.  
  114. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
  115.     if (huart->Instance == USART2) {
  116.         if (BusyTx != EmptyTx) {
  117.             tmp = BUF_Tx[BusyTx++];
  118.             if (BusyTx >= BUF_SIZE)
  119.                 BusyTx = 0;
  120.             HAL_UART_Transmit_IT(&huart2, &tmp, 1);
  121.         }
  122.     }
  123. }
  124. /* USER CODE END 0 */
  125.  
  126. /**
  127.  * @brief  The application entry point.
  128.  *
  129.  * @retval None
  130.  */
  131. int main(void) {
  132.     /* USER CODE BEGIN 1 */
  133.  
  134.     /* USER CODE END 1 */
  135.  
  136.     /* MCU Configuration----------------------------------------------------------*/
  137.  
  138.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  139.     HAL_Init();
  140.  
  141.     /* USER CODE BEGIN Init */
  142.  
  143.     /* USER CODE END Init */
  144.  
  145.     /* Configure the system clock */
  146.     SystemClock_Config();
  147.  
  148.     /* USER CODE BEGIN SysInit */
  149.  
  150.     /* USER CODE END SysInit */
  151.  
  152.     /* Initialize all configured peripherals */
  153.     MX_GPIO_Init();
  154.     MX_USART2_UART_Init();
  155.     MX_IWDG_Init();
  156.     /* USER CODE BEGIN 2 */
  157.  
  158.     /* USER CODE END 2 */
  159.  
  160.     /* Infinite loop */
  161.     /* USER CODE BEGIN WHILE */
  162.     if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) {
  163.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  164.         send("Reset\r\n");
  165.     }
  166.     while (1) {
  167.         IWDG_Refresh();
  168.         /* USER CODE END WHILE */
  169.  
  170.         /* USER CODE BEGIN 3 */
  171.         for(;;) {
  172.  
  173.         }
  174.     }
  175.     /* USER CODE END 3 */
  176.  
  177. }
  178.  
  179. /**
  180.  * @brief System Clock Configuration
  181.  * @retval None
  182.  */
  183. void SystemClock_Config(void) {
  184.  
  185.     RCC_OscInitTypeDef RCC_OscInitStruct;
  186.     RCC_ClkInitTypeDef RCC_ClkInitStruct;
  187.  
  188.     /**Initializes the CPU, AHB and APB busses clocks
  189.      */
  190.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI
  191.             | RCC_OSCILLATORTYPE_LSI;
  192.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  193.     RCC_OscInitStruct.HSICalibrationValue = 16;
  194.     RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  195.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  196.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  197.     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  198.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  199.         _Error_Handler(__FILE__, __LINE__);
  200.     }
  201.  
  202.     /**Initializes the CPU, AHB and APB busses clocks
  203.      */
  204.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  205.             | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  206.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  207.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  208.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  209.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  210.  
  211.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  212.         _Error_Handler(__FILE__, __LINE__);
  213.     }
  214.  
  215.     /**Configure the Systick interrupt time
  216.      */
  217.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
  218.  
  219.     /**Configure the Systick
  220.      */
  221.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  222.  
  223.     /* SysTick_IRQn interrupt configuration */
  224.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  225. }
  226.  
  227. /* IWDG init function */
  228. static void MX_IWDG_Init(void) {
  229.  
  230.     hiwdg.Instance = IWDG;
  231.     hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
  232.     hiwdg.Init.Reload = 4095;
  233.     if (HAL_IWDG_Init(&hiwdg) != HAL_OK) {
  234.         _Error_Handler(__FILE__, __LINE__);
  235.     }
  236.  
  237. }
  238.  
  239. /* USART2 init function */
  240. static void MX_USART2_UART_Init(void) {
  241.  
  242.     huart2.Instance = USART2;
  243.     huart2.Init.BaudRate = 9600;
  244.     huart2.Init.WordLength = UART_WORDLENGTH_8B;
  245.     huart2.Init.StopBits = UART_STOPBITS_1;
  246.     huart2.Init.Parity = UART_PARITY_NONE;
  247.     huart2.Init.Mode = UART_MODE_TX_RX;
  248.     huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  249.     huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  250.     if (HAL_UART_Init(&huart2) != HAL_OK) {
  251.         _Error_Handler(__FILE__, __LINE__);
  252.     }
  253.  
  254. }
  255.  
  256. /** Configure pins as
  257.  * Analog
  258.  * Input
  259.  * Output
  260.  * EVENT_OUT
  261.  * EXTI
  262.  */
  263. static void MX_GPIO_Init(void) {
  264.  
  265.     GPIO_InitTypeDef GPIO_InitStruct;
  266.  
  267.     /* GPIO Ports Clock Enable */
  268.     __HAL_RCC_GPIOC_CLK_ENABLE()
  269.     ;
  270.     __HAL_RCC_GPIOD_CLK_ENABLE()
  271.     ;
  272.     __HAL_RCC_GPIOA_CLK_ENABLE()
  273.     ;
  274.     __HAL_RCC_GPIOB_CLK_ENABLE()
  275.     ;
  276.  
  277.     /*Configure GPIO pin Output Level */
  278.     HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  279.  
  280.     /*Configure GPIO pin : B1_Pin */
  281.     GPIO_InitStruct.Pin = B1_Pin;
  282.     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  283.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  284.     HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  285.  
  286.     /*Configure GPIO pin : LD2_Pin */
  287.     GPIO_InitStruct.Pin = LD2_Pin;
  288.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  289.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  290.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  291.     HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  292.  
  293.     /* EXTI interrupt init*/
  294.     HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  295.     HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  296.  
  297. }
  298.  
  299. /* USER CODE BEGIN 4 */
  300.  
  301. /* USER CODE END 4 */
  302.  
  303. /**
  304.  * @brief  This function is executed in case of error occurrence.
  305.  * @param  file: The file name as string.
  306.  * @param  line: The line in file as a number.
  307.  * @retval None
  308.  */
  309. void _Error_Handler(char *file, int line) {
  310.     /* USER CODE BEGIN Error_Handler_Debug */
  311.     /* User can add his own implementation to report the HAL error return state */
  312.     while (1) {
  313.     }
  314.     /* USER CODE END Error_Handler_Debug */
  315. }
  316.  
  317. #ifdef  USE_FULL_ASSERT
  318. /**
  319.  * @brief  Reports the name of the source file and the source line number
  320.  *         where the assert_param error has occurred.
  321.  * @param  file: pointer to the source file name
  322.  * @param  line: assert_param error line source number
  323.  * @retval None
  324.  */
  325. void assert_failed(uint8_t* file, uint32_t line)
  326. {
  327.     /* USER CODE BEGIN 6 */
  328.     /* User can add his own implementation to report the file name and line number,
  329.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  330.     /* USER CODE END 6 */
  331. }
  332. #endif /* USE_FULL_ASSERT */
  333.  
  334. /**
  335.  * @}
  336.  */
  337.  
  338. /**
  339.  * @}
  340.  */
  341.  
  342. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement