forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_common.h
197 lines (157 loc) · 5.13 KB
/
storage_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef ARDUINO_STORAGE_COMMON_H
#define ARDUINO_STORAGE_COMMON_H
#include "Arduino.h"
#ifdef __cplusplus
extern "C" {
#endif
//#define STORAGE_DEBUG
//#define STORAGE_ASSERT
/* -------------------------------------------------------------------------- */
/* STORAGE DEBUG */
/* -------------------------------------------------------------------------- */
#ifdef STORAGE_DEBUG
#define STORAGE_BUFF_DIM 512
#define PRINT_SIZE 32
extern char rns_storage_dbg_buf[STORAGE_BUFF_DIM];
/** Output a debug message
*
* @param format printf-style format string, followed by variables
*/
static inline void rns_storage_dbg(const char *fmt, ...)
{
memset(rns_storage_dbg_buf,0x00,256);
va_list va;
va_start (va, fmt);
vsnprintf (rns_storage_dbg_buf,STORAGE_BUFF_DIM, fmt, va);
va_end (va);
if(Serial)
Serial.println(rns_storage_dbg_buf);
}
/** Conditionally output a debug message
*
* NOTE: If the condition is constant false (== 0) and the compiler optimization
* level is greater than 0, then the whole function will be compiled away.
*
* @param condition output only if condition is true (!= 0)
* @param format printf-style format string, followed by variables
*/
static inline void rns_storage_dbg_if(int condition, const char *fmt, ...)
{
if (condition) {
memset(rns_storage_dbg_buf,0x00,256);
va_list va;
va_start (va, fmt);
vsnprintf (rns_storage_dbg_buf,STORAGE_BUFF_DIM, fmt, va);
va_end (va);
if(Serial)
Serial.println(rns_storage_dbg_buf);
}
}
static inline void rns_storage_dbg_mem(uint8_t *b, uint32_t _size)
{
if (b != nullptr) {
Serial.println("");
for(int i = 0; i < _size; i++) {
if(i != 0 && i % PRINT_SIZE == 0) {
if(i != 0)
Serial.println();
}
Serial.print(*(b + i) >> 4, HEX);
Serial.print(*(b + i) & 0x0F,HEX);
}
Serial.println();
Serial.println("");
}
}
#else
static inline void rns_storage_dbg_if(int condition, const char *format, ...) {
(void)condition;
(void)format;
}
static inline void rns_storage_dbg(const char *format, ...) {
(void)format;
}
static inline void rns_storage_dbg_mem(uint8_t *b, uint32_t _size) {
(void)b;
(void)_size;
}
#endif // STORAGE_DEBUG
/* -------------------------------------------------------------------------- */
/* STORAGE ASSERTS */
/* -------------------------------------------------------------------------- */
#ifdef STORAGE_ASSERT
#define MBED_ASSERT(expr) do { if (!(expr)) { \
debug("ASSERT FAILED at line %d in file %s",__LINE__,__FILE__); }} while(0)
#else
#define MBED_ASSERT(expr)
#endif
/* -------------------------------------------------------------------------- */
/* DUMMY MUTEX */
/* -------------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif
template<typename T>
class NonCopyable {
protected:
NonCopyable() = default;
~NonCopyable() = default;
public:
NonCopyable(const NonCopyable &) = delete;
NonCopyable &operator=(const NonCopyable &) = delete;
};
class PlatformMutex: private NonCopyable<PlatformMutex> {
public:
PlatformMutex()
{
}
~PlatformMutex()
{
}
void lock()
{
}
void unlock()
{
}
};
#define POLLIN 0x0001 ///< Data may be read without blocking
#define POLLOUT 0x0010 ///< Data may be written without blocking
#define POLLERR 0x1000 ///< An error has occurred on the device or stream
#define POLLHUP 0x2000 ///< The device has been disconnected
#define POLLNVAL 0x4000 ///< The specified file handle value is invalid
uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta);
uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta);
uint8_t core_util_atomic_load_u8(const volatile uint8_t *valuePtr);
void core_util_atomic_store_u8(volatile uint8_t *valuePtr, uint8_t desiredValue);
#ifndef ARDUINO_PACKED
#if defined(__ICCARM__)
#define ARDUINO_PACKED(struct) __packed struct
#else
#define ARDUINO_PACKED(struct) struct __attribute__((packed))
#endif
#endif
class FileHandle;
/** \addtogroup platform-public-api */
/** @{*/
/**
* \defgroup platform_poll poll functions
* @{
*/
struct pollfh {
FileHandle *fh;
short events;
short revents;
};
/** A mechanism to multiplex input/output over a set of file handles(file descriptors).
* For every file handle provided, poll() examines it for any events registered for that particular
* file handle.
*
* @param fhs an array of PollFh struct carrying a FileHandle and bitmasks of events
* @param nfhs number of file handles
* @param timeout timer value to timeout or -1 for loop forever
*
* @return number of file handles selected (for which revents is non-zero). 0 if timed out with nothing selected. -1 for error.
*/
int poll(pollfh fhs[], unsigned nfhs, int timeout);
#endif