I am trying to get a dynamic IP address with custom mpc5775b board and ksz8041 phy device in s32 design studio for pa but I don't get an IP address. I have used eth,phy,tcpip components. I have added my main.c
file below.
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/ethip6.h"
#include "lwip/etharp.h"
#include "lwipcfg.h"
#include "lwip/sys.h"
#include "enetif.h"
#include "enet_driver.h"
#include "osif.h"
#if defined(USING_OS_FREERTOS)
#include "FreeRTOS.h"
#endif /* defined(USING_OS_FREERTOS) */
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "netifcfg.h"
#include "Cpu.h"
#include "clockMan1.h"
#include "pin_mux.h"
#include "ethernet1.h"
#include "device_registers.h"
#include<phy.h>
#include "lwip/prot/dhcp.h" // Include the header file where the enum is defined
#include "lwip/tcpip.h"
#include "lwip/netif.h"
#include "lwip/ip_addr.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/udp.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#include "lwip/opt.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "lwip/dhcp.h"
#define LWIP_HDR_TCPIP_H
#include "lwip/opt.h"
#include "lwip/err.h"
#include "lwip/timeouts.h"
#include "lwip/netif.h"
struct netif netif_ptr;
struct netif netif_ptr_2;
struct pbuf *p;
phy_id_t phy_id;
uint8_t phy = 0; // assuming you want to access PHY 0, change it as needed
phy_state_t phy_state;
int linkup;
phy_speed_t link_speed;
void link_up(uint8_t phy){
status_t id_status = PHY_GetId(phy, &phy_id);
if (id_status == STATUS_SUCCESS) {
printf("PHY ID: %lu\n", phy_id.oui);
} else {
printf("Failed to get PHY ID\n");
}
// Get state of the PHY device
status_t state_status = PHY_GetState(phy, &phy_state);
if (state_status == STATUS_SUCCESS) {
printf("PHY State: %d\n", phy_state);
} else {
printf("Failed to get PHY state\n");
}
// Get link status of the PHY device
status_t link_status = PHY_GetLinkStatus(phy, &linkup);
if (link_status == STATUS_SUCCESS && linkup) {
printf("Link is up\n");
} else {
printf("Link is down\n");
}
// Get link speed of the PHY device
status_t link_speed_status = PHY_GetLinkSpeed(phy, &link_speed);
if (link_speed_status == STATUS_SUCCESS) {
printf("Link speed: %d\n", link_speed);
} else {
printf("Failed to get link speed\n");
}
phy_speed_t speed;
PHY_GetLinkSpeed(phy, &speed);
switch (speed)
{
case PHY_SPEED_10_MBPS:
ENET_DRV_SetSpeed(INST_ETHERNET1, ENET_MII_SPEED_100M);
break;
case PHY_SPEED_100_MBPS:
ENET_DRV_SetSpeed(INST_ETHERNET1, ENET_MII_SPEED_100M);
break;
//default:
}
}
void link_down(uint8_t phy){}
void autoneg_failed(uint8_t phy){}
void autoneg_complete(uint8_t phy){}
void rx_callback(uint8_t instance, enet_event_t event, uint8_t ring){}
// Define the maximum number of network interfaces you expect
#define MAX_INTERFACES 2
static sys_sem_t tcpip_init_sem;
// Define structure to hold network information
typedef struct {
char ip_address[16]; // Assuming IPv4 address (xxx.xxx.xxx.xxx) with null terminator
char net_mask[16]; // Assuming IPv4 netmask
char gat_eway[16]; // Assuming IPv4 gateway
// struct netif netif_ptr; // Pointer to the network interface
} NetworkInfo;
// Global array to store network information
NetworkInfo network_info[MAX_INTERFACES];
// Function prototype for start_example_internal
void start_example_internal(void);
// Function prototype for lwip_init_complete_callback
void tcpip_init_complete_callback(void *arg);
volatile int exit_code = 0;
extern void start_example(void);
// TCP/IP initialization complete callback function
void tcpip_init_complete_callback(void *arg) {
// Signal the semaphore to indicate TCP/IP initialization completion
sys_sem_signal(&tcpip_init_sem);
}
void check_dhcp(void) {
// struct dhcp my_dhcp_state;
struct dhcp *dhcp_state = netif_dhcp_data(&netif_ptr);
if (dhcp_state) {
switch (dhcp_state->state) {
case DHCP_STATE_BOUND:
printf("DHCP bound\n");
print_dhcp_state(dhcp_state);
break;
case DHCP_STATE_OFF:
printf("DHCP off\n");
break;
default:
printf("DHCP state: %u\n", dhcp_state->state);
break;
}
} else {
printf("DHCP not initialized\n");
}
}
void start_example_internal(void) {
int interface_count = 0;
ip_addr_t ipaddr, netmask, gw;
IP4_ADDR(&ipaddr,0,0, 0, 0);
IP4_ADDR(&netmask, 0, 0, 0, 0);
IP4_ADDR(&gw, 0, 0,0, 0);
if (linkup) {
struct netif *netif_result = netif_add(&netif_ptr, &addr, &netmask, &gw, NULL, enet_ethernetif_init, tcpip_input);
if (netif_result == NULL) {
printf("Failed to add network interface\n");
return 1; // Exit the program if netif_add fails
}
while(1)
{
netif_set_default(&netif_ptr);
// netif_set_up(&netif_ptr);
netif_set_link_up(&netif_ptr);
#if LWIP_DHCP
err_t we= dhcp_start(&netif_ptr);
if (we==ERR_OK)
{
printf("dhcp not failed");
}
#endif
// struct dhcp *my_dhcp_state;
// print_dhcp_state(&my_dhcp_state);
netif_set_up(&netif_ptr);
for (int i = 0; i < MAX_INTERFACES; i++) {
PHY_GetLinkStatus(phy, &linkup);
if (!linkup) {
printf("Link is down, waiting...\n");
}
strncpy(network_info[interface_count].ip_address, ip4addr_ntoa(&ipaddr), sizeof(network_info[interface_count].ip_address) - 1);
network_info[interface_count].ip_address[sizeof(network_info[interface_count].ip_address) - 1] = '\0'; // Ensure null termination
strncpy(network_info[interface_count].net_mask, ip4addr_ntoa(&netmask), sizeof(network_info[interface_count].net_mask) - 1);
network_info[interface_count].net_mask[sizeof(network_info[interface_count].net_mask) - 1] = '\0'; // Ensure null termination
strncpy(network_info[interface_count].gat_eway, ip4addr_ntoa(&gw), sizeof(network_info[interface_count].gat_eway) - 1);
network_info[interface_count].gat_eway[sizeof(network_info[interface_count].gat_eway) - 1] = '\0'; // Ensure null termination
char ip[16];
char ipgw[16];
char ipnm[16];
if (linkup) {
strcpy(ip, ip4addr_ntoa(netif_ip4_addr(&netif_ptr)));
strcpy(ipnm, ip4addr_ntoa(netif_ip4_netmask(&netif_ptr)));
strcpy(ipgw, ip4addr_ntoa(netif_ip4_gw(&netif_ptr)));
}
// }
}
}
}
}
int __read_console(void *buf, int len) {
return 0;
}
int __write_console(const void *buf, int len) {
return len;
}
int __close_console(int file) {
return 0;
}
void print_dhcp_state(struct dhcp *dhcp_state) {
printf("DHCP State:\n");
printf(" Transaction ID (xid): %u\n", dhcp_state->xid);
printf(" PCB Allocated: %u\n", dhcp_state->pcb_allocated);
printf(" State: %u\n", dhcp_state->state);
printf(" Tries: %u\n", dhcp_state->tries);
#if LWIP_DHCP_AUTOIP_COOP
printf(" AutoIP Coop State: %u\n", dhcp_state->autoip_coop_state);
#endif
printf(" Subnet Mask Given: %u\n", dhcp_state->subnet_mask_given);
printf(" Request Timeout: %u\n", dhcp_state->request_timeout);
printf(" T1 Timeout: %u\n", dhcp_state->t1_timeout);
printf(" T2 Timeout: %u\n", dhcp_state->t2_timeout);
printf(" T1 Renew Time: %u\n", dhcp_state->t1_renew_time);
printf(" T2 Rebind Time: %u\n", dhcp_state->t2_rebind_time);
printf(" Lease Used: %u\n", dhcp_state->lease_used);
printf(" T0 Timeout: %u\n", dhcp_state->t0_timeout);
printf(" Server IP Address: %s\n", ipaddr_ntoa(&dhcp_state->server_ip_addr));
printf(" Offered IP Address: %s\n", ip4addr_ntoa(&dhcp_state->offered_ip_addr));
printf(" Offered Subnet Mask: %s\n", ip4addr_ntoa(&dhcp_state->offered_sn_mask));
printf(" Offered Gateway Address: %s\n", ip4addr_ntoa(&dhcp_state->offered_gw_addr));
printf(" Offered T0 Lease: %u seconds\n", dhcp_state->offered_t0_lease);
printf(" Offered T1 Renew: %u seconds\n", dhcp_state->offered_t1_renew);
printf(" Offered T2 Rebind: %u seconds\n", dhcp_state->offered_t2_rebind);
#if LWIP_DHCP_BOOTP_FILE
printf(" Offered SI Address: %s\n", ip4addr_ntoa(&dhcp_state->offered_si_addr));
printf(" Boot File Name: %s\n", dhcp_state->boot_file_name);
#endif
}
int main(void)
{
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
status_t clock= CLOCK_DRV_Init(&clockMan1_InitConfig0);
if( clock==STATUS_SUCCESS)
{
printf("clock success");
}
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
status_t pin_init= PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
if (pin_init== STATUS_SUCCESS)
{
printf(" pins are initlilised");
}
SIU->FECCR = SIU_FECCR_FM_MASK;
ENET_DRV_Init(INST_ETHERNET1, ðernet1_State, ðernet1_InitConfig0, ethernet1_buffConfigArr0, ethernet1_MacAddr);
ENET_DRV_EnableMDIO(INST_ETHERNET1, false);
//initialize phy framwwork and phy---
status_t phy_frameinit= PHY_FrameworkInit(phyConfig, phyDrivers);
if( phy_frameinit==STATUS_SUCCESS)
{
printf("phy_framinit complete");
}
status_t phuinit=PHY_Init(0U);
if ( phuinit== STATUS_SUCCESS)
{
printf("phyinit success");
}
status_t link_speed_status = PHY_GetLinkSpeed(phy, &link_speed);
if (link_speed_status == STATUS_SUCCESS) {
printf("Link speed: %d\n", link_speed);
} else {
printf("Failed to get link speed\n");
}
tcpip_init(NULL, NULL);
void tcpip_init_complete_callback(void *arg);
// while(1)
// {
status_t mainfuc = PHY_MainFunction(phy);
if(mainfuc==STATUS_SUCCESS)
link_up(phy);
start_example_internal();
// }
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.1 [05.21]
** for the NXP C55 series of microcontrollers.
**
** ###################################################################
*/