Skip to content

Commit ec4f53e

Browse files
committed
Merge pull request #77 from Links2004/esp8266
Add Esp.restart and Esp.getVCC, add standard includes to Arduino.h
2 parents 1a233d6 + 55b6067 commit ec4f53e

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

hardware/esp8266com/esp8266/cores/esp8266/Arduino.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,23 @@
2020
#ifndef Arduino_h
2121
#define Arduino_h
2222

23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
2327
#include <stdlib.h>
2428
#include <stdint.h>
2529
#include <stdbool.h>
30+
#include <stddef.h>
31+
#include <stdarg.h>
32+
#include <stdio.h>
2633
#include <string.h>
2734
#include <math.h>
2835

2936
#include "binary.h"
3037
#include "pgmspace.h"
3138

32-
#ifdef __cplusplus
33-
extern "C" {
34-
#endif
39+
3540

3641
void yield(void);
3742

hardware/esp8266com/esp8266/cores/esp8266/Esp.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ void EspClass::wdtEnable(int)
4040
ets_wdt_enable();
4141
}
4242

43-
void EspClass::wdtDisable()
43+
void EspClass::wdtDisable(void)
4444
{
4545
ets_wdt_disable();
4646
}
4747

48-
void EspClass::wdtFeed()
48+
void EspClass::wdtFeed(void)
4949
{
5050
wdt_feed();
5151
}
@@ -56,7 +56,17 @@ void EspClass::deepSleep(uint32_t time_us, WakeMode mode)
5656
system_deep_sleep(time_us);
5757
}
5858

59-
void EspClass::reset()
59+
void EspClass::reset(void)
6060
{
6161
((void (*)(void))0x40000080)();
6262
}
63+
64+
void EspClass::restart(void)
65+
{
66+
system_restart();
67+
}
68+
69+
uint16_t EspClass::getVCC(void)
70+
{
71+
return system_get_vdd33();
72+
}

hardware/esp8266com/esp8266/cores/esp8266/Esp.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ class EspClass {
3535

3636
void wdtEnable(int timeout_ms = 0);
3737
// TODO: figure out how to set WDT timeout
38-
void wdtDisable();
39-
void wdtFeed();
38+
void wdtDisable(void);
39+
void wdtFeed(void);
4040

4141
void deepSleep(uint32_t time_us, WakeMode mode = WAKE_RF_DEFAULT);
4242

43-
void reset();
43+
44+
void reset(void);
45+
void restart(void);
46+
uint16_t getVCC(void);
4447
};
4548

4649
extern EspClass ESP;

hardware/esp8266com/esp8266/cores/esp8266/HardwareSerial.cpp

+61-3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ void ICACHE_FLASH_ATTR uart_interrupt_handler(uart_t* uart) {
157157
// ####################################################################################################
158158

159159
void ICACHE_FLASH_ATTR uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed) {
160+
if(uart == 0)
161+
return;
160162
if(uart->txEnabled) {
161163
while(true) {
162164
size_t tx_count = (READ_PERI_REG(UART_STATUS(uart->uart_nr)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT;
@@ -167,25 +169,33 @@ void ICACHE_FLASH_ATTR uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed) {
167169
}
168170

169171
size_t ICACHE_FLASH_ATTR uart_get_tx_fifo_room(uart_t* uart) {
172+
if(uart == 0)
173+
return 0;
170174
if(uart->txEnabled) {
171175
return UART_TX_FIFO_SIZE - ((READ_PERI_REG(UART_STATUS(uart->uart_nr)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT);
172176
}
173177
return 0;
174178
}
175179

176180
void ICACHE_FLASH_ATTR uart_wait_for_transmit(uart_t* uart) {
181+
if(uart == 0)
182+
return;
177183
if(uart->txEnabled) {
178184
uart_wait_for_tx_fifo(uart, UART_TX_FIFO_SIZE);
179185
}
180186
}
181187

182188
void ICACHE_FLASH_ATTR uart_transmit_char(uart_t* uart, char c) {
189+
if(uart == 0)
190+
return;
183191
if(uart->txEnabled) {
184192
WRITE_PERI_REG(UART_FIFO(uart->uart_nr), c);
185193
}
186194
}
187195

188196
void ICACHE_FLASH_ATTR uart_transmit(uart_t* uart, const char* buf, size_t size) {
197+
if(uart == 0)
198+
return;
189199
if(uart->txEnabled) {
190200
while(size) {
191201
size_t part_size = (size > UART_TX_FIFO_SIZE) ? UART_TX_FIFO_SIZE : size;
@@ -201,6 +211,9 @@ void ICACHE_FLASH_ATTR uart_transmit(uart_t* uart, const char* buf, size_t size)
201211
void ICACHE_FLASH_ATTR uart_flush(uart_t* uart) {
202212
uint32_t tmp = 0x00000000;
203213

214+
if(uart == 0)
215+
return;
216+
204217
if(uart->rxEnabled) {
205218
tmp |= UART_RXFIFO_RST;
206219
}
@@ -214,6 +227,8 @@ void ICACHE_FLASH_ATTR uart_flush(uart_t* uart) {
214227
}
215228

216229
void ICACHE_FLASH_ATTR uart_interrupt_enable(uart_t* uart) {
230+
if(uart == 0)
231+
return;
217232
WRITE_PERI_REG(UART_INT_CLR(uart->uart_nr), 0x1ff);
218233
ETS_UART_INTR_ATTACH(&uart_interrupt_handler, uart); // uart parameter is not osed in irq function!
219234
if(uart->rxEnabled) {
@@ -223,6 +238,8 @@ void ICACHE_FLASH_ATTR uart_interrupt_enable(uart_t* uart) {
223238
}
224239

225240
void ICACHE_FLASH_ATTR uart_interrupt_disable(uart_t* uart) {
241+
if(uart == 0)
242+
return;
226243
if(uart->rxEnabled) {
227244
CLEAR_PERI_REG_MASK(UART_INT_ENA(uart->uart_nr), UART_RXFIFO_FULL_INT_ENA);
228245
}
@@ -233,30 +250,43 @@ void ICACHE_FLASH_ATTR uart_interrupt_disable(uart_t* uart) {
233250
}
234251

235252
void ICACHE_FLASH_ATTR uart_arm_tx_interrupt(uart_t* uart) {
253+
if(uart == 0)
254+
return;
236255
if(uart->txEnabled) {
237256
SET_PERI_REG_MASK(UART_INT_ENA(uart->uart_nr), UART_TXFIFO_EMPTY_INT_ENA);
238257
}
239258
}
240259

241260
void ICACHE_FLASH_ATTR uart_disarm_tx_interrupt(uart_t* uart) {
261+
if(uart == 0)
262+
return;
242263
if(uart->txEnabled) {
243264
CLEAR_PERI_REG_MASK(UART_INT_ENA(uart->uart_nr), UART_TXFIFO_EMPTY_INT_ENA);
244265
}
245266
}
246267

247268
void ICACHE_FLASH_ATTR uart_set_baudrate(uart_t* uart, int baud_rate) {
269+
if(uart == 0)
270+
return;
248271
uart->baud_rate = baud_rate;
249272
uart_div_modify(uart->uart_nr, UART_CLK_FREQ / (uart->baud_rate));
250273
}
251274

252275
int ICACHE_FLASH_ATTR uart_get_baudrate(uart_t* uart) {
276+
if(uart == 0)
277+
return 0;
253278
return uart->baud_rate;
254279
}
255280

256281
uart_t* ICACHE_FLASH_ATTR uart_init(UARTnr_t uart_nr, int baudrate) {
257282

258283
uint32_t conf1 = 0x00000000;
259284
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
285+
286+
if(uart == 0) {
287+
return 0;
288+
}
289+
260290
uart->uart_nr = uart_nr;
261291

262292
switch(uart->uart_nr) {
@@ -303,6 +333,8 @@ uart_t* ICACHE_FLASH_ATTR uart_init(UARTnr_t uart_nr, int baudrate) {
303333
}
304334

305335
void ICACHE_FLASH_ATTR uart_uninit(uart_t* uart) {
336+
if(uart == 0)
337+
return;
306338
uart_interrupt_disable(uart);
307339

308340
switch(uart->rxPin) {
@@ -316,7 +348,7 @@ void ICACHE_FLASH_ATTR uart_uninit(uart_t* uart) {
316348
break;
317349
}
318350

319-
switch(uart->rxPin) {
351+
switch(uart->txPin) {
320352
case 1:
321353
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1);
322354
break;
@@ -335,6 +367,8 @@ void ICACHE_FLASH_ATTR uart_uninit(uart_t* uart) {
335367
}
336368

337369
void ICACHE_FLASH_ATTR uart_swap(uart_t* uart) {
370+
if(uart == 0)
371+
return;
338372
switch(uart->uart_nr) {
339373
case UART0:
340374
if(uart->txPin == 1 && uart->rxPin == 3) {
@@ -461,6 +495,10 @@ void ICACHE_FLASH_ATTR HardwareSerial::begin(unsigned long baud, byte config) {
461495

462496
_uart = uart_init(_uart_nr, baud);
463497

498+
if(_uart == 0) {
499+
return;
500+
}
501+
464502
if(_uart->rxEnabled) {
465503
_rx_buffer = new cbuf(SERIAL_RX_BUFFER_SIZE);
466504
}
@@ -481,10 +519,14 @@ void ICACHE_FLASH_ATTR HardwareSerial::end() {
481519
}
482520

483521
void ICACHE_FLASH_ATTR HardwareSerial::swap() {
522+
if(_uart == 0)
523+
return;
484524
uart_swap(_uart);
485525
}
486526

487527
void ICACHE_FLASH_ATTR HardwareSerial::setDebugOutput(bool en) {
528+
if(_uart == 0)
529+
return;
488530
if(en) {
489531
uart_set_debug(_uart->uart_nr);
490532
} else {
@@ -508,6 +550,8 @@ bool ICACHE_FLASH_ATTR HardwareSerial::isRxEnabled(void) {
508550
}
509551

510552
int ICACHE_FLASH_ATTR HardwareSerial::available(void) {
553+
if(_uart == 0)
554+
return 0;
511555
if(_uart->rxEnabled) {
512556
return static_cast<int>(_rx_buffer->getSize());
513557
} else {
@@ -516,6 +560,8 @@ int ICACHE_FLASH_ATTR HardwareSerial::available(void) {
516560
}
517561

518562
int ICACHE_FLASH_ATTR HardwareSerial::peek(void) {
563+
if(_uart == 0)
564+
return -1;
519565
if(_uart->rxEnabled) {
520566
return _rx_buffer->peek();
521567
} else {
@@ -524,6 +570,8 @@ int ICACHE_FLASH_ATTR HardwareSerial::peek(void) {
524570
}
525571

526572
int ICACHE_FLASH_ATTR HardwareSerial::read(void) {
573+
if(_uart == 0)
574+
return -1;
527575
if(_uart->rxEnabled) {
528576
return _rx_buffer->read();
529577
} else {
@@ -532,6 +580,8 @@ int ICACHE_FLASH_ATTR HardwareSerial::read(void) {
532580
}
533581

534582
int ICACHE_FLASH_ATTR HardwareSerial::availableForWrite(void) {
583+
if(_uart == 0)
584+
return 0;
535585
if(_uart->txEnabled) {
536586
return static_cast<int>(_tx_buffer->room());
537587
} else {
@@ -540,6 +590,8 @@ int ICACHE_FLASH_ATTR HardwareSerial::availableForWrite(void) {
540590
}
541591

542592
void ICACHE_FLASH_ATTR HardwareSerial::flush() {
593+
if(_uart == 0)
594+
return;
543595
if(!_uart->txEnabled)
544596
return;
545597
if(!_written)
@@ -552,7 +604,7 @@ void ICACHE_FLASH_ATTR HardwareSerial::flush() {
552604
}
553605

554606
size_t ICACHE_FLASH_ATTR HardwareSerial::write(uint8_t c) {
555-
if(!_uart->txEnabled)
607+
if(_uart == 0 || !_uart->txEnabled)
556608
return 0;
557609
_written = true;
558610
size_t room = uart_get_tx_fifo_room(_uart);
@@ -577,10 +629,16 @@ ICACHE_FLASH_ATTR HardwareSerial::operator bool() const {
577629
}
578630

579631
void ICACHE_FLASH_ATTR HardwareSerial::_rx_complete_irq(char c) {
580-
_rx_buffer->write(c);
632+
if(_rx_buffer) {
633+
_rx_buffer->write(c);
634+
}
581635
}
582636

583637
void ICACHE_FLASH_ATTR HardwareSerial::_tx_empty_irq(void) {
638+
if(_uart == 0)
639+
return;
640+
if(_tx_buffer == 0)
641+
return;
584642
size_t queued = _tx_buffer->getSize();
585643
if(!queued) {
586644
uart_disarm_tx_interrupt(_uart);

hardware/esp8266com/esp8266/cores/esp8266/libc_replacements.c

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ char* ICACHE_FLASH_ATTR strncat(char * dest, const char * src, size_t n) {
159159
return dest;
160160
}
161161

162+
char* ICACHE_FLASH_ATTR strtok(char * str, const char * delimiters) {
163+
return strtok_r(str, delimiters, NULL);
164+
}
165+
162166
char* ICACHE_FLASH_ATTR strtok_r(char * str, const char * delimiters, char ** temp) {
163167
static char * ret = NULL;
164168
char * start = NULL;

0 commit comments

Comments
 (0)