mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-24 08:45:10 +03:00
* Move to PROGMEM aware libc, allow PSTR in printf() A Newlib (libc) patch is in progress to move the _P functions from inside Arduino into first-class citizens in libc. This Arduino patch cleans up code that's been migrated there. Binaries for the new libs are included because it seems they're part of the Arduino git tree, and should be replaced with @igrr built ones when/if the Newlib changes are accepted. Notable changes/additions for Arduino: Allow for use of PROGMEM based format and parameter strings in all *printf functions. No need for copying PSTR()s into RAM before printing them out (transparently saves heap space when using _P functions) and makes it easier to print out constant strings for applications. Add "%S" (capital-S) format that I've been told, but cannot verify, is used in Arduino to specify a PROGMEM string parameter in printfs, as an alias for "%s" since plain "%s" can now handle PROGMEM. Optimized the memcpy_P, strnlen_P, and strncpy_P functions to use 32-bit direct reads whenver possible (source and dest alignment mediated), but there is still room for improvement in others. Finally, move several constant arrays from RODATA into PROGMEM and update their accessors. Among these are the ctype array, ~260 bytes, mprec* arrays, ~300 bytes, and strings/daycounts in the time formatting functions, ~200 bytes. All told, sketches will see from 300 to 800 additional RAM heap free on startup (depending on their use of these routines). * Fix merge error in #ifdef/#endif * Fix host test using the newlib generic pgmspace.h Host tests now use the sys/pgmspace.h for compiles instead of the ESP8266-specific version. * Update with rebuilt libraries using latest newlib * Include binaries built directly from @igrr repo Rebuild the binaries using a git clone of https://github.com/igrr/newlib-xtensa Build commands for posterity: ```` rm -rf ./xtensa-lx106-elf/ ./configure --prefix=<DIR>/esp8266/tools/sdk/libc --with-newlib \ --enable-multilib --disable-newlib-io-c99-formats \ --disable-newlib-supplied-syscalls \ --enable-newlib-nano-formatted-io --enable-newlib-reent-small \ --enable-target-optspace \ --program-transform-name="s&^&xtensa-lx106-elf-&" \ --disable-option-checking --with-target-subdir=xtensa-lx106-elf \ --target=xtensa-lx106-elf rm -f etc/config.cache CROSS_CFLAGS="-fno-omit-frame-pointer -DSIGNAL_PROVIDED -DABORT_PROVIDED"\ " -DMALLOC_PROVIDED" \ PATH=<DIR>/esp8266/tools/xtensa-lx106-elf/bin/:$PATH \ make all install ```` * Fix merge define conflict in c_types.h * Fix strlen_P misaligned source error Include fix from newlib-xtensa/fix-strlen branch cleaning up misaligned access on a non-aligned source string. * Fix strlen_P and strcpy_P edge cases Ran the included test suite on ESP8266 tstring.c with the following defines: #define MAX_1 50 #define memcmp memcmp_P #define memcpy memcpy_P #define memmem memmem_P #define memchr memchr_P #define strcat strcat_P #define strncat strncat_P #define strcpy strcpy_P #define strlen strlen_P #define strnlen strnlen_P #define strcmp strcmp_P #define strncmp strncmp_P Uncovered edge case and return value problems in the optimized versions of the strnlen_P and strncpy_P functions. Corrected. * Fix memcpy_P return value memcpy-1.c test suite showed error in return value of memcpy_P. Correct it. * Fix strnlen_P/strlen_P off-by-4 error Random crashes, often on String constructors using a PSTR, would occur due to the accelerated strnlen_P going past the end of the string. Would make debug builds fail, too (ESP.getVersionString() failure). Fix to fall through to normal copy on a word that's got a 0 byte anywhere in it. * Add device tests for libc functional verification Add test suite used to debug libc optimized _P functions to the device tests. * Rebuild from igrr's repo (same source as prior) Rebuild .a from igrr's repo at 347260af117b4177389e69fd4d04169b11d87a97 * WIP - add exceptions * Fix exception to have 0-terminator * Move some exception constants to TEXT from RODATA * Remove throw stubs * Move more exception stuff to ROM * Enable exceptions in platform.io * Remove atexit, is duplicated in rebuilt lib Need to look at the quick-toolchain options, there seems to be a definition for atexit defined there (libgcc?) that needs to be excised. For now, remove our local do-nothing copy. * Update libgcc to remove soft-fp functions The esp-quick-toolchain generated libgcc.a needed to have the soft-FP routines that are in ROM removed from it. Remove them in the new esp-quick-toolchain and update. * Fix merge typos in Makefile * Add unhandled exception handler to postmortem * Return our atexit() handler * Latest stdc++, minimize exception emercengy area * Remove atexit from newlib atexit was defined in newlib strongly, but we also define a noop atexit in core. Since we never exit, use the core's noop and delete the atexit from libc.a Updated in esp-quick-toolchain as well. * Move __FUNCTION__ static strings to PROGMEM __FUNCTION__ is unlikely to be a timing sensitive variable, so move it to PROGMEM and not RODATA (RAM) using linker magic. asserts() now should take no RAM for any strings. * Clean up linker file, update to latest stdc++ * Update to latest stdc++ which doesn't call strerror * Update to GCC5.1 exception emergency allocator Using GCC 5.1's emergency memory allocator for exceptions, much less space is required in programs which do not use exceptions and when space is allocated it is managed more efficiently. * Initial try with new compiler toolchain * Include newlib built from esp-quick-toolchain * Update JSON with all new esp-quick-toolchain builds * Use 64bit Windows compiler on 64bit Windows * Dump std::exception.what() when possible When doing the panic on unhandled exceptions, try and grab the .what() pointer and dump it as part of the termination info. Makes it easy to see mem errors (std::bad_alloc) or std::runtime_error strings. * Use scripted install from esp-quick-toolchain Makes sure proper libraries and includes are present by using a scripted installation from esp-quick-install instead of a manual one. * Update eqk to remove atexit, fix packaging diff
285 lines
8.9 KiB
C++
285 lines
8.9 KiB
C++
/*
|
|
Arduino.h - Main include file for the Arduino SDK
|
|
Copyright (c) 2005-2013 Arduino Team. All right reserved.
|
|
|
|
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 Arduino_h
|
|
#define Arduino_h
|
|
|
|
#define MOCK "mock: "
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
#include "binary.h"
|
|
#include "twi.h"
|
|
#include "core_esp8266_features.h"
|
|
|
|
#define HIGH 0x1
|
|
#define LOW 0x0
|
|
|
|
#define PWMRANGE 1023
|
|
|
|
//GPIO FUNCTIONS
|
|
#define INPUT 0x00
|
|
#define INPUT_PULLUP 0x02
|
|
#define INPUT_PULLDOWN_16 0x04 // PULLDOWN only possible for pin16
|
|
#define OUTPUT 0x01
|
|
#define OUTPUT_OPEN_DRAIN 0x03
|
|
#define WAKEUP_PULLUP 0x05
|
|
#define WAKEUP_PULLDOWN 0x07
|
|
#define SPECIAL 0xF8 //defaults to the usable BUSes uart0rx/tx uart1tx and hspi
|
|
#define FUNCTION_0 0x08
|
|
#define FUNCTION_1 0x18
|
|
#define FUNCTION_2 0x28
|
|
#define FUNCTION_3 0x38
|
|
#define FUNCTION_4 0x48
|
|
|
|
#define PI 3.1415926535897932384626433832795
|
|
#define HALF_PI 1.5707963267948966192313216916398
|
|
#define TWO_PI 6.283185307179586476925286766559
|
|
#define DEG_TO_RAD 0.017453292519943295769236907684886
|
|
#define RAD_TO_DEG 57.295779513082320876798154814105
|
|
#define EULER 2.718281828459045235360287471352
|
|
|
|
#define SERIAL 0x0
|
|
#define DISPLAY 0x1
|
|
|
|
#define LSBFIRST 0
|
|
#define MSBFIRST 1
|
|
|
|
//Interrupt Modes
|
|
#define DISABLED 0x00
|
|
#define RISING 0x01
|
|
#define FALLING 0x02
|
|
#define CHANGE 0x03
|
|
#define ONLOW 0x04
|
|
#define ONHIGH 0x05
|
|
#define ONLOW_WE 0x0C
|
|
#define ONHIGH_WE 0x0D
|
|
|
|
#define DEFAULT 1
|
|
#define EXTERNAL 0
|
|
|
|
//timer dividers
|
|
#define TIM_DIV1 0 //80MHz (80 ticks/us - 104857.588 us max)
|
|
#define TIM_DIV16 1 //5MHz (5 ticks/us - 1677721.4 us max)
|
|
#define TIM_DIV265 3 //312.5Khz (1 tick = 3.2us - 26843542.4 us max)
|
|
//timer int_types
|
|
#define TIM_EDGE 0
|
|
#define TIM_LEVEL 1
|
|
//timer reload values
|
|
#define TIM_SINGLE 0 //on interrupt routine you need to write a new value to start the timer again
|
|
#define TIM_LOOP 1 //on interrupt the counter will start with the same value again
|
|
|
|
#define timer1_read() (T1V)
|
|
#define timer1_enabled() ((T1C & (1 << TCTE)) != 0)
|
|
#define timer1_interrupted() ((T1C & (1 << TCIS)) != 0)
|
|
|
|
typedef void(*timercallback)(void);
|
|
|
|
void timer1_isr_init(void);
|
|
void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload);
|
|
void timer1_disable(void);
|
|
void timer1_attachInterrupt(timercallback userFunc);
|
|
void timer1_detachInterrupt(void);
|
|
void timer1_write(uint32_t ticks); //maximum ticks 8388607
|
|
|
|
// timer0 is a special CPU timer that has very high resolution but with
|
|
// limited control.
|
|
// it uses CCOUNT (ESP.GetCycleCount()) as the non-resetable timer counter
|
|
// it does not support divide, type, or reload flags
|
|
// it is auto-disabled when the compare value matches CCOUNT
|
|
// it is auto-enabled when the compare value changes
|
|
#define timer0_interrupted() (ETS_INTR_PENDING() & (_BV(ETS_COMPARE0_INUM)))
|
|
#define timer0_read() ((__extension__({uint32_t count;__asm__ __volatile__("esync; rsr %0,ccompare0":"=a" (count));count;})))
|
|
#define timer0_write(count) __asm__ __volatile__("wsr %0,ccompare0; esync"::"a" (count) : "memory")
|
|
|
|
void timer0_isr_init(void);
|
|
void timer0_attachInterrupt(timercallback userFunc);
|
|
void timer0_detachInterrupt(void);
|
|
|
|
// undefine stdlib's abs if encountered
|
|
#ifdef abs
|
|
#undef abs
|
|
#endif
|
|
|
|
#define abs(x) ((x)>0?(x):-(x))
|
|
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
|
|
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
|
|
#define radians(deg) ((deg)*DEG_TO_RAD)
|
|
#define degrees(rad) ((rad)*RAD_TO_DEG)
|
|
#define sq(x) ((x)*(x))
|
|
|
|
void ets_intr_lock();
|
|
void ets_intr_unlock();
|
|
|
|
#ifndef __STRINGIFY
|
|
#define __STRINGIFY(a) #a
|
|
#endif
|
|
|
|
// these low level routines provide a replacement for SREG interrupt save that AVR uses
|
|
// but are esp8266 specific. A normal use pattern is like
|
|
//
|
|
//{
|
|
// uint32_t savedPS = xt_rsil(1); // this routine will allow level 2 and above
|
|
// // do work here
|
|
// xt_wsr_ps(savedPS); // restore the state
|
|
//}
|
|
//
|
|
// level (0-15), interrupts of the given level and above will be active
|
|
// level 15 will disable ALL interrupts,
|
|
// level 0 will enable ALL interrupts,
|
|
//
|
|
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state)); state;}))
|
|
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
|
|
|
|
#define interrupts() xt_rsil(0)
|
|
#define noInterrupts() xt_rsil(15)
|
|
|
|
|
|
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
|
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
|
|
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
|
|
|
|
#define lowByte(w) ((uint8_t) ((w) & 0xff))
|
|
#define highByte(w) ((uint8_t) ((w) >> 8))
|
|
|
|
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
|
|
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
|
|
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
|
|
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
|
|
|
|
// avr-libc defines _NOP() since 1.6.2
|
|
#ifndef _NOP
|
|
#define _NOP() do { __asm__ volatile ("nop"); } while (0)
|
|
#endif
|
|
|
|
typedef unsigned int word;
|
|
|
|
#define bit(b) (1UL << (b))
|
|
#define _BV(b) (1UL << (b))
|
|
|
|
typedef uint8_t boolean;
|
|
typedef uint8_t byte;
|
|
|
|
void init(void);
|
|
void initVariant(void);
|
|
|
|
int atexit(void (*func)()) __attribute__((weak));
|
|
|
|
void pinMode(uint8_t pin, uint8_t mode);
|
|
void digitalWrite(uint8_t pin, uint8_t val);
|
|
int digitalRead(uint8_t pin);
|
|
int analogRead(uint8_t pin);
|
|
void analogReference(uint8_t mode);
|
|
void analogWrite(uint8_t pin, int val);
|
|
void analogWriteFreq(uint32_t freq);
|
|
void analogWriteRange(uint32_t range);
|
|
|
|
unsigned long millis(void);
|
|
unsigned long micros(void);
|
|
void delay(unsigned long);
|
|
void delayMicroseconds(unsigned int us);
|
|
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
|
|
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
|
|
|
|
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
|
|
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
|
|
|
|
void attachInterrupt(uint8_t pin, void (*)(void), int mode);
|
|
void detachInterrupt(uint8_t pin);
|
|
|
|
void setup(void);
|
|
void loop(void);
|
|
|
|
void yield(void);
|
|
void optimistic_yield(uint32_t interval_us);
|
|
|
|
#define digitalPinToPort(pin) (0)
|
|
#define digitalPinToBitMask(pin) (1UL << (pin))
|
|
#define digitalPinToTimer(pin) (0)
|
|
#define portOutputRegister(port) ((volatile uint32_t*) &GPO)
|
|
#define portInputRegister(port) ((volatile uint32_t*) &GPI)
|
|
#define portModeRegister(port) ((volatile uint32_t*) &GPE)
|
|
|
|
#define NOT_A_PIN -1
|
|
#define NOT_A_PORT -1
|
|
#define NOT_AN_INTERRUPT -1
|
|
#define NOT_ON_TIMER 0
|
|
|
|
#ifdef __cplusplus
|
|
} // extern "C"
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#include <pgmspace.h>
|
|
|
|
#include "WCharacter.h"
|
|
#include "WString.h"
|
|
|
|
#include "HardwareSerial.h"
|
|
#include "Esp.h"
|
|
#include "Updater.h"
|
|
#include "debug.h"
|
|
|
|
#ifndef _GLIBCXX_VECTOR
|
|
// arduino is not compatible with std::vector
|
|
#define min(a,b) ((a)<(b)?(a):(b))
|
|
#define max(a,b) ((a)>(b)?(a):(b))
|
|
#endif
|
|
|
|
#define _min(a,b) ((a)<(b)?(a):(b))
|
|
#define _max(a,b) ((a)>(b)?(a):(b))
|
|
|
|
uint16_t makeWord(uint16_t w);
|
|
uint16_t makeWord(byte h, byte l);
|
|
|
|
#define word(...) makeWord(__VA_ARGS__)
|
|
|
|
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
|
|
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
|
|
|
|
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
|
|
void noTone(uint8_t _pin);
|
|
|
|
// WMath prototypes
|
|
long random(long);
|
|
long random(long, long);
|
|
void randomSeed(unsigned long);
|
|
long map(long, long, long, long, long);
|
|
|
|
extern "C" void configTime(long timezone, int daylightOffset_sec,
|
|
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
|
|
|
|
#endif
|
|
|
|
#include "pins_arduino.h"
|
|
|
|
#endif /* Arduino_h */
|