main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2016 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "main.h"
  35. #include "stm32f0xx_hal.h"
  36. /* USER CODE BEGIN Includes */
  37. /* USER CODE END Includes */
  38. /* Private variables ---------------------------------------------------------*/
  39. UART_HandleTypeDef huart1;
  40. /* USER CODE BEGIN PV */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. void Error_Handler(void);
  46. static void MX_GPIO_Init(void);
  47. static void MX_USART1_UART_Init(void);
  48. /* USER CODE BEGIN PFP */
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* USER CODE END PFP */
  51. /* USER CODE BEGIN 0 */
  52. void HAL_Delay_my(uint32_t msec)
  53. {
  54. while (msec--) {
  55. volatile int i;
  56. i=0; while(i++ < 1550);
  57. }
  58. }
  59. #define sendcmd(cmd, delay_after) _sendcmd(cmd, sizeof(cmd)-1, delay_after)
  60. void _sendcmd(char *cmd, size_t cmd_len, int delay_after) {
  61. ENABLE_RS232_GPIO_Port->BSRR = ENABLE_RS232_Pin;
  62. HAL_Delay_my(100);
  63. HAL_UART_Transmit(&huart1, (uint8_t *)cmd, cmd_len, ~0);
  64. HAL_UART_Transmit(&huart1, (uint8_t *)"\r\n", 2, ~0);
  65. ENABLE_RS232_GPIO_Port->BSRR = ENABLE_RS232_Pin << 16;
  66. if (delay_after > 0) {
  67. HAL_Delay_my(delay_after * 1000 - 100);
  68. }
  69. return;
  70. }
  71. /* USER CODE END 0 */
  72. int main(void)
  73. {
  74. /* USER CODE BEGIN 1 */
  75. /* USER CODE END 1 */
  76. /* MCU Configuration----------------------------------------------------------*/
  77. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78. HAL_Init();
  79. /* Configure the system clock */
  80. SystemClock_Config();
  81. /* Initialize all configured peripherals */
  82. MX_GPIO_Init();
  83. MX_USART1_UART_Init();
  84. /* USER CODE BEGIN 2 */
  85. LED1_GPIO_Port->BSRR = LED1_Pin;
  86. sendcmd("rbs", 1);
  87. sendcmd("rbs", 1);
  88. sendcmd("reload", 10);
  89. sendcmd("mount_c2", 1);
  90. sendcmd("formathd /c2", 1);
  91. sendcmd("y", 1);
  92. sendcmd("formathd /d", 1);
  93. sendcmd("y", 1);
  94. sendcmd("reload", 10);
  95. sendcmd("ifconfig le0 169.254.1.1 netmask 255.255.0.0", 1);
  96. /* USER CODE END 2 */
  97. /* Infinite loop */
  98. /* USER CODE BEGIN WHILE */
  99. /* USER CODE END WHILE */
  100. /* USER CODE BEGIN 3 */
  101. LED1_GPIO_Port->BSRR = LED1_Pin << 16;
  102. HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFE);
  103. /* USER CODE END 3 */
  104. }
  105. /** System Clock Configuration
  106. */
  107. void SystemClock_Config(void)
  108. {
  109. RCC_OscInitTypeDef RCC_OscInitStruct;
  110. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  111. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  112. /**Initializes the CPU, AHB and APB busses clocks
  113. */
  114. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  115. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  116. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  117. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  118. {
  119. Error_Handler();
  120. }
  121. /**Initializes the CPU, AHB and APB busses clocks
  122. */
  123. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  124. |RCC_CLOCKTYPE_PCLK1;
  125. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  126. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  127. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  128. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  129. {
  130. Error_Handler();
  131. }
  132. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  133. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  134. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  135. {
  136. Error_Handler();
  137. }
  138. /**Configure the Systick interrupt time
  139. */
  140. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  141. /**Configure the Systick
  142. */
  143. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  144. /* SysTick_IRQn interrupt configuration */
  145. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  146. }
  147. /* USART1 init function */
  148. static void MX_USART1_UART_Init(void)
  149. {
  150. huart1.Instance = USART1;
  151. huart1.Init.BaudRate = 9600;
  152. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  153. huart1.Init.StopBits = UART_STOPBITS_1;
  154. huart1.Init.Parity = UART_PARITY_NONE;
  155. huart1.Init.Mode = UART_MODE_TX_RX;
  156. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  157. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  158. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  159. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  160. if (HAL_UART_Init(&huart1) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. }
  165. /** Configure pins as
  166. * Analog
  167. * Input
  168. * Output
  169. * EVENT_OUT
  170. * EXTI
  171. */
  172. static void MX_GPIO_Init(void)
  173. {
  174. GPIO_InitTypeDef GPIO_InitStruct;
  175. /* GPIO Ports Clock Enable */
  176. __HAL_RCC_GPIOF_CLK_ENABLE();
  177. __HAL_RCC_GPIOA_CLK_ENABLE();
  178. /*Configure GPIO pin Output Level */
  179. HAL_GPIO_WritePin(GPIOA, LED1_Pin|ENABLE_RS232_Pin, GPIO_PIN_RESET);
  180. /*Configure GPIO pins : LED1_Pin ENABLE_RS232_Pin */
  181. GPIO_InitStruct.Pin = LED1_Pin|ENABLE_RS232_Pin;
  182. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  183. GPIO_InitStruct.Pull = GPIO_NOPULL;
  184. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  185. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  186. }
  187. /* USER CODE BEGIN 4 */
  188. /* USER CODE END 4 */
  189. /**
  190. * @brief This function is executed in case of error occurrence.
  191. * @param None
  192. * @retval None
  193. */
  194. void Error_Handler(void)
  195. {
  196. /* USER CODE BEGIN Error_Handler */
  197. /* User can add his own implementation to report the HAL error return state */
  198. while(1)
  199. {
  200. }
  201. /* USER CODE END Error_Handler */
  202. }
  203. #ifdef USE_FULL_ASSERT
  204. /**
  205. * @brief Reports the name of the source file and the source line number
  206. * where the assert_param error has occurred.
  207. * @param file: pointer to the source file name
  208. * @param line: assert_param error line source number
  209. * @retval None
  210. */
  211. void assert_failed(uint8_t* file, uint32_t line)
  212. {
  213. /* USER CODE BEGIN 6 */
  214. /* User can add his own implementation to report the file name and line number,
  215. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  216. /* USER CODE END 6 */
  217. }
  218. #endif
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/