📄 ser2enet.c
字号:
{
//
// Reset the connection timeout.
//
#if TELNET_TIMEOUT
g_ulConnectionTimeout = 0;
#endif
//
// Return OK.
//
return(ERR_OK);
}
//*****************************************************************************
//
//! Accepts a TCP connection for the telnet port.
//!
//! \param arg is not used in this implementation.
//! \param pcb is the pointer to the TCP control structure.
//! \param err is not used in this implementation.
//!
//! This function is called when the lwIP TCP/IP stack has an incoming
//! connection request on the telnet port.
//!
//! \return This function will return an lwIP defined error code.
//
//*****************************************************************************
static err_t
TelnetAccept(void *arg, struct tcp_pcb *pcb, err_t err)
{
unsigned long ulIdx;
//
// Check if already connected.
//
if(g_psTelnetPCB)
{
//
// If we already have a connection, kill it and start over.
//
TelnetClose(g_psTelnetPCB);
}
//
// Set the connection timeout to 0.
//
#if TELNET_TIMEOUT
g_ulConnectionTimeout = 0;
#endif
//
// Set the connection as busy.
//
g_psTelnetPCB = pcb;
//
// Setup the TCP connection priority.
//
tcp_setprio(pcb, TCP_PRIO_MIN);
//
// Setup the TCP callback argument.
//
tcp_arg(pcb, NULL);
//
// Setup the TCP receive function.
//
tcp_recv(pcb, TelnetReceive);
//
// Setup the TCP error function.
//
tcp_err(pcb, TelnetError);
//
// Setup the TCP polling function/interval.
//
tcp_poll(pcb, TelnetPoll, (1000 / TCP_SLOW_INTERVAL));
//
// Setup the TCP sent callback function.
//
tcp_sent(pcb, TelnetSent);
//
// Send the telnet initialization string.
//
for(ulIdx = 0; ulIdx < sizeof(g_pucTelnetInit); ulIdx++)
{
g_pucTelnetBuffer[g_ulTelnetLength++] = g_pucTelnetInit[ulIdx];
}
//
// Return a success code.
//
return(ERR_OK);
}
//*****************************************************************************
//
//! Initializes the lwIP TCP/IP stack.
//!
//! This function handles the initialization of the lwIP TCP/IP stack, along
//! with configuration of the Ethernet controller and the lwIP driver for it.
//!
//! \return None.
//
//*****************************************************************************
static void
lwIPInit(void)
{
struct ip_addr xIpAddr, xNetMask, xGateway;
unsigned long ulUser0, ulUser1;
unsigned char pucMACArray[8];
void *pcb;
//
// Configure the hardware MAC address for Ethernet controller filtering of
// incoming packets.
//
FlashUserGet(&ulUser0, &ulUser1);
pucMACArray[0] = ((ulUser0 >> 0) & 0xff);
pucMACArray[1] = ((ulUser0 >> 8) & 0xff);
pucMACArray[2] = ((ulUser0 >> 16) & 0xff);
pucMACArray[3] = ((ulUser1 >> 0) & 0xff);
pucMACArray[4] = ((ulUser1 >> 8) & 0xff);
pucMACArray[5] = ((ulUser1 >> 16) & 0xff);
//
// Program the hardware with it's MAC address (for filtering).
//
EthernetMACAddrSet(ETH_BASE, pucMACArray);
//
// Low-Level initialization of the lwIP stack modules.
//
stats_init();
sys_init();
mem_init();
memp_init();
pbuf_init();
etharp_init();
ip_init();
udp_init();
tcp_init();
netif_init();
//
// Create, configure and add the Ethernet controller interface.
//
#ifdef USE_STATIC_IP
IP4_ADDR(&xIpAddr, STATIC_IPADDR0, STATIC_IPADDR1, STATIC_IPADDR2,
STATIC_IPADDR3);
IP4_ADDR(&xNetMask, STATIC_NETMASK0, STATIC_NETMASK1, STATIC_NETMASK2,
STATIC_NETMASK3);
IP4_ADDR(&xGateway, STATIC_GWADDR0, STATIC_GWADDR1, STATIC_GWADDR2,
STATIC_GWADDR3);
#else
IP4_ADDR(&xIpAddr, 0, 0, 0, 0);
IP4_ADDR(&xNetMask, 0, 0, 0, 0);
IP4_ADDR(&xGateway, 0, 0, 0, 0);
#endif
netif_add(&g_sEMAC_if, &xIpAddr, &xNetMask, &xGateway, NULL,
ethernetif_init, ip_input);
netif_set_default(&g_sEMAC_if);
#ifdef USE_DHCP
dhcp_start(&g_sEMAC_if);
#endif
//
// Bring the interface up.
//
netif_set_up(&g_sEMAC_if);
//
// Disable Ethernet TX Packet Interrupt.
//
EthernetIntDisable(ETH_BASE, ETH_INT_TX);
//
// Initialize the application to listen on the telnet port.
//
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, TELNET_PORT);
pcb = tcp_listen(pcb);
tcp_accept(pcb, TelnetAccept);
}
//*****************************************************************************
//
//! Handles the main application loop.
//!
//! This function initializes and configures the device and the software, then
//! performs the main loop. All the actual TCP/IP processing occurs within
//! this function (since lwIP is not reentrant).
//!
//! \return Never returns.
//
//*****************************************************************************
int
main(void)
{
unsigned long ulIdx, ulLen;
//
// Set the processor to run at 50 MHz, allowing UART operation at up to
// 3.125 MHz.
//
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_8MHZ);
//
// Enable the UART and the GPIO module that its pins are routed through.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure the UART pins appropriately.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Configure the UART.
//
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), UART_BAUDRATE,
UART_DATABITS | UART_PARITY | UART_STOPBITS);
//
// Enable the UART transmit and receive interrupts.
//
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT | UART_INT_TX);
IntEnable(INT_UART0);
//
// Enable and reset the Ethernet controller.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
//
// Configure LED0/LED1 to output the Ethernet link and activity indicators.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_DIR_MODE_HW);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3,
GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
//
// Configure SysTick for a periodic interrupt.
//
SysTickPeriodSet(SysCtlClockGet() / SYSTICKHZ);
SysTickEnable();
SysTickIntEnable();
//
// Initialize all of the lwIP code, as needed, which will also initialize
// the low-level Ethernet code.
//
lwIPInit();
//
// Main Application Loop (for systems with no RTOS). Run every SYSTICK.
//
while(true)
{
//
// Wait for an event to occur.
//
SysCtlSleep();
//
// Reset the length of the telnet packet to be sent.
//
g_ulTelnetLength = 0;
//
// Check if a SYSTICK has occurred.
//
if(HWREGBITW(&g_ulFlags, FLAG_SYSTICK))
{
//
// Clear the SysTick interrupt flag.
//
HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 0;
//
// Call the ARP timer when appropriate.
//
if(g_ulARPTimer >= ARP_TMR_INTERVAL)
{
g_ulARPTimer = 0;
etharp_tmr();
}
//
// Call the TCP slow timer when appropriate.
//
if(g_ulTCPSlowTimer >= TCP_SLOW_INTERVAL)
{
g_ulTCPSlowTimer = 0;
tcp_slowtmr();
}
//
// Call the TCP fast timer when appropriate.
//
if(g_ulTCPFastTimer >= TCP_FAST_INTERVAL)
{
g_ulTCPFastTimer = 0;
tcp_fasttmr();
}
//
// Call the DHCP coarse timer when appropriate.
//
#ifdef USE_DHCP
if(g_ulDHCPCoarseTimer >= (DHCP_COARSE_TIMER_SECS * 1000))
{
g_ulDHCPCoarseTimer = 0;
dhcp_coarse_tmr();
}
#endif
//
// Call the DHCP fine timer when appropriate.
//
#ifdef USE_DHCP
if(g_ulDHCPFineTimer >= DHCP_FINE_TIMER_MSECS)
{
g_ulDHCPFineTimer = 0;
dhcp_fine_tmr();
}
#endif
}
//
// Check to see if data was received from the Ethernet controller.
//
if(HWREGBITW(&g_ulFlags, FLAG_RXPKT))
{
//
// Clear the Rx Packet interrupt flag.
//
HWREGBITW(&g_ulFlags, FLAG_RXPKT) = 0;
//
// Check and process incoming packets
//
while(ethernetif_input(&g_sEMAC_if) == true)
{
}
//
// Enable Ethernet RX Packet Interrupts.
//
EthernetIntEnable(ETH_BASE, ETH_INT_RX);
}
//
// Check to see if data was received from the UART.
//
if(HWREGBITW(&g_ulFlags, FLAG_RXUART))
{
//
// Clear the UART receive interrupt flag.
//
HWREGBITW(&g_ulFlags, FLAG_RXUART) = 0;
//
// See if there is an active telnet connection.
//
if(g_psTelnetPCB)
{
//
// Get the current receive buffer write pointer since this may
// change during this processing.
//
ulLen = g_ulRXWrite;
//
// See if the write pointer is before the read pointer.
//
if(ulLen < g_ulRXRead)
{
//
// Copy the characters from the read pointer to the end of
// the receive buffer into the telnet packet buffer.
//
for(ulIdx = g_ulRXRead; ulIdx < UART_BUF_SIZE; ulIdx++)
{
g_pucTelnetBuffer[g_ulTelnetLength++] =
g_pucRXBuffer[ulIdx];
}
//
// Copy the characters from the beginning of the receive
// buffer to the write pointer into the telnet packet
// buffer.
//
for(ulIdx = 0; ulIdx < ulLen; ulIdx++)
{
g_pucTelnetBuffer[g_ulTelnetLength++] =
g_pucRXBuffer[ulIdx];
}
}
//
// Otherwise, the read pointer is before the write pointer.
//
else
{
//
// Copy the characters in the receive buffer from the read
// pointer to the write pointer into the telnet packet
// buffer.
//
for(ulIdx = g_ulRXRead; ulIdx < ulLen; ulIdx++)
{
g_pucTelnetBuffer[g_ulTelnetLength++] =
g_pucRXBuffer[ulIdx];
}
}
//
// Move the read pointer to the write pointer, indicating that
// those characters have been transmitted.
//
g_ulRXRead = ulLen;
}
//
// Otherwise, there is no telnet connection.
//
else
{
//
// Move the read pointer to the write pointer, discarding the
// characters received on the UART.
//
g_ulRXRead = g_ulRXWrite;
}
}
//
// See if there is a telnet connection and there are characters in the
// telnet packet buffer.
//
if(g_psTelnetPCB && (g_ulTelnetLength != 0))
{
//
// Send the contents of the telnet packet buffer on the telnet
// connection.
//
tcp_write(g_psTelnetPCB, g_pucTelnetBuffer, g_ulTelnetLength, 1);
tcp_output(g_psTelnetPCB);
}
}
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -