Skip to content

Commit

Permalink
ports/stm32: Use the new debug callbacks.
Browse files Browse the repository at this point in the history
This allows the debug code to write directly to the endpoint buffer
which saves an extra memcpy.
  • Loading branch information
iabdalkader committed Aug 3, 2024
1 parent 2f3666b commit 7c8ff80
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 55 deletions.
22 changes: 1 addition & 21 deletions ports/stm32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,6 @@ void usb_vcp_send_strn(const char *str, int len) {
}
}

uint32_t usb_cdc_buf_len()
{
return usbd_cdc_buf_len(&usb_device.usbd_cdc_itf[0]);
}

uint32_t usb_cdc_get_buf(uint8_t *buf, uint32_t len)
{
return usbd_cdc_get_buf(&usb_device.usbd_cdc_itf[0], buf, len);
}

void usb_cdc_reset_buffers()
{
usbd_cdc_reset_buffers(&usb_device.usbd_cdc_itf[0]);
}

int usb_cdc_debug_mode_enabled()
{
return usbd_cdc_debug_mode_enabled(&usb_device.usbd_cdc_itf[0]);
}

usbd_cdc_itf_t *usb_vcp_get(int idx) {
return &usb_device.usbd_cdc_itf[idx];
}
Expand Down Expand Up @@ -758,7 +738,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_vcp_isconnected_obj, pyb_usb_vcp_isconn

static mp_obj_t pyb_usb_vcp_debug_mode_enabled(mp_obj_t self_in) {
pyb_usb_vcp_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(usbd_cdc_debug_mode_enabled(self->cdc_itf));
return mp_obj_new_bool(self->cdc_itf->dbg_mode_enabled);
}
static MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_vcp_debug_mode_enabled_obj, pyb_usb_vcp_debug_mode_enabled);

Expand Down
6 changes: 1 addition & 5 deletions ports/stm32/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ bool pyb_usb_dev_init(int dev_id, uint16_t vid, uint16_t pid, uint8_t mode, size
void pyb_usb_dev_deinit(void);
bool usb_vcp_is_enabled(void);
int usb_vcp_recv_byte(uint8_t *c); // if a byte is available, return 1 and put the byte in *c, else return 0
void usb_vcp_send_strn(const char* str, int len);
uint32_t usb_cdc_buf_len();
uint32_t usb_cdc_get_buf(uint8_t *buf, uint32_t len);
void usb_cdc_reset_buffers();
int usb_cdc_debug_mode_enabled();
void usb_vcp_send_strn(const char *str, int len);
void usb_vcp_attach_to_repl(const pyb_usb_vcp_obj_t *self, bool attached);

void pyb_usb_host_init(void);
Expand Down
51 changes: 35 additions & 16 deletions ports/stm32/usbd_cdc_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "extmod/modmachine.h"
#include "shared/runtime/interrupt_char.h"
#include "irq.h"
#include "usb.h"
#include "usbdbg.h"

#if MICROPY_HW_ENABLE_USB

Expand All @@ -73,10 +75,7 @@
#define IDE_BAUDRATE_SLOW (921600)
#define IDE_BAUDRATE_FAST (12000000)

extern bool usbdbg_get_irq_enabled();
extern void usbdbg_data_in(void *buffer, int length);
extern void usbdbg_data_out(void *buffer, int length);
extern void usbdbg_control(void *buffer, uint8_t brequest, uint32_t wlength);
void usb_cdc_reset_buffers();

// Used to control the connect_state variable when USB host opens the serial port
static uint8_t usbd_cdc_connect_tx_timer;
Expand Down Expand Up @@ -158,7 +157,7 @@ int8_t usbd_cdc_control(usbd_cdc_state_t *cdc_in, uint8_t cmd, uint8_t *pbuf, ui
MICROPY_BOARD_ENTER_BOOTLOADER(0, 0);
}
#endif
usbd_cdc_reset_buffers(cdc);
usb_cdc_reset_buffers(cdc);
break;
}

Expand Down Expand Up @@ -208,7 +207,7 @@ int8_t usbd_cdc_control(usbd_cdc_state_t *cdc_in, uint8_t cmd, uint8_t *pbuf, ui
return USBD_OK;
}

static void send_packet(usbd_cdc_itf_t *cdc);
static void usbd_cdc_send_packet(usbd_cdc_itf_t *cdc);

static inline uint16_t usbd_cdc_tx_buffer_mask(uint16_t val) {
return val & (MICROPY_HW_USB_CDC_TX_DATA_SIZE - 1);
Expand Down Expand Up @@ -256,10 +255,10 @@ void usbd_cdc_tx_ready(usbd_cdc_state_t *cdc_in) {

if (cdc->dbg_mode_enabled == 1) {
if (cdc->dbg_xfer_length) {
send_packet(cdc);
usbd_cdc_send_packet(cdc);
} else if (cdc->dbg_last_packet == CDC_DATA_MAX_PACKET_SIZE) {
cdc->dbg_last_packet = 0;
USBD_CDC_TransmitPacket(&cdc->base, 0, cdc->dbg_xfer_buffer);
USBD_CDC_TransmitPacket(&cdc->base, 0, NULL); // ZLP
}
return;
}
Expand Down Expand Up @@ -340,21 +339,37 @@ void usbd_cdc_rx_check_resume(usbd_cdc_itf_t *cdc) {
enable_irq(irq_state);
}

uint32_t usbd_cdc_buf_len(usbd_cdc_itf_t *cdc) {
uint32_t usb_cdc_buf_len() {
usbd_cdc_itf_t *cdc = usb_vcp_get(0);
cdc->tx_buf_ptr_out = cdc->tx_buf_ptr_out_next;
if (usbd_cdc_tx_buffer_empty(cdc)) {
return 0;
}
return usbd_cdc_tx_send_length(cdc);
}

uint32_t usbd_cdc_get_buf(usbd_cdc_itf_t *cdc, uint8_t *buf, uint32_t len) {
uint32_t usb_cdc_get_buf(uint8_t *buf, uint32_t len) {
usbd_cdc_itf_t *cdc = usb_vcp_get(0);
cdc->tx_buf_ptr_out = cdc->tx_buf_ptr_out_next;
memcpy(buf, usbd_cdc_tx_buffer_getp(cdc, len), len);
return len;
}

void usbd_cdc_reset_buffers(usbd_cdc_itf_t *cdc) {
uint32_t usb_cdc_read(void *buf, uint32_t len) {
usbd_cdc_itf_t *cdc = usb_vcp_get(0);
memcpy(buf, cdc->rx_packet_buf, len);
return len;
}

uint32_t usb_cdc_write(const void *buf, uint32_t len) {
usbd_cdc_itf_t *cdc = usb_vcp_get(0);
USBD_CDC_TransmitPacket(&cdc->base, len, buf);
return len;
}

void usb_cdc_reset_buffers() {
usbd_cdc_itf_t *cdc = usb_vcp_get(0);

cdc->tx_buf_ptr_in = 0;
cdc->tx_buf_ptr_out = 0;
cdc->tx_buf_ptr_out_next = 0;
Expand All @@ -371,12 +386,16 @@ void usbd_cdc_reset_buffers(usbd_cdc_itf_t *cdc) {
}
}

static void send_packet(usbd_cdc_itf_t *cdc) {
int usb_cdc_debug_mode_enabled() {
usbd_cdc_itf_t *cdc = usb_vcp_get(0);
return cdc->dbg_mode_enabled;
}

static void usbd_cdc_send_packet(usbd_cdc_itf_t *cdc) {
int bytes = MIN(cdc->dbg_xfer_length, CDC_DATA_MAX_PACKET_SIZE);
cdc->dbg_last_packet = bytes;
usbdbg_data_in(cdc->dbg_xfer_buffer, bytes);
cdc->dbg_xfer_length -= bytes;
USBD_CDC_TransmitPacket(&cdc->base, bytes, cdc->dbg_xfer_buffer);
usbdbg_data_in(bytes, usb_cdc_write);
}

// Data received over USB OUT endpoint is processed here.
Expand All @@ -387,14 +406,14 @@ int8_t usbd_cdc_receive(usbd_cdc_state_t *cdc_in, size_t len) {

if (cdc->dbg_mode_enabled == 1) {
if (cdc->dbg_xfer_length) {
usbdbg_data_out(cdc->rx_packet_buf, len);
usbdbg_data_out(len, usb_cdc_read);
cdc->dbg_xfer_length -= len;
} else if (cdc->rx_packet_buf[0] == '\x30') { // command
uint8_t request = cdc->rx_packet_buf[1];
cdc->dbg_xfer_length = *((uint32_t*)(cdc->rx_packet_buf+2));
usbdbg_control(cdc->rx_packet_buf+6, request, cdc->dbg_xfer_length);
if (cdc->dbg_xfer_length && (request & 0x80)) { //request has a device-to-host data phase
send_packet(cdc); //prime tx buffer
usbd_cdc_send_packet(cdc); //prime tx buffer
}
}
} else {
Expand Down
17 changes: 4 additions & 13 deletions ports/stm32/usbd_cdc_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@ typedef struct _usbd_cdc_itf_t {
uint16_t tx_buf_ptr_out_next; // next position of above once transmission finished
uint8_t tx_need_empty_packet; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size

volatile uint8_t dbg_mode_enabled;
volatile uint32_t dbg_last_packet;
volatile uint32_t dbg_xfer_length;
uint8_t dbg_xfer_buffer[CDC_DATA_MAX_PACKET_SIZE];

uint8_t cdc_idx; // between 0 and MICROPY_HW_USB_CDC_NUM-1
volatile uint8_t connect_state; // indicates if we are connected
uint8_t attached_to_repl; // indicates if interface is connected to REPL
uint8_t flow; // USBD_CDC_FLOWCONTROL_* setting flags
uint32_t bitrate;

volatile uint8_t dbg_mode_enabled;
volatile uint32_t dbg_last_packet;
volatile uint32_t dbg_xfer_length;
} usbd_cdc_itf_t;

// This is implemented in usb.c
Expand All @@ -77,10 +76,6 @@ static inline int usbd_cdc_is_connected(usbd_cdc_itf_t *cdc) {
return cdc->connect_state == USBD_CDC_CONNECT_STATE_CONNECTED;
}

static inline int usbd_cdc_debug_mode_enabled(usbd_cdc_itf_t *cdc) {
return cdc->dbg_mode_enabled;
}

int usbd_cdc_tx_half_empty(usbd_cdc_itf_t *cdc);
int usbd_cdc_tx_flow(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len);
int usbd_cdc_tx(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len, uint32_t timeout);
Expand All @@ -90,8 +85,4 @@ int usbd_cdc_rx_num(usbd_cdc_itf_t *cdc);
int usbd_cdc_rx(usbd_cdc_itf_t *cdc, uint8_t *buf, uint32_t len, uint32_t timeout);
void usbd_cdc_rx_event_callback(usbd_cdc_itf_t *cdc);

void usbd_cdc_reset_buffers(usbd_cdc_itf_t *cdc);
uint32_t usbd_cdc_buf_len(usbd_cdc_itf_t *cdc);
uint32_t usbd_cdc_get_buf(usbd_cdc_itf_t *cdc, uint8_t *buf, uint32_t len);

#endif // MICROPY_INCLUDED_STM32_USBD_CDC_INTERFACE_H

0 comments on commit 7c8ff80

Please sign in to comment.