diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 94edcbc..35e2332 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -27,6 +27,9 @@ jobs: matrix: board: + - fqbn: arduino:avr:uno + platforms: | + - name: arduino:avr - fqbn: arduino:samd:nano_33_iot platforms: | - name: arduino:samd diff --git a/README.md b/README.md index 1106d86..e1d77c0 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Sync Labels status](https://github.com/bcmi-labs/Arduino_10BASE_T1S/actions/workflows/sync-labels.yml/badge.svg)](https://github.com/bcmi-labs/Arduino_10BASE_T1S/actions/workflows/sync-labels.yml) [![Arduino Lint](https://github.com/bcmi-labs/Arduino_10BASE_T1S/workflows/Arduino%20Lint/badge.svg)](https://github.com/bcmi-labs/Arduino_10BASE_T1S/actions?workflow=Arduino+Lint) -**Note**: This library works for Arduino [Nano 33 IoT](https://store.arduino.cc/products/arduino-nano-33-iot), Arduino [Zero](https://store.arduino.cc/products/arduino-zero), Arduino [R4 WiFi](https://store.arduino.cc/products/uno-r4-wifi) and Arduino [R4 Minima](https://store.arduino.cc/products/uno-r4-minima). +**Note**: This library works for Arduino [Uno](https://store.arduino.cc/products/arduino-uno-rev3), Arduino [Nano 33 IoT](https://store.arduino.cc/products/arduino-nano-33-iot), Arduino [Zero](https://store.arduino.cc/products/arduino-zero), Arduino [R4 WiFi](https://store.arduino.cc/products/uno-r4-wifi) and Arduino [R4 Minima](https://store.arduino.cc/products/uno-r4-minima). ### How-to-compile/upload ```bash diff --git a/examples/LAN8651-iperf/LAN8651-iperf.ino b/examples/LAN8651-iperf/LAN8651-iperf.ino index 7794c5e..745b3fa 100644 --- a/examples/LAN8651-iperf/LAN8651-iperf.ino +++ b/examples/LAN8651-iperf/LAN8651-iperf.ino @@ -41,7 +41,7 @@ static T1SMacSettings const t1s_mac_settings{MAC_PROMISCUOUS_MODE, MAC_TX_CUT_TH static int const CS_PIN = 10; static int const RESET_PIN = 9; static int const IRQ_PIN = 2; -#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_MINIMA) || defined(ARDUINO_UNOWIFIR4) +#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_MINIMA) || defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_UNO_WIFI_REV2) /* Those are all boards with the Arduino Uno form factor for the T1S shield. */ static int const CS_PIN = 9; static int const RESET_PIN = 4; @@ -54,12 +54,12 @@ static int const IRQ_PIN = 2; * GLOBAL VARIABLES **************************************************************************************/ -auto const tc6_io = std::make_shared +auto const tc6_io = new TC6::TC6_Io ( SPI , CS_PIN , RESET_PIN , IRQ_PIN); -auto const tc6_inst = std::make_shared(tc6_io); +auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S_UDP(tc6_io); /************************************************************************************** * SETUP/LOOP diff --git a/examples/LAN8651-iperf/udp_perf_client.cpp b/examples/LAN8651-iperf/udp_perf_client.cpp index 2a7ba83..270eedc 100644 --- a/examples/LAN8651-iperf/udp_perf_client.cpp +++ b/examples/LAN8651-iperf/udp_perf_client.cpp @@ -68,7 +68,7 @@ const char kLabel[] = 'G' }; -std::shared_ptr priv_tc6_inst; +TC6::TC6_Arduino_10BASE_T1S_UDP * priv_tc6_inst = NULL; void iperf_print_app_header(void) { @@ -272,7 +272,7 @@ static void PrintServerStats(void) } } -void iperf_init(std::shared_ptr const tc6_inst) +void iperf_init(TC6::TC6_Arduino_10BASE_T1S_UDP * tc6_inst) { priv_tc6_inst = tc6_inst; diff --git a/examples/LAN8651-iperf/udp_perf_client.h b/examples/LAN8651-iperf/udp_perf_client.h index 8190789..c19b335 100644 --- a/examples/LAN8651-iperf/udp_perf_client.h +++ b/examples/LAN8651-iperf/udp_perf_client.h @@ -100,7 +100,7 @@ struct perf_stats { /* Number of parallel UDP clients */ #define NUM_OF_PARALLEL_CLIENTS 1 -void iperf_init(std::shared_ptr const tc6_inst); +void iperf_init(TC6::TC6_Arduino_10BASE_T1S_UDP * tc6_inst); void iperf_print_app_header(void); void iperf_start_application(void); void iperf_stop_application(void); diff --git a/src/Arduino_10BASE_T1S_UDP.h b/src/Arduino_10BASE_T1S_UDP.h index 77460a6..a33f76c 100644 --- a/src/Arduino_10BASE_T1S_UDP.h +++ b/src/Arduino_10BASE_T1S_UDP.h @@ -22,9 +22,13 @@ * INCLUDE **************************************************************************************/ -#include - -#include +#if defined(ARDUINO_ARCH_AVR) +# include "Udp.h" +# include "IPAddress.h" +#else +# include +# include +#endif #include "MacAddress.h" #include "T1SMacSettings.h" diff --git a/src/MacAddress.h b/src/MacAddress.h index 0c4f729..092ee58 100644 --- a/src/MacAddress.h +++ b/src/MacAddress.h @@ -22,9 +22,7 @@ * INCLUDE **************************************************************************************/ -#include - -#include +#include #include #include @@ -39,9 +37,17 @@ static size_t constexpr MAC_ADDRESS_NUM_BYTES = 6; * CLASS DECLARATION **************************************************************************************/ -class MacAddress : public arduino::Printable - , public std::array +class MacAddress +#if defined(ARDUINO_ARCH_AVR) + : public Printable +#else + : public arduino::Printable +#endif { +public: + uint8_t * data() { return _data; } + uint8_t const * data() const { return _data; } private: + uint8_t _data[MAC_ADDRESS_NUM_BYTES]; virtual size_t printTo(Print & p) const override; }; diff --git a/src/T1SMacSettings.h b/src/T1SMacSettings.h index 4350993..c103d4f 100644 --- a/src/T1SMacSettings.h +++ b/src/T1SMacSettings.h @@ -22,7 +22,7 @@ * INCLUDE **************************************************************************************/ -#include +#include #include #include @@ -31,7 +31,12 @@ * CLASS DECLARATION **************************************************************************************/ -class T1SMacSettings : public arduino::Printable +class T1SMacSettings +#if defined(ARDUINO_ARCH_AVR) + : public Printable +#else + : public arduino::Printable +#endif { private: bool const _mac_promiscuous_mode; diff --git a/src/T1SPlcaSettings.h b/src/T1SPlcaSettings.h index 4872193..e3738c7 100644 --- a/src/T1SPlcaSettings.h +++ b/src/T1SPlcaSettings.h @@ -22,7 +22,7 @@ * INCLUDE **************************************************************************************/ -#include +#include #include #include @@ -31,7 +31,12 @@ * CLASS DECLARATION **************************************************************************************/ -class T1SPlcaSettings : public arduino::Printable +class T1SPlcaSettings +#if defined(ARDUINO_ARCH_AVR) + : public Printable +#else + : public arduino::Printable +#endif { private: uint8_t const _node_id; diff --git a/src/microchip/TC6_Arduino_10BASE_T1S_UDP.cpp b/src/microchip/TC6_Arduino_10BASE_T1S_UDP.cpp index 46b7952..ac727d6 100644 --- a/src/microchip/TC6_Arduino_10BASE_T1S_UDP.cpp +++ b/src/microchip/TC6_Arduino_10BASE_T1S_UDP.cpp @@ -22,9 +22,6 @@ #include "TC6_Arduino_10BASE_T1S_UDP.h" -#include -#include - #include "lib/libtc6/inc/tc6-regs.h" #include "lib/liblwip/include/lwip/init.h" @@ -49,34 +46,43 @@ namespace TC6 * GLOBAL CONSTANTS **************************************************************************************/ -static std::list T6_LWIP_INSTANCE_LIST; +struct TC6ListNode { + TC6LwIP_t * inst; + struct TC6ListNode * next; +}; + +static TC6ListNode * tc6_lwip_instance_list_head = nullptr; /************************************************************************************** * MODULE INTERNAL FUNCTION DECLARATION **************************************************************************************/ -static TC6LwIP_t *GetContextNetif(struct netif *intf) +static TC6LwIP_t * GetContextNetif(struct netif *intf) { - for (auto const elem: T6_LWIP_INSTANCE_LIST) + for (TC6ListNode * ptr = tc6_lwip_instance_list_head; + ptr != nullptr; + ptr = ptr->next) { /* Compare memory address to retrieve the right * data structure. */ - if (&elem->ip.netint == intf) - return elem; + if (&ptr->inst->ip.netint == intf) + return ptr->inst; } return nullptr; } static TC6LwIP_t *GetContextTC6(TC6_t *pTC6) { - for (auto const elem: T6_LWIP_INSTANCE_LIST) + for (TC6ListNode * ptr = tc6_lwip_instance_list_head; + ptr != nullptr; + ptr = ptr->next) { /* Compare memory address to retrieve the right * data structure. */ - if (elem->tc.tc6 == pTC6) - return elem; + if (ptr->inst->tc.tc6 == pTC6) + return ptr->inst; } return nullptr; } @@ -101,11 +107,26 @@ static void OnPlcaStatus(TC6_t *pInst, bool success, uint32_t addr, uint32_t val static err_t lwIpInit(struct netif *netif); static err_t lwIpOut(struct netif *netif, struct pbuf *p); +static String IPtoString(IPAddress const & ip_addr) +{ +#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) + char msg_buf[16]; + snprintf(msg_buf, + sizeof(msg_buf), + "%u.%u.%u.%u", + ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]); + return String(msg_buf); +#else + return ip_addr.toString(); +#endif +} + + /************************************************************************************** * CTOR/DTOR **************************************************************************************/ -TC6_Arduino_10BASE_T1S_UDP::TC6_Arduino_10BASE_T1S_UDP(std::shared_ptr const tc6_io) +TC6_Arduino_10BASE_T1S_UDP::TC6_Arduino_10BASE_T1S_UDP(TC6_Io * tc6_io) : _tc6_io{tc6_io} { _lw.io = tc6_io; @@ -143,7 +164,11 @@ bool TC6_Arduino_10BASE_T1S_UDP::begin(IPAddress const ip_addr, _lw.tc.tc6 == NULL) return false; - T6_LWIP_INSTANCE_LIST.push_back(&_lw); + TC6ListNode * ptr = tc6_lwip_instance_list_head; + while (ptr != nullptr) ptr = ptr->next; + ptr = new TC6ListNode; + ptr->inst = &_lw; + ptr->next = NULL; /* Initialize TC6 registers. */ if (!TC6Regs_Init( _lw.tc.tc6 @@ -164,9 +189,9 @@ bool TC6_Arduino_10BASE_T1S_UDP::begin(IPAddress const ip_addr, TC6_Service(_lw.tc.tc6, true); /* Assign IP address, network mask and gateway. */ - auto const ip_addr_str = ip_addr.toString(); - auto const network_mask_str = network_mask.toString(); - auto const gateway_str = gateway.toString(); + auto const ip_addr_str = IPtoString(ip_addr); + auto const network_mask_str = IPtoString(network_mask); + auto const gateway_str = IPtoString(gateway); ip4_addr_t lwip_ip_addr; ip4_addr_t lwip_network_mask; diff --git a/src/microchip/TC6_Arduino_10BASE_T1S_UDP.h b/src/microchip/TC6_Arduino_10BASE_T1S_UDP.h index afc63c0..a059b43 100644 --- a/src/microchip/TC6_Arduino_10BASE_T1S_UDP.h +++ b/src/microchip/TC6_Arduino_10BASE_T1S_UDP.h @@ -24,9 +24,7 @@ #include "../Arduino_10BASE_T1S_UDP.h" -#include - -#include +#include #include "lib/liblwip/include/lwip/netif.h" @@ -61,7 +59,7 @@ typedef struct { TC6Lib_t tc; LwIp_t ip; - std::shared_ptr io; + TC6::TC6_Io * io; } TC6LwIP_t; /************************************************************************************** @@ -78,7 +76,7 @@ namespace TC6 class TC6_Arduino_10BASE_T1S_UDP : public Arduino_10BASE_T1S_UDP { public: - TC6_Arduino_10BASE_T1S_UDP(std::shared_ptr const tc6_io); + TC6_Arduino_10BASE_T1S_UDP(TC6_Io * tc6_io); virtual ~TC6_Arduino_10BASE_T1S_UDP(); @@ -99,7 +97,7 @@ class TC6_Arduino_10BASE_T1S_UDP : public Arduino_10BASE_T1S_UDP private: - std::shared_ptr const _tc6_io; + TC6_Io * _tc6_io; TC6LwIP_t _lw; }; diff --git a/src/microchip/TC6_Io.cpp b/src/microchip/TC6_Io.cpp index a0c4e95..8507410 100644 --- a/src/microchip/TC6_Io.cpp +++ b/src/microchip/TC6_Io.cpp @@ -46,7 +46,15 @@ uint8_t const TC6_Io::FALLBACK_MAC[TC6_Io::MAC_SIZE]; * CTOR/DTOR **************************************************************************************/ -TC6_Io::TC6_Io(HardwareSPI &spi, int const cs_pin, int const reset_pin, int const irq_pin) +TC6_Io::TC6_Io( +#if defined(ARDUINO_ARCH_AVR) + SPIClass & spi, +#else + HardwareSPI &spi, +#endif + int const cs_pin, + int const reset_pin, + int const irq_pin) : _spi{spi}, _cs_pin{cs_pin}, _reset_pin{reset_pin}, _irq_pin{irq_pin}, _int_in{0}, _int_out{0}, _int_reported{0} { diff --git a/src/microchip/TC6_Io.h b/src/microchip/TC6_Io.h index d9f7516..b1ac875 100644 --- a/src/microchip/TC6_Io.h +++ b/src/microchip/TC6_Io.h @@ -22,9 +22,7 @@ * INCLUDE **************************************************************************************/ -#include - -#include +#include /************************************************************************************** * NAMESPACE @@ -45,7 +43,12 @@ class TC6_Io static uint8_t constexpr FALLBACK_MAC[MAC_SIZE] = { 0x00u, 0x80u, 0xC2u, 0x00u, 0x01u, 0xCCu }; - TC6_Io(HardwareSPI &spi, + TC6_Io( +#if defined(ARDUINO_ARCH_AVR) + SPIClass & spi, +#else + HardwareSPI & spi, +#endif int const cs_pin, int const reset_pin, int const irq_pin); @@ -62,7 +65,11 @@ class TC6_Io private: - HardwareSPI &_spi; +#if defined(ARDUINO_ARCH_AVR) + SPIClass & _spi; +#else + HardwareSPI & _spi; +#endif int const _cs_pin; int const _reset_pin; int const _irq_pin;