Skip to content

Commit

Permalink
usb: device_next: cdc_acm: move few constant data to flash
Browse files Browse the repository at this point in the history
Add a config structure and move few pointers that don't need to change
into it, save few bytes of SRAM.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
  • Loading branch information
fabiobaltieri authored and kartben committed Jan 29, 2025
1 parent d9ab2eb commit 5a40e49
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions subsys/usb/device_next/class/usbd_cdc_acm.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct usbd_cdc_acm_desc {
struct usb_desc_header nil_desc;
};

struct cdc_acm_uart_data {
struct cdc_acm_uart_config {
/* Pointer to the associated USBD class node */
struct usbd_class_data *c_data;
/* Pointer to the interface description node or NULL */
Expand All @@ -89,6 +89,10 @@ struct cdc_acm_uart_data {
struct usbd_cdc_acm_desc *const desc;
const struct usb_desc_header **const fs_desc;
const struct usb_desc_header **const hs_desc;
};

struct cdc_acm_uart_data {
const struct device *dev;
/* Line Coding Structure */
struct cdc_acm_line_coding line_coding;
/* SetControlLineState bitmap */
Expand Down Expand Up @@ -164,8 +168,8 @@ static uint8_t cdc_acm_get_int_in(struct usbd_class_data *const c_data)
{
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
const struct device *dev = usbd_class_get_private(c_data);
struct cdc_acm_uart_data *data = dev->data;
struct usbd_cdc_acm_desc *desc = data->desc;
const struct cdc_acm_uart_config *cfg = dev->config;
struct usbd_cdc_acm_desc *desc = cfg->desc;

if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
return desc->if0_hs_int_ep.bEndpointAddress;
Expand All @@ -178,8 +182,8 @@ static uint8_t cdc_acm_get_bulk_in(struct usbd_class_data *const c_data)
{
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
const struct device *dev = usbd_class_get_private(c_data);
struct cdc_acm_uart_data *data = dev->data;
struct usbd_cdc_acm_desc *desc = data->desc;
const struct cdc_acm_uart_config *cfg = dev->config;
struct usbd_cdc_acm_desc *desc = cfg->desc;

if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
return desc->if1_hs_in_ep.bEndpointAddress;
Expand All @@ -192,8 +196,8 @@ static uint8_t cdc_acm_get_bulk_out(struct usbd_class_data *const c_data)
{
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
const struct device *dev = usbd_class_get_private(c_data);
struct cdc_acm_uart_data *data = dev->data;
struct usbd_cdc_acm_desc *desc = data->desc;
const struct cdc_acm_uart_config *cfg = dev->config;
struct usbd_cdc_acm_desc *desc = cfg->desc;

if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
return desc->if1_hs_out_ep.bEndpointAddress;
Expand Down Expand Up @@ -340,13 +344,13 @@ static void *usbd_cdc_acm_get_desc(struct usbd_class_data *const c_data,
const enum usbd_speed speed)
{
const struct device *dev = usbd_class_get_private(c_data);
struct cdc_acm_uart_data *data = dev->data;
const struct cdc_acm_uart_config *cfg = dev->config;

if (speed == USBD_SPEED_HS) {
return data->hs_desc;
return cfg->hs_desc;
}

return data->fs_desc;
return cfg->fs_desc;
}

static void cdc_acm_update_uart_cfg(struct cdc_acm_uart_data *const data)
Expand Down Expand Up @@ -492,17 +496,17 @@ static int usbd_cdc_acm_init(struct usbd_class_data *const c_data)
{
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
const struct device *dev = usbd_class_get_private(c_data);
struct cdc_acm_uart_data *data = dev->data;
struct usbd_cdc_acm_desc *desc = data->desc;
const struct cdc_acm_uart_config *cfg = dev->config;
struct usbd_cdc_acm_desc *desc = cfg->desc;

desc->if0_union.bControlInterface = desc->if0.bInterfaceNumber;
desc->if0_union.bSubordinateInterface0 = desc->if1.bInterfaceNumber;

if (data->if_desc_data != NULL) {
if (usbd_add_descriptor(uds_ctx, data->if_desc_data)) {
if (cfg->if_desc_data != NULL) {
if (usbd_add_descriptor(uds_ctx, cfg->if_desc_data)) {
LOG_ERR("Failed to add interface string descriptor");
} else {
desc->if0.iInterface = usbd_str_desc_get_idx(data->if_desc_data);
desc->if0.iInterface = usbd_str_desc_get_idx(cfg->if_desc_data);
}
}

Expand All @@ -521,7 +525,8 @@ static int cdc_acm_send_notification(const struct device *dev,
.data = sys_cpu_to_le16(serial_state),
};
struct cdc_acm_uart_data *data = dev->data;
struct usbd_class_data *c_data = data->c_data;
const struct cdc_acm_uart_config *cfg = dev->config;
struct usbd_class_data *c_data = cfg->c_data;
struct net_buf *buf;
uint8_t ep;
int ret;
Expand Down Expand Up @@ -557,13 +562,15 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
{
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct cdc_acm_uart_data *data;
const struct cdc_acm_uart_config *cfg;
struct usbd_class_data *c_data;
struct net_buf *buf;
size_t len;
int ret;

data = CONTAINER_OF(dwork, struct cdc_acm_uart_data, tx_fifo_work);
c_data = data->c_data;
cfg = data->dev->config;
c_data = cfg->c_data;

if (!atomic_test_bit(&data->state, CDC_ACM_CLASS_ENABLED)) {
LOG_DBG("USB configuration is not enabled");
Expand Down Expand Up @@ -611,13 +618,15 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
static void cdc_acm_rx_fifo_handler(struct k_work *work)
{
struct cdc_acm_uart_data *data;
const struct cdc_acm_uart_config *cfg;
struct usbd_class_data *c_data;
struct net_buf *buf;
uint8_t ep;
int ret;

data = CONTAINER_OF(work, struct cdc_acm_uart_data, rx_fifo_work);
c_data = data->c_data;
cfg = data->dev->config;
c_data = cfg->c_data;

if (!atomic_test_bit(&data->state, CDC_ACM_CLASS_ENABLED) ||
atomic_test_bit(&data->state, CDC_ACM_CLASS_SUSPENDED)) {
Expand Down Expand Up @@ -837,10 +846,12 @@ static int cdc_acm_irq_update(const struct device *dev)
static void cdc_acm_irq_cb_handler(struct k_work *work)
{
struct cdc_acm_uart_data *data;
const struct cdc_acm_uart_config *cfg;
struct usbd_class_data *c_data;

data = CONTAINER_OF(work, struct cdc_acm_uart_data, irq_cb_work);
c_data = data->c_data;
cfg = data->dev->config;
c_data = cfg->c_data;

if (data->cb == NULL) {
LOG_ERR("IRQ callback is not set");
Expand Down Expand Up @@ -1279,23 +1290,27 @@ const static struct usb_desc_header *cdc_acm_hs_desc_##n[] = { \
RING_BUF_DECLARE(cdc_acm_rb_rx_##n, DT_INST_PROP(n, rx_fifo_size)); \
RING_BUF_DECLARE(cdc_acm_rb_tx_##n, DT_INST_PROP(n, tx_fifo_size)); \
\
static struct cdc_acm_uart_data uart_data_##n = { \
.line_coding = CDC_ACM_DEFAULT_LINECODING, \
static const struct cdc_acm_uart_config uart_config_##n = { \
.c_data = &cdc_acm_##n, \
IF_ENABLED(DT_INST_NODE_HAS_PROP(n, label), ( \
.if_desc_data = &cdc_acm_if_desc_data_##n, \
)) \
.desc = &cdc_acm_desc_##n, \
.fs_desc = cdc_acm_fs_desc_##n, \
.hs_desc = cdc_acm_hs_desc_##n, \
}; \
\
static struct cdc_acm_uart_data uart_data_##n = { \
.dev = DEVICE_DT_GET(DT_DRV_INST(n)), \
.line_coding = CDC_ACM_DEFAULT_LINECODING, \
.rx_fifo.rb = &cdc_acm_rb_rx_##n, \
.tx_fifo.rb = &cdc_acm_rb_tx_##n, \
.flow_ctrl = DT_INST_PROP(n, hw_flow_control), \
.notif_sem = Z_SEM_INITIALIZER(uart_data_##n.notif_sem, 0, 1), \
.desc = &cdc_acm_desc_##n, \
.fs_desc = cdc_acm_fs_desc_##n, \
.hs_desc = cdc_acm_hs_desc_##n, \
}; \
\
DEVICE_DT_INST_DEFINE(n, usbd_cdc_acm_preinit, NULL, \
&uart_data_##n, NULL, \
&uart_data_##n, &uart_config_##n, \
PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, \
&cdc_acm_uart_api);

Expand Down

0 comments on commit 5a40e49

Please sign in to comment.