Skip to content

Add CompositeEMAC support for Nuvoton M480 #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

multiplemonomials
Copy link
Collaborator

Summary of changes

Now that I have an M480 board, I figured I would try and upgrade its Ethernet implementation to use the new CompositeEMAC structure. This lets the target-specific part of the code be as small as possible, as most of the work is done in the generic EMAC, PHY, and DMA drivers.

The old driver worked fine, and passed nearly all of the network tests. As far as I could tell, the only omissions were power-down support and multicast support. However, using CompositeEMAC brings new features, such as zero-copy Rx, which should help the code run a little faster. (though sadly zero copy Tx isn't really possible on this device since the hardware doesn't support sending multiple buffers at a time) Also, it just removes duplicated code and helps more of the Ethernet drivers use the same common core.

Impact of changes

Nuvoton M480 Ethernet support now implemented using CompositeEMAC.

Migration actions required

None

Documentation


Pull request type

[] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[X] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[X] Tests / results supplied as part of this PR
Test project /home/jamie/Mbed/mbed-os/build/NUMAKER_PFM_M487-Develop
      Start  96: test-mbed-connectivity-mbedtls-multi
 1/18 Test  #96: test-mbed-connectivity-mbedtls-multi ...........................   Passed    9.42 sec
      Start  97: test-mbed-connectivity-mbedtls-sanity
 2/18 Test  #97: test-mbed-connectivity-mbedtls-sanity ..........................***Skipped   0.01 sec
      Start  98: test-mbed-connectivity-mbedtls-selftest
 3/18 Test  #98: test-mbed-connectivity-mbedtls-selftest ........................***Skipped   0.01 sec
      Start  99: test-mbed-connectivity-nfc-eeprom
 4/18 Test  #99: test-mbed-connectivity-nfc-eeprom ..............................***Skipped   0.01 sec
      Start 100: test-mbed-connectivity-netsocket-lwipstack-dns
 5/18 Test #100: test-mbed-connectivity-netsocket-lwipstack-dns .................   Passed   34.52 sec
      Start 101: test-mbed-connectivity-netsocket-nanostack-dns
 6/18 Test #101: test-mbed-connectivity-netsocket-nanostack-dns .................   Passed   33.97 sec
      Start 102: test-mbed-connectivity-netsocket-nidd
 7/18 Test #102: test-mbed-connectivity-netsocket-nidd ..........................***Skipped   0.01 sec
      Start 103: test-mbed-connectivity-netsocket-lwipstack-tcp
 8/18 Test #103: test-mbed-connectivity-netsocket-lwipstack-tcp .................   Passed   68.45 sec
      Start 104: test-mbed-connectivity-netsocket-nanostack-tcp
 9/18 Test #104: test-mbed-connectivity-netsocket-nanostack-tcp .................   Passed   57.28 sec
      Start 105: test-mbed-connectivity-netsocket-lwipstack-tls
10/18 Test #105: test-mbed-connectivity-netsocket-lwipstack-tls .................***Skipped   0.01 sec
      Start 106: test-mbed-connectivity-netsocket-nanostack-tls
11/18 Test #106: test-mbed-connectivity-netsocket-nanostack-tls .................***Skipped   0.01 sec
      Start 107: test-mbed-connectivity-netsocket-lwipstack-udp
12/18 Test #107: test-mbed-connectivity-netsocket-lwipstack-udp .................   Passed   40.00 sec
      Start 108: test-mbed-connectivity-netsocket-nanostack-udp
13/18 Test #108: test-mbed-connectivity-netsocket-nanostack-udp .................   Passed   41.61 sec
      Start 109: test-mbed-connectivity-network-emac
14/18 Test #109: test-mbed-connectivity-network-emac ............................   Passed  179.86 sec
      Start 110: test-mbed-connectivity-netsocket-lwipstack-network-interface
15/18 Test #110: test-mbed-connectivity-netsocket-lwipstack-network-interface ...   Passed   41.75 sec
      Start 111: test-mbed-connectivity-netsocket-nanostack-network-interface
16/18 Test #111: test-mbed-connectivity-netsocket-nanostack-network-interface ...   Passed   73.46 sec
      Start 112: test-mbed-connectivity-network-lwipstack-wifi
17/18 Test #112: test-mbed-connectivity-network-lwipstack-wifi ..................***Skipped   0.01 sec
      Start 113: test-mbed-connectivity-network-nanostack-wifi
18/18 Test #113: test-mbed-connectivity-network-nanostack-wifi ..................***Skipped   0.01 sec

100% tests passed, 0 tests failed out of 18

@multiplemonomials multiplemonomials requested a review from ccli8 May 6, 2025 16:44
// Maximum frame length.
// Don't know exactly why, but need to set this 4 bytes higher than the MTU of 1514 bytes
// or 1514 byte packets get rejected
base->MRFL = 1518;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ccli8 This was the one mystery I couldn't really figure out. Any idea what's up here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 bytes of CRC

@@ -116,11 +116,11 @@
},
"tcpip-thread-priority": {
"help": "Priority of lwip TCPIP thread",
"value": "osPriorityNormal"
"value": "osPriorityHigh"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rolling in this fix from arduino/ArduinoCore-mbed#955 (comment)

"help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception, see LWIP's opt.h for more information. Current default is 1600.",
"value": 1600
"help": "Size of heap (bytes) - used for outgoing packets, and also used by some drivers for reception, see LWIP's opt.h for more information.",
"value": 4000
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to increase this since, unlike before CompositeEMAC, Tx DMA memory is now allocated off of the LwIP heap instead of being statically allocated.

@@ -62,7 +62,7 @@ emac_mem_buf_t *NanostackMemoryManager::alloc_pool(uint32_t size, uint32_t align

uint32_t NanostackMemoryManager::get_pool_alloc_unit(uint32_t align) const
{
return MBED_CONF_NSAPI_EMAC_RX_POOL_BUF_SIZE;
return MBED_CONF_NSAPI_EMAC_RX_POOL_BUF_SIZE - align;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bringing this in line with the other memory manager implementations -- seems like it was wrong before

// Normally that would be 1518 (1514 byte MTU plus 4 bytes for alignment overhead), but this EMAC also always
// receives the CRC (plus other stuff??) at the end of the packet, with no way to turn it off. So, we need an extra 6
// bytes padding that can be clobbered here.
"emac-rx-pool-buf-size": 1524,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ccli8 Another mystery. I am seeing that 6 bytes after the end of the packet always get written with stuff. Couldn't really find info about this in the datasheet.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 bytes should be 2 bytes of length and 4 bytes of CRC.

@@ -105,3 +105,42 @@ void mbed_sdk_init(void)
SYS_LockReg();
}
}

// Override mbed_mac_address of mbed_interface.c to provide ethernet devices with a semi-unique MAC address
void mbed_mac_address(char *mac)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this here from the old ethernet code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose to move mbed_mac_address() here instead of keeping in emac driver ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I deleted the file where this function previously lived, so I had to move it somewhere.

Also, putting it here is better than putting it in the Ethernet driver, because now it's available to the code even if you don't link the ethernet library. Example: if I connect an external Wi-Fi or bluetooth module to my M480, the wifi and bluetooth stacks also need to get the MAC address, and this allows them to!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. Most WiFi or BLE module should be within eeprom to store mac address.
Just remind, mbed could support dual connection interface, such like as ETH + WiFi. So, it's risky to share mbed_mac_address() for other connection interfaces.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point. To be honest, I kinda feel like no one thought that far ahead -- it's hard enough to get one of the two things working :/.
I did a check of the codebase and right now, every non-Nuvoton or Giga Devices implementation does put this in mbed_overrides.c, so I think this is the accepted place to put it. But I'd be happy to accept a PR down the line to improve this situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants