From 95007b229da203d9a4d5a847914f5304add50447 Mon Sep 17 00:00:00 2001 From: ficeto Date: Mon, 11 May 2015 22:29:02 +0300 Subject: [PATCH 1/5] Add option to select SPI speed at SD::begin --- libraries/SD/src/SD.cpp | 4 ++-- libraries/SD/src/SD.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index 65d32741c..1d42e3738 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -332,7 +332,7 @@ boolean callback_rmdir(SdFile& parentDir, char *filePathComponent, -boolean SDClass::begin(uint8_t csPin) { +boolean SDClass::begin(uint8_t csPin, uint32_t speed) { /* Performs the initialisation required by the sdfatlib library. @@ -340,7 +340,7 @@ boolean SDClass::begin(uint8_t csPin) { Return true if initialization succeeds, false otherwise. */ - return card.init(SPI_HALF_SPEED, csPin) && + return card.init(speed, csPin) && volume.init(card) && root.openRoot(volume); } diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index 7435cf577..6ba08b21b 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -65,7 +65,7 @@ private: public: // This needs to be called to set up the connection to the SD card // before other methods are used. - boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN); + boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t speed = SPI_HALF_SPEED); // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Returns a File object for interacting with the file. From 4425e0921f69ef977c86f4cadf888c20384ac5f2 Mon Sep 17 00:00:00 2001 From: ficeto Date: Tue, 12 May 2015 10:36:12 +0300 Subject: [PATCH 2/5] removing timer aliases --- cores/esp8266/core_esp8266_timer.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/cores/esp8266/core_esp8266_timer.c b/cores/esp8266/core_esp8266_timer.c index 9edee3671..bfdd3692c 100644 --- a/cores/esp8266/core_esp8266_timer.c +++ b/cores/esp8266/core_esp8266_timer.c @@ -30,39 +30,32 @@ void timer1_isr_handler(void *para){ if(timer1_user_cb) timer1_user_cb(); } -extern void __timer1_isr_init(){ +void timer1_isr_init(){ ETS_FRC_TIMER1_INTR_ATTACH(timer1_isr_handler, NULL); } -extern void __timer1_attachInterrupt(void (*userFunc)(void)) { +void timer1_attachInterrupt(void (*userFunc)(void)) { timer1_user_cb = userFunc; ETS_FRC1_INTR_ENABLE(); } -extern void __timer1_detachInterrupt() { +void timer1_detachInterrupt() { timer1_user_cb = 0; TEIE &= ~TEIE1;//edge int disable ETS_FRC1_INTR_DISABLE(); } -extern void __timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload){ +void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload){ T1C = (1 << TCTE) | ((divider & 3) << TCPD) | ((int_type & 1) << TCIT) | ((reload & 1) << TCAR); T1I = 0; } -extern void __timer1_write(uint32_t ticks){ +void timer1_write(uint32_t ticks){ T1L = ((ticks) & 0x7FFFFF); if((T1C & (1 << TCIT)) == 0) TEIE |= TEIE1;//edge int enable } -extern void __timer1_disable(){ +void timer1_disable(){ T1C = 0; T1I = 0; } - -extern void timer1_isr_init(void) __attribute__ ((weak, alias("__timer1_isr_init"))); -extern void timer1_detachInterrupt(void) __attribute__ ((weak, alias("__timer1_detachInterrupt"))); -extern void timer1_disable(void) __attribute__ ((weak, alias("__timer1_disable"))); -extern void timer1_attachInterrupt(void (*userFunc)(void)) __attribute__ ((weak, alias("__timer1_attachInterrupt"))); -extern void timer1_write(uint32_t ticks) __attribute__ ((weak, alias("__timer1_write"))); -extern void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload) __attribute__ ((weak, alias("__timer1_enable"))); \ No newline at end of file From 88c6ee418d3a9129c53ea80f9de4e600939c1cd7 Mon Sep 17 00:00:00 2001 From: ficeto Date: Tue, 12 May 2015 13:58:05 +0300 Subject: [PATCH 3/5] add close and abort methods and enable disconnect callback --- .../ESP8266WiFi/src/include/ClientContext.h | 97 +++++++++---------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index f9fd4e524..bf1166ac3 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -39,7 +39,39 @@ class ClientContext { tcp_sent(pcb, &_s_sent); tcp_err(pcb, &_s_error); } - + + err_t abort(){ + if(_pcb) { + DEBUGV(":abort\r\n"); + tcp_arg(_pcb, NULL); + tcp_sent(_pcb, NULL); + tcp_recv(_pcb, NULL); + tcp_err(_pcb, NULL); + tcp_abort(_pcb); + _pcb = 0; + } + return ERR_ABRT; + } + + err_t close(){ + err_t err = ERR_OK; + if(_pcb) { + DEBUGV(":close\r\n"); + tcp_arg(_pcb, NULL); + tcp_sent(_pcb, NULL); + tcp_recv(_pcb, NULL); + tcp_err(_pcb, NULL); + err = tcp_close(_pcb); + if(err != ERR_OK) { + DEBUGV(":tc err %d\r\n", err); + tcp_abort(_pcb); + err = ERR_ABRT; + } + _pcb = 0; + } + return err; + } + ~ClientContext() { } @@ -58,22 +90,10 @@ class ClientContext { } void unref() { - err_t err; DEBUGV(":ur %d\r\n", _refcnt); if(--_refcnt == 0) { flush(); - if(_pcb) { - tcp_arg(_pcb, NULL); - tcp_sent(_pcb, NULL); - tcp_recv(_pcb, NULL); - tcp_err(_pcb, NULL); - err = tcp_close(_pcb); - if(err != ERR_OK) { - DEBUGV(":tc err %d\r\n", err); - tcp_abort(_pcb); - } - _pcb = 0; - } + close(); delete this; } } @@ -179,6 +199,13 @@ class ClientContext { private: + err_t _sent(tcp_pcb* pcb, uint16_t len) { + DEBUGV(":sent %d\r\n", len); + _size_sent -= len; + if(_size_sent == 0 && _send_waiting) esp_schedule(); + return ERR_OK; + } + void _consume(size_t size) { ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size; if(left > 0) { @@ -204,21 +231,9 @@ class ClientContext { if(pb == 0) // connection closed { - DEBUGV(":rcl\r\n"); - tcp_arg(pcb, NULL); - tcp_sent(pcb, NULL); - tcp_recv(pcb, NULL); - tcp_err(pcb, NULL); - // int error = tcp_close(pcb); - // if (error != ERR_OK) - { - DEBUGV(":rcla\r\n"); - tcp_abort(pcb); - _pcb = 0; - return ERR_ABRT; - } - _pcb = 0; - return ERR_OK; + if(_discard_cb) _discard_cb(_discard_cb_arg, this); + DEBUGV(":rcla\r\n"); + return abort(); } if(_rx_buf) { @@ -231,27 +246,12 @@ class ClientContext { _rx_buf = pb; _rx_buf_offset = 0; } - // tcp_recved(pcb, received); - // pbuf_free(pb); return ERR_OK; } void _error(err_t err) { DEBUGV(":er %d\r\n", err); - - if(_pcb) { - tcp_arg(_pcb, NULL); - tcp_sent(_pcb, NULL); - tcp_recv(_pcb, NULL); - tcp_err(_pcb, NULL); - err = tcp_close(_pcb); - if(err != ERR_OK) { - DEBUGV(":tc err %d\r\n", err); - tcp_abort(_pcb); - } - } - _pcb = 0; - + close(); if(_size_sent && _send_waiting) { esp_schedule(); } @@ -261,13 +261,6 @@ class ClientContext { return ERR_OK; } - err_t _sent(tcp_pcb* pcb, uint16_t len) { - DEBUGV(":sent %d\r\n", len); - _size_sent -= len; - if(_size_sent == 0 && _send_waiting) esp_schedule(); - return ERR_OK; - } - static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) { return reinterpret_cast(arg)->_recv(tpcb, pb, err); } From 1707d3d036f446fc246b9b3a843a5272309205db Mon Sep 17 00:00:00 2001 From: ficeto Date: Tue, 12 May 2015 14:22:16 +0300 Subject: [PATCH 4/5] move discard_cb to unref --- libraries/ESP8266WiFi/src/include/ClientContext.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index bf1166ac3..0001ef08b 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -94,6 +94,7 @@ class ClientContext { if(--_refcnt == 0) { flush(); close(); + if(_discard_cb) _discard_cb(_discard_cb_arg, this); delete this; } } @@ -231,7 +232,6 @@ class ClientContext { if(pb == 0) // connection closed { - if(_discard_cb) _discard_cb(_discard_cb_arg, this); DEBUGV(":rcla\r\n"); return abort(); } From bacc0b1f8bea02b26546df94d452546b91abc072 Mon Sep 17 00:00:00 2001 From: ficeto Date: Tue, 12 May 2015 15:21:35 +0300 Subject: [PATCH 5/5] add method to check for clients without referencing them --- libraries/ESP8266WiFi/src/WiFiServer.cpp | 5 +++++ libraries/ESP8266WiFi/src/WiFiServer.h | 1 + 2 files changed, 6 insertions(+) diff --git a/libraries/ESP8266WiFi/src/WiFiServer.cpp b/libraries/ESP8266WiFi/src/WiFiServer.cpp index ec61b424b..1ed2a64c1 100644 --- a/libraries/ESP8266WiFi/src/WiFiServer.cpp +++ b/libraries/ESP8266WiFi/src/WiFiServer.cpp @@ -75,6 +75,11 @@ void WiFiServer::begin() extern "C" uint32_t esp_micros_at_task_start(); +bool WiFiServer::hasClient(){ + if (_unclaimed) return true; + return false; +} + WiFiClient WiFiServer::available(byte* status) { static uint32_t lastPollTime = 0; diff --git a/libraries/ESP8266WiFi/src/WiFiServer.h b/libraries/ESP8266WiFi/src/WiFiServer.h index 0bd427e2f..7e7b0491b 100644 --- a/libraries/ESP8266WiFi/src/WiFiServer.h +++ b/libraries/ESP8266WiFi/src/WiFiServer.h @@ -44,6 +44,7 @@ private: public: WiFiServer(uint16_t port); WiFiClient available(uint8_t* status = NULL); + bool hasClient(); void begin(); virtual size_t write(uint8_t); virtual size_t write(const uint8_t *buf, size_t size);