Skip to content

Commit

Permalink
Merge pull request #1 from esp8266/esp8266
Browse files Browse the repository at this point in the history
pull master
  • Loading branch information
me-no-dev committed Jul 16, 2015
2 parents a6d8253 + 01361fc commit 1031e26
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 64 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,12 @@ Libraries that don't rely on low-level access to AVR registers should work well.
- [Blynk](https://github.com/blynkkk/blynk-library) - easy IoT framework for Makers (check out the [Kickstarter page](http://tiny.cc/blynk-kick)).
- [DallasTemperature](https://github.com/milesburton/Arduino-Temperature-Control-Library.git)
- [DHT11](https://github.com/adafruit/DHT-sensor-library) - Download latest v1.1.0 library and no changes are necessary. Older versions should initialize DHT as follows: ```DHT dht(DHTPIN, DHTTYPE, 15);```
- [NeoPixelBus](https://github.com/Makuna/NeoPixelBus) - Arduino NeoPixel library compatible with esp8266.
- [NeoPixel](https://github.com/adafruit/Adafruit_NeoPixel) - Adafruit's NeoPixel library, now with support for the ESP8266 (use version 1.0.2 or higher from Arduino's library manager).
- [NeoPixelBus](https://github.com/Makuna/NeoPixelBus) - Arduino NeoPixel library compatible with esp8266. Use the "NeoPixelAnimator" branch for esp8266 to get HSL color support and more.
- [PubSubClient](https://github.com/Imroy/pubsubclient) MQTT library by @Imroy.
- [RTC](https://github.com/Makuna/Rtc) - Arduino Library for Ds1307 & Ds3231 compatible with esp8266.
- [Souliss, Smart Home](https://github.com/souliss/souliss) - Framework for Smart Home based on Arduino, Android and openHAB.
- [ST7735](https://github.com/nzmichaelh/Adafruit-ST7735-Library) - Adafruit's ST7735 library modified to be compatible with esp8266. Just make sure to modify the pins in the examples as they are still AVR specific.

#### Upload via serial port ####
Pick the correct serial port.
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern "C" {
#include "twi.h"

void yield(void);
void optimistic_yield(void);

#define HIGH 0x1
#define LOW 0x0
Expand Down
16 changes: 10 additions & 6 deletions cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,17 @@ bool HardwareSerial::isRxEnabled(void) {
}

int HardwareSerial::available(void) {
if(_uart == 0)
return 0;
if(_uart->rxEnabled) {
return static_cast<int>(_rx_buffer->getSize());
} else {
return 0;
int result = 0;

if (_uart != NULL && _uart->rxEnabled) {
result = static_cast<int>(_rx_buffer->getSize());
}

if (!result) {
optimistic_yield();
}

return result;
}

int HardwareSerial::peek(void) {
Expand Down
18 changes: 13 additions & 5 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extern "C" {
#define LOOP_TASK_PRIORITY 0
#define LOOP_QUEUE_SIZE 1

#define OPTIMISTIC_YIELD_TIME_US 16000

struct rst_info resetInfo;

int atexit(void (*func)()) {
Expand Down Expand Up @@ -62,18 +64,16 @@ extern void (*__init_array_end)(void);
cont_t g_cont __attribute__ ((aligned (16)));
static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];

static uint32_t g_micros_at_task_start;
static uint32_t g_micros_at_last_task_yield;

extern "C" uint32_t esp_micros_at_task_start() {
return g_micros_at_task_start;
}

extern "C" void abort() {
while(1) {
}
}

extern "C" void esp_yield() {
g_micros_at_last_task_yield = system_get_time();
cont_yield(&g_cont);
}

Expand All @@ -87,6 +87,14 @@ extern "C" void __yield() {
}
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));

extern "C" void optimistic_yield(void) {
if (!ETS_INTR_WITHINISR() &&
(system_get_time() - g_micros_at_last_task_yield) > OPTIMISTIC_YIELD_TIME_US)
{
__yield();
}
}

static void loop_wrapper() {
static bool setup_done = false;
if(!setup_done) {
Expand All @@ -99,7 +107,7 @@ static void loop_wrapper() {
}

static void loop_task(os_event_t *events) {
g_micros_at_task_start = system_get_time();
g_micros_at_last_task_yield = system_get_time();
cont_run(&g_cont, &loop_wrapper);
if(cont_check(&g_cont) != 0) {
ets_printf("\r\nheap collided with sketch stack\r\n");
Expand Down
39 changes: 24 additions & 15 deletions cores/esp8266/core_esp8266_noniso.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,49 +149,58 @@ char* ultoa(unsigned long value, char* result, int base) {

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {

if(isnan(number)) {
if (isnan(number)) {
strcpy(s, "nan");
return s;
}
if(isinf(number)) {
if (isinf(number)) {
strcpy(s, "inf");
return s;
}

if(number > 4294967040.0 || number < -4294967040.0) {
if (number > 4294967040.0 || number < -4294967040.0) {
strcpy(s, "ovf");
return s;
}

char* out = s;
int signInt_Part = 1;

// Handle negative numbers
if(number < 0.0) {
*out = '-';
++out;
if (number < 0.0) {
signInt_Part = -1;
number = -number;
}

// calc left over digits
if (prec > 0)
{
width -= (prec + 1);
}

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for(uint8_t i = 0; i < prec; ++i)
for (uint8_t i = 0; i < prec; ++i)
rounding /= 10.0;

number += rounding;

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long) number;
double remainder = number - (double) int_part;
out += sprintf(out, "%ld", int_part);
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
out += sprintf(out, "%*ld", width, int_part * signInt_Part);

// Print the decimal point, but only if there are digits beyond
if(prec > 0) {
if (prec > 0) {
*out = '.';
++out;
}

for (unsigned char decShift = prec; decShift > 0; decShift--) {
remainder *= 10.0;

for (unsigned char decShift = prec; decShift > 0; decShift--) {
remainder *= 10.0;
}
sprintf(out, "%0*d", prec, (int)remainder);
}
sprintf(out, "%0*d", prec, (int)remainder);

return s;
}
Expand Down
19 changes: 10 additions & 9 deletions cores/esp8266/core_esp8266_si2c.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
/*
si2c.c - Software I2C library for esp8266
Copyright (c) 2015 Hristo Gochkov. 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
Expand All @@ -26,20 +26,20 @@ unsigned char twi_dcount = 18;
static unsigned char twi_sda, twi_scl;

#define SDA_LOW() (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low)
#define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high)
#define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high)
#define SDA_READ() ((GPI & (1 << twi_sda)) != 0)
#define SCL_LOW() (GPES = (1 << twi_scl))
#define SCL_HIGH() (GPEC = (1 << twi_scl))
#define SCL_LOW() (GPES = (1 << twi_scl))
#define SCL_HIGH() (GPEC = (1 << twi_scl))
#define SCL_READ() ((GPI & (1 << twi_scl)) != 0)

#ifndef FCPU80
#define FCPU80 80000000L
#endif

#if F_CPU == FCPU80
#define TWI_CLOCK_STRETCH 200
#define TWI_CLOCK_STRETCH 800
#else
#define TWI_CLOCK_STRETCH 400
#define TWI_CLOCK_STRETCH 1600
#endif

void twi_setClock(unsigned int freq){
Expand Down Expand Up @@ -99,7 +99,7 @@ static bool twi_write_stop(void){
twi_delay(twi_dcount);
SDA_HIGH();
twi_delay(twi_dcount);

return true;
}

Expand Down Expand Up @@ -166,7 +166,8 @@ unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned i
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address
for(i=0; i<len; i++) buf[i] = twi_read_byte(false);
for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false);
buf[len-1] = twi_read_byte(true);
if(sendStop) twi_write_stop();
i = 0;
while(SDA_READ() == 0 && (i++) < 10){
Expand Down
1 change: 0 additions & 1 deletion cores/esp8266/core_esp8266_wiring_digital.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ extern void __pinMode(uint8_t pin, uint8_t mode) {
}

extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
val &= 0x01;
if(pin < 16){
if(val) GPOS = (1 << pin);
else GPOC = (1 << pin);
Expand Down
11 changes: 11 additions & 0 deletions cores/esp8266/pgmspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ int printf_P(const char* formatP, ...) {
return ret;
}

int sprintf_P(char* str, const char* formatP, ...) {
int ret;
va_list arglist;
va_start(arglist, formatP);

ret = vsnprintf_P(str, SIZE_IRRELEVANT, formatP, arglist);

va_end(arglist);
return ret;
}

int snprintf_P(char* str, size_t strSize, const char* formatP, ...) {
int ret;
va_list arglist;
Expand Down
3 changes: 2 additions & 1 deletion cores/esp8266/pgmspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ size_t strnlen_P(const char *s, size_t size);
#define strlen_P(strP) strnlen_P((strP), SIZE_IRRELEVANT)

int printf_P(const char *formatP, ...) __attribute__ ((format (printf, 1, 2)));
int snprintf_P(char *str, size_t strSize, const char *formatP, ...) __attribute__ ((format (printf, 3, 4)));
int sprintf_P(char *str, const char *formatP, ...) __attribute__((format(printf, 2, 3)));
int snprintf_P(char *str, size_t strSize, const char *formatP, ...) __attribute__((format(printf, 3, 4)));
int vsnprintf_P(char *str, size_t strSize, const char *formatP, va_list ap) __attribute__ ((format (printf, 3, 0)));

// flash memory must be read using 32 bit aligned addresses else a processor
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
const char* ssid = "**********";
const char* password = "**********";

WiFiServer server(21);
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];

void setup() {
Expand All @@ -45,7 +45,7 @@ void setup() {

Serial1.print("Ready! Use 'telnet ");
Serial1.print(WiFi.localIP());
Serial1.println(" 21' to connect");
Serial1.println(" 23' to connect");
}

void loop() {
Expand Down
17 changes: 7 additions & 10 deletions libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,17 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
return _client->write(reinterpret_cast<const char*>(buf), size);
}

extern "C" uint32_t esp_micros_at_task_start();

int WiFiClient::available()
{
static uint32_t lastPollTime = 0;
if (!_client)
return 0;
int result = 0;

if (lastPollTime > esp_micros_at_task_start())
yield();

lastPollTime = micros();
if (_client) {
result = _client->getSize();
}

int result = _client->getSize();
if (!result) {
optimistic_yield();
}
return result;
}

Expand Down
8 changes: 1 addition & 7 deletions libraries/ESP8266WiFi/src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,13 @@ bool WiFiServer::getNoDelay(){
return tcp_nagle_disabled(_pcb);
}

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;

if (_unclaimed)
{
WiFiClient result(_unclaimed);
Expand All @@ -103,9 +99,7 @@ WiFiClient WiFiServer::available(byte* status)
return result;
}

if (lastPollTime > esp_micros_at_task_start())
yield();
lastPollTime = micros();
optimistic_yield();

return WiFiClient();
}
Expand Down
14 changes: 11 additions & 3 deletions libraries/ESP8266WiFi/src/WiFiUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,17 @@ uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, ui
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiUDP::available() {
if (!_ctx)
return 0;
return static_cast<int>(_ctx->getSize());
int result = 0;

if (_ctx) {
result = static_cast<int>(_ctx->getSize());
}

if (!result) {
optimistic_yield();
}

return result;
}

/* Release any resources being used by this WiFiUDP instance */
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP8266httpUpdate/library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=ESP8266httpUpdate
version=1.0
author=Markus Sattler
maintainer=Markus Sattler
maintainer=Markus Sattler
sentence=Http Update for ESP8266
paragraph=
url=https://github.com/Links2004/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266httpUpdate
architectures=ESP8266
architectures=esp8266
2 changes: 1 addition & 1 deletion libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/

#include "ESP8266HTTPUpdate.h"
#include "ESP8266httpUpdate.h"

ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {

Expand Down
8 changes: 7 additions & 1 deletion libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity){
}

int TwoWire::available(void){
return rxBufferLength - rxBufferIndex;
int result = rxBufferLength - rxBufferIndex;

if (!result) {
optimistic_yield();
}

return result;
}

int TwoWire::read(void){
Expand Down
Loading

0 comments on commit 1031e26

Please sign in to comment.