新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > LM3S9B96 的以太網(wǎng)配置

LM3S9B96 的以太網(wǎng)配置

作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
以太網(wǎng)是TI公司9b96這款芯片特色外設(shè),或者說之所以選擇9b96這款芯片,就是要用它的以太網(wǎng)。但是開發(fā)板給的光盤中有關(guān)以太網(wǎng)的例子,如enet_lwip,初學(xué)者搞不清楚這個(gè)目錄中的這些文件是干什么的,這些文件都需要嗎?
大家可以看出enet_lwip例子文件結(jié)構(gòu)比較混亂,不知道哪些是我們要用到的源文件,不知道哪些是該工程必須用到的庫文件,有些學(xué)生可能自己新建一個(gè)工程,但是編譯時(shí)會(huì)出現(xiàn)錯(cuò)誤,提示找不到包含的頭文件,但又不知道這些頭文件到哪去找?
下面是以太網(wǎng)最簡單的例子,用網(wǎng)絡(luò)助手發(fā)送數(shù)據(jù),可以進(jìn)入到以太網(wǎng)中斷處理函數(shù)中。

/* 頭文件包含區(qū) --------------------------------------------------------------*/
#include "string.h" // strcmp函數(shù)和memset函數(shù)
#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"

/* 數(shù)據(jù)類型定義區(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宏定義 <調(diào)試時(shí)看門狗對應(yīng)的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,網(wǎng)絡(luò)助手時(shí)間戳顯示有問題???
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函數(shù)中,臨時(shí)保存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)時(shí)鐘初始化函數(shù)
//
//*****************************************************************************
void SysClk_Init(void)
{
// 配置系統(tǒng)主時(shí)鐘, 使用外部晶振16M.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
}

//*****************************************************************************
//
// 以太網(wǎng)初始化函數(shù)
//
//*****************************************************************************
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(以太網(wǎng)A、B網(wǎng)切換)管腳為輸出(數(shù)字功能默認(rèn)使能,默認(rèn)2mA驅(qū)動(dòng))
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
}

/******* 這是一個(gè)回調(diào)函數(shù),當(dāng)有UDP數(shù)據(jù)收到時(shí)會(huì)被調(diào)用********/
// addr:筆記本的IP地址(存放向開發(fā)板發(fā)送數(shù)據(jù)的PC的IP地址)
// port:筆記本的端口號(遠(yuǎn)端端口號)
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) // 如果收到的數(shù)據(jù)不為空
{
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地址(遠(yuǎn)端IP地址)
udp_send(UdpPcb, p_tx);
}
UdpPcb->remote_ip = IP_Addr_Tmp;

pbuf_free(p_rx); // 釋放緩沖區(qū)數(shù)據(jù)
}

// 如果在回調(diào)函數(shù)中發(fā)送數(shù)據(jù),不用connect; 在回調(diào)函數(shù)外發(fā)送數(shù)據(jù)必須要connect,否則接收不到數(shù)據(jù)
void UDP_Test_Init(void)
{
p_tx = pbuf_alloc(PBUF_RAW, sizeof(UDPData), PBUF_RAM); // 按照指定類型分配一個(gè)pbuf結(jié)構(gòu)體 // struct pbuf *p_tx;
p_tx->payload = (void *)UDPData; // DI16實(shí)際發(fā)送數(shù)據(jù)內(nèi)存區(qū)

local_addr.addr = 0x0A01A8C0; // 本地IP地址:192.168.1.10

UdpPcb = udp_new(); // 創(chuàng)建udp協(xié)議控制塊
udp_bind(UdpPcb, &local_addr, 1025); // 在協(xié)議控制塊中綁定本地ip地址和本地端口號,本地:開發(fā)板(程序下到開發(fā)板中)
udp_connect(UdpPcb, IP_ADDR_ANY, 1025); // 與遠(yuǎn)端udp主機(jī)建立連接,遠(yuǎn)端:筆記本
udp_recv(UdpPcb, UDP_Receive, NULL); // 設(shè)置數(shù)據(jù)接收時(shí)的回調(diào)函數(shù)
}


//*****************************************************************************
//
// 主函數(shù)
//
//*****************************************************************************
int main(void)
{
// 系統(tǒng)時(shí)鐘初始化,16MHz
SysClk_Init();

// 以太網(wǎng)初始化
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)
{
}

本文引用地址:http://m.butianyuan.cn/article/201611/317012.htm
按照前面講的中斷映射表的配置,將startup_ewarm.c文件中添加兩處代碼。編譯、運(yùn)行即可。



關(guān)鍵詞: LM3S9B96以太網(wǎng)配

評論


技術(shù)專區(qū)

關(guān)閉