Teensy 4.1 and Ethernet

mrm

Member
I'm sorry this is such a basic question. Perhaps someone can point me in the right direction.

I have a Teensy 4.1, and I'm going to hook up a magnetic ethernet jack using the special 6 pins.

1) Are there different specs of eth jack, or can I just use anything that has magnetics, 75 ohm resistors, and LEDs?

2) This is a really basic question and sorry if I missed the answer somewhere already documented. On a Teensy 4.1, which I understand has native ethernet, should I still use the Ethernet library? I read on the Ethernet for Teensy Github (https://github.com/PaulStoffregen/Ethernet):

In 2020, we released Teensy 4.1 with native ethernet, which offers vastly better performance than using a Wiznet chip. I do not anticipate spending much development time on Wiznet support in the foreseeable future. If you use Ethernet with Teensy, please consider Teensy 4.1 for future projects. The performance is so much better.

I could not ascertain from the above text if this means "here's the ethernet library. If you use it on Teensy 4.1 it will be even better", or if it means "use something else on Teensy 4.1, it will be much better".

My aim is to implement MQTT and I want the most simple means of doing this possible.

3) I have seen reference to "NativeEthernet" library on this forum. And also a function that retrieves the MAC address:
Code:
void teensyMAC(uint8_t *mac) 
{
    for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
    for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
    Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

What is the purpose of the argument passed to this function? Looks like it is there to let me SET the mac address. However when I pass it my own MAC address, it returns what is presumably the builtin MAC address on the board.

Thanks
 
3) I have seen reference to "NativeEthernet" library on this forum. And also a function that retrieves the MAC address:
Code:
void teensyMAC(uint8_t *mac) 
{
    for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
    for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
    Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

What is the purpose of the argument passed to this function? Looks like it is there to let me SET the mac address. However when I pass it my own MAC address, it returns what is presumably the builtin MAC address on the board.

Thanks

The argument is used to return the Teensy's built-in mac address.
 
1) Are there different specs of eth jack, or can I just use anything that has magnetics, 75 ohm resistors, and LEDs?

The specs are usually very similar, so most magnetics meant for 10/100 ethernet can work.

Just in case you haven't seen it, this kit is the normal way.

https://www.pjrc.com/store/ethernet_kit.html


2) ... On a Teensy 4.1, which I understand has native ethernet, should I still use the Ethernet library?

Use either NativeEthernet or QNEthernet. The old "Ethernet" library is only for Wiznet chips connected to SPI.



What is the purpose of the argument passed to this function? Looks like it is there to let me SET the mac address.

No, that function gives you a copy of Teensy's assigned mac address. The purpose is to let you give you a copy of the assigned address, in the array format you need to pass to the library.


However when I pass it my own MAC address, it returns what is presumably the builtin MAC address on the board.

If you want to use your own mac address, you certainly can. In that case, you would NOT pass the array with your mac address to the function which gives you a copy of Teensy's mac address, because that causes Teensy's copy to overwrite whatever you had in the array. Instead, you would just skip that step and give your address to the NativeEthernet library to use.
 
The specs are usually very similar, so most magnetics meant for 10/100 ethernet can work.
Just in case you haven't seen it, this kit is the normal way.
https://www.pjrc.com/store/ethernet_kit.html

Thank you Paul, that is very clear. Also just want to take the opportunity to say thanks for all the amazing hard work you have put into the Teensy. I'm new to it and it really is a fabulous little device and I'm so glad I discovered it.
 
Yeah, Paul, Robin, and the products are pretty awesome. “Thorough, robust, deep care.”

I’ll add some points. The reference to “native ethernet” just means “built in”. There’s the two Ethernet libraries Paul mentions above. “NativeEthernet” is simply the library name. It uses FNET as its underlying IP stack and uses a 1ms timer to move things along. QNEthernet uses lwIP as its underlying IP stack and doesn’t use a “refresh” timer, instead relying on an internal `loop()` function being called periodically by the system and by the library itself (and possibly by user code).

Regarding the MAC address, you can actually use anything you want when communicating over Ethernet. Teensy’s “built in” address is just a set of permanent numbers stored in a specific place that you then have to read yourself and relay to the Ethernet stack. The Arduino-style API tells the stack about the desired MAC address using one of the `begin(mac, …)` functions.

In QNEthernet, there’s an additional `begin()` (no parameters) function that does the work of passing those “permanent numbers” to the underlying stack (or whatever has been set using `setMACAddress(buf)`). In fact, its `Ethernet` instance defaults to the internal MAC address value at startup, so you don’t really have to do anything manually. Just calling `begin()` with no arguments will work.

Having said that, you can change the MAC address using `setMACAddress(buf)` (it will restart the stack in QNEthernet), or retrieve the currently used (or about-to-be-used) MAC address using `macAddress(buf)` (or the Arduino-style `MACAddress(buf)`).
 
Back
Top