Browse Source

I've been a little busy
1) I fixed the MQTT command non-sense
2) I've been working reliability fixes
3) I don't remember

curiousmuch 6 years ago
parent
commit
2c996290a3

+ 2 - 1
.vscode/settings.json

@@ -1,5 +1,6 @@
 {
     "files.associations": {
         "ideasx_interface.h": "c"
-    }
+    },
+    "C_Cpp.intelliSenseEngineFallback": "Enabled"
 }

+ 16 - 9
hal/io.c

@@ -106,7 +106,8 @@ uint32_t ICACHE_FLASH_ATTR IO_GetStates(void)
 
 void ICACHE_FLASH_ATTR IO_Shutdown(void)
 {
-    GPIO_OUTPUT_SET(LDO_SHUTDOWN, 0);
+    gpio_output_set(0, BIT(LDO_SHUTDOWN), BIT(LDO_SHUTDOWN), 0);
+    //GPIO_OUTPUT_SET(LDO_SHUTDOWN, 0);
 }
 
 void ICACHE_FLASH_ATTR IO_GPIO_Handler(void)
@@ -140,9 +141,9 @@ void ICACHE_FLASH_ATTR IO_GPIO_Handler(void)
                     }
                     else
                     {
-                        os_delay_us(10000);
-                        if (io_config.switch_a_cb != NULL)
-                            io_config.switch_a_cb(GPIO_REG_READ(GPIO_IN_ADDRESS));
+                        //os_delay_us(10000);
+                        //if (io_config.switch_a_cb != NULL)
+                        //    io_config.switch_a_cb(GPIO_REG_READ(GPIO_IN_ADDRESS));
                         if (io_config.debounce_timer)
                             os_timer_arm(&io_config.switch_a_timer, io_config.debounce_delay, 0);
                     }
@@ -151,9 +152,9 @@ void ICACHE_FLASH_ATTR IO_GPIO_Handler(void)
 				case SWITCH_B:
                 {
                     //ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);    
-                    os_delay_us(10000);                    
-                    if (io_config.switch_b_cb != NULL)
-                        io_config.switch_b_cb(GPIO_REG_READ(GPIO_IN_ADDRESS));
+                    //os_delay_us(10000);                    
+                    //if (io_config.switch_b_cb != NULL)
+                    //    io_config.switch_b_cb(GPIO_REG_READ(GPIO_IN_ADDRESS));
                     if (io_config.debounce_timer)
                         os_timer_arm(&io_config.switch_b_timer, io_config.debounce_delay, 0);
                     break;
@@ -172,7 +173,10 @@ void ICACHE_FLASH_ATTR IO_GPIO_Handler(void)
 static void ICACHE_FLASH_ATTR switchA_debounce(void)
 {
     ESP_LOGV(TAG, "switchA");
-	ETS_GPIO_INTR_DISABLE();
+    ETS_GPIO_INTR_DISABLE();
+
+    if (io_config.switch_a_cb != NULL)
+        io_config.switch_a_cb(GPIO_REG_READ(GPIO_IN_ADDRESS));
 
 	gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
 	os_timer_disarm(&io_config.switch_a_timer);
@@ -184,7 +188,10 @@ static void ICACHE_FLASH_ATTR switchA_debounce(void)
 static void ICACHE_FLASH_ATTR switchB_debounce(void)
 {
     ESP_LOGV(TAG, "switchB");
-	ETS_GPIO_INTR_DISABLE();
+    ETS_GPIO_INTR_DISABLE();
+    
+    if (io_config.switch_b_cb != NULL)
+        io_config.switch_b_cb(GPIO_REG_READ(GPIO_IN_ADDRESS));
 
 	gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
 	os_timer_disarm(&io_config.switch_b_timer);

+ 4 - 3
hal/lsm6ds3.c

@@ -151,11 +151,11 @@ void ICACHE_FLASH_ATTR LSM6DS3_EnableMotion(void)
     LSM6DS3_Reset();
 	Sensor_IO_Set_Register(LSM6DS3_CTRL2_G, LSM6DS3_ODR_G_POWERDOWN);						     // disable gyro
 	Sensor_IO_Set_Register(LSM6DS3_CTRL1_XL, (LSM6DS3_ODR_XL_13Hz + LSM6DS3_FS_XL_2G));		     // 208Hz 2g scale
+    Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, (LSM6DS3_H_LACTIVE + LSM6DS3_PP_OD));                // set int lines active low and open drain    
     Sensor_IO_Set_Register(LSM6DS3_TAP_CFG, LSM6DS3_LIR); 									     // enable interuppt latch and slope filter
 	Sensor_IO_Set_Register(LSM6DS3_WAKE_UP_THS, 0x01); 									         // set thres 32/64*2G = 1G
 	Sensor_IO_Set_Register(LSM6DS3_WAKE_UP_DUR, 0x02); 									         // zero duration
 	Sensor_IO_Set_Register(LSM6DS3_MD1_CFG, LSM6DS3_INT1_WU);								     // set interrupt for INTX (1 is connected to PBA)
-	Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, LSM6DS3_H_LACTIVE);                                  // set int lines active low
 
 }
 
@@ -164,8 +164,9 @@ void ICACHE_FLASH_ATTR LSM6DS3_DisableMotion(void)
 	LSM6DS3_Reset();
 	Sensor_IO_Set_Register(LSM6DS3_CTRL2_G, LSM6DS3_ODR_G_POWERDOWN);      // disable gyro
     Sensor_IO_Set_Register(LSM6DS3_CTRL1_XL, LSM6DS3_ODR_XL_POWERDOWN);    // disable XL
-    Sensor_IO_Set_Register(LSM6DS3_MD1_CFG, 0);                            // clear all interrupt vectors (the reset registers aren't cleared on reset?)
-	Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, LSM6DS3_H_LACTIVE);
+    Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, (LSM6DS3_H_LACTIVE + LSM6DS3_PP_OD));     // set int lines active low and open drain    
+    Sensor_IO_Set_Register(LSM6DS3_TAP_CFG, 0);
+    Sensor_IO_Set_Register(LSM6DS3_MD1_CFG, 0);                                       // clear all interrupt vectors (the reset registers aren't cleared on reset?)
 }
 
 // determine if there has been a FIFO or significant motion interrupt

+ 1 - 1
include/hal/io.h

@@ -19,7 +19,7 @@
 #define SWITCH_B_FUNC       FUNC_GPIO2
 #define LDO_SHUTDOWN_FUNC   FUNC_GPIO4
 
-#define DEBOUNCEDELAY 15    // ms
+#define DEBOUNCEDELAY 10    // ms
 
 
 typedef enum {

+ 3 - 0
include/interface/encoder_interface.h

@@ -24,6 +24,9 @@
 #define Encoder_EnableIMUI2CInterface LSM6DS3_Enable_I2C_Bridge
 
 #define Encoder_InitUART uart_init
+#define Encoder_InitIMU LSM6DS3_DisableMotion
+
+#define Encoder_Find Light_Find
 
 void ICACHE_FLASH_ATTR Encoder_Shutdown(void);
 void ICACHE_FLASH_ATTR Encoder_Restart(void);

+ 3 - 1
include/interface/ideasx_interface.h

@@ -117,10 +117,12 @@ typedef struct {
 #define OTA_COMMAND_TOPIC          "ota"
 #define RESTART_COMMAND_TOPIC      "restart"
 #define SHUTDOWN_COMMAND_TOPIC     "shutdown"
+#define LOCATION_COMMAND_TOPIC     "loc"
 
 #define RESTART_COMMAND_INDEX   0 
 #define SHUTDOWN_COMMAND_INDEX  1
-#define NUMBER_OF_COMMANDS      2 
+#define LOC_COMMAND_INDEX       2
+#define NUMBER_OF_COMMANDS      3 
 #define COMMAND_QOS     1 
 #define COMMAND_RETAIN  0
 

+ 4 - 2
include/interface/light_interface.h

@@ -15,7 +15,9 @@
 #define Light_AdaptiveIMU()                 WS2812_SetColor(WS2812_MAGENTA, WS2812_SOLID)
 #define Light_AdaptiveBar()                 WS2812_SetColor(WS2812_ORANGE, WS2812_SOLID)
 
-void Light_Shutdown(void);
-void Light_Restart(void);
+void ICACHE_FLASH_ATTR Light_Shutdown(void);
+void ICACHE_FLASH_ATTR Light_Restart(void);
+void ICACHE_FLASH_ATTR Light_Find(void);
+
 
 #endif

+ 3 - 3
include/user_config.h

@@ -13,7 +13,7 @@
 #define STORED_APS 1
 
 /* MQTT DEFAULT CONFIGURATIONS */
-#define MQTT_HOST_DEFAULT               "10.42.0.1" //or "mqtt.yourdomain.com"
+#define MQTT_HOST_DEFAULT               "ideasx-cloud.duckdns.org" //or "mqtt.yourdomain.com"
 #define MQTT_PORT_DEFAULT               1883
 #define MQTT_BUF_SIZE                   1024
 #define MQTT_KEEPALIVE_DEFAULT          20             // seconds
@@ -31,8 +31,8 @@
 //#define MQTT_PASS_DEFAULT               "PASS"
 
 /* OTA SETTINGS */
-#define OTA_HOST 	"fphx.duckdns.org"
-#define OTA_PORT 	2000
+#define OTA_HOST_DEFAULT 	"fphx.duckdns.org"
+#define OTA_PORT_DEFAULT 	2000
 #define OTA_ROM0 	"rom0.bin"
 #define OTA_ROM1 	"rom1.bin"
 #define OTA_FILE "file.bin"

+ 4 - 4
include/util/config.h

@@ -10,7 +10,7 @@
 #ifndef __CONFIG_H__
 #define __CONFIG_H__
 
-#include "os_type.h"
+#include "esp_common.h"
 #include "user_config.h"
 
 typedef struct{
@@ -33,12 +33,12 @@ typedef struct{
 	uint8_t ota_host[64];
 	uint32_t ota_port;
 
-} SYSCFG;
+} System_Config_t;
 /*------------------------------------------------------------------------------*/
 typedef struct {
     uint8 flag;
     uint8 pad[3];
-} SAVE_FLAG;
+} Save_Flag_t;
 /*------------------------------------------------------------------------------*/
 
 /*------------------------------------------------------------------------------*/
@@ -51,7 +51,7 @@ void ICACHE_FLASH_ATTR cfg_load();
 /*------------------------------------------------------------------------------*/
 // Allocate Space for System Configuration
 /*------------------------------------------------------------------------------*/
-extern SYSCFG sysCfg;
+extern Save_Flag_t sysCfg;
 /*------------------------------------------------------------------------------*/
 
 #endif /* USER_CONFIG_H_ */

+ 6 - 2
interface/encoder_interface.c

@@ -23,8 +23,12 @@ void ICACHE_FLASH_ATTR Encoder_Shutdown(void)
 {
     Light_Shutdown();
     IO_Shutdown();
-    
-    //system_deep_sleep(0);
+    system_deep_sleep(0);
+}
+
+void ICACHE_FLASH_ATTR Encoder_Factory_Reset(void)
+{
+    system_restore(); 
 }
 
 void ICACHE_FLASH_ATTR Encoder_Restart(void)

+ 7 - 3
interface/ideasx_interface.c

@@ -161,8 +161,10 @@ void ICACHE_FLASH_ATTR ideasX_command_array_init(void)
 {
     os_sprintf(command_array[SHUTDOWN_COMMAND_INDEX].topic, "%s", SHUTDOWN_COMMAND_TOPIC);
     os_sprintf(command_array[RESTART_COMMAND_INDEX].topic, "%s", RESTART_COMMAND_TOPIC); 
-    command_array[SHUTDOWN_COMMAND_INDEX].function = Encoder_Shutdown;
-    command_array[RESTART_COMMAND_INDEX].function = Encoder_Restart;
+    os_sprintf(command_array[LOC_COMMAND_INDEX].topic, "%s", LOC_COMMAND_INDEX);
+    command_array[SHUTDOWN_COMMAND_INDEX].function = &Encoder_Shutdown;
+    command_array[RESTART_COMMAND_INDEX].function = &Encoder_Restart;
+    command_array[LOC_COMMAND_INDEX].function = &Encoder_Find;
 }
 
 static void ICACHE_FLASH_ATTR ideasX_data_cb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
@@ -188,10 +190,12 @@ static void ICACHE_FLASH_ATTR ideasX_data_cb(uint32_t *args, const char* topic,
         if (!strcmp(command_array[i].topic, command_topic))
         {
             ESP_LOGD(TAG, "Found the command: %s", command_array[i].topic);
-            command_array[i].function(); 
+            ideasX_function_t function =  command_array[i].function; 
+            function(); 
         }
     }
 
+  ESP_LOGD(TAG, "we made it out");
   os_free(topicBuf);
   os_free(dataBuf);
 }

+ 19 - 0
interface/light_interface.c

@@ -1,10 +1,27 @@
 #include "interface/light_interface.h"
 
+void ICACHE_FLASH_ATTR Light_Find(void)
+{
+    uint8_t i;
+    for(i=0;i<3;i++)
+    {
+        system_soft_wdt_feed(); // loop is over 3 second long which will cause a WDT to trigger
+
+        WS2812_SetColor(WS2812_ORANGE, WS2812_SOLID);
+        os_delay_us(500000);
+        WS2812_SetColor(WS2812_OFF, WS2812_SOLID);
+        os_delay_us(500000);
+    }
+    WS2812_SetColor(WS2812_BLUE, WS2812_SOLID);
+}
+
 void ICACHE_FLASH_ATTR Light_Shutdown(void)
 {
     uint8_t i;
     for(i=0;i<3;i++)
     {
+        system_soft_wdt_feed(); // loop is over 3 second long which will cause a WDT to trigger
+
         WS2812_SetColor(WS2812_RED, WS2812_SOLID);
         os_delay_us(500000);
         WS2812_SetColor(WS2812_OFF, WS2812_SOLID);
@@ -17,6 +34,8 @@ void ICACHE_FLASH_ATTR Light_Restart(void)
     uint8_t i;
     for(i=0;i<3;i++)
     {
+        system_soft_wdt_feed();
+
         WS2812_SetColor(WS2812_BLUE, WS2812_SOLID);
         os_delay_us(500000);
         WS2812_SetColor(WS2812_OFF, WS2812_SOLID);

+ 9 - 3
interface/uart_interface.c

@@ -6,6 +6,7 @@
 
 #include "interface/uart_interface.h"
 #include "interface/light_interface.h"
+#include "interface/encoder_interface.h"
 #include "hal/io.h"
 #include "hal/lsm6ds3.h"
 
@@ -110,13 +111,13 @@ void process_command(uint8_t *str)
     if (!strcmp(str, "AT+RST"))         // reset
     {
         ESP_LOGI(TAG, "Manual Restarting...\n");
-		system_restart();
+        Light_Restart();
+		Encoder_Restart(); 
 	}
     else if (!strcmp(str, "AT+SD"))     // shutdown
     {
         ESP_LOGI(TAG, "Manual Shutdown...\n");
-        Light_Shutdown();
-        IO_Shutdown();
+        Encoder_Shutdown(); 
     }
     else if (!strcmp(str, "AT+INFO"))   // print system information
     {
@@ -148,4 +149,9 @@ void process_command(uint8_t *str)
     {
         ESP_LOGI(TAG, "N/A\n");
     }
+    else if (!strcmp(str, "AT+FRST"))   // factory reset 
+    {
+        ESP_LOGI(TAG, "Factory Restart...");
+        Encoder_Factory_Reset(); 
+    }
 }

+ 20 - 19
user/user_main.c

@@ -52,10 +52,10 @@ typedef struct {
     bool wifi_connected;
     bool ideasX_connected;
     bool motion_detector;
-} system_config_t;
+} system_state_t;
 
 
-static system_config_t system_config;
+static system_state_t system_state;
 
 void ICACHE_FLASH_ATTR imu_cb(IMU_Interrupt_et interrupt)
 {
@@ -64,16 +64,16 @@ void ICACHE_FLASH_ATTR imu_cb(IMU_Interrupt_et interrupt)
         case IMU_MOTION:
         {
             // the flag is hack because multiple significant motion interrupt are called
-            if (system_config.wifi_connected == false && system_config.motion_detector == true)
+            if (system_state.wifi_connected == false && system_state.motion_detector == true)
             {
-                system_config.motion_detector = false;
+                system_state.motion_detector = false;
                 Encoder_DisableIOInterrupts();
                 Encoder_DisableMotion();
                 Light_AccessPointSearching();
                 WiFi_Connect(WIFI_TIMEOUT);
             }
             // the LSM6DS3 retains and old interrupt that isn't cleared until the first time switch 1 is pressed. 
-            if (system_config.wifi_connected == true && system_config.motion_detector == false)
+            if (system_state.wifi_connected == true && system_state.motion_detector == false)
             {
                 Encoder_EnableIOInterrupts();
             }
@@ -88,15 +88,15 @@ void ICACHE_FLASH_ATTR imu_cb(IMU_Interrupt_et interrupt)
 
 void ICACHE_FLASH_ATTR switch_a_cb(uint32_t gpio_states)
 {
-    if (system_config.wifi_connected == false)
+    if (system_state.wifi_connected == false)
     {
-        system_config.motion_detector = false;
+        system_state.motion_detector = false;
         Encoder_DisableIOInterrupts();
         Encoder_DisableMotion();
         Light_AccessPointSearching();
         WiFi_Connect(WIFI_TIMEOUT);
     }
-    if (system_config.ideasX_connected)
+    if (system_state.ideasX_connected)
     {
         ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);        
         IdeasX_Publish_Buttons(gpio_states);
@@ -105,16 +105,16 @@ void ICACHE_FLASH_ATTR switch_a_cb(uint32_t gpio_states)
 
 void ICACHE_FLASH_ATTR switch_b_cb(uint32_t gpio_states)
 {
-    if (system_config.wifi_connected == false)
+    if (system_state.wifi_connected == false)
     {
-        system_config.motion_detector = false;
+        system_state.motion_detector = false;
         Encoder_DisableIOInterrupts();
         Encoder_DisableMotion();
         Light_AccessPointSearching();
         WiFi_Connect(WIFI_TIMEOUT);
     }
 
-    if (system_config.ideasX_connected)
+    if (system_state.ideasX_connected)
     {
         ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);
         IdeasX_Publish_Buttons(gpio_states);
@@ -124,7 +124,7 @@ void ICACHE_FLASH_ATTR switch_b_cb(uint32_t gpio_states)
 void ICACHE_FLASH_ATTR ideasX_success_cb(void)
 {
     Light_BrokerSuccess();
-    system_config.ideasX_connected = true; 
+    system_state.ideasX_connected = true; 
     Encoder_EnableIOInterrupts();
     IO_ActivateDebounceTimer(); 
 }
@@ -132,14 +132,14 @@ void ICACHE_FLASH_ATTR ideasX_success_cb(void)
 void ICACHE_FLASH_ATTR ideasX_fail_cb(void)
 {
     Light_BrokerFailure();
-    system_config.ideasX_connected = false; 
+    system_state.ideasX_connected = false; 
     Encoder_DisableIOInterrupts();
     IO_DisableDebounceTimer();
 }
 
 void ICACHE_FLASH_ATTR wifi_success_cb(void)
 {
-    system_config.wifi_connected = true;
+    system_state.wifi_connected = true;
     Light_AccessPointConnected();
     IdeasX_Connect();
     //IdeasX_Init();
@@ -147,7 +147,7 @@ void ICACHE_FLASH_ATTR wifi_success_cb(void)
 
 void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
 {
-    system_config.wifi_connected = false;
+    system_state.wifi_connected = false;
     IdeasX_Disconnect();
 
     switch (failure_code)
@@ -196,21 +196,21 @@ void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
     }
 
     // re-enable motion detectors
-    system_config.motion_detector = true;
+    system_state.motion_detector = true;
     Encoder_EnableMotion();
     Encoder_EnableIOInterrupts();
 }
 
  void ICACHE_FLASH_ATTR app_init(void)
 {
-    system_config.wifi_connected = false;
+    system_state.wifi_connected = false;
     Light_AccessPointSearching();
     WiFi_Connect(WIFI_TIMEOUT);
 }
 
 void user_init(void)
 {
-    os_delay_us(1000000);
+    //os_delay_us(1000000);
     Encoder_InitUART(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1); // this needs to be abstracted
 
     os_printf("\n\n----------------------------------------------------------\n");
@@ -221,9 +221,10 @@ void user_init(void)
     WiFi_Initialize();
     IdeasX_Init();
     Encoder_InitIO();
+    Encoder_InitIMU(); 
     Encoder_DisableIOInterrupts();
     Encoder_EnableIMUI2CInterface(true);
-    //IO_Shutdown();                       // set the shutdown pin for testing purporses
+   // IO_Shutdown();                       // set the shutdown pin for testing purporses
 
     WiFi_SetCallbacks(wifi_success_cb, wifi_fail_cb);
     IdeasX_SetStatusCallbacks(ideasX_success_cb, ideasX_fail_cb);

+ 46 - 66
util/config.c

@@ -35,16 +35,10 @@
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
-#include "ets_sys.h"
-#include "os_type.h"
-#include "mem.h"
-#include "osapi.h"
-#include "user_interface.h"
+#include "util/config.h"
+#include "log/esp_log.h"
 
-#include "mqtt/mqtt.h"
-#include "modules/config.h"
-#include "user_config.h"
-#include "driver/uart.h"
+static const char* TAG = "config.c";
 
 /* CFG_LOCATION: 0x2
  * (CFG_LOCATION + 0)*0x1000 = 0x2000
@@ -54,86 +48,72 @@
  *
  */
 
-
-
-SYSCFG sysCfg;
-SAVE_FLAG saveFlag;
-MQTT_TOPICS mqttTopics;
+System_Config_t system_config;
+Save_Flag_t save_flag;
 
 void ICACHE_FLASH_ATTR cfg_save()
 {
-    #if CONFIG_DEBUG
-    uart0_sendStr("Writing New Default Module Configuration\r\n");
-    #endif
+    ESP_LOGI(TAG, "Writing New Default Module Configuration");
+
     spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE,		    // read config sector 3 for
-                       (uint32 *)&saveFlag, sizeof(SAVE_FLAG));		// saveFlag
+                       (uint32 *)&save_flag, sizeof(Save_Flag_t));		// save_flag
 
-    if (saveFlag.flag == 0) {
+    if (save_flag.flag == 0) {
     spi_flash_erase_sector(CFG_LOCATION + 1); 					// erase config sector 1
-    spi_flash_write((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE,	// write sysCfg in config sector 1
-    (uint32 *)&sysCfg, sizeof(SYSCFG));
-    saveFlag.flag = 1;											// set saveFlag.flag
+    spi_flash_write((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE,	// write system_config in config sector 1
+    (uint32 *)&system_config, sizeof(System_Config_t);
+    save_flag.flag = 1;											// set save_flag.flag
     spi_flash_erase_sector(CFG_LOCATION + 3);					// erase config sector 3
-    spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE,	// write saveFlag to config sector 3
-    (uint32 *)&saveFlag, sizeof(SAVE_FLAG));
+    spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE,	// write save_flag to config sector 3
+    (uint32 *)&save_flag, sizeof(Save_Flag_t));
     }
     else {
-    spi_flash_erase_sector(CFG_LOCATION + 0);					// erase config sector 0 for sysCfg
-    spi_flash_write((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE,			// write sysCfg to config sector 0
-    (uint32 *)&sysCfg, sizeof(SYSCFG));
-    saveFlag.flag = 0;								// clear saveFlag.flag
+    spi_flash_erase_sector(CFG_LOCATION + 0);					// erase config sector 0 for system_config
+    spi_flash_write((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE,			// write system_config to config sector 0
+    (uint32 *)&system_config, sizeof(System_Config_t));
+    save_flag.flag = 0;								// clear save_flag.flag
     spi_flash_erase_sector(CFG_LOCATION + 3);					// erase config sector 3
-    spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE,			// write saveFlag to config sector 3
-    (uint32 *)&saveFlag, sizeof(SAVE_FLAG));
+    spi_flash_write((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE,			// write save_flag to config sector 3
+    (uint32 *)&save_flag, sizeof(Save_Flag_t));
     }
 }
 
 void ICACHE_FLASH_ATTR cfg_load()
 {
-    #if CONFIG_DEBUG
-    uart0_sendStr("Loading Default Module Configuration...\r\n");
-    #endif
+    ESP_LOGI(TAG, "Loading Default Module Configuration...");
+
     spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE,			// read save Flag to config sector 3
-       (uint32 *)&saveFlag, sizeof(SAVE_FLAG));
-    if (saveFlag.flag == 0) {						// if saveFlag.flag == 0
-    spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE,		// write sysCfg to config sector 0
-       (uint32 *)&sysCfg, sizeof(SYSCFG));
-    } else {								// if saveFlag.flag == 1
-    spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE,		// write sysCfg to config sector 1
-       (uint32 *)&sysCfg, sizeof(SYSCFG));
+       (uint32 *)&save_flag, sizeof(Save_Flag_t));
+    if (save_flag.flag == 0) {						// if save_flag.flag == 0
+    spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE,		// write system_config to config sector 0
+       (uint32 *)&system_config, sizeof(System_Config_t));
+    } else {								// if save_flag.flag == 1
+    spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE,		// write system_config to config sector 1
+       (uint32 *)&system_config, sizeof(System_Config_t));
     }
-    if(sysCfg.cfg_holder != CFG_HOLDER){			// if sysCfg doesn't contain the proper check value
-    os_memset(&sysCfg, 0x00, sizeof sysCfg);	// clear sysCfg
+    if(system_config.cfg_holder != CFG_HOLDER){			// if system_config doesn't contain the proper check value
+    os_memset(&system_config, 0x00, sizeof(System_Config_t));	// clear system_config
 
-    sysCfg.cfg_holder = CFG_HOLDER;
+    system_config.cfg_holder = CFG_HOLDER;
 
-    os_sprintf(sysCfg.sta_ssid[0], "%s", STA_SSID);				// load STA_SSID into sysCfg
-    os_sprintf(sysCfg.sta_pwd[0], "%s", STA_PASS);				// load STA_PASS into sysCfg
-    sysCfg.sta_type[0] = STA_TYPE;								// load STA_TYPE into sysCfg
-    sysCfg.registered_stations = 1;
-    sysCfg.current_station = 0;
+    os_sprintf(system_config.sta_ssid[0], "%s", DEFAULT_STA_SSID);				// load STA_SSID into system_config
+    os_sprintf(system_config.sta_pwd[0], "%s", DEDFAULT_STA_PASS);				// load STA_PASS into system_config
+    system_config.sta_type[0] = DEFAULT_STA_TYPE;				        				// load STA_TYPE into system_config
+    system_config.registered_stations = 1;
+    system_config.current_station = 0;
 
-    wifi_get_macaddr(STATION_IF, sysCfg.device_id);
-    os_sprintf(sysCfg.mqtt_host, "%s", MQTT_HOST);							// load MQTT_HOST
-    sysCfg.mqtt_port = MQTT_PORT;											// load MQTT_PORT
-    os_sprintf(sysCfg.mqtt_user, "%s", MQTT_USER);							// load MQTT_USER
-    os_sprintf(sysCfg.mqtt_pass, "%s", MQTT_PASS);							// load MQTT_PASS
+    wifi_get_macaddr(STATION_IF, system_config.device_id);
+    os_sprintf(system_config.mqtt_host, "%s", DEFAULT_MQTT_HOST);							// load MQTT_HOST
+    system_config.mqtt_port = DEFAULT_MQTT_PORT;											// load MQTT_PORT
+    os_sprintf(system_config.mqtt_user, "%s", DEFAULT_MQTT_USER);							// load MQTT_USER
+    os_sprintf(system_config.mqtt_pass, "%s", DEFAULT_MQTT_PASS);							// load MQTT_PASS
 
-    sysCfg.security = DEFAULT_SECURITY;										/* default non ssl */
-    sysCfg.mqtt_keepalive = MQTT_KEEPALIVE;									// load MQTT_KEEPALIVE
+    system_config.security = DEFAULT_SECURITY;										/* default non ssl */
+    system_config.mqtt_keepalive = DEFAULT_MQTT_KEEPALIVE;									// load MQTT_KEEPALIVE
 
-    os_sprintf(sysCfg.ota_host, "%s", OTA_HOST);
-    sysCfg.ota_port = OTA_PORT;
+    os_sprintf(system_config.ota_host, "%s", DEFAULT_OTA_HOST);
+    system_config.ota_port = DEFAULT_OTA_PORT;
 
     cfg_save();
     }
-
-    sysCfg.module_state.all_flags = 0U; 					// clear all flags
-    sysCfg.uart_state = 0;
-    sysCfg.module_state.alive = true;
-
-    os_sprintf(mqttTopics.data, "/encoder/" MACSTR "/data", MAC2STR(sysCfg.device_id));
-    os_sprintf(mqttTopics.command, "/encoder/" MACSTR "/command", MAC2STR(sysCfg.device_id));
-    os_sprintf(mqttTopics.health, "/encoder/" MACSTR "/health", MAC2STR(sysCfg.device_id));
-
 }