Commit dcf64c93 authored by Michael Hope's avatar Michael Hope Committed by Anas Nashif
Browse files

drivers: usb: switch the SAM0 driver from a custom allocator to the heap



Also automatically enable the heap if the USB device is selected.

Part of #23178

Signed-off-by: default avatarMichael Hope <mlhx@google.com>
parent 8e8f14e3
Loading
Loading
Loading
Loading
+23 −14
Original line number Diff line number Diff line
@@ -59,10 +59,6 @@ struct usb_sam0_data {

	uint8_t addr;
	uint32_t out_at;

	/* Memory used as a simple heap for the endpoint buffers */
	uint32_t ep_buf[USB_NUM_ENDPOINTS * 64 / 4];
	int brk;
};

static struct usb_sam0_data usb_sam0_data_0;
@@ -347,10 +343,11 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
{
	struct usb_sam0_data *data = usb_sam0_get_data();
	UsbDevice *regs = &REGS->DEVICE;
	uint8_t for_in = cfg->ep_addr & USB_EP_DIR_MASK;
	uint8_t ep = cfg->ep_addr & ~USB_EP_DIR_MASK;
	UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep];
	UsbDeviceDescriptor *desc = &data->descriptors[ep];
	UsbDeviceDescBank *bank;
	void *buf;
	int type;
	int size = -1;
	int i;
@@ -385,22 +382,34 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
		return -EINVAL;
	}

	if (for_in) {
	if (cfg->ep_addr & USB_EP_DIR_MASK) {
		bank = &desc->DeviceDescBank[1];
	} else {
		bank = &desc->DeviceDescBank[0];
	}

	buf = (void *)bank->ADDR.reg;

	if (bank->PCKSIZE.bit.SIZE != size || buf == NULL) {
		/* Release the previous buffer, if any */
		k_free(buf);

		buf = k_malloc(cfg->ep_mps);
		if (buf == NULL) {
			return -ENOMEM;
		}
		bank->PCKSIZE.bit.SIZE = size;
		bank->ADDR.reg = (uintptr_t)buf;
	}

	if (cfg->ep_addr & USB_EP_DIR_MASK) {
		endpoint->EPCFG.bit.EPTYPE1 = type;
		desc->DeviceDescBank[1].PCKSIZE.bit.SIZE = size;
		desc->DeviceDescBank[1].ADDR.reg =
			(uintptr_t)&data->ep_buf[data->brk];
		endpoint->EPSTATUSCLR.bit.BK1RDY = 1;
	} else {
		endpoint->EPCFG.bit.EPTYPE0 = type;
		desc->DeviceDescBank[0].PCKSIZE.bit.SIZE = size;
		desc->DeviceDescBank[0].ADDR.reg =
			(uintptr_t)&data->ep_buf[data->brk];
		endpoint->EPSTATUSCLR.bit.BK0RDY = 1;
	}

	data->brk += cfg->ep_mps / sizeof(uint32_t);

	return 0;
}

+7 −0
Original line number Diff line number Diff line
@@ -41,4 +41,11 @@ config USB_DC_SAM0
config WDT_SAM0
	default WATCHDOG

if USB

config HEAP_MEM_POOL_SIZE
	default 1024

endif # USB

endif # SOC_FAMILY_SAM0