/** * set new mode * @param m WiFiMode_t */ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { if(_persistent){ if(wifi_get_opmode() == (uint8) m && wifi_get_opmode_default() == (uint8) m){ return true; } } else if(wifi_get_opmode() == (uint8) m){ return true; } bool ret = false; if (m != WIFI_STA && m != WIFI_AP_STA) // calls lwIP's dhcp_stop(), // safe to call even if not started wifi_station_dhcpc_stop(); ETS_UART_INTR_DISABLE(); if(_persistent) { ret = wifi_set_opmode(m); } else { ret = wifi_set_opmode_current(m); } ETS_UART_INTR_ENABLE(); return ret; }
/** * @brief Setup commad of join to wifi ap. * @param id: commad id number * @param pPara: AT input param * @retval None */ static void ICACHE_FLASH_ATTR wifi_connect(char * ssid, char * password, char * bssid) { char temp[64]; struct station_config stationConf; int8_t len; connect_attempts++; //wifi_station_get_config(&stationConf); os_bzero(&stationConf, sizeof(struct station_config)); os_memcpy(&stationConf.ssid, ssid, os_strlen(ssid)); //os_memcpy(&stationConf.password, password, os_strlen(password)); os_memcpy(&stationConf.bssid, bssid, 6); stationConf.bssid_set = 1; wifi_station_disconnect(); os_printf("stationConf.ssid: -%s-\r\n", stationConf.ssid); os_printf("stationConf.password: -%s-\r\n", stationConf.password); ETS_UART_INTR_DISABLE(); wifi_station_set_config(&stationConf); ETS_UART_INTR_ENABLE(); wifi_station_connect(); os_timer_disarm(&at_japDelayChack); os_timer_setfn(&at_japDelayChack, (os_timer_func_t *)at_japChack, NULL); os_timer_arm(&at_japDelayChack, 3000, 0); }
size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size) { if(uart == NULL || !uart->rx_enabled) { return 0; } if(uart->rx_buffer->size == new_size) { return uart->rx_buffer->size; } uint8_t * new_buf = (uint8_t*)malloc(new_size); if(!new_buf) { return uart->rx_buffer->size; } size_t new_wpos = 0; ETS_UART_INTR_DISABLE(); while(uart_rx_available(uart) && new_wpos < new_size) { new_buf[new_wpos++] = uart_read_char(uart); } uint8_t * old_buf = uart->rx_buffer->buffer; uart->rx_buffer->rpos = 0; uart->rx_buffer->wpos = new_wpos; uart->rx_buffer->size = new_size; uart->rx_buffer->buffer = new_buf; free(old_buf); ETS_UART_INTR_ENABLE(); return uart->rx_buffer->size; }
/** * @brief Setup commad of set wifi mode. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCwmode(uint8_t id, char *pPara) { uint8_t mode; char temp[32]; pPara++; mode = atoi(pPara); if(mode == at_wifiMode) { uart0_sendStr("no change\r\n"); return; } if((mode >= 1) && (mode <= 3)) { ETS_UART_INTR_DISABLE(); wifi_set_opmode(mode); ETS_UART_INTR_ENABLE(); at_backOk; // system_restart(); } else { at_backError; } }
int ESP8266WiFiClass::disconnect(bool wifioff) { struct station_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); if (_persistent) wifi_station_set_config(&conf); else wifi_station_set_config_current(&conf); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); if(wifioff) { _useClientMode = false; if(_useApMode) { // turn on AP _mode(WIFI_AP); } else { // turn wifi off _mode(WIFI_OFF); } } return 0; }
static char * ICACHE_FLASH_ATTR readRx(RcvMsgBuff *rxBfr) { static int idx = 0; static char bfr[400]; char c; ETS_UART_INTR_DISABLE(); while (rxBfr->count > 0) { c = *(rxBfr->pReadPos); rxBfr->count--; rxBfr->pReadPos++; if (rxBfr->pReadPos == (rxBfr->pRcvMsgBuff + RX_BUFF_SIZE)) { rxBfr->pReadPos = rxBfr->pRcvMsgBuff ; } if (c == '\r' || c == '\n') { if (idx > 0) { // Otherwise ignore blank line bfr[idx] = 0; idx = 0; ETS_UART_INTR_ENABLE(); return bfr; } } else { if (idx < sizeof(bfr)-3) { bfr[idx++] = c; } else { ERRORP("Bfr overflow\n"); } } } ETS_UART_INTR_ENABLE(); return NULL; }
/****************************************************************************** * FunctionName : uart0_rx_intr_handler * Description : Internal used function * UART0 interrupt handler, add self handle code inside * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg * Returns : NONE *******************************************************************************/ static void // must not use ICACHE_FLASH_ATTR ! uart0_rx_intr_handler(void *para) { // we assume that uart1 has interrupts disabled (it uses the same interrupt vector) uint8 uart_no = UART0; const uint32 one_sec = 1000000; // one second in usecs // we end up largely ignoring framing errors and we just print a warning every second max if (READ_PERI_REG(UART_INT_RAW(uart_no)) & UART_FRM_ERR_INT_RAW) { uint32 now = system_get_time(); if (last_frm_err == 0 || (now - last_frm_err) > one_sec) { os_printf("UART framing error (bad baud rate?)\n"); last_frm_err = now; } // clear rx fifo (apparently this is not optional at this point) SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST); CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST); // reset framing error WRITE_PERI_REG(UART_INT_CLR(UART0), UART_FRM_ERR_INT_CLR); // once framing errors are gone for 10 secs we forget about having seen them } else if (last_frm_err != 0 && (system_get_time() - last_frm_err) > 10*one_sec) { last_frm_err = 0; } if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST) || UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) { //DBG_UART("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); ETS_UART_INTR_DISABLE(); post_usr_task(uart_recvTaskNum, 0); } }
/** * Setting the ESP8266 station to connect to the AP (which is recorded) * automatically or not when powered on. Enable auto-connect by default. * @param autoConnect bool * @return if saved */ bool ESP8266WiFiSTAClass::setAutoConnect(bool autoConnect) { bool ret; ETS_UART_INTR_DISABLE(); ret = wifi_station_set_auto_connect(autoConnect); ETS_UART_INTR_ENABLE(); return ret; }
void user_init(void) { ETS_UART_INTR_DISABLE(); UART_SetBaudrate(UART0, BIT_RATE_9600); UART_ResetFifo(UART0); UART_SetBaudrate(UART1, BIT_RATE_115200); UART_ResetFifo(UART1); flash_param_init(); flash_param = flash_param_get(); emsRxBuf = allocateRcvMsgBuff(); uart_init(BIT_RATE_9600, BIT_RATE_115200); rtc_clock_calibration = system_rtc_clock_cali_proc(); // get RTC clock period os_printf("rtc_clock_calibration: %0x\n", rtc_clock_calibration >>12 ); os_printf("system_get_rtc_time: %d\n", system_get_rtc_time()); os_printf("system_get_time: %d\n", system_get_time()); serverInit(flash_param->port); wifi_set_sleep_type(LIGHT_SLEEP_T); system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen); ETS_UART_INTR_ENABLE(); }
void ESP8266WiFiClass::mode(WiFiMode m) { if(wifi_get_opmode() == (uint8)m) { return; } ETS_UART_INTR_DISABLE(); wifi_set_opmode(m); ETS_UART_INTR_ENABLE(); }
/** * @brief Setup commad of module as wifi ap. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCwsap(uint8_t id, char *pPara) { char temp[64]; int8_t len; struct softap_config apConfig; wifi_softap_get_config(&apConfig); if(at_wifiMode == STATION_MODE) { at_backError; return; } pPara++; len = at_dataStrCpy(apConfig.ssid, pPara, 32); if(len < 1) { uart0_sendStr("ssid ERROR\r\n"); return; } pPara += (len+3); len = at_dataStrCpy(apConfig.password, pPara, 64); if(len < 8) { uart0_sendStr("pwd ERROR\r\n"); return; } pPara += (len+3); apConfig.channel = atoi(pPara); if(apConfig.channel<1 || apConfig.channel>13) { uart0_sendStr("ch ERROR\r\n"); return; } pPara++; pPara = strchr(pPara, ','); pPara++; apConfig.authmode = atoi(pPara); if(apConfig.authmode >= 5) { uart0_sendStr("s ERROR\r\n"); return; } // os_sprintf(temp,"%s,%s,%d,%d\r\n", // apConfig.ssid, // apConfig.password, // apConfig.channel, // apConfig.authmode); // uart0_sendStr(temp); ETS_UART_INTR_DISABLE(); wifi_softap_set_config(&apConfig); ETS_UART_INTR_ENABLE(); at_backOk; // system_restart(); }
void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { _useApMode = true; if(_useClientMode) { // turn on AP+STA mode _mode(WIFI_AP_STA); } else { // turn on STA mode _mode(WIFI_AP); } if(!ssid || *ssid == 0 || strlen(ssid) > 31) { // fail SSID too long or missing! return; } if(passphrase && strlen(passphrase) > 63) { // fail passphrase to long! return; } struct softap_config conf; wifi_softap_get_config(&conf); strcpy(reinterpret_cast<char*>(conf.ssid), ssid); conf.channel = channel; conf.ssid_len = strlen(ssid); conf.ssid_hidden = ssid_hidden; conf.max_connection = 4; conf.beacon_interval = 100; if (!passphrase || strlen(passphrase) == 0) { conf.authmode = AUTH_OPEN; *conf.password = 0; } else { conf.authmode = AUTH_WPA2_PSK; strcpy(reinterpret_cast<char*>(conf.password), passphrase); } struct softap_config conf_current; wifi_softap_get_config(&conf_current); if (softap_config_equal(conf, conf_current)) { DEBUGV("softap config unchanged"); return; } ETS_UART_INTR_DISABLE(); if (_persistent) wifi_softap_set_config(&conf); else wifi_softap_set_config_current(&conf); ETS_UART_INTR_ENABLE(); }
int ESP8266WiFiClass::begin() { ETS_UART_INTR_DISABLE(); wifi_station_connect(); ETS_UART_INTR_ENABLE(); if(!_useStaticIp) wifi_station_dhcpc_start(); return status(); }
int ESP8266WiFiClass::disconnect() { struct station_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); wifi_station_set_config(&conf); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); return 0; }
void EEPROMClass::commit() { if (!_size || !_dirty) return; ETS_UART_INTR_DISABLE(); spi_flash_erase_sector(CONFIG_SECTOR); spi_flash_write(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size); ETS_UART_INTR_ENABLE(); _dirty = false; }
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, uint8_t bssid[6]){ _useClientMode = true; if(_useApMode) { // turn on AP+STA mode mode(WIFI_AP_STA); } else { // turn on STA mode mode(WIFI_STA); } if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { // fail SSID to long or missing! return WL_CONNECT_FAILED; } if(passphrase && strlen(passphrase) > 63) { // fail passphrase to long! return WL_CONNECT_FAILED; } struct station_config conf; strcpy(reinterpret_cast<char*>(conf.ssid), ssid); if (passphrase) { strcpy(reinterpret_cast<char*>(conf.password), passphrase); } else { *conf.password = 0; } if (bssid) { conf.bssid_set = 1; memcpy((void *) &conf.bssid[0], (void *) bssid, 6); } else { conf.bssid_set = 0; } ETS_UART_INTR_DISABLE(); wifi_station_set_config(&conf); wifi_station_connect(); ETS_UART_INTR_ENABLE(); if(channel > 0 && channel <= 13) { wifi_set_channel(channel); } if(!_useStaticIp) wifi_station_dhcpc_start(); return status(); }
void ESP8266WiFiClass::_mode(WiFiMode m) { if(wifi_get_opmode() == (uint8)m) { return; } ETS_UART_INTR_DISABLE(); if (_persistent) wifi_set_opmode(m); else wifi_set_opmode_current(m); ETS_UART_INTR_ENABLE(); }
void ICACHE_FLASH_ATTR flash_read() { os_printf("flashRead() size-%d\n", sizeof(FlashData)); flashData->magic =0; ETS_UART_INTR_DISABLE(); spi_flash_read((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE, (uint32 *)flashData, sizeof(FlashData)); ETS_UART_INTR_ENABLE(); if (flashData->magic != MAGIC_NUM) { os_printf("ReadFlash ERROR!\n"); initFlash(); } }
void ICACHE_RAM_ATTR uart_isr(void * arg) { uart_t* uart = (uart_t*)arg; if(uart == NULL || !uart->rx_enabled) { USIC(uart->uart_nr) = 0xffff; ETS_UART_INTR_DISABLE(); return; } uint32_t int_status = USIS(uart->uart_nr); if(int_status & (1 << UIFR)) { USIC(uart->uart_nr) = (1 << UIFR);//clear any frame error } if(int_status & (1 << UIFF) || int_status & (1 << UITO)){ ETS_UART_INTR_DISABLE(); while(((USS(uart->uart_nr) >> USRXC) & 0x7F) != 0){ uint8_t data = USF(uart->uart_nr); size_t nextPos = (uart->rx_buffer->wpos + 1) % uart->rx_buffer->size; if(nextPos != uart->rx_buffer->rpos) { uart->rx_buffer->buffer[uart->rx_buffer->wpos] = data; uart->rx_buffer->wpos = nextPos; } else { //rx buffer OverFlow //maybe stop the loop and try later? } } int_status = USIS(uart->uart_nr); if(int_status & (1 << UIFF)) { USIC(uart->uart_nr) = (1 << UIFF);//clear any FIFO FULL error } if(int_status & (1 << UITO)) { USIC(uart->uart_nr) = (1 << UITO);//clear any TimeOut error } ETS_UART_INTR_ENABLE(); }
void ICACHE_FLASH_ATTR flash_write() { os_printf("flashWrite() size-%d\n", sizeof(FlashData)); ETS_UART_INTR_DISABLE(); spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE); spi_flash_write(0x3c000, (uint32 *)flashData, sizeof(FlashData)); ETS_UART_INTR_ENABLE(); // spi_flash_read(0x3C000, (uint32 *) settings, 1024); // spi_flash_erase_sector(0x3C); // spi_flash_write(0x3C000,(uint32 *)settings,1024); }
// Turn UART interrupts off and poll for nchars or until timeout hits uint16_t ICACHE_FLASH_ATTR uart0_rx_poll(char *buff, uint16_t nchars, uint32_t timeout_us) { ETS_UART_INTR_DISABLE(); uint16_t got = 0; uint32_t start = system_get_time(); // time in us while (system_get_time()-start < timeout_us) { while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { buff[got++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; if (got == nchars) goto done; } } done: ETS_UART_INTR_ENABLE(); return got; }
/** * Use to connect to SDK config. * @return wl_status_t */ wl_status_t ESP8266WiFiSTAClass::begin() { if(!WiFi.enableSTA(true)) { // enable STA failed return WL_CONNECT_FAILED; } ETS_UART_INTR_DISABLE(); wifi_station_connect(); ETS_UART_INTR_ENABLE(); if(!_useStaticIp) { wifi_station_dhcpc_start(); } return status(); }
/** * @brief Setup commad of join to wifi ap. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCwjap(uint8_t id, char *pPara) { char temp[64]; struct station_config stationConf; int8_t len; if (at_wifiMode == SOFTAP_MODE) { at_backError; return; } pPara++; len = at_dataStrCpy(&stationConf.ssid, pPara, 32); if(len != -1) { pPara += (len+3); len = at_dataStrCpy(&stationConf.password, pPara, 64); } if(len != -1) { wifi_station_disconnect(); mdState = m_wdact; ETS_UART_INTR_DISABLE(); wifi_station_set_config(&stationConf); ETS_UART_INTR_ENABLE(); wifi_station_connect(); // if(1) // { // mdState = m_wact; // } // os_sprintf(temp,"%s:%s,%s\r\n", // at_fun[id].at_cmdName, // stationConf.ssid, // stationConf.password); // uart0_sendStr(temp); os_timer_disarm(&at_japDelayChack); os_timer_setfn(&at_japDelayChack, (os_timer_func_t *)at_japChack, NULL); os_timer_arm(&at_japDelayChack, 3000, 0); specialAtState = FALSE; } else { at_backError; } }
/** * set new mode * @param m WiFiMode_t */ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { if(wifi_get_opmode() == (uint8) m) { return true; } bool ret = false; ETS_UART_INTR_DISABLE(); if(_persistent) { ret = wifi_set_opmode(m); } else { ret = wifi_set_opmode_current(m); } ETS_UART_INTR_ENABLE(); return ret; }
bool EEPROMClass::commit() { bool ret = false; if (!_size) return false; if(!_dirty) return true; ETS_UART_INTR_DISABLE(); if(spi_flash_erase_sector(CONFIG_SECTOR) == SPI_FLASH_RESULT_OK) { if(spi_flash_write(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) { _dirty = false; ret = true; } } ETS_UART_INTR_ENABLE(); return ret; }
void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel) { if(_useClientMode) { // turn on AP+STA mode mode(WIFI_AP_STA); } else { // turn on STA mode mode(WIFI_AP); } if(!ssid || strlen(ssid) > 31) { // fail SSID to long or missing! return; } if(passphrase && strlen(passphrase) > 63) { // fail passphrase to long! return; } struct softap_config conf; wifi_softap_get_config(&conf); strcpy(reinterpret_cast<char*>(conf.ssid), ssid); conf.channel = channel; conf.ssid_len = strlen(ssid); conf.ssid_hidden = 0; conf.max_connection = 4; conf.beacon_interval = 100; if (!passphrase || strlen(passphrase) == 0) { conf.authmode = AUTH_OPEN; *conf.password = 0; } else { conf.authmode = AUTH_WPA2_PSK; strcpy(reinterpret_cast<char*>(conf.password), passphrase); } ETS_UART_INTR_DISABLE(); wifi_softap_set_config(&conf); ETS_UART_INTR_ENABLE(); }
/** * @brief Setup commad of join to wifi ap. * @param id: commad id number * @param pPara: AT input param * @retval None */ void ICACHE_FLASH_ATTR at_setupCmdCwjap(uint8_t id, char *pPara) { char temp[64]; struct station_config stationConf; int8_t len; if (at_wifiMode == SOFTAP_MODE) { at_backError; return; } pPara++; len = at_dataStrCpy(&stationConf.ssid, pPara, 32); if(len != -1) { pPara += (len+3); len = at_dataStrCpy(&stationConf.password, pPara, 64); } if(len != -1) { ETS_UART_INTR_DISABLE(); wifi_station_set_config(&stationConf); ETS_UART_INTR_ENABLE(); wifi_station_connect(); if(1) { mdState = m_wact; } // os_sprintf(temp,"%s:%s,%s\r\n", // at_fun[id].at_cmdName, // stationConf.ssid, // stationConf.password); // uart0_sendStr(temp); at_backOk; } else { at_backError; } }
/** * Disconnect from the network * @param wifioff * @return one value of wl_status_t enum */ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { bool ret; struct station_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { wifi_station_set_config(&conf); } else { wifi_station_set_config_current(&conf); } ret = wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); if(wifioff) { WiFi.enableSTA(false); } return ret; }
void ESP8266WiFiClass::mode(WiFiMode m) { if(wifi_get_opmode() == (uint8)m) { return; } if((m & WIFI_AP)) { _useApMode = true; } else { _useApMode = false; } if((m & WIFI_STA)) { _useClientMode = true; } else { _useClientMode = false; } ETS_UART_INTR_DISABLE(); wifi_set_opmode(m); ETS_UART_INTR_ENABLE(); }
/** * @brief Transparent data through ip. * @param arg: no used * @retval None */ void ICACHE_FLASH_ATTR at_ipDataTransparent(void *arg) { if(at_state != at_statIpTraning) { return; } // if(ipDataSendFlag) // { // return; // } // ETS_UART_INTR_DISABLE(); // os_timer_disarm(&at_delayChack); if((at_tranLen == 3) && (os_memcmp(at_dataLine, "+++", 3) == 0)) //UartDev.rcv_buff.pRcvMsgBuff { // ETS_UART_INTR_DISABLE(); // specialAtState = TRUE; at_state = at_statIdle; // ETS_UART_INTR_ENABLE(); // IPMODE = FALSE; return; } else if(at_tranLen) { ETS_UART_INTR_DISABLE(); // espconn_sent(pLink[0].pCon, at_dataLine, at_tranLen); //UartDev.rcv_buff.pRcvMsgBuff //// ipDataSendFlag = 1; // pDataLine = UartDev.rcv_buff.pRcvMsgBuff; pDataLine = at_dataLine; at_tranLen = 0; return; } os_timer_arm(&at_delayChack, 20, 0); // system_os_post(at_recvTaskPrio, 0, 0); //// // ETS_UART_INTR_ENABLE(); }