main.c 29 KB

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