main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. ******************************************************************************
  3. * @file : main.c
  4. * @brief : Main program body
  5. ******************************************************************************
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the "License"; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * opensource.org/licenses/BSD-3-Clause
  15. *
  16. ******************************************************************************
  17. */
  18. #include "main.h"
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <sys/param.h>
  23. #include "serial_io.h"
  24. #include "esp_loader.h"
  25. #include "example_common.h"
  26. UART_HandleTypeDef huart1;
  27. UART_HandleTypeDef huart2;
  28. void SystemClock_Config(void);
  29. static void MX_GPIO_Init(void);
  30. static void MX_USART1_UART_Init(void);
  31. static void MX_USART2_UART_Init(void);
  32. void loader_port_stm32_init(UART_HandleTypeDef *huart,
  33. GPIO_TypeDef *port_io0,
  34. uint16_t pin_num_io0,
  35. GPIO_TypeDef *port_rst,
  36. uint16_t pin_num_rst);
  37. #define HIGHER_BAUDRATE 230400
  38. int main(void)
  39. {
  40. HAL_Init();
  41. SystemClock_Config();
  42. MX_GPIO_Init();
  43. MX_USART1_UART_Init();
  44. MX_USART2_UART_Init();
  45. loader_port_stm32_init(&huart1, GPIOB, TARGET_IO0_Pin, GPIOB, TARGET_RST_Pin);
  46. if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) {
  47. flash_binary(bootloader_bin, bootloader_bin_size, BOOTLOADER_ADDRESS);
  48. flash_binary(partition_table_bin, partition_table_bin_size, PARTITION_ADDRESS);
  49. flash_binary(hello_world_bin, hello_world_bin_size, APPLICATION_ADDRESS);
  50. }
  51. while (1) { }
  52. }
  53. void SystemClock_Config(void)
  54. {
  55. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  56. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  57. /** Configure the main internal regulator output voltage
  58. */
  59. __HAL_RCC_PWR_CLK_ENABLE();
  60. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  61. /** Initializes the CPU, AHB and APB busses clocks
  62. */
  63. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  64. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  65. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  66. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  67. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  68. Error_Handler();
  69. }
  70. /** Initializes the CPU, AHB and APB busses clocks
  71. */
  72. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  73. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  74. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  75. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  76. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  77. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  78. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
  79. Error_Handler();
  80. }
  81. }
  82. static void MX_USART1_UART_Init(void)
  83. {
  84. huart1.Instance = USART1;
  85. huart1.Init.BaudRate = 115200;
  86. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  87. huart1.Init.StopBits = UART_STOPBITS_1;
  88. huart1.Init.Parity = UART_PARITY_NONE;
  89. huart1.Init.Mode = UART_MODE_TX_RX;
  90. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  91. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  92. if (HAL_UART_Init(&huart1) != HAL_OK) {
  93. Error_Handler();
  94. }
  95. }
  96. static void MX_USART2_UART_Init(void)
  97. {
  98. huart2.Instance = USART2;
  99. huart2.Init.BaudRate = 115200;
  100. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  101. huart2.Init.StopBits = UART_STOPBITS_1;
  102. huart2.Init.Parity = UART_PARITY_NONE;
  103. huart2.Init.Mode = UART_MODE_TX_RX;
  104. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  105. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  106. if (HAL_UART_Init(&huart2) != HAL_OK) {
  107. Error_Handler();
  108. }
  109. }
  110. static void MX_GPIO_Init(void)
  111. {
  112. GPIO_InitTypeDef GPIO_InitStruct = {0};
  113. /* GPIO Ports Clock Enable */
  114. __HAL_RCC_GPIOD_CLK_ENABLE();
  115. __HAL_RCC_GPIOB_CLK_ENABLE();
  116. /*Configure GPIO pin Output Level */
  117. HAL_GPIO_WritePin(GPIOD, GREEN_LED_Pin | ORANGE_LED_Pin | RED_LED_Pin | BLUE_LED_Pin, GPIO_PIN_RESET);
  118. /*Configure GPIO pin Output Level */
  119. HAL_GPIO_WritePin(GPIOB, TARGET_RST_Pin | TARGET_IO0_Pin, GPIO_PIN_RESET);
  120. /*Configure GPIO pins : GREEN_LED_Pin ORANGE_LED_Pin RED_LED_Pin BLUE_LED_Pin */
  121. GPIO_InitStruct.Pin = GREEN_LED_Pin | ORANGE_LED_Pin | RED_LED_Pin | BLUE_LED_Pin;
  122. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  123. GPIO_InitStruct.Pull = GPIO_NOPULL;
  124. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  125. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  126. /*Configure GPIO pins : PB4 PB5 */
  127. GPIO_InitStruct.Pin = TARGET_RST_Pin | TARGET_IO0_Pin;
  128. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  129. GPIO_InitStruct.Pull = GPIO_NOPULL;
  130. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  131. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  132. }
  133. /**
  134. * @brief This function is executed in case of error occurrence.
  135. * @retval None
  136. */
  137. void Error_Handler(void)
  138. {
  139. /* User can add his own implementation to report the HAL error return state */
  140. }
  141. #ifdef USE_FULL_ASSERT
  142. /**
  143. * @brief Reports the name of the source file and the source line number
  144. * where the assert_param error has occurred.
  145. * @param file: pointer to the source file name
  146. * @param line: assert_param error line source number
  147. * @retval None
  148. */
  149. void assert_failed(uint8_t *file, uint32_t line)
  150. {
  151. /* User can add his own implementation to report the file name and line number,
  152. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  153. }
  154. #endif /* USE_FULL_ASSERT */
  155. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/