Today I started experimenting.
This code compiles fine, but halts the ethernet configuration. Do any of you know why?
Code:
/ MATLABUDPBroadcastExample.ino
// Copyright 2016 The MathWorks, Inc.
// some edits T Schrama july 2020 for teensy 4.1 nativeEthernet
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
int ledPin = LED_BUILTIN; // Connect an LED->330Ohm to pin 5 or LED_BUILTIN
int packetSize;
//byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xAD, 0xDE};
uint8_t mac[6];
unsigned int broadcastPort = 31416;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ArduinoAckMessage[] = "Arduino:ACK\r"; // "\r" is the carriage return, i.e., char(13)
char MATLABBlinkItCommand[] = "MATLAB:BlinkIt\r";
EthernetUDP Udp;
IPAddress broadcastAddress;
void setup()
{
teensyMAC(mac); // function to auire Teensy 4.1 NativeEthernet MAC, function is define below
Serial.begin(9600);
if (Ethernet.begin(mac)==0){
Serial.println("Failed to configure Ethernet. Halt.");
delay(1000); // Allow some time for message to be printed
abort();
}
Serial.print("Arduino's IP Address is: ");
Serial.println(Ethernet.localIP());
Udp.begin(broadcastPort);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
// if there's data available, read a packet
packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Message: ");
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
packetBuffer[packetSize] = '\0';
Serial.println(packetBuffer);
if (strcmp(packetBuffer, MATLABBlinkItCommand) == 0)
{
broadcastAddress = Udp.remoteIP();
broadcastAddress[3] = 255;
Udp.beginPacket(broadcastAddress, broadcastPort);
Udp.write(ArduinoAckMessage);
Udp.endPacket();
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
}
}
delay(10);
}
// code to aquire MAC adres of teensy 4.1 nativeEthernet
void teensyMAC(uint8_t *mac) {
static char teensyMac[23];
#if defined(HW_OCOTP_MAC1) && defined(HW_OCOTP_MAC0)
Serial.println("using HW_OCOTP_MAC* - see https://forum.pjrc.com/threads/57595-Serial-amp-MAC-Address-Teensy-4-0");
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;
#define MAC_OK
#else
mac[0] = 0x04;
mac[1] = 0xE9;
mac[2] = 0xE5;
uint32_t SN=0;
__disable_irq();
#if defined(HAS_KINETIS_FLASH_FTFA) || defined(HAS_KINETIS_FLASH_FTFL)
Serial.println("using FTFL_FSTAT_FTFA - vis teensyID.h - see https://github.com/sstaub/TeensyID/blob/master/TeensyID.h");
FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
FTFL_FCCOB0 = 0x41;
FTFL_FCCOB1 = 15;
FTFL_FSTAT = FTFL_FSTAT_CCIF;
while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
SN = *(uint32_t *)&FTFL_FCCOB7;
#define MAC_OK
#elif defined(HAS_KINETIS_FLASH_FTFE)
Serial.println("using FTFL_FSTAT_FTFE - vis teensyID.h - see https://github.com/sstaub/TeensyID/blob/master/TeensyID.h");
kinetis_hsrun_disable();
FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
*(uint32_t *)&FTFL_FCCOB3 = 0x41070000;
FTFL_FSTAT = FTFL_FSTAT_CCIF;
while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
SN = *(uint32_t *)&FTFL_FCCOBB;
kinetis_hsrun_enable();
#define MAC_OK
#endif
__enable_irq();
for(uint8_t by=0; by<3; by++) mac[by+3]=(SN >> ((2-by)*8)) & 0xFF;
#endif
#ifdef MAC_OK
sprintf(teensyMac, "MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(teensyMac);
#else
Serial.println("ERROR: could not get MAC");
#endif
}