Bit-Banged Ethernet On The Raspberry Pi Pico

Whilst the Raspberry Pi RP2040 is quite a capable little chip, on the whole it’s nothing really special compared to the big brand offerings. But, the PIO peripheral is a bit special, and its inclusion was clearly a masterstroke of foresight, because it has bestowed the platform all kinds of capabilities that would be really hard to do any other way, especially for the price.

Our focus this time is on Ethernet, utilizing the PIO as a simple serialiser to push out a pre-formatted bitstream. [kingyo] so far has managed to implement the Pico-10BASE-T providing the bare minimum of UDP transmission (GitHub project) using only a handful of resistors as a proof of concept. For a safer implementation it is more usual to couple such a thing magnetically, and [kingyo] does show construction of a rudimentary pulse transformer, although off the shelf parts are obviously available for this. For the sake of completeness, it is also possible to capacitively couple Ethernet hardware (checkout this Micrel app note for starters) but it isn’t done all that much in practice.

Inside the expedient pulse transformer.

UDP is a simple Ethernet protocol for transferring application data. Being connection-less, payload data are simply formatted into a packet buffer up front. This is all fine, until you realize that the packets are pretty long and the bitrate can be quite high for a low-cost uC, which is why devices with dedicated Ethernet MAC functionality have a specific hardware serialiser-deserialiser (SERDES) block just for this function.

Like many small uC devices, the RP2040 does not have a MAC function built in, but it does have the PIO, and that can easily be programmed to perform the SERDES function in only a handful of lines of code, albeit only currently operating at 10 MBit/sec. This will cause some connectivity problems for modern switch hardware, as they will likely no longer support this low speed, but that’s easily solved by snagging some older switch hardware off eBay.

As for the UDP receive, that is promised for the future, but for getting data out of a remote device over a wired network, Pico-10BASE-T is a pretty good starting point. We’ve seen a few projects before that utilize the PIO to generate high speed signals, such as DVI, albeit with a heavy dose of overclocking needed. If you want a bit more of an intro to all things Pico, you could do worse than check out this video series we highlighted a while back.

29 thoughts on “Bit-Banged Ethernet On The Raspberry Pi Pico

      1. UDP just doesn’t bake retry logic into the protocol, the way TCP does – it’s on you to handle that at the application layer if you need it. Which is why TCP is so heavily favored, as much of the time you do want that feature and don’t want to reinvent the wheel.

        But UDP is massively overlooked, probably often due to a creeping suspicion of it being unreliable. Sure, if you’re trying to move data from say San Francisco to Mumbai, there are a zillion possible paths, with probably 30 hops on every one of them, stuff happens and packets can disappear on the way. This shouldn’t be construed as UDP itself being unreliable so much as long haul internet connection is unreliable. If you’re just trying to move data from one side of your lab to the other, or even across your office building, there usually isn’t more than about one way to go and there’s no excuse for any decent switch or router to throw away datagrams on a whim.

        1. Yeah, totally agree. I’m a big fan of UDP when used for very low bandwidth (e.g., IoT cellular) or LAN comms. Compared to TCP, it’s especially easy to code, with very few special cases, and lightweight on the CPU/MCU & the network. You can also multicast easily.

          To validate and characterize our custom industrial hardware, I’ve conducted numerous load tests on LANs and lost, on average, <1 packet per million sent.

          For the right application/s, when you're not reinventing TCP-like connections, it's great.

  1. > UDP is a simple Ethernet protocol for transferring application data.

    UDP is a simple IP protocol for transferring application data. Through clever hackery, [kingyo] has bit-banged the minimum Layer 2 Ethernet and Layer 3 UDP/IP communication stack necessary for transmitting UCP/IP packets as Ethernet frames onto the nearest network switch.

  2. For a 10 Mbit ethernet TX you more or less only need an SPI that can run at 20MHz. With a 45MHz or so SPI you can also receive, so it seems the POI should certainly be able to do it.

    An old code doing 10Base-T TX on a pi zero: https://github.com/BertoldVdb/SPI10M

    A small part of my job brings me in contact with many ethernet switch chips, I never saw one without 10mbps support.

    1. For a dollar or so you can get an ENC28J60 adapter board and you can do real Ethernet. There is a lot of code out there for this chip. It has a bunch of errata but people have worked around all of it. You can use the ram in the chip to hold your packet so you don’t need to allocate a buffer for it. You can use this chip with microcontrollers much more primitive than a pi zero.

  3. Ok, with 10 gigabit that might be the case, but 10 gigabit copper is not that common anyway. I’ve commented on this primarily because the article mentioned that you should look on Ebay for an old switch, suggesting that new ones don’t support 10 mbit, which is far from the truth. I’ve never come across a switch with 1 or 2.5 gigabit that did not support 10 mbits. Since most NICs in PCs appear to be running in 10 mbit mode when the PC is off/in stand-by mode, I don’t think this will change anytime soon, otherwise wake-on-lan would stop working with such switches.

    1. 10 gigabit copper is everywhere, it’s a $100 option on Mac minis, standard equipment on new Intel and AMD motherboards.

      You really want 10 gig for your backbone if you are doing local backups with time machine or rsync. My local ISP is offering greater than gigabit speed, you’re gonna be pulling cat 6a cable if you want to enjoy it.

      You can get 10 gigabit copper cable at Home Depot, the one near me has it in stock right now.

      You can get 10 gig switches for a couple hundred dollars. In 2022 this is not data center stuff any more, it is mainline consumer gear.

      If you are going to the trouble of pulling cables today you really should be installing cat 6a at least.

  4. > In 2022 this is not data center stuff any more, it is mainline consumer gear.

    I thought this has always been mostly consumer and perhaps office type stuff. In datacenters they try to avoid this power hungry 10 gig copper technology – it is so inefficent compared to the other 10 gig options. Better use fiber or if the equipment is close to the switch, go with DAC.

    Anyway, this is pretty off-topic at this point. Still no need to resort to Ebay. If you are running 10 gig copper everywhere, just buy an off-the-shelf gigabit switch for this particular purpose for a few bucks and it’ll support 10 mbit/s just fine.

  5. no, UDP is not “an ethernet protocol”, if anything the IP protocol is. UDP is not “an IP protocol” either, that’s why UDP/IP is not (technically) redundant.

    The rp2040 is *very* special in several ways. It’s multi-channel SRAM with independent DMA channels is not something you’ll easily find elsehwere, certainly not on MCUs of this tier. It also has several times more SRAM than you commonly find on MCUs of this perf/price tier or even more powerful processors. dual-core isn’t that common either for 133Mhz clock. OTOH, some TI processors have long featured PRUs, and hackaday has covered them in the past but those are based on far more powerful ARM cores. So again the rp2040 is unique in offering an unusually powerful feature for it’s perf/price.

    This is just chock full of anodyne misinformed filler.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.