Utility.hpp
Go to the documentation of this file.
1 /****
2  * Utility.hpp - Definitions, common macros and utility functions
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: Nov 2019 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "config.hpp"
25 
58 #define DECL(t) argument_type<void(t)>::type
59 template <typename T> struct argument_type;
60 template <typename T, typename U> struct argument_type<T(U)> {
61  typedef U type;
62 };
63 
86 // clang-format off
87 #define STR(x) XSTR(x)
88 #define XSTR(x) #x
89 #ifdef __WIN32
90 #define IMPORT_FSTR_DATA(name, file) \
91  __asm__(".section .rodata\n" \
92  ".def _" STR(name) "; .scl 2; .type 32; .endef\n" \
93  ".align 4\n" \
94  "_" STR(name) ":\n" \
95  ".long _" STR(name) "_end - _" STR(name) " - 4\n" \
96  ".incbin \"" file "\"\n" \
97  "_" STR(name) "_end:\n");
98 #elif defined(__arm__)
99 #define IMPORT_FSTR_DATA(name, file) \
100  __asm__(".section " ICACHE_RODATA_SECTION "." #name "\n" \
101  ".type " STR(name) ", %object\n" \
102  ".align 4\n" STR(name) ":\n" \
103  ".long _" STR(name) "_end - " STR(name) " - 4\n" \
104  ".incbin \"" file "\"\n" \
105  "_" STR(name) "_end:\n");
106 #else
107 #define IMPORT_FSTR_DATA(name, file) \
108  __asm__(".section " ICACHE_RODATA_SECTION "." #name "\n" \
109  ".type " STR(name) ", @object\n" \
110  ".align 4\n" STR(name) ":\n" \
111  ".long _" STR(name) "_end - " STR(name) " - 4\n" \
112  ".incbin \"" file "\"\n" \
113  "_" STR(name) "_end:\n");
114 #endif
115 // clang-format on
116 
117 namespace FSTR
118 {
126 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 1, T>::type readValue(const T* ptr)
127 {
128  return static_cast<T>(pgm_read_byte(ptr));
129 }
130 
131 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 2, T>::type readValue(const T* ptr)
132 {
133  return static_cast<T>(pgm_read_word(ptr));
134 }
135 
136 template <typename T> FSTR_INLINE typename std::enable_if<sizeof(T) == 4, T>::type readValue(const T* ptr)
137 {
138  union {
139  uint32_t u32;
140  T value;
141  } tmp;
142  tmp.u32 = pgm_read_dword(ptr);
143  return tmp.value;
144 }
145 
146 template <typename T>
147 FSTR_INLINE typename std::enable_if<(sizeof(T) > 4) && IS_ALIGNED(sizeof(T)), T>::type readValue(const T* ptr)
148 {
149  T value;
150  memcpy_aligned(&value, ptr, sizeof(T));
151  return value;
152 }
153 
156 } // namespace FSTR
157 
#define pgm_read_byte(addr)
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:38
#define pgm_read_dword(addr)
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:44
#define pgm_read_word(addr)
Definition: Arch/Esp32/Components/libc/src/include/sys/pgmspace.h:39
uint32_t u32
Definition: c_types.h:19
#define FSTR_INLINE
Definition: config.hpp:28
#define IS_ALIGNED(_x)
determines if the given value is aligned to a word (4-byte) boundary
Definition: FakePgmSpace.h:34
void * memcpy_aligned(void *dst, const void *src, unsigned len)
copy memory aligned to word boundaries
Definition: Array.hpp:108
std::enable_if< sizeof(T)==1, T >::type readValue(const T *ptr)
Read a typed value from flash memory ensuring correct alignment of access.
Definition: Utility.hpp:126
U type
Definition: Utility.hpp:61
Definition: Utility.hpp:59