ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Вторник
21 января
1491880 Топик полностью
vesago (19.01.2025 10:13, просмотров: 49) ответил IBAH на W5500, кто знаком? Несколько вопросов по библиотеке от производителя
Я в свое время использовал 5100. Думаю те-же яйца. Вот основная функция. Там сокета 3, включая удп. Тоже использовал либу производителя. Ос-шмос не смотрите. Это я эксперементировал с OSA. Там она не особо нужна. 

//::::::::::::::::::::::::::::::::::::::::::::::::::::::

// Процессор

//::::::::::::::::::::::::::::::::::::::::::::::::::::::

void Wiznet_Processor(void)

{

u16_t i = 0;

u8_t tmp;

for(;;)

{

__watchdog_reset();

switch(wiznet.state)

{

case WIZNET_STATE_INIT:

W5100_Reset_Init();

W5100_CS_Init();

W5100_Reset_On();

OS_Ttimer_Delay(0.1*SEC);

W5100_Reset_Off();

OS_Ttimer_Delay(0.1*SEC);

setMR(MR_RST);

OS_Ttimer_Delay(0.1*SEC);

IP_Load();

setGAR(wiznet.gateway); // Set default gateway

setSHAR(wiznet.mac_address); // Set mac address

setSUBR(wiznet.subnet_mask); // Set subnet mask

setSIPR(wiznet.ip_address); // Set ip address

IINCHIP_WRITE(RMSR,NET_MEMALLOC);

IINCHIP_WRITE(TMSR,NET_MEMALLOC);

USART0_Open(Cnfg_Get_Baudrate(config_br));

wiznet.pbr_br = Wiznet_Conv_EEConfig_To_PBR_Baudrate(config_br);

wiznet.state = WIZNET_STATE_WORK;

break;

case WIZNET_STATE_WORK:

if(wiznet.timer == 0)

{

wiznet.sockstat = getSn_SR(0);

wiznet.sockstat_config = getSn_SR(1);

wiznet.sockstat_pbr = getSn_SR(2);

if(wiznet.sockstat != wiznet.sockstat_old)

{

wiznet.sockstat_old = wiznet.sockstat;

if(wiznet.sockstat == SOCK_ESTABLISHED)

{

wiznet.wdt = WIZNET_WDT_TIME;

}

}

if(wiznet.sockstat_config != wiznet.sockstat_config_old)

{

wiznet.sockstat_config_old = wiznet.sockstat_config;

if(wiznet.sockstat_config == SOCK_ESTABLISHED)

{

wiznet.wdt_config = WIZNET_WDT_TIME;

}

}

if(wiznet.sockstat_pbr != wiznet.sockstat_pbr_old)

{

wiznet.sockstat_pbr_old = wiznet.sockstat_pbr;

if(wiznet.sockstat_pbr == SOCK_UDP)

{

wiznet.wdt_pbr = WIZNET_WDT_TIME;

}

}

wiznet.timer = WIZNET_TIME;

}

//=======================================================================================================================

//--------------------------------------------------------

switch(wiznet.sockstat)

{

case SOCK_CLOSED:

if(socket(0, Sn_MR_TCP, wiznet.work_port, 0) != 0)

{

if(getSn_SR(0) == SOCK_INIT)

{

if(listen(0) != 0)

{

if(getSn_SR(0) == SOCK_LISTEN)

{

wiznet.sock_close_flag[0] = 0;

break;

}

}

}

}

close(0);

break;

//--------------------------------------------------------

//Соединение установлено - идет обмен

case SOCK_ESTABLISHED:

if(USART0_DataInReceiveBuffer() != 0)

{

i = 0;

do

{

wiznet.buf[i] = USART0_Receive();

i++;

}while((USART0_DataInReceiveBuffer() != 0) && (i<(WIZ_BUF_SIZE - 1)));//-1 потому, что если следующий байт - 0xFF то придется пихать в буфер 0xFF 0xFF

//USART0_Rx_Buf_Clr();

send(0, wiznet.buf, i);

wiznet.wdt = WIZNET_WDT_TIME;

}

else

{

wiznet.rsize = getSn_RX_RSR(0);

if(wiznet.rsize != 0)

{

wiznet.wdt = WIZNET_WDT_TIME;

USART0_Disable_Recive();

if(wiznet.rsize > WIZ_BUF_SIZE) wiznet.rsize = WIZ_BUF_SIZE;

recv(0, wiznet.buf, wiznet.rsize);

//USART0_Tx_Buf_Clr();

i = 0;

do

{

USART0_Transmit(wiznet.buf[i]);

i++;

}while(i<wiznet.rsize);

//OS_Ttimer_Delay(0.03*SEC);

}

}

if(wiznet.wdt == 0)

{

//wiznet.state = WIZNET_STATE_INIT;

close(0);

}

break;

//--------------------------------------------------------

case SOCK_FIN_WAIT:

case SOCK_CLOSING:

case SOCK_TIME_WAIT:

case SOCK_CLOSE_WAIT:

case SOCK_LAST_ACK:

switch (wiznet.sock_close_flag[0])

{

case 0:

disconnect(0);

wiznet.sock_close_flag[0] = 1;

break;

default:

close(0);

break;

}

break;

}

//=======================================================================================================================

//--------------------------------------------------------

switch(wiznet.sockstat_config)

{

case SOCK_CLOSED:

if(socket(1, Sn_MR_TCP, TCP_PORT_CONFIG, 0) != 0)

{

if(getSn_SR(1) == SOCK_INIT)

{

if(listen(1) != 0)

{

if(getSn_SR(1) == SOCK_LISTEN)

{

wiznet.sock_close_flag[1] = 0;

break;

}

}

}

}

close(1);

break;

case SOCK_ESTABLISHED:

if((wiznet.rsize = getSn_RX_RSR(1)) != 0)

{

wiznet.wdt_config = WIZNET_WDT_TIME;

switch(wiznet.rsize)

{

case 1:

recv(1, wiznet.buf, wiznet.rsize);

if(wiznet.buf[0] == 1)

{

wiznet.buf[0] = 1;

wiznet.buf[1] = config_gateway[0];

wiznet.buf[2] = config_gateway[1];

wiznet.buf[3] = config_gateway[2];

wiznet.buf[4] = config_gateway[3];

wiznet.buf[5] = config_subnet_mask[0];

wiznet.buf[6] = config_subnet_mask[1];

wiznet.buf[7] = config_subnet_mask[2];

wiznet.buf[8] = config_subnet_mask[3];

wiznet.buf[9] = config_mac_address[0];

wiznet.buf[10] = config_mac_address[1];

wiznet.buf[11] = config_mac_address[2];

wiznet.buf[12] = config_mac_address[3];

wiznet.buf[13] = config_mac_address[4];

wiznet.buf[14] = config_mac_address[5];

wiznet.buf[15] = config_ip_address[0];

wiznet.buf[16] = config_ip_address[1];

wiznet.buf[17] = config_ip_address[2];

wiznet.buf[18] = config_ip_address[3];

wiznet.buf[19] = config_port[0];

wiznet.buf[20] = config_port[1];

wiznet.buf[21] = config_br;

wiznet.buf[22] = config_gateway[0];

wiznet.buf[23] = config_gateway[1];

wiznet.buf[24] = config_gateway[2];

wiznet.buf[25] = config_gateway[3];

wiznet.buf[26] = config_subnet_mask[0];

wiznet.buf[27] = config_subnet_mask[1];

wiznet.buf[28] = config_subnet_mask[2];

wiznet.buf[29] = config_subnet_mask[3];

wiznet.buf[30] = config_mac_address[0];

wiznet.buf[31] = config_mac_address[1];

wiznet.buf[32] = config_mac_address[2];

wiznet.buf[33] = config_mac_address[3];

wiznet.buf[34] = config_mac_address[4];

wiznet.buf[35] = config_mac_address[5];

wiznet.buf[36] = config_ip_address[0];

wiznet.buf[37] = config_ip_address[1];

wiznet.buf[38] = config_ip_address[2];

wiznet.buf[39] = config_ip_address[3];

wiznet.buf[40] = config_port[0];

wiznet.buf[41] = config_port[1];

wiznet.buf[42] = config_br;

send(1, wiznet.buf, 43);

}

if(wiznet.buf[0] == 3)

{

wiznet.state = WIZNET_STATE_INIT;

}

//Поддержка соединения

//if(wiznet.buf[0] == 4)

break;

case 43:

recv(1, wiznet.buf, wiznet.rsize);

if(wiznet.buf[0] == 2)

{

if(memcmp(&wiznet.buf[0 + 1], &wiznet.buf[21 + 1], 4) != 0) break;

if(memcmp(&wiznet.buf[4 + 1], &wiznet.buf[25 + 1], 4) != 0) break;

if(memcmp(&wiznet.buf[8 + 1], &wiznet.buf[29 + 1], 6) != 0) break;

if(memcmp(&wiznet.buf[14 + 1], &wiznet.buf[35 + 1], 4) != 0) break;

if(memcmp(&wiznet.buf[18 + 1], &wiznet.buf[39 + 1], 2) != 0) break;

if(memcmp(&wiznet.buf[20 + 1], &wiznet.buf[41 + 1], 1) != 0) break;

config_gateway[0] = wiznet.buf[1];

config_gateway[1] = wiznet.buf[2];

config_gateway[2] = wiznet.buf[3];

config_gateway[3] = wiznet.buf[4];

config_subnet_mask[0] = wiznet.buf[5];

config_subnet_mask[1] = wiznet.buf[6];

config_subnet_mask[2] = wiznet.buf[7];

config_subnet_mask[3] = wiznet.buf[8];

config_mac_address[0] = wiznet.buf[9];

config_mac_address[1] = wiznet.buf[10];

config_mac_address[2] = wiznet.buf[11];

config_mac_address[3] = wiznet.buf[12];

config_mac_address[4] = wiznet.buf[13];

config_mac_address[5] = wiznet.buf[14];

config_ip_address[0] = wiznet.buf[15];

config_ip_address[1] = wiznet.buf[16];

config_ip_address[2] = wiznet.buf[17];

config_ip_address[3] = wiznet.buf[18];

config_port[0] = wiznet.buf[19];

config_port[1] = wiznet.buf[20];

config_br = wiznet.buf[21];

}

break;

default:

if(wiznet.rsize > WIZ_BUF_SIZE) wiznet.rsize = WIZ_BUF_SIZE;

recv(1, wiznet.buf, wiznet.rsize);

break;

}

}

if(wiznet.wdt_config == 0)

{

//wiznet.state = WIZNET_STATE_INIT;

close(1);

}

break;

//--------------------------------------------------------

case SOCK_FIN_WAIT:

case SOCK_CLOSING:

case SOCK_TIME_WAIT:

case SOCK_CLOSE_WAIT:

case SOCK_LAST_ACK:

switch (wiznet.sock_close_flag[1])

{

case 0:

disconnect(1);

wiznet.sock_close_flag[1] = 1;

break;

default:

close(1);

break;

}

break;

}

//=======================================================================================================================

//--------------------------------------------------------

switch(wiznet.sockstat_pbr)

{

case SOCK_CLOSED:

if(socket(2, Sn_MR_UDP, UDP_PORT_CMD, 0) != 0)

{

if(getSn_SR(2) == SOCK_UDP)

{

wiznet.sock_close_flag[2] = 0;

break;

}

}

close(2);

break;

case SOCK_UDP:

if((wiznet.rsize = getSn_RX_RSR(2)) > 8)

{

wiznet.rsize -= 8;

wiznet.wdt_config = WIZNET_WDT_TIME;

switch(wiznet.rsize)

{

case 4:

case 5:

recvfrom(2, wiznet.buf, wiznet.rsize, (u8_t*)&remote_ip_address[0], (u16_t*)&remote_port);

if(((wiznet.buf[0] == 'P') || (wiznet.buf[0] == 'p')) &&

((wiznet.buf[1] == 'B') || (wiznet.buf[1] == 'b')) &&

((wiznet.buf[2] == 'R') || (wiznet.buf[2] == 'r')))

{

switch (wiznet.rsize)

{

case 4:

if((wiznet.buf[3] >= '0') && (wiznet.buf[3] <= '9'))

{

tmp = (wiznet.buf[3] - '0');

if(wiznet.pbr_br != tmp)

{

wiznet.pbr_br = tmp;

USART0_Open(Wiznet_Get_PBR_Baudrate(wiznet.pbr_br));

}

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

}

break;

case 5:

if(((wiznet.buf[3] >= '0') && (wiznet.buf[3] <= '9')) &&

((wiznet.buf[4] >= '0') && (wiznet.buf[4] <= '9')))

{

tmp = ((wiznet.buf[3] - '0') << 4) | (wiznet.buf[4] - '0');

if(wiznet.pbr_br != tmp)

{

wiznet.pbr_br = tmp;

USART0_Open(Wiznet_Get_PBR_Baudrate(wiznet.pbr_br));

}

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

}

break;

default:

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

break;

}

}

else

{

if(((wiznet.buf[0] == 'P') || (wiznet.buf[0] == 'p')) &&

((wiznet.buf[1] == 'P') || (wiznet.buf[1] == 'p')) &&

((wiznet.buf[2] == 'R') || (wiznet.buf[2] == 'r')))

{

switch (wiznet.rsize)

{

case 4:

if((wiznet.buf[3] >= '0') && (wiznet.buf[3] <= '4'))

{

tmp = (wiznet.buf[3] - '0');

if(usart0.parity != tmp)

{

usart0.parity = tmp;

USART0_Open(Wiznet_Get_PBR_Baudrate(wiznet.pbr_br));

}

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

}

break;

default:

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

break;

}

}

else

{

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

}

}

break;

default:

if(wiznet.rsize > WIZ_BUF_SIZE) wiznet.rsize = WIZ_BUF_SIZE;

recvfrom(2, wiznet.buf, wiznet.rsize, (u8_t*)&remote_ip_address[0], (u16_t*)&remote_port);

wiznet.buf[0] = 'A';

sendto(2, wiznet.buf, 1, (u8_t*)&remote_ip_address[0], remote_port);

break;

}

}

if(wiznet.wdt_pbr == 0)

{

close(2);

}

break;

//--------------------------------------------------------

case SOCK_FIN_WAIT:

case SOCK_CLOSING:

case SOCK_TIME_WAIT:

case SOCK_CLOSE_WAIT:

case SOCK_LAST_ACK:

switch (wiznet.sock_close_flag[2])

{

case 0:

disconnect(2);

wiznet.sock_close_flag[2] = 1;

break;

default:

close(2);

break;

}

break;

}

break;

}

OS_Yield();

}

}