send function is library’s function i used as is .
[code]uint16 send(
SOCKET s, /< the socket index */
const uint8 * buf, /< a pointer to data */
uint16 len /**< the data size to be send */
)
{
uint8 status=0;
uint16 ret=0;
uint16 freesize=0;
#ifdef DEF_IINCHIP_DBG
printf(“send()\r\n”);
#endif
ret = getIINCHIP_TxMAX(s);
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;
}
#ifdef DEF_IINCHIP_DBG
printf(“socket %d freesize(%d) empty or error\r\n”, s, freesize);
#endif
} while (freesize < ret);
// copy data
send_data_processing(s, (uint8 *)buf, ret);
IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
/* +20071122[chungs]:wait to process the command… /
while( IINCHIP_READ(Sn_CR(s)) )
;
/ ------- */
/* +2008.01 bj /
//#ifdef DEF_IINCHIP_INT
// while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
//#else
while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
//#endif
{
/ m2008.01 [bj] : reduce code /
if ( IINCHIP_READ(Sn_SR(s)) == SOCK_CLOSED )
{
#ifdef DEF_IINCHIP_DBG
printf(“SOCK_CLOSED.\r\n”);
#endif
close(s);
return 0;
}
}
/ +2008.01 bj */
#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;
}[/code]
IINCHIP read and write functions. In w5100.c I use like this : return user_IINCHIP_READ(…) and return user_IINCHIP_WRITE(…)
uint32_t byte_merger(uint8_t opcode, uint16_t addr, uint8_t data)
{
uint32_t merged_stream;
merged_stream = ((uint32_t)(opcode))<<24;
merged_stream |= (((uint32_t)addr)<<8);
merged_stream |= data;
return merged_stream;
}
/**
@brief This function writes the value to W5100 registers.
*/
uint8_t user_IINCHIP_WRITE(uint16_t addr,uint8_t data){
uint8_t spi_counter = 0;
uint32_t temp_value;
uint32_t received_temp_val;
uint32_t received_temp_val1;
wiznet_MOSI_data = byte_merger(0xF0, addr, data);
temp_value = wiznet_MOSI_data;
wiznet_MISO_data = 0;
WIZNET_SEN_ENABLE;
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
for(spi_counter = 0;spi_counter<32;spi_counter++){
wiznet_MOSI_data = temp_value;
wiznet_MOSI_data = (wiznet_MOSI_data << spi_counter)&0x80000000;
wiznet_MOSI_data = (wiznet_MOSI_data >>31);
if(wiznet_MOSI_data == 1) GPIO_SetBits(GPIOB,WIZNET_MOSI);
if(wiznet_MOSI_data == 0) GPIO_ResetBits(GPIOB,WIZNET_MOSI);
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
GPIO_SetBits(GPIOB,WIZNET_SCLK);
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
GPIO_ResetBits(GPIOB,WIZNET_SCLK);
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
}
received_temp_val = wiznet_MISO_data;
received_temp_val1 = received_temp_val & 0x000000FF;
received_current_data = received_temp_val1;
WIZNET_SEN_DISABLE;
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
return 1;
}
/**
@brief This function reads the value from W5100 registers.
*/
uint8_t user_IINCHIP_READ(uint16_t addr)
{
uint8_t spi_counter = 0;
uint32_t temp_value;
uint32_t received_temp_val;
uint32_t received_temp_val1;
wiznet_MOSI_data = byte_merger(0x0F, addr, 0x00);
temp_value = wiznet_MOSI_data;
wiznet_MISO_data = 0;
WIZNET_SEN_ENABLE;
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
for(spi_counter = 0;spi_counter<32;spi_counter++){
wiznet_MOSI_data = temp_value;
wiznet_MOSI_data = (wiznet_MOSI_data << spi_counter)&0x80000000;
wiznet_MOSI_data = (wiznet_MOSI_data >>31);
if(wiznet_MOSI_data == 1) GPIO_SetBits(GPIOB,WIZNET_MOSI);
if(wiznet_MOSI_data == 0) GPIO_ResetBits(GPIOB,WIZNET_MOSI);
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
GPIO_SetBits(GPIOB,WIZNET_SCLK);
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
if(GPIO_ReadInputDataBit(GPIOB,WIZNET_MISO ) == 1){
wiznet_MISO_data = (wiznet_MISO_data >> (31-spi_counter))|0x00000001;
wiznet_MISO_data = (wiznet_MISO_data << (31-spi_counter));
}
if(GPIO_ReadInputDataBit(GPIOB,WIZNET_MISO ) == 0){
wiznet_MISO_data = (wiznet_MISO_data >> (31-spi_counter))&0xFFFFFFFE;
wiznet_MISO_data = (wiznet_MISO_data << (31-spi_counter));
}
GPIO_ResetBits(GPIOB,WIZNET_SCLK);
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
}
received_temp_val = wiznet_MISO_data;
received_temp_val1 = received_temp_val & 0x000000FF;
received_current_data = received_temp_val1;
WIZNET_SEN_DISABLE;
Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS(); Delay_1uS();
return received_current_data;
}