From 0a9d3638eed9ed8ecaae9f03d2bb26267036e623 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 26 Jul 2018 14:49:32 +0800 Subject: [PATCH] config: add write_int --- artiq/firmware/libboard_misoc/config.rs | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/artiq/firmware/libboard_misoc/config.rs b/artiq/firmware/libboard_misoc/config.rs index 443221d76..ba5da0d42 100644 --- a/artiq/firmware/libboard_misoc/config.rs +++ b/artiq/firmware/libboard_misoc/config.rs @@ -29,6 +29,35 @@ impl fmt::Display for Error { } } +struct FmtWrapper<'a> { + buf: &'a mut [u8], + offset: usize, +} + +impl<'a> FmtWrapper<'a> { + fn new(buf: &'a mut [u8]) -> Self { + FmtWrapper { + buf: buf, + offset: 0, + } + } + + fn contents(&self) -> &[u8] { + &self.buf[..self.offset] + } +} + +impl<'a> fmt::Write for FmtWrapper<'a> { + fn write_str(&mut self, s: &str) -> fmt::Result { + let bytes = s.as_bytes(); + let remainder = &mut self.buf[self.offset..]; + let remainder = &mut remainder[..bytes.len()]; + remainder.copy_from_slice(bytes); + self.offset += bytes.len(); + Ok(()) + } +} + #[cfg(has_spiflash)] mod imp { use core::str; @@ -36,6 +65,8 @@ mod imp { use cache; use spiflash; use super::Error; + use core::fmt::Write; + use super::FmtWrapper; // One flash sector immediately before the firmware. const ADDR: usize = ::mem::FLASH_BOOT_ADDRESS - spiflash::SECTOR_SIZE; @@ -224,6 +255,13 @@ mod imp { } } + pub fn write_int(key: &str, value: u32) -> Result<(), Error> { + let mut buf = [0; 16]; + let mut wrapper = FmtWrapper::new(&mut buf); + write!(&mut wrapper, "{}", value).unwrap(); + write(key, wrapper.contents()) + } + pub fn remove(key: &str) -> Result<(), Error> { write(key, &[]) }