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 <stdio.h>
14 #include <esp_attr.h>
15 #include <sys/pgmspace.h>
16 
17 #include "m_printf.h"
18 #include <c_types.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
34 #define IS_ALIGNED(_x) (((uint32_t)(_x)&3) == 0)
35 
39 #define ALIGNUP4(n) (((n) + 3) & ~3)
40 
44 #define ALIGNDOWN4(n) ((n) & ~3)
45 
46 #define printf_P_heap(f_P, ...) \
47  (__extension__({ \
48  char* __localF = (char*)malloc(strlen_P(f_P) + 1); \
49  strcpy_P(__localF, f_P); \
50  int __result = os_printf_plus(__localF, ##__VA_ARGS__); \
51  free(__localF); \
52  __result; \
53  }))
54 
55 #define printf_P_stack(f_P, ...) \
56  (__extension__({ \
57  char __localF[256]; \
58  strncpy_P(__localF, f_P, sizeof(__localF)); \
59  __localF[sizeof(__localF) - 1] = '\0'; \
60  m_printf(__localF, ##__VA_ARGS__); \
61  }))
62 
63 #define printf_P printf_P_stack
64 
72 #define PSTR_COUNTED(str) \
73  (__extension__({ \
74  static const char __pstr__[] PROGMEM = str; \
75  &__pstr__[0]; \
76  }))
77 
83 #define _F(str) \
84  (__extension__({ \
85  DEFINE_PSTR_LOCAL(__pstr__, str); \
86  LOAD_PSTR(buf, __pstr__); \
87  buf; \
88  }))
89 
100 void* memcpy_aligned(void* dst, const void* src, unsigned len);
101 
112 int memcmp_aligned(const void* ptr1, const void* ptr2, unsigned len);
113 
118 #define DEFINE_PSTR(name, str) const char name[] PROGMEM_PSTR = str;
119 
124 #define DEFINE_PSTR_LOCAL(name, str) static DEFINE_PSTR(name, str)
125 
130 #define DECLARE_PSTR(name) extern const char name[] PROGMEM;
131 
148 #define LOAD_PSTR(name, flash_str) \
149  char name[ALIGNUP4(sizeof(flash_str))] __attribute__((aligned(4))); \
150  memcpy_aligned(name, flash_str, sizeof(flash_str));
151 
152 #define _FLOAD(pstr) \
153  (__extension__({ \
154  LOAD_PSTR(_buf, pstr); \
155  _buf; \
156  }))
157 
176 #define PSTR_ARRAY(name, str) \
177  DEFINE_PSTR_LOCAL(__pstr__##name, str); \
178  LOAD_PSTR(name, __pstr__##name)
179 
182 #ifdef __cplusplus
183 }
184 #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