/******************************************************************************
* PV` FileName: user_main.c
*******************************************************************************/
[HASHTAG]#include[/HASHTAG] "user_config.h"
[HASHTAG]#include[/HASHTAG] "ets_sys.h"
[HASHTAG]#include[/HASHTAG] "os_type.h"
[HASHTAG]#include[/HASHTAG] "osapi.h"
[HASHTAG]#include[/HASHTAG] "add_func.h"
[HASHTAG]#include[/HASHTAG] "user_interface.h"
[HASHTAG]#ifdef[/HASHTAG] USE_SRV_WEB_PORT
[HASHTAG]#include[/HASHTAG] "web_srv.h"
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#include[/HASHTAG] "driver/uart.h"
[HASHTAG]#ifdef[/HASHTAG] USE_NETBIOS
[HASHTAG]#include[/HASHTAG] "netbios.h"
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#include[/HASHTAG] "tcp_srv_conn.h"
[HASHTAG]#include[/HASHTAG] "tcp_terminal.h"
[HASHTAG]#include[/HASHTAG] "flash_eep.h"
[HASHTAG]#include[/HASHTAG] "wifi.h"
[HASHTAG]#include[/HASHTAG] "driver/spi_register.h"
[HASHTAG]#include[/HASHTAG] "driver/io_pin_func.h"
[HASHTAG]#include[/HASHTAG] "driver/dht11.h"
[HASHTAG]#include[/HASHTAG] "driver/i2c_bmp180.h"
[HASHTAG]#include[/HASHTAG] "actuat.h"
[HASHTAG]#include[/HASHTAG] "sntp.h"
//void eram_init(void);
#if DEBUGSOO > 1
//#define TEST_TIMER 1
//#include "driver/int_time_us.h"
//#define TEST_RTC_RTNTN 1
[HASHTAG]#endif[/HASHTAG]
LOCAL os_timer_t DebounceTimer;
LOCAL os_timer_t sensor_timer;
LOCAL void input_intr_handler(void *arg);
void ICACHE_FLASH_ATTR debounce_timer_cb(void *arg);
#if (!defined(USE_ESPCONN)) && (SDK_VERSION > 959)
uint32 ICACHE_FLASH_ATTR espconn_init(uint32 x) {
return 1;
}
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#ifdef[/HASHTAG] USE_RESET_CFG_WIFI
void Test_ClrCFG(void);
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#ifdef[/HASHTAG] TEST_RTC_RTNTN
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... ");
[HASHTAG]#endif[/HASHTAG]
if(!system_rtc_mem_read(64, x, sizeof(x))) {
#if DEBUGSOO > 1
os_printf("read error!\n");
[HASHTAG]#endif[/HASHTAG]
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;
[HASHTAG]#endif[/HASHTAG]
};
#if DEBUGSOO > 1
os_printf("changes, new write\n");
[HASHTAG]#endif[/HASHTAG]
return false;
};
#if DEBUGSOO > 1
os_printf("Ok.\n");
[HASHTAG]#endif[/HASHTAG]
return true;
}
[HASHTAG]#endif[/HASHTAG]
void init_done_cb(void)
{
PIN_FUNC_DEFAULT(12);
PIN_FUNC_DEFAULT(14);
set_gpio_io_pin();
os_printf("\nSDK Init - Ok\nCurrent 'heap' size: %d bytes\n", system_get_free_heap_size());
[HASHTAG]#ifdef[/HASHTAG] TEST_RTC_RTNTN
test_rtc_mem();
[HASHTAG]#endif[/HASHTAG]
}
void BtnInit()
{
// Set GPIO 0 to IO
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO0);
// Enable pull up R
PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDI_U);
// Disable interrupts
ETS_GPIO_INTR_DISABLE();
// Attach pin 0 to the interrupt thing
ETS_GPIO_INTR_ATTACH(input_intr_handler, NULL);
GPIO_DIS_OUTPUT(BTN_CONFIG_GPIO);
// Clear gpio status
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(0));
// Enable interrupt
gpio_pin_intr_state_set(GPIO_ID_PIN(BTN_CONFIG_GPIO), GPIO_PIN_INTR_NEGEDGE);
// Global re-enable interrupts
ETS_GPIO_INTR_ENABLE();
os_timer_disarm(&DebounceTimer);
os_timer_setfn(&DebounceTimer, &debounce_timer_cb, 0);
}
void ICACHE_FLASH_ATTR debounce_timer_cb(void *arg)
{
ETS_GPIO_INTR_DISABLE();
gpio_pin_intr_state_set(GPIO_ID_PIN(BTN_CONFIG_GPIO), GPIO_PIN_INTR_NEGEDGE);
ETS_GPIO_INTR_ENABLE();
#if DEBUGSOO > 0
ets_uart_printf("Button CONFMODE pressed, wiping configuration and restart in configuration mode...\r\n");
[HASHTAG]#endif[/HASHTAG]
uint32 wifi_set_err = Setup_WiFi_Def();
#if DEBUGSOO > 0
ets_uart_printf("Restarting in STATIONAP mode...\r\n");
[HASHTAG]#endif[/HASHTAG]
system_restart();
}
LOCAL void input_intr_handler(void *arg)
{
// Not that sure what this does yet and where the register is used for
uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
// Сlear interrupt status
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);
// Disable interrupt
ETS_GPIO_INTR_DISABLE();
gpio_pin_intr_state_set(GPIO_ID_PIN(BTN_CONFIG_GPIO), GPIO_PIN_INTR_DISABLE);
// Enable interrupt
ETS_GPIO_INTR_ENABLE();
os_timer_arm(&DebounceTimer, 200, FALSE);
}
LOCAL void ICACHE_FLASH_ATTR sensor_timer_cb(void *arg)
{
Read_sensor_vol();
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR user_init(void) {
// PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
// PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
uart_init(BIT_RATE_115200, BIT_RATE_230400);
[HASHTAG]#ifdef[/HASHTAG] USE_US_TIMER
system_timer_reinit();
[HASHTAG]#endif[/HASHTAG]
DHTInit();
i2c_init();
BMP180_Init();
BtnInit();
os_timer_disarm(&sensor_timer);
os_timer_setfn(&sensor_timer, (os_timer_func_t *)sensor_timer_cb, (void *)0);
os_timer_arm(&sensor_timer, 1000, 1);
#if DEBUGSOO > 0
os_printf("\nSimple WEB version: " WEB_SVERSION "\nSDK version: %s\n", system_get_sdk_version());
[HASHTAG]#else[/HASHTAG]
[HASHTAG]#endif[/HASHTAG]
sys_read_cfg();
[HASHTAG]#ifdef[/HASHTAG] USE_ESPCONN
if (espconn_tcp_get_max_con() != 5) {
espconn_tcp_set_max_con(5);
system_restart();
}
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#ifdef[/HASHTAG] USE_RESET_CFG_WIFI
if(syscfg.cfg.b.pin_clear_cfg_enable) Test_ClrCFG();
[HASHTAG]#endif[/HASHTAG]
if(syscfg.cfg.b.hi_speed_enable) {
ets_intr_lock();
REG_SET_BIT(0x3ff00014, BIT(0));
os_update_cpu_frequency(160);
ets_intr_unlock();
// CLEAR_PERI_REG_MASK(SPI_CTRL1(0), SPI_CS_DELAY_OFF << SPI_CS_DELAY_OFF_S); // убрать задержку CS у Flash
}
else {
// REG_CLR_BIT(0x3ff00014, BIT(0));
// os_update_cpu_frequency(80);
}
set_gpio_io_pin();
if(!syscfg.cfg.b.debug_print_enable) system_set_os_print(0);
eram_init();
#if DEBUGSOO > 0
os_printf("System memory:\n");
system_print_meminfo();
os_printf("Current 'heap' size: %d bytes\n", system_get_free_heap_size());
[HASHTAG]#endif[/HASHTAG]
#if DEBUGSOO > 0
os_printf("Set CPU CLK: %u MHz\n", ets_get_cpu_frequency());
[HASHTAG]#endif[/HASHTAG]
Setup_WiFi();
[HASHTAG]#ifdef[/HASHTAG] USE_NETBIOS
if(syscfg.cfg.b.netbios_ena) netbios_init();
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#ifdef[/HASHTAG] USE_SNTP
if(syscfg.cfg.b.sntp_ena) sntp_init();
[HASHTAG]#endif[/HASHTAG]
[HASHTAG]#ifdef[/HASHTAG] UDP_TEST_PORT
if(syscfg.udp_port) udp_test_port_init(syscfg.udp_port);
[HASHTAG]#endif[/HASHTAG]
// инициализация и запуск tcp серверa(ов)
[HASHTAG]#ifdef[/HASHTAG] USE_SRV_WEB_PORT
if(syscfg.web_port) webserver_init(syscfg.web_port);
[HASHTAG]#endif[/HASHTAG]
if(syscfg.term_port) tcp_term_init(syscfg.term_port);
system_deep_sleep_set_option(0);
system_init_done_cb(init_done_cb);
}
/*
int main(void)
{
return 0;
}
*/