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 
31 #ifndef FLASH_MEMORY_START_ADDR
32 #define FLASH_MEMORY_START_ADDR 0
33 #endif
34 
38 #define isFlashPtr(ptr) ((uint32_t)ptr >= FLASH_MEMORY_START_ADDR)
39 
43 #define IS_ALIGNED(_x) (((uint32_t)(_x)&3) == 0)
44 
48 #define ALIGNUP4(n) (((n) + 3) & ~3)
49 
53 #define ALIGNDOWN4(n) ((n) & ~3)
54 
55 #define printf_P_heap(f_P, ...) \
56  (__extension__({ \
57  char* __localF = (char*)malloc(strlen_P(f_P) + 1); \
58  strcpy_P(__localF, f_P); \
59  int __result = os_printf_plus(__localF, ##__VA_ARGS__); \
60  free(__localF); \
61  __result; \
62  }))
63 
64 #define printf_P_stack(f_P, ...) \
65  (__extension__({ \
66  char __localF[256]; \
67  strncpy_P(__localF, f_P, sizeof(__localF)); \
68  __localF[sizeof(__localF) - 1] = '\0'; \
69  m_printf(__localF, ##__VA_ARGS__); \
70  }))
71 
72 #define printf_P printf_P_stack
73 
81 #define PSTR_COUNTED(str) \
82  (__extension__({ \
83  static const char __pstr__[] PROGMEM = str; \
84  &__pstr__[0]; \
85  }))
86 
92 #define _F(str) \
93  (__extension__({ \
94  DEFINE_PSTR_LOCAL(__pstr__, str); \
95  LOAD_PSTR(buf, __pstr__); \
96  buf; \
97  }))
98 
99 
110 void* memcpy_aligned(void* dst, const void* src, unsigned len);
111 
122 int memcmp_aligned(const void* ptr1, const void* ptr2, unsigned len);
123 
128 #define DEFINE_PSTR(name, str) const char name[] PROGMEM_PSTR = str;
129 
134 #define DEFINE_PSTR_LOCAL(name, str) static DEFINE_PSTR(name, str)
135 
140 #define DECLARE_PSTR(name) extern const char name[] PROGMEM;
141 
158 #define LOAD_PSTR(name, flash_str) \
159  char name[ALIGNUP4(sizeof(flash_str))] __attribute__((aligned(4))); \
160  memcpy_aligned(name, flash_str, sizeof(flash_str));
161 
162 #define _FLOAD(pstr) \
163  (__extension__({ \
164  LOAD_PSTR(_buf, pstr); \
165  _buf; \
166  }))
167 
186 #define PSTR_ARRAY(name, str) \
187  DEFINE_PSTR_LOCAL(__pstr__##name, str); \
188  LOAD_PSTR(name, __pstr__##name)
189 
192 #ifdef __cplusplus
193 }
194 #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