main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "stm32f0xx_hal.h"
  41. /* USER CODE BEGIN Includes */
  42. /* USER CODE END Includes */
  43. /* Private variables ---------------------------------------------------------*/
  44. UART_HandleTypeDef huart1;
  45. /* USER CODE BEGIN PV */
  46. /* Private variables ---------------------------------------------------------*/
  47. uint32_t delay;
  48. /* USER CODE END PV */
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. static void MX_GPIO_Init(void);
  52. static void MX_USART1_UART_Init(void);
  53. /* USER CODE BEGIN PFP */
  54. /* Private function prototypes -----------------------------------------------*/
  55. /* USER CODE END PFP */
  56. /* USER CODE BEGIN 0 */
  57. /* USER CODE END 0 */
  58. /**
  59. * @brief The application entry point.
  60. *
  61. * @retval None
  62. */
  63. int main(void)
  64. {
  65. /* USER CODE BEGIN 1 */
  66. /* USER CODE END 1 */
  67. /* MCU Configuration----------------------------------------------------------*/
  68. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  69. HAL_Init();
  70. /* USER CODE BEGIN Init */
  71. /* USER CODE END Init */
  72. /* Configure the system clock */
  73. SystemClock_Config();
  74. /* USER CODE BEGIN SysInit */
  75. /* USER CODE END SysInit */
  76. /* Initialize all configured peripherals */
  77. MX_GPIO_Init();
  78. MX_USART1_UART_Init();
  79. /* USER CODE BEGIN 2 */
  80. GPIOA -> BRR = 0b1111;
  81. GPIOA -> BSRR = 0b1000;
  82. for (delay=0;delay<1000000;delay++){};
  83. /* USER CODE END 2 */
  84. /* Infinite loop */
  85. /* USER CODE BEGIN WHILE */
  86. while (1)
  87. {
  88. while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0 || HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == 0 ){
  89. GPIOA -> BRR = 0b1000;
  90. while (1){};
  91. };
  92. /* USER CODE END WHILE */
  93. /* USER CODE BEGIN 3 */
  94. }
  95. /* USER CODE END 3 */
  96. }
  97. /**
  98. * @brief System Clock Configuration
  99. * @retval None
  100. */
  101. void SystemClock_Config(void)
  102. {
  103. RCC_OscInitTypeDef RCC_OscInitStruct;
  104. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  105. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  106. /**Initializes the CPU, AHB and APB busses clocks
  107. */
  108. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  109. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  110. RCC_OscInitStruct.HSICalibrationValue = 16;
  111. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  112. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  113. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  114. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  115. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  116. {
  117. _Error_Handler(__FILE__, __LINE__);
  118. }
  119. /**Initializes the CPU, AHB and APB busses clocks
  120. */
  121. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  122. |RCC_CLOCKTYPE_PCLK1;
  123. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  124. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  125. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  126. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  127. {
  128. _Error_Handler(__FILE__, __LINE__);
  129. }
  130. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  131. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  132. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  133. {
  134. _Error_Handler(__FILE__, __LINE__);
  135. }
  136. /**Configure the Systick interrupt time
  137. */
  138. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  139. /**Configure the Systick
  140. */
  141. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  142. /* SysTick_IRQn interrupt configuration */
  143. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  144. }
  145. /* USART1 init function */
  146. static void MX_USART1_UART_Init(void)
  147. {
  148. huart1.Instance = USART1;
  149. huart1.Init.BaudRate = 38400;
  150. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  151. huart1.Init.StopBits = UART_STOPBITS_1;
  152. huart1.Init.Parity = UART_PARITY_NONE;
  153. huart1.Init.Mode = UART_MODE_TX_RX;
  154. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  155. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  156. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  157. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  158. if (HAL_UART_Init(&huart1) != HAL_OK)
  159. {
  160. _Error_Handler(__FILE__, __LINE__);
  161. }
  162. }
  163. /** Configure pins as
  164. * Analog
  165. * Input
  166. * Output
  167. * EVENT_OUT
  168. * EXTI
  169. */
  170. static void MX_GPIO_Init(void)
  171. {
  172. GPIO_InitTypeDef GPIO_InitStruct;
  173. /* GPIO Ports Clock Enable */
  174. __HAL_RCC_GPIOA_CLK_ENABLE();
  175. /*Configure GPIO pin Output Level */
  176. HAL_GPIO_WritePin(STAT_LED_GPIO_Port, STAT_LED_Pin, GPIO_PIN_RESET);
  177. /*Configure GPIO pins : IN_PIKE_Pin IN_EDGE_Pin S_S_Pin */
  178. GPIO_InitStruct.Pin = IN_PIKE_Pin|IN_EDGE_Pin|S_S_Pin;
  179. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  180. GPIO_InitStruct.Pull = GPIO_NOPULL;
  181. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  182. /*Configure GPIO pin : STAT_LED_Pin */
  183. GPIO_InitStruct.Pin = STAT_LED_Pin;
  184. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  185. GPIO_InitStruct.Pull = GPIO_NOPULL;
  186. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  187. HAL_GPIO_Init(STAT_LED_GPIO_Port, &GPIO_InitStruct);
  188. }
  189. /* USER CODE BEGIN 4 */
  190. /* USER CODE END 4 */
  191. /**
  192. * @brief This function is executed in case of error occurrence.
  193. * @param file: The file name as string.
  194. * @param line: The line in file as a number.
  195. * @retval None
  196. */
  197. void _Error_Handler(char *file, int line)
  198. {
  199. /* USER CODE BEGIN Error_Handler_Debug */
  200. /* User can add his own implementation to report the HAL error return state */
  201. while(1)
  202. {
  203. }
  204. /* USER CODE END Error_Handler_Debug */
  205. }
  206. #ifdef USE_FULL_ASSERT
  207. /**
  208. * @brief Reports the name of the source file and the source line number
  209. * where the assert_param error has occurred.
  210. * @param file: pointer to the source file name
  211. * @param line: assert_param error line source number
  212. * @retval None
  213. */
  214. void assert_failed(uint8_t* file, uint32_t line)
  215. {
  216. /* USER CODE BEGIN 6 */
  217. /* User can add his own implementation to report the file name and line number,
  218. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  219. /* USER CODE END 6 */
  220. }
  221. #endif /* USE_FULL_ASSERT */
  222. /**
  223. * @}
  224. */
  225. /**
  226. * @}
  227. */
  228. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/