forked from M-Labs/artiq
runtime: get rid of config_dummy.rs. NFC.
Use the same strategy as elsewhere.
This commit is contained in:
parent
0ede5d8638
commit
6cbf8786d8
|
@ -1,12 +1,14 @@
|
||||||
use core::str;
|
#[cfg(has_spiflash)]
|
||||||
use std::btree_map::BTreeMap;
|
mod imp {
|
||||||
use byteorder::{ByteOrder, BigEndian};
|
use core::str;
|
||||||
use board::{mem, csr, cache, spiflash};
|
use std::btree_map::BTreeMap;
|
||||||
|
use byteorder::{ByteOrder, BigEndian};
|
||||||
|
use board::{mem, csr, cache, spiflash};
|
||||||
|
|
||||||
const ADDR: usize = mem::FLASH_BOOT_ADDRESS + 0x80000 /* max runtime size */;
|
const ADDR: usize = mem::FLASH_BOOT_ADDRESS + 0x80000 /* max runtime size */;
|
||||||
const SIZE: usize = csr::CONFIG_SPIFLASH_SECTOR_SIZE as usize;
|
const SIZE: usize = csr::CONFIG_SPIFLASH_SECTOR_SIZE as usize;
|
||||||
|
|
||||||
mod lock {
|
mod lock {
|
||||||
use core::slice;
|
use core::slice;
|
||||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
@ -33,22 +35,22 @@ mod lock {
|
||||||
LOCKED.store(0, Ordering::SeqCst)
|
LOCKED.store(0, Ordering::SeqCst)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use self::lock::Lock;
|
use self::lock::Lock;
|
||||||
|
|
||||||
struct Iter<'a> {
|
struct Iter<'a> {
|
||||||
data: &'a [u8],
|
data: &'a [u8],
|
||||||
offset: usize
|
offset: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iter<'a> {
|
impl<'a> Iter<'a> {
|
||||||
fn new(data: &'a [u8]) -> Iter<'a> {
|
fn new(data: &'a [u8]) -> Iter<'a> {
|
||||||
Iter { data: data, offset: 0 }
|
Iter { data: data, offset: 0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for Iter<'a> {
|
impl<'a> Iterator for Iter<'a> {
|
||||||
type Item = Result<(&'a [u8], &'a [u8]), ()>;
|
type Item = Result<(&'a [u8], &'a [u8]), ()>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
@ -82,9 +84,9 @@ impl<'a> Iterator for Iter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(key: &str, f: F) -> R {
|
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(key: &str, f: F) -> R {
|
||||||
f(Lock::take().and_then(|lock| {
|
f(Lock::take().and_then(|lock| {
|
||||||
let mut iter = Iter::new(lock.data());
|
let mut iter = Iter::new(lock.data());
|
||||||
let mut value = &[][..];
|
let mut value = &[][..];
|
||||||
|
@ -97,15 +99,15 @@ pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(key: &str, f: F) -> R {
|
||||||
}
|
}
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(key: &str, f: F) -> R {
|
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(key: &str, f: F) -> R {
|
||||||
read(key, |result| {
|
read(key, |result| {
|
||||||
f(result.and_then(|value| str::from_utf8(value).map_err(|_| ())))
|
f(result.and_then(|value| str::from_utf8(value).map_err(|_| ())))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append_at(mut offset: usize, key: &[u8], value: &[u8]) -> Result<usize, ()> {
|
fn append_at(mut offset: usize, key: &[u8], value: &[u8]) -> Result<usize, ()> {
|
||||||
let record_size = 4 + key.len() + 1 + value.len();
|
let record_size = 4 + key.len() + 1 + value.len();
|
||||||
if offset + record_size > SIZE {
|
if offset + record_size > SIZE {
|
||||||
return Err(())
|
return Err(())
|
||||||
|
@ -128,9 +130,9 @@ fn append_at(mut offset: usize, key: &[u8], value: &[u8]) -> Result<usize, ()> {
|
||||||
|
|
||||||
cache::flush_l2_cache();
|
cache::flush_l2_cache();
|
||||||
Ok(offset)
|
Ok(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compact() -> Result<(), ()> {
|
fn compact() -> Result<(), ()> {
|
||||||
let lock = Lock::take()?;
|
let lock = Lock::take()?;
|
||||||
|
|
||||||
let mut items = BTreeMap::new();
|
let mut items = BTreeMap::new();
|
||||||
|
@ -150,9 +152,9 @@ fn compact() -> Result<(), ()> {
|
||||||
offset = append_at(offset, key, value)?;
|
offset = append_at(offset, key, value)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append(key: &str, value: &[u8]) -> Result<(), ()> {
|
fn append(key: &str, value: &[u8]) -> Result<(), ()> {
|
||||||
let lock = Lock::take()?;
|
let lock = Lock::take()?;
|
||||||
|
|
||||||
let free_offset = {
|
let free_offset = {
|
||||||
|
@ -165,9 +167,9 @@ fn append(key: &str, value: &[u8]) -> Result<(), ()> {
|
||||||
|
|
||||||
append_at(free_offset, key.as_bytes(), value)?;
|
append_at(free_offset, key.as_bytes(), value)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(key: &str, value: &[u8]) -> Result<(), ()> {
|
pub fn write(key: &str, value: &[u8]) -> Result<(), ()> {
|
||||||
match append(key, value) {
|
match append(key, value) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(()) => {
|
Err(()) => {
|
||||||
|
@ -176,17 +178,43 @@ pub fn write(key: &str, value: &[u8]) -> Result<(), ()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(key: &str) -> Result<(), ()> {
|
pub fn remove(key: &str) -> Result<(), ()> {
|
||||||
write(key, &[])
|
write(key, &[])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn erase() -> Result<(), ()> {
|
pub fn erase() -> Result<(), ()> {
|
||||||
let _lock = Lock::take()?;
|
let _lock = Lock::take()?;
|
||||||
|
|
||||||
spiflash::erase_sector(ADDR);
|
spiflash::erase_sector(ADDR);
|
||||||
cache::flush_l2_cache();
|
cache::flush_l2_cache();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(has_spiflash))]
|
||||||
|
mod imp {
|
||||||
|
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(_key: &str, f: F) -> R {
|
||||||
|
f(Err(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(_key: &str, f: F) -> R {
|
||||||
|
f(Err(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(_key: &str, _value: &[u8]) -> Result<(), ()> {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(_key: &str) -> Result<(), ()> {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn erase() -> Result<(), ()> {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use self::imp::*;
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
pub fn read<F: FnOnce(Result<&[u8], ()>) -> R, R>(_key: &str, f: F) -> R {
|
|
||||||
f(Err(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_str<F: FnOnce(Result<&str, ()>) -> R, R>(_key: &str, f: F) -> R {
|
|
||||||
f(Err(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(_key: &str, _value: &[u8]) -> Result<(), ()> {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(_key: &str) -> Result<(), ()> {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn erase() -> Result<(), ()> {
|
|
||||||
Err(())
|
|
||||||
}
|
|
|
@ -34,10 +34,7 @@ macro_rules! borrow_mut {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(has_spiflash)]
|
|
||||||
mod config;
|
mod config;
|
||||||
#[cfg(not(has_spiflash))]
|
|
||||||
#[path="config_dummy.rs"] mod config;
|
|
||||||
mod ethmac;
|
mod ethmac;
|
||||||
mod rtio_mgt;
|
mod rtio_mgt;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue