LM3S9B96 的以太網配置
/* 頭文件包含區(qū) --------------------------------------------------------------*/
#include "string.h" // strcmp函數和memset函數
#include "inc/lm3s9b96.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
#include "driverlib/ethernet.h"
#include "driverlib/flash.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/uart.h"
#include "driverlib/watchdog.h"
#include "utils/locator.h"
#include "utils/lwiplib.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "drivers/set_pinout.h"
#include "utils/uartstdio.h"
/* 數據類型定義區(qū) ------------------------------------------------------------*/
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef enum {FALSE = 0, TRUE = !FALSE} bool;
/* Defines for setting up the system clock -----------------------------------*/
#define SYSTICKHZ 100
#define SYSTICKMS (1000 / SYSTICKHZ)
#define SYSTICKUS (1000000 / SYSTICKHZ)
#define SYSTICKNS (1000000000 / SYSTICKHZ)
/* Position and movement granularity for the status indicator shown while the IP address is being determined--------*/
#define STATUS_X 50
#define STATUS_Y 100
#define MAX_STATUS_X (320 - (2 * STATUS_X))
#define ANIM_STEP_SIZE 8
/* 9b96主板的PF1宏定義 <調試時看門狗對應的LED> ------------------------------*/
#define LED_PERIPH SYSCTL_PERIPH_GPIOF
#define LED_PORT GPIO_PORTF_BASE
#define LED_PIN GPIO_PIN_1
#define LED_ON 1 << 1
#define LED_OFF 0
BYTE UDPData[1020] = {0x30}; // 全為0,網絡助手時間戳顯示有問題???
struct ip_addr local_addr;
struct udp_pcb *UdpPcb;
struct pbuf *p_tx; // the pointer of transmit buffer
struct ip_addr IP_Addr_Tmp; // 用于UDP_Receive函數中,臨時保存UdpPcb->remote_ip中的IP地址
/* External Application references -------------------------------------------*/
extern void fs_init(void);
extern void fs_tick(unsigned long ulTickMS);
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// 系統(tǒng)時鐘初始化函數
//
//*****************************************************************************
void SysClk_Init(void)
{
// 配置系統(tǒng)主時鐘, 使用外部晶振16M.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
}
//*****************************************************************************
//
// 以太網初始化函數
//
//*****************************************************************************
void ETH_Init(void)
{
DWORD ulUser0, ulUser1;
BYTE pucMACArray[6];
// Set the pinout appropriately for this board.
PinoutSet();
// Enable and Reset the Ethernet Controller.
SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
// PA4(以太網A、B網切換)管腳為輸出(數字功能默認使能,默認2mA驅動)
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4);
// Enable Port F for Ethernet LEDs.
// LED0 Bit 3 Output
// LED1 Bit 2 Output
GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
// Configure the hardware MAC address for Ethernet Controller filtering of
// incoming packets.
ulUser0 = 0x00000080;
ulUser1 = 0x00ACDE48;
// 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.
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);
// Initialze the lwIP library, using STATIC.
lwIPInit(pucMACArray, 0xC0A8010A, 0xFFFFFF00, 0, IPADDR_USE_STATIC); //192.168.1.10
}
/******* 這是一個回調函數,當有UDP數據收到時會被調用********/
// addr:筆記本的IP地址(存放向開發(fā)板發(fā)送數據的PC的IP地址)
// port:筆記本的端口號(遠端端口號)
void UDP_Receive(void *arg, struct udp_pcb *upcb, struct pbuf *p_rx,
struct ip_addr *addr, u16_t port)
{
memset(UDPData, 0, 1020);
if (p_rx != NULL) // 如果收到的數據不為空
{
memcpy(UDPData, (char *)p_rx->payload, p_rx->len);
p_tx->len = p_tx->tot_len = p_rx->len;
IP_Addr_Tmp = UdpPcb->remote_ip;
UdpPcb->remote_ip = *addr; // 獲取筆記本的IP地址(遠端IP地址)
udp_send(UdpPcb, p_tx);
}
UdpPcb->remote_ip = IP_Addr_Tmp;
pbuf_free(p_rx); // 釋放緩沖區(qū)數據
}
// 如果在回調函數中發(fā)送數據,不用connect; 在回調函數外發(fā)送數據必須要connect,否則接收不到數據
void UDP_Test_Init(void)
{
p_tx = pbuf_alloc(PBUF_RAW, sizeof(UDPData), PBUF_RAM); // 按照指定類型分配一個pbuf結構體 // struct pbuf *p_tx;
p_tx->payload = (void *)UDPData; // DI16實際發(fā)送數據內存區(qū)
local_addr.addr = 0x0A01A8C0; // 本地IP地址:192.168.1.10
UdpPcb = udp_new(); // 創(chuàng)建udp協議控制塊
udp_bind(UdpPcb, &local_addr, 1025); // 在協議控制塊中綁定本地ip地址和本地端口號,本地:開發(fā)板(程序下到開發(fā)板中)
udp_connect(UdpPcb, IP_ADDR_ANY, 1025); // 與遠端udp主機建立連接,遠端:筆記本
udp_recv(UdpPcb, UDP_Receive, NULL); // 設置數據接收時的回調函數
}
//*****************************************************************************
//
// 主函數
//
//*****************************************************************************
int main(void)
{
// 系統(tǒng)時鐘初始化,16MHz
SysClk_Init();
// 以太網初始化
ETH_Init();
UDP_Test_Init();
// Enable processor interrupts.
IntMasterEnable();
while (1)
{
}
}
//*****************************************************************************
//
// The interrupt handler for the SysTick interrupt.
//
//*****************************************************************************
void SysTickIntHandler(void)
{
// Call the lwIP timer handler.
lwIPTimer(SYSTICKMS);
}
//*****************************************************************************
//
// Required by lwIP library to support any host-related timer functions.
//
//*****************************************************************************
void lwIPHostTimerHandler(void)
{
}
評論