Initial commit: my SECS2 project

This commit is contained in:
2026-06-12 14:19:01 +08:00
commit 2c3e2c4dc2
1138 changed files with 603966 additions and 0 deletions
+247
View File
@@ -0,0 +1,247 @@
#include "bsp_DS1302.h"
#define bsp_DS1302_DELAY() do{ \
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
}while(0)
#define RST_CLR HAL_GPIO_WritePin(DS1302_RST_GPIO_Port, DS1302_RST_Pin, GPIO_PIN_RESET )
#define RST_SET HAL_GPIO_WritePin(DS1302_RST_GPIO_Port, DS1302_RST_Pin, GPIO_PIN_SET )
#define IO_CLR HAL_GPIO_WritePin(DS1302_DIO_GPIO_Port, DS1302_DIO_Pin, GPIO_PIN_RESET )
#define IO_SET HAL_GPIO_WritePin(DS1302_DIO_GPIO_Port, DS1302_DIO_Pin, GPIO_PIN_SET )
#define IO_READ HAL_GPIO_ReadPin (DS1302_DIO_GPIO_Port, DS1302_DIO_Pin )
#define SCK_CLR HAL_GPIO_WritePin(DS1302_CLK_GPIO_Port, DS1302_CLK_Pin, GPIO_PIN_RESET )
#define SCK_SET HAL_GPIO_WritePin(DS1302_CLK_GPIO_Port, DS1302_CLK_Pin, GPIO_PIN_SET )
static void bsp_DS1302Init(void);
static void bsp_DS1302_Task(void);
static u8 bsp_DS1302_Set(bsp_DS1302_Time_t *pTime);
bsp_DS1302_t DS1302 =
{
.Init = bsp_DS1302Init,
.Task = bsp_DS1302_Task,
.Set = bsp_DS1302_Set,
};
bsp_DS1302_t *pDS1302 = &DS1302;
static void bsp_DS1302DataInput(void)
{
GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO结构体
GPIO_InitStruct.Pin = DS1302_DIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DS1302_DIO_GPIO_Port, &GPIO_InitStruct);
}
static void bsp_DS1302DataOutput(void)
{
GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO结构体
GPIO_InitStruct.Pin = DS1302_DIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DS1302_DIO_GPIO_Port, &GPIO_InitStruct);
}
/*向bsp_DS1302写入一字节数据*/
static void bsp_DS1302_write_byte(u8 Addr, u8 Data)
{
u8 i;
RST_SET; /*启动bsp_DS1302总线*/
bsp_DS1302_DELAY();
/*写入目标地址:addr*/
Addr = Addr & 0xFE;/*最低位置零*/
for (i = 0; i < 8; i++)
{
if (Addr & 0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
Addr = Addr >> 1;
}
/*写入数据:d*/
for (i = 0; i < 8; i++)
{
if (Data & 0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
Data = Data >> 1;
}
RST_CLR; /*停止bsp_DS1302总线*/
bsp_DS1302_DELAY();
}
/*从bsp_DS1302读出一字节数据*/
static u8 bsp_DS1302_read_byte(u8 Addr)
{
u8 i;
u8 temp;
RST_SET; /*启动bsp_DS1302总线*/
bsp_DS1302_DELAY();
/*写入目标地址:addr*/
Addr = Addr | 0x01;/*最低位置高*/
for (i = 0; i < 8; i++)
{
if (Addr & 0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
Addr = Addr >> 1;
}
bsp_DS1302DataInput();
/*输出数据:temp*/
for (i = 0; i < 8; i++)
{
temp = temp >> 1;
if (IO_READ)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
}
RST_CLR; /*停止bsp_DS1302总线*/
bsp_DS1302_DELAY();
bsp_DS1302DataOutput();
return temp;
}
static u8 HexToBCD(u8 code)
{
u8 temp;
temp = ((code / 10) << 4) + (code % 10);
return temp;
}
static u8 bsp_DS1302_Set(bsp_DS1302_Time_t *pTime)
{
if ((pTime->Year > 99) || (pTime->Month > 12) || (pTime->Day > 31) ||
(pTime->Hour > 23) || (pTime->Minute > 59) || (pTime->Second > 59))
{
return USR_FALSE;
}
else
{
bsp_DS1302_write_byte(BSP_DS1302_CONTROL_ADDR, 0x00); //关闭写保护
bsp_DS1302_write_byte(BSP_DS1302_SEC_ADDR, 0x80); //暂停
bsp_DS1302_write_byte(BSP_DS1302_YEAR_ADDR, HexToBCD(pTime->Year));
bsp_DS1302_write_byte(BSP_DS1302_MONTH_ADDR, HexToBCD(pTime->Month));
bsp_DS1302_write_byte(BSP_DS1302_DATA_ADDR, HexToBCD(pTime->Day));
bsp_DS1302_write_byte(BSP_DS1302_HOUR_ADDR, HexToBCD(pTime->Hour));
bsp_DS1302_write_byte(BSP_DS1302_MIN_ADDR, HexToBCD(pTime->Minute));
bsp_DS1302_write_byte(BSP_DS1302_SEC_ADDR, HexToBCD(pTime->Second));
bsp_DS1302_write_byte(BSP_DS1302_CONTROL_ADDR, 0x80); //打开写保护
return USR_TRUE;
}
}
static void bsp_DS1302_Task(void)
{
u8 RegData;
RegData = bsp_DS1302_read_byte(BSP_DS1302_YEAR_ADDR);
pDS1302->Time.Year = (RegData / 16) * 10 + RegData % 16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_MONTH_ADDR);
pDS1302->Time.Month = (RegData / 16) * 10 + RegData % 16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_DATA_ADDR);
pDS1302->Time.Day = (RegData / 16) * 10 + RegData % 16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_HOUR_ADDR);
pDS1302->Time.Hour = (RegData / 16) * 10 + RegData % 16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_MIN_ADDR);
pDS1302->Time.Minute = (RegData / 16) * 10 + RegData % 16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_SEC_ADDR);
pDS1302->Time.Second = (RegData / 16) * 10 + RegData % 16;
}
static void bsp_DS1302Init(void)
{
RST_SET;
SCK_CLR;
bsp_DS1302_Task();
if ((pDS1302->Time.Year > 99) || (pDS1302->Time.Month > 12) || (pDS1302->Time.Day > 31) ||
(pDS1302->Time.Hour > 23) || (pDS1302->Time.Minute > 59) || (pDS1302->Time.Second > 59))
{
pDS1302->Time.Year = 25;
pDS1302->Time.Month = 1;
pDS1302->Time.Day = 1;
pDS1302->Time.Hour = 0;
pDS1302->Time.Minute = 0;
pDS1302->Time.Second = 0;
bsp_DS1302_Set(&pDS1302->Time);
}
}
+206
View File
@@ -0,0 +1,206 @@
#include "bsp_DS1302.h"
#define bsp_DS1302_DELAY() do{ \
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
__NOP();__NOP();__NOP();__NOP();\
}while(0)
#define RST_CLR HAL_GPIO_WritePin(DS1302_RST_GPIO_Port, DS1302_RST_Pin, GPIO_PIN_RESET )
#define RST_SET HAL_GPIO_WritePin(DS1302_RST_GPIO_Port, DS1302_RST_Pin, GPIO_PIN_SET )
#define IO_CLR HAL_GPIO_WritePin(DS1302_DIO_GPIO_Port, DS1302_DIO_Pin, GPIO_PIN_RESET )
#define IO_SET HAL_GPIO_WritePin(DS1302_DIO_GPIO_Port, DS1302_DIO_Pin, GPIO_PIN_SET )
#define IO_READ HAL_GPIO_ReadPin (DS1302_DIO_GPIO_Port, DS1302_DIO_Pin )
#define SCK_CLR HAL_GPIO_WritePin(DS1302_CLK_GPIO_Port, DS1302_CLK_Pin, GPIO_PIN_RESET )
#define SCK_SET HAL_GPIO_WritePin(DS1302_CLK_GPIO_Port, DS1302_CLK_Pin, GPIO_PIN_SET )
static void bsp_DS1302Init(void);
static void bsp_DS1302_Task(void);
static void bsp_DS1302_Set(bsp_DS1302_Time_t *pTime);
bsp_DS1302_t DS1302 =
{
.Init = bsp_DS1302Init,
.Task = bsp_DS1302_Task,
.Set = bsp_DS1302_Set,
};
bsp_DS1302_t *pDS1302 = &DS1302;
static void bsp_DS1302DataInput(void)
{
GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO结构体
GPIO_InitStruct.Pin = DS1302_DIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DS1302_DIO_GPIO_Port, &GPIO_InitStruct);
}
static void bsp_DS1302DataOutput(void)
{
GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO结构体
GPIO_InitStruct.Pin = DS1302_DIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DS1302_DIO_GPIO_Port, &GPIO_InitStruct);
}
/*向bsp_DS1302写入一字节数据*/
static void bsp_DS1302_write_byte(u8 Addr, u8 Data)
{
u8 i;
RST_SET; /*启动bsp_DS1302总线*/
bsp_DS1302_DELAY();
/*写入目标地址:addr*/
Addr = Addr & 0xFE;/*最低位置零*/
for(i=0; i<8; i++){
if(Addr&0x01) IO_SET;
else IO_CLR;
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
Addr = Addr >> 1;
}
/*写入数据:d*/
for(i=0; i<8; i++){
if(Data&0x01) IO_SET;
else IO_CLR;
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
Data = Data >> 1;
}
RST_CLR; /*停止bsp_DS1302总线*/
bsp_DS1302_DELAY();
}
/*从bsp_DS1302读出一字节数据*/
static u8 bsp_DS1302_read_byte(u8 Addr)
{
u8 i;
u8 temp;
RST_SET; /*启动bsp_DS1302总线*/
bsp_DS1302_DELAY();
/*写入目标地址:addr*/
Addr = Addr | 0x01;/*最低位置高*/
for(i=0; i<8; i++)
{
if(Addr&0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
Addr = Addr >> 1;
}
bsp_DS1302DataInput();
/*输出数据:temp*/
for(i=0; i<8; i++)
{
temp = temp>>1;
if(IO_READ) temp |= 0x80;
else temp&=0x7F;
bsp_DS1302_DELAY();
SCK_SET;
bsp_DS1302_DELAY();
SCK_CLR;
bsp_DS1302_DELAY();
}
RST_CLR; /*停止bsp_DS1302总线*/
bsp_DS1302_DELAY();
bsp_DS1302DataOutput();
return temp;
}
static u8 HexToBCD(u8 code)
{
u8 temp;
temp = ((code / 10)<<4)+(code % 10);
return temp;
}
static void bsp_DS1302_Set(bsp_DS1302_Time_t *pTime)
{
bsp_DS1302_write_byte(BSP_DS1302_CONTROL_ADDR,0x00); //关闭写保护
bsp_DS1302_write_byte(BSP_DS1302_SEC_ADDR,0x80); //暂停
bsp_DS1302_write_byte(BSP_DS1302_YEAR_ADDR, HexToBCD(pTime->Year));
bsp_DS1302_write_byte(BSP_DS1302_MONTH_ADDR, HexToBCD(pTime->Month));
bsp_DS1302_write_byte(BSP_DS1302_DATA_ADDR, HexToBCD(pTime->Day));
bsp_DS1302_write_byte(BSP_DS1302_HOUR_ADDR, HexToBCD(pTime->Hour));
bsp_DS1302_write_byte(BSP_DS1302_MIN_ADDR, HexToBCD(pTime->Minute));
bsp_DS1302_write_byte(BSP_DS1302_SEC_ADDR, HexToBCD(pTime->Second));
bsp_DS1302_write_byte(BSP_DS1302_CONTROL_ADDR,0x80); //打开写保护
}
static void bsp_DS1302_Task(void)
{
u8 RegData;
RegData = bsp_DS1302_read_byte(BSP_DS1302_YEAR_ADDR);
pDS1302->Time.Year = (RegData/16)*10 + RegData%16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_MONTH_ADDR);
pDS1302->Time.Month = (RegData/16)*10 + RegData%16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_DATA_ADDR);
pDS1302->Time.Day = (RegData/16)*10 + RegData%16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_HOUR_ADDR);
pDS1302->Time.Hour = (RegData/16)*10 + RegData%16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_MIN_ADDR);
pDS1302->Time.Minute = (RegData/16)*10 + RegData%16;
RegData = bsp_DS1302_read_byte(BSP_DS1302_SEC_ADDR);
pDS1302->Time.Second = (RegData/16)*10 + RegData%16;
}
static void bsp_DS1302Init(void)
{
RST_SET;
SCK_CLR;
bsp_DS1302_Task();
if((pDS1302->Time.Year>99)||(pDS1302->Time.Month>12)||(pDS1302->Time.Day>31)||
(pDS1302->Time.Hour>23)||(pDS1302->Time.Minute>59)||(pDS1302->Time.Second>59))
{
pDS1302->Time.Year = 25;
pDS1302->Time.Month = 1;
pDS1302->Time.Day = 1;
pDS1302->Time.Hour = 0;
pDS1302->Time.Minute = 0;
pDS1302->Time.Second = 0;
bsp_DS1302_Set(&pDS1302->Time);
}
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef __BSP_DS1302_H__
#define __BSP_DS1302_H__
#include "main.h"
#define BSP_DS1302_SEC_ADDR 0x80 //秒数据地址
#define BSP_DS1302_MIN_ADDR 0x82 //分数据地址
#define BSP_DS1302_HOUR_ADDR 0x84 //时数据地址
#define BSP_DS1302_DATA_ADDR 0x86 //日数据地址
#define BSP_DS1302_MONTH_ADDR 0x88 //月数据地址
#define BSP_DS1302_DAY_ADDR 0x8a //星期数据地址
#define BSP_DS1302_YEAR_ADDR 0x8c //年数据地址
#define BSP_DS1302_CONTROL_ADDR 0x8e //控制数据地址
#define BSP_DS1302_CHARGER_ADDR 0x90
#define BSP_DS1302_CLKBURST_ADDR 0xbe
typedef struct
{
u16 Year;
u8 Month;
u8 Day;
u8 Hour;
u8 Minute;
u8 Second;
}bsp_DS1302_Time_t;
typedef struct
{
bsp_DS1302_Time_t Time;
void (*Init)(void);
u8 (*Set)(bsp_DS1302_Time_t *);
void (*Task)(void);
}bsp_DS1302_t;
extern bsp_DS1302_t DS1302;//系统时间
#endif
+1
View File
@@ -0,0 +1 @@
#include "bsp_Delay.h"
+6
View File
@@ -0,0 +1,6 @@
#ifndef _BSP_DELAY_H_
#define _BSP_DELAY_H_
#include "main.h"
#endif
+248
View File
@@ -0,0 +1,248 @@
#include "bsp_Flash.h"
#include "string.h"
#include "bsp_Wdg.h"
#include "bsp_W5500.h"
#include "proto_HSMS.h"
/* FLASH Memory Definitions */
#define BSP_FLASH_SIZE (0x10000UL)
#define BSP_FLASH_PAGE_SIZE (FLASH_PAGE_SIZE)
#define BSP_FLASH_PAGE_NUM (BSP_FLASH_SIZE/BSP_FLASH_PAGE_SIZE)
#define BSP_FLASH_ADDR_RW(n) ((uint32_t)(FLASH_BASE + (BSP_FLASH_PAGE_NUM - (n)) * BSP_FLASH_PAGE_SIZE))
#define BSP_FLASH_DATASAVE_ADDR BSP_FLASH_ADDR_RW(1)
static void bsp_Flash_Init(void);
static void bsp_FlashDataWrite(void);
static void bsp_FlashDataRead(void);
static void bsp_FlashReset(void);
bsp_Flash_t Usr_Flash =
{
.Init = bsp_Flash_Init,
.Write = bsp_FlashDataWrite,
.Read = bsp_FlashDataRead,
.Reset = bsp_FlashReset,
};
// 擦除指定页
static HAL_StatusTypeDef bsp_FLASH_ErasePage(uint32_t PageAddress)
{
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t PageError = 0;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = PageAddress;
EraseInitStruct.NbPages = 1;
return HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
}
// 读取数据 - 按32位读取
static void bsp_Flash_STMFLASH_Read(uint32_t ReadAddr, void *pBuffer, uint32_t size)
{
uint8_t *pBuf = (uint8_t*)pBuffer;
uint32_t *addr = (uint32_t*)ReadAddr;
uint32_t words = size / 4;
uint32_t bytes_remaining = size % 4;
// 读取完整的32位字
for(uint32_t i = 0; i < words; i++)
{
*((uint32_t*)pBuf) = addr[i];
pBuf += 4;
}
// 读取剩余的字节
if(bytes_remaining > 0)
{
uint32_t last_word = addr[words];
uint8_t *last_bytes = (uint8_t*)&last_word;
for(uint32_t i = 0; i < bytes_remaining; i++)
{
pBuf[i] = last_bytes[i];
}
}
}
// 写入数据 - 按32位写入
static HAL_StatusTypeDef bsp_Flash_STMFLASH_Write(uint32_t WriteAddr, void *pBuffer, uint32_t size)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t *pBuf = (uint8_t*)pBuffer;
uint32_t words = size / 4;
uint32_t bytes_remaining = size % 4;
uint32_t current_addr = WriteAddr;
HAL_FLASH_Unlock();
// 擦除目标页
status = bsp_FLASH_ErasePage(WriteAddr);
if(status != HAL_OK)
{
HAL_FLASH_Lock();
return status;
}
// 写入完整的32位字
for(uint32_t i = 0; i < words; i++)
{
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
current_addr,
*((uint32_t*)pBuf));
if(status != HAL_OK) break;
current_addr += 4;
pBuf += 4;
}
// 写入剩余的字节
if(status == HAL_OK && bytes_remaining > 0)
{
uint32_t last_word = 0xFFFFFFFF; // 默认填充0xFF
uint8_t *last_bytes = (uint8_t*)&last_word;
// 复制剩余数据
for(uint32_t i = 0; i < bytes_remaining; i++)
{
last_bytes[i] = pBuf[i];
}
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, current_addr, last_word);
}
HAL_FLASH_Lock();
return status;
}
static void bsp_FlashReset(void)
{
u16 port = 5000;
Usr_Flash.FlashData.IP_Addr[0] = 192;
Usr_Flash.FlashData.IP_Addr[1] = 168;
Usr_Flash.FlashData.IP_Addr[2] = 0;
Usr_Flash.FlashData.IP_Addr[3] = 100;
Usr_Flash.FlashData.Port[0] = port >> 8;
Usr_Flash.FlashData.Port[1] = port & 0x00ff;
Usr_Flash.FlashData.Sub_Mask[0] = 255;
Usr_Flash.FlashData.Sub_Mask[1] = 255;
Usr_Flash.FlashData.Sub_Mask[2] = 255;
Usr_Flash.FlashData.Sub_Mask[3] = 0;
Usr_Flash.FlashData.Gateway_IP[0] = 192;
Usr_Flash.FlashData.Gateway_IP[1] = 168;
Usr_Flash.FlashData.Gateway_IP[2] = 0;
Usr_Flash.FlashData.Gateway_IP[3] = 1;
Usr_Flash.FlashData.Devic_Id = 0;
// /*设备型号 - 'Atlas Abatement' */
// Usr_Flash.FlashData.Device_ID[0] = ('A' << 8) | 't'; // 'At'
// Usr_Flash.FlashData.Device_ID[1] = ('l' << 8) | 'a'; // 'la'
// Usr_Flash.FlashData.Device_ID[2] = ('s' << 8) | ' '; // 's '
// Usr_Flash.FlashData.Device_ID[3] = ('A' << 8) | 'b'; // 'Ab'
// Usr_Flash.FlashData.Device_ID[4] = ('a' << 8) | 't'; // 'at'
// Usr_Flash.FlashData.Device_ID[5] = ('e' << 8) | 'm'; // 'em'
// Usr_Flash.FlashData.Device_ID[6] = ('e' << 8) | 'n'; // 'en'
// Usr_Flash.FlashData.Device_ID[7] = ('t' << 8) | 0x00; // 't\0'
//
// /*设备标识 - 'AHTWA07CHAAC' */
// Usr_Flash.FlashData.Device_Model[0] = ('A' << 8) | 'H'; // 'AH'
// Usr_Flash.FlashData.Device_Model[1] = ('T' << 8) | 'W'; // 'TW'
// Usr_Flash.FlashData.Device_Model[2] = ('A' << 8) | '0'; // 'A0'
// Usr_Flash.FlashData.Device_Model[3] = ('7' << 8) | 'C'; // '7C'
// Usr_Flash.FlashData.Device_Model[4] = ('H' << 8) | 'A'; // 'HA'
// Usr_Flash.FlashData.Device_Model[5] = ('A' << 8) | 'C'; // 'AC'
//
// Usr_Flash.FlashData.Data_ID1 = 0;
// Usr_Flash.FlashData.Collection_event_ID1 = 111101; // 文档中的CEID
//// Usr_Flash.FlashData.Report_ID1 = 2329004; // 文档中的Report ID
// Usr_Flash.FlashData.Alarm_ID = 2320614; // 文档中的AlarmID
//
// /* 报警类型初始化 */
// const char *alarm_types[] = {"Major Warning", "Minor Warning", "Major Alarm", "Minor Alarm"};
// for(int i = 0; i < 4; i++) {
// const char *type = alarm_types[i];
// for(int j = 0; j < 13 && type[j]; j++) {
// if(j % 2 == 0) {
// Usr_Flash.FlashData.Alarm_Type[i*2 + j/2] = (type[j] << 8) | (type[j+1] ? type[j+1] : 0);
// }
// }
// }
bsp_FlashDataWrite();
}
static void bsp_Flash_Init(void)
{
u8 i;
bsp_FlashDataRead();
if( Usr_Flash.FlashData.IP_Addr[0] == 0xff
&& Usr_Flash.FlashData.IP_Addr[1] == 0xff
&& Usr_Flash.FlashData.IP_Addr[2] == 0xff
&& Usr_Flash.FlashData.IP_Addr[3] == 0xff
)
{
bsp_FlashReset();
}
memcpy(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t));
W5500.IP_Addr[0] = Usr_Flash.FlashData.IP_Addr[0];
W5500.IP_Addr[1] = Usr_Flash.FlashData.IP_Addr[1];
W5500.IP_Addr[2] = Usr_Flash.FlashData.IP_Addr[2];
W5500.IP_Addr[3] = Usr_Flash.FlashData.IP_Addr[3];
W5500.Gateway_IP[0] =Usr_Flash.FlashData.Gateway_IP[0];
W5500.Gateway_IP[1]=Usr_Flash.FlashData.Gateway_IP[1];
W5500.Gateway_IP[2]=Usr_Flash.FlashData.Gateway_IP[2];
W5500.Gateway_IP[3]=Usr_Flash.FlashData.Gateway_IP[3];
W5500.W5500_Class[0].ConfigData.Port[0] = Usr_Flash.FlashData.Port[0];
W5500.W5500_Class[0].ConfigData.Port[1] = Usr_Flash.FlashData.Port[1];
W5500.Sub_Mask[0] = Usr_Flash.FlashData.Sub_Mask[0];
W5500.Sub_Mask[1] = Usr_Flash.FlashData.Sub_Mask[1];
W5500.Sub_Mask[2] = Usr_Flash.FlashData.Sub_Mask[2];
W5500.Sub_Mask[3] = Usr_Flash.FlashData.Sub_Mask[3];
HSMS.Flash_ConfigData.Device_Id = Usr_Flash.FlashData.Devic_Id;
}
static void bsp_FlashDataWrite(void)
{
/*防止重复擦写相同数据*/
if(memcmp(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t)) != 0)
{
Wdg.Feed();
__disable_irq(); // 禁用全局中断
HAL_StatusTypeDef status = bsp_Flash_STMFLASH_Write(BSP_FLASH_DATASAVE_ADDR,&Usr_Flash.FlashData,sizeof(bsp_FlashData_t));
if(status == HAL_OK)
{
// 写入成功,更新临时数据
memcpy(&Usr_Flash.TempFlashData, &Usr_Flash.FlashData, sizeof(bsp_FlashData_t));
}
else
{
}
__enable_irq(); // 恢复中断
Wdg.Feed();
}
}
static void bsp_FlashDataRead(void)
{
Wdg.Feed();
bsp_Flash_STMFLASH_Read(BSP_FLASH_DATASAVE_ADDR,
&Usr_Flash.FlashData,
sizeof(bsp_FlashData_t));
Wdg.Feed();
}
+63
View File
@@ -0,0 +1,63 @@
#ifndef _BSP_FLASH_H_
#define _BSP_FLASH_H_
#include "main.h"
typedef struct
{
/*用于判断数据是否一致*/
u16 SN[5];
u8 IP_Addr[4];
u8 Gateway_IP[4];
u8 Port[2];
u8 Sub_Mask[4];
u16 Devic_Id;
u16 Device_ID[8]; // 'Atlas Abatement' - 15字符,设备型号(16字节)
u16 Device_Model[8]; // 'AHTWA07CHAAC' - 12字符,报警机台和位置(12字节)
u16 Device_SN[8]; //设备SN号
u16 Station_Name[8]; // 机台名称
u16 Chamber_Name[8]; // 腔体名称
u16 Manufacturer[8]; //厂商名称-10字符
u16 Version[8]; //机台软件版本号-10字符
u16 Station_Type[8]; //机台类型
u16 Alarm_info[20]; //使用PM100中的
u16 Data_ID1; //置空
u32 Collection_event_ID1; //CEID为固定值
u32 Report_ID1; //置空
u32 Alarm_ID; //使用函数内部的
u16 Alarm_Type[7]; // 使用函数内部
u16 Begin_Warning[7]; //函数内部固定为begin
u16 IDS[11]; //置空
u16 Over_Trigger[9];
u16 Equipment[5];
u16 E9[19];
u16 Device_Type; //置空
u16 Sensor;
u16 Alarm_value;
u16 Alarm_ubit;
u16 Start_Time[11]; //开始时间
u16 Alarm_Time[6]; //报警时长
u16 Alarm[6]; //报警类型 策略报警、机台报警
u16 Data_ID2;
u16 Collection_event_ID2;
u16 Report_ID2;
u16 Sub_Device_Type[2]; // 附属设备类型
u16 Sub_Device_State[2]; // 附属设备状态
u16 State_Change_Time[2]; // 状态切换时间格式
} bsp_FlashData_t;
typedef struct
{
bsp_FlashData_t TempFlashData;
bsp_FlashData_t FlashData;
void (*Init)(void);
void (*Write)(void);
void (*Read)(void);
void (*Reset)(void);
} bsp_Flash_t;
extern bsp_Flash_t Usr_Flash;
#endif
+39
View File
@@ -0,0 +1,39 @@
#include "bsp_Key.h"
#include "os_timer.h"
#include "bsp_Flash.h"
#define BSP_KEY_ENTER_NUM 500
static void bsp_key_init(void);
static void bsp_key_task(void);
bsp_key_t key =
{
.init = bsp_key_init,
.task = bsp_key_task,
};
static bsp_key_t *p_key = &key;
static void bsp_key_init(void)
{
}
static void bsp_key_task(void)
{
if(GPIO_PIN_RESET == HAL_GPIO_ReadPin(SYS_RESET_GPIO_Port,SYS_RESET_Pin))
{
p_key->count++;
}
else
{
p_key->count = 0;
}
if(BSP_KEY_ENTER_NUM <= p_key->count)
{
/*ϵͳ¸´Î»*/
Usr_Flash.Reset();
HAL_NVIC_SystemReset();
}
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef _BSP_KEY_H_
#define _BSP_KEY_H_
#include "main.h"
typedef struct
{
u16 count;
void (*init)(void);
void (*task)(void);
}bsp_key_t;
extern bsp_key_t key;
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef _BSP_LED_H_
#define _BSP_LED_H_
#include "main.h"
typedef struct
{
void (*Init)(void);
void (*Flash)(void);
}bsp_Led_t;
extern bsp_Led_t Led;
#endif
+25
View File
@@ -0,0 +1,25 @@
#include "bsp_Led.h"
#include "os_timer.h"
static void bsp_Led_Init(void);
static void bsp_Led_Flash(void);
bsp_Led_t Led =
{
.Init = bsp_Led_Init,
.Flash = bsp_Led_Flash,
};
/*其他外设初始化后快速闪烁,提示初始化完成*/
static void bsp_Led_Init(void)
{
for(u8 i = 0;i < 20;i++)
{
Delay_ms(50);
HAL_GPIO_TogglePin(LED_RUN_GPIO_Port, LED_RUN_Pin);
}
}
static void bsp_Led_Flash(void)
{
HAL_GPIO_TogglePin(LED_RUN_GPIO_Port, LED_RUN_Pin);
}
+207
View File
@@ -0,0 +1,207 @@
#include "bsp_Uart.h"
#include "string.h"
#define RX_TEMP_BUFF_NUM (1100)
#define UART1_TX_LEN (32)
#define UART1_RX_LEN (1100)
#define UART2_TX_LEN (32)
#define UART2_RX_LEN (1100)
u8 Rx_Temp_Buff[RX_TEMP_BUFF_NUM];
/*发送使用DMA发送 接收*/
u8 Uart1_TX_Buff[UART1_TX_LEN];
u8 Uart1_Rx_Buff[UART1_RX_LEN];
u8 Uart2_TX_Buff[UART1_TX_LEN];
u8 Uart2_Rx_Buff[UART1_RX_LEN];
/*接收使用DMA+串口空闲+超时检测*/
static void bsp_Uart_Init(bsp_Uart_t *p_Uart);
static void bsp_Uart_Send(bsp_Uart_t *p_Uart,u8 *pData, u16 Len);
static void bsp_Uart_Tx_DMA_TCInt(bsp_Uart_t *p_Uart);
static void bsp_Uart_Rx_IdleInt(bsp_Uart_t *p_Uart);
static void bsp_Uart_Rx_TimeIncrement(bsp_Uart_t *p_Uart,u16 Time);
static void bsp_Uart_Rx_TimeStart(bsp_Uart_t *p_Uart);
static void bsp_Uart_Rx_TimeStop(bsp_Uart_t *p_Uart);
static void bsp_Uart_Rx_Task(bsp_Uart_t *p_Uart);
bsp_Uart_t COM_Uart1 =
{
.RxQueue = queue(u8,UART1_RX_LEN),
.Uart =USART1,
.Tx_DMA = DMA1,
.Rx_DMA = DMA1,
.Rx_DMA_CH = LL_DMA_CHANNEL_4,
.Tx_DMA_CH = LL_DMA_CHANNEL_5,
.Tx_DMA_Len = UART1_TX_LEN,
.Rx_DMA_Len = UART1_RX_LEN,
.Tx_Addr = &Uart1_TX_Buff[0],
.Rx_Addr = &Uart1_Rx_Buff[0],
.Tx_DMA_CompleteFlag = 1,
.Rx_TimeOver = 10, /*接收超时时间10ms 设置为0时为串口空闲中断触发 无超时检测*/
.Init = bsp_Uart_Init,
.Send = bsp_Uart_Send,
.Tx_DMA_TCInt = bsp_Uart_Tx_DMA_TCInt,
.Rx_IdleInt = bsp_Uart_Rx_IdleInt,
.Rx_TimeIncrementInt = bsp_Uart_Rx_TimeIncrement,
.Rx_DataAnalysis = NULL,
.Rx_Task = bsp_Uart_Rx_Task,
};
bsp_Uart_t COM_Uart2 =
{
.RxQueue = queue(u8,UART2_RX_LEN),
.Uart =USART2,
.Tx_DMA = DMA1,
.Rx_DMA = DMA1,
.Rx_DMA_CH = LL_DMA_CHANNEL_1,
.Tx_DMA_CH = LL_DMA_CHANNEL_2,
.Tx_DMA_Len = UART2_TX_LEN,
.Rx_DMA_Len = UART2_RX_LEN,
.Tx_Addr = &Uart2_TX_Buff[0],
.Rx_Addr = &Uart2_Rx_Buff[0],
.Tx_DMA_CompleteFlag = 1,
.Rx_TimeOver = 5, /*接收超时时间10ms设置为0时为串口空闲中断触发 无超时检测*/
.Init = bsp_Uart_Init,
.Send = bsp_Uart_Send,
.Tx_DMA_TCInt = bsp_Uart_Tx_DMA_TCInt,
.Rx_IdleInt = bsp_Uart_Rx_IdleInt,
.Rx_TimeIncrementInt = bsp_Uart_Rx_TimeIncrement,
.Rx_DataAnalysis = NULL,
.Rx_Task = bsp_Uart_Rx_Task,
};
static void bsp_Uart_Init(bsp_Uart_t *p_Uart)
{
/*配置数据解析函数*/
p_Uart->Rx_DataAnalysis = NULL;
/*RX*/
if(NULL == p_Uart->Uart)
return;
LL_USART_EnableIT_RXNE(p_Uart->Uart); //使能串口接收中断
if(NULL != p_Uart->Rx_DMA)
{
}
}
static void bsp_Uart2_DMASend(bsp_Uart_t *p_Uart,u8 *pData, u16 Len)
{
while (!p_Uart->Tx_DMA_CompleteFlag); /*等待传输完成标志*/
p_Uart->Tx_DMA_CompleteFlag = 0;
if(p_Uart->Tx_DMA_Len < Len)
Len = p_Uart->Tx_DMA_Len;
memcpy(p_Uart->Tx_Addr, pData, Len); /*拷贝数据到发送缓冲*/
}
/*空闲中断*/
static void bsp_Uart_Rx_IdleInt(bsp_Uart_t *p_Uart)
{
u8 rx_data;
rx_data = LL_USART_ReceiveData8(p_Uart->Uart);
queue_push_back(p_Uart->RxQueue,(void *)&rx_data);
/*开始计数*/
bsp_Uart_Rx_TimeStart(p_Uart);
}
/*大数据量发送*/
static void bsp_Uart_Send(bsp_Uart_t *p_Uart,u8 *pData, u16 Len)
{
u16 i;
for (i = 0; i < Len; i++)
{
while (!LL_USART_IsActiveFlag_TXE(p_Uart->Uart));
LL_USART_TransmitData8(p_Uart->Uart, pData[i]); // 发送一个字节的数据
}
}
static void bsp_Uart_Tx_DMA_TCInt(bsp_Uart_t *p_Uart)
{
p_Uart->Tx_DMA_CompleteFlag = 1;
}
/*中断计数*/
static void bsp_Uart_Rx_TimeIncrement(bsp_Uart_t *p_Uart,u16 Time)
{
/*开始计数*/
if(1 == p_Uart->Rx_StartFlag)
{
p_Uart->Rx_TimeCount += Time;
}
}
/*开始计数*/
static void bsp_Uart_Rx_TimeStart(bsp_Uart_t *p_Uart)
{
p_Uart->Rx_StartFlag = 1;
p_Uart->Rx_TimeCount = 0;
}
/*停止计数*/
static void bsp_Uart_Rx_TimeStop(bsp_Uart_t *p_Uart)
{
p_Uart->Rx_StartFlag = 0;
p_Uart->Rx_TimeCount = 0;
}
static void bsp_Uart_Rx_Task(bsp_Uart_t *p_Uart)
{
/*超时计数完成,接收到一帧数据*/
if(p_Uart->Rx_TimeOver < p_Uart->Rx_TimeCount)
{
p_Uart->Rx_Len = queue_size(p_Uart->RxQueue);
/*停止计数*/
bsp_Uart_Rx_TimeStop(p_Uart);
if(p_Uart->Rx_Len <= p_Uart->Rx_DMA_Len && (0 != p_Uart->Rx_Len))
{
if(RX_TEMP_BUFF_NUM < p_Uart->Rx_Len)
{
queue_clear(p_Uart->RxQueue);
}
else
{
for(u16 i = 0;i < p_Uart->Rx_Len;i++)
{
queue_pop(p_Uart->RxQueue,&Rx_Temp_Buff[i]);
}
if(NULL != p_Uart->Rx_DataAnalysis)
{
p_Uart->Rx_DataAnalysis(Rx_Temp_Buff,p_Uart->Rx_Len,p_Uart); /*解析数据*/
}
//p_Uart->Send(p_Uart,Rx_Buff,p_Uart->Rx_Len);
}
}
}
}
+44
View File
@@ -0,0 +1,44 @@
#ifndef _BSP_UART_H_
#define _BSP_UART_H_
#include "main.h"
#include "algo_queue.h"
typedef struct bsp_Uart_t bsp_Uart_t;
struct bsp_Uart_t
{
queue RxQueue; /*数据接收队列*/
USART_TypeDef *Uart; /*串口*/
DMA_TypeDef *Tx_DMA; /*DMA*/
DMA_TypeDef *Rx_DMA;
u8 Tx_DMA_CH;
u8 Rx_DMA_CH;
vu8 Tx_DMA_CompleteFlag; /*DMA接受完成标志位*/
u8 *Tx_Addr; /*DMA搬运缓冲*/
u8 *Rx_Addr;
u16 Tx_DMA_Len;
u16 Rx_DMA_Len;
u16 Rx_Len; /*接收到的数据长度*/
u16 Rx_TimeCount; /*超时计数*/
u16 Rx_TimeOver; /*超时时间*/
u8 Rx_StartFlag; /*开始超时计数标志位*/
void (*Init)(bsp_Uart_t *); /*初始化*/
void (*Send)(bsp_Uart_t *,u8 *,u16); /*串口发送函数*/
void (*Tx_DMA_TCInt)(bsp_Uart_t *); /*DMA发送完成中断*/
void (*Rx_IdleInt)(bsp_Uart_t *); /*空闲中断*/
void (*Rx_TimeIncrementInt)(bsp_Uart_t *,u16); /*中断计数计数*/
void (*Rx_DataAnalysis)(u8 *,u16,void *); /*数据解析*/
void (*Rx_Task)(bsp_Uart_t *); /*串口接收任务*/
};
extern bsp_Uart_t COM_Uart1;
extern bsp_Uart_t COM_Uart2;
#endif
+852
View File
@@ -0,0 +1,852 @@
/**********************************************************************************
* 文件名 W5500.c
* 描述 :W5500 驱动函数库
* 库版本 ST_v3.5
* 作者 :泥人通信模块开发团队
* 博客 http://nirenelec.blog.163.com
* 淘宝 http://nirenelec.taobao.com
**********************************************************************************/
#include "stm32f1xx.h"
#include "stm32f1xx_hal_spi.h"
#include "bsp_W5500.h"
#include "usart.h"
#include "stdio.h"
#include "spi.h"
#include "bsp_print.h"
#define BSP_W5500_SPI_CS_LOW
/*Run_Mode 端口的运行模式*/
#define BSP_W5500_PORT_RUN_MODE_TCP_SERVER 0x00 /*TCP服务器模式*/
#define BSP_W5500_PORT_RUN_MODE_TCP_CLIENT 0x01 /*TCP客户端模式*/
#define BSP_W5500_PORT_RUN_MODE_UDP 0x02 /*UDP(广播)模式*/
/*Run_State 端口的运行状态 BIT位*/
#define BSP_W5500_PORT_RUN_STATE_INIT 0x01 /*端口完成初始化*/
#define BSP_W5500_PORT_RUN_STATE_CONN 0x02 /*端口完成连接,可以正常传输数据*/
/*TR_Data_State 端口的收发数据状态*/
#define BSP_W5500_PORT_DATA_RECEIVE 0x01 /*端口接收到一个数据包*/
#define BSP_W5500_PORT_DATA_TRANSMITOK 0x02 /*端口发送一个数据包完成*/
static void bsp_W5500_Interrupt_Process(void);
static void bsp_W5500_Init(void);
static void bsp_W5500_Task(void);
static void Write_SOCK_Data_Buffer(bsp_W5500_Class_t *pW5500_Class, u8 *dat_ptr, u16 size);
static void network_monitor_task(void);
void bsp_W5500_Socket_Set(bsp_W5500_Class_t *pW5500_Class);
static u8 last_link_status = 0; // 保存上一次链路状态
bsp_W5500_t W5500 =
{
.Gateway_IP = {192,168,1,1},
.Sub_Mask = {255,255,255,0},
.Phy_Addr = {0x0c,0x29,0xab,0x7c,0x00,0x01},
//.IP_Addr = {169,254,107,101},
.IP_Addr = {192,168,1,101},
.Monitor_task = network_monitor_task,
.Interrupt_Process = bsp_W5500_Interrupt_Process,
.Init = bsp_W5500_Init,
.Task = bsp_W5500_Task,
.Socket_Send = Write_SOCK_Data_Buffer,
.W5500_Class[0] =
{
.SocketPort = 0, /*使用端口0*/
.ConfigData.Port = {0x13,0x88},
// .ConfigData.DIP = {192,168,1,32},
// .ConfigData.DPort = {0x03,0x09},
.Run_Mode = BSP_W5500_PORT_RUN_MODE_TCP_SERVER,
// .Rx_DataAnalysis = proto_HSMS_Rx_DataAnalysis,
},
};
bsp_W5500_t *pW5500 = &W5500;
/*******************************************************************************
* 函数名 : SPI1_Send_Byte
* 描述 : SPI1发送1个字节数据
* 输入 : dat:待发送的数据
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void SPI1_Send_Byte(u8 dat)
{
// hspi1.Instance->DR=dat;
HAL_SPI_Transmit(&hspi1, &dat, 1, 0xff);
// while(__HAL_SPI_GET_FLAG(&hspi1,SPI_FLAG_TXE)==RESET);
// SPI_I2S_SendData(SPI1,dat);//写1个字节数据
// while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//等待数据寄存器空
}
/*******************************************************************************
* 函数名 : SPI1_Send_Short
* 描述 : SPI1发送2个字节数据(16位)
* 输入 : dat:待发送的16位数据
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void SPI1_Send_Short(u16 dat)
{
SPI1_Send_Byte(dat >> 8); // 写数据高位
SPI1_Send_Byte(dat); // 写数据低位
}
/*******************************************************************************
* 函数名 : Write_W5500_1Byte
* 描述 : 通过SPI1向指定地址寄存器写1个字节数据
* 输入 : reg:16位寄存器地址,dat:待写入的数据
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void Write_W5500_1Byte(u16 reg, u8 dat)
{
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM1 | RWB_WRITE | COMMON_R); // 通过SPI1写控制字节,1个字节数据长度,写数据,选择通用寄存器
SPI1_Send_Byte(dat); // 写1个字节数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
}
/*******************************************************************************
* 函数名 : Write_W5500_2Byte
* 描述 : 通过SPI1向指定地址寄存器写2个字节数据
* 输入 : reg:16位寄存器地址,dat:16位待写入的数据(2个字节)
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void Write_W5500_2Byte(u16 reg, u16 dat)
{
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM2 | RWB_WRITE | COMMON_R); // 通过SPI1写控制字节,2个字节数据长度,写数据,选择通用寄存器
SPI1_Send_Short(dat); // 写16位数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
}
/*******************************************************************************
* 函数名 : Write_W5500_nByte
* 描述 : 通过SPI1向指定地址寄存器写n个字节数据
* 输入 : reg:16位寄存器地址,*dat_ptr:待写入数据缓冲区指针,size:待写入的数据长度
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void Write_W5500_nByte(u16 reg, u8 *dat_ptr, u16 size)
{
u16 i;
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(VDM | RWB_WRITE | COMMON_R); // 通过SPI1写控制字节,N个字节数据长度,写数据,选择通用寄存器
for (i = 0; i < size; i++) // 循环将缓冲区的size个字节数据写入W5500
{
SPI1_Send_Byte(*dat_ptr++); // 写一个字节数据
}
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
}
/*******************************************************************************
* 函数名 : Write_W5500_SOCK_1Byte
* 描述 : 通过SPI1向指定端口寄存器写1个字节数据
* 输入 : s:端口号,reg:16位寄存器地址,dat:待写入的数据
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void Write_W5500_SOCK_1Byte(SOCKET s, u16 reg, u8 dat)
{
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM1 | RWB_WRITE | (s * 0x20 + 0x08)); // 通过SPI1写控制字节,1个字节数据长度,写数据,选择端口s的寄存器
SPI1_Send_Byte(dat); // 写1个字节数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
}
/*******************************************************************************
* 函数名 : Write_W5500_SOCK_2Byte
* 描述 : 通过SPI1向指定端口寄存器写2个字节数据
* 输入 : s:端口号,reg:16位寄存器地址,dat:16位待写入的数据(2个字节)
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void Write_W5500_SOCK_2Byte(SOCKET s, u16 reg, u16 dat)
{
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM2 | RWB_WRITE | (s * 0x20 + 0x08)); // 通过SPI1写控制字节,2个字节数据长度,写数据,选择端口s的寄存器
SPI1_Send_Short(dat); // 写16位数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
}
/*******************************************************************************
* 函数名 : Write_W5500_SOCK_4Byte
* 描述 : 通过SPI1向指定端口寄存器写4个字节数据
* 输入 : s:端口号,reg:16位寄存器地址,*dat_ptr:待写入的4个字节缓冲区指针
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
void Write_W5500_SOCK_4Byte(SOCKET s, u16 reg, u8 *dat_ptr)
{
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM4 | RWB_WRITE | (s * 0x20 + 0x08)); // 通过SPI1写控制字节,4个字节数据长度,写数据,选择端口s的寄存器
SPI1_Send_Byte(*dat_ptr++); // 写第1个字节数据
SPI1_Send_Byte(*dat_ptr++); // 写第2个字节数据
SPI1_Send_Byte(*dat_ptr++); // 写第3个字节数据
SPI1_Send_Byte(*dat_ptr++); // 写第4个字节数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
}
/*******************************************************************************
* 函数名 : Read_W5500_1Byte
* 描述 : 读W5500指定地址寄存器的1个字节数据
* 输入 : reg:16位寄存器地址
* 输出 : 无
* 返回值 : 读取到寄存器的1个字节数据
* 说明 : 无
*******************************************************************************/
u8 Read_W5500_1Byte(u16 reg)
{
u8 i;
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM1 | RWB_READ | COMMON_R); // 通过SPI1写控制字节,1个字节数据长度,读数据,选择通用寄存器
i = hspi1.Instance->DR;
// HAL_SPI_Receive(&hspi1,&i,1,0xf);
// i=SPI_I2S_ReceiveData(SPI1);
SPI1_Send_Byte(0x00); // 发送一个哑数据
i = hspi1.Instance->DR;
// HAL_SPI_Receive(&hspi1,&i,1,0xf);
// i=SPI_I2S_ReceiveData(SPI1);//读取1个字节数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
return i; // 返回读取到的寄存器数据
}
/*******************************************************************************
* 函数名 : Read_W5500_SOCK_1Byte
* 描述 : 读W5500指定端口寄存器的1个字节数据
* 输入 : s:端口号,reg:16位寄存器地址
* 输出 : 无
* 返回值 : 读取到寄存器的1个字节数据
* 说明 : 无
*******************************************************************************/
u8 Read_W5500_SOCK_1Byte(SOCKET s, u16 reg)
{
u8 i;
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM1 | RWB_READ | (s * 0x20 + 0x08)); // 通过SPI1写控制字节,1个字节数据长度,读数据,选择端口s的寄存器
i = hspi1.Instance->DR;
// i=SPI_I2S_ReceiveData(SPI1);
SPI1_Send_Byte(0x00); // 发送一个哑数据
i = hspi1.Instance->DR;
// i=SPI_I2S_ReceiveData(SPI1);//读取1个字节数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
return i; // 返回读取到的寄存器数据
}
/*******************************************************************************
* 函数名 : Read_W5500_SOCK_2Byte
* 描述 : 读W5500指定端口寄存器的2个字节数据
* 输入 : s:端口号,reg:16位寄存器地址
* 输出 : 无
* 返回值 : 读取到寄存器的2个字节数据(16位)
* 说明 : 无
*******************************************************************************/
u16 Read_W5500_SOCK_2Byte(SOCKET s, u16 reg)
{
u16 i;
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(reg); // 通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM2 | RWB_READ | (s * 0x20 + 0x08)); // 通过SPI1写控制字节,2个字节数据长度,读数据,选择端口s的寄存器
i = hspi1.Instance->DR;
// i=SPI_I2S_ReceiveData(SPI1);
SPI1_Send_Byte(0x00); // 发送一个哑数据
i = hspi1.Instance->DR;
// i=SPI_I2S_ReceiveData(SPI1);//读取高位数据
SPI1_Send_Byte(0x00); // 发送一个哑数据
i *= 256;
i += hspi1.Instance->DR;
// i+=SPI_I2S_ReceiveData(SPI1);//读取低位数据
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
return i; // 返回读取到的寄存器数据
}
/*******************************************************************************
* 函数名 : Read_SOCK_Data_Buffer
* 描述 : 从W5500接收数据缓冲区中读取数据
* 输入 : s:端口号,*dat_ptr:数据保存缓冲区指针
* 输出 : 无
* 返回值 : 读取到的数据长度,rx_size个字节
* 说明 : 无
*******************************************************************************/
u16 Read_SOCK_Data_Buffer(SOCKET s, u8 *dat_ptr)
{
u16 rx_size;
u16 offset, offset1;
u16 i;
u8 j;
rx_size = Read_W5500_SOCK_2Byte(s, Sn_RX_RSR);
if (rx_size == 0)
return 0; // 没接收到数据则返回
if (rx_size > 1460)
rx_size = 1460;
offset = Read_W5500_SOCK_2Byte(s, Sn_RX_RD);
offset1 = offset;
offset &= (S_RX_SIZE - 1); // 计算实际的物理地址
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(offset); // 写16位地址
SPI1_Send_Byte(VDM | RWB_READ | (s * 0x20 + 0x18)); // 写控制字节,N个字节数据长度,读数据,选择端口s的寄存器
j = hspi1.Instance->DR;
// j=SPI_I2S_ReceiveData(SPI1);
if ((offset + rx_size) < S_RX_SIZE) // 如果最大地址未超过W5500接收缓冲区寄存器的最大地址
{
for (i = 0; i < rx_size; i++) // 循环读取rx_size个字节数据
{
SPI1_Send_Byte(0x00); // 发送一个哑数据
j = hspi1.Instance->DR;
// j=SPI_I2S_ReceiveData(SPI1);//读取1个字节数据
*dat_ptr = j; // 将读取到的数据保存到数据保存缓冲区
dat_ptr++; // 数据保存缓冲区指针地址自增1
}
}
else // 如果最大地址超过W5500接收缓冲区寄存器的最大地址
{
offset = S_RX_SIZE - offset;
for (i = 0; i < offset; i++) // 循环读取出前offset个字节数据
{
SPI1_Send_Byte(0x00); // 发送一个哑数据
j = hspi1.Instance->DR;
// j=SPI_I2S_ReceiveData(SPI1);//读取1个字节数据
*dat_ptr = j; // 将读取到的数据保存到数据保存缓冲区
dat_ptr++; // 数据保存缓冲区指针地址自增1
}
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(0x00); // 写16位地址
SPI1_Send_Byte(VDM | RWB_READ | (s * 0x20 + 0x18)); // 写控制字节,N个字节数据长度,读数据,选择端口s的寄存器
j = hspi1.Instance->DR;
// j=SPI_I2S_ReceiveData(SPI1);
for (; i < rx_size; i++) // 循环读取后rx_size-offset个字节数据
{
SPI1_Send_Byte(0x00); // 发送一个哑数据
j = hspi1.Instance->DR;
// j=SPI_I2S_ReceiveData(SPI1);//读取1个字节数据
*dat_ptr = j; // 将读取到的数据保存到数据保存缓冲区
dat_ptr++; // 数据保存缓冲区指针地址自增1
}
}
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
offset1 += rx_size; // 更新实际物理地址,即下次读取接收到的数据的起始地址
Write_W5500_SOCK_2Byte(s, Sn_RX_RD, offset1);
Write_W5500_SOCK_1Byte(s, Sn_CR, RECV); // 发送启动接收命令
return rx_size; // 返回接收到数据的长度
}
/*******************************************************************************
* 函数名 : Write_SOCK_Data_Buffer
* 描述 : 将数据写入W5500的数据发送缓冲区
* 输入 : s:端口号,*dat_ptr:数据保存缓冲区指针,size:待写入数据的长度
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
static void Write_SOCK_Data_Buffer(bsp_W5500_Class_t *pW5500_Class, u8 *dat_ptr, u16 size)
{
u16 offset, offset1;
u16 i;
// 如果是UDP模式,可以在此设置目的主机的IP和端口号
if ((Read_W5500_SOCK_1Byte(pW5500_Class->SocketPort, Sn_MR) & 0x0f) != SOCK_UDP) // 如果Socket打开失败
{
Write_W5500_SOCK_4Byte(pW5500_Class->SocketPort, Sn_DIPR, pW5500_Class->ConfigData.UDP_DIPR); // 设置目的主机IP
Write_W5500_SOCK_2Byte(pW5500_Class->SocketPort, Sn_DPORTR, pW5500_Class->ConfigData.UDP_DPORT[0]<<8 | pW5500_Class->ConfigData.UDP_DPORT[1]); // 设置目的主机端口号
}
offset = Read_W5500_SOCK_2Byte(pW5500_Class->SocketPort, Sn_TX_WR);
offset1 = offset;
offset &= (S_TX_SIZE - 1); // 计算实际的物理地址
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(offset); // 写16位地址
SPI1_Send_Byte(VDM | RWB_WRITE | (pW5500_Class->SocketPort * 0x20 + 0x10)); // 写控制字节,N个字节数据长度,写数据,选择端口s的寄存器
if ((offset + size) < S_TX_SIZE) // 如果最大地址未超过W5500发送缓冲区寄存器的最大地址
{
for (i = 0; i < size; i++) // 循环写入size个字节数据
{
SPI1_Send_Byte(*dat_ptr++); // 写入一个字节的数据
}
}
else // 如果最大地址超过W5500发送缓冲区寄存器的最大地址
{
offset = S_TX_SIZE - offset;
for (i = 0; i < offset; i++) // 循环写入前offset个字节数据
{
SPI1_Send_Byte(*dat_ptr++); // 写入一个字节的数据
}
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_RESET); // 置W5500的SCS为低电平
SPI1_Send_Short(0x00); // 写16位地址
SPI1_Send_Byte(VDM | RWB_WRITE | (pW5500_Class->SocketPort * 0x20 + 0x10)); // 写控制字节,N个字节数据长度,写数据,选择端口s的寄存器
for (; i < size; i++) // 循环写入size-offset个字节数据
{
SPI1_Send_Byte(*dat_ptr++); // 写入一个字节的数据
}
}
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS, GPIO_PIN_SET); // 置W5500的SCS为高电平
offset1 += size; // 更新实际物理地址,即下次写待发送数据到发送数据缓冲区的起始地址
Write_W5500_SOCK_2Byte(pW5500_Class->SocketPort, Sn_TX_WR, offset1);
Write_W5500_SOCK_1Byte(pW5500_Class->SocketPort, Sn_CR, SEND); // 发送启动发送命令
}
/*******************************************************************************
* 函数名 : W5500_Hardware_Reset
* 描述 : 硬件复位W5500
* 输入 : 无
* 输出 : 无
* 返回值 : 无
* 说明 : W5500的复位引脚保持低电平至少500us以上,才能重围W5500
*******************************************************************************/
void W5500_Hardware_Reset(void)
{
HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST, GPIO_PIN_RESET); // 复位引脚拉低
HAL_Delay(50);
HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST, GPIO_PIN_SET); // 复位引脚拉高
HAL_Delay(100);
// while((Read_W5500_1Byte(PHYCFGR)&LINK)==0);//等待以太网连接完成
}
/*******************************************************************************
* 函数名 : W5500_Init
* 描述 : 初始化W5500寄存器函数
* 输入 : 无
* 输出 : 无
* 返回值 : 无
* 说明 : 在使用W5500之前,先对W5500初始化
*******************************************************************************/
void W5500_Init(void)
{
u16 i = 0;
Write_W5500_1Byte(MR, RST); // 软件复位W5500,置1有效,复位后自动清0
HAL_Delay(10); // 延时10ms,自己定义该函数
// 设置网关(Gateway)的IP地址,Gateway_IP为4字节u8数组,自己定义
// 使用网关可以使通信突破子网的局限,通过网关可以访问到其它子网或进入Internet
Write_W5500_nByte(GAR, pW5500->Gateway_IP, 4);
// 设置子网掩码(MASK)值,SUB_MASK为4字节u8数组,自己定义
// 子网掩码用于子网运算
Write_W5500_nByte(SUBR, pW5500->Sub_Mask, 4);
// 设置物理地址,PHY_ADDR为6字节u8数组,自己定义,用于唯一标识网络设备的物理地址值
// 该地址值需要到IEEE申请,按照OUI的规定,前3个字节为厂商代码,后三个字节为产品序号
// 如果自己定义物理地址,注意第一个字节必须为偶数
Write_W5500_nByte(SHAR, pW5500->Phy_Addr, 6);
// 设置本机的IP地址,IP_ADDR为4字节u8数组,自己定义
// 注意,网关IP必须与本机IP属于同一个子网,否则本机将无法找到网关
Write_W5500_nByte(SIPR, pW5500->IP_Addr, 4);
// 设置发送缓冲区和接收缓冲区的大小,参考W5500数据手册
for (i = 0; i < 8; i++)
{
Write_W5500_SOCK_1Byte(i, Sn_RXBUF_SIZE, 0x02); // Socket Rx memory size=2k
Write_W5500_SOCK_1Byte(i, Sn_TXBUF_SIZE, 0x02); // Socket Tx mempry size=2k
}
// 设置重试时间,默认为2000(200ms)
// 每一单位数值为100微秒,初始化时值设为2000(0x07D0),等于200毫秒
Write_W5500_2Byte(RTR, 0x07d0);
// 设置重试次数,默认为8次
// 如果重发的次数超过设定值,则产生超时中断(相关的端口中断寄存器中的Sn_IR 超时位(TIMEOUT)置“1”)
Write_W5500_1Byte(RCR, 8);
}
/*******************************************************************************
* 函数名 : Detect_Gateway
* 描述 : 检查网关服务器
* 输入 : 无
* 输出 : 无
* 返回值 : 成功返回TRUE(0xFF),失败返回FALSE(0x00)
* 说明 : 无
*******************************************************************************/
u8 Detect_Gateway(void)
{
u8 ip_adde[4];
ip_adde[0] = pW5500->IP_Addr[0] + 1;
ip_adde[1] = pW5500->IP_Addr[1] + 1;
ip_adde[2] = pW5500->IP_Addr[2] + 1;
ip_adde[3] = pW5500->IP_Addr[3] + 1;
// 检查网关及获取网关的物理地址
Write_W5500_SOCK_4Byte(0, Sn_DIPR, ip_adde); // 向目的地址寄存器写入与本机IP不同的IP值
Write_W5500_SOCK_1Byte(0, Sn_MR, MR_TCP); // 设置socket为TCP模式
Write_W5500_SOCK_1Byte(0, Sn_CR, OPEN); // 打开Socket
HAL_Delay(5); // 延时5ms
if (Read_W5500_SOCK_1Byte(0, Sn_SR) != SOCK_INIT) // 如果socket打开失败
{
Write_W5500_SOCK_1Byte(0, Sn_CR, CLOSE); // 打开不成功,关闭Socket
return FALSE; // 返回FALSE(0x00)
}
Write_W5500_SOCK_1Byte(0, Sn_CR, CONNECT); // 设置Socket为Connect模式
do
{
u16 j = 0;
j = Read_W5500_SOCK_1Byte(0, Sn_IR); // 读取Socket0中断标志寄存器
if (j != 0)
Write_W5500_SOCK_1Byte(0, Sn_IR, j);
HAL_Delay(5); // 延时5ms
if ((j & IR_TIMEOUT) == IR_TIMEOUT)
{
return FALSE;
}
else if (Read_W5500_SOCK_1Byte(0, Sn_DHAR) != 0xff)
{
Write_W5500_SOCK_1Byte(0, Sn_CR, CLOSE); // 关闭Socket
return TRUE;
}
} while (1);
}
/*******************************************************************************
* 函数名 : Socket_Init
* 描述 : 指定Socket(0~7)初始化
* 输入 : s:待初始化的端口
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
static void bsp_W5500_Socket_Init(bsp_W5500_Class_t *pW5500_Class)
{
Write_W5500_SOCK_2Byte(pW5500_Class->SocketPort, Sn_MSSR, 1460); // 最大分片字节数=1460(0x5b4)
Write_W5500_SOCK_2Byte(pW5500_Class->SocketPort, Sn_PORT, pW5500_Class->ConfigData.Port[0]<<8 | pW5500_Class->ConfigData.Port[1]);
// 设置端口0目的(远程)端口号
Write_W5500_SOCK_2Byte(pW5500_Class->SocketPort, Sn_DPORTR, pW5500_Class->ConfigData.DPort[0]<<8 | pW5500_Class->ConfigData.DPort[1]);
// 设置端口0目的(远程)IP地址
Write_W5500_SOCK_4Byte(pW5500_Class->SocketPort, Sn_DIPR, pW5500_Class->ConfigData.DIP);
}
/*******************************************************************************
* 函数名 : Socket_Connect
* 描述 : 设置指定Socket(0~7)为客户端与远程服务器连接
* 输入 : s:待设定的端口
* 输出 : 无
* 返回值 : 成功返回TRUE(0xFF),失败返回FALSE(0x00)
* 说明 : 当本机Socket工作在客户端模式时,引用该程序,与远程服务器建立连接
* 如果启动连接后出现超时中断,则与服务器连接失败,需要重新调用该程序连接
* 该程序每调用一次,就与服务器产生一次连接
*******************************************************************************/
u8 Socket_Connect(SOCKET s)
{
Write_W5500_SOCK_1Byte(s, Sn_MR, MR_TCP); // 设置socket为TCP模式
Write_W5500_SOCK_1Byte(s, Sn_CR, OPEN); // 打开Socket
HAL_Delay(5); // 延时5ms
if (Read_W5500_SOCK_1Byte(s, Sn_SR) != SOCK_INIT) // 如果socket打开失败
{
Write_W5500_SOCK_1Byte(s, Sn_CR, CLOSE); // 打开不成功,关闭Socket
return FALSE; // 返回FALSE(0x00)
}
Write_W5500_SOCK_1Byte(s, Sn_CR, CONNECT); // 设置Socket为Connect模式
return TRUE; // 返回TRUE,设置成功
}
/*******************************************************************************
* 函数名 : Socket_Listen
* 描述 : 设置指定Socket(0~7)作为服务器等待远程主机的连接
* 输入 : s:待设定的端口
* 输出 : 无
* 返回值 : 成功返回TRUE(0xFF),失败返回FALSE(0x00)
* 说明 : 当本机Socket工作在服务器模式时,引用该程序,等等远程主机的连接
* 该程序只调用一次,就使W5500设置为服务器模式
*******************************************************************************/
u8 Socket_Listen(SOCKET s)
{
Write_W5500_SOCK_1Byte(s, Sn_MR, MR_TCP); // 设置socket为TCP模式
Write_W5500_SOCK_1Byte(s, Sn_CR, OPEN); // 打开Socket
HAL_Delay(5); // 延时5ms
if (Read_W5500_SOCK_1Byte(s, Sn_SR) != SOCK_INIT) // 如果socket打开失败
{
Write_W5500_SOCK_1Byte(s, Sn_CR, CLOSE); // 打开不成功,关闭Socket
return FALSE; // 返回FALSE(0x00)
}
Write_W5500_SOCK_1Byte(s, Sn_CR, LISTEN); // 设置Socket为侦听模式
HAL_Delay(5); // 延时5ms
if (Read_W5500_SOCK_1Byte(s, Sn_SR) != SOCK_LISTEN) // 如果socket设置失败
{
Write_W5500_SOCK_1Byte(s, Sn_CR, CLOSE); // 设置不成功,关闭Socket
return FALSE; // 返回FALSE(0x00)
}
return TRUE;
// 至此完成了Socket的打开和设置侦听工作,至于远程客户端是否与它建立连接,则需要等待Socket中断,
// 以判断Socket的连接是否成功。参考W5500数据手册的Socket中断状态
// 在服务器侦听模式不需要设置目的IP和目的端口号
}
/*******************************************************************************
* 函数名 : Socket_UDP
* 描述 : 设置指定Socket(0~7)为UDP模式
* 输入 : s:待设定的端口
* 输出 : 无
* 返回值 : 成功返回TRUE(0xFF),失败返回FALSE(0x00)
* 说明 : 如果Socket工作在UDP模式,引用该程序,在UDP模式下,Socket通信不需要建立连接
* 该程序只调用一次,就使W5500设置为UDP模式
*******************************************************************************/
u8 Socket_UDP(SOCKET s)
{
Write_W5500_SOCK_1Byte(s, Sn_MR, MR_UDP); // 设置Socket为UDP模式*/
Write_W5500_SOCK_1Byte(s, Sn_CR, OPEN); // 打开Socket*/
HAL_Delay(5); // 延时5ms
if (Read_W5500_SOCK_1Byte(s, Sn_SR) != SOCK_UDP) // 如果Socket打开失败
{
Write_W5500_SOCK_1Byte(s, Sn_CR, CLOSE); // 打开不成功,关闭Socket
return FALSE; // 返回FALSE(0x00)
}
else
return TRUE;
// 至此完成了Socket的打开和UDP模式设置,在这种模式下它不需要与远程主机建立连接
// 因为Socket不需要建立连接,所以在发送数据前都可以设置目的主机IP和目的Socket的端口号
// 如果目的主机IP和目的Socket的端口号是固定的,在运行过程中没有改变,那么也可以在这里设置
}
/*******************************************************************************
* 函数名 : W5500_Interrupt_Process
* 描述 : W5500中断处理程序框架
* 输入 : 无
* 输出 : 无
* 返回值 : 无
* 说明 : 无
*******************************************************************************/
static void bsp_W5500_Interrupt_Process(void)
{
u8 i, j;
u8 Int_Flag,Socket_Flag;
IntDispose:
Int_Flag = Read_W5500_1Byte(SIR); // 读取端口中断标志寄存器
HAL_Delay(10);
for(i=0;i<BSP_W5500_PORT_NUM;i++)
{
if(Int_Flag & (0x01 << i))
{
Socket_Flag = Read_W5500_SOCK_1Byte(pW5500->W5500_Class[i].SocketPort, Sn_IR); // 读取Socket0中断标志寄存器
Write_W5500_SOCK_1Byte(pW5500->W5500_Class[i].SocketPort, Sn_IR, Socket_Flag);
if (Socket_Flag & IR_CON) // 在TCP模式下,Socket0成功连接
{
pW5500->W5500_Class[i].Run_State |= BSP_W5500_PORT_RUN_STATE_CONN; // 网络连接状态0x02,端口完成连接,可以正常传输数据
}
if (Socket_Flag & IR_DISCON) // 在TCP模式下Socket断开连接处理
{
Write_W5500_SOCK_1Byte(pW5500->W5500_Class[i].SocketPort, Sn_CR, CLOSE); // 关闭端口,等待重新打开连接
bsp_W5500_Socket_Init(&pW5500->W5500_Class[i]); // 指定Socket(0~7)初始化,初始化端口0
pW5500->W5500_Class[i].Run_State = 0; // 网络连接状态0x00,端口连接失败
}
if (Socket_Flag & IR_SEND_OK) // Socket0数据发送完成,可以再次启动S_tx_process()函数发送数据
{
pW5500->W5500_Class[i].TR_Data_State |= BSP_W5500_PORT_DATA_TRANSMITOK; // 端口发送一个数据包完成
}
if (Socket_Flag & IR_RECV) // Socket接收到数据,可以启动S_rx_process()函数
{
pW5500->W5500_Class[i].TR_Data_State |= BSP_W5500_PORT_DATA_RECEIVE; // 端口接收到一个数据包
}
if (Socket_Flag & IR_TIMEOUT) {
// 1. 清除超时中断标志位 (你的代码已有)
Write_W5500_SOCK_1Byte(pW5500->W5500_Class[i].SocketPort, Sn_IR, Socket_Flag);
// 2. 强制关闭Socket,确保其状态机复位
Write_W5500_SOCK_1Byte(pW5500->W5500_Class[i].SocketPort, Sn_CR, CLOSE);
// 3. (强烈推荐) 增加一小段延时,等待硬件完成关闭操作
HAL_Delay(10);
// 4. 关键修复:重新打开Socket并进入监听模式
// bsp_W5500_Socket_Init 函数用于配置Socket参数
bsp_W5500_Socket_Init(&pW5500->W5500_Class[i]);
// bsp_W5500_Socket_Set 函数会根据配置的模式(TCP Server)调用 Socket_Listen
bsp_W5500_Socket_Set(&pW5500->W5500_Class[i]);
// 5. 清理相关的状态标志位
pW5500->W5500_Class[i].Run_State = 0;
pW5500->W5500_Class[i].TR_Data_State = 0;
}
}
}
if (Read_W5500_1Byte(SIR) != 0)
goto IntDispose;
}
/**
* @brief 设置指定Socket的Keep-Alive间隔
* @param s: Socket端口号 (0~7)
* @param interval_5s: 间隔时间,单位5秒,例如2表示10秒。0表示禁用。
*/
void w5500_set_keepalive(SOCKET s, uint8_t interval_5s)
{
Write_W5500_SOCK_1Byte(s, Sn_KPALVTR, interval_5s);
}
void bsp_W5500_Socket_Set(bsp_W5500_Class_t *pW5500_Class)
{
if (0 == pW5500_Class->Run_State)
{
switch(pW5500_Class->Run_Mode)
{
/*TCP服务器模式*/
case BSP_W5500_PORT_RUN_MODE_TCP_SERVER:
{
if (Socket_Listen(pW5500_Class->SocketPort) == TRUE)
{
pW5500_Class->Run_State = BSP_W5500_PORT_RUN_STATE_INIT;
w5500_set_keepalive(pW5500_Class->SocketPort, 2);
}
else
pW5500_Class->Run_State = 0;
}break;
/*TCP客户端模式*/
case BSP_W5500_PORT_RUN_MODE_TCP_CLIENT:
{
if(Socket_Connect(pW5500_Class->SocketPort)==TRUE)
pW5500_Class->Run_State = BSP_W5500_PORT_RUN_STATE_INIT;
else
pW5500_Class->Run_State = 0;
}break;
/*UDP模式*/
case BSP_W5500_PORT_RUN_MODE_UDP:
{
if(Socket_UDP(pW5500_Class->SocketPort)==TRUE)
pW5500_Class->Run_State = BSP_W5500_PORT_RUN_STATE_INIT | BSP_W5500_PORT_RUN_STATE_CONN;
else
pW5500_Class->Run_State = 0;
}break;
default:break;
}
}
}
static void bsp_W5500_Init()
{
u8 i;
W5500_Hardware_Reset(); /*硬件复位W5500*/
W5500_Init(); /*初始化W5500寄存器函数*/
Detect_Gateway(); /*检查网关服务器*/
for(i=0;i<BSP_W5500_PORT_NUM;i++)
{
bsp_W5500_Socket_Init(&pW5500->W5500_Class[i]);
pW5500->W5500_Class[i].Run_State = 0; /*复位状态*/
//bsp_W5500_Socket_Set(&pW5500->W5500_Class[i]); /*W5500端口初始化配置*/
}
}
static void bsp_W5500_Task(void)
{
u8 i;
for(i=0;i<BSP_W5500_PORT_NUM;i++)
{
bsp_W5500_Socket_Set(&pW5500->W5500_Class[i]); /*W5500端口初始化配置*/
}
bsp_W5500_Interrupt_Process(); // W5500中断处理程序框架
for(i=0;i<BSP_W5500_PORT_NUM;i++)
{
if ((pW5500->W5500_Class[i].TR_Data_State & BSP_W5500_PORT_DATA_RECEIVE) == BSP_W5500_PORT_DATA_RECEIVE) // 如果Socket0接收到数据
{
pW5500->W5500_Class[i].TR_Data_State &= ~BSP_W5500_PORT_DATA_RECEIVE;
u16 Len = Read_SOCK_Data_Buffer(0, pW5500->W5500_Class[i].Rx_Buffer);
// Write_SOCK_Data_Buffer(&pW5500->W5500_Class[i], pW5500->W5500_Class[i].Rx_Buffer, Len);
// printf("RX");
// Debug_UartSend(pW5500->W5500_Class[i].Rx_Buffer, Len);
if(pW5500->W5500_Class[i].Rx_DataAnalysis != NULL)
{
pW5500->W5500_Class[i].Rx_DataAnalysis(&pW5500->W5500_Class[i],pW5500->W5500_Class[i].Rx_Buffer,Len);/*数据解析*/
}
}
}
}
/**
* @brief 获取 W5500 物理链路状态
* @return 1: 链路连接, 0: 链路断开
*/
static u8 w5500_get_link_status(void)
{
u8 phycfgr = Read_W5500_1Byte(PHYCFGR); // 读取 PHYCFGR 寄存器
return (phycfgr & LINK) ? 1 : 0; // 返回 LINK 位
}
static void network_monitor_task(void)
{
u8 current_link = w5500_get_link_status();
if (last_link_status == 0 && current_link == 1) {
/*链路从断开变为连接,执行恢复操作*/
W5500.Init();
}
last_link_status = current_link;
}
+270
View File
@@ -0,0 +1,270 @@
#ifndef _W5500_H_
#define _W5500_H_
#include "main.h"
/***************** Common Register *****************/
#define MR 0x0000
#define RST 0x80
#define WOL 0x20
#define PB 0x10
#define PPP 0x08
#define FARP 0x02
#define GAR 0x0001
#define SUBR 0x0005
#define SHAR 0x0009
#define SIPR 0x000f
#define INTLEVEL 0x0013
#define IR 0x0015
#define CONFLICT 0x80
#define UNREACH 0x40
#define PPPOE 0x20
#define MP 0x10
#define IMR 0x0016
#define IM_IR7 0x80
#define IM_IR6 0x40
#define IM_IR5 0x20
#define IM_IR4 0x10
#define SIR 0x0017
#define S7_INT 0x80
#define S6_INT 0x40
#define S5_INT 0x20
#define S4_INT 0x10
#define S3_INT 0x08
#define S2_INT 0x04
#define S1_INT 0x02
#define S0_INT 0x01
#define SIMR 0x0018
#define S7_IMR 0x80
#define S6_IMR 0x40
#define S5_IMR 0x20
#define S4_IMR 0x10
#define S3_IMR 0x08
#define S2_IMR 0x04
#define S1_IMR 0x02
#define S0_IMR 0x01
#define RTR 0x0019
#define RCR 0x001b
#define PTIMER 0x001c
#define PMAGIC 0x001d
#define PHA 0x001e
#define PSID 0x0024
#define PMRU 0x0026
#define UIPR 0x0028
#define UPORT 0x002c
#define PHYCFGR 0x002e
#define RST_PHY 0x80
#define OPMODE 0x40
#define DPX 0x04
#define SPD 0x02
#define LINK 0x01
#define VERR 0x0039
/********************* Socket Register *******************/
#define Sn_MR 0x0000
#define MULTI_MFEN 0x80
#define BCASTB 0x40
#define ND_MC_MMB 0x20
#define UCASTB_MIP6B 0x10
#define MR_CLOSE 0x00
#define MR_TCP 0x01
#define MR_UDP 0x02
#define MR_MACRAW 0x04
#define Sn_CR 0x0001
#define OPEN 0x01
#define LISTEN 0x02
#define CONNECT 0x04
#define DISCON 0x08
#define CLOSE 0x10
#define SEND 0x20
#define SEND_MAC 0x21
#define SEND_KEEP 0x22
#define RECV 0x40
#define Sn_IR 0x0002
#define IR_SEND_OK 0x10
#define IR_TIMEOUT 0x08
#define IR_RECV 0x04
#define IR_DISCON 0x02
#define IR_CON 0x01
#define Sn_SR 0x0003
#define SOCK_CLOSED 0x00
#define SOCK_INIT 0x13
#define SOCK_LISTEN 0x14
#define SOCK_ESTABLISHED 0x17
#define SOCK_CLOSE_WAIT 0x1c
#define SOCK_UDP 0x22
#define SOCK_MACRAW 0x02
#define SOCK_SYNSEND 0x15
#define SOCK_SYNRECV 0x16
#define SOCK_FIN_WAI 0x18
#define SOCK_CLOSING 0x1a
#define SOCK_TIME_WAIT 0x1b
#define SOCK_LAST_ACK 0x1d
#define Sn_PORT 0x0004
#define Sn_DHAR 0x0006
#define Sn_DIPR 0x000c
#define Sn_DPORTR 0x0010
#define Sn_MSSR 0x0012
#define Sn_TOS 0x0015
#define Sn_TTL 0x0016
#define Sn_RXBUF_SIZE 0x001e
#define Sn_TXBUF_SIZE 0x001f
#define Sn_TX_FSR 0x0020
#define Sn_TX_RD 0x0022
#define Sn_TX_WR 0x0024
#define Sn_RX_RSR 0x0026
#define Sn_RX_RD 0x0028
#define Sn_RX_WR 0x002a
#define Sn_IMR 0x002c
#define IMR_SENDOK 0x10
#define IMR_TIMEOUT 0x08
#define IMR_RECV 0x04
#define IMR_DISCON 0x02
#define IMR_CON 0x01
#define Sn_FRAG 0x002d
#define Sn_KPALVTR 0x002f
/*******************************************************************/
/************************ SPI Control Byte *************************/
/*******************************************************************/
/* Operation mode bits */
#define VDM 0x00
#define FDM1 0x01
#define FDM2 0x02
#define FDM4 0x03
/* Read_Write control bit */
#define RWB_READ 0x00
#define RWB_WRITE 0x04
/* Block select bits */
#define COMMON_R 0x00
/* Socket 0 */
#define S0_REG 0x08
#define S0_TX_BUF 0x10
#define S0_RX_BUF 0x18
/* Socket 1 */
#define S1_REG 0x28
#define S1_TX_BUF 0x30
#define S1_RX_BUF 0x38
/* Socket 2 */
#define S2_REG 0x48
#define S2_TX_BUF 0x50
#define S2_RX_BUF 0x58
/* Socket 3 */
#define S3_REG 0x68
#define S3_TX_BUF 0x70
#define S3_RX_BUF 0x78
/* Socket 4 */
#define S4_REG 0x88
#define S4_TX_BUF 0x90
/* Socket 5 */
#define S5_REG 0xa8
#define S5_TX_BUF 0xb0
#define S5_RX_BUF 0xb8
/* Socket 6 */
#define S6_REG 0xc8
#define S6_TX_BUF 0xd0
#define S6_RX_BUF 0xd8
/* Socket 7 */
#define S7_REG 0xe8
#define S7_TX_BUF 0xf0
#define S7_RX_BUF 0xf8
#define TRUE 0xff
#define FALSE 0x00
#define S_RX_SIZE 2048 /*定义Socket接收缓冲区的大小,可以根据W5500_RMSR的设置修改 */
#define S_TX_SIZE 2048 /*定义Socket发送缓冲区的大小,可以根据W5500_TMSR的设置修改 */
/***************----- W5500 GPIO定义 -----***************/
#define W5500_SCS GPIO_PIN_0 // 定义W5500的CS引脚
#define W5500_SCS_PORT GPIOB
#define W5500_RST GPIO_PIN_4 // 定义W5500的RST引脚
#define W5500_RST_PORT GPIOA
#define W5500_INT GPIO_PIN_1 // 定义W5500的INT引脚
#define W5500_INT_PORT GPIOA
typedef u8 SOCKET; // 自定义端口号数据类型
#define BSP_W5500_PORT_NUM 1
#define BSP_W5500_TX_DATA_LEN 2048
#define BSP_W5500_RX_DATA_LEN 256
typedef struct bsp_W5500_Class_t bsp_W5500_Class_t;
struct bsp_W5500_Class_t
{
SOCKET SocketPort;
struct
{
/***************----- 网络参数变量定义 -----***************/
u8 Port[2]; /*端口0的端口号(5000) */
u8 DIP[4]; /*端口0目的IP地址*/
u8 DPort[2]; /*端口0目的端口号(6000)*/
u8 UDP_DIPR[4]; /*UDP(广播)模式,目的主机IP地址*/
u8 UDP_DPORT[2]; /*UDP(广播)模式,目的主机端口号*/
}ConfigData;
/***************----- 端口的运行模式 -----***************/
u8 Run_Mode;
/***************----- 端口的运行状态 -----***************/
u8 Run_State;
/***************----- 端口收发数据的状态 -----***********/
u8 TR_Data_State;
/***************----- 端口数据缓冲区 -----***************/
u8 Rx_Buffer[BSP_W5500_RX_DATA_LEN]; // 端口接收数据缓冲区
u8 Tx_Buffer[BSP_W5500_TX_DATA_LEN]; // 端口发送数据缓冲区
u8 Interrupt; // W5500中断标志(0:无中断,1:有中断)
void (*Rx_DataAnalysis)(bsp_W5500_Class_t *,u8 *,u16 );
};
typedef struct
{
u8 Gateway_IP[4]; /*网关IP地址*/
u8 Sub_Mask[4]; /*子网掩码*/
u8 Phy_Addr[6]; /*物理地址(MAC)*/
u8 IP_Addr[4]; /*本机IP地址*/
bsp_W5500_Class_t W5500_Class[BSP_W5500_PORT_NUM]; /*端口成员*/
void (*Interrupt_Process)(void); /*处理进程*/
void (*Init)(void); /*初始化*/
void (*Task)(void); /*任务*/
void (*Monitor_task)(void); /*重连检测*/
void (*Socket_Send)(bsp_W5500_Class_t *, u8 *, u16 ); /*端口发送数据*/
}bsp_W5500_t;
extern bsp_W5500_t W5500;
#endif
+25
View File
@@ -0,0 +1,25 @@
#include "bsp_Wdg.h"
#include "iwdg.h"
static void bsp_Wdg_Init(void);
static void bsp_Wdg_Feed(void);
bsp_Wdg_t Wdg =
{
.Init = bsp_Wdg_Init,
.Feed = bsp_Wdg_Feed,
};
bsp_Wdg_t *pWdg = &Wdg;
static void bsp_Wdg_Init(void)
{
__HAL_DBGMCU_FREEZE_IWDG(); //调试模式下,冻结看门狗计数器时钟
}
static void bsp_Wdg_Feed(void)
{
HAL_IWDG_Refresh(&hiwdg);
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef _BSP_WDG_H_
#define _BSP_WDG_H_
#include "main.h"
typedef struct
{
void (*Init)(void);
void (*Feed)(void);
}bsp_Wdg_t;
extern bsp_Wdg_t Wdg;
#endif
+455
View File
@@ -0,0 +1,455 @@
#include "bsp_iap.h"
#include <stdio.h>
#include <string.h>
#include "app.h"
#include "bsp_uart.h"
#include "usr_config.h"
#include "os_timer.h"
/* IAP 状态定义 */
#define BSP_IAP_STATE_NOMORAL (0) /* 正常模式,等待跳转 */
#define BSP_IAP_STATE_UPDATEING (1) /* 正在升级 */
#define BSP_IAP_STATE_SUCCEED (2) /* 升级完成,准备跳转 */
/* IAP 协议接收帧头 */
#define BSP_IAP_PROTO_RX_HEADER (0x5A)
/* IAP 协议接收命令码 */
#define BSP_IAP_PROTO_RX_CMD_GET_SV (0x01) /* 读取版本号 */
#define BSP_IAP_PROTO_RX_CMD_CODE_SIZE (0x02) /* 获取代码大小并擦除扇区 */
#define BSP_IAP_PROTO_RX_CMD_WRITE_DATA (0x03) /* 写入数据 */
#define BSP_IAP_PROTO_RX_CMD_UNKNOW_1 (0x04) /* 未知命令1 */
#define BSP_IAP_PROTO_RX_CMD_UNKNOW_2 (0x05) /* 未知命令2 */
#define BSP_IAP_PROTO_RX_CMD_UNKNOW_3 (0x06) /* 未知命令3 */
/* IAP 协议发送帧头 */
#define BSP_IAP_PROTO_TX_HEADER (0xA5)
/* IAP 协议错误码 */
#define BSP_IAP_PROTO_ERROR_CODE_SUCCEED (0) /* 解析成功 */
#define BSP_IAP_PROTO_ERROR_CODE_CHECK (1) /* 校验错误 */
#define BSP_IAP_PROTO_ERROR_CODE_CMD (2) /* 命令错误 */
#define BSP_IAP_PROTO_ERROR_CODE_LENGTH (3) /* 长度错误 */
/* 发送缓冲区大小 */
#define BSP_IAP_TX_LEN (32)
/* 静态变量 */
static u16 bsp_app_time_start; /* 等待跳转的起始时间(毫秒) */
static u8 bsp_iap_tx_buf[BSP_IAP_TX_LEN]; /* 发送缓冲区 */
/* 函数声明 */
static void app_jump(void);
static void flash_page_erase(u16 page_num);
static void flash_write_u32(u32 write_addr, u32 *p_buffer, u16 num_write);
static void flash_write_u8(u32 write_addr, u8 *p_buffer, u16 num_write);
static void bsp_iap_send(u8 *p_data, u16 len);
static void bsp_iap_rx_task(u8 *p_data, u16 len, void *other_data);
static void bsp_iap_init(void);
static void bsp_iap_task(void);
static u8 sum_check(u8 *p_data, u16 len);
/******************************************
* 结构体: bsp_iap
* 功能: IAP 控制实例
* 描述: 定义 IAP 的具体配置和回调函数
*******************************************/
bsp_iap_t iap =
{
.init = bsp_iap_init,
.task = bsp_iap_task,
};
/* 全局指针,指向 IAP 结构体 */
bsp_iap_t *p_iap = &iap;
bsp_Uart_t *p_rx_uart;
/* 应用程序起始地址 */
#define APP_START_ADDR 0x08002800
/* 扇区大小(字节) */
#define SECTOR_SIZE FLASH_PAGE_SIZE
/******************************************
* 函数: app_jump
* 功能: 跳转到应用程序
* 参数: 无
* 返回: 无
* 描述: 关闭所有外设和中断,设置堆栈指针,跳转到用户程序
*******************************************/
static void app_jump(void)
{
u32 app_addr = APP_START_ADDR;
/* 检查应用程序是否存在(堆栈指针有效) */
if (((*(u32*)app_addr) & 0x2FFE0000) == 0x20000000)
{
typedef void (*iapfun)(void);
iapfun jump_to_app;
/* 关闭所有中断 */
__disable_irq();
/* 设置所有时钟到默认状态,使用HSI时钟 */
HAL_RCC_DeInit();
// LL_USART_Disable(USART1);
// LL_USART_Disable(USART2);
//
// LL_USART_DisableIT_RXNE_RXFNE(USART1);
// LL_USART_DisableIT_RXNE_RXFNE(USART2);
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
/* 关闭所有中断,清除所有中断挂起标志 */
for (u8 i = 0; i < 8; i++)
{
NVIC->ICER[i]=0xFFFFFFFF;
NVIC->ICPR[i]=0xFFFFFFFF;
}
/* 跳转到应用程序 */
jump_to_app = (iapfun)*(u32*)(app_addr + 4); /* 复位向量地址 */
__set_MSP(*(u32*)app_addr); /* 设置主堆栈指针 */
__enable_irq();
jump_to_app(); /* 跳转 */
while(1);
}
}
static void bsp_Flash_FLASH_ErasePage(uint32_t Address)
{
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t PageError = 0;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = Address;
EraseInitStruct.NbPages = 1;
HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
}
/******************************************
* 函数: flash_page_erase
* 功能: 擦除指定数量的 Flash 扇区
* 参数: page_num - 要擦除的扇区数量
* 返回: 无
* 描述: 从 APP_START_ADDR 开始擦除连续 page_num 个扇区
*******************************************/
static void flash_page_erase(u16 page_num)
{
u16 i;
HAL_FLASH_Unlock();//解锁
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR );
for (i = 0; i < page_num; i++)
{
bsp_Flash_FLASH_ErasePage(i * SECTOR_SIZE+APP_START_ADDR);
}
HAL_FLASH_Lock();//上锁
}
#include <string.h> // 使用 memcpy
/**
* @brief 将数据缓冲区写入 Flash(半字编程)
* @param write_addr Flash 目标地址(必须是 8 字节对齐)
* @param code_buffer 源数据缓冲区(可以为任意对齐)
* @param size 要写入的字节数
*/
static void flash_code_write(uint32_t write_addr, uint8_t *code_buffer, uint16_t size)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
// 按半字(2字节)写入
for (uint16_t i = 0; i < size; i += 2)
{
uint16_t data;
if (i + 1 < size)
data = ((uint16_t)code_buffer[i+1] << 8) | code_buffer[i];
else
data = (uint16_t)code_buffer[i]; // 最后一个字节,高8位补0
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, write_addr + i, data);
}
HAL_FLASH_Lock();
}
/******************************************
* 函数: bsp_iap_send
* 功能: 通过 UART1 发送数据
* 参数: p_data - 数据缓冲区指针
* len - 数据长度
* 返回: 无
* 描述: 调用 UART1 的发送接口
*******************************************/
static void bsp_iap_send(u8 *p_data, u16 len)
{
if(p_rx_uart != NULL)
{
p_rx_uart->Send(p_rx_uart, p_data, len);
}
}
/******************************************
* 函数: bsp_iap_init
* 功能: IAP 初始化
* 参数: 无
* 返回: 无
* 描述: 设置初始状态,并注册接收处理函数
*******************************************/
static void bsp_iap_init(void)
{
p_iap->state = BSP_IAP_STATE_NOMORAL;
//COM_Uart1.Rx_DataAnalysis = bsp_iap_rx_task;
COM_Uart2.Rx_DataAnalysis = bsp_iap_rx_task;
}
/******************************************
* 函数: bsp_iap_task
* 功能: IAP 任务(主循环中调用)
* 参数: 无
* 返回: 无
* 描述: 根据当前状态执行相应操作
*******************************************/
static void bsp_iap_task(void)
{
switch (p_iap->state)
{
case BSP_IAP_STATE_NOMORAL: /* 正常模式,2 秒后跳转 APP */
{
if (TIME_TRUE == OsTimer_CheckTimeOut(bsp_app_time_start, osTime_MSecTick, 1000))
{
app_jump();
}
} break;
case BSP_IAP_STATE_UPDATEING: /* 升级中 */
{
/* 预留,可能处理超时等 */
} break;
case BSP_IAP_STATE_SUCCEED: /* 升级完成,立即跳转 */
{
app_jump();
} break;
default:
break;
}
}
/******************************************
* 函数: sum_check
* 功能: 计算校验和(累加和取反)
* 参数: p_data - 数据指针
* len - 数据长度
* 返回: 校验和字节
* 描述: 对数据进行累加求和,然后取反(补码)
*******************************************/
static u8 sum_check(u8 *p_data, u16 len)
{
u16 i;
u8 sum = 0;
for (i = 0; i < len; i++)
{
sum += p_data[i];
}
return (-sum);
}
/******************************************
* 函数: bsp_iap_rx_task
* 功能: IAP 协议接收处理函数
* 参数: p_data - 接收数据缓冲区
* len - 数据长度
* other_data - 附加数据(未使用)
* 返回: 无
* 描述: 解析 IAP 协议帧,执行相应命令并回复
*******************************************/
static void bsp_iap_rx_task(u8 *p_data, u16 len, void *other_data)
{
u16 i, cmd, data_len, tx_len = 0;
u8 check_data, error_code = BSP_IAP_PROTO_ERROR_CODE_SUCCEED;
u8 *p_data_offset;
if(BSP_IAP_STATE_SUCCEED == p_iap->state)/*升级成功后,不再进行检测*/
{
return;
}
/* 帧头检测和长度检查 */
if (p_data[0] != BSP_IAP_PROTO_RX_HEADER || len < 4)
{
return;
}
data_len = p_data[1] << 8 | p_data[2];
if (len - 4 != data_len) /* 长度错误 */
{
error_code = BSP_IAP_PROTO_ERROR_CODE_LENGTH;
}
else
{
check_data = sum_check(p_data, len - 1);
if (check_data != p_data[len - 1]) /* 校验错误 */
{
error_code = BSP_IAP_PROTO_ERROR_CODE_CHECK;
}
}
p_rx_uart = (bsp_Uart_t*)(other_data);
cmd = p_data[3];
p_data_offset = &p_data[4];
/* 根据命令处理 */
switch (cmd)
{
case BSP_IAP_PROTO_RX_CMD_GET_SV: /* 获取软件版本号 */
{
u8 str_len, tx_data_len;
p_iap->state = BSP_IAP_STATE_UPDATEING; /* 进入升级状态 */
str_len = strlen(SwVersion) + 1;
memcpy(&bsp_iap_tx_buf[3], SwVersion, str_len);
tx_data_len = str_len;
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = tx_data_len + 1;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[tx_data_len + 3] = sum_check(bsp_iap_tx_buf, bsp_iap_tx_buf[1] + 2);
tx_len = bsp_iap_tx_buf[1] + 3;
} break;
case BSP_IAP_PROTO_RX_CMD_CODE_SIZE: /* 获取代码大小并擦除扇区 */
{
u8 tx_data_len;
u16 erase_size, erase_page, page_size;
erase_page = p_data_offset[0] << 8 | p_data_offset[1];
page_size = p_data_offset[2] << 8 | p_data_offset[3];
p_iap->page_size = page_size;
/* 计算需要擦除的扇区数 */
erase_page /= ((float)SECTOR_SIZE / (float)page_size);
flash_page_erase(erase_page);
tx_data_len = 0;
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = tx_data_len + 1;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[tx_data_len + 3] = sum_check(bsp_iap_tx_buf, bsp_iap_tx_buf[1] + 2);
tx_len = bsp_iap_tx_buf[1] + 3;
} break;
case BSP_IAP_PROTO_RX_CMD_WRITE_DATA: /* 写入数据 */
{
u8 tx_data_len;
u16 index, code_size, addr_offset, i;
u32 addr;
u8 *p_code_data;
index = p_data_offset[0] << 8 | p_data_offset[1];
code_size = data_len - 3;
addr = index * p_iap->page_size;
p_code_data = &p_data_offset[2];
flash_code_write(APP_START_ADDR+addr, p_code_data, code_size);
/* 构造应答帧,返回当前索引 */
bsp_iap_tx_buf[3] = index >> 8;
bsp_iap_tx_buf[4] = index & 0x00ff;
tx_data_len = 2;
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = tx_data_len + 1;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[tx_data_len + 3] = sum_check(bsp_iap_tx_buf, bsp_iap_tx_buf[1] + 2);
tx_len = bsp_iap_tx_buf[1] + 3;
} break;
case BSP_IAP_PROTO_RX_CMD_UNKNOW_1: /* 未知命令1(升级完成) */
{
u8 tx_data_len;
tx_data_len = 0;
p_iap->state = BSP_IAP_STATE_SUCCEED; /* 升级完成 */
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = tx_data_len + 1;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[tx_data_len + 3] = sum_check(bsp_iap_tx_buf, bsp_iap_tx_buf[1] + 2);
tx_len = bsp_iap_tx_buf[1] + 3;
} break;
case BSP_IAP_PROTO_RX_CMD_UNKNOW_3: /* 未知命令3(回传两个字节) */
{
u8 tx_data_len;
tx_data_len = 2;
bsp_iap_tx_buf[3] = p_data_offset[0];
bsp_iap_tx_buf[4] = p_data_offset[1];
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = tx_data_len + 1;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[tx_data_len + 3] = sum_check(bsp_iap_tx_buf, bsp_iap_tx_buf[1] + 2);
tx_len = bsp_iap_tx_buf[1] + 3;
} break;
case BSP_IAP_PROTO_RX_CMD_UNKNOW_2: /* 未知命令2(简单应答) */
{
u8 tx_data_len;
tx_data_len = 0;
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = tx_data_len + 1;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[tx_data_len + 3] = sum_check(bsp_iap_tx_buf, bsp_iap_tx_buf[1] + 2);
tx_len = bsp_iap_tx_buf[1] + 3;
} break;
default:
{
error_code = BSP_IAP_PROTO_ERROR_CODE_CMD;
} break;
}
/* 错误处理 */
switch (error_code)
{
case BSP_IAP_PROTO_ERROR_CODE_SUCCEED:
{
bsp_iap_send(bsp_iap_tx_buf, tx_len);
} break;
case BSP_IAP_PROTO_ERROR_CODE_CHECK:
case BSP_IAP_PROTO_ERROR_CODE_CMD:
case BSP_IAP_PROTO_ERROR_CODE_LENGTH:
{
bsp_iap_tx_buf[0] = BSP_IAP_PROTO_TX_HEADER;
bsp_iap_tx_buf[1] = 0x01;
bsp_iap_tx_buf[2] = cmd;
bsp_iap_tx_buf[3] = error_code;
bsp_iap_tx_buf[4] = sum_check(bsp_iap_tx_buf, 4);
bsp_iap_send(bsp_iap_tx_buf, 5);
} break;
default:
break;
}
}
+32
View File
@@ -0,0 +1,32 @@
#ifndef _BSP_IAP_H_
#define _BSP_IAP_H_
#include "main.h"
typedef union
{
struct
{
u8 u8_Data[4];
};
uint32_t u64_Data;
}u64_Temp_n;
/******************************************
* 结构体: bsp_iap_t
* 功能: IAP 控制结构体
* 描述: 存储 IAP 状态和函数指针
*******************************************/
typedef struct
{
u8 state; /* IAP 状态 */
u16 page_size; /* 页面大小(字节) */
void (*init)(void); /* 初始化函数指针 */
void (*task)(void); /* 任务函数指针 */
} bsp_iap_t;
/* 声明外部变量 */
extern bsp_iap_t iap;
#endif /* _BSP_IAP_H_ */
+27
View File
@@ -0,0 +1,27 @@
#include "bsp_print.h"
#include "bsp_Uart.h"
#include "stdio.h"
//#include "wk_usart.h"
/*ÖØ¶¨Òåfputcº¯Êý*/
int fputc(int ch, FILE *f)
{
u8 Data = ch;
COM_Uart1.Send(&COM_Uart1,&Data,1);
}
void Debug_UartSend(u8 *pData,u16 Len)
{
//COM_Uart1.Send(&COM_Uart1,pData,Len);
}
void UartSend(u8 ch)
{
COM_Uart1.Send(&COM_Uart1,&ch,1);
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef _BSP_PRINT_H_
#define _BSP_PRINT_H_
#include "main.h"
void Debug_UartSend(u8 *pData,u16 Len);
#endif
+106
View File
@@ -0,0 +1,106 @@
#ifndef _SYS_H_
#define _SYS_H_
#include "stm32f1xx.h"
//定义一些常用的数据类型短关键字
typedef int64_t s64;
typedef int32_t s32;
typedef int16_t s16;
typedef int8_t s8;
typedef const int32_t sc32;
typedef const int16_t sc16;
typedef const int8_t sc8;
typedef __IO int32_t vs32;
typedef __IO int16_t vs16;
typedef __IO int8_t vs8;
typedef __I int32_t vsc32;
typedef __I int16_t vsc16;
typedef __I int8_t vsc8;
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef const uint32_t uc32;
typedef const uint16_t uc16;
typedef const uint8_t uc8;
typedef __IO uint32_t vu32;
typedef __IO uint16_t vu16;
typedef __IO uint8_t vu8;
typedef __I uint32_t vuc32;
typedef __I uint16_t vuc16;
typedef __I uint8_t vuc8;
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
/////////////////////////////////////////////////////////////////
//Ex_NVIC_Config专用定义
#define GPIO_A 0
#define GPIO_B 1
#define GPIO_C 2
#define GPIO_D 3
#define GPIO_E 4
#define GPIO_F 5
#define GPIO_G 6
#define FTIR 1 //下降沿触发
#define RTIR 2 //上升沿触发
#endif