Skip to content

Commit

Permalink
generic: spi: Assign discriminant values to clock rate enum
Browse files Browse the repository at this point in the history
To simplify some clock calculation code in the future, assign the
divider power-of-two as the discriminant values in the `SerialClockRate`
enum.

Additionally, provide an `into_divider()` method to get the numeric
clock divider value for a given clock rate.
  • Loading branch information
Rahix committed Jul 25, 2024
1 parent afdadeb commit 65b304e
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions avr-hal-generic/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ use embedded_hal::spi::{self, SpiBus};
/// | `OscfOver128` | 125 kHz | 62.5 kHz |
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SerialClockRate {
OscfOver2,
OscfOver4,
OscfOver8,
OscfOver16,
OscfOver32,
OscfOver64,
OscfOver128,
OscfOver2 = 1,
OscfOver4 = 2,
OscfOver8 = 3,
OscfOver16 = 4,
OscfOver32 = 5,
OscfOver64 = 6,
OscfOver128 = 7,
}

impl SerialClockRate {
pub fn into_divider(self) -> u8 {
2u8.pow(self as u32)
}
}

/// Order of data transmission, either MSB first or LSB first
Expand Down

0 comments on commit 65b304e

Please sign in to comment.