main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. /* USER CODE END PTD */
  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */
  32. /* USER CODE END PD */
  33. /* Private macro -------------------------------------------------------------*/
  34. /* USER CODE BEGIN PM */
  35. /* USER CODE END PM */
  36. /* Private variables ---------------------------------------------------------*/
  37. #define ApplicationAddress 0x8010000
  38. #define VECT_TAB_OFFSET 0x10000
  39. int i;
  40. /* USER CODE BEGIN PV */
  41. uint8_t msg[7]={0xFF,0xFF,0x00,0x00,0x00,0x00,0x0D};
  42. uint8_t NACK[1]={0x79};
  43. int rf=0;
  44. int row=0;
  45. int data_count=0;
  46. uint32_t temp[10000];
  47. uint32_t FlashData1[10000];
  48. uint8_t FlashData[16];
  49. uint8_t com[7]={0xFF,0xFF,0x00,0x00,0x00,0x01,0x0D};
  50. uint8_t com1[7]={0xFF,0xFF,0x00,0x00,0x00,0x02,0x0D};
  51. uint8_t start[7]={0xFF,0xFF,0x00,0x00,0x00,0x03,0x0D};
  52. /* USER CODE END PV */
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. void UserAppStart(void);
  56. void writeFlashTest(void);
  57. void printFlashTest(void);
  58. /* USER CODE BEGIN PFP */
  59. /* USER CODE END PFP */
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62. /* USER CODE END 0 */
  63. /**
  64. * @brief The application entry point.
  65. * @retval int
  66. */
  67. int main(void)
  68. {
  69. /* USER CODE BEGIN 1 */
  70. /* USER CODE END 1 */
  71. /* MCU Configuration--------------------------------------------------------*/
  72. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  73. HAL_Init();
  74. /* USER CODE BEGIN Init */
  75. /* USER CODE END Init */
  76. /* Configure the system clock */
  77. SystemClock_Config();
  78. /* USER CODE BEGIN SysInit */
  79. /* USER CODE END SysInit */
  80. /* Initialize all configured peripherals */
  81. MX_GPIO_Init();
  82. MX_USART2_UART_Init();
  83. MX_UART4_Init();
  84. MX_USART1_UART_Init();
  85. /* USER CODE BEGIN 2 */
  86. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  87. /*------Zigbee setup-----------------------------------------------*/
  88. HAL_UART_Transmit(&huart4,(uint8_t*)start,sizeof(start),10);
  89. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET);
  90. /*-----------------------------------------------------*/
  91. /* USER CODE END 2 */
  92. /* Infinite loop */
  93. /* USER CODE BEGIN WHILE */
  94. while (1)
  95. {
  96. /* USER CODE END WHILE */
  97. if(rf==0)
  98. {
  99. HAL_UART_Receive_IT(&huart4, (uint8_t *)msg,sizeof(msg));
  100. }
  101. else
  102. {
  103. HAL_UART_Receive_IT(&huart4, (uint8_t *)FlashData,sizeof(FlashData));
  104. }
  105. //HAL_UART_Receive_IT(&huart4, (uint8_t *)msg,sizeof(msg));
  106. /* USER CODE BEGIN 3 */
  107. }
  108. /* USER CODE END 3 */
  109. }
  110. /**
  111. * @brief System Clock Configuration
  112. * @retval None
  113. */
  114. void SystemClock_Config(void)
  115. {
  116. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  117. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  118. /** Configure the main internal regulator output voltage
  119. */
  120. __HAL_RCC_PWR_CLK_ENABLE();
  121. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  122. /** Initializes the CPU, AHB and APB busses clocks
  123. */
  124. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  125. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  126. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  127. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  128. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  129. RCC_OscInitStruct.PLL.PLLM = 8;
  130. RCC_OscInitStruct.PLL.PLLN = 72;
  131. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  132. RCC_OscInitStruct.PLL.PLLQ = 3;
  133. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. /** Initializes the CPU, AHB and APB busses clocks
  138. */
  139. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  141. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  145. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. }
  150. /* USER CODE BEGIN 4 */
  151. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  152. {
  153. if(huart->Instance == UART4)
  154. {
  155. if(rf==1)
  156. {
  157. //printf("row:%d\r\n",row);
  158. for(i=0;i<4;i++)
  159. {
  160. FlashData1[(4*row)+i]=(FlashData[4*i]<<24)+(FlashData[1+(4*i)]<<16)+(FlashData[2+(4*i)]<<8)+FlashData[3+(4*i)];
  161. //printf("%x\r\n",FlashData1[(4*row)+i]);
  162. }
  163. if(row==(data_count-1))
  164. {
  165. writeFlashTest();
  166. printFlashTest();
  167. UserAppStart();
  168. rf=0;
  169. data_count=0;
  170. row=0;
  171. }
  172. row=row+1;
  173. }
  174. else if(rf==0)
  175. {
  176. if(msg[0]==0xFF && msg[1]==0x01 && msg[6]==0x0D)
  177. {
  178. HAL_UART_Transmit(&huart4, (uint8_t *)NACK,1,0xFFFF);
  179. rf=1;
  180. data_count=msg[2]*100+msg[3];
  181. }
  182. if(msg[0]==0xFF && msg[1]==0x02 && msg[6]==0x0D)
  183. {
  184. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET);
  185. HAL_UART_Transmit(&huart4,(uint8_t*)com,sizeof(com),10);
  186. }
  187. if(msg[0]==0xFF && msg[1]==0x03 && msg[6]==0x0D)
  188. {
  189. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_RESET);
  190. HAL_UART_Transmit(&huart4,(uint8_t*)com1,sizeof(com1),10);
  191. }
  192. }
  193. }
  194. }
  195. void UserAppStart(void)
  196. { typedef void (*pFunction)(void);
  197. pFunction Jump_To_Application;
  198. uint32_t JumpAddress;
  199. if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
  200. {
  201. /* Jump to user application */
  202. JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
  203. Jump_To_Application = (pFunction) JumpAddress;
  204. /* Initialize user application's Stack Pointer */
  205. __set_MSP(*(__IO uint32_t*) ApplicationAddress);
  206. Jump_To_Application();
  207. }
  208. }
  209. void writeFlashTest(void)
  210. {
  211. FLASH_EraseInitTypeDef pEraseInit;
  212. HAL_FLASH_Unlock();
  213. pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
  214. pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  215. pEraseInit.Sector = FLASH_SECTOR_4;
  216. pEraseInit.NbSectors = 1;
  217. uint32_t PageError=0;
  218. HAL_FLASHEx_Erase(&pEraseInit,&PageError);
  219. for(i=0;i<(data_count*4);i++)
  220. {
  221. HAL_FLASH_Program(TYPEPROGRAM_WORD,ApplicationAddress+(i*4),FlashData1[i]);
  222. }
  223. HAL_FLASH_Lock();
  224. }
  225. void printFlashTest(void)
  226. {
  227. for(i=0;i<(data_count*4);i++)
  228. {
  229. temp[i] = *(__IO uint32_t*)(ApplicationAddress+(i*4));
  230. //printf("addr:0x%x, data:0x%x\r\n",ApplicationAddress+(i*4), temp[i]);
  231. //printf("%x\r\n", temp[i]);
  232. }
  233. }
  234. /* USER CODE END 4 */
  235. /**
  236. * @brief This function is executed in case of error occurrence.
  237. * @retval None
  238. */
  239. void Error_Handler(void)
  240. {
  241. /* USER CODE BEGIN Error_Handler_Debug */
  242. /* User can add his own implementation to report the HAL error return state */
  243. /* USER CODE END Error_Handler_Debug */
  244. }
  245. #ifdef USE_FULL_ASSERT
  246. /**
  247. * @brief Reports the name of the source file and the source line number
  248. * where the assert_param error has occurred.
  249. * @param file: pointer to the source file name
  250. * @param line: assert_param error line source number
  251. * @retval None
  252. */
  253. void assert_failed(uint8_t *file, uint32_t line)
  254. {
  255. /* USER CODE BEGIN 6 */
  256. /* User can add his own implementation to report the file name and line number,
  257. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  258. /* USER CODE END 6 */
  259. }
  260. #endif /* USE_FULL_ASSERT */
  261. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/