Skip to content

Commit

Permalink
Fix bus reset, try to prevent ep0 overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
kauwua committed Feb 22, 2024
1 parent c1b07fd commit f2a89d5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/wch-ch56x-lib/USBDevice/usb20.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,13 @@ __attribute__((interrupt("WCH-Interrupt-fast"))) void USBHS_IRQHandler(void)

if (ep0_passthrough_enabled)
{
usb2_backend_current_device->endpoints.endp0_passthrough_setup_callback(
volatile USB_ENDPOINT* endp0 = &usb2_backend_current_device->endpoints.rx[0];

endp0->state = usb2_backend_current_device->endpoints.endp0_passthrough_setup_callback(
usb2_backend_current_device->endpoints.rx[0].buffer, sizeof(USB_SETUP));
*usb2_get_rx_endpoint_addr_reg(0) = (uint32_t)endp0->buffer;
R8_UEP0_TX_CTRL = UEP_T_RES_NAK | RB_UEP_T_TOG_1;
R8_UEP0_RX_CTRL = UEP_R_RES_ACK | RB_UEP_T_TOG_1;
R8_UEP0_RX_CTRL = endp0->state | RB_UEP_T_TOG_1;
}
else
{
Expand Down Expand Up @@ -815,11 +818,10 @@ __attribute__((interrupt("WCH-Interrupt-fast"))) void USBHS_IRQHandler(void)
}
else if (R8_USB_INT_FG & RB_USB_IF_BUSRST)
{
// usb2_user_handled.usb2_device_handle_bus_reset();
usb2_user_handled.usb2_device_handle_bus_reset();
usb2_set_device_address(0);
usb2_backend_current_device->addr = 0;
usb2_setup_endpoints();
// usb2_backend_current_device->speed = SPEED_NONE;
usb2_backend_current_device->state = DEFAULT;
R8_USB_INT_FG = RB_USB_IF_BUSRST;
}
Expand Down
7 changes: 5 additions & 2 deletions src/wch-ch56x-lib/USBDevice/usb_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ uint16_t _default_endp0_user_handled_control_request(USB_SETUP* request,
{
return 0xffff;
}
void _default_endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size);
void _default_endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size) {}
uint8_t _default_endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size);
uint8_t _default_endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size)
{
return ENDP_STATE_ACK;
}
void _default_endp_tx_complete(TRANSACTION_STATUS status);
void _default_endp_tx_complete(TRANSACTION_STATUS status) {}
uint8_t _default_endp_rx_callback(uint8_t* const ptr, uint16_t size);
Expand Down
4 changes: 3 additions & 1 deletion src/wch-ch56x-lib/USBDevice/usb_endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ typedef struct usb_endpoints_t
/**
* @brief Called by the USB2 backend in passthrough mode after receiving a
* SETUP request.
* @return new state of endpoint response (ACK 0x00, NAK 0X02, STALL 0X03).
* This will set the response for the next transfer (not this one).
*/
void (*endp0_passthrough_setup_callback)(uint8_t* ptr, uint16_t size);
uint8_t (*endp0_passthrough_setup_callback)(uint8_t* ptr, uint16_t size);

/**
* @brief Called by the backend when it has confirmed data has been sent
Expand Down
4 changes: 2 additions & 2 deletions tests/test_firmware_usb_loopback/User/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ uint16_t endp0_user_handled_control_request(USB_SETUP* request,
return 0xffff;
}

void endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size);
void endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size) {}
uint8_t endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size);
uint8_t endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size) { return ENDP_STATE_ACK; }

void usb2_device_handle_bus_reset(void);
void usb2_device_handle_bus_reset(void)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ uint16_t endp0_user_handled_control_request(USB_SETUP* request,
return 0xffff;
}

void endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size);
void endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size) {}
uint8_t endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size);
uint8_t endp0_passthrough_setup_callback(uint8_t* ptr, uint16_t size) { return ENDP_STATE_ACK; }

void usb2_device_handle_bus_reset(void);
void usb2_device_handle_bus_reset(void)
Expand Down

0 comments on commit f2a89d5

Please sign in to comment.