From e372850a35a2030d8a675a9c2855b9374c627569 Mon Sep 17 00:00:00 2001 From: Roman Mashta Date: Fri, 10 Apr 2015 22:35:44 +0300 Subject: [PATCH 1/4] Fix slashes in Adafruit_ILI9341 library --- libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp | 2 +- libraries/Adafruit_ILI9341/Adafruit_ILI9341.h | 2 +- libraries/Adafruit_ILI9341/hspi.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp b/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp index dc649f55e..eb8699718 100644 --- a/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp +++ b/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp @@ -5,7 +5,7 @@ extern "C"{ #include #include #include -#include "driver\hspi.h" +#include "driver/hspi.h" } #define SWAPBYTES(i) ((i>>8) | (i<<8)) diff --git a/libraries/Adafruit_ILI9341/Adafruit_ILI9341.h b/libraries/Adafruit_ILI9341/Adafruit_ILI9341.h index 17ed41197..fdf5a50ec 100644 --- a/libraries/Adafruit_ILI9341/Adafruit_ILI9341.h +++ b/libraries/Adafruit_ILI9341/Adafruit_ILI9341.h @@ -8,7 +8,7 @@ extern "C" #include #include #include -#include "driver\hspi.h" +#include "driver/hspi.h" } #define ILI9341_TFTWIDTH 240 diff --git a/libraries/Adafruit_ILI9341/hspi.c b/libraries/Adafruit_ILI9341/hspi.c index e12310cb6..506c62bb8 100644 --- a/libraries/Adafruit_ILI9341/hspi.c +++ b/libraries/Adafruit_ILI9341/hspi.c @@ -1,4 +1,4 @@ -#include "driver\hspi.h" +#include "driver/hspi.h" /* Pinout: From 5c94d333c5c398ac43427c3cd36d9b07cff470bd Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sat, 11 Apr 2015 10:58:57 +0800 Subject: [PATCH 2/4] Add watchdog and deep sleep APIs requested in #34 --- cores/esp8266/Arduino.h | 1 + cores/esp8266/Esp.cpp | 57 +++++++++++++++++++++++++++++++++++++++++ cores/esp8266/Esp.h | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 cores/esp8266/Esp.cpp create mode 100644 cores/esp8266/Esp.h diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 716cced9d..f507bef3b 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -158,6 +158,7 @@ volatile uint32_t* portModeRegister(uint32_t port); #include "WString.h" #include "HardwareSerial.h" +#include "Esp.h" uint16_t makeWord(uint16_t w); uint16_t makeWord(byte h, byte l); diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp new file mode 100644 index 000000000..8e7caf6e6 --- /dev/null +++ b/cores/esp8266/Esp.cpp @@ -0,0 +1,57 @@ +/* + Esp.cpp - ESP8266-specific APIs + Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "Arduino.h" + +extern "C" { +#include "user_interface.h" +} + +extern "C" void ets_wdt_enable (void); +extern "C" void ets_wdt_disable (void); +extern "C" void wdt_feed (void); + +EspClass ESP; + +EspClass::EspClass() +{ + +} + +void EspClass::wdtEnable(int) +{ + ets_wdt_enable(); +} + +void EspClass::wdtDisable() +{ + ets_wdt_disable(); +} + +void EspClass::wdtFeed() +{ + wdt_feed(); +} + +void EspClass::deepSleep(uint32_t time_us, WakeMode mode) +{ + system_deep_sleep_set_option(static_cast(mode)); + system_deep_sleep(time_us); +} diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h new file mode 100644 index 000000000..7f10ec1d6 --- /dev/null +++ b/cores/esp8266/Esp.h @@ -0,0 +1,46 @@ +/* + Esp.h - ESP8266-specific APIs + Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef ESP_H +#define ESP_H + + +enum WakeMode { + WAKE_RF_DEFAULT = 0, // RF_CAL or not after deep-sleep wake up, depends on init data byte 108. + WAKE_RFCAL = 1, // RF_CAL after deep-sleep wake up, there will be large current. + WAKE_NO_RFCAL = 2, // no RF_CAL after deep-sleep wake up, there will only be small current. + WAKE_RF_DISABLED = 4 // disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current. +}; + +class EspClass { + public: + EspClass(); + + void wdtEnable(int timeout_ms = 0); + // TODO: figure out how to set WDT timeout + void wdtDisable(); + void wdtFeed(); + + void deepSleep(uint32_t time_us, WakeMode mode = WAKE_RF_DEFAULT); +}; + +extern EspClass ESP; + +#endif //ESP_H From 1866ef0c9829083d2ac73b5bd96edc7b4f1262be Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sat, 11 Apr 2015 10:59:36 +0800 Subject: [PATCH 3/4] Move some functions to .irom0.text --- cores/esp8266/i2c.cpp | 14 ++++----- cores/esp8266/libc_replacements.c | 50 +++++++++++++++---------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/cores/esp8266/i2c.cpp b/cores/esp8266/i2c.cpp index 8f14a350a..e5510add4 100644 --- a/cores/esp8266/i2c.cpp +++ b/cores/esp8266/i2c.cpp @@ -66,13 +66,13 @@ static inline void i2c_wait() { delayMicroseconds(s_i2c_delay); } -void i2c_freq(int freq_hz) { +void ICACHE_FLASH_ATTR i2c_freq(int freq_hz) { s_i2c_delay = 1000000 / freq_hz / 4 - 1; if(s_i2c_delay < 0) s_i2c_delay = 0; } -void i2c_init(int sda_pin, int scl_pin) { +void ICACHE_FLASH_ATTR i2c_init(int sda_pin, int scl_pin) { s_sda_pin = sda_pin; s_scl_pin = scl_pin; pinMode(ESP_PINS_OFFSET + sda_pin, OUTPUT_OPEN_DRAIN); @@ -81,7 +81,7 @@ void i2c_init(int sda_pin, int scl_pin) { i2c_wait(); } -void i2c_release() { +void ICACHE_FLASH_ATTR i2c_release() { pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT); pinMode(ESP_PINS_OFFSET + s_scl_pin, INPUT); } @@ -151,7 +151,7 @@ void i2c_write(uint8_t val) { i2c_set_sda(1); } -size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendStop) { +size_t ICACHE_FLASH_ATTR i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendStop) { i2c_start(); i2c_write(address << 1 | 1); int ack = i2c_get_ack(); @@ -171,7 +171,7 @@ size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendSt return size; } -size_t i2c_master_write_to(int address, const uint8_t* data, size_t size, bool sendStop) { +size_t ICACHE_FLASH_ATTR i2c_master_write_to(int address, const uint8_t* data, size_t size, bool sendStop) { i2c_start(); i2c_write(address << 1); int ack = i2c_get_ack(); @@ -193,11 +193,11 @@ void twi_init(void) { void twi_setAddress(uint8_t) { } -uint8_t twi_readFrom(uint8_t addr, uint8_t* data, uint8_t size, uint8_t sendStop) { +uint8_t ICACHE_FLASH_ATTR twi_readFrom(uint8_t addr, uint8_t* data, uint8_t size, uint8_t sendStop) { return i2c_master_read_from(addr, data, size, sendStop); } -uint8_t twi_writeTo(uint8_t addr, uint8_t* data, uint8_t size, uint8_t wait, uint8_t sendStop) { +uint8_t ICACHE_FLASH_ATTR twi_writeTo(uint8_t addr, uint8_t* data, uint8_t size, uint8_t wait, uint8_t sendStop) { return i2c_master_write_to(addr, data, size, sendStop); } diff --git a/cores/esp8266/libc_replacements.c b/cores/esp8266/libc_replacements.c index a8dfa5e8d..baeb63fee 100644 --- a/cores/esp8266/libc_replacements.c +++ b/cores/esp8266/libc_replacements.c @@ -111,15 +111,15 @@ int strncmp(const char *s1, const char *s2, size_t len) { return ets_strncmp(s1, s2, len); } -char *strncpy(char * dest, const char * src, size_t n) { +char* strncpy(char * dest, const char * src, size_t n) { return ets_strncpy(dest, src, n); } -char *ets_strstr(const char *haystack, const char *needle) { - return strstr(haystack, needle); +char* strstr(const char *haystack, const char *needle) { + return ets_strstr(haystack, needle); } -char * strchr(const char * str, int character) { +char* strchr(const char * str, int character) { while(1) { if(*str == 0x00) { return NULL; @@ -144,11 +144,11 @@ char * strrchr(const char * str, int character) { } } -char * strcat(char * dest, const char * src) { +char* ICACHE_FLASH_ATTR strcat(char * dest, const char * src) { return strncat(dest, src, strlen(src)); } -char * strncat(char * dest, const char * src, size_t n) { +char* ICACHE_FLASH_ATTR strncat(char * dest, const char * src, size_t n) { uint32_t offset = strlen(dest); for(uint32_t i = 0; i < n; i++) { *(dest + i + offset) = *(src + i); @@ -159,7 +159,7 @@ char * strncat(char * dest, const char * src, size_t n) { return dest; } -char * strtok_r(char * str, const char * delimiters, char ** temp) { +char* ICACHE_FLASH_ATTR strtok_r(char * str, const char * delimiters, char ** temp) { static char * ret = NULL; char * start = NULL; char * end = NULL; @@ -213,7 +213,7 @@ int strcasecmp(const char * str1, const char * str2) { return d; } -char * strdup(const char *str) { +char* ICACHE_FLASH_ATTR strdup(const char *str) { size_t len = strlen(str) + 1; char *cstr = malloc(len); if(cstr) { @@ -441,7 +441,7 @@ int isblank(int c) { static int errno_var = 0; -int * __errno(void) { +int* ICACHE_FLASH_ATTR __errno(void) { os_printf("__errno is called last error: %d (not current)\n", errno_var); return &errno_var; } @@ -450,67 +450,67 @@ int * __errno(void) { // __ieee754 functions // ########################################################################## -double __ieee754_sinh(double x) { +double ICACHE_FLASH_ATTR __ieee754_sinh(double x) { return sinh(x); } -double __ieee754_hypot(double x, double y) { +double ICACHE_FLASH_ATTR __ieee754_hypot(double x, double y) { return hypot(x, y); } -float __ieee754_hypotf(float x, float y) { +float ICACHE_FLASH_ATTR __ieee754_hypotf(float x, float y) { return hypotf(x, y); } -float __ieee754_logf(float x) { +float ICACHE_FLASH_ATTR __ieee754_logf(float x) { return logf(x); } -double __ieee754_log10(double x) { +double ICACHE_FLASH_ATTR __ieee754_log10(double x) { return log10(x); } -double __ieee754_exp(double x) { +double ICACHE_FLASH_ATTR __ieee754_exp(double x) { return exp(x); } -double __ieee754_cosh(double x) { +double ICACHE_FLASH_ATTR __ieee754_cosh(double x) { return cosh(x); } -float __ieee754_expf(float x) { +float ICACHE_FLASH_ATTR __ieee754_expf(float x) { return expf(x); } -float __ieee754_log10f(float x) { +float ICACHE_FLASH_ATTR __ieee754_log10f(float x) { return log10f(x); } -double __ieee754_atan2(double x, double y) { +double ICACHE_FLASH_ATTR __ieee754_atan2(double x, double y) { return atan2(x, y); } -float __ieee754_sqrtf(float x) { +float ICACHE_FLASH_ATTR __ieee754_sqrtf(float x) { return sqrtf(x); } -float __ieee754_sinhf(float x) { +float ICACHE_FLASH_ATTR __ieee754_sinhf(float x) { return sinhf(x); } -double __ieee754_log(double x) { +double ICACHE_FLASH_ATTR __ieee754_log(double x) { return log(x); } -double __ieee754_sqrt(double x) { +double ICACHE_FLASH_ATTR __ieee754_sqrt(double x) { return sqrt(x); } -float __ieee754_coshf(float x) { +float ICACHE_FLASH_ATTR __ieee754_coshf(float x) { return coshf(x); } -float __ieee754_atan2f(float x, float y) { +float ICACHE_FLASH_ATTR __ieee754_atan2f(float x, float y) { return atan2f(x, y); } From 89fe3daa852ce77179d85f8c2ed3937d6a5b8b4c Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sat, 11 Apr 2015 11:07:01 +0800 Subject: [PATCH 4/4] Update readme [ci skip] --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 953971c0d..8c34a27d2 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,14 @@ Before using I2C, pins for SDA and SCL need to be set by calling An initial SPI support for the HSPI interface (GPIO12-15) was implemented by [Sermus](https://github.com/Sermus). The implementation supports the entire Arduino SPI API including transactions, except setting phase and polarity as it's unclear how to set them in ESP8266 yet. +#### ESP-specific APIs #### + +APIs related to deep sleep and watchdog timer are available in the ```ESP``` object. + +```ESP.deepSleep(microseconds, mode)``` will put the chip into deep sleep. ```mode``` is one of ```WAKE_DEFAULT```, ```WAKE_RFCAL```, ```WAKE_NO_RFCAL```, ```WAKE_RF_DISABLED```. + +```ESP.wdtEnable()```, ```ESP.wdtDisable()```, and ```ESP.wdtFeed()``` provide some control over the watchdog timer. + #### OneWire (from https://www.pjrc.com/teensy/td_libs_OneWire.html) #### Library was adapted to work with ESP8266 by including register definitions into OneWire.h