Skip to content

Commit

Permalink
Merge #3468
Browse files Browse the repository at this point in the history
3468: capsules: spi_controller: Don't provide read buffer for write ops r=bradjc a=alistair23

### Pull Request Overview

The SPI HIL uses a single `read_write_bytes()` function which is used for both reading/writing. The `read_buffer` is optional if the caller just wants to write data and doesn't care about reading any data back they can provide `None`

The current userspace capsule always supplies a read buffer, even if userspace doesn't want to perform a read. This results in the data being read from the bus, passed back to the capsule and then not being used.

To avoid reading and copying data that was never requested, let's avoid passing the read buffer to the HIL implemenation if userspace doesn't want to read the data. This way implementations can avoid copying the data from the bus when it isn't going to be used.

### Testing Strategy

Running SPI operations

### TODO or Help Wanted

N/A

### Documentation Updated

- [X] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [X] Ran `make prepush`.


Co-authored-by: Alistair Francis <alistair.francis@wdc.com>
  • Loading branch information
bors[bot] and alistair23 committed Jun 9, 2023
2 parents 8a444ed + 7bced7e commit 484b864
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions capsules/core/src/spi_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,22 @@ impl<'a, S: SpiMasterDevice> Spi<'a, S> {
app.index = start + tmp_len;
tmp_len
});

let rlen = kernel_data
.get_readwrite_processbuffer(rw_allow::READ)
.map_or(0, |read| read.len());

// TODO verify SPI return value
let _ = self.spi_master.read_write_bytes(
self.kernel_write.take().unwrap(),
self.kernel_read.take(),
write_len,
);
let _ = if rlen == 0 {
self.spi_master
.read_write_bytes(self.kernel_write.take().unwrap(), None, write_len)
} else {
self.spi_master.read_write_bytes(
self.kernel_write.take().unwrap(),
self.kernel_read.take(),
write_len,
)
};
}
}

Expand Down Expand Up @@ -322,7 +332,9 @@ impl<S: SpiMasterDevice> SpiMasterClient for Spi<'_, S> {
src
});

self.kernel_read.put(rbuf);
if rbuf.is_some() {
self.kernel_read.put(rbuf);
}
self.kernel_write.replace(writebuf);

if app.index == app.len {
Expand Down

0 comments on commit 484b864

Please sign in to comment.