1use core::{
4 convert::{TryFrom, TryInto as _},
5 fmt,
6 marker::PhantomData,
7};
8
9use embedded_hal::blocking::i2c;
10
11use crate::{
12 dma::{self, transfer::state::Ready},
13 init_state::Enabled,
14 pac::{
15 dma0::channel::xfercfg::{DSTINC_A, SRCINC_A},
16 i2c0::{stat::MSTSTATE_A, MSTCTL, MSTDAT},
17 },
18 reg_proxy::{Reg, RegProxy},
19};
20
21use super::{Error, Instance};
22
23pub struct Master<I: Instance, State, ModeState> {
39 _state: PhantomData<State>,
40 _mode_state: PhantomData<ModeState>,
41
42 mstctl: RegProxy<MstCtl<I>>,
43 mstdat: RegProxy<MstDat<I>>,
44}
45
46impl<I, State, ModeState> Master<I, State, ModeState>
47where
48 I: Instance,
49{
50 pub(super) fn new() -> Self {
51 Self {
52 _state: PhantomData,
53 _mode_state: PhantomData,
54
55 mstctl: RegProxy::new(),
56 mstdat: RegProxy::new(),
57 }
58 }
59}
60
61impl<I, C> Master<I, Enabled<PhantomData<C>>, Enabled>
62where
63 I: Instance,
64{
65 pub fn write_all(
71 mut self,
72 address: u8,
73 buffer: &'static [u8],
74 channel: dma::Channel<I::MstChannel, Enabled>,
75 ) -> Result<dma::Transfer<Ready, I::MstChannel, &'static [u8], Self>, Error>
76 {
77 self.start_operation(address, Rw::Write)?;
78 self.wait_for_state(State::TxReady)?;
79 self.mstctl.modify(|_, w| w.mstdma().enabled());
80 Ok(dma::Transfer::new(channel, buffer, self))
81 }
82
83 pub fn read_all(
89 mut self,
90 address: u8,
91 buffer: &'static mut [u8],
92 channel: dma::Channel<I::MstChannel, Enabled>,
93 ) -> Result<
94 dma::Transfer<Ready, I::MstChannel, Self, &'static mut [u8]>,
95 Error,
96 > {
97 self.start_operation(address, Rw::Read)?;
98 self.mstctl.modify(|_, w| w.mstdma().enabled());
99 Ok(dma::Transfer::new(channel, self, buffer))
100 }
101
102 fn wait_for_state(&self, expected: State) -> Result<(), Error> {
107 let i2c = unsafe { &*I::REGISTERS };
109
110 while i2c.stat.read().mstpending().is_in_progress() {
111 Error::read::<I>()?;
112 }
113
114 let mststate = i2c.stat.read().mststate();
115 let actual =
116 mststate.variant().try_into().map_err(|()| mststate.bits());
117 if Ok(&expected) != actual.as_ref() {
118 return Err(Error::UnexpectedState { expected, actual });
119 }
120
121 Ok(())
122 }
123
124 fn start_operation(&mut self, address: u8, rw: Rw) -> Result<(), Error> {
125 Error::check_address(address)?;
126 self.wait_for_state(State::Idle)?;
127
128 let address_rw = (address << 1) | rw as u8;
130 self.mstdat.write(|w| unsafe {
131 w.data().bits(address_rw)
133 });
134
135 self.mstctl.write(|w| w.mststart().start());
137
138 Ok(())
139 }
140
141 fn finish_write(&mut self) -> Result<(), Error> {
142 self.wait_for_state(State::TxReady)?;
143
144 self.mstctl.write(|w| w.mststop().stop());
146
147 Ok(())
148 }
149
150 fn finish_read(&mut self) -> Result<(), Error> {
151 self.wait_for_state(State::RxReady)?;
152
153 self.mstctl.write(|w| w.mststop().stop());
155
156 Ok(())
157 }
158}
159
160impl<I, C> i2c::Write for Master<I, Enabled<PhantomData<C>>, Enabled>
161where
162 I: Instance,
163{
164 type Error = Error;
165
166 fn write(&mut self, address: u8, data: &[u8]) -> Result<(), Self::Error> {
172 self.start_operation(address, Rw::Write)?;
173
174 for &b in data {
175 self.wait_for_state(State::TxReady)?;
176
177 self.mstdat.write(|w| unsafe { w.data().bits(b) });
179
180 self.mstctl.write(|w| w.mstcontinue().continue_());
182 }
183
184 self.finish_write()?;
185
186 Ok(())
187 }
188}
189
190impl<I, C> i2c::Read for Master<I, Enabled<PhantomData<C>>, Enabled>
191where
192 I: Instance,
193{
194 type Error = Error;
195
196 fn read(
202 &mut self,
203 address: u8,
204 buffer: &mut [u8],
205 ) -> Result<(), Self::Error> {
206 self.start_operation(address, Rw::Read)?;
207
208 for (i, b) in buffer.iter_mut().enumerate() {
209 if i != 0 {
210 self.mstctl.write(|w| w.mstcontinue().continue_());
212 }
213
214 self.wait_for_state(State::RxReady)?;
215
216 *b = self.mstdat.read().data().bits();
218 }
219
220 self.finish_read()?;
221
222 Ok(())
223 }
224}
225
226impl<I, State, ModeState> crate::private::Sealed for Master<I, State, ModeState> where
227 I: Instance
228{
229}
230
231impl<I, C> dma::Dest for Master<I, Enabled<PhantomData<C>>, Enabled>
232where
233 I: Instance,
234{
235 type Error = Error;
236
237 fn is_valid(&self) -> bool {
238 true
239 }
240
241 fn is_full(&self) -> bool {
242 false
243 }
244
245 fn increment(&self) -> DSTINC_A {
246 DSTINC_A::NO_INCREMENT
247 }
248
249 fn transfer_count(&self) -> Option<u16> {
250 None
251 }
252
253 fn end_addr(&mut self) -> *mut u8 {
254 (unsafe { &(*I::REGISTERS).mstdat }) as *const _ as *mut u8
257 }
258
259 fn finish(&mut self) -> nb::Result<(), Self::Error> {
260 self.mstctl.modify(|_, w| w.mstdma().disabled());
261 self.finish_write()?;
262 Ok(())
263 }
264}
265
266impl<I, C> dma::Source for Master<I, Enabled<PhantomData<C>>, Enabled>
267where
268 I: Instance,
269{
270 type Error = Error;
271
272 fn is_valid(&self) -> bool {
273 true
274 }
275
276 fn is_empty(&self) -> bool {
277 false
278 }
279
280 fn increment(&self) -> SRCINC_A {
281 SRCINC_A::NO_INCREMENT
282 }
283
284 fn transfer_count(&self) -> Option<u16> {
285 None
286 }
287
288 fn end_addr(&self) -> *const u8 {
289 (unsafe { &(*I::REGISTERS).mstdat }) as *const _ as *mut u8
292 }
293
294 fn finish(&mut self) -> nb::Result<(), Self::Error> {
295 self.mstctl.modify(|_, w| w.mstdma().disabled());
296 self.finish_read()?;
297 Ok(())
298 }
299}
300
301impl<I, State, ModeState> fmt::Debug for Master<I, State, ModeState>
304where
305 I: Instance,
306{
307 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
308 f.debug_struct("Master")
309 .field("_state", &self._state)
310 .field("_mode_state", &self._mode_state)
311 .field("mstctl", &self.mstctl)
312 .field("mstdat", &self.mstdat)
313 .finish()
314 }
315}
316
317#[repr(u8)]
319enum Rw {
320 Write = 0,
321 Read = 1,
322}
323
324#[derive(Debug, Eq, PartialEq)]
326pub enum State {
327 Idle,
331
332 RxReady,
337
338 TxReady,
343
344 NackAddress,
346
347 NackData,
349}
350
351impl TryFrom<Option<MSTSTATE_A>> for State {
352 type Error = ();
353
354 fn try_from(state: Option<MSTSTATE_A>) -> Result<Self, Self::Error> {
355 match state {
356 Some(MSTSTATE_A::IDLE) => Ok(Self::Idle),
357 Some(MSTSTATE_A::RECEIVE_READY) => Ok(Self::RxReady),
358 Some(MSTSTATE_A::TRANSMIT_READY) => Ok(Self::TxReady),
359 Some(MSTSTATE_A::NACK_ADDRESS) => Ok(Self::NackAddress),
360 Some(MSTSTATE_A::NACK_DATA) => Ok(Self::NackData),
361 None => Err(()),
362 }
363 }
364}
365
366struct MstCtl<I>(PhantomData<I>);
367
368unsafe impl<I> Reg for MstCtl<I>
370where
371 I: Instance,
372{
373 type Target = MSTCTL;
374
375 fn get() -> *const Self::Target {
376 unsafe { &(*I::REGISTERS).mstctl as *const _ }
379 }
380}
381
382impl<I> fmt::Debug for MstCtl<I> {
385 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
386 write!(f, "MstCtl(...)")
387 }
388}
389
390struct MstDat<I>(PhantomData<I>);
391
392unsafe impl<I> Reg for MstDat<I>
394where
395 I: Instance,
396{
397 type Target = MSTDAT;
398
399 fn get() -> *const Self::Target {
400 unsafe { &(*I::REGISTERS).mstdat as *const _ }
403 }
404}
405
406impl<I> fmt::Debug for MstDat<I> {
409 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
410 write!(f, "MstDat(...)")
411 }
412}