main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "dma.h"
  23. #include "tim.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. int fputc(int ch, FILE *f)
  35. {
  36. uint8_t temp[10] = {ch};
  37. HAL_UART_Transmit(&huart1, temp,1, 2);
  38. HAL_UART_Transmit(&huart3, temp,1, 2);
  39. return ch;
  40. }
  41. /* USER CODE END PD */
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44. /* USER CODE END PM */
  45. /* Private variables ---------------------------------------------------------*/
  46. /* USER CODE BEGIN PV */
  47. uint8_t data_to_send[50];
  48. uint16_t CRC_value = 0;
  49. uint16_t initial_address = 0x00C1;
  50. uint32_t System_version = Version_sys;
  51. uint16_t BaudrateValue = 0x0000;
  52. uint16_t Adc2_CalibrationValue ;
  53. uint16_t Adc3_CalibrationValue ;
  54. uint8_t Rx_Flag = 0;
  55. uint16_t Rx_Len = 0;
  56. uint8_t Rx_Buf[Rx_Max] = {0};
  57. /* USER CODE END PV */
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. /* USER CODE BEGIN PFP */
  61. /* USER CODE END PFP */
  62. /* Private user code ---------------------------------------------------------*/
  63. /* USER CODE BEGIN 0 */
  64. /* USER CODE END 0 */
  65. /**
  66. * @brief The application entry point.
  67. * @retval int
  68. */
  69. int main(void)
  70. {
  71. /* USER CODE BEGIN 1 */
  72. /* USER CODE END 1 */
  73. /* MCU Configuration--------------------------------------------------------*/
  74. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  75. HAL_Init();
  76. /* USER CODE BEGIN Init */
  77. /* USER CODE END Init */
  78. /* Configure the system clock */
  79. SystemClock_Config();
  80. /* USER CODE BEGIN SysInit */
  81. /* USER CODE END SysInit */
  82. /* Initialize all configured peripherals */
  83. MX_GPIO_Init();
  84. MX_DMA_Init();
  85. MX_USART1_UART_Init();
  86. MX_ADC1_Init();
  87. MX_TIM2_Init();
  88. MX_USART3_UART_Init();
  89. /* USER CODE BEGIN 2 */
  90. HAL_TIM_Base_Start_IT(&htim2);
  91. read_new_address(ADDR_FLASH_PAGE_94);
  92. updatePbStatus(read_flash_16(ADDR_FLASH_PAGE_104));
  93. update_baudrate(read_flash_16(ADDR_FLASH_PAGE_114));
  94. AdcCalibration_init();
  95. HAL_UART_Receive_DMA(&huart1, Rx_Buf, Rx_Max);
  96. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  97. /* USER CODE END 2 */
  98. /* Infinite loop */
  99. /* USER CODE BEGIN WHILE */
  100. while (1)
  101. {
  102. if (Rx_Flag == 1 && (calculate_crc(Rx_Buf,6) == (Rx_Buf[7] << 8 | Rx_Buf[6]))) {
  103. if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  104. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, 0x00, gpioaStatus, 0x00, 0x00};
  105. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  106. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  107. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  108. HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
  109. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  110. Rx_Flag = 0;
  111. HAL_UART_AbortReceive(&huart1);
  112. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  113. }
  114. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  115. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, 0x00, gpiobStatus, 0x00, 0x00};
  116. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  117. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  118. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  119. HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
  120. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  121. Rx_Flag = 0;
  122. HAL_UART_AbortReceive(&huart1);
  123. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  124. }
  125. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x03 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  126. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc2_byte1, adc2_byte2, 0x00, 0x00};
  127. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  128. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  129. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  130. HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
  131. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  132. Rx_Flag = 0;
  133. HAL_UART_AbortReceive(&huart1);
  134. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  135. }
  136. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x04 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  137. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc3_byte1, adc3_byte2, 0x00, 0x00};
  138. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  139. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  140. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  141. HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
  142. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  143. Rx_Flag = 0;
  144. HAL_UART_AbortReceive(&huart1);
  145. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  146. }
  147. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x05 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  148. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc1_byte1, adc1_byte2, 0x00, 0x00};
  149. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  150. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  151. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  152. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
  153. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  154. Rx_Flag = 0;
  155. HAL_UART_AbortReceive(&huart1);
  156. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  157. }
  158. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
  159. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, 0x00, gpioaStatus, 0x00, gpiobStatus, 0x00, 0x00};
  160. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  161. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  162. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  163. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
  164. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  165. Rx_Flag = 0;
  166. HAL_UART_AbortReceive(&huart1);
  167. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  168. }
  169. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x03){
  170. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, 0x00, 0x00};
  171. uint16_t CRC_value = calculate_crc(data_to_send, 9);
  172. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  173. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  174. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 10);
  175. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  176. Rx_Flag = 0;
  177. HAL_UART_AbortReceive(&huart1);
  178. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  179. }
  180. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x04){
  181. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x08, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
  182. uint16_t CRC_value = calculate_crc(data_to_send, 11);
  183. data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
  184. data_to_send[12] = (uint8_t)(CRC_value >> 8);
  185. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 12);
  186. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  187. Rx_Flag = 0;
  188. HAL_UART_AbortReceive(&huart1);
  189. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  190. }
  191. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x01 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x05){
  192. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x0A, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
  193. uint16_t CRC_value = calculate_crc(data_to_send, 13);
  194. data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
  195. data_to_send[14] = (uint8_t)(CRC_value >> 8);
  196. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 15, 14);
  197. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  198. Rx_Flag = 0;
  199. HAL_UART_AbortReceive(&huart1);
  200. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  201. }
  202. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
  203. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, 0x00, 0x00};
  204. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  205. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  206. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  207. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
  208. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  209. Rx_Flag = 0;
  210. HAL_UART_AbortReceive(&huart1);
  211. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  212. }
  213. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x03){
  214. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
  215. uint16_t CRC_value = calculate_crc(data_to_send, 9);
  216. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  217. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  218. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 10);
  219. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  220. Rx_Flag = 0;
  221. HAL_UART_AbortReceive(&huart1);
  222. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  223. }
  224. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x04){
  225. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x08, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
  226. uint16_t CRC_value = calculate_crc(data_to_send, 11);
  227. data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
  228. data_to_send[12] = (uint8_t)(CRC_value >> 8);
  229. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 12);
  230. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  231. Rx_Flag = 0;
  232. HAL_UART_AbortReceive(&huart1);
  233. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  234. }
  235. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x03 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
  236. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
  237. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  238. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  239. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  240. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
  241. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  242. Rx_Flag = 0;
  243. HAL_UART_AbortReceive(&huart1);
  244. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  245. }
  246. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x03 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x03){
  247. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
  248. uint16_t CRC_value = calculate_crc(data_to_send, 9);
  249. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  250. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  251. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 10);
  252. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  253. Rx_Flag = 0;
  254. HAL_UART_AbortReceive(&huart1);
  255. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  256. }
  257. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x04 && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
  258. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
  259. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  260. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  261. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  262. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
  263. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  264. Rx_Flag = 0;
  265. HAL_UART_AbortReceive(&huart1);
  266. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  267. }
  268. else if((Rx_Buf[0] == (uint8_t)initial_address || Rx_Buf[0] == 0xFA) && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xAA && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  269. uint8_t data_to_send[] = {0xFA, 0x03, 0x02, 0x00, (uint8_t)initial_address, 0x00, 0x00};
  270. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  271. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  272. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  273. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 6);
  274. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  275. Rx_Flag = 0;
  276. HAL_UART_AbortReceive(&huart1);
  277. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  278. }
  279. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x03 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xBB && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x02){
  280. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, (uint8_t)(System_version >> 24), (uint8_t)(System_version >> 16), (uint8_t)(System_version >> 8), (uint8_t)(System_version ), 0x00, 0x00};
  281. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  282. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  283. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  284. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 8);
  285. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  286. Rx_Flag = 0;
  287. HAL_UART_AbortReceive(&huart1);
  288. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  289. }
  290. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x02 && Rx_Buf[4] == 0x00){
  291. updatePbStatus(Rx_Buf[5]);
  292. Write_Information(ADDR_FLASH_PAGE_104, Rx_Buf, 5); // 写入Flash
  293. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, Rx_Buf[5], 0x00, 0x00};
  294. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  295. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  296. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  297. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
  298. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  299. Rx_Flag = 0;
  300. HAL_UART_AbortReceive(&huart1);
  301. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  302. }
  303. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x0A && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  304. Write_Information(ADDR_FLASH_PAGE_124, Rx_Buf, 0);
  305. uint16_t now_calibrationValue = Adc2_CalibrationValue;
  306. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, (uint8_t)((now_calibrationValue >> 8) & 0xFF), (uint8_t)(now_calibrationValue & 0xFF), 0x00, 0x00};
  307. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  308. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  309. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  310. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
  311. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  312. Rx_Flag = 0;
  313. HAL_UART_AbortReceive(&huart1);
  314. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  315. }
  316. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x0B && Rx_Buf[4] == 0x00 && Rx_Buf[5] == 0x01){
  317. Write_Information(ADDR_FLASH_PAGE_134, Rx_Buf, 0);
  318. uint16_t now_calibrationValue = Adc3_CalibrationValue;
  319. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, (uint8_t)((now_calibrationValue >> 8) & 0xFF), (uint8_t)(now_calibrationValue & 0xFF), 0x00, 0x00};
  320. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  321. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  322. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  323. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
  324. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  325. Rx_Flag = 0;
  326. HAL_UART_AbortReceive(&huart1);
  327. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  328. }
  329. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0x0C && Rx_Buf[4] == 0x00){
  330. update_baudrate(Rx_Buf[5]);
  331. Write_Information(ADDR_FLASH_PAGE_114, Rx_Buf, 5); // 写入Flash
  332. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x00, 0x0C, 0x00, Rx_Buf[5], 0x00, 0x00};
  333. uint16_t CRC_value = calculate_crc(data_to_send, 6);
  334. data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
  335. data_to_send[7] = (uint8_t)(CRC_value >> 8);
  336. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 8, 8);
  337. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  338. Rx_Flag = 0;
  339. HAL_UART_AbortReceive(&huart1);
  340. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  341. }
  342. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xAA && Rx_Buf[4] == 0x00){
  343. Write_Information(ADDR_FLASH_PAGE_94, Rx_Buf, 5); // 写入Flash
  344. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, (uint8_t)initial_address, 0x00, 0x00};
  345. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  346. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  347. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  348. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 7);
  349. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  350. Rx_Flag = 0;
  351. HAL_UART_AbortReceive(&huart1);
  352. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  353. }
  354. else if(Rx_Buf[0] == (uint8_t)initial_address && Rx_Buf[1] == 0x06 && Rx_Buf[2] == 0x00 && Rx_Buf[3] == 0xCC && Rx_Buf[4] == 0xA5 && Rx_Buf[5] == 0x5A){
  355. Rx_Flag = 0; // 将标志位重新置为0
  356. memset(Rx_Buf,0,sizeof(Rx_Buf));
  357. HAL_NVIC_SystemReset();
  358. }
  359. else {
  360. memset(Rx_Buf, 0, sizeof(Rx_Buf));
  361. Rx_Flag = 0;
  362. HAL_UART_AbortReceive(&huart1);
  363. HAL_UART_Receive_DMA(&huart1, Rx_Buf, sizeof(Rx_Buf));
  364. }
  365. }
  366. gpioaStatus = GetPaInputStatus();
  367. gpiobStatus = GetPbOutputStatus();
  368. GetADCResults(&hadc1);
  369. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  370. HAL_Delay(1);
  371. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  372. HAL_UART_Receive_DMA(&huart1,Rx_Buf,Rx_Max);
  373. /* USER CODE END WHILE */
  374. /* USER CODE BEGIN 3 */
  375. }
  376. /* USER CODE END 3 */
  377. }
  378. /**
  379. * @brief System Clock Configuration
  380. * @retval None
  381. */
  382. void SystemClock_Config(void)
  383. {
  384. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  385. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  386. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  387. /** Initializes the RCC Oscillators according to the specified parameters
  388. * in the RCC_OscInitTypeDef structure.
  389. */
  390. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  391. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  392. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  393. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  394. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  395. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  396. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  397. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  398. {
  399. Error_Handler();
  400. }
  401. /** Initializes the CPU, AHB and APB buses clocks
  402. */
  403. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  404. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  405. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  406. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  407. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  408. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  409. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  410. {
  411. Error_Handler();
  412. }
  413. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  414. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  415. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  416. {
  417. Error_Handler();
  418. }
  419. }
  420. /* USER CODE BEGIN 4 */
  421. uint16_t read_flash_16(uint32_t addr){
  422. uint16_t data = *(volatile uint16_t*)addr; // 从Flash中读取2个字节
  423. return data;
  424. }
  425. void read_new_address(uint32_t addr){
  426. uint16_t init_address = read_flash_16(addr);
  427. if(init_address == 0xFFFF) {
  428. initial_address = initial_address;
  429. } else {
  430. initial_address = init_address; // 读取新地址并赋值给initial_address
  431. }
  432. }
  433. void erase_flash(uint32_t ADDR_FLASH){ //进行擦除
  434. FLASH_EraseInitTypeDef erase_init;
  435. erase_init.TypeErase = FLASH_TYPEERASE_PAGES; // 擦除类型为页擦除
  436. erase_init.PageAddress = ADDR_FLASH;
  437. erase_init.NbPages = 1; // 擦除的页数
  438. uint32_t page_error = 0;
  439. HAL_FLASH_Unlock(); // 解锁Flash
  440. HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &page_error); // 执行擦除操作
  441. HAL_FLASH_Lock(); // 上锁Flash
  442. if(status == HAL_OK){
  443. if(ADDR_FLASH == ADDR_FLASH_PAGE_94) {
  444. initial_address = 0; // 将initial_address清零
  445. }
  446. else if(ADDR_FLASH == ADDR_FLASH_PAGE_104) {
  447. gpiobStatus = 0; // 将pb_status清零
  448. }
  449. else if(ADDR_FLASH == ADDR_FLASH_PAGE_114) {
  450. BaudrateValue = 0xFFFF;
  451. }
  452. else if(ADDR_FLASH == ADDR_FLASH_PAGE_124) {
  453. Adc2_CalibrationValue = 0x0000; // 清零
  454. }
  455. else if(ADDR_FLASH == ADDR_FLASH_PAGE_134) {
  456. Adc3_CalibrationValue = 0x0000; // 清零
  457. }
  458. }
  459. }
  460. void Write_Information(uint32_t addr, uint8_t* rx_buffer, uint8_t buffer_index) {
  461. uint16_t newValue = 0;
  462. erase_flash(addr);
  463. HAL_FLASH_Unlock(); // 解锁Flash
  464. if (addr == ADDR_FLASH_PAGE_94) { // 写入地址到Flash
  465. newValue = rx_buffer[buffer_index];
  466. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_94, newValue);
  467. if (status == HAL_OK) {
  468. initial_address = newValue; // 将新地址赋值给initial_address
  469. }
  470. }
  471. else if (addr == ADDR_FLASH_PAGE_104) { // 写入PB状态到Flash
  472. newValue = rx_buffer[buffer_index];
  473. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_104, newValue);
  474. if (status == HAL_OK) {
  475. gpiobStatus = newValue; // 将新状态赋值给gpiobStatus
  476. }
  477. }
  478. else if (addr == ADDR_FLASH_PAGE_114) { // 写入波特率到Flash
  479. newValue = rx_buffer[buffer_index];
  480. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_114, newValue);
  481. if (status == HAL_OK) {
  482. BaudrateValue = newValue; // 将新状态赋值给BaudrateValue
  483. }
  484. }
  485. else if (addr == ADDR_FLASH_PAGE_124 || addr == ADDR_FLASH_PAGE_134) { // 写入ADC校准值到Flash
  486. uint16_t Standard_value = (0.6 / (3.3 / 4096));
  487. if (addr == ADDR_FLASH_PAGE_124) {
  488. if (adc2_rawValue >= Standard_value) {
  489. newValue = adc2_rawValue - Standard_value;
  490. } else {
  491. newValue = Standard_value - adc2_rawValue;
  492. newValue |= 0x8000;
  493. }
  494. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_124, newValue);
  495. if (status == HAL_OK) {
  496. Adc2_CalibrationValue = newValue & 0x7FFF;
  497. Value_old_addr2 = newValue;
  498. }
  499. } else if (addr == ADDR_FLASH_PAGE_134) {
  500. if (adc3_rawValue >= Standard_value) {
  501. newValue = adc3_rawValue - Standard_value;
  502. } else {
  503. newValue = Standard_value - adc3_rawValue;
  504. newValue |= 0x8000;
  505. }
  506. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_134, newValue);
  507. if (status == HAL_OK) {
  508. Adc3_CalibrationValue = newValue & 0x7FFF;
  509. Value_old_addr3 = newValue;
  510. }
  511. }
  512. }
  513. HAL_FLASH_Lock(); // 上锁Flash
  514. }
  515. /* USER CODE END 4 */
  516. /**
  517. * @brief This function is executed in case of error occurrence.
  518. * @retval None
  519. */
  520. void Error_Handler(void)
  521. {
  522. /* USER CODE BEGIN Error_Handler_Debug */
  523. /* User can add his own implementation to report the HAL error return state */
  524. __disable_irq();
  525. while (1)
  526. {
  527. }
  528. /* USER CODE END Error_Handler_Debug */
  529. }
  530. #ifdef USE_FULL_ASSERT
  531. /**
  532. * @brief Reports the name of the source file and the source line number
  533. * where the assert_param error has occurred.
  534. * @param file: pointer to the source file name
  535. * @param line: assert_param error line source number
  536. * @retval None
  537. */
  538. void assert_failed(uint8_t *file, uint32_t line)
  539. {
  540. /* USER CODE BEGIN 6 */
  541. /* User can add his own implementation to report the file name and line number,
  542. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  543. /* USER CODE END 6 */
  544. }
  545. #endif /* USE_FULL_ASSERT */