CTS-SAT-1-OBC-Firmware
Loading...
Searching...
No Matches
lfs_util.h
Go to the documentation of this file.
1/*
2 * lfs utility functions
3 *
4 * Copyright (c) 2022, The littlefs authors.
5 * Copyright (c) 2017, Arm Limited. All rights reserved.
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8#ifndef LFS_UTIL_H
9#define LFS_UTIL_H
10
11// ================ CTS-SAT-1 Custom Configuration =================
12
13#include "FreeRTOS.h"
14#include <stddef.h>
15#include <stdint.h>
16
17extern uint32_t LFS_debug_malloc_total_count;
18extern uint32_t LFS_debug_malloc_failed_count;
19extern uint32_t LFS_debug_free_total_count;
20
21
22static inline void *lfs_port_impl_malloc(size_t size) {
23 // Note: When investigated, the allocated size for the main operation (opening a file)
24 // is 2048 bytes (LFS_CACHE_SIZE).
26 void *ptr = pvPortMalloc(size);
27 if (ptr == NULL) {
29 }
30 return ptr;
31}
32
33static inline void lfs_port_impl_free(void *ptr) {
35 vPortFree(ptr);
36}
37
38// Route allocations to FreeRTOS heap.
39#define LFS_MALLOC(size) lfs_port_impl_malloc(size)
40#define LFS_FREE(ptr) lfs_port_impl_free(ptr)
41
42
43// TODO: Try using FreeRTOS asserts
44// #define LFS_ASSERT(x) configASSERT(x)
45
46// Disable logging. // TODO: Try setting these macros to write to debug UART or logs, maybe.
47#define LFS_NO_DEBUG 1
48#define LFS_NO_WARN 1
49#define LFS_NO_ERROR 1
50#define LFS_NO_ASSERT 1
51// #define LFS_NO_INTRINSICS // Use the default for this.
52
53// ================ End CTS-SAT-1 Custom Configuration =================
54
55
56
57#define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
58#define LFS_STRINGIZE2(x) #x
59
60// Users can override lfs_util.h with their own configuration by defining
61// LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
62//
63// If LFS_CONFIG is used, none of the default utils will be emitted and must be
64// provided by the config file. To start, I would suggest copying lfs_util.h
65// and modifying as needed.
66#ifdef LFS_CONFIG
67#include LFS_STRINGIZE(LFS_CONFIG)
68#else
69
70// Alternatively, users can provide a header file which defines
71// macros and other things consumed by littlefs.
72//
73// For example, provide my_defines.h, which contains
74// something like:
75//
76// #include <stddef.h>
77// extern void *my_malloc(size_t sz);
78// #define LFS_MALLOC(sz) my_malloc(sz)
79//
80// And build littlefs with the header by defining LFS_DEFINES.
81// (-DLFS_DEFINES=my_defines.h)
82
83#ifdef LFS_DEFINES
84#include LFS_STRINGIZE(LFS_DEFINES)
85#endif
86
87// System includes
88#include <stdint.h>
89#include <stdbool.h>
90#include <string.h>
91#include <inttypes.h>
92
93#ifndef LFS_NO_MALLOC
94#include <stdlib.h>
95#endif
96#ifndef LFS_NO_ASSERT
97#include <assert.h>
98#endif
99#if !defined(LFS_NO_DEBUG) || \
100 !defined(LFS_NO_WARN) || \
101 !defined(LFS_NO_ERROR) || \
102 defined(LFS_YES_TRACE)
103#include <stdio.h>
104#endif
105
106#ifdef __cplusplus
107extern "C"
108{
109#endif
110
111
112// Macros, may be replaced by system specific wrappers. Arguments to these
113// macros must not have side-effects as the macros can be removed for a smaller
114// code footprint
115
116// Logging functions
117#ifndef LFS_TRACE
118#ifdef LFS_YES_TRACE
119#define LFS_TRACE_(fmt, ...) \
120 printf("%s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
121#define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
122#else
123#define LFS_TRACE(...)
124#endif
125#endif
126
127#ifndef LFS_DEBUG
128#ifndef LFS_NO_DEBUG
129#define LFS_DEBUG_(fmt, ...) \
130 printf("%s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
131#define LFS_DEBUG(...) LFS_DEBUG_(__VA_ARGS__, "")
132#else
133#define LFS_DEBUG(...)
134#endif
135#endif
136
137#ifndef LFS_WARN
138#ifndef LFS_NO_WARN
139#define LFS_WARN_(fmt, ...) \
140 printf("%s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
141#define LFS_WARN(...) LFS_WARN_(__VA_ARGS__, "")
142#else
143#define LFS_WARN(...)
144#endif
145#endif
146
147#ifndef LFS_ERROR
148#ifndef LFS_NO_ERROR
149#define LFS_ERROR_(fmt, ...) \
150 printf("%s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
151#define LFS_ERROR(...) LFS_ERROR_(__VA_ARGS__, "")
152#else
153#define LFS_ERROR(...)
154#endif
155#endif
156
157// Runtime assertions
158#ifndef LFS_ASSERT
159#ifndef LFS_NO_ASSERT
160#define LFS_ASSERT(test) assert(test)
161#else
162#define LFS_ASSERT(test)
163#endif
164#endif
165
166
167// Builtin functions, these may be replaced by more efficient
168// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
169// expensive basic C implementation for debugging purposes
170
171// Min/max functions for unsigned 32-bit numbers
172static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
173 return (a > b) ? a : b;
174}
175
176static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
177 return (a < b) ? a : b;
178}
179
180// Align to nearest multiple of a size
181static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
182 return a - (a % alignment);
183}
184
185static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
186 return lfs_aligndown(a + alignment-1, alignment);
187}
188
189// Find the smallest power of 2 greater than or equal to a
190static inline uint32_t lfs_npw2(uint32_t a) {
191#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
192 return 32 - __builtin_clz(a-1);
193#else
194 uint32_t r = 0;
195 uint32_t s;
196 a -= 1;
197 s = (a > 0xffff) << 4; a >>= s; r |= s;
198 s = (a > 0xff ) << 3; a >>= s; r |= s;
199 s = (a > 0xf ) << 2; a >>= s; r |= s;
200 s = (a > 0x3 ) << 1; a >>= s; r |= s;
201 return (r | (a >> 1)) + 1;
202#endif
203}
204
205// Count the number of trailing binary zeros in a
206// lfs_ctz(0) may be undefined
207static inline uint32_t lfs_ctz(uint32_t a) {
208#if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
209 return __builtin_ctz(a);
210#else
211 return lfs_npw2((a & -a) + 1) - 1;
212#endif
213}
214
215// Count the number of binary ones in a
216static inline uint32_t lfs_popc(uint32_t a) {
217#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
218 return __builtin_popcount(a);
219#else
220 a = a - ((a >> 1) & 0x55555555);
221 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
222 return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
223#endif
224}
225
226// Find the sequence comparison of a and b, this is the distance
227// between a and b ignoring overflow
228static inline int lfs_scmp(uint32_t a, uint32_t b) {
229 return (int)(unsigned)(a - b);
230}
231
232// Convert between 32-bit little-endian and native order
233static inline uint32_t lfs_fromle32(uint32_t a) {
234#if (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
235 (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
236 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
237 return a;
238#elif !defined(LFS_NO_INTRINSICS) && ( \
239 (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
240 (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
241 (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
242 return __builtin_bswap32(a);
243#else
244 return (((uint8_t*)&a)[0] << 0) |
245 (((uint8_t*)&a)[1] << 8) |
246 (((uint8_t*)&a)[2] << 16) |
247 (((uint8_t*)&a)[3] << 24);
248#endif
249}
250
251static inline uint32_t lfs_tole32(uint32_t a) {
252 return lfs_fromle32(a);
253}
254
255// Convert between 32-bit big-endian and native order
256static inline uint32_t lfs_frombe32(uint32_t a) {
257#if !defined(LFS_NO_INTRINSICS) && ( \
258 (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
259 (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
260 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
261 return __builtin_bswap32(a);
262#elif (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
263 (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
264 (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
265 return a;
266#else
267 return (((uint8_t*)&a)[0] << 24) |
268 (((uint8_t*)&a)[1] << 16) |
269 (((uint8_t*)&a)[2] << 8) |
270 (((uint8_t*)&a)[3] << 0);
271#endif
272}
273
274static inline uint32_t lfs_tobe32(uint32_t a) {
275 return lfs_frombe32(a);
276}
277
278// Calculate CRC-32 with polynomial = 0x04c11db7
279#ifdef LFS_CRC
280uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) {
281 return LFS_CRC(crc, buffer, size)
282}
283#else
284uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
285#endif
286
287// Allocate memory, only used if buffers are not provided to littlefs
288//
289// littlefs current has no alignment requirements, as it only allocates
290// byte-level buffers.
291static inline void *lfs_malloc(size_t size) {
292#if defined(LFS_MALLOC)
293 return LFS_MALLOC(size);
294#elif !defined(LFS_NO_MALLOC)
295 return malloc(size);
296#else
297 (void)size;
298 return NULL;
299#endif
300}
301
302// Deallocate memory, only used if buffers are not provided to littlefs
303static inline void lfs_free(void *p) {
304#if defined(LFS_FREE)
305 LFS_FREE(p);
306#elif !defined(LFS_NO_MALLOC)
307 free(p);
308#else
309 (void)p;
310#endif
311}
312
313
314#ifdef __cplusplus
315} /* extern "C" */
316#endif
317
318#endif
319#endif
uint32_t LFS_debug_malloc_failed_count
Definition littlefs_telecommands.c:9
static int lfs_scmp(uint32_t a, uint32_t b)
Definition lfs_util.h:228
static uint32_t lfs_popc(uint32_t a)
Definition lfs_util.h:216
static uint32_t lfs_ctz(uint32_t a)
Definition lfs_util.h:207
static void lfs_port_impl_free(void *ptr)
Definition lfs_util.h:33
#define LFS_FREE(ptr)
Definition lfs_util.h:40
uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size)
Definition lfs_util.c:17
static uint32_t lfs_npw2(uint32_t a)
Definition lfs_util.h:190
#define LFS_MALLOC(size)
Definition lfs_util.h:39
static uint32_t lfs_frombe32(uint32_t a)
Definition lfs_util.h:256
uint32_t LFS_debug_free_total_count
Definition littlefs_telecommands.c:10
static uint32_t lfs_tole32(uint32_t a)
Definition lfs_util.h:251
uint32_t LFS_debug_malloc_total_count
Definition littlefs_telecommands.c:8
static void lfs_free(void *p)
Definition lfs_util.h:303
static void * lfs_malloc(size_t size)
Definition lfs_util.h:291
static void * lfs_port_impl_malloc(size_t size)
Definition lfs_util.h:22
static uint32_t lfs_min(uint32_t a, uint32_t b)
Definition lfs_util.h:176
static uint32_t lfs_alignup(uint32_t a, uint32_t alignment)
Definition lfs_util.h:185
static uint32_t lfs_tobe32(uint32_t a)
Definition lfs_util.h:274
static uint32_t lfs_fromle32(uint32_t a)
Definition lfs_util.h:233
static uint32_t lfs_max(uint32_t a, uint32_t b)
Definition lfs_util.h:172
static uint32_t lfs_aligndown(uint32_t a, uint32_t alignment)
Definition lfs_util.h:181