Skip to content

Commit 831d643

Browse files
mhightower83earlephilhower
authored andcommitted
Add typedef for putc1, fn_putc1_t. (#6550)
* Add typedef for putc1, fn_putc1_t. Replaced relevant usage of `(void *)` with `fn_putc1_t`. Correct usage of `ets_putc()`, returning 0, in libc_replacement.cpp This PR assumes PR #6489 (comment) has merged and removes `uart_buff_switch` from `umm_performance.cpp` Updated method of defining `_rom_putc1` to be more acceptable (I hope) to the new compiler. * Use PROVIDE to expose ROM function entry point, ets_uart_putc1. Added comments to ets_putc() and ets_uart_putc1() to explain their differences. Change prototype of ets_putc() to conform with fp_putc_t. Updated _isr_safe_printf_P to use new definition, ets_uart_putc1.
1 parent ff1e8e9 commit 831d643

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

cores/esp8266/core_esp8266_postmortem.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ void __wrap_system_restart_local() {
108108
else
109109
rst_info.reason = s_user_reset_reason;
110110

111-
// TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast
112-
ets_install_putc1((void *)&uart_write_char_d);
111+
ets_install_putc1(&uart_write_char_d);
113112

114113
if (s_panic_line) {
115114
ets_printf_P(PSTR("\nPanic %S:%d %S"), s_panic_file, s_panic_line, s_panic_func);

cores/esp8266/libc_replacements.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) __attribute__((
100100
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) {
101101
(void) r;
102102
if (file->_file == STDOUT_FILENO) {
103-
return ets_putc(c);
103+
ets_putc(c);
104+
return c;
104105
}
105106
return EOF;
106107
}

cores/esp8266/uart.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,8 @@ void
899899
uart_set_debug(int uart_nr)
900900
{
901901
s_uart_debug_nr = uart_nr;
902-
void (*func)(char) = NULL;
903-
switch(s_uart_debug_nr)
902+
fp_putc_t func = NULL;
903+
switch(s_uart_debug_nr)
904904
{
905905
case UART0:
906906
func = &uart0_write_char;
@@ -929,7 +929,7 @@ uart_set_debug(int uart_nr)
929929
} else {
930930
system_set_os_print(0);
931931
}
932-
ets_install_putc1((void *) func);
932+
ets_install_putc1(func);
933933
}
934934
}
935935

cores/esp8266/umm_malloc/umm_performance.cpp

+1-18
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,8 @@ bool ICACHE_FLASH_ATTR get_umm_get_perf_data(struct _UMM_TIME_STATS *p, size_t s
3939
Objective: To be able to print "last gasp" diagnostic messages
4040
when interrupts are disabled and w/o availability of heap resources.
4141
*/
42-
43-
// ROM _putc1, ignores CRs and sends CR/LF for LF, newline.
44-
// Always returns character sent.
45-
int constexpr (*_rom_putc1)(int) = (int (*)(int))0x40001dcc;
46-
void uart_buff_switch(uint8_t);
47-
4842
int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
4943
int ICACHE_RAM_ATTR _isr_safe_printf_P(const char *fmt, ...) {
50-
#ifdef DEBUG_ESP_PORT
51-
#define VALUE(x) __STRINGIFY(x)
52-
// Preprocessor and compiler together will optimize away the if.
53-
if (strcmp("Serial1", VALUE(DEBUG_ESP_PORT)) == 0) {
54-
uart_buff_switch(1U);
55-
} else {
56-
uart_buff_switch(0U);
57-
}
58-
#else
59-
uart_buff_switch(0U); // Side effect, clears RX FIFO
60-
#endif
6144
/*
6245
To use ets_strlen() and ets_memcpy() safely with PROGMEM, flash storage,
6346
the PROGMEM address must be word (4 bytes) aligned. The destination
@@ -71,7 +54,7 @@ int ICACHE_RAM_ATTR _isr_safe_printf_P(const char *fmt, ...) {
7154
ets_memcpy(ram_buf, fmt, buf_len);
7255
va_list argPtr;
7356
va_start(argPtr, fmt);
74-
int result = ets_vprintf(_rom_putc1, ram_buf, argPtr);
57+
int result = ets_vprintf(ets_uart_putc1, ram_buf, argPtr);
7558
va_end(argPtr);
7659
return result;
7760
}

tools/sdk/include/ets_sys.h

+40-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
extern "C" {
3434
#endif
3535

36+
/*
37+
* This "print character function" prototype is modeled after the argument for
38+
* ets_install_putc1() found in "ESP8266_NONOS_SDK/include/osapi.h". This
39+
* deviates away from the familiar C library definition of putchar; however, it
40+
* agrees with the code we are working with. Note, in the ROM some "printf
41+
* character functions" always return 0 (uart_tx_one_char and ets_putc), some
42+
* return last character printed (buildin _putc1), and some return nothing
43+
* (ets_write_char). Using a void return type safely represents them all.
44+
*/
45+
typedef void (*fp_putc_t)(char);
46+
3647
typedef uint32_t ETSSignal;
3748
typedef uint32_t ETSParam;
3849

@@ -205,15 +216,41 @@ char *ets_strstr(const char *haystack, const char *needle);
205216
int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
206217
int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
207218
int ets_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
208-
void ets_install_putc1(void* routine);
219+
void ets_install_putc1(fp_putc_t routine);
209220
void ets_isr_mask(int intr);
210221
void ets_isr_unmask(int intr);
211222
void ets_isr_attach(int intr, int_handler_t handler, void *arg);
212223
void ets_intr_lock();
213224
void ets_intr_unlock();
214225
int ets_vsnprintf(char * s, size_t n, const char * format, va_list arg) __attribute__ ((format (printf, 3, 0)));
215-
int ets_vprintf(int (*print_function)(int), const char * format, va_list arg) __attribute__ ((format (printf, 2, 0)));
216-
int ets_putc(int);
226+
int ets_vprintf(fp_putc_t print_function, const char * format, va_list arg) __attribute__ ((format (printf, 2, 0)));
227+
228+
/*
229+
* ets_putc(), a "print character function" in ROM, prints a character to a
230+
* UART. It always returns 0; however, the prototype here is defined with void
231+
* return to make compatible with other usages of fp_putc_t. ets_putc() provides
232+
* a "raw", print as is, interface. '\r' and '\n' are each printed exactly as is
233+
* w/o addition. For a "cooked" interface use ets_uart_putc1().
234+
* The use of this function requires a prior setup call to uart_buff_switch() to
235+
* select the UART.
236+
*/
237+
void ets_putc(char);
238+
239+
/*
240+
* ets_uart_putc1(), a "print character function" in ROM, prints a character to
241+
* a UART. It returns the character printed; however, the prototype here is
242+
* defined with void return to make compatible with other usages of fp_putc_t.
243+
* This function provides additional processing to characters '\r' and '\n'. It
244+
* filters out '\r'. When called with '\n', it prints characters '\r' and '\n'.
245+
* This is sometimes refered to as a "cooked" interface. For a "raw", print as
246+
* is, interface use ets_putc(). The use of this function requires a prior setup
247+
* call to uart_buff_switch() to select the UART.
248+
* ets_uart_putc1() is used internally by ets_uart_printf. It is also the
249+
* function that gets installed by ets_uart_install_printf through a call to
250+
* ets_install_putc1.
251+
*/
252+
void ets_uart_putc1(char);
253+
217254
bool ets_task(ETSTask task, uint8 prio, ETSEvent *queue, uint8 qlen);
218255
bool ets_post(uint8 prio, ETSSignal sig, ETSParam par);
219256
void ets_update_cpu_frequency(uint32_t ticks_per_us);

tools/sdk/ld/eagle.rom.addr.v6.ld

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ PROVIDE ( ets_install_external_printf = 0x40002450 );
119119
PROVIDE ( ets_install_putc1 = 0x4000242c );
120120
PROVIDE ( ets_install_putc2 = 0x4000248c );
121121
PROVIDE ( ets_install_uart_printf = 0x40002438 );
122+
/* Undocumented function to print character to UART */
123+
PROVIDE ( ets_uart_putc1 = 0x40001dcc );
122124
/* permanently hide reimplemented ets_intr_*lock(), see #6484
123125
PROVIDE ( ets_intr_lock = 0x40000f74 );
124126
PROVIDE ( ets_intr_unlock = 0x40000f80 );

0 commit comments

Comments
 (0)