main.c 27 KB

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