Hello,
I am trying a udp (to send data out of controller,incoming data source FPGA)
MY SAMPLE CODE
#include <stdbool.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/emac.h"
#include "inc/hw_emac.h"
#include "driverlib/flash.h"
#include "driverlib/gpio.h"
#include "inc/hw_gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "utils/locator.h"
#include "utils/lwiplib.h"
#include "utils/uartstdio.h"
#include "driverlib/debug.h"
#include "driverlib/epi.h"
#include "inc/hw_epi.h"
#include "utils/ustdlib.h"
#include "drivers/pinout.h"
#include "lwip/inet.h"
#include "inc/hw_sysctl.h"
#include "drivers/pinout.h"
//*****************************************************************************
//
// Defines for setting up the system clock.
//
//*****************************************************************************
#define SYSTICKHZ 100
#define SYSTICKMS (1000 / SYSTICKHZ)
//*****************************************************************************
//
// Interrupt priority definitions. The top 3 bits of these values are
// significant with lower values indicating higher priority interrupts.
//
//*****************************************************************************
#define SYSTICK_INT_PRIORITY 0x80
#define ETHERNET_INT_PRIORITY 0xC0
//*****************************************************************************
//
// External Application references.
//
//*****************************************************************************
extern void my_udp_init(void);
uint32_t sourceaddr, netmask,gateway;
struct ip_addr udpDestIpAddr;
//*************************************************************
struct udp_pcb *my_pcb;
struct pbuf* pbuf1;
char buf [120];
//struct udp_pcb *pcb;
struct udp_pcb *g_upcb1,*g_upcb2;
unsigned char *datasram;
int i,j;
//*****************************************************************************
//
// The current IP address.
//
//*****************************************************************************
uint32_t g_ui32IPAddress;
//*****************************************************************************
//
// The system clock frequency. Used by the SD card driver.
//
//*****************************************************************************
uint32_t g_ui32SysClock;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//**************************************************
// UDP transmit ...........................................
void my_udp_tx(u8_t *data, u16_t len)
{
my_pcb = udp_new();
{
if (my_pcb->remote_port != (uint16_t)0)
{
// memcpy(p->payload, data, len);
static int n = 0;
pbuf1 = pbuf_alloc(PBUF_TRANSPORT, 100, PBUF_RAM); //Get a pbuf struct.
pbuf1->payload = (void*)buf;
pbuf1->tot_len = 15;
pbuf1->len = 15;
buf[0] = 0x10; // = text
buf[1] = 0;
usprintf(&buf[4], " hi karthi %d", n++);
IP4_ADDR(&udpDestIpAddr,172,17,2,30);
udp_sendto(my_pcb, pbuf1,&udpDestIpAddr,1001);
pbuf_free(pbuf1);
}
}
}
void my_udp_rx(struct udp_pcb *upcb,struct ip_addr *addr, u16_t port,u8_t *buf)
{
/* process the payload in p->payload */
udp_connect(upcb, addr, port); /* connect to the remote host */
my_udp_tx(buf,1456);
free(buf); /* don't leak the pbuf!*/
}
/*
void UDP_PKTRX(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
if (p != NULL)
{
UDPData = p->payload;
gain = UDPData[16];
Config_PGAGain(gain);
pbuf_free(p);
}
}
*/
// UDP initialization ......................................
void my_udp_init(void)
{
struct udp_pcb * mypcb;
mypcb = udp_new();
if (mypcb == NULL)
{
UARTprintf("udp failed.\n");
}
else
{
UARTprintf("udp up.\n");
}
if (udp_bind(mypcb, IP_ADDR_ANY, 8760) != ERR_OK)
{
UARTprintf("udp bind failed.\n");
}
//udp_bind(g_upcb, IP_ADDR_ANY, 777);
udp_recv(mypcb,&my_udp_rx, NULL);
}
//
//*****************************************************************************
//
// The interrupt handler for the SysTick interrupt.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
//
// Call the lwIP timer handler.
//
lwIPTimer(SYSTICKMS);
}
//*****************************************************************************
//
// Display an lwIP type IP Address.
//
//*****************************************************************************
void
DisplayIPAddress(uint32_t ui32Addr)
{
char pcBuf[16];
//
// Convert the IP Address into a string.
//
usprintf(pcBuf, "%d.%d.%d.%d", ui32Addr & 0xff, (ui32Addr >> 8) & 0xff,
(ui32Addr >> 16) & 0xff, (ui32Addr >> 24) & 0xff);
//
// Display the string.
//
UARTprintf(pcBuf);
}
//*****************************************************************************
//
// Required by lwIP library to support any host-related timer functions.
//
//*****************************************************************************
void
lwIPHostTimerHandler(void)
{
uint32_t ui32Idx, ui32NewIPAddress;
//
// Get the current IP address.
//
ui32NewIPAddress = lwIPLocalIPAddrGet();
//
// See if the IP address has changed.
//
if(ui32NewIPAddress != g_ui32IPAddress)
{
//
// See if there is an IP address assigned.
//
if(ui32NewIPAddress == 0xffffffff)
{
//
// Indicate that there is no link.
//
UARTprintf("Waiting for link.\n");
}
else if(ui32NewIPAddress == 0)
{
//
// There is no IP address, so indicate that the DHCP process is
// running.
//
UARTprintf("Waiting for IP address.\n");
}
else
{
//
// Display the new IP address.
//
UARTprintf("IP Address: ");
DisplayIPAddress(ui32NewIPAddress);
UARTprintf("\n");
UARTprintf("Open a browser and enter the IP address.\n");
}
//
// Save the new IP address.
//
g_ui32IPAddress = ui32NewIPAddress;
}
//
// If there is not an IP address.
//
if((ui32NewIPAddress == 0) || (ui32NewIPAddress == 0xffffffff))
{
//
// Loop through the LED animation.
//
for(ui32Idx = 1; ui32Idx < 17; ui32Idx++)
{
//
// Toggle the GPIO
//
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1,
(MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) ^
GPIO_PIN_1));
SysCtlDelay(g_ui32SysClock/(ui32Idx << 1));
}
}
}
//*****************************************************************************
//
// This example demonstrates the use of the Ethernet Controller and lwIP
// TCP/IP stack to control various peripherals on the board via a web
// browser.
//
//*****************************************************************************
int
main(void)
{
uint32_t ui32User0, ui32User1;
uint8_t pui8MACArray[8];
//
// Make sure the main oscillator is enabled because this is required by
// the PHY. The system must have a 25MHz crystal attached to the OSC
// pins. The SYSCTL_MOSC_HIGHFREQ parameter is used when the crystal
// frequency is 10MHz or higher.
//
SysCtlMOSCConfigSet(SYSCTL_MOSC_HIGHFREQ);
//
// Run from the PLL at 120 MHz.
//
g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// Configure the device pins.
//
PinoutSet(true, false);
//
// Configure debug port for internal use.
//
UARTStdioConfig(0, 115200, g_ui32SysClock);
//
// Clear the terminal and print a banner.
//
UARTprintf("\033[2J\033[H");
UARTprintf("Ethernet IO Example\n\n");
//
// Configure SysTick for a periodic interrupt.
//
MAP_SysTickPeriodSet(g_ui32SysClock / SYSTICKHZ);
MAP_SysTickEnable();
MAP_SysTickIntEnable();
//
// Configure the hardware MAC address for Ethernet Controller filtering of
// incoming packets. The MAC address will be stored in the non-volatile
// USER0 and USER1 registers.
//
MAP_FlashUserGet(&ui32User0, &ui32User1);
if((ui32User0 == 0xffffffff) || (ui32User1 == 0xffffffff))
{
//
// Let the user know there is no MAC address
//
UARTprintf("No MAC programmed!\n");
while(1)
{
}
}
//
// Tell the user what we are doing just now.
//
UARTprintf("Waiting for IP.\n");
//
// Convert the 24/24 split MAC address from NV ram into a 32/16 split
// MAC address needed to program the hardware registers, then program
// the MAC address into the Ethernet Controller registers.
//
pui8MACArray[0] = ((ui32User0 >> 0) & 0xff);
pui8MACArray[1] = ((ui32User0 >> 8) & 0xff);
pui8MACArray[2] = ((ui32User0 >> 16) & 0xff);
pui8MACArray[3] = ((ui32User1 >> 0) & 0xff);
pui8MACArray[4] = ((ui32User1 >> 8) & 0xff);
pui8MACArray[5] = ((ui32User1 >> 16) & 0xff);
//
// Initialze the lwIP library, using DHCP.
sourceaddr=inet_addr("28.1.17.172");
netmask=inet_addr("0.0.255.255");
gateway=inet_addr("3.0.17.172");
lwIPInit(g_ui32SysClock,pui8MACArray,sourceaddr,netmask,gateway,IPADDR_USE_STATIC);
//
// Setup the device locator service.
//
LocatorInit();
LocatorMACAddrSet(pui8MACArray);
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1,
(MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) ^
GPIO_PIN_1));
my_udp_init();
/* struct udp_pcb * mypcb;
mypcb = udp_new();
if (mypcb == NULL)
{
UARTprintf("udp failed.\n");
}
else
{
UARTprintf("udp up.\n");
}
if (udp_bind(mypcb, IP_ADDR_ANY, 8760) != ERR_OK)
{
UARTprintf("udp bind failed.\n");
}
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1,
(MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) ^
GPIO_PIN_1));
*/
while(1)
{
/* // MAP_IntPrioritySet(INT_EMAC0, ETHERNET_INT_PRIORITY);
// MAP_IntPrioritySet(FAULT_SYSTICK, SYSTICK_INT_PRIORITY);
static int n = 0;
pbuf1 = pbuf_alloc(PBUF_TRANSPORT, 100, PBUF_RAM);
pbuf1->payload = (void*)buf;
pbuf1->tot_len = 15;
pbuf1->len = 15;
buf[0] = 0x10;
buf[1] = 0;
usprintf(&buf[4], " hi karthi %d", n++);
IP4_ADDR(&udpDestIpAddr,172,17,2,30);
udp_sendto(mypcb, pbuf1,&udpDestIpAddr,1001);
pbuf_free(pbuf1);
*/
}
}
UDP _REC FUNCTION IS IN COMPATIABLE.iNTIALLY I SEND THE HARDCODED DATA OUT USING UDP STRUCTURE IN MAIN LOOP.But now requirement is i have to get fpga data into a structure and then send it.
Thanking For um members for all support,
Regards,
Krishnan