Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 8fe690d

Browse files
authored
Merge pull request #2 from khoih-prog/main
Modified Cstr routine
2 parents ca11d5d + 19f2546 commit 8fe690d

File tree

52 files changed

+723
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+723
-126
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Arduino IDE version: 1.8.19
5252
`ArduinoCore-mbed` mbed_portenta core v3.3.0
5353
Portenta_H7 Rev2 ABX00042
5454
OS: Ubuntu 20.04 LTS
55-
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
55+
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
5656
5757
Context:
5858
I encountered a crash while using this library

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Arduino IDE version: 1.8.19
3131
`ArduinoCore-mbed` mbed_portenta core v3.3.0
3232
Portenta_H7 Rev2 ABX00042
3333
OS: Ubuntu 20.04 LTS
34-
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
34+
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
3535
3636
Context:
3737
I encountered a crash while using this library
@@ -52,3 +52,4 @@ There are usually some outstanding feature requests in the [existing issues list
5252
### Sending Pull Requests
5353

5454
Pull Requests with changes and fixes are also welcome!
55+

README.md

Lines changed: 187 additions & 24 deletions
Large diffs are not rendered by default.

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
## Table of Contents
1212

1313
* [Changelog](#changelog)
14+
* [Releases v1.4.0](#Releases-v140)
1415
* [Releases v1.3.0](#Releases-v130)
1516
* [Releases v1.2.1](#Releases-v121)
1617
* [Releases v1.2.0](#Releases-v120)
@@ -23,6 +24,11 @@
2324

2425
## Changelog
2526

27+
### Releases v1.4.0
28+
29+
1. Support using `CString` in optional `SDRAM` to save heap to send `very large data`. Check [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8)
30+
2. Add multiple examples to demo the new feature
31+
2632
### Releases v1.3.0
2733

2834
1. Fix issue with slow browsers or network. Check [Target stops responding after variable time when using Firefox on Windows 10 #3](https://github.com/khoih-prog/AsyncWebServer_RP2040W/issues/3)

examples/Ethernet/Async_AdvancedWebServer/Async_AdvancedWebServer.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#endif
4444

4545
#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1
46-
#define _PORTENTA_H7_AWS_LOGLEVEL_ 1
46+
#define _PORTENTA_H7_AWS_LOGLEVEL_ 4
4747

4848
#define USE_ETHERNET_PORTENTA_H7 true
4949

@@ -177,7 +177,7 @@ void setup()
177177
digitalWrite(LED_BUILTIN, LED_OFF);
178178

179179
Serial.begin(115200);
180-
while (!Serial);
180+
while (!Serial && millis() < 5000);
181181

182182
delay(200);
183183

examples/Ethernet/Async_AdvancedWebServer_MemoryIssues_SendArduinoString/Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************************************************************
2-
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet
2+
Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino - - Dead simple AsyncWebServer for Portenta_H7
33
44
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
55
@@ -53,6 +53,9 @@
5353

5454
#include <Portenta_H7_AsyncWebServer.h>
5555

56+
// In bytes
57+
#define STRING_SIZE 40000
58+
5659
// Enter a MAC address and IP address for your controller below.
5760
#define NUMBER_OF_MAC 20
5861

@@ -146,26 +149,49 @@ void handleNotFound(AsyncWebServerRequest *request)
146149
digitalWrite(LED_BUILTIN, LED_OFF);
147150
}
148151

149-
void PrintHeapData(String hIn){
150-
mbed_stats_heap_t heap_stats;
151-
152-
Serial.print("HEAP DATA - ");
153-
Serial.print(hIn);
152+
void PrintHeapData(String hIn)
153+
{
154+
static mbed_stats_heap_t heap_stats;
155+
static uint32_t maxHeapSize = 0;
154156

155157
mbed_stats_heap_get(&heap_stats);
156-
Serial.print(" Cur heap: ");
157-
Serial.print(heap_stats.current_size);
158-
Serial.print(" Res Size: ");
159-
Serial.print(heap_stats.reserved_size);
160-
Serial.print(" Max heap: ");
161-
Serial.println(heap_stats.max_size);
158+
159+
// Print and update only when different
160+
if (maxHeapSize != heap_stats.max_size)
161+
{
162+
maxHeapSize = heap_stats.max_size;
163+
164+
Serial.print("\nHEAP DATA - ");
165+
Serial.print(hIn);
166+
167+
Serial.print(" Cur heap: ");
168+
Serial.print(heap_stats.current_size);
169+
Serial.print(" Res Size: ");
170+
Serial.print(heap_stats.reserved_size);
171+
Serial.print(" Max heap: ");
172+
Serial.println(heap_stats.max_size);
173+
}
174+
}
175+
176+
void PrintStringSize(String & out)
177+
{
178+
static uint32_t count = 0;
179+
180+
// Print only when cStr length too large and corrupting memory or every (20 * 5) s
181+
if ( (out.length() >= STRING_SIZE) || (++count > 20) )
182+
{
183+
Serial.print("\nOut String Length=");
184+
Serial.println(out.length());
185+
186+
count = 0;
187+
}
162188
}
163189

164190
void drawGraph(AsyncWebServerRequest *request)
165191
{
166192
String out;
167193

168-
out.reserve(40000);
194+
out.reserve(STRING_SIZE);
169195
char temp[70];
170196

171197
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1810\" height=\"150\">\n";
@@ -180,19 +206,18 @@ void drawGraph(AsyncWebServerRequest *request)
180206
out += temp;
181207
y = y2;
182208
}
209+
183210
out += "</g>\n</svg>\n";
184211

185212
PrintHeapData("Pre Send");
186213

187-
Serial.print("Out String Length=");
188-
Serial.println(out.length());
214+
PrintStringSize(out);
189215

190216
request->send(200, "image/svg+xml", out);
191217

192218
PrintHeapData("Post Send");
193219
}
194220

195-
196221
void setup()
197222
{
198223
pinMode(LED_BUILTIN, OUTPUT);
@@ -264,7 +289,6 @@ void setup()
264289
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
265290
Serial.println(Ethernet.localIP());
266291

267-
268292
PrintHeapData("Pre Create Arduino String");
269293

270294
}
Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/****************************************************************************************************************************
2-
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet
3-
2+
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for Portenta_H7
3+
44
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
55
66
Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet
@@ -43,7 +43,7 @@
4343
#endif
4444

4545
#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1
46-
#define _PORTENTA_H7_AWS_LOGLEVEL_ 1
46+
#define _PORTENTA_H7_AWS_LOGLEVEL_ 0
4747

4848
#define USE_ETHERNET_PORTENTA_H7 true
4949

@@ -53,7 +53,17 @@
5353

5454
#include <Portenta_H7_AsyncWebServer.h>
5555

56-
#include "SDRAM.h"
56+
char *cStr;
57+
58+
// In bytes
59+
#define CSTRING_SIZE 40000
60+
61+
// Select either cString is stored in SDRAM or not
62+
#define USING_CSTRING_IN_SDRAM true
63+
64+
#if USING_CSTRING_IN_SDRAM
65+
#include "SDRAM.h"
66+
#endif
5767

5868
// Enter a MAC address and IP address for your controller below.
5969
#define NUMBER_OF_MAC 20
@@ -91,7 +101,6 @@ int reqCount = 0; // number of requests received
91101
#define LED_OFF HIGH
92102
#define LED_ON LOW
93103

94-
95104
#define BUFFER_SIZE 768 // a little larger in case required for header shift (destructive send)
96105
char temp[BUFFER_SIZE];
97106

@@ -148,24 +157,35 @@ void handleNotFound(AsyncWebServerRequest *request)
148157
digitalWrite(LED_BUILTIN, LED_OFF);
149158
}
150159

151-
void PrintHeapData(String hIn){
152-
mbed_stats_heap_t heap_stats;
153-
154-
Serial.print("HEAP DATA - ");
155-
Serial.print(hIn);
160+
void PrintHeapData(String hIn)
161+
{
162+
static mbed_stats_heap_t heap_stats;
163+
static uint32_t maxHeapSize = 0;
156164

157165
mbed_stats_heap_get(&heap_stats);
158-
Serial.print(" Cur heap: ");
159-
Serial.print(heap_stats.current_size);
160-
Serial.print(" Res Size: ");
161-
Serial.print(heap_stats.reserved_size);
162-
Serial.print(" Max heap: ");
163-
Serial.println(heap_stats.max_size);
164-
}
165-
166166

167-
char *cStr;
167+
// Print and update only when different
168+
if (maxHeapSize != heap_stats.max_size)
169+
{
170+
maxHeapSize = heap_stats.max_size;
171+
172+
Serial.print("\nHEAP DATA - ");
173+
Serial.print(hIn);
174+
175+
Serial.print(" Cur heap: ");
176+
Serial.print(heap_stats.current_size);
177+
Serial.print(" Res Size: ");
178+
Serial.print(heap_stats.reserved_size);
179+
Serial.print(" Max heap: ");
180+
Serial.println(heap_stats.max_size);
181+
}
182+
}
168183

184+
void PrintStringSize(const char* cStr)
185+
{
186+
Serial.print("\nOut String Length=");
187+
Serial.println(strlen(cStr));
188+
}
169189

170190
void drawGraph(AsyncWebServerRequest *request)
171191
{
@@ -190,15 +210,17 @@ void drawGraph(AsyncWebServerRequest *request)
190210

191211
PrintHeapData("Pre Send");
192212

193-
Serial.print("Out String Length=");
194-
Serial.println(strlen(cStr));
213+
// Print only when cStr length too large and corrupting memory
214+
if ( (strlen(cStr) >= CSTRING_SIZE))
215+
{
216+
PrintStringSize(cStr);
217+
}
195218

196219
request->send(200, "image/svg+xml", cStr, false);
197220

198221
PrintHeapData("Post Send");
199222
}
200223

201-
202224
void setup()
203225
{
204226
pinMode(LED_BUILTIN, OUTPUT);
@@ -209,17 +231,32 @@ void setup()
209231

210232
delay(200);
211233

212-
Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_Send_CString on "); Serial.print(BOARD_NAME);
234+
#if USING_CSTRING_IN_SDRAM
235+
Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_Send_CString using SDRAM on ");
236+
#else
237+
Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_Send_CString on ");
238+
#endif
239+
240+
Serial.print(BOARD_NAME);
213241
Serial.print(" with "); Serial.println(SHIELD_TYPE);
214242
Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
215243
Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);
216244

245+
Serial.print("TCP_MSS = "); Serial.print(TCP_MSS); Serial.print(", TCP_SND_BUF = "); Serial.println(TCP_SND_BUF);
246+
247+
248+
#if USING_CSTRING_IN_SDRAM
217249
SDRAM.begin();
218250

219-
cStr = (char *)SDRAM.malloc(100000); // make a little larger than required
251+
cStr = (char *) SDRAM.malloc(CSTRING_SIZE); // make a little larger than required
252+
#else
253+
cStr = (char *) malloc(CSTRING_SIZE); // make a little larger than required
254+
#endif
220255

221-
if (cStr == NULL) {
256+
if (cStr == NULL)
257+
{
222258
Serial.println("Unable top Allocate RAM");
259+
223260
for(;;);
224261
}
225262

@@ -279,7 +316,6 @@ void setup()
279316
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
280317
Serial.println(Ethernet.localIP());
281318

282-
283319
PrintHeapData("Pre Create Arduino String");
284320

285321
}
@@ -292,7 +328,8 @@ void heartBeatPrint()
292328

293329
if (num == 80)
294330
{
295-
Serial.println();
331+
//Serial.println();
332+
PrintStringSize(cStr);
296333
num = 1;
297334
}
298335
else if (num++ % 10 == 0)

examples/Ethernet/Async_HelloServer/Async_HelloServer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void setup()
101101
digitalWrite(LED_BUILTIN, LED_OFF);
102102

103103
Serial.begin(115200);
104-
while (!Serial);
104+
while (!Serial && millis() < 5000);
105105

106106
delay(200);
107107

examples/Ethernet/Async_HelloServer2/Async_HelloServer2.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void setup()
100100
digitalWrite(LED_BUILTIN, LED_OFF);
101101

102102
Serial.begin(115200);
103-
while (!Serial);
103+
while (!Serial && millis() < 5000);
104104

105105
delay(200);
106106

examples/Ethernet/Async_HttpBasicAuth/Async_HttpBasicAuth.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const char* www_password = "portenta";
6161
void setup()
6262
{
6363
Serial.begin(115200);
64-
while (!Serial);
64+
while (!Serial && millis() < 5000);
6565

6666
delay(200);
6767

0 commit comments

Comments
 (0)