Skip to content

Commit

Permalink
Fix USB CDC receive code (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszwojcik authored Nov 30, 2024
1 parent b1fd122 commit 50e684d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 8 additions & 5 deletions core/src/core/usb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ pub fn Usb(comptime f: anytype) type {
const data_chunk = S.buffer_reader.try_peek(64);

if (data_chunk.len > 0) {
f.usb_start_tx(Endpoint.EP0_IN_IDX, data_chunk);
f.usb_start_tx(Endpoint.EP0_IN_ADDR, data_chunk);
}
}

fn send_cmd_ack() void {
f.usb_start_tx(Endpoint.EP0_IN_IDX, &.{});
f.usb_start_tx(Endpoint.EP0_IN_ADDR, &.{});
}
};

Expand Down Expand Up @@ -441,12 +441,12 @@ pub fn Usb(comptime f: anytype) type {
const next_data_chunk = buffer_reader.try_peek(64);
if (next_data_chunk.len > 0) {
f.usb_start_tx(
Endpoint.EP0_IN_IDX,
Endpoint.EP0_IN_ADDR,
next_data_chunk,
);
} else {
f.usb_start_rx(
Endpoint.EP0_OUT_IDX,
Endpoint.EP0_OUT_ADDR,
0,
);

Expand All @@ -461,7 +461,7 @@ pub fn Usb(comptime f: anytype) type {
// OUT) a zero-byte DATA packet, so, set that
// up:
f.usb_start_rx(
Endpoint.EP0_OUT_IDX,
Endpoint.EP0_OUT_ADDR,
0,
);

Expand All @@ -477,6 +477,9 @@ pub fn Usb(comptime f: anytype) type {
if (driver != null) {
driver.?.transfer(epb.endpoint_address, epb.buffer);
}
if (Endpoint.dir_from_address(epb.endpoint_address) == .Out) {
f.endpoint_reset_rx(epb.endpoint_address);
}
},
}
}
Expand Down
3 changes: 0 additions & 3 deletions core/src/core/usb/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ pub const Endpoint = struct {
return if (addr & Dir.DIR_IN_MASK != 0) Dir.In else Dir.Out;
}

pub const EP0_OUT_IDX = 0;
pub const EP0_IN_IDX = 1;

pub const EP0_IN_ADDR: u8 = to_address(0, .In);
pub const EP0_OUT_ADDR: u8 = to_address(0, .Out);
};
Expand Down
21 changes: 15 additions & 6 deletions port/raspberrypi/rp2xxx/src/hal/usb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ const resets = @import("resets.zig");

pub const RP2XXX_MAX_ENDPOINTS_COUNT = 16;

pub const EP0_OUT_IDX = 0;
pub const EP0_IN_IDX = 1;

pub const UsbConfig = struct {
// Comptime defined supported max endpoints number, can be reduced to save RAM space
max_endpoints_count: u8 = RP2XXX_MAX_ENDPOINTS_COUNT,
Expand Down Expand Up @@ -65,6 +62,7 @@ const HardwareEndpoint = struct {
endpoint_control_index: usize,
buffer_control_index: usize,
data_buffer_index: usize,
awaiting_rx: bool,
};

// +++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down Expand Up @@ -346,6 +344,9 @@ pub fn F(comptime config: UsbConfig) type {

const ep = hardware_endpoint_get_by_address(ep_addr);

if (ep.awaiting_rx)
return;

// Check which DATA0/1 PID this endpoint is expecting next.
const np: u1 = if (ep.next_pid_1) 1 else 0;
// Configure the OUT:
Expand All @@ -358,6 +359,12 @@ pub fn F(comptime config: UsbConfig) type {

// Flip the DATA0/1 PID for the next receive
ep.next_pid_1 = !ep.next_pid_1;
ep.awaiting_rx = true;
}

pub fn endpoint_reset_rx(ep_addr: u8) void {
const ep = hardware_endpoint_get_by_address(ep_addr);
ep.awaiting_rx = false;
}

/// Check which interrupt flags are set
Expand Down Expand Up @@ -414,7 +421,7 @@ pub fn F(comptime config: UsbConfig) type {
}

pub fn reset_ep0() void {
var ep = hardware_endpoint_get_by_address(Endpoint.EP0_IN_IDX);
var ep = hardware_endpoint_get_by_address(Endpoint.EP0_IN_ADDR);
ep.next_pid_1 = true;
}

Expand Down Expand Up @@ -443,14 +450,16 @@ pub fn F(comptime config: UsbConfig) type {
ep.max_packet_size = max_packet_size;
ep.transfer_type = transfer_type;
ep.next_pid_1 = false;
ep.awaiting_rx = false;

ep.buffer_control_index = 2 * ep_num + ep_dir.as_number_reversed();
// TODO - some other way to deal with it
ep.data_buffer_index = 2 * ep_num + ep_dir.as_number();

if (ep_num == 0) {
ep.data_buffer_index = 0;
ep.endpoint_control_index = 0;
} else {
// TODO - some other way to deal with it
ep.data_buffer_index = 2 * ep_num + ep_dir.as_number();
ep.endpoint_control_index = 2*ep_num - ep_dir.as_number();
endpoint_alloc(ep, transfer_type);
}
Expand Down

0 comments on commit 50e684d

Please sign in to comment.