|
#include "stm32f0xx_hal.h"
|
#include "w5500_port.h"
|
#include "Globaldef.h"
|
#include "KMachine.h"
|
/**
|
* @brief enter critical section
|
* @param none
|
* @return none
|
*/
|
static void w5500_cris_enter(void)
|
{
|
__disable_irq();
|
}
|
|
/**
|
* @brief exit critical section
|
* @param none
|
* @return none
|
*/
|
static void w5500_cris_exit(void)
|
{
|
__enable_irq();
|
}
|
|
|
|
#define setW5500CLK_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_3)
|
#define setW5500CLK_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_3)
|
#define SetW5500SER_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_5)
|
#define SetW5500SER_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_5)
|
#define GetW5500SER() LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_4)
|
|
/**
|
* @brief select chip
|
* @param none
|
* @return none
|
*/
|
int nIndexCount=0;
|
static void w5500_cs_select(void)
|
{
|
// setW5500CLK_1();
|
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15);
|
nIndexCount=0;
|
}
|
|
/**
|
* @brief deselect chip
|
* @param none
|
* @return none
|
*/
|
static void w5500_cs_deselect(void)
|
{
|
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15);
|
}
|
|
|
#ifdef SOFT_SPI
|
|
static uint8_t w5500_spi_readbyte_sw(void)
|
{
|
uint8_t value=0;
|
unsigned int mask1=0x0080;
|
// volatile uint32_t * p1=&GPIOA->IDR;
|
SetW5500SER_1();
|
for (;mask1;)
|
{
|
setW5500CLK_0();
|
//*p1=LL_GPIO_PIN_13;
|
|
setW5500CLK_1();
|
if (GetW5500SER()) value |= mask1;
|
mask1>>=1;
|
// __nop();
|
//*p2=LL_GPIO_PIN_13;
|
}
|
KMem.SDB[128+nIndexCount] = value;
|
nIndexCount++;
|
if (nIndexCount>40) {nIndexCount=0;}
|
return value;
|
}
|
|
static void w5500_spi_writebyte_sw(uint8_t wb)
|
{
|
unsigned int mask1=0x0080;
|
//volatile uint32_t * p1 = &GPIOB->BRR;
|
//volatile uint32_t * p2 = &GPIOB->BSRR;
|
KMem.SDB[128+nIndexCount] = wb;
|
nIndexCount++;
|
if (nIndexCount>40) {nIndexCount=0;}
|
|
for (;mask1;)
|
{
|
setW5500CLK_0();
|
//*p1=LL_GPIO_PIN_13;
|
if (wb&mask1) {SetW5500SER_1();}
|
else {SetW5500SER_0();}
|
mask1>>=1;
|
setW5500CLK_1();
|
// __nop();
|
//*p2=LL_GPIO_PIN_13;
|
}
|
|
}
|
#else
|
/**
|
* @brief read byte in SPI interface
|
* @param none
|
* @return the value of the byte read
|
*/
|
#define TIMEOUT 50
|
static uint8_t w5500_spi_readbyte(void)
|
{
|
uint8_t value;
|
int timeout_cnt= 0;
|
|
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;}
|
KMem.SDT[88]=timeout_cnt;
|
value = LL_SPI_ReceiveData8( SPI1);
|
LL_SPI_TransmitData8(SPI1,0x5a);
|
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; }
|
KMem.SDT[89]=timeout_cnt;
|
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;}
|
KMem.SDT[89]+=timeout_cnt;
|
value = LL_SPI_ReceiveData8( SPI1);
|
KMem.SDB[128+nIndexCount] = value;
|
|
nIndexCount++;
|
if (nIndexCount>40) {nIndexCount=0;}
|
return value;
|
}
|
|
/**
|
* @brief write byte in SPI interface
|
* @param wb the value to write
|
* @return none
|
*/
|
static void w5500_spi_writebyte(uint8_t wb)
|
{
|
int timeout_cnt= 0;
|
uint8_t value;
|
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; }
|
KMem.SDT[90]=timeout_cnt;
|
LL_SPI_TransmitData8(SPI1,wb);
|
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; }
|
KMem.SDT[91]=timeout_cnt;
|
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;}
|
KMem.SDT[91]+=timeout_cnt;
|
value = LL_SPI_ReceiveData8( SPI1);
|
|
}
|
#endif
|
|
/**
|
* @brief burst read byte in SPI interface
|
* @param pBuf pointer of data buf
|
* @param len number of bytes to read
|
* @return none
|
*/
|
static void w5500_spi_readburst(uint8_t* pBuf, uint16_t len)
|
{
|
if (!pBuf) {
|
return;
|
}
|
uint8_t value;
|
for (int i=0;i<len;i++){
|
LL_SPI_TransmitData8(SPI1,0xFF);
|
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { }
|
while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) { }
|
value = LL_SPI_ReceiveData8( SPI1);
|
pBuf[i]=value;
|
}
|
}
|
|
/**
|
* @brief burst write byte in SPI interface
|
* @param pBuf pointer of data buf
|
* @param len number of bytes to write
|
* @return none
|
*/
|
static void w5500_spi_writeburst(uint8_t* pBuf, uint16_t len)
|
{
|
if (!pBuf) {
|
return;
|
}
|
for (int i=0;i<len;i++)
|
{
|
|
LL_SPI_TransmitData8(SPI1,pBuf[i]);
|
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { }
|
}
|
while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) { }
|
}
|
|
/**
|
* @brief hard reset
|
* @param none
|
* @return none
|
*/
|
static void w5500_hard_reset(void)
|
{
|
HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST_PIN, GPIO_PIN_RESET);
|
HAL_Delay(50);
|
HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST_PIN, GPIO_PIN_SET);
|
HAL_Delay(10);
|
}
|
|
/**
|
* @brief Initializes WIZCHIP with socket buffer size
|
* @param none
|
* @return errcode
|
* @retval 0 success
|
* @retval -1 fail
|
*/
|
static int w5500_chip_init(void)
|
{
|
/* default size is 2KB */
|
|
return wizchip_init(NULL, NULL);
|
}
|
|
/**
|
* @brief set phy config if autonego is disable
|
* @param none
|
* @return none
|
*/
|
static void w5500_phy_init(void)
|
{
|
#ifdef USE_AUTONEGO
|
// no thing to do
|
#else
|
wiz_PhyConf conf;
|
|
conf.by = PHY_CONFBY_SW;
|
conf.mode = PHY_MODE_MANUAL;
|
conf.speed = PHY_SPEED_100;
|
conf.duplex = PHY_DUPLEX_FULL;
|
|
wizphy_setphyconf(&conf);
|
#endif
|
}
|
|
/**
|
* @brief initializes the network infomation
|
* @param none
|
* @return none
|
*/
|
static void w5500_network_info_init(void)
|
{
|
wiz_NetInfo info;
|
|
uint8_t mac[6] = DEFAULT_MAC_ADDR;
|
uint8_t ip[4] = DEFAULT_IP_ADDR;
|
uint8_t sn[4] = DEFAULT_SUB_MASK;
|
uint8_t gw[4] = DEFAULT_GW_ADDR;
|
uint8_t dns[4] = DEFAULT_DNS_ADDR;
|
|
memcpy(info.mac, mac, 6);
|
memcpy(info.ip, ip, 4);
|
memcpy(info.sn, sn, 4);
|
memcpy(info.gw, gw, 4);
|
memcpy(info.dns, dns, 4);
|
|
#ifdef USE_DHCP
|
info.dhcp = NETINFO_DHCP;
|
#else
|
info.dhcp = NETINFO_STATIC;
|
#endif
|
|
wizchip_setnetinfo(&info);
|
}
|
|
/**
|
* @brief read and show the network infomation
|
* @param none
|
* @return none
|
*/
|
void w5500_network_info_show(void)
|
{
|
wiz_NetInfo info;
|
|
wizchip_getnetinfo(&info);
|
|
// printf("w5500 network infomation:\r\n");
|
// printf(" -mac:%d:%d:%d:%d:%d:%d\r\n", info.mac[0], info.mac[1], info.mac[2],
|
// info.mac[3], info.mac[4], info.mac[5]);
|
// printf(" -ip:%d.%d.%d.%d\r\n", info.ip[0], info.ip[1], info.ip[2], info.ip[3]);
|
// printf(" -sn:%d.%d.%d.%d\r\n", info.sn[0], info.sn[1], info.sn[2], info.sn[3]);
|
// printf(" -gw:%d.%d.%d.%d\r\n", info.gw[0], info.gw[1], info.gw[2], info.gw[3]);
|
// printf(" -dns:%d.%d.%d.%d\r\n", info.dns[0], info.dns[1], info.dns[2], info.dns[3]);
|
|
if (info.dhcp == NETINFO_DHCP) {
|
// printf(" -dhcp_mode: dhcp\r\n");
|
} else {
|
// printf(" -dhcp_mode: static\r\n");
|
}
|
|
KMem.SDB[192]=info.mac[0];KMem.SDB[193]=info.mac[1];KMem.SDB[194]=info.mac[2];
|
KMem.SDB[195]=info.mac[3];KMem.SDB[196]=info.mac[4];KMem.SDB[197]=info.mac[5];
|
|
KMem.SDB[200]=info.ip[0];KMem.SDB[201]=info.ip[1];KMem.SDB[202]=info.ip[2]; KMem.SDB[203]=info.ip[3];
|
KMem.SDB[204]=info.sn[0];KMem.SDB[205]=info.sn[1];KMem.SDB[206]=info.sn[2]; KMem.SDB[207]=info.sn[3];
|
|
KMem.SDB[208]=info.gw[0];KMem.SDB[209]=info.gw[1];KMem.SDB[210]=info.gw[2]; KMem.SDB[211]=info.gw[3];
|
KMem.SDB[212]=info.dns[0];KMem.SDB[213]=info.dns[1];KMem.SDB[214]=info.dns[2]; KMem.SDB[215]=info.dns[3];
|
|
}
|
|
/**
|
* @brief w5500 init
|
* @param none
|
* @return errcode
|
* @retval 0 success
|
* @retval -1 chip init fail
|
*/
|
int w5500_init(void)
|
{
|
/* W5500 hard reset */
|
w5500_hard_reset();
|
|
/* Register spi driver function */
|
reg_wizchip_cris_cbfunc(w5500_cris_enter, w5500_cris_exit);
|
reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
|
#ifdef SOFT_SPI
|
reg_wizchip_spi_cbfunc(w5500_spi_readbyte_sw, w5500_spi_writebyte_sw);
|
|
#else
|
reg_wizchip_spi_cbfunc(w5500_spi_readbyte, w5500_spi_writebyte);
|
#endif
|
|
// reg_wizchip_spiburst_cbfunc(w5500_spi_readburst, w5500_spi_writeburst);
|
|
/* socket buffer size init */
|
if (w5500_chip_init() != 0) {
|
return -1;
|
}
|
|
/* phy init */
|
w5500_phy_init();
|
|
/* network infomation init */
|
w5500_network_info_init();
|
|
/* show network infomation */
|
w5500_network_info_show();
|
|
return 0;
|
}
|