#include #include #include #include #include #include #include #include #include #include #include #include #include #define LAN_RST IOPORT_CREATE_PIN(PORTF, 3) #define LAN_RDY IOPORT_CREATE_PIN(PORTG, 5) #define LAN_INT IOPORT_CREATE_PIN(PORTE, 7) #define SPI_SS IOPORT_CREATE_PIN(PORTB, 0) #define SPI_SCK IOPORT_CREATE_PIN(PORTB, 1) #define SPI_MOSI IOPORT_CREATE_PIN(PORTB, 2) #define SPI_MISO IOPORT_CREATE_PIN(PORTB, 3) #define SPI_WIZNET_CS IOPORT_CREATE_PIN(PORTG, 2) #define DATA_BUF_SIZE 2048 uint8_t txsize[8] = {2,2,2,2,2,2,2,2}; uint8_t rxsize[8] = {2,2,2,2,2,2,2,2}; uint8_t socket_buffer[DATA_BUF_SIZE]; void wizchip_select(void) { gpio_set_pin_low( SPI_WIZNET_CS ); } void wizchip_deselect(void) { gpio_set_pin_high( SPI_WIZNET_CS ); } void wizchip_write(uint8_t wb) { SPDR = wb; while (!(SPSR & (1 << SPIF))); } uint8_t wizchip_read() { SPDR = 0x00; while (!(SPSR & (1 << SPIF))); return SPDR; } int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size) { int32_t ret = recv(sn,buf,size); if(ret < 0) // <- I can allways see properly received data in buf[] here { close(sn); return ret; } ret = send(sn,buf,ret); if(ret < 0) close(sn); return ret; } int main(void) { board_init(); // INIT PINS ioport_configure_pin( SPI_SS, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT); ioport_configure_pin(SPI_WIZNET_CS,IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT ); ioport_configure_pin(SPI_SCK, IOPORT_INIT_HIGH |IOPORT_DIR_OUTPUT ); ioport_configure_pin(SPI_MOSI, IOPORT_INIT_HIGH |IOPORT_DIR_OUTPUT ); ioport_configure_pin(SPI_MISO, IOPORT_DIR_INPUT ); ioport_set_pin_dir( LAN_RST, IOPORT_DIR_OUTPUT ); ioport_set_pin_dir( LAN_RDY, IOPORT_DIR_INPUT ); // INIT SPI SPCR = (1 << SPE) | (1 << MSTR) | (0 << CPOL) | (0 << CPHA) | (1 << SPR1); // W5500 RESET AND WAIT FOE RDY gpio_set_pin_high(LED0_GPIO); gpio_set_pin_high(LAN_RST); delay_ms(500); gpio_set_pin_low(LAN_RST); delay_ms(500); gpio_set_pin_high(LAN_RST); while ( gpio_pin_is_low( LAN_RDY ) ); // INIT W5500 reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); wizchip_init( txsize, rxsize ); // LISTEN AND LOOPBACK uint8_t tmp; while(1) { socket(3,Sn_MR_TCP,80,SF_TCP_NODELAY); listen(3); do getsockopt(3,SO_STATUS, &tmp); while(tmp != SOCK_CLOSED && tmp != SOCK_ESTABLISHED); loopback_tcps(3, socket_buffer, DATA_BUF_SIZE); close(3); } }