Files
stm32-comm/SLP_OPR/Core/Src/main.c
T
2026-03-31 13:27:23 +08:00

627 lines
23 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "dma.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "w5500/w5500_port.h"
#include "w5500/modbusSlave.h"
#include "w5500/modbus_crc.h"
#include "w5500/loopback.h"
#include "w5500/httpParser.h"
#include "w5500/httpServer.h"
#include "w5500/webpages.h"
#include "w5500/httpUtil.h"
#include "w5500/slmpMaster.h"
#include "w5500/modbusRtuMaster.h"
#include "w5500/loopback2.h"
#include "w5500/rtuMaster.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define FLASH_LAST_PAGE_512 0x0807F800
#define IP_ADDR 20
#define run_lamp_on HAL_GPIO_WritePin(run_lamp_GPIO_Port, run_lamp_Pin, GPIO_PIN_RESET)
#define run_lamp_off HAL_GPIO_WritePin(run_lamp_GPIO_Port, run_lamp_Pin, GPIO_PIN_SET)
#define comm_lamp_on HAL_GPIO_WritePin(comm_lamp_GPIO_Port, comm_lamp_Pin, GPIO_PIN_RESET)
#define comm_lamp_off HAL_GPIO_WritePin(comm_lamp_GPIO_Port, comm_lamp_Pin, GPIO_PIN_SET)
#define RE_SET HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_SET)
#define RE_RESET HAL_GPIO_WritePin(re485_GPIO_Port, re485_Pin, GPIO_PIN_RESET)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t commType = 0; // 0 slmp, 1 omr
_Bool comm1, comm2;
uint16_t comm_cnt, prev_cnt;
uint16_t slmpTimeout = 0;
uint16_t slmpTimegap = 1;
uint16_t slmp_send_loop = 0;
uint8_t w5500_rx[1024];
uint8_t uart_rx[255];
uint8_t uart_tx[255];
uint8_t local_ip[4];
uint8_t slmp_ip[4];
uint16_t slmp_port;
uint16_t in1_word = 0;
uint16_t in2_word = 0;
uint16_t in3_word = 0;
uint16_t in4_word = 0;
uint16_t out_word1 = 0;
uint16_t out_word2 = 0;
////////////////////////////////////////////////////////// 新增变量 //////////////////////////
rtumST readST[10] = { { .slave_id = 0, .slave_address = 1, .plc_address = 2, .size = 3 }, { .slave_id = 4, .slave_address = 5, .plc_address = 6, .size = 7 }, {
.slave_id = 8, .slave_address = 9, .plc_address = 10, .size = 11 }, { .slave_id = 12, .slave_address = 13, .plc_address = 14, .size = 15 }, {
.slave_id = 16, .slave_address = 17, .plc_address = 18, .size = 19 }, { .slave_id = 20, .slave_address = 21, .plc_address = 22, .size = 23 }, {
.slave_id = 24, .slave_address = 25, .plc_address = 26, .size = 27 }, { .slave_id = 28, .slave_address = 29, .plc_address = 30, .size = 31 }, {
.slave_id = 32, .slave_address = 33, .plc_address = 34, .size = 35 }, { .slave_id = 36, .slave_address = 37, .plc_address = 38, .size = 39 } };
rtumST writeST[10] = { { .slave_id = 40, .slave_address = 41, .plc_address = 42, .size = 43 }, { .slave_id = 44, .slave_address = 45, .plc_address = 46, .size =
47 }, { .slave_id = 48, .slave_address = 49, .plc_address = 50, .size = 51 }, { .slave_id = 52, .slave_address = 53, .plc_address = 54, .size = 55 }, {
.slave_id = 56, .slave_address = 57, .plc_address = 58, .size = 59 }, { .slave_id = 60, .slave_address = 61, .plc_address = 62, .size = 63 }, {
.slave_id = 64, .slave_address = 65, .plc_address = 66, .size = 67 }, { .slave_id = 68, .slave_address = 69, .plc_address = 70, .size = 71 }, {
.slave_id = 72, .slave_address = 73, .plc_address = 74, .size = 75 }, { .slave_id = 76, .slave_address = 77, .plc_address = 78, .size = 79 } };
rtumST myRtumST[20];
RtuMaster master = { .localStartIdx = 1400 };
////////////////////////////////////////////////////////// 新增变量 //////////////////////////
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE {
HAL_UART_Transmit(&huart1, (uint8_t*) &ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/**
* uint16_t data_array[] = {0x1234, 0x5678, 0x9ABC};
* write_flash_u16(data_array, 3);
*/
void write_flash_u16(uint16_t *data, uint32_t length, uint32_t user_save_flash) {
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SectorError;
// 擦除扇区
EraseInitStruct.TypeErase = TYPEERASE_PAGES;
EraseInitStruct.PageAddress = user_save_flash;
EraseInitStruct.NbPages = 1;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
// 擦除错误处理
HAL_FLASH_Lock();
Error_Handler();
}
// 写入数据
for (uint32_t i = 0; i < length; i += 2) {
uint32_t word = 0;
if (i + 1 < length) {
// 将两�??????? u16 组合成一�??????? u32
word = ((uint32_t) data[i + 1] << 16) | data[i];
} else {
// 如果数据长度是奇数,�???????后一�??????? u16 �???????要单独处�???????
word = data[i];
}
// 写入组合好的 32 位数�???????
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, user_save_flash, word) != HAL_OK) {
HAL_FLASH_Lock();
Error_Handler();
}
user_save_flash += 4; // 4 字节为一个单位,移动到下�???????个地�???????
}
HAL_FLASH_Lock(); // 写完后锁�??????? Flash
}
/**
* uint16_t data_array[3];
* read_flash_u16(data_array, 3);
*/
void read_flash_u16(uint16_t *data, uint32_t length, uint32_t user_save_flash) {
for (uint32_t i = 0; i < length; i += 2) {
uint32_t word = *(uint32_t*) user_save_flash; // �??????? Flash 中读�??????? 32 位数�???????
// 拆分 32 位数据为两个 16 位数�???????
data[i] = (uint16_t) (word & 0xFFFF); // �??????? 16 �???????
if (i + 1 < length) {
data[i + 1] = (uint16_t) ((word >> 16) & 0xFFFF); // �??????? 16 �???????
}
user_save_flash += 4; // 每次读取后,移动到下�???????�??????? 32 位地�???????
}
}
union {
uint16_t u16;
uint8_t u8[2];
} u16_u8;
void load_flash() {
uint16_t data_array[10];
read_flash_u16(data_array, 10, FLASH_LAST_PAGE_512);
set_register_range(20, 10, data_array);
}
_Bool check_ipv4(uint8_t ip[4]) {
for (int i = 0; i < 4; i++) {
if (ip[i] >= 255 || ip[i] == 0) {
return 0;
}
}
return 1;
}
//////////////////////////////////////////////////////////// ↓定时器(TIME6=1MS↓ /////////////////////////////////////////////
uint16_t timeout_beat, rtu_master_timeout, rstbtn_timeout, runbeat;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim == &htim6) {
if (master.timeout < 0xffff) {
master.timeout++;
}
if (slmpTimeout < 0xffff) {
slmpTimeout++;
}
if (HAL_GPIO_ReadPin(rstbtn_GPIO_Port, rstbtn_Pin) == GPIO_PIN_RESET) {
if (rstbtn_timeout < 0xffff) {
rstbtn_timeout++;
} else {
rstbtn_timeout = 0;
}
}
if (runbeat < 1000) { //控制运行指示灯间隔1秒闪烁
runbeat++;
} else {
runbeat = 0;
HAL_GPIO_TogglePin(run_lamp_GPIO_Port, run_lamp_Pin);
}
}
}
uint16_t flash_reset[100];
void reset_button() {
if (rstbtn_timeout > 3000) {
memset(flash_reset, 0xFF, 200);
Write_To_Flash_uint16(EDIT_FLASH_ADDRESS, flash_reset, 100);
rstbtn_timeout = 0;
}
}
//////////////////////////////////////////////////////////// ↑定时器↑ /////////////////////////////////////////////
//////////////////////////////////////////////////////////// ↓485主站↓ /////////////////////////////////////////////
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if (huart == &huart1) {
RE_RESET;
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, master.txbuf, 512);
}
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
if (huart == &huart1) {
master.rxdone = 1;
master.rxsize = Size;
}
}
//////////////////////////////////////////////////////////// ↑485主站↑ /////////////////////////////////////////////
int getBit(int num, int n) {
return (num & (1 << n)) >> n;
}
void map_out(uint16_t startAddress) {
out_word1 = get_register(startAddress);
out_word2 = get_register(startAddress + 1);
(getBit(out_word1, 0)) ? HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_SET);
(getBit(out_word1, 1)) ? HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_SET);
(getBit(out_word1, 2)) ? HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_SET);
(getBit(out_word1, 3)) ? HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_SET);
(getBit(out_word1, 4)) ? HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_SET);
(getBit(out_word1, 5)) ? HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_SET);
(getBit(out_word1, 6)) ? HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_SET);
(getBit(out_word1, 7)) ? HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_SET);
(getBit(out_word1, 8)) ? HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_SET);
(getBit(out_word1, 9)) ? HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_SET);
(getBit(out_word1, 10)) ? HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_SET);
(getBit(out_word1, 11)) ? HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_SET);
(getBit(out_word1, 12)) ? HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_SET);
(getBit(out_word1, 13)) ? HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_SET);
(getBit(out_word1, 14)) ? HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_SET);
(getBit(out_word1, 15)) ? HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_SET);
(getBit(out_word2, 0)) ? HAL_GPIO_WritePin(o17_GPIO_Port, o17_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o17_GPIO_Port, o17_Pin, GPIO_PIN_SET);
(getBit(out_word2, 1)) ? HAL_GPIO_WritePin(o18_GPIO_Port, o18_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o18_GPIO_Port, o18_Pin, GPIO_PIN_SET);
(getBit(out_word2, 2)) ? HAL_GPIO_WritePin(o19_GPIO_Port, o19_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o19_GPIO_Port, o19_Pin, GPIO_PIN_SET);
(getBit(out_word2, 3)) ? HAL_GPIO_WritePin(o20_GPIO_Port, o20_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o20_GPIO_Port, o20_Pin, GPIO_PIN_SET);
(getBit(out_word2, 4)) ? HAL_GPIO_WritePin(o21_GPIO_Port, o21_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o21_GPIO_Port, o21_Pin, GPIO_PIN_SET);
(getBit(out_word2, 5)) ? HAL_GPIO_WritePin(o22_GPIO_Port, o22_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o22_GPIO_Port, o22_Pin, GPIO_PIN_SET);
(getBit(out_word2, 6)) ? HAL_GPIO_WritePin(o23_GPIO_Port, o23_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o23_GPIO_Port, o23_Pin, GPIO_PIN_SET);
(getBit(out_word2, 7)) ? HAL_GPIO_WritePin(o24_GPIO_Port, o24_Pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(o24_GPIO_Port, o24_Pin, GPIO_PIN_SET);
}
void map_in(uint16_t startAddress) {
(HAL_GPIO_ReadPin(in1_GPIO_Port, in1_Pin) == 1) ? (in1_word &= ~(1 << 0)) : (in1_word |= (1 << 0));
(HAL_GPIO_ReadPin(in2_GPIO_Port, in2_Pin) == 1) ? (in1_word &= ~(1 << 1)) : (in1_word |= (1 << 1));
(HAL_GPIO_ReadPin(in3_GPIO_Port, in3_Pin) == 1) ? (in1_word &= ~(1 << 2)) : (in1_word |= (1 << 2));
(HAL_GPIO_ReadPin(in4_GPIO_Port, in4_Pin) == 1) ? (in1_word &= ~(1 << 3)) : (in1_word |= (1 << 3));
(HAL_GPIO_ReadPin(in5_GPIO_Port, in5_Pin) == 1) ? (in1_word &= ~(1 << 4)) : (in1_word |= (1 << 4));
(HAL_GPIO_ReadPin(in6_GPIO_Port, in6_Pin) == 1) ? (in1_word &= ~(1 << 5)) : (in1_word |= (1 << 5));
(HAL_GPIO_ReadPin(in7_GPIO_Port, in7_Pin) == 1) ? (in1_word &= ~(1 << 6)) : (in1_word |= (1 << 6));
(HAL_GPIO_ReadPin(in8_GPIO_Port, in8_Pin) == 1) ? (in1_word &= ~(1 << 7)) : (in1_word |= (1 << 7));
(HAL_GPIO_ReadPin(in9_GPIO_Port, in9_Pin) == 1) ? (in1_word &= ~(1 << 8)) : (in1_word |= (1 << 8));
(HAL_GPIO_ReadPin(in10_GPIO_Port, in10_Pin) == 1) ? (in1_word &= ~(1 << 9)) : (in1_word |= (1 << 9));
(HAL_GPIO_ReadPin(in11_GPIO_Port, in11_Pin) == 1) ? (in1_word &= ~(1 << 10)) : (in1_word |= (1 << 10));
(HAL_GPIO_ReadPin(in12_GPIO_Port, in12_Pin) == 1) ? (in1_word &= ~(1 << 11)) : (in1_word |= (1 << 11));
(HAL_GPIO_ReadPin(in13_GPIO_Port, in13_Pin) == 1) ? (in1_word &= ~(1 << 12)) : (in1_word |= (1 << 12));
(HAL_GPIO_ReadPin(in14_GPIO_Port, in14_Pin) == 1) ? (in1_word &= ~(1 << 13)) : (in1_word |= (1 << 13));
(HAL_GPIO_ReadPin(in15_GPIO_Port, in15_Pin) == 1) ? (in1_word &= ~(1 << 14)) : (in1_word |= (1 << 14));
(HAL_GPIO_ReadPin(in16_GPIO_Port, in16_Pin) == 1) ? (in1_word &= ~(1 << 15)) : (in1_word |= (1 << 15));
(HAL_GPIO_ReadPin(in17_GPIO_Port, in17_Pin) == 1) ? (in2_word &= ~(1 << 0)) : (in2_word |= (1 << 0));
(HAL_GPIO_ReadPin(in18_GPIO_Port, in18_Pin) == 1) ? (in2_word &= ~(1 << 1)) : (in2_word |= (1 << 1));
(HAL_GPIO_ReadPin(in19_GPIO_Port, in19_Pin) == 1) ? (in2_word &= ~(1 << 2)) : (in2_word |= (1 << 2));
(HAL_GPIO_ReadPin(in20_GPIO_Port, in20_Pin) == 1) ? (in2_word &= ~(1 << 3)) : (in2_word |= (1 << 3));
(HAL_GPIO_ReadPin(in21_GPIO_Port, in21_Pin) == 1) ? (in2_word &= ~(1 << 4)) : (in2_word |= (1 << 4));
(HAL_GPIO_ReadPin(in22_GPIO_Port, in22_Pin) == 1) ? (in2_word &= ~(1 << 5)) : (in2_word |= (1 << 5));
(HAL_GPIO_ReadPin(in23_GPIO_Port, in23_Pin) == 1) ? (in2_word &= ~(1 << 6)) : (in2_word |= (1 << 6));
(HAL_GPIO_ReadPin(in24_GPIO_Port, in24_Pin) == 1) ? (in2_word &= ~(1 << 7)) : (in2_word |= (1 << 7));
(HAL_GPIO_ReadPin(a1_GPIO_Port, a1_Pin) == 1) ? (in3_word &= ~(1 << 0)) : (in3_word |= (1 << 0));
(HAL_GPIO_ReadPin(b1_GPIO_Port, b1_Pin) == 1) ? (in3_word &= ~(1 << 1)) : (in3_word |= (1 << 1));
(HAL_GPIO_ReadPin(c1_GPIO_Port, c1_Pin) == 1) ? (in3_word &= ~(1 << 2)) : (in3_word |= (1 << 2));
(HAL_GPIO_ReadPin(e1_GPIO_Port, e1_Pin) == 1) ? (in3_word &= ~(1 << 3)) : (in3_word |= (1 << 3));
(HAL_GPIO_ReadPin(f1_GPIO_Port, f1_Pin) == 1) ? (in3_word &= ~(1 << 4)) : (in3_word |= (1 << 4));
(HAL_GPIO_ReadPin(g1_GPIO_Port, g1_Pin) == 1) ? (in3_word &= ~(1 << 5)) : (in3_word |= (1 << 5));
(HAL_GPIO_ReadPin(a2_GPIO_Port, a2_Pin) == 1) ? (in4_word &= ~(1 << 0)) : (in4_word |= (1 << 0));
(HAL_GPIO_ReadPin(b2_GPIO_Port, b2_Pin) == 1) ? (in4_word &= ~(1 << 1)) : (in4_word |= (1 << 1));
(HAL_GPIO_ReadPin(c2_GPIO_Port, c2_Pin) == 1) ? (in4_word &= ~(1 << 2)) : (in4_word |= (1 << 2));
(HAL_GPIO_ReadPin(e2_GPIO_Port, e2_Pin) == 1) ? (in4_word &= ~(1 << 3)) : (in4_word |= (1 << 3));
(HAL_GPIO_ReadPin(f2_GPIO_Port, f2_Pin) == 1) ? (in4_word &= ~(1 << 4)) : (in4_word |= (1 << 4));
(HAL_GPIO_ReadPin(g2_GPIO_Port, g2_Pin) == 1) ? (in4_word &= ~(1 << 5)) : (in4_word |= (1 << 5));
set_register_noSave(startAddress, in1_word);
set_register_noSave(startAddress + 1, in2_word);
set_register_noSave(startAddress + 2, in3_word);
set_register_noSave(startAddress + 3, in4_word);
}
void Read_From_Flash(uint32_t startAddress, uint16_t *buffer, uint32_t numElements) {
for (uint32_t i = 0; i < numElements; i++) {
buffer[i] = *(__IO uint16_t*) (startAddress + (i * 2));
}
}
void combineMyRtumST() {
for (int i = 0; i < 10; i++) {
myRtumST[i * 2] = readST[i];
myRtumST[i * 2 + 1] = writeST[i];
}
}
/* 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_DMA_Init();
MX_SPI1_Init();
// MX_USART1_UART_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
// 初始化保存数据
uint16_t readDataBuffer[FLASH_SAVE_SIZE];
Read_From_Flash(EDIT_FLASH_ADDRESS, readDataBuffer, FLASH_SAVE_SIZE);
set_register_range(0, FLASH_SAVE_SIZE, readDataBuffer);
//加载参数
slmp_port = get_register(85);
slmp_ip[0] = get_register(81);
slmp_ip[1] = get_register(82);
slmp_ip[2] = get_register(83);
slmp_ip[3] = get_register(84);
if (slmp_ip[0] == 0xff || slmp_ip[1] == 0xff || slmp_ip[2] == 0xff || slmp_ip[3] == 0xff) {
slmp_ip[0] = 192;
slmp_ip[1] = 168;
slmp_ip[2] = 1;
slmp_ip[3] = 9;
set_register(81, 192);
set_register(82, 168);
set_register(83, 1);
set_register(84, 9);
}
if (slmp_port == 0xffff) {
set_register(85, 4096);
}
if (get_register(91) == 0xffff) {
set_register(91, 0);
}
commType = get_register(91);
local_ip[0] = get_register(86);
local_ip[1] = get_register(87);
local_ip[2] = get_register(88);
local_ip[3] = get_register(89);
if (local_ip[0] == 0xff || local_ip[1] == 0xff || local_ip[2] == 0xff || local_ip[3] == 0xff) {
local_ip[0] = 192;
local_ip[1] = 168;
local_ip[2] = 1;
local_ip[3] = 14;
set_register(86, 192);
set_register(87, 168);
set_register(88, 1);
set_register(89, 14);
}
if (get_register(90) == 0xffff) {
set_register(90, 120);
}
set_slaveid(get_register(90));
if (get_register(80) == 0xffff) {
set_register(80, 0);
}
uint32_t BaudRate = 0;
switch (get_register(80)) { // 80号地址波特率
case 0:
BaudRate = 9600;
break;
case 1:
BaudRate = 14400;
break;
case 2:
BaudRate = 19200;
break;
case 3:
BaudRate = 38400;
break;
case 4:
BaudRate = 56000;
break;
case 5:
BaudRate = 57600;
break;
case 6:
BaudRate = 115200;
break;
default:
BaudRate = 128000;
break;
}
for (uint16_t i = 0; i < 80; i++) {
if (get_register(i) == 0xffff) {
set_register(i, 0);
}
}
// 网页
httpServer_init();
reg_httpServer_cbfunc(NVIC_SystemReset, NULL);
reg_httpServer_webContent((uint8_t*) "index.html", (uint8_t*) rtu_master);
reg_httpServer_webContent((uint8_t*) "ajax.js", (uint8_t*) w5x00web_ajax_js);
//初始化硬件
HAL_TIM_Base_Start_IT(&htim6);
w5500_init(local_ip);
MX_USART1_UART_Init(BaudRate);
HAL_GPIO_WritePin(run_lamp_GPIO_Port, run_lamp_Pin, GPIO_PIN_RESET);
combineMyRtumST();
updateMyRtumST(myRtumST);
setupRtumST(readST, writeST);
master.localSlaveId = get_slaveid();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
set_register(109, HAL_GPIO_ReadPin(run_lamp_GPIO_Port, run_lamp_Pin) ? 1 : 0);
map_in(100);
map_out(110);
RTU_Master_Loop(&master, myRtumST);
prev_cnt = comm_cnt;
loopback_tcps_modbus(0, w5500_rx, 502, &comm_cnt, &comm1);
if (slmp_send_loop < 2) {
slmp_send_loop++;
} else {
slmp_send_loop = 0;
if (commType == 0) {
loopback_tcpc_slmp(1, slmp_ip, slmp_port, &comm_cnt, &comm2);
} else if (commType == 1) {
loopback_udpc_omrfins(1, slmp_ip, slmp_port, local_ip[3], &comm_cnt, &comm2);
}
}
if (comm_cnt > 100) {
HAL_GPIO_TogglePin(comm_lamp_GPIO_Port, comm_lamp_Pin);
comm_cnt = 0;
}
if (!comm1 && !comm2) {
HAL_GPIO_WritePin(comm_lamp_GPIO_Port, comm_lamp_Pin, GPIO_PIN_SET);
}
web_loop();
reset_button();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 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();
}
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
}
/* USER CODE BEGIN 4 */
/* 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 */