main.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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[50];
  47. uint16_t CRC_value = 0;
  48. uint16_t initial_address = 0x00C1;
  49. uint32_t System_version = 0x23051600;
  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. /* USER CODE END 2 */
  91. /* Infinite loop */
  92. /* USER CODE BEGIN WHILE */
  93. while (1)
  94. {
  95. C1:
  96. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  97. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  98. #if Modbus
  99. gpioaStatus = GetPaInputStatus();
  100. gpiobStatus = GetPbOutputStatus();
  101. GetADCResults(&hadc1);
  102. if (flag == 1 && (calculate_crc(receive_buffer,6) == (receive_buffer[7] << 8 | receive_buffer[6]))) // 如果标志位为1
  103. {
  104. 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){
  105. data_to_send[0] = (uint8_t)initial_address;
  106. data_to_send[1] = 0x03;
  107. data_to_send[2] = 0x02;
  108. data_to_send[3] = 0x00;
  109. data_to_send[4] = gpioaStatus;
  110. CRC_value = calculate_crc(data_to_send,5);
  111. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  112. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  113. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  114. if (status == HAL_OK) {
  115. // 发送成功
  116. flag = 0; // 将标志位重新置为0
  117. memset(receive_buffer,0,sizeof(receive_buffer));
  118. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  119. } else {
  120. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  121. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  122. memset(receive_buffer,0,sizeof(receive_buffer));
  123. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  124. }
  125. goto C1;
  126. }
  127. else 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){
  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] = 0x00;
  132. data_to_send[4] = gpiobStatus;
  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_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  137. if (status == HAL_OK) {
  138. // 发送成功
  139. flag = 0; // 将标志位重新置为0
  140. memset(receive_buffer,0,sizeof(receive_buffer));
  141. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  142. } else {
  143. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  144. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  145. memset(receive_buffer,0,sizeof(receive_buffer));
  146. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  147. }
  148. goto C1;
  149. }
  150. else 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){
  151. data_to_send[0] = (uint8_t)initial_address;
  152. data_to_send[1] = 0x03;
  153. data_to_send[2] = 0x02;
  154. data_to_send[3] = adc2_byte1;
  155. data_to_send[4] = adc2_byte2;
  156. CRC_value = calculate_crc(data_to_send,5);
  157. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  158. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  159. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  160. if (status == HAL_OK) {
  161. // 发送成功
  162. flag = 0; // 将标志位重新置为0
  163. memset(receive_buffer,0,sizeof(receive_buffer));
  164. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  165. } else {
  166. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  167. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  168. memset(receive_buffer,0,sizeof(receive_buffer));
  169. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  170. }
  171. goto C1;
  172. }
  173. else 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){
  174. data_to_send[0] = (uint8_t)initial_address;
  175. data_to_send[1] = 0x03;
  176. data_to_send[2] = 0x02;
  177. data_to_send[3] = adc3_byte1;
  178. data_to_send[4] = adc3_byte2;
  179. CRC_value = calculate_crc(data_to_send,5);
  180. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  181. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  182. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  183. if (status == HAL_OK) {
  184. // 发送成功
  185. flag = 0; // 将标志位重新置为0
  186. memset(receive_buffer,0,sizeof(receive_buffer));
  187. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  188. } else {
  189. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  190. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  191. memset(receive_buffer,0,sizeof(receive_buffer));
  192. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  193. }
  194. goto C1;
  195. }
  196. else 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){
  197. data_to_send[0] = (uint8_t)initial_address;
  198. data_to_send[1] = 0x03;
  199. data_to_send[2] = 0x02;
  200. data_to_send[3] = adc1_byte1;
  201. data_to_send[4] = adc1_byte2;
  202. CRC_value = calculate_crc(data_to_send,5);
  203. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  204. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  205. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  206. if (status == HAL_OK) {
  207. // 发送成功
  208. flag = 0; // 将标志位重新置为0
  209. memset(receive_buffer,0,sizeof(receive_buffer));
  210. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  211. } else {
  212. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  213. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  214. memset(receive_buffer,0,sizeof(receive_buffer));
  215. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  216. }
  217. goto C1;
  218. }
  219. else 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){
  220. erase_flash(ADDR_FLASH_PAGE_64);
  221. write_flash_autoAdc(ADDR_FLASH_PAGE_64);
  222. uint16_t now_calibrationValue = Adc2_CalibrationValue;
  223. CoverADC(ADDR_FLASH_PAGE_64);
  224. data_to_send[0] = (uint8_t)initial_address;
  225. data_to_send[1] = 0x06;
  226. data_to_send[2] = 0x02;
  227. data_to_send[3] = (uint8_t)((now_calibrationValue >> 8) & 0xFF);
  228. data_to_send[4] = (uint8_t)(now_calibrationValue & 0xFF); // 获取低8位
  229. CRC_value = calculate_crc(data_to_send,5);
  230. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  231. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  232. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  233. if (status == HAL_OK) {
  234. // 发送成功
  235. flag = 0; // 将标志位重新置为0
  236. memset(receive_buffer,0,sizeof(receive_buffer));
  237. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  238. } else {
  239. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  240. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  241. memset(receive_buffer,0,sizeof(receive_buffer));
  242. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  243. }
  244. goto C1;
  245. }
  246. else 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){
  247. erase_flash(ADDR_FLASH_PAGE_65);
  248. write_flash_autoAdc(ADDR_FLASH_PAGE_65);
  249. uint16_t now_calibrationValue = Adc3_CalibrationValue;
  250. CoverADC(ADDR_FLASH_PAGE_65);
  251. data_to_send[0] = (uint8_t)initial_address; // 将接收到的数据存储到一个新的数组中
  252. data_to_send[1] = 0x06;
  253. data_to_send[2] = 0x02; // 将接收到的数据存储到一个新的数组中
  254. data_to_send[3] = (uint8_t)((now_calibrationValue >> 8) & 0xFF);
  255. data_to_send[4] = (uint8_t)(now_calibrationValue & 0xFF); // 获取低8位
  256. CRC_value = calculate_crc(data_to_send,5);
  257. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  258. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  259. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  260. if (status == HAL_OK) {
  261. // 发送成功
  262. flag = 0; // 将标志位重新置为0
  263. memset(receive_buffer,0,sizeof(receive_buffer));
  264. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  265. } else {
  266. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  267. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  268. memset(receive_buffer,0,sizeof(receive_buffer));
  269. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  270. }
  271. goto C1;
  272. }
  273. else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 ){
  274. erase_flash(ADDR_FLASH_PAGE_61);
  275. write_flash_address(receive_buffer); // 写入Flash
  276. data_to_send[0] = (uint8_t)initial_address; // 将接收到的数据存储到一个新的数组中
  277. data_to_send[1] = 0x06;
  278. data_to_send[2] = 0x02; // 将接收到的数据存储到一个新的数组中
  279. data_to_send[3] = 0x00;
  280. data_to_send[4] = receive_buffer[5]; // 获取低8位
  281. CRC_value = calculate_crc(data_to_send,5);
  282. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  283. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  284. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  285. if (status == HAL_OK) {
  286. // 发送成功
  287. flag = 0; // 将标志位重新置为0
  288. memset(receive_buffer,0,sizeof(receive_buffer));
  289. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  290. } else {
  291. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  292. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  293. memset(receive_buffer,0,sizeof(receive_buffer));
  294. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  295. }
  296. goto C1;
  297. }
  298. else 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){
  299. data_to_send[0] = (uint8_t)initial_address;
  300. data_to_send[1] = 0x03;
  301. data_to_send[2] = 0x04;
  302. data_to_send[3] = (uint8_t)(System_version >> 24);
  303. data_to_send[4] = (uint8_t)(System_version >> 16);
  304. data_to_send[5] = (uint8_t)(System_version >> 8);
  305. data_to_send[6] = (uint8_t)(System_version );
  306. CRC_value = calculate_crc(data_to_send,7);
  307. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  308. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  309. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  310. if (status == HAL_OK) {
  311. // 发送成功
  312. flag = 0; // 将标志位重新置为0
  313. memset(receive_buffer,0,sizeof(receive_buffer));
  314. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  315. } else {
  316. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  317. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  318. memset(receive_buffer,0,sizeof(receive_buffer));
  319. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  320. }
  321. goto C1;
  322. }
  323. else 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){
  324. memset(receive_buffer,0,sizeof(receive_buffer));
  325. HAL_NVIC_SystemReset();
  326. flag = 0; // 将标志位重新置为0
  327. }
  328. else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 ){
  329. updatePbStatus(receive_buffer[5]);
  330. erase_flash(ADDR_FLASH_PAGE_62);
  331. write_flash_PbStatus(receive_buffer); // 写入Flash
  332. data_to_send[0] = (uint8_t)initial_address;
  333. data_to_send[1] = 0x06;
  334. data_to_send[2] = 0x02;
  335. data_to_send[3] = 0x00;
  336. data_to_send[4] = receive_buffer[5];
  337. CRC_value = calculate_crc(data_to_send,5);
  338. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  339. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  340. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  341. if (status == HAL_OK) {
  342. // 发送成功
  343. flag = 0; // 将标志位重新置为0
  344. memset(receive_buffer,0,sizeof(receive_buffer));
  345. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  346. } else {
  347. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  348. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  349. memset(receive_buffer,0,sizeof(receive_buffer));
  350. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  351. }
  352. goto C1;
  353. }
  354. else 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 ){
  355. data_to_send[0] = (uint8_t)initial_address;
  356. data_to_send[1] = 0x03;
  357. data_to_send[2] = 0x04;
  358. data_to_send[3] = 0x00;
  359. data_to_send[4] = gpioaStatus;
  360. data_to_send[5] = 0x00;
  361. data_to_send[6] = gpiobStatus;
  362. CRC_value = calculate_crc(data_to_send,7);
  363. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  364. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  365. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  366. if (status == HAL_OK) {
  367. // 发送成功
  368. flag = 0; // 将标志位重新置为0
  369. memset(receive_buffer,0,sizeof(receive_buffer));
  370. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  371. } else {
  372. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  373. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  374. memset(receive_buffer,0,sizeof(receive_buffer));
  375. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  376. }
  377. goto C1;
  378. }
  379. else 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 ){
  380. data_to_send[0] = (uint8_t)initial_address;
  381. data_to_send[1] = 0x03;
  382. data_to_send[2] = 0x06;
  383. data_to_send[3] = 0x00;
  384. data_to_send[4] = gpioaStatus;
  385. data_to_send[5] = 0x00;
  386. data_to_send[6] = gpiobStatus;
  387. data_to_send[7] = adc2_byte1;
  388. data_to_send[8] = adc2_byte2;
  389. CRC_value = calculate_crc(data_to_send,9);
  390. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  391. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  392. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 15);
  393. if (status == HAL_OK) {
  394. // 发送成功
  395. flag = 0; // 将标志位重新置为0
  396. memset(receive_buffer,0,sizeof(receive_buffer));
  397. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  398. } else {
  399. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  400. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  401. memset(receive_buffer,0,sizeof(receive_buffer));
  402. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  403. }
  404. goto C1;
  405. }
  406. else 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 ){
  407. data_to_send[0] = (uint8_t)initial_address;
  408. data_to_send[1] = 0x03;
  409. data_to_send[2] = 0x08;
  410. data_to_send[3] = 0x00;
  411. data_to_send[4] = gpioaStatus;
  412. data_to_send[5] = 0x00;
  413. data_to_send[6] = gpiobStatus;
  414. data_to_send[7] = adc2_byte1;
  415. data_to_send[8] = adc2_byte2;
  416. data_to_send[9] = adc3_byte1;
  417. data_to_send[10] = adc3_byte2;
  418. CRC_value = calculate_crc(data_to_send,11);
  419. data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
  420. data_to_send[12] = (uint8_t)(CRC_value >> 8);
  421. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
  422. if (status == HAL_OK) {
  423. // 发送成功
  424. flag = 0; // 将标志位重新置为0
  425. memset(receive_buffer,0,sizeof(receive_buffer));
  426. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  427. } else {
  428. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  429. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  430. memset(receive_buffer,0,sizeof(receive_buffer));
  431. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  432. }
  433. goto C1;
  434. }
  435. else 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 ){
  436. data_to_send[0] = (uint8_t)initial_address;
  437. data_to_send[1] = 0x03;
  438. data_to_send[2] = 0x0A;
  439. data_to_send[3] = 0x00;
  440. data_to_send[4] = gpioaStatus;
  441. data_to_send[5] = 0x00;
  442. data_to_send[6] = gpiobStatus;
  443. data_to_send[7] = adc2_byte1;
  444. data_to_send[8] = adc2_byte2;
  445. data_to_send[9] = adc3_byte1;
  446. data_to_send[10] = adc3_byte2;
  447. data_to_send[11] = adc1_byte1;
  448. data_to_send[12] = adc1_byte2;
  449. CRC_value = calculate_crc(data_to_send,13);
  450. data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
  451. data_to_send[14] = (uint8_t)(CRC_value >> 8);
  452. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 15, 16);
  453. if (status == HAL_OK) {
  454. // 发送成功
  455. flag = 0; // 将标志位重新置为0
  456. memset(receive_buffer,0,sizeof(receive_buffer));
  457. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  458. } else {
  459. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  460. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  461. memset(receive_buffer,0,sizeof(receive_buffer));
  462. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  463. }
  464. goto C1;
  465. }
  466. else 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 ){
  467. data_to_send[0] = (uint8_t)initial_address;
  468. data_to_send[1] = 0x03;
  469. data_to_send[2] = 0x04;
  470. data_to_send[3] = 0x00;
  471. data_to_send[4] = gpiobStatus;
  472. data_to_send[5] = adc2_byte1;
  473. data_to_send[6] = adc2_byte2;
  474. CRC_value = calculate_crc(data_to_send,7);
  475. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  476. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  477. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  478. if (status == HAL_OK) {
  479. // 发送成功
  480. flag = 0; // 将标志位重新置为0
  481. memset(receive_buffer,0,sizeof(receive_buffer));
  482. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  483. } else {
  484. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  485. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  486. memset(receive_buffer,0,sizeof(receive_buffer));
  487. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  488. }
  489. goto C1;
  490. }
  491. else 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 ){
  492. data_to_send[0] = (uint8_t)initial_address;
  493. data_to_send[1] = 0x03;
  494. data_to_send[2] = 0x06;
  495. data_to_send[3] = 0x00;
  496. data_to_send[4] = gpiobStatus;
  497. data_to_send[5] = adc2_byte1;
  498. data_to_send[6] = adc2_byte2;
  499. data_to_send[7] = adc3_byte1;
  500. data_to_send[8] = adc3_byte2;
  501. CRC_value = calculate_crc(data_to_send,9);
  502. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  503. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  504. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 15);
  505. if (status == HAL_OK) {
  506. // 发送成功
  507. flag = 0; // 将标志位重新置为0
  508. memset(receive_buffer,0,sizeof(receive_buffer));
  509. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  510. } else {
  511. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  512. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  513. memset(receive_buffer,0,sizeof(receive_buffer));
  514. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  515. }
  516. goto C1;
  517. }
  518. else 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 ){
  519. data_to_send[0] = (uint8_t)initial_address;
  520. data_to_send[1] = 0x03;
  521. data_to_send[2] = 0x08;
  522. data_to_send[3] = 0x00;
  523. data_to_send[4] = gpiobStatus;
  524. data_to_send[5] = adc2_byte1;
  525. data_to_send[6] = adc2_byte2;
  526. data_to_send[7] = adc3_byte1;
  527. data_to_send[8] = adc3_byte2;
  528. data_to_send[9] = adc1_byte1;
  529. data_to_send[10] = adc1_byte2;
  530. CRC_value = calculate_crc(data_to_send,11);
  531. data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
  532. data_to_send[12] = (uint8_t)(CRC_value >> 8);
  533. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
  534. if (status == HAL_OK) {
  535. // 发送成功
  536. flag = 0; // 将标志位重新置为0
  537. memset(receive_buffer,0,sizeof(receive_buffer));
  538. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  539. } else {
  540. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  541. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  542. memset(receive_buffer,0,sizeof(receive_buffer));
  543. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  544. }
  545. goto C1;
  546. }
  547. else 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 ){
  548. data_to_send[0] = (uint8_t)initial_address;
  549. data_to_send[1] = 0x03;
  550. data_to_send[2] = 0x04;
  551. data_to_send[3] = adc2_byte1;
  552. data_to_send[4] = adc2_byte2;
  553. data_to_send[5] = adc3_byte1;
  554. data_to_send[6] = adc3_byte2;
  555. CRC_value = calculate_crc(data_to_send,7);
  556. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  557. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  558. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  559. if (status == HAL_OK) {
  560. // 发送成功
  561. flag = 0; // 将标志位重新置为0
  562. memset(receive_buffer,0,sizeof(receive_buffer));
  563. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  564. } else {
  565. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  566. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  567. memset(receive_buffer,0,sizeof(receive_buffer));
  568. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  569. }
  570. goto C1;
  571. }
  572. else 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 ){
  573. data_to_send[0] = (uint8_t)initial_address;
  574. data_to_send[1] = 0x03;
  575. data_to_send[2] = 0x06;
  576. data_to_send[3] = adc2_byte1;
  577. data_to_send[4] = adc2_byte2;
  578. data_to_send[5] = adc3_byte1;
  579. data_to_send[6] = adc3_byte2;
  580. data_to_send[7] = adc1_byte1;
  581. data_to_send[8] = adc1_byte2;
  582. CRC_value = calculate_crc(data_to_send,9);
  583. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  584. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  585. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 15);
  586. if (status == HAL_OK) {
  587. // 发送成功
  588. flag = 0; // 将标志位重新置为0
  589. memset(receive_buffer,0,sizeof(receive_buffer));
  590. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  591. } else {
  592. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  593. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  594. memset(receive_buffer,0,sizeof(receive_buffer));
  595. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  596. }
  597. goto C1;
  598. }
  599. else 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 ){
  600. data_to_send[0] = (uint8_t)initial_address;
  601. data_to_send[1] = 0x03;
  602. data_to_send[2] = 0x04;
  603. data_to_send[3] = adc3_byte1;
  604. data_to_send[4] = adc3_byte2;
  605. data_to_send[5] = adc1_byte1;
  606. data_to_send[6] = adc1_byte2;
  607. CRC_value = calculate_crc(data_to_send,7);
  608. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  609. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  610. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  611. if (status == HAL_OK) {
  612. // 发送成功
  613. flag = 0; // 将标志位重新置为0
  614. memset(receive_buffer,0,sizeof(receive_buffer));
  615. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  616. } else {
  617. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  618. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  619. memset(receive_buffer,0,sizeof(receive_buffer));
  620. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  621. }
  622. goto C1;
  623. }
  624. else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0C && receive_buffer[4] == 0x00 ){
  625. update_baudrate(receive_buffer[5]);
  626. erase_flash(ADDR_FLASH_PAGE_63);
  627. write_flash_Baudrate(receive_buffer); // 写入Flash
  628. data_to_send[0] = (uint8_t)initial_address; // 将接收到的数据存储到一个新的数组中
  629. data_to_send[1] = 0x06;
  630. data_to_send[2] = 0x00; // 将接收到的数据存储到一个新的数组中
  631. data_to_send[3] = 0x0C;
  632. data_to_send[4] = 0x00;
  633. data_to_send[5] = receive_buffer[5];
  634. CRC_value = calculate_crc(data_to_send,6);
  635. data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
  636. data_to_send[7] = (uint8_t)(CRC_value >> 8);
  637. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 8, 15);
  638. if (status == HAL_OK) {
  639. // 发送成功
  640. flag = 0; // 将标志位重新置为0
  641. memset(receive_buffer,0,sizeof(receive_buffer));
  642. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  643. } else {
  644. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  645. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  646. memset(receive_buffer,0,sizeof(receive_buffer));
  647. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  648. }
  649. goto C1;
  650. }
  651. else 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 ){
  652. data_to_send[0] = 0xFA;
  653. data_to_send[1] = 0x03;
  654. data_to_send[2] = 0x02;
  655. data_to_send[3] = 0x00;
  656. data_to_send[4] = (uint8_t)initial_address;
  657. CRC_value = calculate_crc(data_to_send,5);
  658. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  659. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  660. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  661. if (status == HAL_OK) {
  662. // 发送成功
  663. flag = 0; // 将标志位重新置为0
  664. memset(receive_buffer,0,sizeof(receive_buffer));
  665. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  666. } else {
  667. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  668. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  669. memset(receive_buffer,0,sizeof(receive_buffer));
  670. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  671. }
  672. goto C1;
  673. }
  674. }
  675. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  676. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  677. HAL_Delay(1);
  678. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  679. #endif
  680. /* USER CODE END WHILE */
  681. /* USER CODE BEGIN 3 */
  682. }
  683. /* USER CODE END 3 */
  684. }
  685. /**
  686. * @brief System Clock Configuration
  687. * @retval None
  688. */
  689. void SystemClock_Config(void)
  690. {
  691. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  692. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  693. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  694. /** Initializes the RCC Oscillators according to the specified parameters
  695. * in the RCC_OscInitTypeDef structure.
  696. */
  697. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  698. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  699. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  700. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  701. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  702. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  703. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  704. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  705. {
  706. Error_Handler();
  707. }
  708. /** Initializes the CPU, AHB and APB buses clocks
  709. */
  710. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  711. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  712. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  713. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  714. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  715. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  716. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  717. {
  718. Error_Handler();
  719. }
  720. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  721. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  722. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  723. {
  724. Error_Handler();
  725. }
  726. }
  727. /* USER CODE BEGIN 4 */
  728. uint16_t read_flash_16(uint32_t flash_address){
  729. uint16_t data = *(volatile uint16_t*)flash_address; // 从Flash中读取2个字节
  730. return data;
  731. }
  732. void read_new_address_from_flash(uint32_t flash_address){
  733. uint16_t init_address = read_flash_16(flash_address);
  734. if(init_address == 0xFFFF) {
  735. initial_address = initial_address;
  736. } else {
  737. initial_address = read_flash_16(flash_address); // 读取新地址并赋值给initial_address
  738. }
  739. }
  740. void erase_flash(uint32_t ADDR_FLASH){ //进行擦除
  741. FLASH_EraseInitTypeDef erase_init;
  742. erase_init.TypeErase = FLASH_TYPEERASE_PAGES; // 擦除类型为页擦除
  743. erase_init.PageAddress = ADDR_FLASH;
  744. erase_init.NbPages = 1; // 擦除的页数
  745. uint32_t page_error = 0;
  746. HAL_FLASH_Unlock(); // 解锁Flash
  747. HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &page_error); // 执行擦除操作
  748. HAL_FLASH_Lock(); // 上锁Flash
  749. if(status == HAL_OK){
  750. if(ADDR_FLASH == ADDR_FLASH_PAGE_61) {
  751. initial_address = 0; // 将initial_address清零
  752. }
  753. if(ADDR_FLASH == ADDR_FLASH_PAGE_62) {
  754. gpiobStatus = 0; // 将pb_status清零
  755. }
  756. if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
  757. Adc2_CalibrationValue = 0x0000; // 清零
  758. }
  759. if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
  760. Adc3_CalibrationValue = 0x0000; // 清零
  761. }
  762. if(ADDR_FLASH == ADDR_FLASH_PAGE_63) {
  763. BaudrateValue = 0xFFFF;
  764. }
  765. }
  766. }
  767. void write_flash_address(uint8_t* rx_buffer) { //数组位置5进行写入——地址
  768. uint8_t newAddress = rx_buffer[5];
  769. HAL_FLASH_Unlock(); // 解锁Flash
  770. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_61, newAddress); // 写入新地址到Flash
  771. HAL_FLASH_Lock(); // 上锁Flash
  772. if(status == HAL_OK){
  773. initial_address = newAddress; // 将新地址赋值给initial_address
  774. }
  775. }
  776. void write_flash_PbStatus(uint8_t* rx_buffer){ //数组位置5进行写入——PB
  777. uint8_t newStatus = rx_buffer[5];
  778. HAL_FLASH_Unlock(); // 解锁Flash
  779. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_62, newStatus); // 写入PbStatus到Flash
  780. HAL_FLASH_Lock(); // 上锁Flash
  781. if(status == HAL_OK){
  782. gpiobStatus = newStatus; // 将新状态赋值给pb_status
  783. }
  784. }
  785. void write_flash_autoAdc(uint32_t ADDR_FLASH){
  786. uint16_t Standard_value;
  787. Standard_value = (0.6/(3.3/4096));
  788. uint16_t newValue = 0;
  789. if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
  790. if(adc2_rawValue >= Standard_value) {
  791. newValue = adc2_rawValue - Standard_value;
  792. } else {
  793. newValue = Standard_value - adc2_rawValue;
  794. newValue |= 0x8000;
  795. }
  796. HAL_FLASH_Unlock(); // 解锁Flash
  797. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue);
  798. HAL_FLASH_Lock(); // 上锁Flash
  799. if(status == HAL_OK){
  800. Adc2_CalibrationValue = newValue & 0x7FFF;
  801. }
  802. }
  803. if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
  804. if(adc3_rawValue >= Standard_value) {
  805. newValue = adc3_rawValue - Standard_value;
  806. } else {
  807. newValue = Standard_value - adc3_rawValue;
  808. newValue |= 0x8000;
  809. }
  810. HAL_FLASH_Unlock(); // 解锁Flash
  811. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue);
  812. HAL_FLASH_Lock(); // 上锁Flash
  813. if(status == HAL_OK){
  814. Adc3_CalibrationValue = newValue & 0x7FFF;
  815. }
  816. }
  817. }
  818. void CoverADC(uint32_t ADDR) {
  819. uint16_t Overwrite_Value = read_flash_16(ADDR);
  820. if (ADDR == ADDR_FLASH_PAGE_64) {
  821. Value_old_addr2 = Overwrite_Value;
  822. }
  823. if (ADDR == ADDR_FLASH_PAGE_65) {
  824. Value_old_addr3 = Overwrite_Value;
  825. }
  826. }
  827. void write_flash_Baudrate(uint8_t* rx_buffer){ //数组位置5进行写入——Baudrate
  828. uint8_t newStatus = rx_buffer[5];
  829. HAL_FLASH_Unlock(); // 解锁Flash
  830. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_63, newStatus); // 写入PbStatus到Flash
  831. HAL_FLASH_Lock(); // 上锁Flash
  832. if(status == HAL_OK){
  833. BaudrateValue = newStatus; // 将新状态赋值给pb_status
  834. }
  835. }
  836. /* USER CODE END 4 */
  837. /**
  838. * @brief This function is executed in case of error occurrence.
  839. * @retval None
  840. */
  841. void Error_Handler(void)
  842. {
  843. /* USER CODE BEGIN Error_Handler_Debug */
  844. /* User can add his own implementation to report the HAL error return state */
  845. __disable_irq();
  846. while (1)
  847. {
  848. }
  849. /* USER CODE END Error_Handler_Debug */
  850. }
  851. #ifdef USE_FULL_ASSERT
  852. /**
  853. * @brief Reports the name of the source file and the source line number
  854. * where the assert_param error has occurred.
  855. * @param file: pointer to the source file name
  856. * @param line: assert_param error line source number
  857. * @retval None
  858. */
  859. void assert_failed(uint8_t *file, uint32_t line)
  860. {
  861. /* USER CODE BEGIN 6 */
  862. /* User can add his own implementation to report the file name and line number,
  863. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  864. /* USER CODE END 6 */
  865. }
  866. #endif /* USE_FULL_ASSERT */