main.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 = 0x23051700;
  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. #if Modbus
  96. if(flag == 1 && (calculate_crc(receive_buffer,6) == (receive_buffer[7] << 8 | receive_buffer[6]))) {
  97. 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){
  98. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, 0x00, gpioaStatus, 0x00, 0x00};
  99. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  100. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  101. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  102. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  103. if(status == HAL_OK) {
  104. memset(receive_buffer, 0, sizeof(receive_buffer));
  105. flag = 0;
  106. } else {
  107. memset(receive_buffer, 0, sizeof(receive_buffer));
  108. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  109. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  110. }
  111. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  112. }
  113. 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){
  114. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, 0x00, gpiobStatus, 0x00, 0x00};
  115. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  116. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  117. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  118. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  119. if(status == HAL_OK) {
  120. memset(receive_buffer, 0, sizeof(receive_buffer));
  121. flag = 0;
  122. } else {
  123. memset(receive_buffer, 0, sizeof(receive_buffer));
  124. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  125. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  126. }
  127. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  128. }
  129. 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){
  130. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc2_byte1, adc2_byte2, 0x00, 0x00};
  131. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  132. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  133. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  134. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  135. if(status == HAL_OK) {
  136. memset(receive_buffer, 0, sizeof(receive_buffer));
  137. flag = 0;
  138. } else {
  139. memset(receive_buffer, 0, sizeof(receive_buffer));
  140. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  141. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  142. }
  143. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  144. }
  145. 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){
  146. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc3_byte1, adc3_byte2, 0x00, 0x00};
  147. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  148. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  149. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  150. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  151. if(status == HAL_OK) {
  152. memset(receive_buffer, 0, sizeof(receive_buffer));
  153. flag = 0;
  154. } else {
  155. memset(receive_buffer, 0, sizeof(receive_buffer));
  156. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  157. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  158. }
  159. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  160. }
  161. 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){
  162. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x02, adc1_byte1, adc1_byte2, 0x00, 0x00};
  163. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  164. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  165. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  166. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  167. if(status == HAL_OK) {
  168. memset(receive_buffer, 0, sizeof(receive_buffer));
  169. flag = 0;
  170. } else {
  171. memset(receive_buffer, 0, sizeof(receive_buffer));
  172. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  173. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  174. }
  175. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  176. }
  177. 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){
  178. 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};
  179. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  180. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  181. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  182. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  183. if(status == HAL_OK) {
  184. memset(receive_buffer, 0, sizeof(receive_buffer));
  185. flag = 0;
  186. } else {
  187. memset(receive_buffer, 0, sizeof(receive_buffer));
  188. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  189. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  190. }
  191. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  192. }
  193. 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){
  194. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, 0x00, gpioaStatus, 0x00, gpiobStatus, 0x00, 0x00};
  195. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  196. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  197. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  198. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  199. if(status == HAL_OK) {
  200. memset(receive_buffer, 0, sizeof(receive_buffer));
  201. flag = 0;
  202. } else {
  203. memset(receive_buffer, 0, sizeof(receive_buffer));
  204. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  205. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  206. }
  207. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  208. }
  209. 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){
  210. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, 0x00, 0x00};
  211. uint16_t CRC_value = calculate_crc(data_to_send, 9);
  212. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  213. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  214. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 15);
  215. if(status == HAL_OK) {
  216. memset(receive_buffer, 0, sizeof(receive_buffer));
  217. flag = 0;
  218. } else {
  219. memset(receive_buffer, 0, sizeof(receive_buffer));
  220. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  221. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  222. }
  223. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  224. }
  225. 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){
  226. 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};
  227. uint16_t CRC_value = calculate_crc(data_to_send, 11);
  228. data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
  229. data_to_send[12] = (uint8_t)(CRC_value >> 8);
  230. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
  231. if(status == HAL_OK) {
  232. memset(receive_buffer, 0, sizeof(receive_buffer));
  233. flag = 0;
  234. } else {
  235. memset(receive_buffer, 0, sizeof(receive_buffer));
  236. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  237. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  238. }
  239. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  240. }
  241. 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){
  242. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x08, 0x00, gpioaStatus, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte2, 0x00, 0x00};
  243. uint16_t CRC_value = calculate_crc(data_to_send, 13);
  244. data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
  245. data_to_send[14] = (uint8_t)(CRC_value >> 8);
  246. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 15, 15);
  247. if(status == HAL_OK) {
  248. memset(receive_buffer, 0, sizeof(receive_buffer));
  249. flag = 0;
  250. } else {
  251. memset(receive_buffer, 0, sizeof(receive_buffer));
  252. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  253. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  254. }
  255. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  256. }
  257. 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){
  258. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, 0x00, gpiobStatus, adc2_byte1, adc2_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, 15);
  263. if(status == HAL_OK) {
  264. memset(receive_buffer, 0, sizeof(receive_buffer));
  265. flag = 0;
  266. } else {
  267. memset(receive_buffer, 0, sizeof(receive_buffer));
  268. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  269. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  270. }
  271. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  272. }
  273. 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){
  274. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, 0x00, gpiobStatus, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
  275. uint16_t CRC_value = calculate_crc(data_to_send, 9);
  276. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  277. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  278. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 15);
  279. if(status == HAL_OK) {
  280. memset(receive_buffer, 0, sizeof(receive_buffer));
  281. flag = 0;
  282. } else {
  283. memset(receive_buffer, 0, sizeof(receive_buffer));
  284. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  285. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  286. }
  287. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  288. }
  289. 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){
  290. 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};
  291. uint16_t CRC_value = calculate_crc(data_to_send, 11);
  292. data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
  293. data_to_send[12] = (uint8_t)(CRC_value >> 8);
  294. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
  295. if(status == HAL_OK) {
  296. memset(receive_buffer, 0, sizeof(receive_buffer));
  297. flag = 0;
  298. } else {
  299. memset(receive_buffer, 0, sizeof(receive_buffer));
  300. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  301. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  302. }
  303. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  304. }
  305. 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){
  306. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, 0x00, 0x00};
  307. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  308. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  309. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  310. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  311. if(status == HAL_OK) {
  312. memset(receive_buffer, 0, sizeof(receive_buffer));
  313. flag = 0;
  314. } else {
  315. memset(receive_buffer, 0, sizeof(receive_buffer));
  316. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  317. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  318. }
  319. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  320. }
  321. 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){
  322. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x06, adc2_byte1, adc2_byte2, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte1, 0x00, 0x00};
  323. uint16_t CRC_value = calculate_crc(data_to_send, 9);
  324. data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
  325. data_to_send[10] = (uint8_t)(CRC_value >> 8);
  326. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 15);
  327. if(status == HAL_OK) {
  328. memset(receive_buffer, 0, sizeof(receive_buffer));
  329. flag = 0;
  330. } else {
  331. memset(receive_buffer, 0, sizeof(receive_buffer));
  332. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  333. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  334. }
  335. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  336. }
  337. 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){
  338. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x03, 0x04, adc3_byte1, adc3_byte2, adc1_byte1, adc1_byte1, 0x00, 0x00};
  339. uint16_t CRC_value = calculate_crc(data_to_send, 7);
  340. data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
  341. data_to_send[8] = (uint8_t)(CRC_value >> 8);
  342. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 15);
  343. if(status == HAL_OK) {
  344. memset(receive_buffer, 0, sizeof(receive_buffer));
  345. flag = 0;
  346. } else {
  347. memset(receive_buffer, 0, sizeof(receive_buffer));
  348. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  349. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  350. }
  351. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  352. }
  353. else if((receive_buffer[0] == (uint8_t)initial_address || receive_buffer[0] == 0xFA) && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
  354. uint8_t data_to_send[] = {0xFA, 0x03, 0x02, 0x00, (uint8_t)initial_address, 0x00, 0x00};
  355. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  356. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  357. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  358. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  359. if(status == HAL_OK) {
  360. memset(receive_buffer, 0, sizeof(receive_buffer));
  361. flag = 0;
  362. } else {
  363. memset(receive_buffer, 0, sizeof(receive_buffer));
  364. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  365. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  366. }
  367. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  368. }
  369. 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){
  370. updatePbStatus(receive_buffer[5]);
  371. erase_flash(ADDR_FLASH_PAGE_62);
  372. write_flash_PbStatus(receive_buffer); // 写入Flash
  373. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, receive_buffer[5], 0x00, 0x00};
  374. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  375. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  376. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  377. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  378. if(status == HAL_OK) {
  379. memset(receive_buffer, 0, sizeof(receive_buffer));
  380. flag = 0;
  381. } else {
  382. memset(receive_buffer, 0, sizeof(receive_buffer));
  383. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  384. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  385. }
  386. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  387. }
  388. 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){
  389. erase_flash(ADDR_FLASH_PAGE_64);
  390. write_flash_autoAdc(ADDR_FLASH_PAGE_64);
  391. uint16_t now_calibrationValue = Adc2_CalibrationValue;
  392. CoverADC(ADDR_FLASH_PAGE_64);
  393. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, (uint8_t)((now_calibrationValue >> 8) & 0xFF), (uint8_t)(now_calibrationValue & 0xFF), 0x00, 0x00};
  394. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  395. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  396. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  397. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  398. if(status == HAL_OK) {
  399. memset(receive_buffer, 0, sizeof(receive_buffer));
  400. flag = 0;
  401. } else {
  402. memset(receive_buffer, 0, sizeof(receive_buffer));
  403. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  404. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  405. }
  406. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  407. }
  408. 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){
  409. erase_flash(ADDR_FLASH_PAGE_65);
  410. write_flash_autoAdc(ADDR_FLASH_PAGE_65);
  411. uint16_t now_calibrationValue = Adc3_CalibrationValue;
  412. CoverADC(ADDR_FLASH_PAGE_65);
  413. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, (uint8_t)((now_calibrationValue >> 8) & 0xFF), (uint8_t)(now_calibrationValue & 0xFF), 0x00, 0x00};
  414. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  415. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  416. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  417. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  418. if(status == HAL_OK) {
  419. memset(receive_buffer, 0, sizeof(receive_buffer));
  420. flag = 0;
  421. } else {
  422. memset(receive_buffer, 0, sizeof(receive_buffer));
  423. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  424. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  425. }
  426. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  427. }
  428. 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){
  429. update_baudrate(receive_buffer[5]);
  430. erase_flash(ADDR_FLASH_PAGE_63);
  431. write_flash_Baudrate(receive_buffer); // 写入Flash
  432. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x00, 0x0C, 0x00, receive_buffer[5], 0x00, 0x00};
  433. uint16_t CRC_value = calculate_crc(data_to_send, 6);
  434. data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
  435. data_to_send[7] = (uint8_t)(CRC_value >> 8);
  436. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 8, 15);
  437. if(status == HAL_OK) {
  438. memset(receive_buffer, 0, sizeof(receive_buffer));
  439. flag = 0;
  440. } else {
  441. memset(receive_buffer, 0, sizeof(receive_buffer));
  442. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  443. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  444. }
  445. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  446. }
  447. 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){
  448. erase_flash(ADDR_FLASH_PAGE_61);
  449. write_flash_address(receive_buffer); // 写入Flash
  450. uint8_t data_to_send[] = {(uint8_t)initial_address, 0x06, 0x02, 0x00, (uint8_t)initial_address, 0x00, 0x00};
  451. uint16_t CRC_value = calculate_crc(data_to_send, 5);
  452. data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
  453. data_to_send[6] = (uint8_t)(CRC_value >> 8);
  454. HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 15);
  455. if(status == HAL_OK) {
  456. memset(receive_buffer, 0, sizeof(receive_buffer));
  457. flag = 0;
  458. } else {
  459. memset(receive_buffer, 0, sizeof(receive_buffer));
  460. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE);
  461. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
  462. }
  463. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  464. }
  465. 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){
  466. flag = 0; // 将标志位重新置为0
  467. memset(receive_buffer,0,sizeof(receive_buffer));
  468. HAL_NVIC_SystemReset();
  469. }
  470. else {
  471. memset(receive_buffer, 0, sizeof(receive_buffer));
  472. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  473. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  474. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  475. }
  476. }
  477. gpioaStatus = GetPaInputStatus();
  478. gpiobStatus = GetPbOutputStatus();
  479. GetADCResults(&hadc1);
  480. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
  481. __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
  482. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  483. HAL_Delay(1);
  484. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  485. HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
  486. #endif
  487. /* USER CODE END WHILE */
  488. /* USER CODE BEGIN 3 */
  489. }
  490. /* USER CODE END 3 */
  491. }
  492. /**
  493. * @brief System Clock Configuration
  494. * @retval None
  495. */
  496. void SystemClock_Config(void)
  497. {
  498. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  499. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  500. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  501. /** Initializes the RCC Oscillators according to the specified parameters
  502. * in the RCC_OscInitTypeDef structure.
  503. */
  504. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  505. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  506. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  507. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  508. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  509. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  510. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  511. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  512. {
  513. Error_Handler();
  514. }
  515. /** Initializes the CPU, AHB and APB buses clocks
  516. */
  517. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  518. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  519. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  520. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  521. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  522. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  523. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  524. {
  525. Error_Handler();
  526. }
  527. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  528. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  529. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  530. {
  531. Error_Handler();
  532. }
  533. }
  534. /* USER CODE BEGIN 4 */
  535. uint16_t read_flash_16(uint32_t flash_address){
  536. uint16_t data = *(volatile uint16_t*)flash_address; // 从Flash中读取2个字节
  537. return data;
  538. }
  539. void read_new_address_from_flash(uint32_t flash_address){
  540. uint16_t init_address = read_flash_16(flash_address);
  541. if(init_address == 0xFFFF) {
  542. initial_address = initial_address;
  543. } else {
  544. initial_address = read_flash_16(flash_address); // 读取新地址并赋值给initial_address
  545. }
  546. }
  547. void erase_flash(uint32_t ADDR_FLASH){ //进行擦除
  548. FLASH_EraseInitTypeDef erase_init;
  549. erase_init.TypeErase = FLASH_TYPEERASE_PAGES; // 擦除类型为页擦除
  550. erase_init.PageAddress = ADDR_FLASH;
  551. erase_init.NbPages = 1; // 擦除的页数
  552. uint32_t page_error = 0;
  553. HAL_FLASH_Unlock(); // 解锁Flash
  554. HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &page_error); // 执行擦除操作
  555. HAL_FLASH_Lock(); // 上锁Flash
  556. if(status == HAL_OK){
  557. if(ADDR_FLASH == ADDR_FLASH_PAGE_61) {
  558. initial_address = 0; // 将initial_address清零
  559. }
  560. if(ADDR_FLASH == ADDR_FLASH_PAGE_62) {
  561. gpiobStatus = 0; // 将pb_status清零
  562. }
  563. if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
  564. Adc2_CalibrationValue = 0x0000; // 清零
  565. }
  566. if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
  567. Adc3_CalibrationValue = 0x0000; // 清零
  568. }
  569. if(ADDR_FLASH == ADDR_FLASH_PAGE_63) {
  570. BaudrateValue = 0xFFFF;
  571. }
  572. }
  573. }
  574. void write_flash_address(uint8_t* rx_buffer) { //数组位置5进行写入——地址
  575. uint8_t newAddress = rx_buffer[5];
  576. HAL_FLASH_Unlock(); // 解锁Flash
  577. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_61, newAddress); // 写入新地址到Flash
  578. HAL_FLASH_Lock(); // 上锁Flash
  579. if(status == HAL_OK){
  580. initial_address = newAddress; // 将新地址赋值给initial_address
  581. }
  582. }
  583. void write_flash_PbStatus(uint8_t* rx_buffer){ //数组位置5进行写入——PB
  584. uint8_t newStatus = rx_buffer[5];
  585. HAL_FLASH_Unlock(); // 解锁Flash
  586. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_62, newStatus); // 写入PbStatus到Flash
  587. HAL_FLASH_Lock(); // 上锁Flash
  588. if(status == HAL_OK){
  589. gpiobStatus = newStatus; // 将新状态赋值给pb_status
  590. }
  591. }
  592. void write_flash_autoAdc(uint32_t ADDR_FLASH){
  593. uint16_t Standard_value;
  594. Standard_value = (0.6/(3.3/4096));
  595. uint16_t newValue = 0;
  596. if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
  597. if(adc2_rawValue >= Standard_value) {
  598. newValue = adc2_rawValue - Standard_value;
  599. } else {
  600. newValue = Standard_value - adc2_rawValue;
  601. newValue |= 0x8000;
  602. }
  603. HAL_FLASH_Unlock(); // 解锁Flash
  604. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue);
  605. HAL_FLASH_Lock(); // 上锁Flash
  606. if(status == HAL_OK){
  607. Adc2_CalibrationValue = newValue & 0x7FFF;
  608. }
  609. }
  610. if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
  611. if(adc3_rawValue >= Standard_value) {
  612. newValue = adc3_rawValue - Standard_value;
  613. } else {
  614. newValue = Standard_value - adc3_rawValue;
  615. newValue |= 0x8000;
  616. }
  617. HAL_FLASH_Unlock(); // 解锁Flash
  618. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue);
  619. HAL_FLASH_Lock(); // 上锁Flash
  620. if(status == HAL_OK){
  621. Adc3_CalibrationValue = newValue & 0x7FFF;
  622. }
  623. }
  624. }
  625. void CoverADC(uint32_t ADDR) {
  626. uint16_t Overwrite_Value = read_flash_16(ADDR);
  627. if (ADDR == ADDR_FLASH_PAGE_64) {
  628. Value_old_addr2 = Overwrite_Value;
  629. }
  630. if (ADDR == ADDR_FLASH_PAGE_65) {
  631. Value_old_addr3 = Overwrite_Value;
  632. }
  633. }
  634. void write_flash_Baudrate(uint8_t* rx_buffer){ //数组位置5进行写入——Baudrate
  635. uint8_t newStatus = rx_buffer[5];
  636. HAL_FLASH_Unlock(); // 解锁Flash
  637. HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_63, newStatus); // 写入PbStatus到Flash
  638. HAL_FLASH_Lock(); // 上锁Flash
  639. if(status == HAL_OK){
  640. BaudrateValue = newStatus; // 将新状态赋值给pb_status
  641. }
  642. }
  643. /* USER CODE END 4 */
  644. /**
  645. * @brief This function is executed in case of error occurrence.
  646. * @retval None
  647. */
  648. void Error_Handler(void)
  649. {
  650. /* USER CODE BEGIN Error_Handler_Debug */
  651. /* User can add his own implementation to report the HAL error return state */
  652. __disable_irq();
  653. while (1)
  654. {
  655. }
  656. /* USER CODE END Error_Handler_Debug */
  657. }
  658. #ifdef USE_FULL_ASSERT
  659. /**
  660. * @brief Reports the name of the source file and the source line number
  661. * where the assert_param error has occurred.
  662. * @param file: pointer to the source file name
  663. * @param line: assert_param error line source number
  664. * @retval None
  665. */
  666. void assert_failed(uint8_t *file, uint32_t line)
  667. {
  668. /* USER CODE BEGIN 6 */
  669. /* User can add his own implementation to report the file name and line number,
  670. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  671. /* USER CODE END 6 */
  672. }
  673. #endif /* USE_FULL_ASSERT */