Skip to content

Commit c853aed

Browse files
committed
M487, NUC472 EMAC code fulfill astyle
1 parent 16e447e commit c853aed

File tree

3 files changed

+291
-242
lines changed

3 files changed

+291
-242
lines changed

features/netsocket/emac-drivers/TARGET_NUVOTON_EMAC/TARGET_M480/m480_eth.c

Lines changed: 100 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Nuvoton Technology Corp.
2+
* Copyright (c) 2018 Nuvoton Technology Corp.
33
* Copyright (c) 2018 ARM Limited
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,18 +28,18 @@
2828
#define ETH_ENABLE_RX() do{EMAC->CTL |= EMAC_CTL_RXON_Msk;}while(0)
2929
#define ETH_DISABLE_TX() do{EMAC->CTL &= ~EMAC_CTL_TXON;}while(0)
3030
#define ETH_DISABLE_RX() do{EMAC->CTL &= ~EMAC_CTL_RXON_Msk;}while(0)
31-
31+
3232
#define EMAC_ENABLE_INT(emac, u32eIntSel) ((emac)->INTEN |= (u32eIntSel))
33-
#define EMAC_DISABLE_INT(emac, u32eIntSel) ((emac)->INTEN &= ~ (u32eIntSel))
33+
#define EMAC_DISABLE_INT(emac, u32eIntSel) ((emac)->INTEN &= ~ (u32eIntSel))
3434

3535
MBED_ALIGN(4) struct eth_descriptor rx_desc[RX_DESCRIPTOR_NUM];
3636
MBED_ALIGN(4) struct eth_descriptor tx_desc[TX_DESCRIPTOR_NUM];
3737

3838
struct eth_descriptor volatile *cur_tx_desc_ptr, *cur_rx_desc_ptr, *fin_tx_desc_ptr;
3939

40-
__attribute__ ((section("EMAC_RAM")))
40+
__attribute__((section("EMAC_RAM")))
4141
MBED_ALIGN(4) uint8_t rx_buf[RX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE];
42-
__attribute__ ((section("EMAC_RAM")))
42+
__attribute__((section("EMAC_RAM")))
4343
MBED_ALIGN(4) uint8_t tx_buf[TX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE];
4444

4545
eth_callback_t nu_eth_txrx_cb = NULL;
@@ -74,7 +74,7 @@ static uint16_t mdio_read(uint8_t addr, uint8_t reg)
7474
EMAC->MIIMCTL = (addr << EMAC_MIIMCTL_PHYADDR_Pos) | reg | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_MDCON_Msk;
7575
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk);
7676

77-
return(EMAC->MIIMDAT);
77+
return (EMAC->MIIMDAT);
7878
}
7979

8080
static int reset_phy(void)
@@ -87,15 +87,18 @@ static int reset_phy(void)
8787
mdio_write(CONFIG_PHY_ADDR, MII_BMCR, BMCR_RESET);
8888

8989
delayCnt = 2000;
90-
while(delayCnt-- > 0) {
91-
if((mdio_read(CONFIG_PHY_ADDR, MII_BMCR) & BMCR_RESET) == 0)
90+
91+
while (delayCnt > 0) {
92+
delayCnt--;
93+
if ((mdio_read(CONFIG_PHY_ADDR, MII_BMCR) & BMCR_RESET) == 0) {
9294
break;
95+
}
9396

9497
}
9598

96-
if(delayCnt == 0) {
99+
if (delayCnt == 0) {
97100
NU_DEBUGF(("Reset phy failed\n"));
98-
return(-1);
101+
return (-1);
99102
}
100103

101104
mdio_write(CONFIG_PHY_ADDR, MII_ADVERTISE, ADVERTISE_CSMA |
@@ -108,38 +111,41 @@ static int reset_phy(void)
108111
mdio_write(CONFIG_PHY_ADDR, MII_BMCR, reg | BMCR_ANRESTART);
109112

110113
delayCnt = 200000;
111-
while(delayCnt-- > 0) {
112-
if((mdio_read(CONFIG_PHY_ADDR, MII_BMSR) & (BMSR_ANEGCOMPLETE | BMSR_LSTATUS))
113-
== (BMSR_ANEGCOMPLETE | BMSR_LSTATUS))
114+
115+
while (delayCnt > 0) {
116+
delayCnt--;
117+
if ((mdio_read(CONFIG_PHY_ADDR, MII_BMSR) & (BMSR_ANEGCOMPLETE | BMSR_LSTATUS))
118+
== (BMSR_ANEGCOMPLETE | BMSR_LSTATUS)) {
114119
break;
120+
}
115121
}
116122

117-
if(delayCnt == 0) {
123+
if (delayCnt == 0) {
118124
NU_DEBUGF(("AN failed. Set to 100 FULL\n"));
119125
EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
120-
return(-1);
126+
return (-1);
121127
} else {
122128
reg = mdio_read(CONFIG_PHY_ADDR, MII_LPA);
123129
phyLPAval = reg;
124130

125-
if(reg & ADVERTISE_100FULL) {
131+
if (reg & ADVERTISE_100FULL) {
126132
NU_DEBUGF(("100 full\n"));
127133
EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
128-
} else if(reg & ADVERTISE_100HALF) {
134+
} else if (reg & ADVERTISE_100HALF) {
129135
NU_DEBUGF(("100 half\n"));
130136
EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_FUDUP_Msk) | EMAC_CTL_OPMODE_Msk;
131-
} else if(reg & ADVERTISE_10FULL) {
137+
} else if (reg & ADVERTISE_10FULL) {
132138
NU_DEBUGF(("10 full\n"));
133139
EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_OPMODE_Msk) | EMAC_CTL_FUDUP_Msk;
134140
} else {
135141
NU_DEBUGF(("10 half\n"));
136142
EMAC->CTL &= ~(EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
137143
}
138144
}
139-
printf("PHY ID 1:0x%x\r\n", mdio_read(CONFIG_PHY_ADDR, MII_PHYSID1));
140-
printf("PHY ID 2:0x%x\r\n", mdio_read(CONFIG_PHY_ADDR, MII_PHYSID2));
145+
printf("PHY ID 1:0x%x\r\n", mdio_read(CONFIG_PHY_ADDR, MII_PHYSID1));
146+
printf("PHY ID 2:0x%x\r\n", mdio_read(CONFIG_PHY_ADDR, MII_PHYSID2));
141147

142-
return(0);
148+
return (0);
143149
}
144150

145151

@@ -150,7 +156,7 @@ static void init_tx_desc(void)
150156

151157
cur_tx_desc_ptr = fin_tx_desc_ptr = &tx_desc[0];
152158

153-
for(i = 0; i < TX_DESCRIPTOR_NUM; i++) {
159+
for (i = 0; i < TX_DESCRIPTOR_NUM; i++) {
154160
tx_desc[i].status1 = TXFD_PADEN | TXFD_CRCAPP | TXFD_INTEN;
155161
tx_desc[i].buf = &tx_buf[i][0];
156162
tx_desc[i].status2 = 0;
@@ -168,7 +174,7 @@ static void init_rx_desc(void)
168174

169175
cur_rx_desc_ptr = &rx_desc[0];
170176

171-
for(i = 0; i < RX_DESCRIPTOR_NUM; i++) {
177+
for (i = 0; i < RX_DESCRIPTOR_NUM; i++) {
172178
rx_desc[i].status1 = OWNERSHIP_EMAC;
173179
rx_desc[i].buf = &rx_buf[i][0];
174180
rx_desc[i].status2 = 0;
@@ -199,13 +205,13 @@ static void __eth_clk_pin_init()
199205

200206
/* Enable IP clock */
201207
CLK_EnableModuleClock(EMAC_MODULE);
202-
208+
203209
// Configure MDC clock rate to HCLK / (127 + 1) = 1.25 MHz if system is running at 160 MH
204210
CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));
205-
211+
206212
/* Update System Core Clock */
207213
SystemCoreClockUpdate();
208-
214+
209215
/*---------------------------------------------------------------------------------------------------------*/
210216
/* Init I/O Multi-function */
211217
/*---------------------------------------------------------------------------------------------------------*/
@@ -219,10 +225,10 @@ static void __eth_clk_pin_init()
219225
SYS->GPE_MFPH &= ~(SYS_GPE_MFPH_PE8MFP_Msk | SYS_GPE_MFPH_PE9MFP_Msk | SYS_GPE_MFPH_PE10MFP_Msk |
220226
SYS_GPE_MFPH_PE11MFP_Msk | SYS_GPE_MFPH_PE12MFP_Msk);
221227
SYS->GPE_MFPH |= SYS_GPE_MFPH_PE8MFP_EMAC_RMII_MDC |
222-
SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
223-
SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
224-
SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
225-
SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;
228+
SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
229+
SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
230+
SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
231+
SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;
226232

227233
// Enable high slew rate on all RMII TX output pins
228234
PE->SLEWCTL = (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN10_Pos) |
@@ -238,13 +244,13 @@ static void __eth_clk_pin_init()
238244

239245
void numaker_eth_init(uint8_t *mac_addr)
240246
{
241-
242-
// init CLK & pins
243-
__eth_clk_pin_init();
244-
247+
248+
// init CLK & pins
249+
__eth_clk_pin_init();
250+
245251
// Reset MAC
246252
EMAC->CTL = EMAC_CTL_RST_Msk;
247-
while(EMAC->CTL & EMAC_CTL_RST_Msk) {}
253+
while (EMAC->CTL & EMAC_CTL_RST_Msk) {}
248254

249255
init_tx_desc();
250256
init_rx_desc();
@@ -270,17 +276,15 @@ void numaker_eth_init(uint8_t *mac_addr)
270276
EMAC->CAMCTL = EMAC_CAMCTL_CMPEN_Msk |
271277
EMAC_CAMCTL_AMP_Msk |
272278
EMAC_CAMCTL_ABP_Msk;
273-
EMAC->CAMEN = 1; // Enable CAM entry 0
279+
EMAC->CAMEN = 1; // Enable CAM entry 0
274280
/* Limit the max receive frame length to 1514 + 4 */
275281
EMAC->MRFL = NU_ETH_MAX_FLEN;
276-
282+
277283
/* Set RX FIFO threshold as 8 words */
278284
EMAC->FIFOCTL = 0x00200100;
279-
280-
if (isPhyReset != true)
281-
{
282-
if (!reset_phy())
283-
{
285+
286+
if (isPhyReset != true) {
287+
if (!reset_phy()) {
284288
isPhyReset = true;
285289
}
286290
} else {
@@ -296,9 +300,9 @@ void numaker_eth_init(uint8_t *mac_addr)
296300
} else {
297301
NU_DEBUGF(("10 half\n"));
298302
EMAC->CTL &= ~(EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
299-
}
303+
}
300304
}
301-
305+
302306
EMAC_ENABLE_RX();
303307
EMAC_ENABLE_TX();
304308

@@ -321,11 +325,15 @@ void EMAC_RX_IRQHandler(void)
321325
if (m_status & EMAC_INTSTS_RXBEIF_Msk) {
322326
// Shouldn't goes here, unless descriptor corrupted
323327
mbed_error_printf("### RX Bus error [0x%x]\r\n", m_status);
324-
if (nu_eth_txrx_cb != NULL) nu_eth_txrx_cb('B', nu_userData);
328+
if (nu_eth_txrx_cb != NULL) {
329+
nu_eth_txrx_cb('B', nu_userData);
330+
}
325331
return;
326332
}
327333
EMAC_DISABLE_INT(EMAC, (EMAC_INTEN_RDUIEN_Msk | EMAC_INTEN_RXGDIEN_Msk));
328-
if (nu_eth_txrx_cb != NULL) nu_eth_txrx_cb('R', nu_userData);
334+
if (nu_eth_txrx_cb != NULL) {
335+
nu_eth_txrx_cb('R', nu_userData);
336+
}
329337
}
330338

331339

@@ -340,42 +348,48 @@ int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
340348
unsigned int cur_entry, status;
341349

342350
cur_entry = EMAC->CRXDSA;
343-
if ((cur_entry == (uint32_t)cur_rx_desc_ptr) && (!(m_status & EMAC_INTSTS_RDUIF_Msk))) // cur_entry may equal to cur_rx_desc_ptr if RDU occures
344-
return -1;
351+
if ((cur_entry == (uint32_t)cur_rx_desc_ptr) && (!(m_status & EMAC_INTSTS_RDUIF_Msk))) { // cur_entry may equal to cur_rx_desc_ptr if RDU occures
352+
return -1;
353+
}
345354
status = cur_rx_desc_ptr->status1;
346355

347-
if(status & OWNERSHIP_EMAC)
348-
return -1;
356+
if (status & OWNERSHIP_EMAC) {
357+
return -1;
358+
}
349359

350360
if (status & RXFD_RXGD) {
351361
*buf = cur_rx_desc_ptr->buf;
352362
*len = status & 0xFFFF;
353363
// length of payload should be <= 1514
354-
if ( *len > (NU_ETH_MAX_FLEN - 4) ) {
364+
if (*len > (NU_ETH_MAX_FLEN - 4)) {
355365
NU_DEBUGF(("%s... unexpected long packet length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf));
356366
*len = 0; // Skip this unexpected long packet
357367
}
358-
if (*len == (NU_ETH_MAX_FLEN - 4)) NU_DEBUGF(("%s... length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf));
368+
if (*len == (NU_ETH_MAX_FLEN - 4)) {
369+
NU_DEBUGF(("%s... length=%d, buf=0x%x\r\n", __FUNCTION__, *len, *buf));
370+
}
359371
}
360372
return 0;
361-
}
373+
}
362374

363375
void numaker_eth_rx_next(void)
364376
{
365377
cur_rx_desc_ptr->status1 = OWNERSHIP_EMAC;
366-
cur_rx_desc_ptr = cur_rx_desc_ptr->next;
367-
}
378+
cur_rx_desc_ptr = cur_rx_desc_ptr->next;
379+
}
368380

369381
void EMAC_TX_IRQHandler(void)
370382
{
371383
unsigned int cur_entry, status;
372384

373385
status = EMAC->INTSTS & 0xFFFF0000;
374386
EMAC->INTSTS = status;
375-
if(status & EMAC_INTSTS_TXBEIF_Msk) {
387+
if (status & EMAC_INTSTS_TXBEIF_Msk) {
376388
// Shouldn't goes here, unless descriptor corrupted
377389
mbed_error_printf("### TX Bus error [0x%x]\r\n", status);
378-
if (nu_eth_txrx_cb != NULL) nu_eth_txrx_cb('B', nu_userData);
390+
if (nu_eth_txrx_cb != NULL) {
391+
nu_eth_txrx_cb('B', nu_userData);
392+
}
379393
return;
380394
}
381395

@@ -385,16 +399,19 @@ void EMAC_TX_IRQHandler(void)
385399

386400
fin_tx_desc_ptr = fin_tx_desc_ptr->next;
387401
}
388-
389-
if (nu_eth_txrx_cb != NULL) nu_eth_txrx_cb('T', nu_userData);
402+
403+
if (nu_eth_txrx_cb != NULL) {
404+
nu_eth_txrx_cb('T', nu_userData);
405+
}
390406
}
391407

392408
uint8_t *numaker_eth_get_tx_buf(void)
393409
{
394-
if(cur_tx_desc_ptr->status1 & OWNERSHIP_EMAC)
395-
return(NULL);
396-
else
397-
return(cur_tx_desc_ptr->buf);
410+
if (cur_tx_desc_ptr->status1 & OWNERSHIP_EMAC) {
411+
return (NULL);
412+
} else {
413+
return (cur_tx_desc_ptr->buf);
414+
}
398415
}
399416

400417
void numaker_eth_trigger_tx(uint16_t length, void *p)
@@ -411,11 +428,12 @@ void numaker_eth_trigger_tx(uint16_t length, void *p)
411428

412429
int numaker_eth_link_ok(void)
413430
{
414-
/* first, a dummy read to latch */
415-
mdio_read(CONFIG_PHY_ADDR, MII_BMSR);
416-
if(mdio_read(CONFIG_PHY_ADDR, MII_BMSR) & BMSR_LSTATUS)
417-
return 1;
418-
return 0;
431+
/* first, a dummy read to latch */
432+
mdio_read(CONFIG_PHY_ADDR, MII_BMSR);
433+
if (mdio_read(CONFIG_PHY_ADDR, MII_BMSR) & BMSR_LSTATUS) {
434+
return 1;
435+
}
436+
return 0;
419437
}
420438

421439
void numaker_eth_set_cb(eth_callback_t eth_cb, void *userData)
@@ -437,43 +455,44 @@ void mbed_mac_address(char *mac)
437455
// http://en.wikipedia.org/wiki/MAC_address
438456
uint32_t word1 = *(uint32_t *)0x7F800; // 2KB Data Flash at 0x7F800
439457

440-
if( word0 == 0xFFFFFFFF ) // Not burn any mac address at 1st 2 words of Data Flash
441-
{
458+
if (word0 == 0xFFFFFFFF) { // Not burn any mac address at 1st 2 words of Data Flash
442459
// with a semi-unique MAC address from the UUID
443460
/* Enable FMC ISP function */
444461
SYS_UnlockReg();
445462
FMC_Open();
446463
// = FMC_ReadUID(0);
447464
uID1 = FMC_ReadUID(1);
448465
word1 = (uID1 & 0x003FFFFF) | ((uID1 & 0x030000) << 6) >> 8;
449-
word0 = ((FMC_ReadUID(0) >> 4) << 20) | ((uID1 & 0xFF)<<12) | (FMC_ReadUID(2) & 0xFFF);
466+
word0 = ((FMC_ReadUID(0) >> 4) << 20) | ((uID1 & 0xFF) << 12) | (FMC_ReadUID(2) & 0xFFF);
450467
/* Disable FMC ISP function */
451468
FMC_Close();
452469
/* Lock protected registers */
453470
SYS_LockReg();
454-
}
471+
}
455472

456473
word1 |= 0x00000200;
457474
word1 &= 0x0000FEFF;
458475

459-
mac[0] = (word1 & 0x0000ff00) >> 8;
476+
mac[0] = (word1 & 0x0000ff00) >> 8;
460477
mac[1] = (word1 & 0x000000ff);
461478
mac[2] = (word0 & 0xff000000) >> 24;
462479
mac[3] = (word0 & 0x00ff0000) >> 16;
463480
mac[4] = (word0 & 0x0000ff00) >> 8;
464481
mac[5] = (word0 & 0x000000ff);
465-
466-
NU_DEBUGF(("mac address %02x-%02x-%02x-%02x-%02x-%02x \r\n", mac[0], mac[1],mac[2],mac[3],mac[4],mac[5]));
482+
483+
NU_DEBUGF(("mac address %02x-%02x-%02x-%02x-%02x-%02x \r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]));
467484
}
468485

469-
void numaker_eth_enable_interrupts(void) {
486+
void numaker_eth_enable_interrupts(void)
487+
{
470488
EMAC->INTEN |= EMAC_INTEN_RXIEN_Msk |
471489
EMAC_INTEN_TXIEN_Msk ;
472-
NVIC_EnableIRQ(EMAC_RX_IRQn);
473-
NVIC_EnableIRQ(EMAC_TX_IRQn);
490+
NVIC_EnableIRQ(EMAC_RX_IRQn);
491+
NVIC_EnableIRQ(EMAC_TX_IRQn);
474492
}
475493

476-
void numaker_eth_disable_interrupts(void) {
477-
NVIC_DisableIRQ(EMAC_RX_IRQn);
478-
NVIC_DisableIRQ(EMAC_TX_IRQn);
494+
void numaker_eth_disable_interrupts(void)
495+
{
496+
NVIC_DisableIRQ(EMAC_RX_IRQn);
497+
NVIC_DisableIRQ(EMAC_TX_IRQn);
479498
}

0 commit comments

Comments
 (0)