|
| 1 | +/** \copyright |
| 2 | + * Copyright (c) 2021, Mike Dunston |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * - Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + * |
| 27 | + * \file ESP32C3CanLoadTest.ino |
| 28 | + * |
| 29 | + * Main file for the ESP32-C3 CAN Load Test application. |
| 30 | + * |
| 31 | + * @author Mike Dunston |
| 32 | + * @date 2 May 2021 |
| 33 | + */ |
| 34 | + |
| 35 | +#include <Arduino.h> |
| 36 | +#include <SPIFFS.h> |
| 37 | +#include <esp_spi_flash.h> |
| 38 | + |
| 39 | +#include <OpenMRNLite.h> |
| 40 | +#include <openlcb/TcpDefs.hxx> |
| 41 | + |
| 42 | +#include <openlcb/MultiConfiguredConsumer.hxx> |
| 43 | +#include <utils/GpioInitializer.hxx> |
| 44 | +#include <freertos_drivers/arduino/CpuLoad.hxx> |
| 45 | + |
| 46 | +// Pick an operating mode below, if you select USE_WIFI it will expose |
| 47 | +// this node on WIFI if you select USE_TWAI, this node will be available |
| 48 | +// on CAN. |
| 49 | +// Enabling both options will allow the ESP32 to be accessible from |
| 50 | +// both WiFi and CAN interfaces. |
| 51 | + |
| 52 | +#define USE_WIFI |
| 53 | +//#define USE_TWAI |
| 54 | + |
| 55 | +// Uncomment USE_TWAI_SELECT to enable the usage of select() for the TWAI |
| 56 | +// interface. |
| 57 | +//#define USE_TWAI_SELECT |
| 58 | + |
| 59 | +// uncomment the line below to have all packets printed to the Serial |
| 60 | +// output. This is not recommended for production deployment. |
| 61 | +//#define PRINT_PACKETS |
| 62 | + |
| 63 | +// If USE_TWAI_SELECT or USE_TWAI_ASYNC is enabled but USE_TWAI is not, enable |
| 64 | +// USE_TWAI. |
| 65 | +#if defined(USE_TWAI_SELECT) && !defined(USE_TWAI) |
| 66 | +#define USE_TWAI |
| 67 | +#endif // USE_TWAI_SELECT && !USE_TWAI |
| 68 | + |
| 69 | +#include "config.h" |
| 70 | + |
| 71 | +/// This is the node id to assign to this device, this must be unique |
| 72 | +/// on the CAN bus. |
| 73 | +static constexpr uint64_t NODE_ID = UINT64_C(0x05010101182c); |
| 74 | + |
| 75 | +#if defined(USE_WIFI) |
| 76 | +// Configuring WiFi accesspoint name and password |
| 77 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 78 | +// There are two options: |
| 79 | +// 1) edit the sketch to set this information just below. Use quotes: |
| 80 | +// const char* ssid = "linksys"; |
| 81 | +// const char* password = "superSecret"; |
| 82 | +// 2) add a new file to the sketch folder called something.cpp with the |
| 83 | +// following contents: |
| 84 | +// #include <OpenMRNLite.h> |
| 85 | +// |
| 86 | +// char WIFI_SSID[] = "linksys"; |
| 87 | +// char WIFI_PASS[] = "theTRUEsupers3cr3t"; |
| 88 | + |
| 89 | +/// This is the name of the WiFi network (access point) to connect to. |
| 90 | +const char *ssid = WIFI_SSID; |
| 91 | + |
| 92 | +/// Password of the wifi network. |
| 93 | +const char *password = WIFI_PASS; |
| 94 | + |
| 95 | +/// This is the hostname which the ESP32 will advertise via mDNS, it should be |
| 96 | +/// unique. |
| 97 | +const char *hostname = "esp32mrn"; |
| 98 | + |
| 99 | +OVERRIDE_CONST(gridconnect_buffer_size, 3512); |
| 100 | +//OVERRIDE_CONST(gridconnect_buffer_delay_usec, 200000); |
| 101 | +OVERRIDE_CONST(gridconnect_buffer_delay_usec, 2000); |
| 102 | +OVERRIDE_CONST(gc_generate_newlines, CONSTANT_TRUE); |
| 103 | +OVERRIDE_CONST(executor_select_prescaler, 60); |
| 104 | +OVERRIDE_CONST(gridconnect_bridge_max_outgoing_packets, 2); |
| 105 | + |
| 106 | +#endif // USE_WIFI |
| 107 | + |
| 108 | +#if defined(USE_TWAI) |
| 109 | +// This is the ESP32-C3 pin connected to the SN65HVD23x/MCP2551 R (RX) pin. |
| 110 | +// Note: Any pin can be used for this other than 11-17 which are connected to |
| 111 | +// the onboard flash. |
| 112 | +// Note: Adjusting this pin assignment will require updating the GPIO_PIN |
| 113 | +// declarations below for input/outputs. |
| 114 | +constexpr gpio_num_t TWAI_RX_PIN = GPIO_NUM_18; |
| 115 | + |
| 116 | +// This is the ESP32-C3 pin connected to the SN65HVD23x/MCP2551 D (TX) pin. |
| 117 | +// Note: Any pin can be used for this other than 11-17 which are connected to |
| 118 | +// the onboard flash. |
| 119 | +// Note: Adjusting this pin assignment will require updating the GPIO_PIN |
| 120 | +// declarations below for input/outputs. |
| 121 | +constexpr gpio_num_t TWAI_TX_PIN = GPIO_NUM_19; |
| 122 | + |
| 123 | +#endif // USE_TWAI |
| 124 | + |
| 125 | +// This is the primary entrypoint for the OpenMRN/LCC stack. |
| 126 | +OpenMRN openmrn(NODE_ID); |
| 127 | + |
| 128 | +// This tracks the CPU usage of the ESP32-C3 through the usage of a hardware |
| 129 | +// timer that records what the CPU is currently executing roughly 163 times per |
| 130 | +// second. |
| 131 | +CpuLoad cpu_load; |
| 132 | + |
| 133 | +// This will report the usage to the console output. |
| 134 | +CpuLoadLog cpu_log(openmrn.stack()->service()); |
| 135 | + |
| 136 | +// ConfigDef comes from config.h and is specific to this particular device and |
| 137 | +// target. It defines the layout of the configuration memory space and is also |
| 138 | +// used to generate the cdi.xml file. Here we instantiate the configuration |
| 139 | +// layout. The argument of offset zero is ignored and will be removed later. |
| 140 | +static constexpr openlcb::ConfigDef cfg(0); |
| 141 | + |
| 142 | +#if defined(USE_WIFI) |
| 143 | +Esp32WiFiManager wifi_mgr(ssid, password, openmrn.stack(), cfg.seg().wifi()); |
| 144 | +#endif // USE_WIFI |
| 145 | + |
| 146 | +#if defined(USE_TWAI) |
| 147 | +Esp32HardwareTwai twai(TWAI_RX_PIN, TWAI_TX_PIN); |
| 148 | +#endif // USE_TWAI |
| 149 | + |
| 150 | +// This will perform the factory reset procedure for this node's configuration |
| 151 | +// items. |
| 152 | +// |
| 153 | +// The node name and description will be set to the SNIP model name field |
| 154 | +// value. |
| 155 | +// Descriptions for intputs and outputs will be set to a blank string, input |
| 156 | +// debounce parameters will be set to default values. |
| 157 | +class FactoryResetHelper : public DefaultConfigUpdateListener { |
| 158 | +public: |
| 159 | + UpdateAction apply_configuration(int fd, bool initial_load, |
| 160 | + BarrierNotifiable *done) OVERRIDE { |
| 161 | + AutoNotify n(done); |
| 162 | + return UPDATED; |
| 163 | + } |
| 164 | + |
| 165 | + void factory_reset(int fd) override |
| 166 | + { |
| 167 | + cfg.userinfo().name().write(fd, openlcb::SNIP_STATIC_DATA.model_name); |
| 168 | + cfg.userinfo().description().write( |
| 169 | + fd, openlcb::SNIP_STATIC_DATA.model_name); |
| 170 | + } |
| 171 | +} factory_reset_helper; |
| 172 | + |
| 173 | +namespace openlcb |
| 174 | +{ |
| 175 | + // Name of CDI.xml to generate dynamically. |
| 176 | + const char CDI_FILENAME[] = "/spiffs/cdi.xml"; |
| 177 | + |
| 178 | + // This will stop openlcb from exporting the CDI memory space upon start. |
| 179 | + extern const char CDI_DATA[] = ""; |
| 180 | + |
| 181 | + // Path to where OpenMRN should persist general configuration data. |
| 182 | + extern const char *const CONFIG_FILENAME = "/spiffs/openlcb_config"; |
| 183 | + |
| 184 | + // The size of the memory space to export over the above device. |
| 185 | + extern const size_t CONFIG_FILE_SIZE = cfg.seg().size() + cfg.seg().offset(); |
| 186 | + |
| 187 | + // Default to store the dynamic SNIP data is stored in the same persistant |
| 188 | + // data file as general configuration data. |
| 189 | + extern const char *const SNIP_DYNAMIC_FILENAME = CONFIG_FILENAME; |
| 190 | +} |
| 191 | + |
| 192 | +// Callback function for the hardware timer configured to fire roughly 163 |
| 193 | +// times per second. |
| 194 | +void ARDUINO_ISR_ATTR record_cpu_usage() |
| 195 | +{ |
| 196 | +#if CONFIG_ARDUINO_ISR_IRAM |
| 197 | + // if the ISR is called with flash disabled we can not safely recored the |
| 198 | + // cpu usage. |
| 199 | + if (!spi_flash_cache_enabled()) |
| 200 | + { |
| 201 | + return; |
| 202 | + } |
| 203 | +#endif |
| 204 | + // Retrieves the vtable pointer from the currently running executable. |
| 205 | + unsigned *pp = (unsigned *)openmrn.stack()->executor()->current(); |
| 206 | + cpuload_tick(pp ? pp[0] | 1 : 0); |
| 207 | +} |
| 208 | + |
| 209 | +void setup() |
| 210 | +{ |
| 211 | + Serial.begin(115200L); |
| 212 | + LOG(INFO, "[Node] ID: %s", uint64_to_string_hex(NODE_ID).c_str()); |
| 213 | + LOG(INFO, "[SNIP] version:%d, manufacturer:%s, model:%s, hw-v:%s, sw-v:%s" |
| 214 | + , openlcb::SNIP_STATIC_DATA.version |
| 215 | + , openlcb::SNIP_STATIC_DATA.manufacturer_name |
| 216 | + , openlcb::SNIP_STATIC_DATA.model_name |
| 217 | + , openlcb::SNIP_STATIC_DATA.hardware_version |
| 218 | + , openlcb::SNIP_STATIC_DATA.software_version); |
| 219 | + |
| 220 | + // Register hardware timer zero to use a 1Mhz resolution and to count up |
| 221 | + // from zero when the timer triggers. |
| 222 | + auto timer = timerBegin(0, 80, true); |
| 223 | + // Attach our callback function to be called when the timer is ready to |
| 224 | + // fire. Note that the edge parameter is not used/supported on the |
| 225 | + // ESP32-C3. |
| 226 | + timerAttachInterrupt(timer, record_cpu_usage, true); |
| 227 | + // Configure the trigger point to be roughly 163 times per second. |
| 228 | + timerAlarmWrite(timer, 1000000/163, true); |
| 229 | + // Enable the timer. |
| 230 | + timerAlarmEnable(timer); |
| 231 | + |
| 232 | + // Initialize the SPIFFS filesystem as our persistence layer |
| 233 | + if (!SPIFFS.begin()) |
| 234 | + { |
| 235 | + LOG(WARNING, "SPIFFS failed to mount, attempting to format and remount"); |
| 236 | + if (!SPIFFS.begin(true)) |
| 237 | + { |
| 238 | + LOG_ERROR("SPIFFS mount failed even with format, giving up!"); |
| 239 | + while (1) |
| 240 | + { |
| 241 | + // Unable to start SPIFFS successfully, give up and wait |
| 242 | + // for WDT to kick in |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + // Create the CDI.xml dynamically |
| 248 | + openmrn.create_config_descriptor_xml(cfg, openlcb::CDI_FILENAME); |
| 249 | + |
| 250 | + // Create the default internal configuration file |
| 251 | + openmrn.stack()->create_config_file_if_needed(cfg.seg().internal_config(), |
| 252 | + openlcb::CANONICAL_VERSION, openlcb::CONFIG_FILE_SIZE); |
| 253 | + |
| 254 | +#if defined(USE_TWAI) |
| 255 | + twai.hw_init(); |
| 256 | +#endif // USE_TWAI |
| 257 | + |
| 258 | + // Start the OpenMRN stack |
| 259 | + openmrn.begin(); |
| 260 | + |
| 261 | +#if defined(PRINT_PACKETS) |
| 262 | + // Dump all packets as they are sent/received. |
| 263 | + // Note: This should not be enabled in deployed nodes as it will |
| 264 | + // have performance impact. |
| 265 | + openmrn.stack()->print_all_packets(); |
| 266 | +#endif // PRINT_PACKETS |
| 267 | + |
| 268 | +#if defined(USE_TWAI_SELECT) |
| 269 | + // add TWAI driver with select() usage |
| 270 | + openmrn.add_can_port_select("/dev/twai/twai0"); |
| 271 | + |
| 272 | + // start executor thread since this is required for select() to work in the |
| 273 | + // OpenMRN executor. |
| 274 | + openmrn.start_executor_thread(); |
| 275 | +#else |
| 276 | + // add TWAI driver with non-blocking usage |
| 277 | + openmrn.add_can_port_async("/dev/twai/twai0"); |
| 278 | +#endif // USE_TWAI_SELECT |
| 279 | +} |
| 280 | + |
| 281 | +void loop() |
| 282 | +{ |
| 283 | + // Call the OpenMRN executor, this needs to be done as often |
| 284 | + // as possible from the loop() method. |
| 285 | + openmrn.loop(); |
| 286 | +} |
0 commit comments