Compare commits
3 commits
d35863c98a
...
338f4574c0
Author | SHA1 | Date | |
---|---|---|---|
338f4574c0 | |||
e584061d2e | |||
fe18eb56ef |
5 changed files with 5729 additions and 5681 deletions
|
@ -2,4 +2,6 @@
|
|||
|
||||
[littlefs](https://github.com/littlefs-project/littlefs) is a great filesystem for microcontrollers.
|
||||
|
||||
This is a pet project to port it to rust, mostly for me to learn how to port c to rust.
|
||||
This ~~is~~ was a pet project to port it to rust, mostly for me to learn how to port c to rust.
|
||||
|
||||
But it takes way too much effort, so you might as well use ffi.
|
||||
|
|
|
@ -1 +1,5 @@
|
|||
#![allow(unused)]
|
||||
|
||||
mod util;
|
||||
mod wip;
|
||||
pub mod littlefs;
|
||||
|
|
5978
src/littlefs.rs
5978
src/littlefs.rs
File diff suppressed because it is too large
Load diff
189
src/util.rs
Normal file
189
src/util.rs
Normal file
|
@ -0,0 +1,189 @@
|
|||
/*
|
||||
* lfs utility functions
|
||||
*
|
||||
* Copyright (c) 2022, The littlefs authors.
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
// Macros, may be replaced by system specific wrappers. Arguments to these
|
||||
// macros must not have side-effects as the macros can be removed for a smaller
|
||||
// code footprint
|
||||
|
||||
// Logging functions
|
||||
// TODO: tracing
|
||||
// #ifndef LFS_TRACE
|
||||
// #ifdef LFS_YES_TRACE
|
||||
// #define LFS_TRACE_(fmt, ...) \
|
||||
// printf("%s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
|
||||
// #define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
|
||||
// #else
|
||||
// #define LFS_TRACE(...)
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
// #ifndef LFS_DEBUG
|
||||
// #ifndef LFS_NO_DEBUG
|
||||
// #define LFS_DEBUG_(fmt, ...) \
|
||||
// printf("%s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
|
||||
// #define LFS_DEBUG(...) LFS_DEBUG_(__VA_ARGS__, "")
|
||||
// #else
|
||||
// #define LFS_DEBUG(...)
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
// #ifndef LFS_WARN
|
||||
// #ifndef LFS_NO_WARN
|
||||
// #define LFS_WARN_(fmt, ...) \
|
||||
// printf("%s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
|
||||
// #define LFS_WARN(...) LFS_WARN_(__VA_ARGS__, "")
|
||||
// #else
|
||||
// #define LFS_WARN(...)
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
// #ifndef LFS_ERROR
|
||||
// #ifndef LFS_NO_ERROR
|
||||
// #define LFS_ERROR_(fmt, ...) \
|
||||
// printf("%s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
|
||||
// #define LFS_ERROR(...) LFS_ERROR_(__VA_ARGS__, "")
|
||||
// #else
|
||||
// #define LFS_ERROR(...)
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
// Builtin functions, these may be replaced by more efficient
|
||||
// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
|
||||
// expensive basic C implementation for debugging purposes
|
||||
|
||||
use std::cmp;
|
||||
|
||||
// Min/max functions for unsigned 32-bit numbers
|
||||
pub fn lfs_max(a: u32, b: u32) -> u32 {
|
||||
cmp::max(a, b)
|
||||
}
|
||||
|
||||
pub fn lfs_min(a: u32, b: u32) -> u32 {
|
||||
cmp::min(a, b)
|
||||
}
|
||||
|
||||
// Align to nearest multiple of a size
|
||||
pub fn lfs_aligndown(a: u32, alignment: u32) -> u32 {
|
||||
a - (a % alignment)
|
||||
}
|
||||
|
||||
pub fn lfs_alignup(a: u32, alignment: u32) -> u32 {
|
||||
lfs_aligndown(a + alignment - 1, alignment)
|
||||
}
|
||||
|
||||
// Find the smallest power of 2 greater than or equal to a
|
||||
pub fn lfs_npw2(a: u32) -> u32 {
|
||||
// let mut r: u32 = 0;
|
||||
// let mut s: u32;
|
||||
// a -= 1;
|
||||
// s = (a > 0xffff) << 4; a >>= s; r |= s;
|
||||
// s = (a > 0xff ) << 3; a >>= s; r |= s;
|
||||
// s = (a > 0xf ) << 2; a >>= s; r |= s;
|
||||
// s = (a > 0x3 ) << 1; a >>= s; r |= s;
|
||||
// return (r | (a >> 1)) + 1;
|
||||
|
||||
1 << u32::BITS - a.count_zeros();
|
||||
todo!()
|
||||
}
|
||||
|
||||
// Count the number of trailing binary zeros in a
|
||||
// lfs_ctz(0) may be undefined
|
||||
pub fn lfs_ctz(a: u32) -> u32 {
|
||||
todo!()
|
||||
// return lfs_npw2((a & -a) + 1) - 1;
|
||||
}
|
||||
|
||||
// Count the number of binary ones in a
|
||||
pub fn lfs_popc(a: u32) -> u32 {
|
||||
a.count_ones()
|
||||
}
|
||||
|
||||
// Find the sequence comparison of a and b, this is the distance
|
||||
// between a and b ignoring overflow
|
||||
pub fn lfs_scmp(a: u32, b: u32) -> i32 {
|
||||
// return (int)(unsigned)(a - b);
|
||||
todo!()
|
||||
}
|
||||
|
||||
// Convert between 32-bit little-endian and native order
|
||||
pub fn lfs_fromle32(a: u32) -> u32 {
|
||||
// #if (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
||||
// return a;
|
||||
// #elif !defined(LFS_NO_INTRINSICS) && ( \
|
||||
// (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
|
||||
// return __builtin_bswap32(a);
|
||||
// #else
|
||||
// return (((uint8_t*)&a)[0] << 0) |
|
||||
// (((uint8_t*)&a)[1] << 8) |
|
||||
// (((uint8_t*)&a)[2] << 16) |
|
||||
// (((uint8_t*)&a)[3] << 24);
|
||||
// #endif
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn lfs_tole32(a: u32) -> u32 {
|
||||
lfs_fromle32(a)
|
||||
}
|
||||
|
||||
// Convert between 32-bit big-endian and native order
|
||||
pub fn lfs_frombe32(a: u32) -> u32 {
|
||||
// #if !defined(LFS_NO_INTRINSICS) && ( \
|
||||
// (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
|
||||
// return __builtin_bswap32(a);
|
||||
// #elif (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
|
||||
// (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
// return a;
|
||||
// #else
|
||||
// return (((uint8_t*)&a)[0] << 24) |
|
||||
// (((uint8_t*)&a)[1] << 16) |
|
||||
// (((uint8_t*)&a)[2] << 8) |
|
||||
// (((uint8_t*)&a)[3] << 0);
|
||||
// #endif
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn lfs_tobe32(a: u32) -> u32 {
|
||||
lfs_frombe32(a)
|
||||
}
|
||||
|
||||
// Allocate memory, only used if buffers are not provided to littlefs
|
||||
//
|
||||
// littlefs current has no alignment requirements, as it only allocates
|
||||
// byte-level buffers.
|
||||
pub fn lfs_malloc(size: usize) -> Vec<u8> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// Deallocate memory, only used if buffers are not provided to littlefs
|
||||
pub fn lfs_free(p: &[u8]) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Software CRC implementation with small lookup table
|
||||
/// Calculate CRC-32 with polynomial = 0x04c11db7
|
||||
pub fn lfs_crc(mut crc: u32, data: &[u8], size: usize) -> u32 {
|
||||
static RTABLE: [u32; 16] = [
|
||||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158,
|
||||
0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4,
|
||||
0xa00ae278, 0xbdbdf21c,
|
||||
];
|
||||
|
||||
for i in 0..size {
|
||||
crc = (crc >> 4) ^ RTABLE[((crc ^ (data[i] as u32 >> 0)) & 0xf) as usize];
|
||||
crc = (crc >> 4) ^ RTABLE[((crc ^ (data[i] as u32 >> 4)) & 0xf) as usize];
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
5235
src/wip.rs
Normal file
5235
src/wip.rs
Normal file
File diff suppressed because it is too large
Load diff
Reference in a new issue