aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjrmoulton <[email protected]>2024-06-18 10:43:34 -0600
committerjrmoulton <[email protected]>2024-08-13 09:46:16 -0600
commit70dfbc03b0399ebeb57ba9b8ee177154325db374 (patch)
treed0dd9a52e883ed1a3c0825353eeb24d69ab9358f
parent65c7457c01317a803817cfdfac7b66ab23adf710 (diff)
Add more docs
-rw-r--r--embassy-stm32/src/i2c/config.rs43
-rw-r--r--embassy-stm32/src/i2c/mod.rs4
-rw-r--r--embassy-stm32/src/i2c/v2.rs2
3 files changed, 37 insertions, 12 deletions
diff --git a/embassy-stm32/src/i2c/config.rs b/embassy-stm32/src/i2c/config.rs
index eba14f6e0..dac7847e8 100644
--- a/embassy-stm32/src/i2c/config.rs
+++ b/embassy-stm32/src/i2c/config.rs
@@ -5,14 +5,23 @@ use crate::gpio::Pull;
5#[repr(u8)] 5#[repr(u8)]
6#[derive(Copy, Clone)] 6#[derive(Copy, Clone)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))] 7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8/// Bits of the I2C OA2 register to mask out.
8pub enum AddrMask { 9pub enum AddrMask {
10 /// No mask
9 NOMASK, 11 NOMASK,
12 /// OA2\[1\] is masked and don’t care. Only OA2\[7:2\] are compared.
10 MASK1, 13 MASK1,
14 /// OA2\[2:1\] are masked and don’t care. Only OA2\[7:3\] are compared.
11 MASK2, 15 MASK2,
16 /// OA2\[3:1\] are masked and don’t care. Only OA2\[7:4\] are compared.
12 MASK3, 17 MASK3,
18 /// OA2\[4:1\] are masked and don’t care. Only OA2\[7:5\] are compared.
13 MASK4, 19 MASK4,
20 /// OA2\[5:1\] are masked and don’t care. Only OA2\[7:6\] are compared.
14 MASK5, 21 MASK5,
22 /// OA2\[6:1\] are masked and don’t care. Only OA2\[7:6\] are compared.
15 MASK6, 23 MASK6,
24 /// OA2\[7:1\] are masked and don’t care. No comparison is done, and all (except reserved) 7-bit received addresses are acknowledged
16 MASK7, 25 MASK7,
17} 26}
18impl From<AddrMask> for Oamsk { 27impl From<AddrMask> for Oamsk {
@@ -32,8 +41,13 @@ impl From<AddrMask> for Oamsk {
32 41
33#[derive(Debug, Copy, Clone, PartialEq, Eq)] 42#[derive(Debug, Copy, Clone, PartialEq, Eq)]
34#[cfg_attr(feature = "defmt", derive(defmt::Format))] 43#[cfg_attr(feature = "defmt", derive(defmt::Format))]
44/// An I2C address. Either 7 or 10 bit.
35pub enum Address { 45pub enum Address {
46 /// A 7 bit address
36 SevenBit(u8), 47 SevenBit(u8),
48 /// A 10 bit address.
49 ///
50 /// When using an address to configure the Own Address, only the OA1 register can be set to a 10-bit address.
37 TenBit(u16), 51 TenBit(u16),
38} 52}
39impl From<u8> for Address { 53impl From<u8> for Address {
@@ -54,6 +68,9 @@ impl Address {
54 Address::TenBit(_) => stm32_metapac::i2c::vals::Addmode::BIT10, 68 Address::TenBit(_) => stm32_metapac::i2c::vals::Addmode::BIT10,
55 } 69 }
56 } 70 }
71 /// Get the inner address as a u16.
72 ///
73 /// For 7 bit addresses, the u8 that was used to store the address is returned as a u16.
57 pub fn addr(&self) -> u16 { 74 pub fn addr(&self) -> u16 {
58 match self { 75 match self {
59 Address::SevenBit(addr) => *addr as u16, 76 Address::SevenBit(addr) => *addr as u16,
@@ -64,17 +81,29 @@ impl Address {
64 81
65#[derive(Copy, Clone)] 82#[derive(Copy, Clone)]
66#[cfg_attr(feature = "defmt", derive(defmt::Format))] 83#[cfg_attr(feature = "defmt", derive(defmt::Format))]
84/// The second Own Address register.
67pub struct OA2 { 85pub struct OA2 {
86 /// The address.
68 pub addr: u8, 87 pub addr: u8,
88 /// The bit mask that will affect how the own address 2 register is compared.
69 pub mask: AddrMask, 89 pub mask: AddrMask,
70} 90}
71 91
72#[derive(Copy, Clone)] 92#[derive(Copy, Clone)]
73#[cfg_attr(feature = "defmt", derive(defmt::Format))] 93#[cfg_attr(feature = "defmt", derive(defmt::Format))]
94/// The Own Address(es) of the I2C peripheral.
74pub enum OwnAddresses { 95pub enum OwnAddresses {
96 /// Configuration for only the OA1 register.
75 OA1(Address), 97 OA1(Address),
98 /// Configuration for only the OA2 register.
76 OA2(OA2), 99 OA2(OA2),
77 Both { oa1: Address, oa2: OA2 }, 100 /// Configuration for both the OA1 and OA2 registers.
101 Both {
102 /// The [Address] for the OA1 register.
103 oa1: Address,
104 /// The [OA2] configuration.
105 oa2: OA2,
106 },
78} 107}
79 108
80/// Slave Configuration 109/// Slave Configuration
@@ -87,16 +116,10 @@ pub struct SlaveAddrConfig {
87 pub general_call: bool, 116 pub general_call: bool,
88} 117}
89impl SlaveAddrConfig { 118impl SlaveAddrConfig {
90 pub fn new_oa1(addr: Address, general_call: bool) -> Self { 119 /// Create a new slave address configuration with only the OA1 register set in 7 bit mode and the general call disabled.
91 Self { 120 pub fn basic(addr: u8) -> Self {
92 addr: OwnAddresses::OA1(addr),
93 general_call,
94 }
95 }
96
97 pub fn basic(addr: Address) -> Self {
98 Self { 121 Self {
99 addr: OwnAddresses::OA1(addr), 122 addr: OwnAddresses::OA1(Address::SevenBit(addr)),
100 general_call: false, 123 general_call: false,
101 } 124 }
102 } 125 }
diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs
index a5ff61f3d..1464a6851 100644
--- a/embassy-stm32/src/i2c/mod.rs
+++ b/embassy-stm32/src/i2c/mod.rs
@@ -16,7 +16,7 @@ use embassy_hal_internal::{Peripheral, PeripheralRef};
16use embassy_sync::waitqueue::AtomicWaker; 16use embassy_sync::waitqueue::AtomicWaker;
17#[cfg(feature = "time")] 17#[cfg(feature = "time")]
18use embassy_time::{Duration, Instant}; 18use embassy_time::{Duration, Instant};
19use mode::{Master, MasterMode, MultiMaster}; 19use mode::{Master, MasterMode};
20 20
21use crate::dma::ChannelAndRequest; 21use crate::dma::ChannelAndRequest;
22#[cfg(gpio_v2)] 22#[cfg(gpio_v2)]
@@ -146,7 +146,9 @@ pub enum CommandKind {
146#[cfg_attr(feature = "defmt", derive(defmt::Format))] 146#[cfg_attr(feature = "defmt", derive(defmt::Format))]
147/// The command kind to the slave from the master and the address that the slave matched 147/// The command kind to the slave from the master and the address that the slave matched
148pub struct Command { 148pub struct Command {
149 /// The kind of command
149 pub kind: CommandKind, 150 pub kind: CommandKind,
151 /// The address that the slave matched
150 pub address: Address, 152 pub address: Address,
151} 153}
152 154
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs
index acf4b3f12..86efd0da2 100644
--- a/embassy-stm32/src/i2c/v2.rs
+++ b/embassy-stm32/src/i2c/v2.rs
@@ -6,7 +6,7 @@ use config::{Address, OwnAddresses, OA2};
6use embassy_embedded_hal::SetConfig; 6use embassy_embedded_hal::SetConfig;
7use embassy_hal_internal::drop::OnDrop; 7use embassy_hal_internal::drop::OnDrop;
8use embedded_hal_1::i2c::Operation; 8use embedded_hal_1::i2c::Operation;
9use mode::Master; 9use mode::{Master, MultiMaster};
10use stm32_metapac::i2c::vals::Addmode; 10use stm32_metapac::i2c::vals::Addmode;
11 11
12use super::*; 12use super::*;