Skip to content

Commit

Permalink
Remove Sequence API in favor of SequenceBuilder
Browse files Browse the repository at this point in the history
The commit marks Sequence members as private to the crate. The commit
updates all tests and documentation to use SequenceBuilder.
  • Loading branch information
mciantyre committed Nov 15, 2020
1 parent 3d07668 commit 35dddb9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 194 deletions.
123 changes: 19 additions & 104 deletions src/flexspi_lut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,13 @@ pub(crate) const INSTRUCTIONS_PER_SEQUENCE: usize = 8;

/// A collection of FlexSPI LUT instructions
///
/// Each `Sequence` may have up to eight instructions. Any unused instructions must
/// be inlined to [`STOP`](constant.STOP.html). The sequences you'll require are dependent
/// on the specific flash memory that you're interacting with.
/// Each `Sequence` may have up to eight instructions. Use [`SequenceBuilder`] to create
/// a `Sequence`. The sequences you'll require are dependent on the specific flash memory that
/// you're interacting with.
///
/// `Sequence`s are used to create a [`LookupTable`](../serial_flash/lookup/struct.LookupTable.html).
///
/// # Example
///
/// ```
/// use imxrt_boot_gen::serial_flash::{
/// Sequence,
/// Instr,
/// STOP,
/// Pads,
/// opcodes::sdr::*,
/// };
///
/// const SEQ_READ: Sequence = Sequence([
/// Instr::new(CMD, Pads::One, 0xEB),
/// Instr::new(READ, Pads::Four, 0x04),
/// STOP,
/// STOP,
/// STOP,
/// STOP,
/// STOP,
/// STOP,
/// ]);
/// ```
#[derive(Clone, Copy)]
pub struct Sequence(pub [Instr; INSTRUCTIONS_PER_SEQUENCE]);
pub struct Sequence(pub(crate) [Instr; INSTRUCTIONS_PER_SEQUENCE]);
pub(crate) const SEQUENCE_SIZE: usize = INSTRUCTIONS_PER_SEQUENCE * INSTRUCTION_SIZE;

impl Sequence {
Expand Down Expand Up @@ -371,7 +348,6 @@ mod test {
use super::opcodes::sdr::*;
use super::Instr;
use super::Pads;
use super::STOP;
use super::{Sequence, SequenceBuilder};

fn seq_to_bytes(seq: Sequence) -> Vec<u8> {
Expand All @@ -395,26 +371,6 @@ mod test {
0xEB, 0x04, 0x18, 0x0A, 0x06, 0x32, 0x04, 0x26, 0, 0, 0, 0, 0, 0, 0, 0,
];

const SEQUENCE: Sequence = Sequence([
Instr::new(CMD, Pads::One, 0xEB),
Instr::new(RADDR, Pads::Four, 0x18),
Instr::new(DUMMY, Pads::Four, 0x06),
Instr::new(READ, Pads::Four, 0x04),
STOP,
STOP,
STOP,
STOP,
]);

assert_eq!(&seq_to_bytes(SEQUENCE), &EXPECTED);
}

#[test]
fn teensy4_read_builder() {
const EXPECTED: [u8; super::SEQUENCE_SIZE] = [
0xEB, 0x04, 0x18, 0x0A, 0x06, 0x32, 0x04, 0x26, 0, 0, 0, 0, 0, 0, 0, 0,
];

const SEQUENCE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, 0xEB))
.instr(Instr::new(RADDR, Pads::Four, 0x18))
Expand All @@ -427,22 +383,6 @@ mod test {

#[test]
fn teensy4_read_status() {
const EXPECTED: [u8; 4] = [0x05, 0x04, 0x04, 0x24];
const SEQUENCE: Sequence = Sequence([
Instr::new(CMD, Pads::One, 0x05),
Instr::new(READ, Pads::One, 0x04),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
assert_eq!(&seq_to_bytes(SEQUENCE)[0..4], &EXPECTED);
}

#[test]
fn teensy4_read_status_builder() {
const EXPECTED: [u8; 4] = [0x05, 0x04, 0x04, 0x24];
const SEQUENCE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, 0x05))
Expand All @@ -454,64 +394,39 @@ mod test {
#[test]
fn teensy4_write_enable() {
const EXPECTED: u128 = 0x0000_0406;
const SEQUENCE: Sequence = Sequence([
Instr::new(CMD, Pads::One, 0x06),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQUENCE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, 0x06))
.build();
assert_eq!(&EXPECTED.to_le_bytes(), &seq_to_bytes(SEQUENCE)[..]);
}

#[test]
fn teensy4_erase_sector() {
const EXPECTED: u128 = 0x0818_0420;
const SEQUENCE: Sequence = Sequence([
Instr::new(CMD, Pads::One, 0x20),
Instr::new(RADDR, Pads::One, 0x18),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQUENCE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, 0x20))
.instr(Instr::new(RADDR, Pads::One, 0x18))
.build();
assert_eq!(&EXPECTED.to_le_bytes(), &seq_to_bytes(SEQUENCE)[..]);
}

#[test]
fn teensy4_page_program() {
const EXPECTED: u128 = 0x0000_2004_0818_0402;
const SEQUENCE: Sequence = Sequence([
Instr::new(CMD, Pads::One, 0x02),
Instr::new(RADDR, Pads::One, 0x18),
Instr::new(WRITE, Pads::One, 0x04),
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQUENCE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, 0x02))
.instr(Instr::new(RADDR, Pads::One, 0x18))
.instr(Instr::new(WRITE, Pads::One, 0x04))
.build();
assert_eq!(&EXPECTED.to_le_bytes(), &seq_to_bytes(SEQUENCE)[..]);
}

#[test]
fn teensy4_chip_erase() {
const EXPECTED: u128 = 0x0000_0460;
const SEQUENCE: Sequence = Sequence([
Instr::new(CMD, Pads::One, 0x60),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQUENCE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, 0x60))
.build();
assert_eq!(&EXPECTED.to_le_bytes(), &seq_to_bytes(SEQUENCE)[..]);
}
}
Expand Down
30 changes: 10 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,18 @@
//! };
//!
//! // READ sequence
//! const SEQ_READ: Sequence = Sequence([
//! Instr::new(CMD, Pads::One, 0xEB),
//! Instr::new(RADDR, Pads::Four, 0x18),
//! Instr::new(DUMMY, Pads::Four, 0x06),
//! Instr::new(READ, Pads::Four, 0x04),
//! STOP,
//! STOP,
//! STOP,
//! STOP,
//! ]);
//! const SEQ_READ: Sequence = SequenceBuilder::new()
//! .instr(Instr::new(CMD, Pads::One, 0xEB))
//! .instr(Instr::new(RADDR, Pads::Four, 0x18))
//! .instr(Instr::new(DUMMY, Pads::Four, 0x06))
//! .instr(Instr::new(READ, Pads::Four, 0x04))
//! .build();
//!
//! // ERASE SECTOR sequence
//! const SEQ_ERASE_SECTOR: Sequence = Sequence([
//! Instr::new(CMD, Pads::One, 0x20),
//! Instr::new(RADDR, Pads::One, 0x18),
//! STOP,
//! STOP,
//! STOP,
//! STOP,
//! STOP,
//! STOP,
//! ]);
//! const SEQ_ERASE_SECTOR: Sequence = SequenceBuilder::new()
//! .instr(Instr::new(CMD, Pads::One, 0x20))
//! .instr(Instr::new(RADDR, Pads::One, 0x18))
//! .build();
//! // Other sequences...
//!
//! // Add the sequences in the lookup table
Expand Down
15 changes: 5 additions & 10 deletions src/serial_flash/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,18 @@ const NUMBER_OF_SEQUENCES: usize = LOOKUP_TABLE_SIZE_BYTES / SEQUENCE_SIZE;
/// use imxrt_boot_gen::serial_flash::{
/// LookupTable,
/// CommandSequence,
/// SequenceBuilder,
/// Sequence, Instr,
/// opcodes::sdr::*,
/// Pads,
/// STOP,
/// };
///
/// let mut lookup_table = LookupTable::new();
/// lookup_table[CommandSequence::Read] = Sequence([
/// Instr::new(CMD, Pads::One, 0xEB),
/// Instr::new(RADDR, Pads::Four, 0x02),
/// STOP,
/// STOP,
/// STOP,
/// STOP,
/// STOP,
/// STOP,
/// ]);
/// lookup_table[CommandSequence::Read] = SequenceBuilder::new()
/// .instr(Instr::new(CMD, Pads::One, 0xEB))
/// .instr(Instr::new(RADDR, Pads::Four, 0x02))
/// .build();
/// ```
pub struct LookupTable([Sequence; NUMBER_OF_SEQUENCES]);

Expand Down
85 changes: 25 additions & 60 deletions tests/teensy4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,71 +20,36 @@ use winbond::*;
// Sequences for lookup table
//

const SEQ_READ: Sequence = Sequence([
Instr::new(CMD, Pads::One, FAST_READ_QUAD_IO),
Instr::new(RADDR, Pads::Four, 0x18),
Instr::new(DUMMY, Pads::Four, 0x06),
Instr::new(READ, Pads::Four, 0x04),
STOP,
STOP,
STOP,
STOP,
]);
const SEQ_READ: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, FAST_READ_QUAD_IO))
.instr(Instr::new(RADDR, Pads::Four, 0x18))
.instr(Instr::new(DUMMY, Pads::Four, 0x06))
.instr(Instr::new(READ, Pads::Four, 0x04))
.build();

const SEQ_READ_STATUS: Sequence = Sequence([
Instr::new(CMD, Pads::One, READ_STATUS_REGISTER_1),
Instr::new(READ, Pads::One, 0x04),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQ_READ_STATUS: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, READ_STATUS_REGISTER_1))
.instr(Instr::new(READ, Pads::One, 0x04))
.build();

const SEQ_WRITE_ENABLE: Sequence = Sequence([
Instr::new(CMD, Pads::One, WRITE_ENABLE),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQ_WRITE_ENABLE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, WRITE_ENABLE))
.build();

const SEQ_ERASE_SECTOR: Sequence = Sequence([
Instr::new(CMD, Pads::One, SECTOR_ERASE),
Instr::new(RADDR, Pads::One, 0x18),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQ_ERASE_SECTOR: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, SECTOR_ERASE))
.instr(Instr::new(RADDR, Pads::One, 0x18))
.build();

const SEQ_PAGE_PROGRAM: Sequence = Sequence([
Instr::new(CMD, Pads::One, PAGE_PROGRAM),
Instr::new(RADDR, Pads::One, 0x18),
Instr::new(WRITE, Pads::One, 0x04),
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQ_PAGE_PROGRAM: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, PAGE_PROGRAM))
.instr(Instr::new(RADDR, Pads::One, 0x18))
.instr(Instr::new(WRITE, Pads::One, 0x04))
.build();

const SEQ_CHIP_ERASE: Sequence = Sequence([
Instr::new(CMD, Pads::One, CHIP_ERASE),
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
STOP,
]);
const SEQ_CHIP_ERASE: Sequence = SequenceBuilder::new()
.instr(Instr::new(CMD, Pads::One, CHIP_ERASE))
.build();

#[test]
fn teensy4_fcb() {
Expand Down

0 comments on commit 35dddb9

Please sign in to comment.