|
| 1 | +/* |
| 2 | + * This example has been tested with the Arduino 10BASE-T1S (T1TOS) shield. |
| 3 | + * |
| 4 | + * Author: |
| 5 | + * Alexander Entinger |
| 6 | + */ |
| 7 | + |
| 8 | +/************************************************************************************** |
| 9 | + * INCLUDE |
| 10 | + **************************************************************************************/ |
| 11 | + |
| 12 | +#include <Arduino_10BASE_T1S.h> |
| 13 | + |
| 14 | +#include <SPI.h> |
| 15 | +#include <Wire.h> |
| 16 | + |
| 17 | +/************************************************************************************** |
| 18 | + * CONSTANTS |
| 19 | + **************************************************************************************/ |
| 20 | + |
| 21 | +static uint8_t const T1S_PLCA_NODE_ID = 2; |
| 22 | + |
| 23 | +static IPAddress const ip_addr {192, 168, 42, 100 + T1S_PLCA_NODE_ID}; |
| 24 | +static IPAddress const network_mask{255, 255, 255, 0}; |
| 25 | +static IPAddress const gateway {192, 168, 42, 100}; |
| 26 | + |
| 27 | +static T1SPlcaSettings const t1s_plca_settings{T1S_PLCA_NODE_ID}; |
| 28 | +static T1SMacSettings const t1s_default_mac_settings; |
| 29 | + |
| 30 | +static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 1}; |
| 31 | +static uint16_t const UDP_CLIENT_PORT = 8889; |
| 32 | +static uint16_t const UDP_SERVER_PORT = 8888; |
| 33 | +static uint8_t * udp_tx_msg_buf[256] = {0}; |
| 34 | +static uint8_t * udp_rx_msg_buf[256] = {0}; |
| 35 | + |
| 36 | +/************************************************************************************** |
| 37 | + * GLOBAL VARIABLES |
| 38 | + **************************************************************************************/ |
| 39 | + |
| 40 | +auto const tc6_io = new TC6::TC6_Io |
| 41 | + ( SPI |
| 42 | + , CS_PIN |
| 43 | + , RESET_PIN |
| 44 | + , IRQ_PIN); |
| 45 | +auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S_UDP(tc6_io); |
| 46 | + |
| 47 | +/************************************************************************************** |
| 48 | + * SETUP/LOOP |
| 49 | + **************************************************************************************/ |
| 50 | + |
| 51 | +void setup() |
| 52 | +{ |
| 53 | + Serial.begin(115200); |
| 54 | + while (!Serial) { } |
| 55 | + delay(1000); |
| 56 | + |
| 57 | + /* I2C (Wire) is needed to obtain an individual MAC |
| 58 | + * address from the AT24MAC402 EEPROM located on the |
| 59 | + * Mikroe Two-Wire ETH click board. |
| 60 | + */ |
| 61 | + Wire.begin(); |
| 62 | + |
| 63 | + /* Initialize digital IO interface for interfacing |
| 64 | + * with the LAN8651. |
| 65 | + */ |
| 66 | + pinMode(IRQ_PIN, INPUT_PULLUP); |
| 67 | + attachInterrupt(digitalPinToInterrupt(IRQ_PIN), |
| 68 | + []() { tc6_io->onInterrupt(); }, |
| 69 | + FALLING); |
| 70 | + |
| 71 | + /* Initialize IO module. */ |
| 72 | + if (!tc6_io->begin()) |
| 73 | + { |
| 74 | + Serial.println("'TC6_Io::begin(...)' failed."); |
| 75 | + for (;;) { } |
| 76 | + } |
| 77 | + |
| 78 | + /* Obtain MAC address stored on EEPROM of Mikroe |
| 79 | + * Two-Wire ETH Click board. |
| 80 | + */ |
| 81 | + MacAddress mac_addr; |
| 82 | + if (!get_mac_address(mac_addr.data())) |
| 83 | + { |
| 84 | + Serial.println("'get_mac_address(...)' failed, using fallback MAC address."); |
| 85 | + memcpy(mac_addr.data(), TC6::TC6_Io::FALLBACK_MAC, MAC_ADDRESS_NUM_BYTES); |
| 86 | + } |
| 87 | + |
| 88 | + if (!tc6_inst->begin( ip_addr |
| 89 | + , network_mask |
| 90 | + , gateway |
| 91 | + , mac_addr |
| 92 | + , t1s_plca_settings |
| 93 | + , t1s_default_mac_settings)) |
| 94 | + { |
| 95 | + Serial.println("'TC6::begin(...)' failed."); |
| 96 | + for (;;) { } |
| 97 | + } |
| 98 | + |
| 99 | + Serial.print("IP\t"); |
| 100 | + Serial.println(ip_addr); |
| 101 | + Serial.println(mac_addr); |
| 102 | + Serial.println(t1s_plca_settings); |
| 103 | + Serial.println(t1s_default_mac_settings); |
| 104 | + |
| 105 | + if (!tc6_inst->begin(UDP_CLIENT_PORT)) |
| 106 | + { |
| 107 | + Serial.println("begin(...) failed for UDP server"); |
| 108 | + for (;;) { } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void loop() |
| 113 | +{ |
| 114 | + /* Services the hardware and the protocol stack. |
| 115 | + * Must be called cyclic. The faster the better. |
| 116 | + */ |
| 117 | + tc6_inst->service(); |
| 118 | + |
| 119 | + static unsigned long prev_beacon_check = 0; |
| 120 | + |
| 121 | + auto const now = millis(); |
| 122 | + |
| 123 | + if ((now - prev_beacon_check) > 1000) |
| 124 | + { |
| 125 | + prev_beacon_check = now; |
| 126 | + if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) |
| 127 | + Serial.println("getPlcaStatus(...) failed"); |
| 128 | + } |
| 129 | + |
| 130 | + /* Send a UDP packet to the UDP server. */ |
| 131 | + int const tx_packet_size = snprintf((char *)udp_tx_msg_buf, sizeof(udp_tx_msg_buf), "%ld: hello server", millis()); |
| 132 | + |
| 133 | + tc6_inst->beginPacket(UDP_SERVER_IP_ADDR, UDP_SERVER_PORT); |
| 134 | + tc6_inst->write((const uint8_t *)udp_tx_msg_buf, tx_packet_size); |
| 135 | + tc6_inst->endPacket(); |
| 136 | + |
| 137 | + /* Check for incoming UDP packets. */ |
| 138 | + int const rx_packet_size = tc6_inst->parsePacket(); |
| 139 | + if (rx_packet_size) |
| 140 | + { |
| 141 | + /* Receive incoming UDP packets. */ |
| 142 | + Serial.print("Received "); |
| 143 | + Serial.print(rx_packet_size); |
| 144 | + Serial.print(" bytes from "); |
| 145 | + Serial.print(tc6_inst->remoteIP()); |
| 146 | + Serial.print(" port "); |
| 147 | + Serial.print(tc6_inst->remotePort()); |
| 148 | + Serial.println(); |
| 149 | + |
| 150 | + int len = tc6_inst->read(reinterpret_cast<unsigned char *>(udp_rx_msg_buf), sizeof(udp_rx_msg_buf)); |
| 151 | + if (len > 0) { |
| 152 | + udp_rx_msg_buf[len] = 0; |
| 153 | + } |
| 154 | + Serial.print("UDP packet contents: "); |
| 155 | + Serial.print(reinterpret_cast<char *>(udp_rx_msg_buf)); |
| 156 | + Serial.println(); |
| 157 | + |
| 158 | + /* Send back a reply, to the IP address and port we got the packet from. */ |
| 159 | + tc6_inst->beginPacket(tc6_inst->remoteIP(), tc6_inst->remotePort()); |
| 160 | + tc6_inst->write((const uint8_t *)udp_rx_msg_buf, sizeof(udp_rx_msg_buf)); |
| 161 | + tc6_inst->endPacket(); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +static void OnPlcaStatus(bool success, bool plcaStatus) |
| 167 | +{ |
| 168 | + if (!success) |
| 169 | + { |
| 170 | + Serial.println("PLCA status register read failed"); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + if (plcaStatus) |
| 175 | + Serial.println("PLCA Mode active"); |
| 176 | + else { |
| 177 | + Serial.println("CSMA/CD fallback"); |
| 178 | + tc6_inst->enablePlca(); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +static bool get_mac_address(uint8_t * p_mac) |
| 183 | +{ |
| 184 | + static uint8_t const MAC_EEPROM_I2C_SLAVE_ADDR = 0x58; |
| 185 | + static uint8_t const MAC_EEPROM_EUI_REG_ADDR = 0x9A; |
| 186 | + static uint8_t const MAC_EEPROM_MAC_SIZE = 6; |
| 187 | + bool success = false; |
| 188 | + |
| 189 | + Wire.beginTransmission(MAC_EEPROM_I2C_SLAVE_ADDR); |
| 190 | + Wire.write(MAC_EEPROM_EUI_REG_ADDR); |
| 191 | + Wire.endTransmission(); |
| 192 | + |
| 193 | + Wire.requestFrom(MAC_EEPROM_I2C_SLAVE_ADDR, MAC_EEPROM_MAC_SIZE); |
| 194 | + |
| 195 | + uint32_t const start = millis(); |
| 196 | + |
| 197 | + size_t bytes_read = 0; |
| 198 | + while (bytes_read < MAC_EEPROM_MAC_SIZE && ((millis() - start) < 1000)) { |
| 199 | + if (Wire.available()) { |
| 200 | + p_mac[bytes_read] = Wire.read(); |
| 201 | + bytes_read++; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + success = (bytes_read == MAC_EEPROM_MAC_SIZE); |
| 206 | + return success; |
| 207 | +} |
0 commit comments