FakePgmSpace.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FakePgmSpace.h - Support for reading flash memory
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <esp_attr.h>
14 #include <sys/pgmspace.h>
15 
16 #include "m_printf.h"
17 #include "c_types.h"
18 
19 #ifdef __cplusplus
20 extern "C"
21 {
22 #endif
23 
34 #define isFlashPtr(ptr) ((uint32_t)ptr >= 0x40200000)
35 
39 #define IS_ALIGNED(_x) (((uint32_t)(_x)&3) == 0)
40 
44 #define ALIGNUP(_n) (((_n) + 3) & ~3)
45 
49 #define ALIGNDOWN(_n) ((_n) & ~3)
50 
51 #define printf_P_heap(f_P, ...) \
52  (__extension__({ \
53  char* __localF = (char*)malloc(strlen_P(f_P) + 1); \
54  strcpy_P(__localF, f_P); \
55  int __result = os_printf_plus(__localF, ##__VA_ARGS__); \
56  free(__localF); \
57  __result; \
58  }))
59 
60 #define printf_P_stack(f_P, ...) \
61  (__extension__({ \
62  char __localF[256]; \
63  strncpy_P(__localF, f_P, sizeof(__localF)); \
64  __localF[sizeof(__localF) - 1] = '\0'; \
65  m_printf(__localF, ##__VA_ARGS__); \
66  }))
67 
68 #define printf_P printf_P_stack
69 
77 #define PSTR_COUNTED(str) \
78  (__extension__({ \
79  static const char __pstr__[] PROGMEM = str; \
80  &__pstr__[0]; \
81  }))
82 
88 #define _F(str) \
89  (__extension__({ \
90  DEFINE_PSTR_LOCAL(__pstr__, str); \
91  LOAD_PSTR(buf, __pstr__); \
92  buf; \
93  }))
94 
95 
106 void* memcpy_aligned(void* dst, const void* src, unsigned len);
107 
118 int memcmp_aligned(const void* ptr1, const void* ptr2, unsigned len);
119 
124 #define DEFINE_PSTR(name, str) const char name[] PROGMEM_PSTR = str;
125 
130 #define DEFINE_PSTR_LOCAL(name, str) static DEFINE_PSTR(name, str)
131 
136 #define DECLARE_PSTR(name) extern const char name[] PROGMEM;
137 
154 #define LOAD_PSTR(name, flash_str) \
155  char name[ALIGNUP(sizeof(flash_str))] __attribute__((aligned(4))); \
156  memcpy_aligned(name, flash_str, sizeof(flash_str));
157 
158 #define _FLOAD(pstr) \
159  (__extension__({ \
160  LOAD_PSTR(_buf, pstr); \
161  _buf; \
162  }))
163 
182 #define PSTR_ARRAY(name, str) \
183  DEFINE_PSTR_LOCAL(__pstr__##name, str); \
184  LOAD_PSTR(name, __pstr__##name)
185 
188 #ifdef __cplusplus
189 }
190 #endif
void * memcpy_aligned(void *dst, const void *src, unsigned len)
copy memory aligned to word boundaries
int memcmp_aligned(const void *ptr1, const void *ptr2, unsigned len)
compare memory aligned to word boundaries