void W5500Initialze(void) { unsigned char temp; unsigned char W5500FifoSize[2][8] = {{2,2,2,2,2,2,2,2,},{2,2,2,2,2,2,2,2}}; W5500DeSelect(); /* spi function register */ reg_wizchip_spi_cbfunc(W5500ReadByte, W5500WriteByte); /* CS function register */ reg_wizchip_cs_cbfunc(W5500Select,W5500DeSelect); if(ctlwizchip(CW_INIT_WIZCHIP,(void*)W5500FifoSize) == -1) { printf("W5500 initialized fail.\r\n"); } do{//check phy status. if(ctlwizchip(CW_GET_PHYLINK,(void*)&temp) == -1){ printf("Unknown PHY link status.\r\n"); } }while(temp == PHY_LINK_OFF); }
/*---------------------------------------------------------------------------*/ bool HALW5500_IsLinkUp(void) { uint8_t state; ctlwizchip(CW_GET_PHYLINK, (void*) &state); //DEBUG("HALW5500_ Link is %u\n", state); return state == PHY_LINK_ON; }
void W5500_Init() { uint8_t memsize[2][8] = { { 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2 } }; Chip_GPIO_SetPinState(LPC_GPIO, 0, 2, true); // SSEL(CS) Chip_GPIO_SetPinState(LPC_GPIO, 0, 22, false); // N_RESET delay_cnt(1000); Chip_GPIO_SetPinState(LPC_GPIO, 0, 22, true); // N_RESET reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); reg_wizchip_spiburst_cbfunc(wizchip_readburst, wizchip_writeburst); /* wizchip initialize*/ if (ctlwizchip(CW_INIT_WIZCHIP, (void*) memsize) == -1) { //printf("WIZCHIP Initialized fail.\r\n"); while (1); } /* For Delay */ delay_cnt(10000); setRCR(3); }
/*JSON{ "type":"staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "description" : "Initialise the WIZnet module and return an Ethernet object", "params" : [ ], "return" : ["JsVar", "An Ethernet Object"], "return_object":"Ethernet" }*/ JsVar *jswrap_wiznet_connect() { JsVar *ethObj = jspNewObject(0, "Ethernet"); // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); // CS Configuration jshSetPinStateIsManual(ETH_CS_PIN, false); jshPinOutput(ETH_CS_PIN, 1); // de-assert CS // Wiznet reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZCHIP Initialized fail.\r\n"); return 0; } /* PHY link status check */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); return 0; } } while (tmp == PHY_LINK_OFF); JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
/*---------------------------------------------------------------------------*/ bool HALW5500_Init(uint8_t *mac) { DEBUG("HALW5500: MAC "); for (uint8_t i = 0; i <= 5; i++) DEBUG("%02X", mac[i]); DEBUG("\n"); memset(&netInfo, 0, sizeof(netInfo)); memcpy(netInfo.mac, mac, 6); HAL_EthReset(); reg_wizchip_cs_cbfunc(HAL_EthCS0, HAL_EthCS1); reg_wizchip_spi_cbfunc(HALW5500_Read, HALW5500_Write); uint8_t memsize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}}; return ctlwizchip(CW_INIT_WIZCHIP, (void*) memsize) >= 0; }
static void s2e_sockestablished_process(uint8_t sock) { uint8_t tmp; struct __network_info *net = (struct __network_info *)get_S2E_Packet_pointer()->network_info; if(reconn_flag) reconn_flag = reconn_time = 0; switch(net->working_mode) { case TCP_MIXED_MODE: case TCP_SERVER_MODE: if(auth_flag) { auth_process(sock); return; } case TCP_CLIENT_MODE: if((inactive_flag == 0) && net->inactivity) inactive_flag = 1; else if(inactive_flag == 2) { inactive_flag = 0; disconnect(sock); } if(keepsend_flag == 0) { keepsend_flag = 1; } else if(keepsend_flag == 2) { keepsend_flag = 0; ctlwizchip(CW_GET_PHYLINK, (void*) &tmp); if(tmp == PHY_LINK_OFF) { //disconnect(sock); //TODO } } ether_to_uart(sock); uart_to_ether(sock); break; case UDP_MODE: close(sock); break; default: break; } }
///////////////////////////////////////////////////////////// // Intialize the network information to be used in WIZCHIP // ///////////////////////////////////////////////////////////// void network_init(void) { uint8_t tmpstr[6]; ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); // Display Network Information ctlwizchip(CW_GET_ID,(void*)tmpstr); printf("\r\n=== %s NET CONF ===\r\n",(char*)tmpstr); printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2], gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]); printf("SIP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]); printf("GAR: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]); printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]); printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]); printf("======================\r\n"); }
// brief Initialize modules static void W5500_Init(void){ // Set Tx and Rx buffer size to 2KB uint8_t buffsize[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; // Reset module PMOD_PORT &= ~_BV(RST_W5500); //Set Rst low _delay_ms(500); //Minimun of 500us, 10ms to be safe PMOD_PORT |= _BV(RST_W5500); //Set rst high _delay_ms(1); // Wizchip initialize wizchip_init(buffsize, buffsize, 0, 0, cs_sel, cs_desel, 0, 0, spi_rb, spi_wb); // Wizchip netconf ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO); ctlwizchip(CW_SET_PHYCONF, (void*) &phyConf); }
static void PHYStatus_Check(void) { uint8_t tmp; static bool LED_status_backup; LED_status_backup = RGBLED_enable; do { ctlwizchip(CW_GET_PHYLINK, (void*) &tmp); // Error indicator: LED Green ON when no PHY link detected if(tmp == PHY_LINK_OFF) { RGBLED_enable = false; Board_LED_Set(2, true); } }while(tmp == PHY_LINK_OFF); RGBLED_enable = LED_status_backup; }
static void Display_Net_Conf() { #ifdef _MAIN_DEBUG_ uint8_t tmpstr[6] = {0,}; #endif ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO); #ifdef _MAIN_DEBUG_ // Display Network Information ctlwizchip(CW_GET_ID,(void*)tmpstr); if(gWIZNETINFO.dhcp == NETINFO_DHCP) printf("\r\n===== %s NET CONF : DHCP =====\r\n",(char*)tmpstr); else printf("\r\n===== %s NET CONF : Static =====\r\n",(char*)tmpstr); printf(" MAC : %02X:%02X:%02X:%02X:%02X:%02X\r\n", gWIZNETINFO.mac[0], gWIZNETINFO.mac[1], gWIZNETINFO.mac[2], gWIZNETINFO.mac[3], gWIZNETINFO.mac[4], gWIZNETINFO.mac[5]); printf(" IP : %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3]); printf(" GW : %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0], gWIZNETINFO.gw[1], gWIZNETINFO.gw[2], gWIZNETINFO.gw[3]); printf(" SN : %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0], gWIZNETINFO.sn[1], gWIZNETINFO.sn[2], gWIZNETINFO.sn[3]); printf("=======================================\r\n"); #endif }
STATIC void wiznet5k_init(void) { // SPI configuration SPI_InitTypeDef *init = &wiznet5k_obj.spi->spi->Init; init->Mode = SPI_MODE_MASTER; init->Direction = SPI_DIRECTION_2LINES; init->DataSize = SPI_DATASIZE_8BIT; init->CLKPolarity = SPI_POLARITY_LOW; // clock is low when idle init->CLKPhase = SPI_PHASE_1EDGE; // data latched on first edge, which is rising edge for low-idle init->NSS = SPI_NSS_SOFT; init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz init->FirstBit = SPI_FIRSTBIT_MSB; init->TIMode = SPI_TIMODE_DISABLED; init->CRCCalculation = SPI_CRCCALCULATION_DISABLED; init->CRCPolynomial = 7; // unused spi_init(wiznet5k_obj.spi, false); mp_hal_pin_output(wiznet5k_obj.cs); mp_hal_pin_output(wiznet5k_obj.rst); // Reset the chip mp_hal_pin_low(wiznet5k_obj.rst); mp_hal_delay_ms(1); // datasheet says 2us mp_hal_pin_high(wiznet5k_obj.rst); mp_hal_delay_ms(150); // datasheet says 150ms // Set physical interface callbacks reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit); reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect); reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write); // Configure 16k buffers for fast MACRAW uint8_t sn_size[16] = {16, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0}; ctlwizchip(CW_INIT_WIZCHIP, sn_size); // Seems we need a small delay after init mp_hal_delay_ms(250); // Hook the Wiznet into lwIP wiznet5k_lwip_init(&wiznet5k_obj); }
/* *System initialize */ uint8_t wizSystemInit(void) { uint8_t W5500SockBufSize[2][8] = {{2,2,2,2,2,2,2,2,},{2,2,2,2,2,2,2,2}}; uint32_t tickStart; /* spi function register */ reg_wizchip_spi_cbfunc(W5500ReadByte, W5500WriteByte); /* CS function register */ reg_wizchip_cs_cbfunc(W5500Select, W5500DeSelect); /* Critical section */ reg_wizchip_cris_cbfunc(spiMutexEnter,spiMutexExit); //W5500Select(); if(ctlwizchip(CW_INIT_WIZCHIP,(void*)W5500SockBufSize) == -1) { #if WIZSYSTEM_DEBUG == 1 printf("W5500 initialized fail.\r\n"); #endif return -1; } /*Set network information by default*/ wizchip_setnetinfo(&NetInfo); tickStart = HAL_GetTick(); do{//check phy status. randomKey++; if((HAL_GetTick() - tickStart) > NETINFO_LINK_TIMEOUT) { #if WIZSYSTEM_DEBUG == 1 printf("Ethernet Link timeout.\r\n"); #endif return -1; } }while(!isLinked()); return 1; }
/** * @brief Main routine for W5500 EVB firmware * @return Function should not exit. */ int main(void) { #if defined (__USE_LPCOPEN) #if !defined(NO_BOARD_LIB) // Read clock settings and update SystemCoreClock variable SystemCoreClockUpdate(); // Set up and initialize all required blocks and // functions related to the board hardware Board_Init(); #endif #endif bool run_user_applications = false; int32_t ret; /* Network Initialization */ SPI_Init(); W5500_Init(); Net_Conf(); /* Enable and setup SysTick Timer at a periodic rate */ SysTick_Config(SystemCoreClock / TICKRATE_HZ1); /* Enable and setup 32-bit Timer 0 */ Timer32_0_Init(TICKRATE_HZ2); /* PHY Status check enable */ PHYStatus_check_enable = true; #ifdef _MAIN_DEBUG_ uint8_t tmpstr[6] = {0,}; ctlwizchip(CW_GET_ID,(void*)tmpstr); printf("\r\n=======================================\r\n"); printf(" WIZnet %s EVB - DHCP client v%d.%.2d\r\n", tmpstr, VER_H, VER_L); printf("=======================================\r\n"); //Display_Net_Conf(); // handled by dhcp_run function #endif /* DHCP client Initialization */ if(gWIZNETINFO.dhcp == NETINFO_DHCP) { DHCP_init(SOCK_DHCP, gDATABUF); // if you want different action instead default ip assign, update, conflict. // if cbfunc == 0, act as default. reg_dhcp_cbfunc(my_ip_assign, my_ip_assign, my_ip_conflict); run_user_applications = false; // flag for running user's code } else { // Static #ifdef _MAIN_DEBUG_ Display_Net_Conf(); #endif run_user_applications = true; // flag for running user's code } /* Main loop ***************************************/ while(1) { /* PHY Status checker: Check every 'SEC_PHYSTATUS_CHECK' seconds */ if(PHYStatus_check_flag) { PHYStatus_check_flag = false; PHYStatus_Check(); } /* DHCP */ /* DHCP IP allocation and check the DHCP lease time (for IP renewal) */ if(gWIZNETINFO.dhcp == NETINFO_DHCP) { switch(DHCP_run()) { case DHCP_IP_ASSIGN: case DHCP_IP_CHANGED: /* If this block empty, act with default_ip_assign & default_ip_update */ // // This example calls my_ip_assign in the two case. // // Add to ... // break; case DHCP_IP_LEASED: // // TODO: insert user's code here run_user_applications = true; // break; case DHCP_FAILED: /* ===== Example pseudo code ===== */ // The below code can be replaced your code or omitted. // if omitted, retry to process DHCP my_dhcp_retry++; if(my_dhcp_retry > MY_MAX_DHCP_RETRY) { gWIZNETINFO.dhcp = NETINFO_STATIC; DHCP_stop(); // if restart, recall DHCP_init() #ifdef _MAIN_DEBUG_ printf(">> DHCP %d Failed\r\n", my_dhcp_retry); Net_Conf(); Display_Net_Conf(); // print out static netinfo to serial #endif my_dhcp_retry = 0; } break; default: break; } } // TODO: insert user's code here if(run_user_applications) { // Loopback test : TCP Server if ((ret = loopback_tcps(SOCK_TCPS, gDATABUF, PORT_TCPS)) < 0) // TCP server loopback test { #ifdef _MAIN_DEBUG_ printf("SOCKET ERROR : %ld\r\n", ret); #endif } ; } // End of user's code } // End of Main loop return 0; }
/* ********************************************************************************************************* * CHECK DHCP STATE * * Description: This function checks the state of DHCP. * Arguments : None. * Returns : None. * Note : ********************************************************************************************************* */ void check_DHCP_state(uint8_t s, wiz_NetInfo *pWIZNETINFO) { uint16_t len, i; uint8_t type, flag; uint8_t d_addr[4]; type = 0; if ((len = getSn_RX_RSR(s)) > 0) { type = parseDHCPMSG(s, len, pWIZNETINFO); } switch ( dhcp_state ) { case STATE_DHCP_DISCOVER : if (type == DHCP_OFFER) { jsiConsolePrintf("> Receive DHCP_OFFER\r\n"); for (i = 0; i < 4; i++) d_addr[i] = 0xff; send_DHCP_REQUEST(s, Cip, d_addr, pWIZNETINFO); dhcp_state = STATE_DHCP_REQUEST; } else check_Timeout(s, pWIZNETINFO); break; case STATE_DHCP_REQUEST : if (type == DHCP_ACK) { jsiConsolePrintf("> Receive DHCP_ACK\r\n"); if (check_leasedIP(s, pWIZNETINFO)) { //iinchip_init() - WIZnet chip reset & Delay (10ms); //Set_network(); ctlwizchip(CW_RESET_WIZCHIP, 0); ctlnetwork(CN_SET_NETINFO, (void*)&(*pWIZNETINFO)); /* for (i = 0; i < 12; i++) { //EEP_Write(EEP_LIP+i, *(uint8_t *)((*pWIZNETINFO).ip+i)); } */ next_time = jshGetSystemTime() + DHCP_WAIT_TIME; retry_count = 0; dhcp_state = STATE_DHCP_LEASED; } else { next_time = jshGetSystemTime() + DHCP_WAIT_TIME1; retry_count = 0; //dhcp_state = STATE_DHCP_DISCOVER; dhcp_state = STATE_DHCP_LEASED; jsiConsolePrintf("> => Recceived IP is invalid\r\n"); //iinchip_init(); //Set_Default(); //Set_network(); //ctlwizchip(CW_RESET_WIZCHIP, 0); //set_default_netinfo(); //ctlnetwork(CN_SET_NETINFO, (void*)&(*pWIZNETINFO)); } } else if (type == DHCP_NAK) { jsiConsolePrintf("> Receive DHCP_NACK\r\n"); next_time = jshGetSystemTime() + DHCP_WAIT_TIME; retry_count = 0; dhcp_state = STATE_DHCP_DISCOVER; } else check_Timeout(s, pWIZNETINFO); break; case STATE_DHCP_LEASED : if ((lease_time.lVal != 0xffffffff) && ((lease_time.lVal/2) < jshGetSystemTime())) { jsiConsolePrintf("> Renewal IP address \r\n"); type = 0; for (i = 0; i < 4; i++) OLD_SIP[i] = (*pWIZNETINFO).ip[i]; for (i = 0; i < 4; i++) d_addr[i] = DHCP_SIP[i]; DHCP_XID++; send_DHCP_REQUEST(s, (*pWIZNETINFO).ip, d_addr, pWIZNETINFO); dhcp_state = STATE_DHCP_REREQUEST; next_time = jshGetSystemTime() + DHCP_WAIT_TIME; } break; case STATE_DHCP_REREQUEST : if (type == DHCP_ACK) { retry_count = 0; flag = 0; for (i = 0; i < 4; i++) { if (OLD_SIP[i] != (*pWIZNETINFO).ip[i]) { flag = 1; break; } } // change to new IP address if (flag) { //iinchip_init(); //Set_network(); ctlwizchip(CW_RESET_WIZCHIP, 0); ctlnetwork(CN_SET_NETINFO, (void*)&(*pWIZNETINFO)); } next_time = jshGetSystemTime() + DHCP_WAIT_TIME; dhcp_state = STATE_DHCP_LEASED; } else if (type == DHCP_NAK) { next_time = jshGetSystemTime() + DHCP_WAIT_TIME; retry_count = 0; dhcp_state = STATE_DHCP_DISCOVER; // jsiConsolePrintf("state : STATE_DHCP_DISCOVER\r\n"); } else check_Timeout(s, pWIZNETINFO); break; case STATE_DHCP_RELEASE : break; default : break; } }
int main(void) { uint8_t ret = 0; //int32_t loopback_ret; #if defined (__USE_LPCOPEN) #if !defined(NO_BOARD_LIB) // Read clock settings and update SystemCoreClock variable SystemCoreClockUpdate(); // Set up and initialize all required blocks and // functions related to the board hardware Board_Init(); // Set the LED to the state of "On" Board_LED_Set(0, true); //Board_LED_Set(1, true); //Board_LED_Set(2, true); #endif #endif SPI_Init(); W5500_Init(); Net_Conf(gWIZNETINFO); #ifdef _MAIN_DEBUG_ uint8_t tmpstr[6] = {0,}; ctlwizchip(CW_GET_ID,(void*)tmpstr); /* printf("\r\n=======================================\r\n"); printf(" WIZnet %s EVB Demos v%d.%.2d\r\n", tmpstr, VER_H, VER_L); printf("=======================================\r\n"); printf(">> W5500 based FTP Client Example\r\n"); printf("=======================================\r\n"); */ Display_Net_Conf(); // Print out the network information to serial terminal #endif /* Enable and setup SysTick Timer at a periodic rate */ SysTick_Config(SystemCoreClock / TICKRATE_HZ1); /* Initialize buttons on the W5500 EVB board */ Board_Buttons_Init(); g_sdcard_done = 0; #if defined(F_APP_FTPC) ftpc_init(gWIZNETINFO.ip); #endif ret = flash_mount(); if(ret > 0) { display_SDcard_Info(ret); } while(1) { /* Button: SW1 */ if(Check_Buttons_Pressed() == BUTTONS_BUTTON1) { printf("\r\n########## SW1 was pressed.\r\n"); printf("########## Data flash flag was cleared.\r\n"); printf("########## Please reset a target.\r\n"); release_factory_flag(); } #if defined(F_APP_FTPC) ftpc_run(gFTPBUF); #endif /* Loopback Test: TCP Server and UDP */ // Test for Ethernet data transfer validation { //loopback_tcps(SOCK_TCPS, gDATABUF, PORT_TCPS); //loopback_udps(SOCK_UDPS, gDATABUF, PORT_UDPS); //loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport); //if(loopback_ret < 0) printf("loopback ret: %ld\r\n", loopback_ret); // TCP Socket Error code } } return 0 ; }
/*JSON{ "type" : "staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"], ["cs", "pin", "The pin to use for Chip Select"] ], "return" : ["JsVar","An Ethernet Object"], "return_object" : "Ethernet" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); spiDevice = ETH_SPI; } if (!jshIsPinValid(cs)) cs = ETH_CS_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); net.data.device = spiDevice; net.data.pinCS = cs; networkSet(&net); JsVar *ethObj = jspNewObject(0, "Ethernet"); // CS Configuration jshSetPinStateIsManual(net.data.pinCS, false); jshPinOutput(net.data.pinCS, 1); // de-assert CS // Initialise WIZnet functions reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZnet Initialize failed.\r\n"); networkFree(&net); return 0; } #if _WIZCHIP_ == 5500 /* PHY link status check - W5100 doesn't have this */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); networkFree(&net); return 0; } } while (tmp == PHY_LINK_OFF); #endif networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
/** * @brief Main program * @param None * @retval None */ int main() { //uint8_t tx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; //uint8_t rx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0x33}; //uint8_t src_addr[4] = {192, 168, 0, 80}; //uint8_t gw_addr[4] = {192, 168, 0, 1}; //uint8_t sub_addr[4] = {255, 255, 255, 0}; //uint8_t dns_server[4] = {8, 8, 8, 8}; // for Example domain name server //uint8_t tmp[8]; //int ret; int i; #if defined (_MAIN_DEBUG_) && defined (_USE_SDCARD_) int ret; #endif #if defined(F_APP_FTP) wiz_NetInfo gWIZNETINFO; #endif #if defined(F_APP_DHCP) || defined(F_APP_DNS) S2E_Packet *value = get_S2E_Packet_pointer(); #endif #if defined(F_APP_DNS) uint8_t dns_server_ip[4]; #endif /* External Clock */ CRG_PLL_InputFrequencySelect(CRG_OCLK); /* Set System init */ SystemInit(); /* UART Init */ UART_StructInit(&UART_InitStructure); UART_Init(UART_DEBUG,&UART_InitStructure); /* SysTick_Config */ SysTick_Config((GetSystemClock()/1000)); /* Set WZ_100US Register */ setTIC100US((GetSystemClock()/10000)); //getTIC100US(); //printf(" GetSystemClock: %X, getTIC100US: %X, (%X) \r\n", // GetSystemClock, getTIC100US(), *(uint32_t *)TIC100US); LED_Init(LED1); LED_Init(LED2); LED_Init(LED3); LED_Off(LED1); LED_Off(LED2); LED_Off(LED3); g_sdcard_done = 0; BOOT_Pin_Init(); Board_factory_Init(); EXTI_Configuration(); /* Load Configure Information */ load_S2E_Packet_from_storage(); UART_Configuration(); /* Check MAC Address */ check_mac_address(); Timer0_Configuration(); // ADC initialize ADC_Init(); #ifdef _MAIN_DEBUG_ uint8_t tmpstr[6] = {0,}; ctlwizchip(CW_GET_ID,(void*)tmpstr); printf("\r\n============================================\r\n"); printf(" WIZnet %s EVB Demo v%d.%.2d\r\n", tmpstr, VER_H, VER_L); printf("============================================\r\n"); printf(" WIZwiki Platform based WEBBoot Example\r\n"); printf("============================================\r\n"); #endif #ifdef __DEF_USED_IC101AG__ //For using IC+101AG *(volatile uint32_t *)(0x41003068) = 0x64; //TXD0 - set PAD strengh and pull-up *(volatile uint32_t *)(0x4100306C) = 0x64; //TXD1 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003070) = 0x64; //TXD2 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003074) = 0x64; //TXD3 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003050) = 0x64; //TXE - set PAD strengh and pull-up #endif #ifdef __DEF_USED_MDIO__ /* mdio Init */ mdio_init(GPIOB, MDC, MDIO ); /* PHY Link Check via gpio mdio */ while( link() == 0x0 ) { printf("."); delay(500); } printf("PHY is linked. \r\n"); #else delay(1000); delay(1000); #endif Mac_Conf(); #if defined(F_APP_DHCP) if(value->options.dhcp_use) // DHCP { uint32_t ret; uint8_t dhcp_retry = 0; #ifdef _MAIN_DEBUG_ printf(" - DHCP Client running\r\n"); #endif DHCP_init(SOCK_DHCP, TX_BUF); reg_dhcp_cbfunc(w5500_dhcp_assign, w5500_dhcp_assign, w5500_dhcp_conflict); while(1) { ret = DHCP_run(); if(ret == DHCP_IP_LEASED) { #ifdef _MAIN_DEBUG_ printf(" - DHCP Success: DHCP Leased time : %ld Sec.\r\n\r\n", getDHCPLeasetime()); #endif break; } else if(ret == DHCP_FAILED) { dhcp_retry++; #ifdef _MAIN_DEBUG_ if(dhcp_retry <= 3) printf(" - DHCP Timeout occurred and retry [%d]\r\n", dhcp_retry); #endif } if(dhcp_retry > 3) { #ifdef _MAIN_DEBUG_ printf(" - DHCP Failed\r\n\r\n"); #endif value->options.dhcp_use = 0; Net_Conf(); break; } do_udp_config(SOCK_CONFIG); } } else // Static { Net_Conf(); } #else Net_Conf(); #endif #ifdef _MAIN_DEBUG_ display_Net_Info(); #endif #if defined(F_APP_ATC) atc_init(&rxring, &txring); op_mode = OP_DATA; #endif TFTP_init(SOCK_TFTP, socket_buf); ret = application_update(); printf("[DEBUG] check trigger:%d ret:%d \r\n", get_bootpin_Status(), ret); if((get_bootpin_Status() == 1) && (ret != TFTP_FAIL)) { uint32_t tmp; #if !defined(MULTIFLASH_ENABLE) tmp = *(volatile uint32_t *)APP_BASE; #else tmp = *(volatile uint32_t *)flash.flash_app_base; #endif if((tmp & 0xffffffff) != 0xffffffff) { printf("[DEBUG] application_jump\r\n"); application_jump(); } } #ifdef _USE_SDCARD_ // SD card Initialization ret = mmc_mount(); if(ret <= 0) { #ifdef _MAIN_DEBUG_ printf("\r\n - Can't mount SD card: Please Reboot WIZ750WEB Board or Insert SD card\r\n"); #endif //while(!(ret = mmc_mount())); } if(ret > 0) { #ifdef _MAIN_DEBUG_ display_SDcard_Info(ret); #endif } #endif httpServer_init(TX_BUF, RX_BUF, MAX_HTTPSOCK, socknumlist); #ifdef _USE_WATCHDOG_ reg_httpServer_cbfunc(NVIC_SystemReset, IWDG_ReloadCounter); // Callback: STM32 MCU Reset / WDT Reset (IWDG) #else reg_httpServer_cbfunc(NVIC_SystemReset, NULL); // Callback: STM32 MCU Reset #endif IO_status_init(); #if defined(F_APP_FTP) ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO); ftpd_init(gWIZNETINFO.ip); // Added by James for FTP #endif #ifdef _USE_WATCHDOG_ // IWDG Initialization: STM32 Independent WatchDog IWDG_Configureation(); #endif while (1) { #ifdef _USE_WATCHDOG_ IWDG_ReloadCounter(); // Feed IWDG #endif #if defined(F_APP_ATC) atc_run(); #endif if(g_op_mode == NORMAL_MODE) { do_udp_config(SOCK_CONFIG); } else { if(TFTP_run() != TFTP_PROGRESS) g_op_mode = NORMAL_MODE; } #if defined(F_APP_DHCP) if(value->options.dhcp_use) DHCP_run(); #endif for(i = 0; i < MAX_HTTPSOCK; i++) httpServer_run(i); // HTTP server handler #if defined(F_APP_FTP) ftpd_run(FTP_DBUF); #endif #ifdef _USE_WATCHDOG_ IWDG_ReloadCounter(); // Feed IWDG #endif } return 0; }
/** * @brief Main program * @param None * @retval None */ int main() { //uint8_t tx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; //uint8_t rx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0x34}; uint8_t src_addr[4] = {192, 168, 0, 80}; uint8_t gw_addr[4] = {192, 168, 0, 1}; uint8_t sub_addr[4] = {255, 255, 255, 0}; //uint8_t dns_server[4] = {8, 8, 8, 8}; // for Example domain name server uint8_t tmp[8]; #if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLOCK int ret; uint8_t sn; #if defined(TEST_TCPC) int32_t loopback_ret; struct sockaddr_in sa; uint32_t s_addr; #endif #endif // For TCP client loopback examples; destination network info //uint8_t destip[4] = {192, 168, 0, 2}; //uint16_t destport = 5000; //int32_t loopback_ret; /* External Clock */ CRG_PLL_InputFrequencySelect(CRG_OCLK); /* Set Systme init */ SystemInit(); /* UART Init */ UART_StructInit(&UART_InitStructure); UART_Init(UART1,&UART_InitStructure); /* SysTick_Config */ SysTick_Config((GetSystemClock()/1000)); /* Set WZ_100US Register */ setTIC100US((GetSystemClock()/10000)); //getTIC100US(); //printf(" GetSystemClock: %X, getTIC100US: %X, (%X) \r\n", // GetSystemClock, getTIC100US(), *(uint32_t *)TIC100US); #ifdef _MAIN_DEBUG_ uint8_t tmpstr[6] = {0,}; ctlwizchip(CW_GET_ID,(void*)tmpstr); printf("\r\n============================================\r\n"); printf(" WIZnet %s EVB Demo v%d.%.2d\r\n", tmpstr, VER_H, VER_L); printf("============================================\r\n"); printf(" WIZwiki Platform based Loopback Example\r\n"); printf("============================================\r\n"); #endif #ifdef __DEF_USED_IC101AG__ //For using IC+101AG *(volatile uint32_t *)(0x41003068) = 0x64; //TXD0 - set PAD strengh and pull-up *(volatile uint32_t *)(0x4100306C) = 0x64; //TXD1 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003070) = 0x64; //TXD2 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003074) = 0x64; //TXD3 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003050) = 0x64; //TXE - set PAD strengh and pull-up #endif #ifdef __DEF_USED_MDIO__ /* mdio Init */ mdio_init(GPIOB, MDC, MDIO ); /* PHY Link Check via gpio mdio */ while( link() == 0x0 ) { printf("."); delay(500); } printf("PHY is linked. \r\n"); #else delay(1000); delay(1000); #endif /* Network Configuration */ setSHAR(mac_addr); setSIPR(src_addr); setGAR(gw_addr); setSUBR(sub_addr); getSHAR(tmp); printf("MAC ADDRESS : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\r\n",tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]); getSIPR(tmp); printf("IP ADDRESS : %.3d.%.3d.%.3d.%.3d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); getGAR(tmp); printf("GW ADDRESS : %.3d.%.3d.%.3d.%.3d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); getSUBR(tmp); printf("SN MASK: %.3d.%.3d.%.3d.%.3d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); /* Set Network Configuration */ //wizchip_init(tx_size, rx_size); #if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLOCK #if defined(TEST_TCPS) || defined(TEST_TCPC) ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); #elif defined(TEST_UDPS) ret = socket(AF_INET, SOCK_STREAM, IPPROTO_UDP); #endif #if defined(TEST_TCPC) memset(&sa, 0, sizeof(sa)); sa.sin_len = (uint16_t)sizeof(sa); sa.sin_family = AF_INET; sa.sin_port = htons(destport); s_addr = (((((destip[0] << 8) | destip[1]) << 8) | destip[2]) << 8) | destip[3]; sa.sin_addr.s_addr = htonl(s_addr); #endif if(ret == -1) { #ifdef _LOOPBACK_DEBUG_ printf("%d:Socket Error\r\n", ret); #endif return ret; } sn = ret; #endif while(1) { /* Loopback Test: TCP Server and UDP */ #if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLOCK #if defined(TEST_TCPS) loopback_tcps_bsd(sn, gDATABUF, TEST_PORT); #elif defined(TEST_UDPS) loopback_udps_bsd(sn, gDATABUF, TEST_PORT); #elif defined(TEST_TCPC) loopback_ret = loopback_tcpc_bsd(sn, gDATABUF, (struct sockaddr*)&sa); if(loopback_ret < 0) printf("loopback ret: %ld\r\n", loopback_ret); // TCP Socket Error code #endif #elif LOOPBACK_MODE == LOOPBACK_MAIN_SAMPLE #if defined(TEST_TCPS) loopback_tcps_bsd(test_buf); #elif defined(TEST_TCPC) loopback_tcpc_bsd(test_buf); #endif #endif } }
/** * @brief Main program * @param None * @retval None */ int main() { //uint8_t tx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; //uint8_t rx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0x33}; //uint8_t src_addr[4] = {192, 168, 0, 80}; //uint8_t gw_addr[4] = {192, 168, 0, 1}; //uint8_t sub_addr[4] = {255, 255, 255, 0}; //uint8_t dns_server[4] = {8, 8, 8, 8}; // for Example domain name server //uint8_t tmp[8]; //int ret; /* External Clock */ CRG_PLL_InputFrequencySelect(CRG_OCLK); /* Set System init */ SystemInit(); /* UART Init */ UART_StructInit(&UART_InitStructure); UART_Init(UART_DEBUG,&UART_InitStructure); /* SysTick_Config */ SysTick_Config((GetSystemClock()/1000)); /* Set WZ_100US Register */ setTIC100US((GetSystemClock()/10000)); //getTIC100US(); //printf(" GetSystemClock: %X, getTIC100US: %X, (%X) \r\n", // GetSystemClock, getTIC100US(), *(uint32_t *)TIC100US); LED_Init(LED1); LED_Init(LED2); LED_On(LED1); LED_Off(LED2); BOOT_Pin_Init(); /* Load Configure Information */ load_S2E_Packet_from_storage(); UART_Configuration(); /* Check MAC Address */ check_mac_address(); Timer0_Configuration(); #ifdef _MAIN_DEBUG_ uint8_t tmpstr[6] = {0,}; ctlwizchip(CW_GET_ID,(void*)tmpstr); printf("\r\n============================================\r\n"); printf(" WIZnet %s EVB Demo v%d.%.2d\r\n", tmpstr, VER_H, VER_L); printf("============================================\r\n"); printf(" WIZwiki Platform based TFTP App Example\r\n"); printf("============================================\r\n"); #endif #ifdef __DEF_USED_IC101AG__ //For using IC+101AG *(volatile uint32_t *)(0x41003068) = 0x64; //TXD0 - set PAD strengh and pull-up *(volatile uint32_t *)(0x4100306C) = 0x64; //TXD1 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003070) = 0x64; //TXD2 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003074) = 0x64; //TXD3 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003050) = 0x64; //TXE - set PAD strengh and pull-up #endif #ifdef __DEF_USED_MDIO__ /* mdio Init */ mdio_init(GPIOB, MDC, MDIO ); /* PHY Link Check via gpio mdio */ while( link() == 0x0 ) { printf("."); delay(500); } printf("PHY is linked. \r\n"); #else delay(1000); delay(1000); #endif #if 0 /* Network Configuration */ setSHAR(mac_addr); setSIPR(src_addr); setGAR(gw_addr); setSUBR(sub_addr); getSHAR(tmp); printf("MAC ADDRESS : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\r\n",tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]); getSIPR(tmp); printf("IP ADDRESS : %.3d.%.3d.%.3d.%.3d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); getGAR(tmp); printf("GW ADDRESS : %.3d.%.3d.%.3d.%.3d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); getSUBR(tmp); printf("SN MASK: %.3d.%.3d.%.3d.%.3d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); /* Set Network Configuration */ //wizchip_init(tx_size, rx_size); #else Net_Conf(); #endif //TFTP_init(SOCK_TFTP, socket_buf); while (1) { if(g_op_mode == NORMAL_MODE) { do_udp_config(SOCK_CONFIG); } else { if(TFTP_run() != TFTP_PROGRESS) g_op_mode = NORMAL_MODE; } } return 0; }