Skip to content

Commit 763824d

Browse files
committed
Refactored read/write_all methods to use the more general read/write_some case.
1 parent 869de97 commit 763824d

File tree

2 files changed

+11
-107
lines changed

2 files changed

+11
-107
lines changed

src/i2c.rs

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use as_slice::{
1818
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
1919

2020
#[cfg(feature = "stm32l0x2")]
21-
use crate::dma;
22-
#[cfg(feature = "stm32l0x2")]
23-
use crate::dma::Buffer;
21+
use crate::dma::{
22+
self,
23+
Buffer
24+
};
2425
use crate::gpio::gpioa::{PA10, PA9};
2526
use crate::gpio::gpiob::{PB6, PB7};
2627
use crate::gpio::{AltMode, OpenDrain, Output};
@@ -209,7 +210,7 @@ where
209210
}
210211

211212
#[cfg(feature = "stm32l0x2")]
212-
pub fn write_all<Channel, Buffer>(mut self,
213+
pub fn write_all<Channel, Buffer>(self,
213214
dma: &mut dma::Handle,
214215
channel: Channel,
215216
address: u8,
@@ -222,44 +223,8 @@ where
222223
Buffer: Deref + 'static,
223224
Buffer::Target: AsSlice<Element=u8>,
224225
{
225-
self.start_transfer(address, buffer.as_slice().len(), RD_WRNW::WRITE);
226-
227-
// This token represents the transmission capability of I2C and this is
228-
// what the `dma::Target` trait is implemented for. It can't be
229-
// implemented for `I2c` itself, as that would allow for the user to
230-
// pass, for example, a channel that can do I2C RX to `write_all`.
231-
//
232-
// Theoretically, one could create both `Rx` and `Tx` at the same time,
233-
// or create multiple tokens of the same type, and use that to create
234-
// multiple simultaneous DMA transfers, which would be wrong and is not
235-
// supported by the I2C peripheral. We prevent that by only ever
236-
// creating an `Rx` or `Tx` token while we have ownership of `I2c`, and
237-
// dropping the token before returning ownership of `I2c` ot the user.
238-
let token = Tx(PhantomData);
239-
240-
// Safe, because we're only taking the address of a register.
241-
let address = &unsafe { &*I::ptr() }.txdr as *const _ as u32;
242-
243226
let num_words = buffer.len();
244-
// Safe, because the trait bounds of this method guarantee that the
245-
// buffer can be read from.
246-
let transfer = unsafe {
247-
dma::Transfer::new(
248-
dma,
249-
token,
250-
channel,
251-
buffer,
252-
num_words,
253-
address,
254-
dma::Priority::high(),
255-
dma::Direction::memory_to_peripheral(),
256-
)
257-
};
258-
259-
Transfer {
260-
target: self,
261-
inner: transfer,
262-
}
227+
self.write_some(dma, channel, address, buffer, num_words)
263228
}
264229

265230
#[cfg(feature = "stm32l0x2")]
@@ -318,7 +283,7 @@ where
318283
}
319284

320285
#[cfg(feature = "stm32l0x2")]
321-
pub fn read_all<Channel, Buffer>(mut self,
286+
pub fn read_all<Channel, Buffer>(self,
322287
dma: &mut dma::Handle,
323288
channel: Channel,
324289
address: u8,
@@ -331,34 +296,8 @@ where
331296
Buffer: DerefMut + 'static,
332297
Buffer::Target: AsMutSlice<Element=u8>,
333298
{
334-
self.start_transfer(address, buffer.as_slice().len(), RD_WRNW::READ);
335-
336-
// See explanation of tokens in `write_all`.
337-
let token = Rx(PhantomData);
338-
339-
// Safe, because we're only taking the address of a register.
340-
let address = &unsafe { &*I::ptr() }.rxdr as *const _ as u32;
341-
342299
let num_words = buffer.len();
343-
// Safe, because the trait bounds of this method guarantee that the
344-
// buffer can be written to.
345-
let transfer = unsafe {
346-
dma::Transfer::new(
347-
dma,
348-
token,
349-
channel,
350-
buffer,
351-
num_words,
352-
address,
353-
dma::Priority::high(),
354-
dma::Direction::peripheral_to_memory(),
355-
)
356-
};
357-
358-
Transfer {
359-
target: self,
360-
inner: transfer,
361-
}
300+
self.read_some(dma, channel, address, buffer, num_words)
362301
}
363302

364303
#[cfg(feature = "stm32l0x2")]

src/serial.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -378,27 +378,10 @@ macro_rules! usart {
378378
Buffer::Target: AsMutSlice<Element=u8>,
379379
Channel: dma::Channel,
380380
{
381-
// Safe, because we're only taking the address of a
382-
// register.
383-
let address =
384-
&unsafe { &*$USARTX::ptr() }.rdr as *const _ as u32;
385-
386381
let num_words = (*buffer).len();
387-
// Safe, because the trait bounds of this method guarantee
388-
// that the buffer can be written to.
389-
unsafe {
390-
dma::Transfer::new(
391-
dma,
392-
self,
393-
channel,
394-
buffer,
395-
num_words,
396-
address,
397-
dma::Priority::high(),
398-
dma::Direction::peripheral_to_memory(),
399-
)
400-
}
382+
self.read_some(dma, buffer, num_words, channel)
401383
}
384+
402385
pub fn read_some<Buffer, Channel>(self,
403386
dma: &mut dma::Handle,
404387
buffer: Pin<Buffer>,
@@ -530,26 +513,8 @@ macro_rules! usart {
530513
Buffer::Target: AsSlice<Element=u8>,
531514
Channel: dma::Channel,
532515
{
533-
// Safe, because we're only taking the address of a
534-
// register.
535-
let address =
536-
&unsafe { &*$USARTX::ptr() }.tdr as *const _ as u32;
537-
538516
let num_words = (*buffer).len();
539-
// Safe, because the trait bounds of this method guarantee
540-
// that the buffer can be read from.
541-
unsafe {
542-
dma::Transfer::new(
543-
dma,
544-
self,
545-
channel,
546-
buffer,
547-
num_words,
548-
address,
549-
dma::Priority::high(),
550-
dma::Direction::memory_to_peripheral(),
551-
)
552-
}
517+
self.write_some(dma, buffer, num_words, channel)
553518
}
554519

555520
pub fn write_some<Buffer, Channel>(self,

0 commit comments

Comments
 (0)