Skip to content

Commit 4f64fc2

Browse files
author
Henrik Alsér
committed
Twim write_read for embedded hal: copy data to RAM if needed, write all chunks then read
1 parent 287ea22 commit 4f64fc2

File tree

1 file changed

+120
-10
lines changed

1 file changed

+120
-10
lines changed

nrf-hal-common/src/twim.rs

Lines changed: 120 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ where
133133
while self.0.events_lasttx.read().bits() == 0 {}
134134
self.0.events_lasttx.write(|w| w); // reset event
135135

136-
// Stop read operation
136+
// Stop write operation
137137
self.0.tasks_stop.write(|w|
138138
// `1` is a valid value to write to task registers.
139139
unsafe { w.bits(1) });
@@ -229,7 +229,8 @@ where
229229
/// Write data to an I2C slave, then read data from the slave without
230230
/// triggering a stop condition between the two
231231
///
232-
/// The buffer must have a length of at most 255 bytes.
232+
/// The buffers must have a length of at most 255 bytes on the nRF52832
233+
/// and at most 65535 bytes on the nRF52840.
233234
pub fn write_then_read(
234235
&mut self,
235236
address: u8,
@@ -333,6 +334,122 @@ where
333334
Ok(())
334335
}
335336

337+
/// Copy data into RAM and write to an I2C slave, then read data from the slave without
338+
/// triggering a stop condition between the two
339+
///
340+
/// The read buffer must have a length of at most 255 bytes on the nRF52832
341+
/// and at most 65535 bytes on the nRF52840.
342+
pub fn copy_write_then_read(
343+
&mut self,
344+
address: u8,
345+
tx_buffer: &[u8],
346+
rx_buffer: &mut [u8],
347+
) -> Result<(), Error> {
348+
if rx_buffer.len() > EASY_DMA_SIZE {
349+
return Err(Error::RxBufferTooLong);
350+
}
351+
352+
// Conservative compiler fence to prevent optimizations that do not
353+
// take in to account actions by DMA. The fence has been placed here,
354+
// before any DMA action has started
355+
compiler_fence(SeqCst);
356+
357+
self.0
358+
.address
359+
.write(|w| unsafe { w.address().bits(address) });
360+
361+
// Set up the DMA read
362+
self.0.rxd.ptr.write(|w|
363+
// We're giving the register a pointer to the stack. Since we're
364+
// waiting for the I2C transaction to end before this stack pointer
365+
// becomes invalid, there's nothing wrong here.
366+
//
367+
// The PTR field is a full 32 bits wide and accepts the full range
368+
// of values.
369+
unsafe { w.ptr().bits(rx_buffer.as_mut_ptr() as u32) });
370+
self.0.rxd.maxcnt.write(|w|
371+
// We're giving it the length of the buffer, so no danger of
372+
// accessing invalid memory. We have verified that the length of the
373+
// buffer fits in an `u8`, so the cast to the type of maxcnt
374+
// is also fine.
375+
//
376+
// Note that that nrf52840 maxcnt is a wider
377+
// type than a u8, so we use a `_` cast rather than a `u8` cast.
378+
// The MAXCNT field is thus at least 8 bits wide and accepts the
379+
// full range of values that fit in a `u8`.
380+
unsafe { w.maxcnt().bits(rx_buffer.len() as _) });
381+
382+
// Chunk write data
383+
for chunk in tx_buffer.chunks(FORCE_COPY_BUFFER_SIZE) {
384+
// Copy chunk into RAM
385+
let wr_buffer = &mut [0; FORCE_COPY_BUFFER_SIZE][..];
386+
wr_buffer[..chunk.len()].copy_from_slice(chunk);
387+
388+
// Set up the DMA write
389+
self.0.txd.ptr.write(|w|
390+
// We're giving the register a pointer to the stack. Since we're
391+
// waiting for the I2C transaction to end before this stack pointer
392+
// becomes invalid, there's nothing wrong here.
393+
//
394+
// The PTR field is a full 32 bits wide and accepts the full range
395+
// of values.
396+
unsafe { w.ptr().bits(wr_buffer.as_ptr() as u32) });
397+
398+
self.0.txd.maxcnt.write(|w|
399+
// We're giving it the length of the buffer, so no danger of
400+
// accessing invalid memory. We have verified that the length of the
401+
// buffer fits in an `u8`, so the cast to `u8` is also fine.
402+
//
403+
// The MAXCNT field is 8 bits wide and accepts the full range of
404+
// values.
405+
unsafe { w.maxcnt().bits(wr_buffer.len() as _) });
406+
407+
// Start write operation
408+
self.0.tasks_starttx.write(|w|
409+
// `1` is a valid value to write to task registers.
410+
unsafe { w.bits(1) });
411+
412+
// Wait until write operation is about to end
413+
while self.0.events_lasttx.read().bits() == 0 {}
414+
self.0.events_lasttx.write(|w| w); // reset event
415+
416+
// Check for bad writes
417+
if self.0.txd.amount.read().bits() != wr_buffer.len() as u32 {
418+
return Err(Error::Transmit);
419+
}
420+
}
421+
422+
// Start read operation
423+
self.0.tasks_startrx.write(|w|
424+
// `1` is a valid value to write to task registers.
425+
unsafe { w.bits(1) });
426+
427+
// Wait until read operation is about to end
428+
while self.0.events_lastrx.read().bits() == 0 {}
429+
self.0.events_lastrx.write(|w| w); // reset event
430+
431+
// Stop read operation
432+
self.0.tasks_stop.write(|w|
433+
// `1` is a valid value to write to task registers.
434+
unsafe { w.bits(1) });
435+
436+
// Wait until total operation has ended
437+
while self.0.events_stopped.read().bits() == 0 {}
438+
self.0.events_stopped.write(|w| w); // reset event
439+
440+
// Conservative compiler fence to prevent optimizations that do not
441+
// take in to account actions by DMA. The fence has been placed here,
442+
// after all possible DMA actions have completed
443+
compiler_fence(SeqCst);
444+
445+
// Check for bad reads
446+
if self.0.rxd.amount.read().bits() != rx_buffer.len() as u32 {
447+
return Err(Error::Receive);
448+
}
449+
450+
Ok(())
451+
}
452+
336453
/// Return the raw interface to the underlying TWIM peripheral
337454
pub fn free(self) -> T {
338455
self.0
@@ -387,14 +504,7 @@ where
387504
if slice_in_ram(bytes) {
388505
self.write_then_read(addr, bytes, buffer)
389506
} else {
390-
let txi = bytes.chunks(FORCE_COPY_BUFFER_SIZE);
391-
let rxi = buffer.chunks_mut(FORCE_COPY_BUFFER_SIZE);
392-
let tx_buf = &mut [0; FORCE_COPY_BUFFER_SIZE][..];
393-
txi.zip(rxi).try_for_each(|(tx_chunk, rx_chunk)| {
394-
tx_buf[..tx_chunk.len()].copy_from_slice(tx_chunk);
395-
self.write_then_read(addr, &tx_buf[..tx_chunk.len()], rx_chunk)
396-
})?;
397-
Ok(())
507+
self.copy_write_then_read(addr, bytes, buffer)
398508
}
399509
}
400510
}

0 commit comments

Comments
 (0)