Migrate EEPROM to FRAM

This commit is contained in:
Siwat Sirichai 2023-09-28 14:55:02 +07:00
parent 95d968a920
commit c96ce8a67b
3 changed files with 29 additions and 27 deletions

View File

@ -12,7 +12,7 @@
platform = espressif32 platform = espressif32
board = wt32-eth01 board = wt32-eth01
framework = arduino framework = arduino
lib_deps = siwats/ESPMegaPROR3@^1.2.2 lib_deps = siwats/ESPMegaPROR3@^1.3.0
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
ivanseidel/ArduinoThread@^2.1.1 ivanseidel/ArduinoThread@^2.1.1
arduino-libraries/Arduino_BuiltIn@^1.0.0 arduino-libraries/Arduino_BuiltIn@^1.0.0
@ -20,5 +20,6 @@ lib_deps = siwats/ESPMegaPROR3@^1.2.2
z3t0/IRremote@^4.2.0 z3t0/IRremote@^4.2.0
robtillaart/DHTNEW@^0.4.18 robtillaart/DHTNEW@^0.4.18
seithan/Easy Nextion Library@^1.0.6 seithan/Easy Nextion Library@^1.0.6
robtillaart/FRAM_I2C@^0.6.1
monitor_speed = 115200 monitor_speed = 115200
board_build.partitions = no_ota.csv board_build.partitions = no_ota.csv

View File

@ -4,13 +4,8 @@
#define EMG_LCD_ENABLED #define EMG_LCD_ENABLED
void lcd_send_stop_bit(); void lcd_send_stop_bit();
int lcd_get_int();
float lcd_get_float();
String lcd_get_str();
String lcd_set_var(String var, String value);
void lcd_send_command(String command); void lcd_send_command(String command);
String lcd_wait_response(); String lcd_wait_response();
void lcd_touch_callback(int page, int id);
void lcd_reset(); void lcd_reset();
void lcd_init(); void lcd_init();
#endif #endif

View File

@ -12,6 +12,10 @@
#include <EasyNextionLibrary.h> #include <EasyNextionLibrary.h>
#include <lcd.hpp> #include <lcd.hpp>
// OS Configuration
#define FASTBOOT
// Network Connectivity // Network Connectivity
char HOSTNAME[15]; char HOSTNAME[15];
IPAddress IP(0, 0, 0, 0); IPAddress IP(0, 0, 0, 0);
@ -235,29 +239,29 @@ void loop()
void eeprom_retrieve_init() void eeprom_retrieve_init()
{ {
// EEPROM Data Retrival // EEPROM Data Retrival
ac_mode = ESPMega_EEPROM.readByte(EEPROM_ADDRESS_AC_MODE); ac_mode = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_MODE);
ac_temperature = ESPMega_EEPROM.readByte(EEPROM_ADDRESS_AC_TEMPERATURE); ac_temperature = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_TEMPERATURE);
ac_fan_speed = ESPMega_EEPROM.readByte(EEPROM_ADDRESS_AC_FAN_SPEED); ac_fan_speed = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_FAN_SPEED);
// EEPROM Data Retrival Validation // EEPROM Data Retrival Validation
if (ac_mode > 2) if (ac_mode > 2)
{ {
ac_mode = 0; ac_mode = 0;
ESPMega_EEPROM.writeByte(EEPROM_ADDRESS_AC_MODE, ac_mode); ESPMega_FRAM.write8(EEPROM_ADDRESS_AC_MODE, ac_mode);
} }
if (ac_temperature > AC_MAX_TEMPERATURE || ac_temperature < AC_MIN_TEMPERATURE) if (ac_temperature > AC_MAX_TEMPERATURE || ac_temperature < AC_MIN_TEMPERATURE)
{ {
ac_temperature = AC_MAX_TEMPERATURE; ac_temperature = AC_MAX_TEMPERATURE;
ESPMega_EEPROM.writeByte(EEPROM_ADDRESS_AC_TEMPERATURE, ac_temperature); ESPMega_FRAM.write8(EEPROM_ADDRESS_AC_TEMPERATURE, ac_temperature);
} }
if (ac_fan_speed > 3) if (ac_fan_speed > 3)
{ {
ac_fan_speed = 0; ac_fan_speed = 0;
ESPMega_EEPROM.writeByte(EEPROM_ADDRESS_AC_TEMPERATURE, ac_fan_speed); ESPMega_FRAM.write8(EEPROM_ADDRESS_AC_TEMPERATURE, ac_fan_speed);
} }
ac_set_state(ac_mode, ac_temperature, ac_fan_speed); ac_set_state(ac_mode, ac_temperature, ac_fan_speed);
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_PWM_STATE, pwm_states_eeprom, 16); ESPMega_FRAM.read(EEPROM_ADDRESS_PWM_STATE, pwm_states_eeprom, 16);
memcpy(pwm_states, pwm_states_eeprom, 16); memcpy(pwm_states, pwm_states_eeprom, 16);
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_PWM_VALUE, pwm_values_eeprom, 32); ESPMega_FRAM.read(EEPROM_ADDRESS_PWM_VALUE, pwm_values_eeprom, 32);
memcpy(pwm_values, pwm_values_eeprom, 32); memcpy(pwm_values, pwm_values_eeprom, 32);
for (int i = 0; i < 15; i++) for (int i = 0; i < 15; i++)
{ {
@ -323,6 +327,7 @@ void network_begin()
ETH.begin(); ETH.begin();
ETH.setHostname(HOSTNAME); ETH.setHostname(HOSTNAME);
ETH.config(IP, GATEWAY, SUBNET, DNS, DNS); ETH.config(IP, GATEWAY, SUBNET, DNS, DNS);
#ifndef FASTBOOT
delay(1000); delay(1000);
lcd_send_command("boot_state.txt=\"Ethernet Core Initializing \""); lcd_send_command("boot_state.txt=\"Ethernet Core Initializing \"");
delay(500); delay(500);
@ -334,6 +339,7 @@ void network_begin()
delay(500); delay(500);
lcd_send_command("boot_state.txt=\"NTP Core Initializing . . .\""); lcd_send_command("boot_state.txt=\"NTP Core Initializing . . .\"");
delay(500); delay(500);
#endif
char ntp[19]; char ntp[19];
MQTT_SERVER.toString().toCharArray(ntp,19); MQTT_SERVER.toString().toCharArray(ntp,19);
ESPMega_configNTP(utc_offset*3600,0,ntp); ESPMega_configNTP(utc_offset*3600,0,ntp);
@ -400,7 +406,7 @@ void thread_initialization()
environment_reporter.onRun(publish_env_state); environment_reporter.onRun(publish_env_state);
environment_reporter.setInterval(5000); environment_reporter.setInterval(5000);
eeprom_pwm_updater.onRun(eeprom_pwm_update); eeprom_pwm_updater.onRun(eeprom_pwm_update);
eeprom_pwm_updater.setInterval(5000); eeprom_pwm_updater.setInterval(1000);
} }
void pwm_state_callback(String topic, String message) void pwm_state_callback(String topic, String message)
@ -708,7 +714,7 @@ void ac_set_state(int mode, int temperature, int fan_speed)
} }
publish_ac_state(); publish_ac_state();
uint8_t ac_datablock[3] = {ac_mode, ac_temperature, ac_fan_speed}; uint8_t ac_datablock[3] = {ac_mode, ac_temperature, ac_fan_speed};
ESPMega_EEPROM.writeBlock(0, ac_datablock, 3); ESPMega_FRAM.write(0, ac_datablock, 3);
switch (mode) switch (mode)
{ {
case 0: case 0:
@ -836,7 +842,7 @@ void lcd_ac_refresh_mode()
// auto high mid low // auto high mid low
panel.writeNum("mode_cool.pic", ac_mode == 1 ? 12 : 13); panel.writeNum("mode_cool.pic", ac_mode == 1 ? 12 : 13);
panel.writeNum("mode_fan.pic", ac_mode == 2 ? 22 : 23); panel.writeNum("mode_fan.pic", ac_mode == 2 ? 22 : 23);
panel.writeNum("mode_off.pic", ac_mode == 0 ? 24 : 23); panel.writeNum("mode_off.pic", ac_mode == 0 ? 24 : 25);
} }
void lcd_ac_refresh_fan() void lcd_ac_refresh_fan()
@ -944,13 +950,13 @@ void eeprom_pwm_update()
if (memcmp(pwm_states, pwm_states_eeprom, 16)) if (memcmp(pwm_states, pwm_states_eeprom, 16))
{ {
memcpy(pwm_states_eeprom, pwm_states, 16); memcpy(pwm_states_eeprom, pwm_states, 16);
ESPMega_EEPROM.writeBlock(3, pwm_states_eeprom, 16); ESPMega_FRAM.write(3, pwm_states_eeprom, 16);
lcd_send_stop_bit(); lcd_send_stop_bit();
} }
if (memcmp(pwm_values, pwm_values_eeprom, 32)) if (memcmp(pwm_values, pwm_values_eeprom, 32))
{ {
memcpy(pwm_values_eeprom, pwm_values, 32); memcpy(pwm_values_eeprom, pwm_values, 32);
ESPMega_EEPROM.writeBlock(19, pwm_values_eeprom, 32); ESPMega_FRAM.write(19, pwm_values_eeprom, 32);
lcd_send_stop_bit(); lcd_send_stop_bit();
} }
} }
@ -983,46 +989,46 @@ void set_mqtt_server(String address)
void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
{ {
uint8_t addressblock[4] = {byte1, byte2, byte3, byte4}; uint8_t addressblock[4] = {byte1, byte2, byte3, byte4};
ESPMega_EEPROM.writeBlockVerify(rom_address, addressblock, 4); ESPMega_FRAM.write(rom_address, addressblock, 4);
} }
IPAddress eeprom_ip_retrieve(uint16_t rom_address) IPAddress eeprom_ip_retrieve(uint16_t rom_address)
{ {
uint8_t addressblock[4]; uint8_t addressblock[4];
ESPMega_EEPROM.readBlock(rom_address, addressblock, 4); ESPMega_FRAM.read(rom_address, addressblock, 4);
return IPAddress(addressblock[0], addressblock[1], addressblock[2], addressblock[3]); return IPAddress(addressblock[0], addressblock[1], addressblock[2], addressblock[3]);
} }
void set_hostname(String hostname) void set_hostname(String hostname)
{ {
hostname.toCharArray(HOSTNAME, 15); hostname.toCharArray(HOSTNAME, 15);
ESPMega_EEPROM.writeBlock(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15); ESPMega_FRAM.write(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15);
} }
void eeprom_hostname_retrieve() void eeprom_hostname_retrieve()
{ {
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15); ESPMega_FRAM.read(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15);
} }
void set_basetopic(String topic) void set_basetopic(String topic)
{ {
topic.toCharArray(MQTT_BASE_TOPIC, 20); topic.toCharArray(MQTT_BASE_TOPIC, 20);
ESPMega_EEPROM.writeBlock(EEPROM_ADDRESS_TOPIC, (uint8_t *)MQTT_BASE_TOPIC, 20); ESPMega_FRAM.write(EEPROM_ADDRESS_TOPIC, (uint8_t *)MQTT_BASE_TOPIC, 20);
} }
void eeprom_basetopic_retrieve() void eeprom_basetopic_retrieve()
{ {
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_TOPIC, (uint8_t *)MQTT_BASE_TOPIC, 20); ESPMega_FRAM.read(EEPROM_ADDRESS_TOPIC, (uint8_t *)MQTT_BASE_TOPIC, 20);
} }
void mqtt_port_set(uint16_t port) void mqtt_port_set(uint16_t port)
{ {
uint8_t port_arr[2]; uint8_t port_arr[2];
memcpy(port_arr, &port, 2); memcpy(port_arr, &port, 2);
ESPMega_EEPROM.writeBlock(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2); ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2);
} }
void eeprom_mqtt_port_retrieve() void eeprom_mqtt_port_retrieve()
{ {
uint8_t port_arr[2]; uint8_t port_arr[2];
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2); ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2);
memcpy(&MQTT_PORT, port_arr, 2); memcpy(&MQTT_PORT, port_arr, 2);
} }