Skip to content

Commit 6c1d43d

Browse files
committed
feat: Move transfer size to generics
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
1 parent 0809605 commit 6c1d43d

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

bluepill-prog/src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ assign_resources! {
5050
}
5151
}
5252

53+
const USB_BUFFER_SIZE: usize = 64;
54+
5355
// According to Serial Flasher Protocol Specification - version 1
5456
#[embassy_executor::main]
5557
async fn main(spawner: Spawner) {
@@ -131,7 +133,7 @@ async fn main(spawner: Spawner) {
131133
let serprog_class = {
132134
static STATE: StaticCell<State> = StaticCell::new();
133135
let state = STATE.init(State::new());
134-
CdcAcmClass::new(&mut builder, state, 64)
136+
CdcAcmClass::new(&mut builder, state, USB_BUFFER_SIZE.try_into().unwrap())
135137
};
136138

137139
let usb = builder.build();
@@ -190,7 +192,13 @@ async fn serprog_task(mut class: CdcAcmClass<'static, CustomUsbDriver>, r: SpiRe
190192

191193
loop {
192194
class.wait_connection().await;
193-
let serprog = serprog::Serprog::new(spi, cs, led, class, Some(set_freq_cb));
195+
let serprog = serprog::Serprog::<_, _, _, _, _, USB_BUFFER_SIZE>::new(
196+
spi,
197+
cs,
198+
led,
199+
class,
200+
Some(set_freq_cb),
201+
);
194202
serprog.run_loop().await
195203
}
196204
}

picoprog/src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ assign_resources! {
5151
}
5252

5353
const FLASH_SIZE: usize = 2 * 1024 * 1024;
54+
const USB_BUFFER_SIZE: usize = 64;
5455

5556
// According to Serial Flasher Protocol Specification - version 1
5657

@@ -106,13 +107,13 @@ async fn main(spawner: Spawner) {
106107
let uart_class = {
107108
static STATE: StaticCell<State> = StaticCell::new();
108109
let state = STATE.init(State::new());
109-
CdcAcmClass::new(&mut builder, state, 64)
110+
CdcAcmClass::new(&mut builder, state, USB_BUFFER_SIZE.try_into().unwrap())
110111
};
111112

112113
let serprog_class = {
113114
static STATE: StaticCell<State> = StaticCell::new();
114115
let state = STATE.init(State::new());
115-
CdcAcmClass::new(&mut builder, state, 64)
116+
CdcAcmClass::new(&mut builder, state, USB_BUFFER_SIZE.try_into().unwrap())
116117
};
117118

118119
let usb = builder.build();
@@ -166,7 +167,13 @@ async fn serprog_task(class: CdcAcmClass<'static, CustomUsbDriver>, r: SpiResour
166167
spi.set_frequency(freq);
167168
};
168169

169-
let serprog = serprog::Serprog::new(spi, cs, led, class, Some(set_freq_cb));
170+
let serprog = serprog::Serprog::<_, _, _, _, _, USB_BUFFER_SIZE>::new(
171+
spi,
172+
cs,
173+
led,
174+
class,
175+
Some(set_freq_cb),
176+
);
170177
serprog.run_loop().await
171178
}
172179

serprog/src/lib.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,29 @@ impl QCmdMapResponse {
199199
}
200200
}
201201

202-
pub struct Serprog<SPI, CS, LED, T: Transport, F> {
202+
pub struct Serprog<SPI, CS, LED, T: Transport<TRANSFER_SIZE>, F, const TRANSFER_SIZE: usize> {
203203
spi: SPI,
204204
cs: CS,
205205
led: LED,
206206
transport: T,
207207
freq_callback: Option<F>,
208208
}
209209

210-
async fn ospiop_usb_task<T: Transport>(
210+
async fn ospiop_usb_task<T: Transport<TRANSFER_SIZE>, const TRANSFER_SIZE: usize>(
211211
transport: &mut T,
212-
mut sender: Sender<'_, NoopRawMutex, Vec<u8, 64>>,
212+
mut sender: Sender<'_, NoopRawMutex, Vec<u8, TRANSFER_SIZE>>,
213213
sdata_size: usize,
214-
mut receiver: Receiver<'_, NoopRawMutex, Vec<u8, 64>>,
214+
mut receiver: Receiver<'_, NoopRawMutex, Vec<u8, TRANSFER_SIZE>>,
215215
rdata_size: usize,
216216
) -> Result<(), SerprogError> {
217217
// First block - already contains header + initial data, send as-is
218218
let mut data_to_read = sdata_size;
219-
let first_block_data_size = data_to_read.min(64 - 6);
219+
let first_block_data_size = data_to_read.min(TRANSFER_SIZE - 6);
220220
sender.send_done();
221221
data_to_read -= first_block_data_size;
222222

223223
while data_to_read > 0 {
224-
let read_size = data_to_read.min(64);
224+
let read_size = data_to_read.min(TRANSFER_SIZE);
225225
let buf = sender.send().await;
226226
buf.clear();
227227
buf.resize(read_size, 0).ok();
@@ -250,11 +250,11 @@ async fn ospiop_usb_task<T: Transport>(
250250
Ok(())
251251
}
252252

253-
async fn ospiop_spi_task<SPI: SpiBus<u8>, CS: OutputPin>(
253+
async fn ospiop_spi_task<SPI: SpiBus<u8>, CS: OutputPin, const TRANSFER_SIZE: usize>(
254254
spi: &mut SPI,
255-
mut receiver: Receiver<'_, NoopRawMutex, Vec<u8, 64>>,
255+
mut receiver: Receiver<'_, NoopRawMutex, Vec<u8, TRANSFER_SIZE>>,
256256
sdata_size: usize,
257-
mut sender: Sender<'_, NoopRawMutex, Vec<u8, 64>>,
257+
mut sender: Sender<'_, NoopRawMutex, Vec<u8, TRANSFER_SIZE>>,
258258
rdata_size: usize,
259259
cs: &mut CS,
260260
) -> Result<(), SerprogError> {
@@ -271,7 +271,7 @@ async fn ospiop_spi_task<SPI: SpiBus<u8>, CS: OutputPin>(
271271
let write_slice = if is_first {
272272
// First buffer: skip the 6-byte header
273273
is_first = false;
274-
let data_len = data_to_write.min(64 - 6);
274+
let data_len = data_to_write.min(TRANSFER_SIZE - 6);
275275
data_to_write -= data_len;
276276
&buf[6..6 + data_len]
277277
} else {
@@ -288,7 +288,7 @@ async fn ospiop_spi_task<SPI: SpiBus<u8>, CS: OutputPin>(
288288
while data_to_read > 0 {
289289
let buf = sender.send().await;
290290
buf.clear();
291-
let read_size = data_to_read.min(64);
291+
let read_size = data_to_read.min(TRANSFER_SIZE);
292292
buf.resize(read_size, 0).ok();
293293
spi.read(buf.as_mut_slice())
294294
.await
@@ -302,12 +302,12 @@ async fn ospiop_spi_task<SPI: SpiBus<u8>, CS: OutputPin>(
302302
Ok(())
303303
}
304304

305-
impl<SPI, CS, LED, T, F> Serprog<SPI, CS, LED, T, F>
305+
impl<SPI, CS, LED, T, F, const TRANSFER_SIZE: usize> Serprog<SPI, CS, LED, T, F, TRANSFER_SIZE>
306306
where
307307
SPI: SpiBus<u8>,
308308
CS: OutputPin,
309309
LED: OutputPin,
310-
T: Transport,
310+
T: Transport<TRANSFER_SIZE>,
311311
F: FnMut(&mut SPI, u32) + Send + Sync,
312312
{
313313
pub fn new(spi: SPI, cs: CS, led: LED, transport: T, freq_callback: Option<F>) -> Self {
@@ -320,7 +320,11 @@ where
320320
}
321321
}
322322

323-
pub async fn run_loop(mut self) -> ! {
323+
pub async fn run_loop(mut self) -> !
324+
where
325+
CS::Error: core::fmt::Debug,
326+
LED::Error: core::fmt::Debug,
327+
{
324328
let mut buf = [0; 1];
325329

326330
loop {
@@ -439,8 +443,8 @@ where
439443
debug!("Received OSpiOp CMD");
440444

441445
// Read directly into the first channel buffer
442-
let mut usb_rx_spi_tx_buf = [const { Vec::<u8, 64>::new() }; 4];
443-
usb_rx_spi_tx_buf[0].resize(64, 0).ok();
446+
let mut usb_rx_spi_tx_buf = [const { Vec::<u8, TRANSFER_SIZE>::new() }; 4];
447+
usb_rx_spi_tx_buf[0].resize(TRANSFER_SIZE, 0).ok();
444448
self.transport
445449
.read(usb_rx_spi_tx_buf[0].as_mut_slice())
446450
.await
@@ -450,25 +454,31 @@ where
450454
let op_slen = le_u24_to_u32(&usb_rx_spi_tx_buf[0][0..3]) as usize;
451455
let op_rlen = le_u24_to_u32(&usb_rx_spi_tx_buf[0][3..6]) as usize;
452456

453-
let mut usb_rx_spi_tx_channel: Channel<'_, NoopRawMutex, Vec<u8, 64>> =
457+
let mut usb_rx_spi_tx_channel: Channel<'_, NoopRawMutex, Vec<u8, TRANSFER_SIZE>> =
454458
Channel::new(&mut usb_rx_spi_tx_buf);
455459
let (usb_rx, spi_tx) = usb_rx_spi_tx_channel.split();
456460

457-
let mut usb_tx_spi_rx_buf = [const { Vec::<u8, 64>::new() }; 8];
458-
let mut usb_tx_spi_rx_channel: Channel<'_, NoopRawMutex, Vec<u8, 64>> =
461+
let mut usb_tx_spi_rx_buf = [const { Vec::<u8, TRANSFER_SIZE>::new() }; 8];
462+
let mut usb_tx_spi_rx_channel: Channel<'_, NoopRawMutex, Vec<u8, TRANSFER_SIZE>> =
459463
Channel::new(&mut usb_tx_spi_rx_buf);
460464
let (spi_rx, usb_tx) = usb_tx_spi_rx_channel.split();
461465

462466
let (spi_res, usb_res) = block_on(join(
463-
ospiop_spi_task(
467+
ospiop_spi_task::<_, _, TRANSFER_SIZE>(
464468
&mut self.spi,
465469
spi_tx,
466470
op_slen,
467471
spi_rx,
468472
op_rlen,
469473
&mut self.cs,
470474
),
471-
ospiop_usb_task(&mut self.transport, usb_rx, op_slen, usb_tx, op_rlen),
475+
ospiop_usb_task::<_, TRANSFER_SIZE>(
476+
&mut self.transport,
477+
usb_rx,
478+
op_slen,
479+
usb_tx,
480+
op_rlen,
481+
),
472482
));
473483
if let Err(spi_err) = spi_res {
474484
self.transport

serprog/src/transport.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use core::future;
22
use embassy_usb::class::cdc_acm::CdcAcmClass;
33

4-
pub trait Transport {
4+
pub trait Transport<const TRANSFER_SIZE: usize> {
55
fn read(&mut self, buf: &mut [u8]) -> impl future::Future<Output = Result<(), ()>>;
66
fn write(&mut self, data: &[u8]) -> impl future::Future<Output = Result<(), ()>>;
77
}
88

9-
impl<'d, D: embassy_usb::driver::Driver<'d>> Transport for CdcAcmClass<'d, D> {
9+
impl<'d, D: embassy_usb::driver::Driver<'d>, const TRANSFER_SIZE: usize> Transport<TRANSFER_SIZE>
10+
for CdcAcmClass<'d, D>
11+
{
1012
async fn read(&mut self, buf: &mut [u8]) -> Result<(), ()> {
1113
let packet_size = self.max_packet_size() as usize;
1214
let buf_len = buf.len();
1315

1416
// Use a buffer large enough for full speed and high speed
15-
let mut buffer = [0; 512];
17+
let mut buffer = [0; TRANSFER_SIZE];
1618
let mut size = 0;
1719
if buf_len < packet_size {
1820
let bytes_read = self

0 commit comments

Comments
 (0)