增加协议 发送队列 50ms发送数据

“
”
This commit is contained in:
Hjh
2025-01-15 16:02:53 +08:00
commit e48aa3d84b
271 changed files with 93708 additions and 0 deletions

20
BSP/Src/IoControl.c Normal file
View File

@@ -0,0 +1,20 @@
#include "IoControl.h"
#include "Include.h"
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_2);
}

58
BSP/Src/Timer.c Normal file
View File

@@ -0,0 +1,58 @@
#include "Timer.h"
#include "Include.h"
#include "Usart.h"
uint8_t usart_test_dat[5] = {0x11,0x22,0x33,0x44,0x55};
uint8_t usart_test_len = 5;
uint8_t tim_cnt = 0;
uint8_t tim_flag = 0;
void Tim2Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 249;
TIM_TimeBaseInitStruct.TIM_Prescaler = 8399;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 5;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);//¿ªÆô¶¨Ê±50ms
}
void TIM2_IRQHandler(void)
{
if(RESET != TIM_GetITStatus(TIM2, TIM_IT_Update))
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
tim_flag = 1;
tim_cnt++;
if(tim_cnt == 10)
{
GPIO_ToggleBits(GPIOE, GPIO_Pin_2);
}
//TIM_Cmd(TIM2, DISABLE);
}
}

210
BSP/Src/Usart.c Normal file
View File

@@ -0,0 +1,210 @@
#include "Usart.h"
#include "Include.h"
#include "string.h"
#include "user_queue.h"
#include "Timer.h"
uint16_t Usart1_ucRx_length = 0;//串口1接收长度
uint8_t usart1_rx_done = 0;//串口1接收完成标志
uint8_t Usart1_Rx_Buf[USART1_RX_BUFFER_SIZE]={0};//串口1接收数据
device_state_t device[MAX_SLAVE_NUMBER];//8个设备的状态、控制结构体
extern uint16_t usRec_Length; //网口接收数据长度
extern uint8_t ucRec_Buffer[1024*10]; //网口接收数据缓存
uint8_t usart1_tx_done = 0;//串口1发送完成标志
//BLE_USART_TX_e A9UsartTxState_e = A9_USART_TX_IDLE;
void ble_usart_init(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(BLE_USART_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(BLE_USART_CLK, ENABLE);
GPIO_PinAFConfig(BLE_USART_GPIO_PORT, BLE_USART_TX_PinSource, BLE_GPIO_AF_USART);
GPIO_PinAFConfig(BLE_USART_GPIO_PORT, BLE_USART_RX_PinSource, BLE_GPIO_AF_USART);
GPIO_InitStructure.GPIO_Pin = BLE_USART_TX_GPIO_PIN | BLE_USART_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度 50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(BLE_USART_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为 8 位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(BLE_USART, &USART_InitStructure); //初始化串口
NVIC_InitStructure.NVIC_IRQChannel = BLE_NVIC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 6;//抢占优先级 6
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(BLE_USART, USART_IT_RXNE, ENABLE);//开启中断
USART_ITConfig(BLE_USART, USART_IT_IDLE, ENABLE);//开启空闲中断
USART_Cmd(BLE_USART, ENABLE); //使能串口
}
void USART1_IRQHandler(void)
{
uint8_t temp;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
if(Usart1_ucRx_length >= USART1_RX_BUFFER_SIZE)
{
Usart1_ucRx_length = 0;
}
Usart1_Rx_Buf[Usart1_ucRx_length++] = USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_IT_RXNE);
}
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
temp = USART1 ->SR;
temp = USART1 ->DR;
// ble_usart_send(Usart1_Rx_Buf, Usart1_ucRx_length);
// Usart1_ucRx_length = 0;
usart1_rx_done = 1;
}
}
//void ble_usart_send(uint8_t *ucpTx_Data,uint8_t ucTx_length)
//{
// uint8_t i;
// for(i=0;i<ucTx_length;i++)
// {
// USART_SendData(USART1,*(ucpTx_Data+i));
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// }
//
//}
uint8_t ble_usart_send(uint8_t ucTx_length)
{
uint8_t i,temp;
uint8_t ucpTx_Data[ucTx_length];
OutQueue(&queue,&ucpTx_Data[0],ucTx_length);//出队
for(i=0;i<ucTx_length;i++)
{
USART_SendData(USART1,ucpTx_Data[i]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
return 2;
}
void net_received_data_analysis(void)
{
//通过串口发送至52832
// ble_usart_send(ucRec_Buffer, usRec_Length);
if(Analysis_data())//解析
{
if(EnterQueue(&queue,ucRec_Buffer,usRec_Length))//入队
{
}
}
if(tim_flag)//定时50ms之后发送
{
tim_flag = 0;
if(!IsEmptyQueue(&queue))
{
ble_usart_send(usRec_Length); //通过串口发送至52832
}
}
}
void parameters_init(void)
{
uint8_t i, j;
for(i = 0; i < MAX_SLAVE_NUMBER; i++)
{
device[i].channel1.EMG_data_mode = RMS_Data;
device[i].channel1.mode = ChannelModeAcquisition;
device[i].channel1.rate = AcquisitionRate4K;
device[i].channel1.state = TurnOff;
device[i].channel2.EMG_data_mode = RMS_Data;
device[i].channel2.mode = ChannelModeAcquisition;
device[i].channel2.rate = AcquisitionRate4K;
device[i].channel2.state = TurnOff;
device[i].output_current_mA = 0;
device[i].preinstall_state = TurnOff;
device[i].stimulate_state = TurnOff;
device[i].slice_state = SliceConnect;
device[i].slice_detect_state = TurnOff;
device[i].formwave_state = IdleState;
for(j = 0; j < MAC_ADDRESS_LENGTH; j++)
{
device[i].mac_address[j] = 0x00;
}
device[i].connection_state = BLE_Disconnect;
device[i].adapter_state = BatterySupply;
device[i].electric_quantity = 100;
device[i].reset_flag = Reset;
device[i].frequency_Hz = 0;
device[i].width_us = 0;
device[i].climb_time_ms = 0;
device[i].keep_time_ms = 0;
device[i].down_time_ms = 0;
device[i].rest_time_ms = 0;
}
}
uint8_t Analysis_data()
{
uint8_t bcc=0,i=0,bcc1=0,j=0;
while(ucRec_Buffer[i] != 0xAA)
{
i++;
if(i >= usRec_Length)
{
return 0;
}
}
bcc = ucRec_Buffer[ucRec_Buffer[++i]+2];//校验位 此时队首到了帧长的位置
for(j=2;j<ucRec_Buffer[i]+2;j++)
{
bcc1+=ucRec_Buffer[j] ;
}
if(bcc != bcc1)//判断校验
{
return 0;
}
if(ucRec_Buffer[ucRec_Buffer[i]+3]!=0x55)//判断帧尾
{
return 0;
}
return 1;
}

148
BSP/Src/WatchDog.c Normal file
View File

@@ -0,0 +1,148 @@
/******************************************************************
*版权所有 (C)2019, 河南翔宇医疗有限公司南京分公司
*
*文件名称: WatchDog.c
*文件标识:
*内容摘要:
*其它说明:
*当前版本:
*作 者:
*完成日期:
*
*修改记录1:
* 修改日期:
* 版 本 号:
* 修 改 人:
* 修改内容:
******************************************************************/
/******************************************************************
* 头文件 *
******************************************************************/
#include "WatchDog.h"
#include "Include.h"
#include "Delay.h"
#include "stm32f4xx_iwdg.h"
/******************************************************************
* 常量 *
******************************************************************/
/******************************************************************
* 宏定义 *
******************************************************************/
/******************************************************************
* 数据类型 *
******************************************************************/
/******************************************************************
* 全局变量声明 *
******************************************************************/
/******************************************************************
* 全局函数声明 *
******************************************************************/
/******************************************************************
*函数名称: void CloseHardWatchDogGpioInit(void)
*功能描述: 关闭硬件看门狗
*输入参数: void
*输出参数: void
*返回值: void
*其它说明:
*修改日期 版本号 修改人 修改内容
*---------------------------------------------------
* 2019 / 10 / 17 ZhangLing
******************************************************************/
void CloseHardWatchDogGpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(WDG_WDI_GPIO_CLK | WDG_EN_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_Pin = WDG_WDI_GPIO_PIN;
GPIO_Init(WDG_WDI_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = WDG_EN_GPIO_PIN;
GPIO_Init(WDG_EN_GPIO_PORT, &GPIO_InitStruct);
GPIO_SetBits(WDG_EN_GPIO_PORT, WDG_EN_GPIO_PIN);
GPIO_ResetBits(WDG_WDI_GPIO_PORT, WDG_WDI_GPIO_PIN);
}
/******************************************************************
*函数名称: void WatchDogGpioInit(void)
*功能描述: 看门狗gpio初始化
*输入参数: void
*输出参数: void
*返回值: void
*其它说明:
*修改日期 版本号 修改人 修改内容
*---------------------------------------------------
* 2019 / 10 / 17 ZhangLing
******************************************************************/
void WatchDogGpioInit(void)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //取消寄存器写保护
IWDG_SetPrescaler(IWDG_Prescaler_4); //设置 IWDG 分频系数
IWDG_SetReload(1200); //设置 IWDG 装载值
IWDG_ReloadCounter(); //reload
}
/******************************************************************
*函数名称: void WatchDogEnable(void)
*功能描述: 使能看门狗
*输入参数: void
*输出参数: void
*返回值: void
*其它说明:
*修改日期 版本号 修改人 修改内容
*---------------------------------------------------
* 2019 / 10 / 17 ZhangLing
******************************************************************/
void WatchDogEnable(void)
{
IWDG_Enable(); //使能看门狗
}
/******************************************************************
*函数名称: void WatchDogDisable(void)
*功能描述: 失能看门狗
*输入参数: void
*输出参数: void
*返回值: void
*其它说明:
*修改日期 版本号 修改人 修改内容
*---------------------------------------------------
* 2019 / 10 / 17 ZhangLing
******************************************************************/
void WatchDogDisable(void)
{
GPIO_SetBits(WDG_EN_GPIO_PORT, WDG_EN_GPIO_PIN);
}
/******************************************************************
*函数名称: void FeedDog(void)
*功能描述: 喂狗
*输入参数: void
*输出参数: void
*返回值: void
*其它说明:
*修改日期 版本号 修改人 修改内容
*---------------------------------------------------
* 2019 / 10 / 17 ZhangLing
******************************************************************/
void FeedDog(void)
{
IWDG_ReloadCounter(); //reload
}

459
BSP/Src/socket.c Normal file
View File

@@ -0,0 +1,459 @@
/*
*
@file socket.c
@brief setting chip register for socket
last update : 2013. Nov
*
*/
#include "stdio.h"
#include "w5500.h"
#include "socket.h"
static uint16 local_port;
extern uint16 sent_ptr;
/**
@brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5200 done it.
@return 1 for sucess else 0.
*/
uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag)
{
uint8 ret;
if (
((protocol&0x0F) == Sn_MR_TCP) ||
((protocol&0x0F) == Sn_MR_UDP) ||
((protocol&0x0F) == Sn_MR_IPRAW) ||
((protocol&0x0F) == Sn_MR_MACRAW) ||
((protocol&0x0F) == Sn_MR_PPPOE)
)
{
close(s);
IINCHIP_WRITE(Sn_MR(s) ,protocol | flag);
if (port != 0) {
IINCHIP_WRITE( Sn_PORT0(s) ,(uint8)((port & 0xff00) >> 8));
IINCHIP_WRITE( Sn_PORT1(s) ,(uint8)(port & 0x00ff));
} else {
local_port++; // if don't set the source port, set local_port number.
IINCHIP_WRITE(Sn_PORT0(s) ,(uint8)((local_port & 0xff00) >> 8));
IINCHIP_WRITE(Sn_PORT1(s) ,(uint8)(local_port & 0x00ff));
}
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_OPEN); // run sockinit Sn_CR
while( IINCHIP_READ(Sn_CR(s))); // wait to process the command...
/* ------- */
ret = 1;
}
else
{
ret = 0;
}
return ret;
}
/**
@brief This function close the socket and parameter is "s" which represent the socket number
*/
void close(SOCKET s)
{
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_CLOSE);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s)) );
/* ------- */
/* all clear */
IINCHIP_WRITE( Sn_IR(s) , 0xFF);
}
/**
@brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
@return 1 for success else 0.
*/
uint8 listen(SOCKET s)
{
uint8 ret;
if (IINCHIP_READ( Sn_SR(s) ) == SOCK_INIT)
{
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_LISTEN);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s) ) )
;
/* ------- */
ret = 1;
}
else
{
ret = 0;
}
return ret;
}
/**
@brief This function established the connection for the channel in Active (client) mode.
This function waits for the untill the connection is established.
@return 1 for success else 0.
*/
uint8 connect(SOCKET s, uint8 * addr, uint16 port)
{
uint8 ret;
if
(
((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
(port == 0x00)
)
{
ret = 0;
}
else
{
ret = 1;
// set destination IP
IINCHIP_WRITE( Sn_DIPR0(s), addr[0]);
IINCHIP_WRITE( Sn_DIPR1(s), addr[1]);
IINCHIP_WRITE( Sn_DIPR2(s), addr[2]);
IINCHIP_WRITE( Sn_DIPR3(s), addr[3]);
IINCHIP_WRITE( Sn_DPORT0(s), (uint8)((port & 0xff00) >> 8));
IINCHIP_WRITE( Sn_DPORT1(s), (uint8)(port & 0x00ff));
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_CONNECT);
/* wait for completion */
while ( IINCHIP_READ(Sn_CR(s) ) ) ;
while ( IINCHIP_READ(Sn_SR(s)) != SOCK_SYNSENT )
{
if(IINCHIP_READ(Sn_SR(s)) == SOCK_ESTABLISHED)
{
break;
}
if (getSn_IR(s) & Sn_IR_TIMEOUT)
{
IINCHIP_WRITE(Sn_IR(s), (Sn_IR_TIMEOUT)); // clear TIMEOUT Interrupt
ret = 0;
break;
}
}
}
return ret;
}
/**
@brief This function used for disconnect the socket and parameter is "s" which represent the socket number
@return 1 for success else 0.
*/
void disconnect(SOCKET s)
{
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_DISCON);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s) ) )
;
/* ------- */
}
/**
@brief This function used to send the data in TCP mode
@return 1 for success else 0.
*/
uint16 send(SOCKET s, const uint8 * buf, uint16 len)
{
uint8 status=0;
uint16 ret=0;
uint16 freesize=0;
if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
else ret = len;
// if freebuf is available, start.
do
{
freesize = getSn_TX_FSR(s);
status = IINCHIP_READ(Sn_SR(s));
if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT))
{
ret = 0;
break;
}
} while (freesize < ret);
// copy data
send_data_processing(s, (uint8 *)buf, ret);
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_SEND);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s) ) );
while ( (IINCHIP_READ(Sn_IR(s) ) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
{
status = IINCHIP_READ(Sn_SR(s));
if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT) )
{
// uart4.printf("SEND_OK Problem!!\r\n");
close(s);
return 0;
}
}
IINCHIP_WRITE( Sn_IR(s) , Sn_IR_SEND_OK);
#ifdef __DEF_IINCHIP_INT__
putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
#else
IINCHIP_WRITE( Sn_IR(s) , Sn_IR_SEND_OK);
#endif
return ret;
}
/**
@brief This function is an application I/F function which is used to receive the data in TCP mode.
It continues to wait for data as much as the application wants to receive.
@return received data size for success else -1.
*/
uint16 recv(SOCKET s, uint8 * buf, uint16 len)
{
uint16 ret=0;
if ( len > 0 )
{
recv_data_processing(s, buf, len);
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_RECV);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s) ));
/* ------- */
ret = len;
}
return ret;
}
/**
@brief This function is an application I/F function which is used to send the data for other then TCP mode.
Unlike TCP transmission, The peer's destination address and the port is needed.
@return This function return send data size for success else -1.
*/
uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port)
{
uint16 ret=0;
if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
else ret = len;
if( ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || ((port == 0x00)) )//||(ret == 0) )
{
/* added return value */
ret = 0;
}
else
{
IINCHIP_WRITE( Sn_DIPR0(s), addr[0]);
IINCHIP_WRITE( Sn_DIPR1(s), addr[1]);
IINCHIP_WRITE( Sn_DIPR2(s), addr[2]);
IINCHIP_WRITE( Sn_DIPR3(s), addr[3]);
IINCHIP_WRITE( Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8));
IINCHIP_WRITE( Sn_DPORT1(s),(uint8)(port & 0x00ff));
// copy data
send_data_processing(s, (uint8 *)buf, ret);
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_SEND);
/* wait to process the command... */
while( IINCHIP_READ( Sn_CR(s) ) )
;
/* ------- */
while( (IINCHIP_READ( Sn_IR(s) ) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
{
if (IINCHIP_READ( Sn_IR(s) ) & Sn_IR_TIMEOUT)
{
/* clear interrupt */
IINCHIP_WRITE( Sn_IR(s) , (Sn_IR_SEND_OK | Sn_IR_TIMEOUT)); /* clear SEND_OK & TIMEOUT */
return 0;
}
}
IINCHIP_WRITE( Sn_IR(s) , Sn_IR_SEND_OK);
}
return ret;
}
/**
@brief This function is an application I/F function which is used to receive the data in other then
TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well.
@return This function return received data size for success else -1.
*/
uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port)
{
uint8 head[8];
uint16 data_len=0;
uint16 ptr=0;
uint32 addrbsb =0;
if ( len > 0 )
{
ptr = IINCHIP_READ(Sn_RX_RD0(s) );
ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD1(s));
addrbsb = (uint32)(ptr<<8) + (s<<5) + 0x18;
switch (IINCHIP_READ(Sn_MR(s) ) & 0x07)
{
case Sn_MR_UDP :
wiz_read_buf(addrbsb, head, 0x08);
ptr += 8;
// read peer's IP address, port number.
addr[0] = head[0];
addr[1] = head[1];
addr[2] = head[2];
addr[3] = head[3];
*port = head[4];
*port = (*port << 8) + head[5];
data_len = head[6];
data_len = (data_len << 8) + head[7];
addrbsb = (uint32)(ptr<<8) + (s<<5) + 0x18;
wiz_read_buf(addrbsb, buf, data_len);
ptr += data_len;
IINCHIP_WRITE( Sn_RX_RD0(s), (uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_RX_RD1(s), (uint8)(ptr & 0x00ff));
break;
case Sn_MR_IPRAW :
wiz_read_buf(addrbsb, head, 0x06);
ptr += 6;
addr[0] = head[0];
addr[1] = head[1];
addr[2] = head[2];
addr[3] = head[3];
data_len = head[4];
data_len = (data_len << 8) + head[5];
addrbsb = (uint32)(ptr<<8) + (s<<5) + 0x18;
wiz_read_buf(addrbsb, buf, data_len);
ptr += data_len;
IINCHIP_WRITE( Sn_RX_RD0(s), (uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_RX_RD1(s), (uint8)(ptr & 0x00ff));
break;
case Sn_MR_MACRAW :
wiz_read_buf(addrbsb, head, 0x02);
ptr+=2;
data_len = head[0];
data_len = (data_len<<8) + head[1] - 2;
if(data_len > 1514)
{
// uart4.printf("data_len over 1514\r\n");
while(1);
}
addrbsb = (uint32)(ptr<<8) + (s<<5) + 0x18;
wiz_read_buf(addrbsb, buf, data_len);
ptr += data_len;
IINCHIP_WRITE( Sn_RX_RD0(s), (uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_RX_RD1(s), (uint8)(ptr & 0x00ff));
break;
default :
break;
}
IINCHIP_WRITE( Sn_CR(s) ,Sn_CR_RECV);
/* wait to process the command... */
while( IINCHIP_READ( Sn_CR(s)) ) ;
/* ------- */
}
return data_len;
}
#ifdef __MACRAW__
void macraw_open(void)
{
uint8 sock_num;
uint16 dummyPort = 0;
uint8 mFlag = 0;
sock_num = 0;
close(sock_num); // Close the 0-th socket
socket(sock_num, Sn_MR_MACRAW, dummyPort,mFlag); // OPen the 0-th socket with MACRAW mode
}
uint16 macraw_send( const uint8 * buf, uint16 len )
{
uint16 ret=0;
uint8 sock_num;
sock_num =0;
if (len > getIINCHIP_TxMAX(sock_num)) ret = getIINCHIP_TxMAX(sock_num); // check size not to exceed MAX size.
else ret = len;
send_data_processing(sock_num, (uint8 *)buf, len);
//W5500 SEND COMMAND
IINCHIP_WRITE(Sn_CR(sock_num),Sn_CR_SEND);
while( IINCHIP_READ(Sn_CR(sock_num)) );
while ( (IINCHIP_READ(Sn_IR(sock_num)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK );
IINCHIP_WRITE(Sn_IR(sock_num), Sn_IR_SEND_OK);
return ret;
}
uint16 macraw_recv( uint8 * buf, uint16 len )
{
uint8 sock_num;
uint16 data_len=0;
uint16 dummyPort = 0;
uint16 ptr = 0;
uint8 mFlag = 0;
sock_num = 0;
if ( len > 0 )
{
data_len = 0;
ptr = IINCHIP_READ(Sn_RX_RD0(sock_num));
ptr = (uint16)((ptr & 0x00ff) << 8) + IINCHIP_READ( Sn_RX_RD1(sock_num) );
//-- read_data(s, (uint8 *)ptr, data, len); // read data
data_len = IINCHIP_READ_RXBUF(0, ptr);
ptr++;
data_len = ((data_len<<8) + IINCHIP_READ_RXBUF(0, ptr)) - 2;
ptr++;
if(data_len > 1514)
{
uart4.printf("data_len over 1514\r\n");
uart4.printf("\r\nptr: %X, data_len: %X", ptr, data_len);
//while(1);
/** recommand : close and open **/
close(sock_num); // Close the 0-th socket
socket(sock_num, Sn_MR_MACRAW, dummyPort,mFlag); // OPen the 0-th socket with MACRAW mode
return 0;
}
IINCHIP_READ_RXBUF_BURST(sock_num, ptr, data_len, (uint8*)(buf));
ptr += data_len;
IINCHIP_WRITE(Sn_RX_RD0(sock_num),(uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE(Sn_RX_RD1(sock_num),(uint8)(ptr & 0x00ff));
IINCHIP_WRITE(Sn_CR(sock_num), Sn_CR_RECV);
while( IINCHIP_READ(Sn_CR(sock_num)) ) ;
}
return data_len;
}
#endif

150
BSP/Src/spi1.c Normal file
View File

@@ -0,0 +1,150 @@
/*
* FILE : spi1.c
* DESCRIPTION : This file is iCore3 driver demo.
* Author : XiaomaGee@Gmail.com
* Copyright :
*
* History
* -------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* -------------------
*/
//-----------------Include files-------------------------//
#include "spi1.h"
#include "w5500.h"
//---------------- Function Prototype ------------------//
static int initialize_spi(void);
static int write(int number, uint8_t * buf);
static uint8_t send_data(uint8_t data);
//-----------------Variable-----------------------------//
SPI1_T spi1 = {
.initialize = initialize_spi,
.write = write,
.send_data = send_data
};
//-----------------Function-----------------------------//
/*
* Name : initialize_spi1
* Description : ---
* Author : XiaomaGee.
*
* History
* -------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* -------------------
*/
static int initialize_spi(void) /*spi1*/
{
GPIO_InitTypeDef GPIO_uInitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_uInitStructure);
SPI1_CS_ON;
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;
GPIO_Init(GPIOA,&GPIO_uInitStructure);
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;;
GPIO_Init(GPIOA,&GPIO_uInitStructure);
SPI_Cmd(SPI1,DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex ;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = 0x00;
SPI_InitStructure.SPI_CPHA = 0x00;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
return 0;
}
/*
* Name : initialize_spi1
* Description : ---
* Author : XiaomaGee.
*
* History
* -------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* -------------------
*/
static uint8_t send_data(uint8_t data)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == 0);
SPI_I2S_SendData(SPI1,data);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == 0);
return SPI_I2S_ReceiveData(SPI1);
}
/*
* Name : write
* Description : ---
* Author : XiaomaGee.
*
* History
* -------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* -------------------
*/
static int write(int number, uint8_t * buf)
{
int i;
SPI1_CS_OFF;
for (i = 0; i < number; i++){
buf[i] = send_data(buf[i]);
}
SPI1_CS_ON;
return 0;
}

912
BSP/Src/w5500.c Normal file
View File

@@ -0,0 +1,912 @@
/*
* FILE : w5500.c
* DESCRIPTION : This file is iCore3 files.
* Author : ysloveivy
* Copyright :
*
* History
* --------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* --------------------
*/
//--------------------------- Include ---------------------------//
#include "spi1.h"
#include "stm32f4xx_gpio.h"
#include "w5500.h"
#include "socket.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
//--------------------- Function Prototype ----------------------//
static void delay(int ms);
static int initialize(void);
//--------------------------- Variable --------------------------//
W5500_T w5500={
.initialize=initialize,
.mac = {0x00,0x98,0xdc,0x42,0x61,0x11},
.ip = {192,168,1,10},
.sub = {255,255,255,0},
.gw = {192,168,1,1}
};
unsigned char txsize[MAX_SOCK_NUM] = {2,2,2,2,2,2,2,2};
unsigned char rxsize[MAX_SOCK_NUM] = {2,2,2,2,2,2,2,2};
static unsigned char I_STATUS[MAX_SOCK_NUM];
static unsigned short int SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel */
static unsigned short int RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel */
//--------------------------- Function --------------------------//
/*
* Name : getISR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned char getISR(unsigned char s)
{
return I_STATUS[s];
}
/*
* Name : putISR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void putISR(unsigned char s, unsigned char val)
{
I_STATUS[s] = val;
}
/*
* Name : getIINCHIP_RxMAX
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned short int getIINCHIP_RxMAX(unsigned char s)
{
return RSIZE[s];
}
/*
* Name : getIINCHIP_TxMAX
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned short int getIINCHIP_TxMAX(unsigned char s)
{
return SSIZE[s];
}
/*
* Name : IINCHIP_CSoff
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void IINCHIP_CSoff(void)
{
SPI1_CS_OFF;
}
/*
* Name : IINCHIP_CSon
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void IINCHIP_CSon(void)
{
SPI1_CS_ON;
}
/*
* Name : IINCHIP_SpiSendData
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned char IINCHIP_SpiSendData(unsigned char dat)
{
return(spi1.send_data(dat));
}
/*
* Name : IINCHIP_WRITE
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void IINCHIP_WRITE( unsigned long int addrbsb, unsigned char data)
{
SPI1_CS_OFF;
spi1.send_data( (addrbsb & 0x00FF0000)>>16); // Address byte 1
spi1.send_data( (addrbsb & 0x0000FF00)>> 8); // Address byte 2
spi1.send_data( (addrbsb & 0x000000F8) + 4); // Data write command and Write data length 1
spi1.send_data(data); // Data write (write 1byte data)
SPI1_CS_ON;
}
/*
* Name : IINCHIP_READ
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned char IINCHIP_READ(unsigned long int addrbsb)
{
unsigned char data = 0;
SPI1_CS_OFF;
spi1.send_data( (addrbsb & 0x00FF0000)>>16); // Address byte 1
spi1.send_data( (addrbsb & 0x0000FF00)>> 8); // Address byte 2
spi1.send_data( (addrbsb & 0x000000F8)) ; // Data read command and Read data length 1
data = spi1.send_data(0x00); // Data read (read 1byte data)
SPI1_CS_ON;
return data;
}
/*
* Name : wiz_write_buf
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned short int wiz_write_buf(unsigned long int addrbsb,unsigned char* buf,unsigned short int len)
{
unsigned short int idx = 0;
SPI1_CS_OFF;
spi1.send_data( (addrbsb & 0x00FF0000)>>16); // Address byte 1
spi1.send_data( (addrbsb & 0x0000FF00)>> 8); // Address byte 2
spi1.send_data( (addrbsb & 0x000000F8) + 4); // Data write command and Write data length 1
for(idx = 0; idx < len; idx++) // Write data in loop
{
spi1.send_data(buf[idx]);
}
SPI1_CS_ON;
return len;
}
/*
* Name : wiz_read_buf
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned short int wiz_read_buf(unsigned long int addrbsb, unsigned char* buf,unsigned short int len)
{
unsigned short int idx = 0;
SPI1_CS_OFF;
spi1.send_data( (addrbsb & 0x00FF0000)>>16); // Address byte 1
spi1.send_data( (addrbsb & 0x0000FF00)>> 8); // Address byte 2
spi1.send_data( (addrbsb & 0x000000F8)); // Data write command and Write data length 1
for(idx = 0; idx < len; idx++) // Write data in loop
{
buf[idx] = spi1.send_data(0x00);
}
SPI1_CS_ON;
return len;
}
/*
* Name : iinchip_init
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void iinchip_init(void)
{
setMR( MR_RST );
}
/*
* Name : sysinit
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void sysinit( uint8_t * tx_size, uint8_t * rx_size )
{
short int i;
short int ssum,rsum;
ssum = 0;
rsum = 0;
for (i = 0 ; i < MAX_SOCK_NUM; i++) // Set the size, masking and base address of Tx & Rx memory by each channel
{
IINCHIP_WRITE( (Sn_TXMEM_SIZE(i)), tx_size[i]);
IINCHIP_WRITE( (Sn_RXMEM_SIZE(i)), rx_size[i]);
SSIZE[i] = (int16)(0);
RSIZE[i] = (int16)(0);
if (ssum <= 16384)
{
switch( tx_size[i] )
{
case 1:
SSIZE[i] = (int16)(1024);
break;
case 2:
SSIZE[i] = (int16)(2048);
break;
case 4:
SSIZE[i] = (int16)(4096);
break;
case 8:
SSIZE[i] = (int16)(8192);
break;
case 16:
SSIZE[i] = (int16)(16384);
break;
default :
RSIZE[i] = (int16)(2048);
break;
}
}
if (rsum <= 16384)
{
switch( rx_size[i] )
{
case 1:
RSIZE[i] = (int16)(1024);
break;
case 2:
RSIZE[i] = (int16)(2048);
break;
case 4:
RSIZE[i] = (int16)(4096);
break;
case 8:
RSIZE[i] = (int16)(8192);
break;
case 16:
RSIZE[i] = (int16)(16384);
break;
default :
RSIZE[i] = (int16)(2048);
break;
}
}
ssum += SSIZE[i];
rsum += RSIZE[i];
}
}
/*
* Name : setGAR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setGAR(unsigned char * addr)
{
wiz_write_buf(GAR0, addr, 4);
}
/*
* Name : getGWIP
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void getGWIP(uint8_t * addr)
{
wiz_read_buf(GAR0, addr, 4);
}
/*
* Name : setSUBR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setSUBR(unsigned char * addr)
{
wiz_write_buf(SUBR0, addr, 4);
}
/*
* Name : setSHAR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setSHAR(unsigned char * addr)
{
wiz_write_buf(SHAR0, addr, 6);
}
/*
* Name : setSIPR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setSIPR(unsigned char * addr)
{
wiz_write_buf(SIPR0, addr, 4);
}
/*
* Name : getGAR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void getGAR(unsigned char * addr)
{
wiz_read_buf(GAR0, addr, 4);
}
/*
* Name : getSUBR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void getSUBR(unsigned char * addr)
{
wiz_read_buf(SUBR0, addr, 4);
}
/*
* Name : getSHAR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void getSHAR(unsigned char * addr)
{
wiz_read_buf(SHAR0, addr, 6);
}
/*
* Name : getSIPR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void getSIPR(unsigned char * addr)
{
wiz_read_buf(SIPR0, addr, 4);
}
/*
* Name : setMR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setMR(unsigned char val)
{
IINCHIP_WRITE(MR,val);
}
/*
* Name : getIR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
uint8_t getIR( void )
{
return IINCHIP_READ(IR);
}
/*
* Name : setRTR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setRTR(unsigned short int timeout)
{
IINCHIP_WRITE(RTR0,(unsigned char)((timeout & 0xff00) >> 8));
IINCHIP_WRITE(RTR1,(unsigned char)(timeout & 0x00ff));
}
/*
* Name : setRCR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setRCR(unsigned char retry)
{
IINCHIP_WRITE(WIZ_RCR,retry);
}
/*
* Name : clearIR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void clearIR(unsigned char mask)
{
IINCHIP_WRITE(IR, ~mask | getIR() ); // must be setted 0x10.
}
/*
* Name : setSn_MSS
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setSn_MSS(SOCKET s, unsigned short int Sn_MSSR)
{
IINCHIP_WRITE( Sn_MSSR0(s), (unsigned char)((Sn_MSSR & 0xff00) >> 8));
IINCHIP_WRITE( Sn_MSSR1(s), (unsigned char)(Sn_MSSR & 0x00ff));
}
/*
* Name : setSn_TTL
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setSn_TTL(SOCKET s, unsigned char ttl)
{
IINCHIP_WRITE( Sn_TTL(s) , ttl);
}
/*
* Name : getSn_IR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
uint8_t getSn_IR(SOCKET s)
{
return IINCHIP_READ(Sn_IR(s));
}
/*
* Name : getSn_SR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
uint8_t getSn_SR(SOCKET s)
{
return IINCHIP_READ(Sn_SR(s));
}
/*
* Name : getSn_TX_FSR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned short int getSn_TX_FSR(SOCKET s)
{
unsigned short int val=0,val1=0;
do
{
val1 = IINCHIP_READ(Sn_TX_FSR0(s));
val1 = (val1 << 8) + IINCHIP_READ(Sn_TX_FSR1(s));
if (val1 != 0)
{
val = IINCHIP_READ(Sn_TX_FSR0(s));
val = (val << 8) + IINCHIP_READ(Sn_TX_FSR1(s));
}
} while (val != val1);
return val;
}
/*
* Name : getSn_RX_RSR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
unsigned short int getSn_RX_RSR(SOCKET s)
{
unsigned short int val=0,val1=0;
do
{
val1 = IINCHIP_READ(Sn_RX_RSR0(s));
val1 = (val1 << 8) + IINCHIP_READ(Sn_RX_RSR1(s));
if(val1 != 0)
{
val = IINCHIP_READ(Sn_RX_RSR0(s));
val = (val << 8) + IINCHIP_READ(Sn_RX_RSR1(s));
}
} while (val != val1);
return val;
}
/*
* Name : send_data_processing
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void send_data_processing(SOCKET s, unsigned char *data, unsigned short int len)
{
unsigned short int ptr = 0;
unsigned long int addrbsb = 0;
ptr = IINCHIP_READ( Sn_TX_WR0(s) );
ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_TX_WR1(s));
addrbsb = ((uint32)ptr<<8) + (s<<5) + 0x10;
wiz_write_buf(addrbsb, data, len);
ptr += len;
IINCHIP_WRITE( Sn_TX_WR0(s) ,(uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_TX_WR1(s),(uint8)(ptr & 0x00ff));
}
/*
* Name : recv_data_processing
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void recv_data_processing(SOCKET s, unsigned char *data, unsigned short int len)
{
unsigned short int ptr = 0;
unsigned long int addrbsb = 0;
ptr = IINCHIP_READ( Sn_RX_RD0(s) );
ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ( Sn_RX_RD1(s) );
addrbsb = ((uint32)ptr<<8) + (s<<5) + 0x18;
wiz_read_buf(addrbsb, data, len);
ptr += len;
IINCHIP_WRITE( Sn_RX_RD0(s), (uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_RX_RD1(s), (uint8)(ptr & 0x00ff));
}
/*
* Name : setSn_IR
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
void setSn_IR(unsigned char s, unsigned char val)
{
IINCHIP_WRITE(Sn_IR(s), val);
}
/*
* Name : initialize
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
static int initialize(void) //初始化设置
{
delay(2000);
while((IINCHIP_READ(PHYCFGR)& LINK)==0); //等待以太网连接完成
setMR( MR_RST );
delay(100);
setSHAR(w5500.mac);
setSUBR(w5500.sub);
setGAR(w5500.gw);
setSIPR(w5500.ip);
sysinit(txsize,rxsize);
setRTR(2000); /*设置溢出时间值*/
setRCR(8); /*设置最大重新发送次数*/
IINCHIP_WRITE( IMR,IM_IR7 | IM_IR6);
IINCHIP_WRITE( SIMR, S0_IMR);
IINCHIP_WRITE( Sn_IMR(0), IMR_SENDOK | IMR_TIMEOUT | IMR_RECV | IMR_DISCON | IMR_CON);
return 0;
}
/*
* Name : delay
* Description : ---
* Author : ysloveivy.
*
* History
* ----------------------
* Rev : 0.00
* Date : 12/20/2015
*
* create.
* ----------------------
*/
static void delay(int ct) //ms
{
int i, j;
for (i = 0; i < ct; i++)
{
for (j = 0; j < 10000; j++) ;
}
}
void W5500_Run(void)
{
int receive_length;
unsigned char receive_buffer[1024];
unsigned char remote_ip[4] = {192,168,0,2};
unsigned short int local_port;
//处理TCP client信息
switch(getSn_SR(0)) /*获取socket0的状态*/
{
case SOCK_INIT: /*socket初始化完成*/
connect(0, remote_ip ,60001); /*在TCP模式下向服务器发送连接请求*/
break;
case SOCK_ESTABLISHED: /*socket连接建立*/
if(getSn_IR(0) & Sn_IR_CON)
{
setSn_IR(0, Sn_IR_CON); /*Sn_IR的第0位置1*/
}
receive_length = getSn_RX_RSR(0); /*len为已接收数据的大小*/
if(receive_length > 0)
{
memset(receive_buffer,0,sizeof(receive_buffer));
recv(0,receive_buffer,receive_length); /*W5500接收来自Sever的数据*/
// send(0,receive_buffer,receive_length);
}
break;
case SOCK_CLOSE_WAIT: /*socket等待关闭状态*/
disconnect(0);
break;
case SOCK_CLOSED: /*socket关闭*/
local_port = rand() % 10000 + 50000;
socket(0,Sn_MR_TCP,local_port,Sn_MR_ND); /*打开socket0的一个端口*/
break;
}
if((IINCHIP_READ(PHYCFGR)& LINK)==0)
{
disconnect(0);
local_port = rand() % 10000 + 50000;
socket(0,Sn_MR_TCP,local_port,Sn_MR_ND); /*打开socket0的一个端口*/
connect(0, remote_ip ,60001); /*在TCP模式下向服务器发送连接请求*/
}
}