debug_fram

This commit is contained in:
Siwat Sirichai 2023-12-31 02:18:57 +07:00
parent b4b7232937
commit 091dc183fe
7 changed files with 126 additions and 48 deletions

View file

@ -1,7 +1,5 @@
#include <ESPMegaIoT.hpp>
#include <ETH.h>
#define NETWORK_CONFIG_ADDRESS 34
#define MQTT_CONFIG_ADDRESS 34 + sizeof(NetworkConfig)
ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient)
{
@ -316,13 +314,34 @@ void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config)
void ESPMegaIoT::loadNetworkConfig()
{
// Load the network config from FRAM
fram->read(0, (uint8_t *)&network_config, sizeof(NetworkConfig));
network_config.ip = fram->read32(FRAM_ADDRESS);
network_config.gateway = fram->read32(FRAM_ADDRESS + 4);
network_config.subnet = fram->read32(FRAM_ADDRESS + 8);
network_config.dns1 = fram->read32(FRAM_ADDRESS + 12);
network_config.dns2 = fram->read32(FRAM_ADDRESS + 16);
fram->read(FRAM_ADDRESS + 20, (uint8_t*)network_config.hostname, 32);
network_config.useStaticIp = fram->read8(FRAM_ADDRESS + 52);
network_config.useWifi = fram->read8(FRAM_ADDRESS + 53);
network_config.wifiUseAuth = fram->read8(FRAM_ADDRESS + 54);
fram->read(FRAM_ADDRESS + 55, (uint8_t*)network_config.ssid, 32);
fram->read(FRAM_ADDRESS + 87, (uint8_t*)network_config.password, 32);
}
void ESPMegaIoT::saveNetworkConfig()
{
// Save the network config to FRAM
fram->write(NETWORK_CONFIG_ADDRESS, (uint8_t *)&network_config, sizeof(NetworkConfig));
fram->write32(FRAM_ADDRESS, network_config.ip);
fram->write32(FRAM_ADDRESS + 4, network_config.gateway);
fram->write32(FRAM_ADDRESS + 8, network_config.subnet);
fram->write32(FRAM_ADDRESS + 12, network_config.dns1);
fram->write32(FRAM_ADDRESS + 16, network_config.dns2);
fram->write(FRAM_ADDRESS + 20, (uint8_t*)network_config.hostname, 32);
fram->write8(FRAM_ADDRESS + 52, network_config.useStaticIp);
fram->write8(FRAM_ADDRESS + 53, network_config.useWifi);
fram->write8(FRAM_ADDRESS + 54, network_config.wifiUseAuth);
fram->write(FRAM_ADDRESS + 55, (uint8_t*)network_config.ssid, 32);
fram->write(FRAM_ADDRESS + 87, (uint8_t*)network_config.password, 32);
}
void ESPMegaIoT::ethernetBegin()
@ -333,13 +352,23 @@ void ESPMegaIoT::ethernetBegin()
void ESPMegaIoT::loadMqttConfig()
{
// Load the mqtt config from FRAM
fram->read(sizeof(NetworkConfig), (uint8_t *)&this->mqtt_config, sizeof(MqttConfig));
// Populate the mqtt connection parameters
// We skip bytes 119-127 because they are reserved for the network config
mqtt_config.mqtt_port = fram->read16(FRAM_ADDRESS + 128);
fram->read(FRAM_ADDRESS + 130, (uint8_t*)mqtt_config.mqtt_server, 32);
fram->read(FRAM_ADDRESS + 162, (uint8_t*)mqtt_config.mqtt_user, 32);
fram->read(FRAM_ADDRESS + 194, (uint8_t*)mqtt_config.mqtt_password, 32);
mqtt_config.mqtt_useauth = fram->read8(FRAM_ADDRESS + 226);
fram->read(FRAM_ADDRESS + 227, (uint8_t*)mqtt_config.base_topic, 32);
}
void ESPMegaIoT::saveMqttConfig()
{
fram->write(MQTT_CONFIG_ADDRESS, (uint8_t *)&mqtt_config, sizeof(MqttConfig));
fram->write16(FRAM_ADDRESS + 128, mqtt_config.mqtt_port);
fram->write(FRAM_ADDRESS + 130, (uint8_t*)mqtt_config.mqtt_server, 32);
fram->write(FRAM_ADDRESS + 162, (uint8_t*)mqtt_config.mqtt_user, 32);
fram->write(FRAM_ADDRESS + 194, (uint8_t*)mqtt_config.mqtt_password, 32);
fram->write8(FRAM_ADDRESS + 226, mqtt_config.mqtt_useauth);
fram->write(FRAM_ADDRESS + 227, (uint8_t*)mqtt_config.base_topic, 32);
}
void ESPMegaIoT::connectToMqtt()
@ -407,4 +436,9 @@ bool ESPMegaIoT::networkConnected()
return WiFi.status() == WL_CONNECTED;
else
return ethernetIface->linkUp();
}
void ESPMegaIoT::bindFRAM(FRAM *fram)
{
this->fram = fram;
}