1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use embedded_hal::digital::v2::OutputPin;
use crate::{
hal::blocking::spi::{
Transfer,
Write,
},
interface::{
ReadFromRegister,
WriteToRegister,
},
registers::{
ChipId,
ConfigReg,
InterfaceConfig,
ReadReg,
},
BMA400Error,
Config,
BMA400,
};
/// SPI Interface wrapper
// Wrapper class to instantiate BMA400 with an SPI interface
// (extending the Write and WriteRead traits to WriteToRegister and ReadFromRegister)
#[derive(Debug)]
pub struct SPIInterface<SPI, CSBPin> {
spi: SPI,
csb: CSBPin,
}
impl<SPI, CSBPin> SPIInterface<SPI, CSBPin> {
/// Consumes the Interface returning underlying SPI peripheral and the pin
pub fn destroy(self) -> (SPI, CSBPin) {
(self.spi, self.csb)
}
}
impl<SPI, CSBPin, InterfaceError, PinError> WriteToRegister for SPIInterface<SPI, CSBPin>
where
SPI: Write<u8, Error = InterfaceError>,
CSBPin: OutputPin<Error = PinError>,
{
type Error = BMA400Error<InterfaceError, PinError>;
fn write_register<T: ConfigReg>(&mut self, register: T) -> Result<(), Self::Error> {
self.csb.set_low().map_err(BMA400Error::ChipSelectPinError)?;
self.spi.write(&[register.addr(), register.to_byte()]).map_err(BMA400Error::IOError)?;
self.csb.set_high().map_err(BMA400Error::ChipSelectPinError)?;
Ok(())
}
}
impl<SPI, CSBPin, InterfaceError, PinError> ReadFromRegister for SPIInterface<SPI, CSBPin>
where
SPI: Transfer<u8, Error = InterfaceError>,
CSBPin: OutputPin<Error = PinError>,
{
type Error = BMA400Error<InterfaceError, PinError>;
fn read_register<T: ReadReg>(&mut self, register: T, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.csb.set_low().map_err(BMA400Error::ChipSelectPinError)?;
self.spi.transfer(&mut [register.addr() | 1 << 7, 0]).map_err(BMA400Error::IOError)?;
self.spi.transfer(buffer).map_err(BMA400Error::IOError)?;
self.csb.set_high().map_err(BMA400Error::ChipSelectPinError)?;
Ok(())
}
}
impl<SPI, CSBPin, InterfaceError, PinError> BMA400<SPIInterface<SPI, CSBPin>>
where
SPI: Transfer<u8, Error = InterfaceError> + Write<u8, Error = InterfaceError>,
CSBPin: OutputPin<Error = PinError>,
{
/// Create a new instance of the BMA400 using 4-wire SPI
///
/// # Examples
/// ```
/// # use embedded_hal_mock::{
/// # spi::{Mock, Transaction},
/// # pin::{Mock as MockPin, Transaction as PinTransaction, State},
/// # };
/// use bma400::BMA400;
/// # let expected_io = vec![
/// # Transaction::transfer(vec![0x80, 0x00], vec![0x00,0x00]),
/// # Transaction::transfer(vec![0x00], vec![0x00]),
/// # Transaction::transfer(vec![0x80, 0x00], vec![0x00, 0x00]),
/// # Transaction::transfer(vec![0x00], vec![0x90]),
/// # ];
/// # let expected_pin = vec![
/// # PinTransaction::set(State::Low),
/// # PinTransaction::set(State::High),
/// # PinTransaction::set(State::Low),
/// # PinTransaction::set(State::High),
/// # ];
/// # let spi = Mock::new(&expected_io);
/// # let csb_pin = MockPin::new(&expected_pin);
/// // spi implements embedded-hal spi::Transfer and spi::Write
/// // csb_pin implements embedded-hal digital::v2::OutputPin
/// let mut accelerometer = BMA400::new_spi(spi, csb_pin);
/// assert!(accelerometer.is_ok());
/// ```
pub fn new_spi(spi: SPI, csb: CSBPin) -> Result<BMA400<SPIInterface<SPI, CSBPin>>, BMA400Error<InterfaceError, PinError>> {
let mut interface = SPIInterface { spi, csb };
let config = Config::default();
// Initialize SPI Mode by doing a dummy read
interface.read_register(ChipId, &mut [0u8; 1])?;
// Validate Chip ID
let mut chip_id = [0u8; 1];
interface.read_register(ChipId, &mut chip_id)?;
if chip_id[0] != 0x90 {
Err(BMA400Error::ChipIdReadFailed)
} else {
Ok(BMA400 { interface, config })
}
}
/// Create a new instance of the BMA400 using 3-wire SPI
///
/// # Examples
/// ```
/// # use embedded_hal_mock::{
/// # spi::{Mock, Transaction},
/// # pin::{Mock as MockPin, Transaction as PinTransaction, State},
/// # };
/// use bma400::BMA400;
/// # let expected_io = vec![
/// # Transaction::transfer(vec![0x80, 0x00], vec![0x00,0x00]),
/// # Transaction::transfer(vec![0x00], vec![0x00]),
/// # Transaction::transfer(vec![0x80, 0x00], vec![0x00, 0x00]),
/// # Transaction::transfer(vec![0x00], vec![0x90]),
/// # Transaction::write(vec![0x7C, 0x01]),
/// # ];
/// # let expected_pin = vec![
/// # PinTransaction::set(State::Low),
/// # PinTransaction::set(State::High),
/// # PinTransaction::set(State::Low),
/// # PinTransaction::set(State::High),
/// # PinTransaction::set(State::Low),
/// # PinTransaction::set(State::High),
/// # ];
/// # let spi = Mock::new(&expected_io);
/// # let csb_pin = MockPin::new(&expected_pin);
/// // spi implements embedded-hal spi::Transfer and spi::Write
/// // csb_pin implements embedded-hal digital::v2::OutputPin
/// let mut accelerometer = BMA400::new_spi_3wire(spi, csb_pin);
/// assert!(accelerometer.is_ok());
/// ```
pub fn new_spi_3wire(spi: SPI, csb: CSBPin) -> Result<BMA400<SPIInterface<SPI, CSBPin>>, BMA400Error<InterfaceError, PinError>> {
let mut interface = SPIInterface { spi, csb };
let config = Config::default();
// Initialize SPI Mode by doing a dummy read
interface.read_register(ChipId, &mut [0u8; 1])?;
let mut chip_id = [0u8; 1];
interface.read_register(ChipId, &mut chip_id)?;
let if_config = InterfaceConfig::default().with_spi_3wire_mode(true);
interface.write_register(if_config)?;
if chip_id[0] != 0x90 {
Err(BMA400Error::ChipIdReadFailed)
} else {
Ok(BMA400 { interface, config })
}
}
}