From d20c3c65c89949e149e76151d7dc3150a191cfd0 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 31 Dec 2021 13:13:51 +0100 Subject: [PATCH] Add a polyfill for `pgm_read_dword()` (issue #1433) --- extras/tests/Helpers/progmem_emulation.hpp | 4 ---- src/ArduinoJson/Polyfills/pgmspace.hpp | 10 +++++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/extras/tests/Helpers/progmem_emulation.hpp b/extras/tests/Helpers/progmem_emulation.hpp index 7ca5f158..7a977db8 100644 --- a/extras/tests/Helpers/progmem_emulation.hpp +++ b/extras/tests/Helpers/progmem_emulation.hpp @@ -31,10 +31,6 @@ inline float pgm_read_float(const void* p) { return *reinterpret_cast(convertFlashToPtr(p)); } -inline uint32_t pgm_read_dword(const void* p) { - return *reinterpret_cast(convertFlashToPtr(p)); -} - #define ARDUINOJSON_DEFINE_PROGMEM_ARRAY(type, name, value) \ static type const ARDUINOJSON_CONCAT2(name, _progmem)[] = value; \ static type const* name = reinterpret_cast( \ diff --git a/src/ArduinoJson/Polyfills/pgmspace.hpp b/src/ArduinoJson/Polyfills/pgmspace.hpp index b4c56fce..28ce4f8b 100644 --- a/src/ArduinoJson/Polyfills/pgmspace.hpp +++ b/src/ArduinoJson/Polyfills/pgmspace.hpp @@ -14,7 +14,7 @@ namespace ARDUINOJSON_NAMESPACE { // Wraps a const char* so that the our functions are picked only if the // originals are missing struct pgm_p { - pgm_p(const char* p) : address(p) {} + pgm_p(const void* p) : address(reinterpret_cast(p)) {} const char* address; }; } // namespace ARDUINOJSON_NAMESPACE @@ -95,3 +95,11 @@ inline void* memcpy_P(void* dst, ARDUINOJSON_NAMESPACE::pgm_p src, size_t n) { return dst; } #endif + +#ifndef pgm_read_dword +inline uint32_t pgm_read_dword(ARDUINOJSON_NAMESPACE::pgm_p p) { + uint32_t result; + memcpy_P(&result, p, 4); + return result; +} +#endif