123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "adc.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- int fputc(int ch, FILE *f)
- {
- uint8_t temp[10] = {ch};
- HAL_UART_Transmit(&huart1, temp,10, 50);
- HAL_UART_Transmit(&huart3, temp,10, 50);
- return ch;
- }
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- uint8_t data_to_send[300];
- uint16_t CRC_value = 0;
- uint16_t initial_address = 0x0000;
- uint32_t System_version = 0x23051100;
- uint16_t BaudrateValue = 0x0000;
- uint16_t Adc2_CalibrationValue ;
- uint16_t Adc3_CalibrationValue ;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_ADC1_Init();
- MX_TIM2_Init();
- MX_USART3_UART_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim2);
- read_new_address_from_flash(ADDR_FLASH_PAGE_61);
- updatePbStatus(read_flash_16(ADDR_FLASH_PAGE_62));
- update_baudrate(read_flash_16(ADDR_FLASH_PAGE_63));
- AdcCalibration_init();
- // printf("init\r\n");
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- // HAL_Delay(1200);
- #if Modbus
- gpioaStatus = GetPaInputStatus();
- gpiobStatus = GetPbOutputStatus();
- GetADCResults(&hadc1);
- if (flag == 1 && (calculate_crc(receive_buffer,6) == (receive_buffer[7] << 8 | receive_buffer[6]))) // 如果标志位为1
- {
- 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){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x02;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpioaStatus;
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
- 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){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x02;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpiobStatus;
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x02;
- data_to_send[3] = adc2_byte1;
- data_to_send[4] = adc2_byte2;
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x02;
- data_to_send[3] = adc3_byte1;
- data_to_send[4] = adc3_byte2;
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x02;
- data_to_send[3] = adc1_byte1;
- data_to_send[4] = adc1_byte2;
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- erase_flash(ADDR_FLASH_PAGE_64);
- write_flash_autoAdc(ADDR_FLASH_PAGE_64);
- uint16_t now_calibrationValue = Adc2_CalibrationValue;
- CoverADC(ADDR_FLASH_PAGE_64);
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x06;
- data_to_send[2] = 0x02;
- data_to_send[3] = (uint8_t)((now_calibrationValue >> 8) & 0xFF);
- data_to_send[4] = (uint8_t)(now_calibrationValue & 0xFF); // 获取低8位
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- erase_flash(ADDR_FLASH_PAGE_65);
- write_flash_autoAdc(ADDR_FLASH_PAGE_65);
- uint16_t now_calibrationValue = Adc3_CalibrationValue;
- CoverADC(ADDR_FLASH_PAGE_65);
- data_to_send[0] = (uint8_t)initial_address; // 将接收到的数据存储到一个新的数组中
- data_to_send[1] = 0x06;
- data_to_send[2] = 0x02; // 将接收到的数据存储到一个新的数组中
- data_to_send[3] = (uint8_t)((now_calibrationValue >> 8) & 0xFF);
- data_to_send[4] = (uint8_t)(now_calibrationValue & 0xFF); // 获取低8位
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 ){
- erase_flash(ADDR_FLASH_PAGE_61);
- write_flash_address(receive_buffer); // 写入Flash
- data_to_send[0] = (uint8_t)initial_address; // 将接收到的数据存储到一个新的数组中
- data_to_send[1] = 0x06;
- data_to_send[2] = 0x02; // 将接收到的数据存储到一个新的数组中
- data_to_send[3] = 0x00;
- data_to_send[4] = receive_buffer[5]; // 获取低8位
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x04;
- data_to_send[3] = (uint8_t)(System_version >> 24);
- data_to_send[4] = (uint8_t)(System_version >> 16);
- data_to_send[5] = (uint8_t)(System_version >> 8);
- data_to_send[6] = (uint8_t)(System_version );
- CRC_value = calculate_crc(data_to_send,7);
- data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[8] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 9, 9); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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){
- HAL_NVIC_SystemReset();
- flag = 0; // 将标志位重新置为0
- }
-
- if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 ){
- updatePbStatus(receive_buffer[5]);
- erase_flash(ADDR_FLASH_PAGE_62);
- write_flash_PbStatus(receive_buffer); // 写入Flash
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x06;
- data_to_send[2] = 0x02;
- data_to_send[3] = 0x00;
- data_to_send[4] = receive_buffer[5];
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x04;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpioaStatus;
- data_to_send[5] = 0x00;
- data_to_send[6] = gpiobStatus;
- CRC_value = calculate_crc(data_to_send,7);
- data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[8] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x06;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpioaStatus;
- data_to_send[5] = 0x00;
- data_to_send[6] = gpiobStatus;
- data_to_send[7] = adc2_byte1;
- data_to_send[8] = adc2_byte2;
- CRC_value = calculate_crc(data_to_send,9);
- data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[10] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x08;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpioaStatus;
- data_to_send[5] = 0x00;
- data_to_send[6] = gpiobStatus;
- data_to_send[7] = adc2_byte1;
- data_to_send[8] = adc2_byte2;
- data_to_send[9] = adc3_byte1;
- data_to_send[10] = adc3_byte2;
- CRC_value = calculate_crc(data_to_send,11);
- data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[12] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 13, 12); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x0A;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpioaStatus;
- data_to_send[5] = 0x00;
- data_to_send[6] = gpiobStatus;
- data_to_send[7] = adc2_byte1;
- data_to_send[8] = adc2_byte2;
- data_to_send[9] = adc3_byte1;
- data_to_send[10] = adc3_byte2;
- data_to_send[11] = adc1_byte1;
- data_to_send[12] = adc1_byte2;
- CRC_value = calculate_crc(data_to_send,13);
- data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[14] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 15, 14); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x04;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpiobStatus;
- data_to_send[5] = adc2_byte1;
- data_to_send[6] = adc2_byte2;
- CRC_value = calculate_crc(data_to_send,7);
- data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[8] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x06;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpiobStatus;
- data_to_send[5] = adc2_byte1;
- data_to_send[6] = adc2_byte2;
- data_to_send[7] = adc3_byte1;
- data_to_send[8] = adc3_byte2;
- CRC_value = calculate_crc(data_to_send,9);
- data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[10] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x08;
- data_to_send[3] = 0x00;
- data_to_send[4] = gpiobStatus;
- data_to_send[5] = adc2_byte1;
- data_to_send[6] = adc2_byte2;
- data_to_send[7] = adc3_byte1;
- data_to_send[8] = adc3_byte2;
- data_to_send[9] = adc1_byte1;
- data_to_send[10] = adc1_byte2;
- CRC_value = calculate_crc(data_to_send,11);
- data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[12] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 13, 12); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x04;
- data_to_send[3] = adc2_byte1;
- data_to_send[4] = adc2_byte2;
- data_to_send[5] = adc3_byte1;
- data_to_send[6] = adc3_byte2;
- CRC_value = calculate_crc(data_to_send,7);
- data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[8] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x06;
- data_to_send[3] = adc2_byte1;
- data_to_send[4] = adc2_byte2;
- data_to_send[5] = adc3_byte1;
- data_to_send[6] = adc3_byte2;
- data_to_send[7] = adc1_byte1;
- data_to_send[8] = adc1_byte2;
- CRC_value = calculate_crc(data_to_send,9);
- data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[10] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = (uint8_t)initial_address;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x04;
- data_to_send[3] = adc3_byte1;
- data_to_send[4] = adc3_byte2;
- data_to_send[5] = adc1_byte1;
- data_to_send[6] = adc1_byte2;
- CRC_value = calculate_crc(data_to_send,7);
- data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[8] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0C && receive_buffer[4] == 0x00 ){
- update_baudrate(receive_buffer[5]);
- erase_flash(ADDR_FLASH_PAGE_63);
- write_flash_Baudrate(receive_buffer); // 写入Flash
- data_to_send[0] = (uint8_t)initial_address; // 将接收到的数据存储到一个新的数组中
- data_to_send[1] = 0x06;
- data_to_send[2] = 0x00; // 将接收到的数据存储到一个新的数组中
- data_to_send[3] = 0x0C;
- data_to_send[4] = 0x00;
- data_to_send[5] = receive_buffer[5];
- CRC_value = calculate_crc(data_to_send,6);
- data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[7] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 8, 8); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
-
- 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 ){
- data_to_send[0] = 0xFA;
- data_to_send[1] = 0x03;
- data_to_send[2] = 0x02;
- data_to_send[3] = 0x00;
- data_to_send[4] = (uint8_t)initial_address;
- CRC_value = calculate_crc(data_to_send,5);
- data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
- data_to_send[6] = (uint8_t)(CRC_value >> 8);
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
- flag = 0; // 将标志位重新置为0
- }
- // gpioaStatus = 0;adc1_byte1 =0;adc1_byte2=0;adc2_byte1 =0;adc2_byte2=0;adc3_byte1 =0;adc3_byte2=0;
- HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
- }
- HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
- #endif
- // printf("success\r\n");
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
- HAL_Delay(1);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- uint16_t read_flash_16(uint32_t flash_address){
- uint16_t data = *(volatile uint16_t*)flash_address; // 从Flash中读取2个字节
- return data;
- }
- void read_new_address_from_flash(uint32_t flash_address){
- initial_address = read_flash_16(flash_address); // 读取新地址并赋值给initial_address
- }
- void erase_flash(uint32_t ADDR_FLASH){ //进行擦除
- FLASH_EraseInitTypeDef erase_init;
- erase_init.TypeErase = FLASH_TYPEERASE_PAGES; // 擦除类型为页擦除
- erase_init.PageAddress = ADDR_FLASH;
- erase_init.NbPages = 1; // 擦除的页数
- uint32_t page_error = 0;
- HAL_FLASH_Unlock(); // 解锁Flash
- HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &page_error); // 执行擦除操作
- HAL_FLASH_Lock(); // 上锁Flash
- if(status == HAL_OK){
- if(ADDR_FLASH == ADDR_FLASH_PAGE_61) {
- initial_address = 0; // 将initial_address清零
- }
- if(ADDR_FLASH == ADDR_FLASH_PAGE_62) {
- gpiobStatus = 0; // 将pb_status清零
- }
- if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
- Adc2_CalibrationValue = 0x0000; // 清零
- }
- if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
- Adc3_CalibrationValue = 0x0000; // 清零
- }
-
- if(ADDR_FLASH == ADDR_FLASH_PAGE_63) {
- BaudrateValue = 0xFFFF;
- }
- }
- }
- void write_flash_address(uint8_t* rx_buffer) { //数组位置5进行写入——地址
- uint8_t newAddress = rx_buffer[5];
- HAL_FLASH_Unlock(); // 解锁Flash
- HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_61, newAddress); // 写入新地址到Flash
- HAL_FLASH_Lock(); // 上锁Flash
- if(status == HAL_OK){
- initial_address = newAddress; // 将新地址赋值给initial_address
- }
- }
- void write_flash_PbStatus(uint8_t* rx_buffer){ //数组位置5进行写入——PB
- uint8_t newStatus = rx_buffer[5];
- HAL_FLASH_Unlock(); // 解锁Flash
- HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_62, newStatus); // 写入PbStatus到Flash
- HAL_FLASH_Lock(); // 上锁Flash
- if(status == HAL_OK){
- gpiobStatus = newStatus; // 将新状态赋值给pb_status
- }
- }
- void write_flash_autoAdc(uint32_t ADDR_FLASH){
- uint16_t Standard_value;
- Standard_value = (0.6/(3.3/4096));
- uint16_t newValue = 0;
- if(ADDR_FLASH == ADDR_FLASH_PAGE_64) {
- if(adc2_rawValue >= Standard_value) {
- newValue = adc2_rawValue - Standard_value;
- } else {
- newValue = Standard_value - adc2_rawValue;
- newValue |= 0x8000;
- }
- HAL_FLASH_Unlock(); // 解锁Flash
- HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue);
- HAL_FLASH_Lock(); // 上锁Flash
- if(status == HAL_OK){
- Adc2_CalibrationValue = newValue & 0x7FFF;
- }
- }
- if(ADDR_FLASH == ADDR_FLASH_PAGE_65) {
- if(adc3_rawValue >= Standard_value) {
- newValue = adc3_rawValue - Standard_value;
- } else {
- newValue = Standard_value - adc3_rawValue;
- newValue |= 0x8000;
- }
- HAL_FLASH_Unlock(); // 解锁Flash
- HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH, newValue);
- HAL_FLASH_Lock(); // 上锁Flash
- if(status == HAL_OK){
- Adc3_CalibrationValue = newValue & 0x7FFF;
- }
- }
- }
- void CoverADC(uint32_t ADDR) {
- uint16_t Overwrite_Value = read_flash_16(ADDR);
- if (ADDR == ADDR_FLASH_PAGE_64) {
- Value_old_addr2 = Overwrite_Value;
- }
- if (ADDR == ADDR_FLASH_PAGE_65) {
- Value_old_addr3 = Overwrite_Value;
- }
- }
- void write_flash_Baudrate(uint8_t* rx_buffer){ //数组位置5进行写入——Baudrate
- uint8_t newStatus = rx_buffer[5];
- HAL_FLASH_Unlock(); // 解锁Flash
- HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ADDR_FLASH_PAGE_63, newStatus); // 写入PbStatus到Flash
- HAL_FLASH_Lock(); // 上锁Flash
- if(status == HAL_OK){
- BaudrateValue = newStatus; // 将新状态赋值给pb_status
- }
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
|