/*
* (c)COPYRIGHT
* ALL RIGHT RESERVED
*
* FileName : w5500.c
* -----------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "w5500/config.h"
#include "w5500/SPI2.h"
#include "w5500/w5500.h"
#include "w5500/socket.h"
#ifdef __DEF_IINCHIP_PPP__
#include "md5.h"
#endif
static uint8 I_STATUS[MAX_SOCK_NUM];
static uint16 SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel */
static uint16 RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel */
uint8 getISR(uint8 s)
{
return I_STATUS[s];
}
void putISR(uint8 s, uint8 val)
{
I_STATUS[s] = val;
}
uint16 getIINCHIP_RxMAX(uint8 s)
{
return RSIZE[s];
}
uint16 getIINCHIP_TxMAX(uint8 s)
{
return SSIZE[s];
}
void IINCHIP_CSoff(void)
{
WIZ_CS(LOW);
}
void IINCHIP_CSon(void)
{
WIZ_CS(HIGH);
}
uint8 IINCHIP_SpiSendData(uint8 dat)
{
return(SPI2_SendByte(dat));
}
void IINCHIP_WRITE( uint32 addrbsb, uint8 data)
{
IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
IINCHIP_CSoff(); // CS=0, SPI start
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);// Address byte 1
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);// Address byte 2
IINCHIP_SpiSendData( (addrbsb & 0x000000F8) + 4); // Data write command and Write data length 1
IINCHIP_SpiSendData(data); // Data write (write 1byte data)
IINCHIP_CSon(); // CS=1, SPI end
IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
}
uint8 IINCHIP_READ(uint32 addrbsb)
{
uint8 data = 0;
IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
IINCHIP_CSoff(); // CS=0, SPI start
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);// Address byte 1
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);// Address byte 2
IINCHIP_SpiSendData( (addrbsb & 0x000000F8)) ;// Data read command and Read data length 1
data = IINCHIP_SpiSendData(0x00); // Data read (read 1byte data)
IINCHIP_CSon(); // CS=1, SPI end
IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
return data;
}
uint16 wiz_write_buf(uint32 addrbsb,uint8* buf,uint16 len)
{
uint16 idx = 0;
if(len == 0) ;//printf("Unexpected2 length 0\r\n");
IINCHIP_ISR_DISABLE();
IINCHIP_CSoff(); // CS=0, SPI start
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);// Address byte 1
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);// Address byte 2
IINCHIP_SpiSendData( (addrbsb & 0x000000F8) + 4); // Data write command and Write data length 1
for(idx = 0; idx < len; idx++) // Write data in loop
{
IINCHIP_SpiSendData(buf[idx]);
}
IINCHIP_CSon(); // CS=1, SPI end
IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
return len;
}
uint16 wiz_read_buf(uint32 addrbsb, uint8* buf,uint16 len)
{
uint16 idx = 0;
if(len == 0)
{
//printf("Unexpected2 length 0\r\n");
}
IINCHIP_ISR_DISABLE();
//SPI MODE I/F
IINCHIP_CSoff(); // CS=0, SPI start
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);// Address byte 1
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);// Address byte 2
IINCHIP_SpiSendData( (addrbsb & 0x000000F8)); // Data write command and Write data length 1
for(idx = 0; idx < len; idx++) // Write data in loop
{
buf[idx] = IINCHIP_SpiSendData(0x00);
}
IINCHIP_CSon(); // CS=1, SPI end
IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
return len;
}
/**
@brief This function is for resetting of the iinchip. Initializes the iinchip to work in whether DIRECT or INDIRECT mode
*/
void iinchip_init(void)
{
setMR( MR_RST );
#ifdef __DEF_IINCHIP_DBG__
printf("MR value is %02x \r\n",IINCHIP_READ_COMMON(MR));
#endif
}
/**
@brief This function set the transmit & receive buffer size as per the channels is used
Note for TMSR and RMSR bits are as follows\n
bit 1-0 : memory size of channel #0 \n
bit 3-2 : memory size of channel #1 \n
bit 5-4 : memory size of channel #2 \n
bit 7-6 : memory size of channel #3 \n
bit 9-8 : memory size of channel #4 \n
bit 11-10 : memory size of channel #5 \n
bit 12-12 : memory size of channel #6 \n
bit 15-14 : memory size of channel #7 \n
Maximum memory size for Tx, Rx in the W5500 is 16K Bytes,\n
In the range of 16KBytes, the memory size could be allocated dynamically by each channel.\n
Be attentive to sum of memory size shouldn't exceed 8Kbytes\n
and to data transmission and receiption from non-allocated channel may cause some problems.\n
If the 16KBytes memory is already assigned to centain channel, \n
other 3 channels couldn't be used, for there's no available memory.\n
If two 4KBytes memory are assigned to two each channels, \n
other 2 channels couldn't be used, for there's no available memory.\n
*/
void sysinit( uint8 * tx_size, uint8 * rx_size )
{
int16 i;
int16 ssum,rsum;
#ifdef __DEF_IINCHIP_DBG__
printf("sysinit()\r\n");
#endif
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