first push
This commit is contained in:
1 parent
53c85e94af
commit
32f5ef9d3b
398 files changed
+1675
-140886
No files matched your search
+396
@@ -0,0 +1,396 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2026 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 "iwdg.h"
|
||||
#include "spi.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "w5500/w5500.h"
|
||||
#include "w5500/w5500_port.h"
|
||||
#include "w5500/loopback.h"
|
||||
#include "w5500/webpages.h"
|
||||
#include "w5500/httpUtil.h"
|
||||
#include "w5500/slmpMaster.h"
|
||||
#include "w5500/modbus_crc.h"
|
||||
#include "w5500/modbusSlave.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 */
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PM */
|
||||
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
uint8_t local_ip[4], slmp_ip[4];
|
||||
uint16_t slmp_port;
|
||||
|
||||
uint16_t out_word1 = 0;
|
||||
|
||||
uint16_t speed = 0;
|
||||
uint16_t speed_idx = 0;
|
||||
/* 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 */
|
||||
//////////////////////////////////////////////////////////// ↓定时器(TIME6=1MS)↓ /////////////////////////////////////////////
|
||||
uint16_t beatcnt = 0;
|
||||
_Bool beatval;
|
||||
uint16_t runbeat, rstbtn_timeout;
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
|
||||
|
||||
if (htim == &htim6) {
|
||||
update_timeout_slmp();
|
||||
update_timeout_fins();
|
||||
|
||||
if (runbeat < 1000) { //控制运行指示灯间隔1秒闪烁
|
||||
runbeat++;
|
||||
} else {
|
||||
runbeat = 0;
|
||||
HAL_GPIO_TogglePin(run_GPIO_Port, run_Pin);
|
||||
set_register(109, HAL_GPIO_ReadPin(run_GPIO_Port, run_Pin) ? 1 : 0); //109运行心跳位,间隔1秒闪烁
|
||||
}
|
||||
|
||||
if (HAL_GPIO_ReadPin(rstbtn_GPIO_Port, rstbtn_Pin) == GPIO_PIN_RESET) { //系统参数初始化等待延时,如果按下3秒系统设定数据复位
|
||||
if (rstbtn_timeout < 0xffff) {
|
||||
rstbtn_timeout++;
|
||||
} else {
|
||||
rstbtn_timeout = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//////////////////////////////////////////////////////////// ↑定时器↑ /////////////////////////////////////////////
|
||||
/////////////////////////////////////
|
||||
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;
|
||||
set_register(120, get_register(120) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////↓将110地址输出给继电器↓/////////////////////////////////////////////
|
||||
int getBit(int num, int n) {
|
||||
return (num & (1 << n)) >> n;
|
||||
}
|
||||
|
||||
void map_out(uint16_t startAddress) {
|
||||
out_word1 = get_register(startAddress);
|
||||
|
||||
(getBit(out_word1, 0)) ? HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o1_GPIO_Port, o1_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 1)) ? HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o2_GPIO_Port, o2_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 2)) ? HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o3_GPIO_Port, o3_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 3)) ? HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o4_GPIO_Port, o4_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 4)) ? HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o5_GPIO_Port, o5_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 5)) ? HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o6_GPIO_Port, o6_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 6)) ? HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o7_GPIO_Port, o7_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 7)) ? HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o8_GPIO_Port, o8_Pin, GPIO_PIN_RESET);
|
||||
|
||||
(getBit(out_word1, 8)) ? HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o9_GPIO_Port, o9_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 9)) ? HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o10_GPIO_Port, o10_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 10)) ? HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o11_GPIO_Port, o11_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 11)) ? HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o12_GPIO_Port, o12_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 12)) ? HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o13_GPIO_Port, o13_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 13)) ? HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o14_GPIO_Port, o14_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 14)) ? HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o15_GPIO_Port, o15_Pin, GPIO_PIN_RESET);
|
||||
(getBit(out_word1, 15)) ? HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(o16_GPIO_Port, o16_Pin, GPIO_PIN_RESET);
|
||||
|
||||
}
|
||||
////////////////////////////////////////////////////////////↑将110地址输出给继电器上/////////////////////////////////////////////
|
||||
|
||||
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 load_data() {
|
||||
// 初始化保存数据
|
||||
uint16_t readDataBuffer[FLASH_SAVE_SIZE];
|
||||
|
||||
Read_From_Flash(EDIT_FLASH_ADDRESS, readDataBuffer, FLASH_SAVE_SIZE);
|
||||
|
||||
set_register_range(MODBUS_SAVE_START, 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, 1025);
|
||||
}
|
||||
|
||||
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] = 13;
|
||||
set_register(86, 192);
|
||||
set_register(87, 168);
|
||||
set_register(88, 1);
|
||||
set_register(89, 13);
|
||||
}
|
||||
|
||||
if (get_register(90) < 1 || get_register(90) > 0xff) {
|
||||
set_register(90, 120);
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < 80; i++) {
|
||||
if (get_register(i) == 0xffff) {
|
||||
set_register(i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
set_slaveid(get_register(90));
|
||||
|
||||
if (get_register(80) == 0xffff) {
|
||||
set_register(80, 0);
|
||||
}
|
||||
|
||||
uint32_t BaudRate = 0;
|
||||
switch (get_register(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 = 9600;
|
||||
break;
|
||||
}
|
||||
MX_USART1_UART_Init(BaudRate);
|
||||
|
||||
if (get_register(91) == 0xffff) {
|
||||
set_register(91, 0);
|
||||
}
|
||||
|
||||
master.localSlaveId = get_register(90);
|
||||
master.isMaster = get_register(91) ? 1 : 0;
|
||||
master.plc_type = get_register(92);
|
||||
}
|
||||
|
||||
void commled() {
|
||||
if (comm_count > 100) {
|
||||
HAL_GPIO_TogglePin(comm_GPIO_Port, comm_Pin);
|
||||
comm_count = 0;
|
||||
}
|
||||
|
||||
if (getSn_SR(0) == SOCK_CLOSED && getSn_SR(1) == SOCK_CLOSED) {
|
||||
HAL_GPIO_WritePin(comm_GPIO_Port, comm_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
}
|
||||
|
||||
/* 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_IWDG_Init();
|
||||
// MX_USART1_UART_Init();
|
||||
MX_SPI1_Init();
|
||||
MX_TIM6_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
HAL_TIM_Base_Start_IT(&htim6);
|
||||
load_data();
|
||||
w5500_init(local_ip);
|
||||
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);
|
||||
initRtuMaster();
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1) {
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
map_out(110);
|
||||
reset_button();
|
||||
commled();
|
||||
|
||||
httpServer_run_main();
|
||||
|
||||
// 网口通信
|
||||
loopback_comm(slmp_ip, slmp_port, local_ip[3]);
|
||||
// 串口通信
|
||||
RTU_Master_Loop();
|
||||
}
|
||||
/* 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_HSI | RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
|
||||
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_1) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
Reference in new issue
Block a user