Skip to content

Commit

Permalink
Formatting Changes
Browse files Browse the repository at this point in the history
- Updated Changlog, added EEPROM section
- Linter / format fixes
  • Loading branch information
amcelroy committed May 19, 2023
1 parent f8db917 commit 8d392be
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 85 deletions.
29 changes: 23 additions & 6 deletions examples/tiva-c-launchpad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch

use core::fmt::Write;
use cortex_m_rt::entry;
use tm4c123x_hal::eeprom::{
Blocks, Eeprom, EepromAddress, EepromError, Erase, Read, Write as EepromWrite,
};
use tm4c123x_hal::{self as hal, prelude::*};
use tm4c123x_hal::eeprom::{Eeprom, Read, Write as EepromWrite, EepromAddress, EepromError, Erase, Blocks};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -56,7 +58,12 @@ fn main() -> ! {
}
}

pub fn eeprom_test_write_read(eeprom: &mut Eeprom, address: &EepromAddress, data_to_write: &[u8], read_buffer: &mut [u8]) -> Result<(), EepromError> {
pub fn eeprom_test_write_read(
eeprom: &mut Eeprom,
address: &EepromAddress,
data_to_write: &[u8],
read_buffer: &mut [u8],
) -> Result<(), EepromError> {
eeprom.write(address, &data_to_write)?;
eeprom.read(address, data_to_write.len(), read_buffer)?;

Expand All @@ -73,11 +80,18 @@ pub fn eeprom_test_all(eeprom: &mut Eeprom) -> Result<(), EepromError> {
// Sanity check for simple mapping from word offset to an EepromAddress
let mut address = eeprom.word_index_to_address(52).unwrap();
assert_eq!(address.block(), 3, "Word 52 should be in block 3, offset 4");
assert_eq!(address.offset(), 4, "Word 52 should be in block 3, offset 4");
assert_eq!(
address.offset(),
4,
"Word 52 should be in block 3, offset 4"
);

// Sanity check for EepromAddress to word offset
let word_index = eeprom.address_to_word_index(&address).unwrap();
assert_eq!(word_index, 52, "Word index for block 3, offset 4 should be 52");
assert_eq!(
word_index, 52,
"Word index for block 3, offset 4 should be 52"
);

// Simplest case, middle of a block, no straddle
let test_array_1: [u8; 4] = [1, 2, 3, 4];
Expand All @@ -104,9 +118,12 @@ pub fn eeprom_test_all(eeprom: &mut Eeprom) -> Result<(), EepromError> {
assert_eq!(buffer[i], 0, "Buffer[0..3] should be all 0's");
}
_ => {
assert_eq!(buffer[i], test_array_2[i], "Buffer[4..9] should match test_array_2")
assert_eq!(
buffer[i], test_array_2[i],
"Buffer[4..9] should match test_array_2"
)
}
}
}
}

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions tm4c-hal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ depending on your processor.

## Changelog

* Basic EEPROM Read, Write, Erase added

### Unreleased Changes ([Source](https://github.com/rust-embedded-community/tm4c-hal/tree/master/tm4c-hal) [Diff](https://github.com/rust-embedded-community/tm4c-hal/compare/tm4c-hal-0.4.1...master))

* Implement use of sealed traits by downstream crates (i.e. `tm4c123x-hal` and `tm4c129x-hal`)
Expand Down
42 changes: 23 additions & 19 deletions tm4c-hal/src/eeprom.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//! Code for the EEProm module.
//!
//!
//! Tested on a TM4C123 Tiva C Series Launchpad
//!
//!
//! Note: This code manually increments the EEBLOCK and EEOFFSET registers
//! after each read and write instead of using the EERDWRINC register. The
//! debugger was giving inconsistent register results for the EEOFFSET register
//! after using EERDWRINC. Also, the EERDWRINC does not increment the block in
//! the case of a wrap of the offset, so it seems less useful for data that
//! spans blocks.
//!
//! after using EERDWRINC. Also, the EERDWRINC does not increment the block in
//! the case of a wrap of the offset, so it seems less useful for data that
//! spans blocks.
//!
//! This flexibility comes at the cost of efficiency, as the
//! datasheet calls for at least 4 cycles of delay after setting the EEBLOCK
//! register.
//! register.
/// Possible errors for the Flash memory module
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -39,8 +39,12 @@ impl core::fmt::Display for EepromError {
EepromError::AddressOutOfBounds => write!(f, "Address is out of bounds"),
EepromError::BlockOutOfBounds => write!(f, "Block is out of bounds"),
EepromError::OffsetOutOfBounds => write!(f, "Offset is out of bounds"),
EepromError::WriteWouldOverflow => write!(f, "Writing this data would overflow the EEPROM"),
EepromError::ReadWouldOverflow => write!(f, "Reading this data would overflow the EEPROM"),
EepromError::WriteWouldOverflow => {
write!(f, "Writing this data would overflow the EEPROM")
}
EepromError::ReadWouldOverflow => {
write!(f, "Reading this data would overflow the EEPROM")
}
EepromError::ReadBufferTooSmall => write!(f, "Allocated buffer too small for reading"),
}
}
Expand Down Expand Up @@ -71,7 +75,7 @@ impl EepromAddress {
self.offset
}

/// Increments the offset by one, if that would cause an overflow, increment the block. If
/// Increments the offset by one, if that would cause an overflow, increment the block. If
/// both the block and offset wrap, the output for the new block and offset
/// will both be 0.
pub fn increment(&mut self, offset_size: usize, block_size: usize) -> &mut Self {
Expand All @@ -91,13 +95,13 @@ impl EepromAddress {
/// Series of traits to make access blocks easier
pub trait Blocks {
/// Returns the blocksize for read / write to the flash
fn block_size(&self) -> Result<usize, EepromError> ;
fn block_size(&self) -> Result<usize, EepromError>;

/// Returns the EepromAddress for a given index. Valid indexes are 0 to
/// EEPROM_END_ADDRESS_WORDS.
fn word_index_to_address(&self, index: usize) -> Result<EepromAddress, EepromError>;

/// Gives the the word index (0 to EEPROM_END_ADDRESS_WORDS) for a
/// Gives the the word index (0 to EEPROM_END_ADDRESS_WORDS) for a
/// given EepromAddress
fn address_to_word_index(&self, block: &EepromAddress) -> Result<usize, EepromError>;
}
Expand All @@ -115,7 +119,7 @@ pub trait Erase {
pub trait Busy {
/// Check the EEDONE register, true if busy
fn is_busy(&self) -> bool;

/// Blocks until the EEPROM is not busy
fn wait(&self);
}
Expand All @@ -129,10 +133,10 @@ pub trait Write {
/// Read data from the EEPROM
pub trait Read {
/// Eeprom Address to start reading data from
fn read(&mut self, address: &EepromAddress, bytes_to_read: usize, buffer: &mut [u8]) -> Result<(), EepromError>;
fn read(
&mut self,
address: &EepromAddress,
bytes_to_read: usize,
buffer: &mut [u8],
) -> Result<(), EepromError>;
}





2 changes: 1 addition & 1 deletion tm4c-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

pub mod bb;
pub mod delay;
pub mod eeprom;
pub mod gpio;
pub mod i2c;
pub mod serial;
pub mod sysctl;
pub mod time;
pub mod eeprom;

///! An internal macro to implement the GPIO functionality for each port
#[macro_export]
Expand Down
Loading

0 comments on commit 8d392be

Please sign in to comment.