Modify code format

This commit is contained in:
dejavudwh 2023-06-14 16:42:00 +08:00 committed by Man, Jianting (Meco)
parent 0131018884
commit 49ae4ab614
1 changed files with 119 additions and 118 deletions

View File

@ -19,19 +19,19 @@
#include "lwipopts.h" #include "lwipopts.h"
#include "luminaryif.h" #include "luminaryif.h"
#define MAX_ADDR_LEN 6 #define MAX_ADDR_LEN 6
struct net_device struct net_device
{ {
/* inherit from ethernet device */ /* inherit from ethernet device */
struct eth_device parent; struct eth_device parent;
/* interface address info. */ /* interface address info. */
rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */ rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
}; };
static struct net_device luminaryif_dev_entry; static struct net_device luminaryif_dev_entry;
static struct net_device *luminaryif_dev =&luminaryif_dev_entry; static struct net_device *luminaryif_dev = &luminaryif_dev_entry;
static struct rt_semaphore tx_sem; static struct rt_semaphore tx_sem;
//***************************************************************************** //*****************************************************************************
@ -101,7 +101,7 @@ rt_err_t luminaryif_init(rt_device_t dev)
// //
EthernetIntEnable(ETH_BASE, ETH_INT_RX | ETH_INT_TX); EthernetIntEnable(ETH_BASE, ETH_INT_RX | ETH_INT_TX);
return RT_EOK; return RT_EOK;
} }
void luminaryif_isr(void) void luminaryif_isr(void)
@ -117,74 +117,76 @@ void luminaryif_isr(void)
// //
// Check to see if an RX Interrupt has occured. // Check to see if an RX Interrupt has occured.
// //
if(ulTemp & ETH_INT_RX) if (ulTemp & ETH_INT_RX)
{ {
// //
// Indicate that a packet has been received. // Indicate that a packet has been received.
// //
rt_err_t result; rt_err_t result;
/* a frame has been received */
result = eth_device_ready((struct eth_device*)&(luminaryif_dev->parent));
if(result != RT_EOK) rt_set_errno(-RT_ERROR); /* a frame has been received */
result = eth_device_ready((struct eth_device *)&(luminaryif_dev->parent));
if (result != RT_EOK)
rt_set_errno(-RT_ERROR);
// //
// Disable Ethernet RX Interrupt. // Disable Ethernet RX Interrupt.
// //
EthernetIntDisable(ETH_BASE, ETH_INT_RX); EthernetIntDisable(ETH_BASE, ETH_INT_RX);
} }
if(ulTemp & ETH_INT_TX) if (ulTemp & ETH_INT_TX)
{ {
/* A frame has been transmitted. */ /* A frame has been transmitted. */
rt_sem_release(&tx_sem); rt_sem_release(&tx_sem);
} }
} }
/* control the interface */ /* control the interface */
rt_err_t luminaryif_control(rt_device_t dev, int cmd, void *args) rt_err_t luminaryif_control(rt_device_t dev, int cmd, void *args)
{ {
switch(cmd) switch (cmd)
{ {
case NIOCTL_GADDR: case NIOCTL_GADDR:
/* get mac address */ /* get mac address */
if(args) rt_memcpy(args, luminaryif_dev_entry.dev_addr, 6); if (args)
else return -RT_ERROR; rt_memcpy(args, luminaryif_dev_entry.dev_addr, 6);
break; else
return -RT_ERROR;
break;
default : default:
break; break;
} }
return RT_EOK; return RT_EOK;
} }
/* Open the ethernet interface */ /* Open the ethernet interface */
rt_err_t luminaryif_open(rt_device_t dev, rt_uint16_t oflag) rt_err_t luminaryif_open(rt_device_t dev, rt_uint16_t oflag)
{ {
return RT_EOK; return RT_EOK;
} }
/* Close the interface */ /* Close the interface */
rt_err_t luminaryif_close(rt_device_t dev) rt_err_t luminaryif_close(rt_device_t dev)
{ {
return RT_EOK; return RT_EOK;
} }
/* Read */ /* Read */
rt_size_t luminaryif_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) rt_size_t luminaryif_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{ {
rt_set_errno(-RT_ENOSYS); rt_set_errno(-RT_ENOSYS);
return 0; return 0;
} }
/* Write */ /* Write */
rt_size_t luminaryif_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) rt_size_t luminaryif_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{ {
rt_set_errno(-RT_ENOSYS); rt_set_errno(-RT_ENOSYS);
return 0; return 0;
} }
//**************************************************************************** //****************************************************************************
// //
@ -202,19 +204,19 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
int iGather; int iGather;
unsigned long ulGather; unsigned long ulGather;
unsigned char *pucGather; unsigned char *pucGather;
unsigned long ulTemp; unsigned long ulTemp;
/* lock tx operation */ /* lock tx operation */
rt_sem_take(&tx_sem, RT_WAITING_FOREVER); rt_sem_take(&tx_sem, RT_WAITING_FOREVER);
// //
// Wait for space available in the TX FIFO. // Wait for space available in the TX FIFO.
// //
while(!EthernetSpaceAvail(ETH_BASE)) while (!EthernetSpaceAvail(ETH_BASE))
{ {
} }
// //
// Fill in the first two bytes of the payload data (configured as padding // Fill in the first two bytes of the payload data (configured as padding
// with ETH_PAD_SIZE = 2) with the total length of the payload data // with ETH_PAD_SIZE = 2) with the total length of the payload data
// (minus the Ethernet MAC layer header). // (minus the Ethernet MAC layer header).
@ -231,7 +233,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// //
// Copy data from the pbuf(s) into the TX Fifo. // Copy data from the pbuf(s) into the TX Fifo.
// //
for(q = p; q != NULL; q = q->next) for (q = p; q != NULL; q = q->next)
{ {
// //
// Intialize a char pointer and index to the pbuf payload data. // Intialize a char pointer and index to the pbuf payload data.
@ -243,7 +245,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// If the gather buffer has leftover data from a previous pbuf // If the gather buffer has leftover data from a previous pbuf
// in the chain, fill it up and write it to the Tx FIFO. // in the chain, fill it up and write it to the Tx FIFO.
// //
while((iBuf < q->len) && (iGather != 0)) while ((iBuf < q->len) && (iGather != 0))
{ {
// //
// Copy a byte from the pbuf into the gather buffer. // Copy a byte from the pbuf into the gather buffer.
@ -260,7 +262,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// If the gather index is 0 and the pbuf index is non-zero, // If the gather index is 0 and the pbuf index is non-zero,
// we have a gather buffer to write into the Tx FIFO. // we have a gather buffer to write into the Tx FIFO.
// //
if((iGather == 0) && (iBuf != 0)) if ((iGather == 0) && (iBuf != 0))
{ {
HWREG(ETH_BASE + MAC_O_DATA) = ulGather; HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
ulGather = 0; ulGather = 0;
@ -270,14 +272,14 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// Copy words of pbuf data into the Tx FIFO, but don't go past // Copy words of pbuf data into the Tx FIFO, but don't go past
// the end of the pbuf. // the end of the pbuf.
// //
if((iBuf % 4) != 0) if ((iBuf % 4) != 0)
{ {
while((iBuf + 4) <= q->len) while ((iBuf + 4) <= q->len)
{ {
ulTemp = (pucBuf[iBuf++] << 0); ulTemp = (pucBuf[iBuf++] << 0);
ulTemp |= (pucBuf[iBuf++] << 8); ulTemp |= (pucBuf[iBuf++] << 8);
ulTemp |= (pucBuf[iBuf++] << 16); ulTemp |= (pucBuf[iBuf++] << 16);
ulTemp |= (pucBuf[iBuf++] << 24); ulTemp |= (pucBuf[iBuf++] << 24);
HWREG(ETH_BASE + MAC_O_DATA) = ulTemp; HWREG(ETH_BASE + MAC_O_DATA) = ulTemp;
} }
} }
@ -288,7 +290,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// //
pulBuf = (unsigned long *)&pucBuf[iBuf]; pulBuf = (unsigned long *)&pucBuf[iBuf];
while((iBuf + 4) <= q->len) while ((iBuf + 4) <= q->len)
{ {
HWREG(ETH_BASE + MAC_O_DATA) = *pulBuf++; HWREG(ETH_BASE + MAC_O_DATA) = *pulBuf++;
iBuf += 4; iBuf += 4;
@ -298,7 +300,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// Check if leftover data in the pbuf and save it in the gather // Check if leftover data in the pbuf and save it in the gather
// buffer for the next time. // buffer for the next time.
// //
while(iBuf < q->len) while (iBuf < q->len)
{ {
// //
// Copy a byte from the pbuf into the gather buffer. // Copy a byte from the pbuf into the gather buffer.
@ -326,7 +328,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
lwip_stats.link.xmit++; lwip_stats.link.xmit++;
#endif #endif
return(ERR_OK); return (ERR_OK);
} }
//***************************************************************************** //*****************************************************************************
@ -335,7 +337,7 @@ rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
// of the incoming packet from the interface into the pbuf. // of the incoming packet from the interface into the pbuf.
// //
//***************************************************************************** //*****************************************************************************
struct pbuf * luminaryif_rx(rt_device_t dev) struct pbuf *luminaryif_rx(rt_device_t dev)
{ {
struct pbuf *p, *q; struct pbuf *p, *q;
u16_t len; u16_t len;
@ -343,16 +345,16 @@ struct pbuf * luminaryif_rx(rt_device_t dev)
int i; int i;
unsigned long *ptr; unsigned long *ptr;
if(!EthernetPacketAvail(ETH_BASE)) if (!EthernetPacketAvail(ETH_BASE))
{ {
// //
// Enable Ethernet RX Interrupt. // Enable Ethernet RX Interrupt.
// //
EthernetIntEnable(ETH_BASE, ETH_INT_RX); EthernetIntEnable(ETH_BASE, ETH_INT_RX);
return(NULL); return (NULL);
} }
// //
// Obtain the size of the packet and put it into the "len" variable. // Obtain the size of the packet and put it into the "len" variable.
// Note: The length returned in the FIFO length position includes the // Note: The length returned in the FIFO length position includes the
@ -366,7 +368,7 @@ struct pbuf * luminaryif_rx(rt_device_t dev)
// //
p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM); p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM);
if(p != NULL) if (p != NULL)
{ {
// //
// Place the first word into the first pbuf location. // Place the first word into the first pbuf location.
@ -379,7 +381,7 @@ struct pbuf * luminaryif_rx(rt_device_t dev)
// Process all but the last buffer in the pbuf chain. // Process all but the last buffer in the pbuf chain.
// //
q = p; q = p;
while(q != NULL) while (q != NULL)
{ {
// //
// Setup a byte pointer into the payload section of the pbuf. // Setup a byte pointer into the payload section of the pbuf.
@ -390,7 +392,7 @@ struct pbuf * luminaryif_rx(rt_device_t dev)
// Read data from FIFO into the current pbuf // Read data from FIFO into the current pbuf
// (assume pbuf length is modulo 4) // (assume pbuf length is modulo 4)
// //
for(i = 0; i < q->len; i += 4) for (i = 0; i < q->len; i += 4)
{ {
*ptr++ = HWREG(ETH_BASE + MAC_O_DATA); *ptr++ = HWREG(ETH_BASE + MAC_O_DATA);
} }
@ -416,7 +418,7 @@ struct pbuf * luminaryif_rx(rt_device_t dev)
// //
// Just read all of the remaining data from the FIFO and dump it. // Just read all of the remaining data from the FIFO and dump it.
// //
for(i = 4; i < len; i+=4) for (i = 4; i < len; i += 4)
{ {
ulTemp = HWREG(ETH_BASE + MAC_O_DATA); ulTemp = HWREG(ETH_BASE + MAC_O_DATA);
} }
@ -425,71 +427,70 @@ struct pbuf * luminaryif_rx(rt_device_t dev)
lwip_stats.link.memerr++; lwip_stats.link.memerr++;
lwip_stats.link.drop++; lwip_stats.link.drop++;
#endif #endif
// //
// Enable Ethernet RX Interrupt. // Enable Ethernet RX Interrupt.
// //
EthernetIntEnable(ETH_BASE, ETH_INT_RX); EthernetIntEnable(ETH_BASE, ETH_INT_RX);
} }
return(p); return (p);
} }
int rt_hw_luminaryif_init(void) int rt_hw_luminaryif_init(void)
{ {
rt_err_t result; rt_err_t result;
unsigned long ulUser0, ulUser1; unsigned long ulUser0, ulUser1;
/* Enable and Reset the Ethernet Controller. */ /* Enable and Reset the Ethernet Controller. */
SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH); SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH); SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
/* /*
Enable Port F for Ethernet LEDs. Enable Port F for Ethernet LEDs.
LED0 Bit 3 Output LED0 Bit 3 Output
LED1 Bit 2 Output LED1 Bit 2 Output
*/ */
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_DIR_MODE_HW); GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_DIR_MODE_HW);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3,
GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD); GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
FlashUserSet(0x12345678, 0x12345678); FlashUserSet(0x12345678, 0x12345678);
/* Configure the hardware MAC address */ /* Configure the hardware MAC address */
FlashUserGet(&ulUser0, &ulUser1); FlashUserGet(&ulUser0, &ulUser1);
if((ulUser0 == 0xffffffff) || (ulUser1 == 0xffffffff)) if ((ulUser0 == 0xffffffff) || (ulUser1 == 0xffffffff))
{ {
rt_kprintf("Fatal error in geting MAC address\n"); rt_kprintf("Fatal error in geting MAC address\n");
} }
/* init rt-thread device interface */ /* init rt-thread device interface */
luminaryif_dev_entry.parent.parent.init = luminaryif_init; luminaryif_dev_entry.parent.parent.init = luminaryif_init;
luminaryif_dev_entry.parent.parent.open = luminaryif_open; luminaryif_dev_entry.parent.parent.open = luminaryif_open;
luminaryif_dev_entry.parent.parent.close = luminaryif_close; luminaryif_dev_entry.parent.parent.close = luminaryif_close;
luminaryif_dev_entry.parent.parent.read = luminaryif_read; luminaryif_dev_entry.parent.parent.read = luminaryif_read;
luminaryif_dev_entry.parent.parent.write = luminaryif_write; luminaryif_dev_entry.parent.parent.write = luminaryif_write;
luminaryif_dev_entry.parent.parent.control = luminaryif_control; luminaryif_dev_entry.parent.parent.control = luminaryif_control;
luminaryif_dev_entry.parent.eth_rx = luminaryif_rx; luminaryif_dev_entry.parent.eth_rx = luminaryif_rx;
luminaryif_dev_entry.parent.eth_tx = luminaryif_tx; luminaryif_dev_entry.parent.eth_tx = luminaryif_tx;
/*
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.
*/
luminaryif_dev_entry.dev_addr[0] = ((ulUser0 >> 0) & 0xff);
luminaryif_dev_entry.dev_addr[1] = ((ulUser0 >> 8) & 0xff);
luminaryif_dev_entry.dev_addr[2] = ((ulUser0 >> 16) & 0xff);
luminaryif_dev_entry.dev_addr[3] = ((ulUser1 >> 0) & 0xff);
luminaryif_dev_entry.dev_addr[4] = ((ulUser1 >> 8) & 0xff);
luminaryif_dev_entry.dev_addr[5] = ((ulUser1 >> 16) & 0xff);
/* Program the hardware with it's MAC address (for filtering). */ /*
EthernetMACAddrSet(ETH_BASE, luminaryif_dev_entry.dev_addr); 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
rt_sem_init(&tx_sem, "emac", 1, RT_IPC_FLAG_FIFO); address into the Ethernet Controller registers.
*/
luminaryif_dev_entry.dev_addr[0] = ((ulUser0 >> 0) & 0xff);
luminaryif_dev_entry.dev_addr[1] = ((ulUser0 >> 8) & 0xff);
luminaryif_dev_entry.dev_addr[2] = ((ulUser0 >> 16) & 0xff);
luminaryif_dev_entry.dev_addr[3] = ((ulUser1 >> 0) & 0xff);
luminaryif_dev_entry.dev_addr[4] = ((ulUser1 >> 8) & 0xff);
luminaryif_dev_entry.dev_addr[5] = ((ulUser1 >> 16) & 0xff);
result = eth_device_init(&(luminaryif_dev->parent), "E0"); /* Program the hardware with it's MAC address (for filtering). */
EthernetMACAddrSet(ETH_BASE, luminaryif_dev_entry.dev_addr);
return result;
rt_sem_init(&tx_sem, "emac", 1, RT_IPC_FLAG_FIFO);
result = eth_device_init(&(luminaryif_dev->parent), "E0");
return result;
} }