Exemplo n.º 1
0
void ICACHE_FLASH_ATTR
app_load(void)
{
	system_param_load(
	    (APP_START_SEC),
	    0,
	    (void *)(&sys_status),
	    sizeof(sys_status));

	uint32_t warm_boot = 0;
	system_rtc_mem_read(64+20, (void *)&warm_boot, sizeof(warm_boot));
	//INFO("rtc warm_boot = %X\r\n", warm_boot);

	if (sys_status.init_flag) {
		if (warm_boot != 0x66AA) {
			INFO("Cold boot up, set the switch on!\r\n");
			sys_status.mcu_status.s = 1;

			warm_boot = 0x66AA;
			system_rtc_mem_write(64+20, (void *)&warm_boot, sizeof(warm_boot));
		} else {
			INFO("Warm boot up, use the status saved in flash!\r\n");
		}
	} else {
		sys_status.init_flag = 1;
	}
	sys_status.start_count += 1;
	sys_status.start_continue += 1;
	app_save();
}
Exemplo n.º 2
0
void user_rf_pre_init(void) {
    RESET_COUNTER counter;
    if(system_get_rst_info()->reason != REASON_EXT_SYS_RST) {
        reset_counter(0);
    } else {
        system_rtc_mem_read(64, &counter, sizeof(counter));
        if(counter.magic == RESET_COUNTER_MAGIC && counter.resetCounter <= RESET_NUM) {
            counter.resetCounter++;
            if(counter.resetCounter == RESET_NUM) {
                reset_counter(0);
                mSpecialMode = 1;
            } else {
                system_rtc_mem_write(RESET_COUNTER_RTC_ADDRESS, &counter, sizeof(counter));
                os_timer_disarm(&mResetTimer);
                os_timer_setfn(&mResetTimer, (os_timer_func_t *)reset_counter, NULL);
                os_timer_arm(&mResetTimer, 1000, 0);
            }
        } else {
            reset_counter(0);
        }
    }

    system_restore();
    rtc_mem_check(0);
}
Exemplo n.º 3
0
bool ICACHE_FLASH_ATTR
persist_load()
{
    bool rv = false;

    rv = system_rtc_mem_read(USER_DATA_OFFSET/4, &data, sizeof(data));

    dirty = false;

    return rv;
}
Exemplo n.º 4
0
bool TaskManager::RestartedFromSleep(void* state, uint16_t sizeofState)
{
    rst_info* resetInfo = ESP.getResetInfoPtr();
    bool wasSleeping = (resetInfo && REASON_DEEP_SLEEP_AWAKE == resetInfo->reason);
    if (wasSleeping)
    {
        if (state != NULL && sizeofState > 0)
        {
            system_rtc_mem_read(RTC_MEM_SLEEP_ADDR, state, sizeofState);
        }
    }
    return wasSleeping;
}
	bool read() {
		// Read memory to temporary variable to retain initial values in struct T.
		rtc_memory<T> read;

		// An initial rtc memory is random.
		bool ok = system_rtc_mem_read(USER_DATA_ADDR, &read, sizeof(read));
		if (ok) {
			// Only hashes are matched and copy to struct.
			if (read.hash == calc_hash(read.data)) {
				memcpy(this, &read, sizeof(read));
			}
		}
		return ok;
	}
Exemplo n.º 6
0
/******************************************************************************
 * FunctionName : user_plug_long_press
 * Description  : key's long press function, needed to be installed
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
LOCAL void  
user_key_long_press(void)
{
    int boot_flag=12345;
 //   user_esp_platform_set_active(0);
    system_restore();
    
    system_rtc_mem_write(70, &boot_flag, sizeof(boot_flag));
    system_rtc_mem_read(70, &boot_flag, sizeof(boot_flag));

#if RESTORE_KEEP_TIMER
    user_platform_timer_bkup();
#endif 

    system_restart();
}
void ICACHE_FLASH_ATTR data_func() {
    // Read out the sensor data structure from RTC memory
    system_rtc_mem_read( SENSOR_DATA_MEM_ADDR, &sensor_data, sizeof(SENSOR_DATA_RTC_MEM) );
    
    // When the system powers on for the first time, the data in the rtc memory is random.
    struct esp_platform_saved_param esp_param_t;
    user_esp_platform_load_param(&esp_param_t);  // Stored in flash
    // Load user params to check if the device was successfully registered to the server
    // If it wasn't, it usually returns 255 (from the flash.)

    if(sensor_data.init_flg!=INIT_MAGIC || sensor_data.cnt>SENSOR_DATA_NUM ) {
        // This case runs when we first power on or when it time to flush the RTC memory of old data.
        if(esp_param_t.activeflag!=1) {   // If registered & activated
            user_esp_platform_init();     // Router is not configured. Setup softAP. Wait for config. 
            #ifdef SERVER_SSL_ENABLE
            user_webserver_init(SERVER_SSL_PORT);
            #else
            user_webserver_init(SERVER_PORT);
            #endif
        } else { // was connected! So we set init magic to exit the setup loop
            sensor_data.init_flg = INIT_MAGIC;
            sensor_data.cnt = 0;
            system_rtc_mem_write(SENSOR_DATA_MEM_ADDR, &sensor_data, sizeof(SENSOR_DATA_RTC_MEM));
            __SET__DEEP_SLEEP__WAKEUP_NO_RF__; 
            system_deep_sleep(100000); 
        }
    } else { // This is where the measurements are made
        uint16 vdd_val = 0;
        if(sensor_data.cnt<0 || sensor_data.cnt>=SENSOR_DATA_NUM) 
            sensor_data.cnt=0; // range check and resets counter if needed

        /* Reads power supply voltage, byte 107 of init_data.bin should be set to 0xFF.
        *  Replace with your own code.*/
        sensor_data.data[sensor_data.cnt++] = (uint16)(phy_get_vdd33());
        system_rtc_mem_write( SENSOR_DATA_MEM_ADDR, &sensor_data, sizeof(SENSOR_DATA_RTC_MEM) );

        // Setup next sleep cycle
        if(sensor_data.cnt==SENSOR_DATA_NUM-1) { __SET__DEEP_SLEEP__WAKEUP_NORMAL__; }
        else { __SET__DEEP_SLEEP__WAKEUP_NO_RF__; }

        // Uploads or go to sleep
        if(sensor_data.cnt == SENSOR_DATA_NUM) { user_esp_platform_init(); }
        else { system_deep_sleep(SENSOR_DEEP_SLEEP_TIME); }
    }
}
Exemplo n.º 8
0
bool ICACHE_FLASH_ATTR test_rtc_mem(void) {
	uint32 x[128];
	uint32 i, t = 0x43545240;
	bool chg = false;
#if DEBUGSOO > 1
	os_printf("Test rtc memory retention... ");
#endif
	if(!system_rtc_mem_read(64, x, sizeof(x))) {
#if DEBUGSOO > 1
		os_printf("read error!\n");
#endif
		return false;
	}
	for(i = 0; i < 128; i++) {
		if(x[i] != t) {
			x[i] = t;
			chg = true;
		};
		t++;
	};
	if(chg) {
		if(!system_rtc_mem_write(64, x, sizeof(x))) {
#if DEBUGSOO > 1
			os_printf("write error!\n");
			return false;
#endif
		};
#if DEBUGSOO > 1
		os_printf("changes, new write\n");
#endif
		return false;
	};
#if DEBUGSOO > 1
	os_printf("Ok.\n");
#endif
	return true;
}
Exemplo n.º 9
0
/******************************************************************************
 * FunctionName : user_esp_platform_init
 * Description  : device parame init based on espressif platform
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_init(void)
{

	os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\
	IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG);
	os_printf("IOT VERSION = %s\n",iot_version);

	system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param));

	struct rst_info *rtc_info = system_get_rst_info();

	os_printf("reset reason: %x\n", rtc_info->reason);

	if (rtc_info->reason == REASON_WDT_RST ||
		rtc_info->reason == REASON_EXCEPTION_RST ||
		rtc_info->reason == REASON_SOFT_WDT_RST) {
		if (rtc_info->reason == REASON_EXCEPTION_RST) {
			os_printf("Fatal exception (%d):\n", rtc_info->exccause);
		}
		os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n",
				rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc);
	}

	/***add by tzx for saving ip_info to avoid dhcp_client start****/
    struct dhcp_client_info dhcp_info;
    struct ip_info sta_info;
    system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info));
	if(dhcp_info.flag == 0x01 ) {
		if (true == wifi_station_dhcpc_status())
		{
			wifi_station_dhcpc_stop();
		}
		sta_info.ip = dhcp_info.ip_addr;
		sta_info.gw = dhcp_info.gw;
		sta_info.netmask = dhcp_info.netmask;
		if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) {
			os_printf("set default ip wrong\n");
		}
	}
    os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info));
    system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info));


#if AP_CACHE
    wifi_station_ap_number_set(AP_CACHE_NUMBER);
#endif

#if 0
    {
        char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
        char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab};
        struct ip_info info;

        wifi_set_macaddr(SOFTAP_IF, sofap_mac);
        wifi_set_macaddr(STATION_IF, sta_mac);

        IP4_ADDR(&info.ip, 192, 168, 3, 200);
        IP4_ADDR(&info.gw, 192, 168, 3, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        wifi_set_ip_info(STATION_IF, &info);

        IP4_ADDR(&info.ip, 10, 10, 10, 1);
        IP4_ADDR(&info.gw, 10, 10, 10, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        wifi_set_ip_info(SOFTAP_IF, &info);
    }
#endif

    if (esp_param.activeflag != 1) {
#ifdef SOFTAP_ENCRYPT
        struct softap_config config;
        char password[33];
        char macaddr[6];

        wifi_softap_get_config(&config);
        wifi_get_macaddr(SOFTAP_IF, macaddr);

        os_memset(config.password, 0, sizeof(config.password));
        os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD);
        os_memcpy(config.password, password, os_strlen(password));
        config.authmode = AUTH_WPA_WPA2_PSK;

        wifi_softap_set_config(&config);
#endif
		
		wifi_station_set_hostname( HOST_NAME );
        wifi_set_opmode(STATION_MODE);
    }

#if SENSOR_DEVICE
    user_sensor_init(esp_param.activeflag);
#endif

#if 0
    if (wifi_get_opmode() != SOFTAP_MODE) {
        os_timer_disarm(&client_timer);
        os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1);
        os_timer_arm(&client_timer, 100, 0);
    }
	
	// 2015-12-27 
	single_key[0] = key_init_single( 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, NULL, GPIO_INTER);
	keys.key_num = 1;
	keys.single_key = single_key;
	key_init(&keys);
#endif
}
Exemplo n.º 10
0
bool ICACHE_FLASH_ATTR rboot_get_rtc_data(rboot_rtc_data *rtc) {
	if (system_rtc_mem_read(RBOOT_RTC_ADDR, rtc, sizeof(rboot_rtc_data))) {
		return (rtc->chksum == calc_chksum((uint8*)rtc, (uint8*)&rtc->chksum));
	}
	return false;
}