Skip to content

Commit 0c7e225

Browse files
committed
Merge pull request #325 from Links2004/esp8266
add 2 compiler options from last SDK, __attribute__ format to Print::printf, link to arduinoWebSockets
2 parents 502bb74 + f18bb28 commit 0c7e225

21 files changed

+187
-54
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Libraries that don't rely on low-level access to AVR registers should work well.
195195
- [NeoPixelBus](https://github.com/Makuna/NeoPixelBus) - Arduino NeoPixel library compatible with esp8266.
196196
- [RTC](https://github.com/Makuna/Rtc) - Arduino Library for Ds1307 & Ds3231 compatible with esp8266.
197197
- [Blynk](https://github.com/blynkkk/blynk-library) - easy IoT framework for Makers (check out the [Kickstarter page](http://tiny.cc/blynk-kick)).
198+
- [arduinoWebSockets](https://github.com/Links2004/arduinoWebSockets) - WebSocket Server and Client compatible with esp8266 (RFC6455)
198199

199200
#### Upload via serial port ####
200201
Pick the correct serial port.

cores/esp8266/Arduino.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern "C" {
3535

3636
#include "stdlib_noniso.h"
3737
#include "binary.h"
38-
#include "pgmspace.h"
3938
#include "esp8266_peri.h"
4039
#include "twi.h"
4140

@@ -205,6 +204,8 @@ void loop(void);
205204

206205
#ifdef __cplusplus
207206

207+
#include "pgmspace.h"
208+
208209
#include "WCharacter.h"
209210
#include "WString.h"
210211

cores/esp8266/Esp.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
extern "C" {
2424
#include "user_interface.h"
25+
26+
extern struct rst_info resetInfo;
2527
}
2628

2729
//extern "C" void ets_wdt_init(uint32_t val);
@@ -279,3 +281,38 @@ uint32_t EspClass::getFlashChipSizeByChipId(void) {
279281
return 0;
280282
}
281283
}
284+
285+
String EspClass::getResetInfo(void) {
286+
if(resetInfo.reason != 0) {
287+
char buff[150];
288+
sprintf(&buff[0], "Fatal exception:%d flag:%d epc1:0x%08x epc2:0x%08x epc3:0x%08x excvaddr:0x%08x depc:0x%08x", resetInfo.exccause, resetInfo.reason, resetInfo.epc1, resetInfo.epc2, resetInfo.epc3, resetInfo.excvaddr, resetInfo.depc);
289+
return String(buff);
290+
}
291+
return String("flag: 0");
292+
}
293+
294+
struct rst_info * EspClass::getResetInfoPtr(void) {
295+
return &resetInfo;
296+
}
297+
298+
bool EspClass::eraseESPconfig(void) {
299+
bool ret = true;
300+
size_t cfgAddr = (ESP.getFlashChipSize() - 0x4000);
301+
size_t cfgSize = (8*1024);
302+
303+
noInterrupts();
304+
while(cfgSize) {
305+
306+
if(spi_flash_erase_sector((cfgAddr / SPI_FLASH_SEC_SIZE)) != SPI_FLASH_RESULT_OK) {
307+
ret = false;
308+
}
309+
310+
cfgSize -= SPI_FLASH_SEC_SIZE;
311+
cfgAddr += SPI_FLASH_SEC_SIZE;
312+
}
313+
interrupts();
314+
315+
return ret;
316+
}
317+
318+

cores/esp8266/Esp.h

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class EspClass {
9898
FlashMode_t getFlashChipMode(void);
9999
uint32_t getFlashChipSizeByChipId(void);
100100

101+
String getResetInfo(void);
102+
struct rst_info * getResetInfoPtr(void);
103+
104+
bool eraseESPconfig(void);
105+
101106
inline uint32_t getCycleCount(void);
102107
};
103108

cores/esp8266/Print.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Print {
6363
return write((const uint8_t *) buffer, size);
6464
}
6565

66-
size_t printf(const char * format, ...);
66+
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
6767
size_t print(const __FlashStringHelper *);
6868
size_t print(const String &);
6969
size_t print(const char[]);

cores/esp8266/core_esp8266_main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extern "C" {
3434
#define LOOP_TASK_PRIORITY 0
3535
#define LOOP_QUEUE_SIZE 1
3636

37+
struct rst_info resetInfo;
38+
3739
int atexit(void (*func)()) {
3840
return 0;
3941
}
@@ -124,6 +126,15 @@ void user_rf_pre_init() {
124126

125127
extern "C" {
126128
void user_init(void) {
129+
uart_div_modify(0, UART_CLK_FREQ / (74480));
130+
131+
system_rtc_mem_read(0, &resetInfo, sizeof(struct rst_info));
132+
if(resetInfo.reason == WDT_RST_FLAG || resetInfo.reason == EXCEPTION_RST_FLAG) {
133+
os_printf("Last Reset:\n - flag=%d\n - Fatal exception (%d):\n - epc1=0x%08x,epc2=0x%08x,epc3=0x%08x,excvaddr=0x%08x,depc=0x%08x\n", resetInfo.reason, resetInfo.exccause, resetInfo.epc1, resetInfo.epc2, resetInfo.epc3, resetInfo.excvaddr, resetInfo.depc);
134+
}
135+
struct rst_info info = { 0 };
136+
system_rtc_mem_write(0, &info, sizeof(struct rst_info));
137+
127138
uart_div_modify(0, UART_CLK_FREQ / (115200));
128139

129140
init();

cores/esp8266/core_esp8266_timer.c

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "wiring_private.h"
2222
#include "pins_arduino.h"
2323
#include "c_types.h"
24+
#include "ets_sys.h"
2425

2526
void (*timer1_user_cb)(void);
2627

cores/esp8266/debug.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include "debug.h"
2323

2424
void ICACHE_RAM_ATTR hexdump(uint8_t *mem, uint32_t len, uint8_t cols) {
25-
os_printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", mem, len, len);
25+
os_printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (size_t)mem, len, len);
2626
for(uint32_t i = 0; i < len; i++) {
2727
if(i % cols == 0) {
28-
os_printf("\n[0x%08X] 0x%08X: ", mem, i);
28+
os_printf("\n[0x%08X] 0x%08X: ", (size_t)mem, i);
2929
yield();
3030
}
3131
os_printf("%02X ", *mem);

cores/esp8266/pgmspace.cpp

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
/*
2-
pgmspace.cpp - string functions that support PROGMEM
3-
Copyright (c) 2015 Michael C. Miller. All right reserved.
2+
pgmspace.cpp - string functions that support PROGMEM
3+
Copyright (c) 2015 Michael C. Miller. All right reserved.
44
5-
This library is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU Lesser General Public
7-
License as published by the Free Software Foundation; either
8-
version 2.1 of the License, or (at your option) any later version.
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
99
10-
This library is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13-
Lesser General Public License for more details.
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
1414
15-
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18-
*/
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
1919

2020
#include <ctype.h>
2121
#include "pgmspace.h"
2222

23-
size_t ICACHE_FLASH_ATTR strnlen_P(const char* s, size_t size) {
23+
size_t strnlen_P(const char* s, size_t size) {
2424
const char* cp;
2525
for (cp = s; size != 0 && pgm_read_byte(cp) != '\0'; cp++, size--);
26-
return (size_t)(cp - s);
26+
return (size_t) (cp - s);
2727
}
2828

29-
void* ICACHE_FLASH_ATTR memcpy_P(void* dest, const void* src, size_t count) {
29+
void* memcpy_P(void* dest, const void* src, size_t count) {
3030
const uint8_t* read = reinterpret_cast<const uint8_t*>(src);
3131
uint8_t* write = reinterpret_cast<uint8_t*>(dest);
3232

@@ -39,7 +39,7 @@ void* ICACHE_FLASH_ATTR memcpy_P(void* dest, const void* src, size_t count) {
3939
return dest;
4040
}
4141

42-
char* ICACHE_FLASH_ATTR strncpy_P(char* dest, const char* src, size_t size) {
42+
char* strncpy_P(char* dest, const char* src, size_t size) {
4343
const char* read = src;
4444
char* write = dest;
4545
char ch = '.';
@@ -48,19 +48,19 @@ char* ICACHE_FLASH_ATTR strncpy_P(char* dest, const char* src, size_t size) {
4848
ch = pgm_read_byte(read++);
4949
*write++ = ch;
5050
size--;
51-
}
51+
}
5252

5353
return dest;
5454
}
5555

56-
char* ICACHE_FLASH_ATTR strncat_P(char* dest, const char* src, size_t size) {
56+
char* strncat_P(char* dest, const char* src, size_t size) {
5757
char* write = dest;
5858

5959
while (*write != '\0')
6060
{
6161
write++;
6262
}
63-
63+
6464
const char* read = src;
6565
char ch = '.';
6666

@@ -80,7 +80,7 @@ char* ICACHE_FLASH_ATTR strncat_P(char* dest, const char* src, size_t size) {
8080
return dest;
8181
}
8282

83-
int ICACHE_FLASH_ATTR strncmp_P(const char* str1, const char* str2P, size_t size) {
83+
int strncmp_P(const char* str1, const char* str2P, size_t size) {
8484
int result = 0;
8585

8686
while (size > 0)
@@ -99,7 +99,7 @@ int ICACHE_FLASH_ATTR strncmp_P(const char* str1, const char* str2P, size_t size
9999
return result;
100100
}
101101

102-
int ICACHE_FLASH_ATTR strncasecmp_P(const char* str1, const char* str2P, size_t size) {
102+
int strncasecmp_P(const char* str1, const char* str2P, size_t size) {
103103
int result = 0;
104104

105105
while (size > 0)
@@ -118,7 +118,7 @@ int ICACHE_FLASH_ATTR strncasecmp_P(const char* str1, const char* str2P, size_t
118118
return result;
119119
}
120120

121-
int ICACHE_FLASH_ATTR printf_P(const char* formatP, ...) {
121+
int printf_P(const char* formatP, ...) {
122122
int ret;
123123
va_list arglist;
124124
va_start(arglist, formatP);
@@ -129,13 +129,13 @@ int ICACHE_FLASH_ATTR printf_P(const char* formatP, ...) {
129129

130130
ret = os_printf(format, arglist);
131131

132-
delete [] format;
132+
delete[] format;
133133

134134
va_end(arglist);
135135
return ret;
136136
}
137137

138-
int ICACHE_FLASH_ATTR snprintf_P(char* str, size_t strSize, const char* formatP, ...) {
138+
int snprintf_P(char* str, size_t strSize, const char* formatP, ...) {
139139
int ret;
140140
va_list arglist;
141141
va_start(arglist, formatP);
@@ -146,7 +146,7 @@ int ICACHE_FLASH_ATTR snprintf_P(char* str, size_t strSize, const char* formatP,
146146
return ret;
147147
}
148148

149-
int ICACHE_FLASH_ATTR vsnprintf_P(char* str, size_t strSize, const char* formatP, va_list ap) {
149+
int vsnprintf_P(char* str, size_t strSize, const char* formatP, va_list ap) {
150150
int ret;
151151

152152
size_t fmtLen = strlen_P(formatP);
@@ -155,7 +155,7 @@ int ICACHE_FLASH_ATTR vsnprintf_P(char* str, size_t strSize, const char* formatP
155155

156156
ret = ets_vsnprintf(str, strSize, format, ap);
157157

158-
delete [] format;
158+
delete[] format;
159159

160160
return ret;
161-
}
161+
}

cores/esp8266/pgmspace.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ typedef uint32_t prog_uint32_t;
3232

3333
#define SIZE_IRRELEVANT 0x7fffffff
3434

35-
extern void* memcpy_P(void* dest, const void* src, size_t count);
35+
void* memcpy_P(void* dest, const void* src, size_t count);
3636

37-
extern char* strncpy_P(char* dest, const char* src, size_t size);
37+
char* strncpy_P(char* dest, const char* src, size_t size);
3838
#define strcpy_P(dest, src) strncpy_P((dest), (src), SIZE_IRRELEVANT)
3939

40-
extern char* strncat_P(char* dest, const char* src, size_t size);
40+
char* strncat_P(char* dest, const char* src, size_t size);
4141
#define strcat_P(dest, src) strncat_P((dest), (src), SIZE_IRRELEVANT)
4242

43-
extern int strncmp_P(const char* str1, const char* str2P, size_t size);
43+
int strncmp_P(const char* str1, const char* str2P, size_t size);
4444
#define strcmp_P(str1, str2P) strncmp_P((str1), (str2P), SIZE_IRRELEVANT)
4545

46-
extern int strncasecmp_P(const char* str1, const char* str2P, size_t size);
46+
int strncasecmp_P(const char* str1, const char* str2P, size_t size);
4747
#define strcasecmp_P(str1, str2P) strncasecmp_P((str1), (str2P), SIZE_IRRELEVANT)
4848

49-
extern size_t strnlen_P(const char *s, size_t size);
49+
size_t strnlen_P(const char *s, size_t size);
5050
#define strlen_P(strP) strnlen_P((strP), SIZE_IRRELEVANT)
5151

52-
extern int printf_P(const char *formatP, ...);
53-
extern int snprintf_P(char *str, size_t strSize, const char *formatP, ...);
54-
extern int vsnprintf_P(char *str, size_t strSize, const char *formatP, va_list ap);
52+
int printf_P(const char *formatP, ...) __attribute__ ((format (printf, 1, 2)));
53+
int snprintf_P(char *str, size_t strSize, const char *formatP, ...) __attribute__ ((format (printf, 3, 4)));
54+
int vsnprintf_P(char *str, size_t strSize, const char *formatP, va_list ap) __attribute__ ((format (printf, 3, 0)));
5555

5656
// flash memory must be read using 32 bit aligned addresses else a processor
5757
// exception will be triggered

libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,32 @@ uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac)
203203
return mac;
204204
}
205205

206+
String ESP8266WiFiClass::macAddress(void)
207+
{
208+
uint8_t mac[6];
209+
char macStr[18] = {0};
210+
wifi_get_macaddr(STATION_IF, mac);
211+
212+
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
213+
return String(macStr);
214+
}
215+
206216
uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac)
207217
{
208218
wifi_get_macaddr(SOFTAP_IF, mac);
209219
return mac;
210220
}
211221

222+
String ESP8266WiFiClass::softAPmacAddress(void)
223+
{
224+
uint8_t mac[6];
225+
char macStr[18] = {0};
226+
wifi_get_macaddr(SOFTAP_IF, mac);
227+
228+
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
229+
return String(macStr);
230+
}
231+
212232
IPAddress ESP8266WiFiClass::localIP()
213233
{
214234
struct ip_info ip;
@@ -251,10 +271,25 @@ uint8_t* ESP8266WiFiClass::BSSID(void)
251271
return reinterpret_cast<uint8_t*>(conf.bssid);
252272
}
253273

274+
String ESP8266WiFiClass::BSSIDstr(void)
275+
{
276+
static struct station_config conf;
277+
char mac[18] = {0};
278+
wifi_station_get_config(&conf);
279+
sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]);
280+
return String(mac);
281+
}
282+
283+
254284
int32_t ESP8266WiFiClass::channel(void) {
255285
return wifi_get_channel();
256286
}
257287

288+
289+
int32_t ESP8266WiFiClass::RSSI(void) {
290+
return wifi_station_get_rssi();
291+
}
292+
258293
extern "C"
259294
{
260295
typedef STAILQ_HEAD(, bss_info) bss_info_head_t;
@@ -353,6 +388,17 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i)
353388
return it->bssid;
354389
}
355390

391+
String ESP8266WiFiClass::BSSIDstr(uint8_t i)
392+
{
393+
char mac[18] = {0};
394+
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));
395+
if (!it)
396+
return String("");
397+
398+
sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]);
399+
return String(mac);
400+
}
401+
356402
int32_t ESP8266WiFiClass::channel(uint8_t i)
357403
{
358404
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));

0 commit comments

Comments
 (0)