Skip to content

Commit 966812a

Browse files
committed
Changes on printing to comply with new understanding of SPI bus availability
from an ISR vs forground with INTLEVEL 15.
1 parent eed4b99 commit 966812a

File tree

7 files changed

+72
-75
lines changed

7 files changed

+72
-75
lines changed

cores/esp8266/heap.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,39 @@ void* _calloc_r(struct _reent* unused, size_t count, size_t size)
121121
#undef calloc
122122
#undef realloc
123123

124-
static const char oom_fmt[] PROGMEM STORE_ATTR = ":oom(%d)@?\n";
125-
static const char oom_fmt_1[] PROGMEM STORE_ATTR = ":oom(%d)@";
126-
static const char oom_fmt_2[] PROGMEM STORE_ATTR = ":%d\n";
124+
#define DEBUG_HEAP_PRINTF ets_uart_printf
127125

128126
void ICACHE_RAM_ATTR print_loc(size_t size, const char* file, int line)
129127
{
130128
(void)size;
131129
(void)line;
132130
if (system_get_os_print()) {
133-
DBGLOG_FUNCTION_P(oom_fmt_1, (int)size);
134-
DBGLOG_FUNCTION_P(file);
135-
DBGLOG_FUNCTION_P(oom_fmt_2, line);
131+
DEBUG_HEAP_PRINTF(":oom(%d)@", (int)size);
132+
133+
bool inISR = ETS_INTR_WITHINISR();
134+
if (inISR && (uint32_t)file >= 0x40200000) {
135+
DEBUG_HEAP_PRINTF("%p", file);
136+
} else if (!inISR && (uint32_t)file >= 0x40200000) {
137+
char buf[ets_strlen(file)] __attribute__ ((aligned(4)));
138+
ets_strcpy(buf, file);
139+
DEBUG_HEAP_PRINTF(buf);
140+
} else {
141+
DEBUG_HEAP_PRINTF(file);
142+
}
143+
144+
DEBUG_HEAP_PRINTF(":%d\n", line);
136145
}
137146
}
138147

139-
#define OOM_CHECK__PRINT_OOM(p, f, s) if (!p && system_get_os_print()) DBGLOG_FUNCTION_P(f, s)
148+
void ICACHE_RAM_ATTR print_oom_size(size_t size)
149+
{
150+
(void)size;
151+
if (system_get_os_print()) {
152+
DEBUG_HEAP_PRINTF(":oom(%d)@?\n", (int)size);
153+
}
154+
}
155+
156+
#define OOM_CHECK__PRINT_OOM(p, s) if (!p) print_oom_size(s)
140157
#define OOM_CHECK__PRINT_LOC(p, s, f, l) if (!p) print_loc(s, f, l)
141158

142159
#else // ! DEBUG_ESP_OOM
@@ -148,7 +165,7 @@ void ICACHE_RAM_ATTR print_loc(size_t size, const char* file, int line)
148165
#define PTR_CHECK__LOG_LAST_FAIL(p, s, a)
149166
#endif
150167

151-
#define OOM_CHECK__PRINT_OOM(p, f, s)
168+
#define OOM_CHECK__PRINT_OOM(p, s)
152169
#define OOM_CHECK__PRINT_LOC(p, s, f, l)
153170
#endif
154171

@@ -160,7 +177,7 @@ void* ICACHE_RAM_ATTR malloc(size_t size)
160177
POISON_CHECK__ABORT();
161178
void* ret = __umm_malloc(size);
162179
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
163-
OOM_CHECK__PRINT_OOM(ret, oom_fmt, (int)size);
180+
OOM_CHECK__PRINT_OOM(ret, size);
164181
return ret;
165182
}
166183

@@ -170,7 +187,7 @@ void* ICACHE_RAM_ATTR calloc(size_t count, size_t size)
170187
POISON_CHECK__ABORT();
171188
void* ret = __umm_calloc(count, size);
172189
PTR_CHECK__LOG_LAST_FAIL(ret, count * size, __builtin_return_address(0));
173-
OOM_CHECK__PRINT_OOM(ret, oom_fmt, (int)size);
190+
OOM_CHECK__PRINT_OOM(ret, size);
174191
return ret;
175192
}
176193

@@ -180,7 +197,7 @@ void* ICACHE_RAM_ATTR realloc(void* ptr, size_t size)
180197
void* ret = __umm_realloc_fl(ptr, size, NULL, 0);
181198
POISON_CHECK__ABORT();
182199
PTR_CHECK__LOG_LAST_FAIL(ret, size, __builtin_return_address(0));
183-
OOM_CHECK__PRINT_OOM(ret, oom_fmt, (int)size);
200+
OOM_CHECK__PRINT_OOM(ret, size);
184201
return ret;
185202
}
186203

@@ -247,9 +264,6 @@ size_t ICACHE_RAM_ATTR xPortWantedSizeAlign(size_t size)
247264

248265
void system_show_malloc(void)
249266
{
250-
#if !defined(UMM_INFO_PRINT)
251-
::printf(PSTR("\nTo use \"system_show_malloc()\", you need to build with the \"-DUMM_INFO_PRINT\" option.\n"));
252-
#endif
253267
umm_info(NULL, 1);
254268
}
255269

cores/esp8266/umm_malloc/isr_safe_printf.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
#include "umm_malloc_cfg.h"
1414
extern "C" {
1515

16-
#if defined(DEBUG_ESP_ISR)
16+
#if 0 //defined(DEBUG_ESP_ISR)
1717

1818
int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
1919
int ICACHE_RAM_ATTR _isr_safe_printf_P(const char *fmt, ...) {
20+
if (ETS_INTR_WITHINISR() && (uint32_t)fmt >= 0x40200000) {
21+
ets_uart_printf("\nCannot print flash string, %p, from ISR\n", fmt);
22+
return 0;
23+
}
2024
/*
2125
To use ets_strlen() and ets_memcpy() safely with PROGMEM, flash storage,
2226
the PROGMEM address must be word (4 bytes) aligned. The destination

cores/esp8266/umm_malloc/isr_safe_printf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
extern "C" {
99
#endif
1010

11+
#if 0
1112
/*
1213
ISR Safe to call for debugging. Not really safe to use all the time. The
1314
problem is going over 10us with interrupts disabled. It takes 86.8us to send
@@ -18,6 +19,7 @@ int _isr_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)
1819
// PROGMEM. Only the 1st parameter, fmt, is supported in PROGMEM.
1920
#define ISR_PRINTF(fmt, ...) _isr_safe_printf_P(PSTR(fmt), ##__VA_ARGS__)
2021
#define ISR_PRINTF_P(fmt, ...) _isr_safe_printf_P(fmt, ##__VA_ARGS__)
22+
#endif
2123

2224
#ifdef __cplusplus
2325
}

cores/esp8266/umm_malloc/umm_local.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ void print_stats(int force) {
194194
#endif
195195

196196

197+
198+
int ICACHE_FLASH_ATTR umm_info_safe_printf_P(const char *fmt, ...) {
199+
/*
200+
To use ets_strlen() and ets_strcpy() safely with PROGMEM, flash storage,
201+
the PROGMEM address must be word (4 bytes) aligned. The destination
202+
address for ets_memcpy must also be word-aligned.
203+
*/
204+
char ram_buf[ets_strlen(fmt)] __attribute__ ((aligned(4)));
205+
ets_strcpy(ram_buf, fmt);
206+
va_list argPtr;
207+
va_start(argPtr, fmt);
208+
int result = ets_vprintf(ets_uart_putc1, ram_buf, argPtr);
209+
va_end(argPtr);
210+
return result;
211+
}
212+
197213
#endif // BUILD_UMM_MALLOC_C
198214

199215

cores/esp8266/umm_malloc/umm_local.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/*
44
* A home for local items exclusive to umm_malloc.c and not to be shared in
55
* umm_malloc_cfg.h. And, not for upstream version.
6+
* Also used to redefine defines made in upstream files we donet want to edit.
67
*
78
*/
89

@@ -13,13 +14,17 @@
1314
#define memmove ets_memmove
1415
#define memset ets_memset
1516

16-
#if !defined(UMM_INFO_PRINT)
17-
/* This will stop umm_info from printing.
18-
* At this time DBGLOG_FORCE is only used by umm_info.
17+
18+
/*
19+
* This redefines DBGLOG_FORCE defined in dbglog/dbglog.h
20+
* Just for printing from umm_info() which is assumed to always be called from
21+
* non-ISR. Thus SPI bus is available to handle cache-miss and reading a flash
22+
* string while INTLEVEL is non-zero.
1923
*/
2024
#undef DBGLOG_FORCE
21-
#define DBGLOG_FORCE(force, format, ...) {(void)force; (void)format;}
22-
#endif
25+
#define DBGLOG_FORCE(force, format, ...) {if(force) {UMM_INFO_PRINTF(format, ## __VA_ARGS__);}}
26+
// #define DBGLOG_FORCE(force, format, ...) {if(force) {::printf(PSTR(format), ## __VA_ARGS__);}}
27+
2328

2429
#if defined(DEBUG_ESP_OOM) || defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || defined(UMM_INTEGRITY_CHECK)
2530
#else
@@ -30,12 +35,20 @@
3035
#define umm_free(p) free(p)
3136
#endif
3237

38+
3339
#if defined(UMM_POISON_CHECK_LITE)
3440
static int check_poison_neighbors( unsigned short cur );
3541
#endif
3642

43+
3744
#if defined(UMM_STATS) || defined(UMM_STATS_FULL)
3845
void ICACHE_FLASH_ATTR print_stats(int force);
3946
#endif
4047

48+
49+
50+
int ICACHE_FLASH_ATTR umm_info_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
51+
#define UMM_INFO_PRINTF(fmt, ...) umm_info_safe_printf_P(PSTR(fmt), ##__VA_ARGS__)
52+
53+
4154
#endif

cores/esp8266/umm_malloc/umm_malloc_cfg.h

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <debug.h>
1212
#include <pgmspace.h>
13-
#include "isr_safe_printf.h"
13+
#include <esp8266_undocumented.h>
1414

1515
#ifdef __cplusplus
1616
extern "C" {
@@ -113,25 +113,6 @@ extern char _heap_start[];
113113
#else
114114
#endif
115115

116-
/*
117-
* -D UMM_INFO_PRINT
118-
*
119-
* umm_info(NULL, true) is used to print a debug view of heap information to the
120-
* debug port. It has to walk the heap and print out information while in a
121-
* critical section. This requires that the print function be able to print w/o
122-
* doing malloc calls and from an IRQ disabled context. This requires extra IRAM
123-
* based code for printing. Without this define `umm_info(NULL, true)` will
124-
* not print.
125-
*
126-
* UMM_INFO_PRINT is enabled as part of selecting `Debug port: "Serial" or
127-
* "Serial1"`. To make available all the time use '-D UMM_INFO_PRINT`.
128-
*
129-
* Status: Local Enhancement - addresses platform specific issue.
130-
*/
131-
#if defined(DEBUG_ESP_PORT) && !defined(UMM_INFO_PRINT) && defined(UMM_INFO)
132-
#define UMM_INFO_PRINT
133-
#endif
134-
135116
/*
136117
* -D UMM_STATS :
137118
* -D UMM_STATS_FULL
@@ -587,20 +568,10 @@ static inline void _critical_exit(UMM_TIME_STAT *p, uint32_t *saved_ps) {
587568

588569
#if defined(DEBUG_ESP_PORT) || defined(DEBUG_ESP_OOM) || \
589570
defined(UMM_POISON_CHECK) || defined(UMM_POISON_CHECK_LITE) || \
590-
defined(UMM_INFO_PRINT) || defined(UMM_INTEGRITY_CHECK)
591-
592-
#ifndef DEBUG_ESP_ISR
593-
#define DEBUG_ESP_ISR
594-
#endif
595-
596-
// Note, ISR_PRINTF_P will not handle additional string arguments in
597-
// PROGMEM. Only the 1st parameter, fmt, is supported in PROGMEM.
598-
#define DBGLOG_FUNCTION(fmt, ...) ISR_PRINTF(fmt, ##__VA_ARGS__)
599-
#define DBGLOG_FUNCTION_P(fmt, ...) ISR_PRINTF_P(fmt, ##__VA_ARGS__)
600-
571+
defined(UMM_INTEGRITY_CHECK)
572+
#define DBGLOG_FUNCTION(fmt, ...) ets_uart_printf(fmt, ##__VA_ARGS__)
601573
#else
602574
#define DBGLOG_FUNCTION(fmt, ...) do { (void)fmt; } while(false)
603-
#define DBGLOG_FUNCTION_P(fmt, ...) do { (void)fmt; } while(false)
604575
#endif
605576

606577
/////////////////////////////////////////////////

cores/esp8266/umm_malloc/umm_performance.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)