#define DEVICE_MODE_ONLY true
#define SAMD11 DEVICE_MODE_ONLY
#ifndef ARM_MATH_CM4
# define ARM_MATH_CM4
#endif
#include "compiler.h"
#undef LITTLE_ENDIAN
#include "samd51j18a.h"
#include <stdbool.h>
#include <string.h>
#include "arm_math.h"
#include "status_codes.h"
#include "usb.h"
#define USB_LPM_ATTRIBUT_BLINKSTATE_MASK (0xF << 0)
#define USB_LPM_ATTRIBUT_HIRD_MASK (0xF << 4)
#define USB_LPM_ATTRIBUT_REMOTEWAKE_MASK (1 << 8)
#define USB_LPM_ATTRIBUT_BLINKSTATE(value) ((value & 0xF) << 0)
#define USB_LPM_ATTRIBUT_HIRD(value) ((value & 0xF) << 4)
#define USB_LPM_ATTRIBUT_REMOTEWAKE(value) ((value & 1) << 8)
#define USB_LPM_ATTRIBUT_BLINKSTATE_L1 USB_LPM_ATTRIBUT_BLINKSTATE(1)
#define USB_EP_ADDR_MASK 0x0f
#define USB_EP_DIR_IN 0x80
#define USB_EP_DIR_OUT 0x00
COMPILER_PACK_SET(1)
COMPILER_WORD_ALIGNED
union {
UsbDeviceDescriptor usb_endpoint_table[USB_EPT_NUM];
} usb_descriptor_table;
COMPILER_PACK_RESET()
static struct usb_module *_usb_instances;
static uint32_t device_callback_lpm_wakeup_enable;
static struct usb_endpoint_callback_parameter ep_callback_para;
static const uint16_t _usb_device_irq_bits[USB_DEVICE_CALLBACK_N] = {
USB_DEVICE_INTFLAG_SOF, USB_DEVICE_INTFLAG_EORST, USB_DEVICE_INTFLAG_WAKEUP | USB_DEVICE_INTFLAG_EORSM | USB_DEVICE_INTFLAG_UPRSM, USB_DEVICE_INTFLAG_RAMACER, USB_DEVICE_INTFLAG_SUSPEND, USB_DEVICE_INTFLAG_LPMNYET, USB_DEVICE_INTFLAG_LPMSUSP,
};
static const uint8_t _usb_endpoint_irq_bits[USB_DEVICE_EP_CALLBACK_N] = {USB_DEVICE_EPINTFLAG_TRCPT_Msk, USB_DEVICE_EPINTFLAG_TRFAIL_Msk, USB_DEVICE_EPINTFLAG_RXSTP, USB_DEVICE_EPINTFLAG_STALL_Msk};
enum status_code usb_device_register_callback(struct usb_module *module_inst, enum usb_device_callback callback_type, usb_device_callback_t callback_func) {
Assert(module_inst);
Assert(callback_func);
module_inst->device_callback[callback_type] = callback_func;
module_inst->device_registered_callback_mask |= _usb_device_irq_bits[callback_type];
return STATUS_OK;
}
enum status_code usb_device_unregister_callback(struct usb_module *module_inst, enum usb_device_callback callback_type) {
Assert(module_inst);
module_inst->device_callback[callback_type] = NULL;
module_inst->device_registered_callback_mask &= ~_usb_device_irq_bits[callback_type];
return STATUS_OK;
}
enum status_code usb_device_enable_callback(struct usb_module *module_inst, enum usb_device_callback callback_type) {
Assert(module_inst);
Assert(module_inst->hw);
module_inst->hw->DEVICE.INTFLAG.reg = _usb_device_irq_bits[callback_type];
module_inst->device_enabled_callback_mask |= _usb_device_irq_bits[callback_type];
module_inst->hw->DEVICE.INTENSET.reg = _usb_device_irq_bits[callback_type];
return STATUS_OK;
}
enum status_code usb_device_disable_callback(struct usb_module *module_inst, enum usb_device_callback callback_type) {
Assert(module_inst);
Assert(module_inst->hw);
module_inst->device_enabled_callback_mask &= ~_usb_device_irq_bits[callback_type];
module_inst->hw->DEVICE.INTENCLR.reg = _usb_device_irq_bits[callback_type];
return STATUS_OK;
}
enum status_code usb_device_endpoint_register_callback(struct usb_module *module_inst, uint8_t ep_num, enum usb_device_endpoint_callback callback_type, usb_device_endpoint_callback_t callback_func) {
Assert(module_inst);
Assert(ep_num < USB_EPT_NUM);
Assert(callback_func);
module_inst->device_endpoint_callback[ep_num][callback_type] = callback_func;
module_inst->device_endpoint_registered_callback_mask[ep_num] |= _usb_endpoint_irq_bits[callback_type];
return STATUS_OK;
}
enum status_code usb_device_endpoint_unregister_callback(struct usb_module *module_inst, uint8_t ep_num, enum usb_device_endpoint_callback callback_type) {
Assert(module_inst);
Assert(ep_num < USB_EPT_NUM);
module_inst->device_endpoint_callback[ep_num][callback_type] = NULL;
module_inst->device_endpoint_registered_callback_mask[ep_num] &= ~_usb_endpoint_irq_bits[callback_type];
return STATUS_OK;
}
enum status_code usb_device_endpoint_enable_callback(struct usb_module *module_inst, uint8_t ep, enum usb_device_endpoint_callback callback_type) {
Assert(module_inst);
Assert(module_inst->hw);
uint8_t ep_num = ep & USB_EP_ADDR_MASK;
Assert(ep_num < USB_EPT_NUM);
module_inst->device_endpoint_enabled_callback_mask[ep_num] |= _usb_endpoint_irq_bits[callback_type];
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRCPT) {
if (ep_num == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0 | USB_DEVICE_EPINTENSET_TRCPT1;
} else if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0;
}
}
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL) {
if (ep_num == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRFAIL0 | USB_DEVICE_EPINTENSET_TRFAIL1;
} else if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRFAIL1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRFAIL0;
}
}
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_RXSTP) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_RXSTP;
}
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_STALL) {
if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_STALL1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_STALL0;
}
}
return STATUS_OK;
}
enum status_code usb_device_endpoint_disable_callback(struct usb_module *module_inst, uint8_t ep, enum usb_device_endpoint_callback callback_type) {
Assert(module_inst);
Assert(module_inst->hw);
uint8_t ep_num = ep & USB_EP_ADDR_MASK;
Assert(ep_num < USB_EPT_NUM);
module_inst->device_endpoint_enabled_callback_mask[ep_num] &= ~_usb_endpoint_irq_bits[callback_type];
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRCPT) {
if (ep_num == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT0 | USB_DEVICE_EPINTENCLR_TRCPT1;
} else if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT0;
}
}
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL) {
if (ep_num == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRFAIL0 | USB_DEVICE_EPINTENCLR_TRFAIL1;
} else if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRFAIL1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRFAIL0;
}
}
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_RXSTP) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_RXSTP;
}
if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_STALL) {
if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_STALL1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_STALL0;
}
}
return STATUS_OK;
}
void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config) {
Assert(ep_config);
ep_config->ep_address = 0;
ep_config->ep_size = USB_ENDPOINT_8_BYTE;
ep_config->auto_zlp = false;
ep_config->ep_type = USB_DEVICE_ENDPOINT_TYPE_CONTROL;
}
enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, struct usb_device_endpoint_config *ep_config) {
Assert(module_inst);
Assert(ep_config);
uint8_t ep_num = ep_config->ep_address & USB_EP_ADDR_MASK;
uint8_t ep_bank = (ep_config->ep_address & USB_EP_DIR_IN) ? 1 : 0;
switch (ep_config->ep_type) {
case USB_DEVICE_ENDPOINT_TYPE_DISABLE:
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0) | USB_DEVICE_EPCFG_EPTYPE1(0);
return STATUS_OK;
case USB_DEVICE_ENDPOINT_TYPE_CONTROL:
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0 && (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(1) | USB_DEVICE_EPCFG_EPTYPE1(1);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
} else {
return STATUS_ERR_DENIED;
}
if (true == ep_config->auto_zlp) {
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.reg |= USB_DEVICE_PCKSIZE_AUTO_ZLP;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.reg |= USB_DEVICE_PCKSIZE_AUTO_ZLP;
} else {
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.reg &= ~USB_DEVICE_PCKSIZE_AUTO_ZLP;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.reg &= ~USB_DEVICE_PCKSIZE_AUTO_ZLP;
}
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.bit.SIZE = ep_config->ep_size;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.bit.SIZE = ep_config->ep_size;
return STATUS_OK;
case USB_DEVICE_ENDPOINT_TYPE_ISOCHRONOUS:
if (ep_bank) {
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE1(2);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
} else {
return STATUS_ERR_DENIED;
}
} else {
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE0(2);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
} else {
return STATUS_ERR_DENIED;
}
}
break;
case USB_DEVICE_ENDPOINT_TYPE_BULK:
if (ep_bank) {
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE1(3);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
} else {
return STATUS_ERR_DENIED;
}
} else {
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE0(3);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
} else {
return STATUS_ERR_DENIED;
}
}
break;
case USB_DEVICE_ENDPOINT_TYPE_INTERRUPT:
if (ep_bank) {
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE1(4);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
} else {
return STATUS_ERR_DENIED;
}
} else {
if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE0(4);
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
} else {
return STATUS_ERR_DENIED;
}
}
break;
default:
break;
}
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[ep_bank].PCKSIZE.bit.SIZE = ep_config->ep_size;
if (true == ep_config->auto_zlp) {
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[ep_bank].PCKSIZE.reg |= USB_DEVICE_PCKSIZE_AUTO_ZLP;
} else {
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[ep_bank].PCKSIZE.reg &= ~USB_DEVICE_PCKSIZE_AUTO_ZLP;
}
return STATUS_OK;
}
bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep) {
uint8_t ep_num = ep & USB_EP_ADDR_MASK;
uint8_t flag;
if (ep & USB_EP_DIR_IN) {
flag = (uint8_t)(module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.bit.EPTYPE1);
} else {
flag = (uint8_t)(module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.bit.EPTYPE0);
}
return ((enum usb_device_endpoint_type)(flag) != USB_DEVICE_ENDPOINT_TYPE_DISABLE);
}
void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep) {
uint8_t ep_num;
ep_num = ep & USB_EP_ADDR_MASK;
if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
}
}
bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep) {
uint8_t ep_num = ep & USB_EP_ADDR_MASK;
if (ep & USB_EP_DIR_IN) {
return (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUS.reg & USB_DEVICE_EPSTATUSSET_STALLRQ1);
} else {
return (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUS.reg & USB_DEVICE_EPSTATUSSET_STALLRQ0);
}
}
void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep) {
uint8_t ep_num = ep & USB_EP_ADDR_MASK;
if (ep & USB_EP_DIR_IN) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
} else {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
}
}
void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep) {
uint8_t ep_num = ep & USB_EP_ADDR_MASK;
if (ep & USB_EP_DIR_IN) {
if (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUS.reg & USB_DEVICE_EPSTATUSSET_STALLRQ1) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_STALLRQ1;
if (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_STALL1) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL1;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSSET_DTGLIN;
}
}
} else {
if (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUS.reg & USB_DEVICE_EPSTATUSSET_STALLRQ0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_STALLRQ0;
if (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_STALL0) {
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL0;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSSET_DTGLOUT;
}
}
}
}
enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst, uint8_t ep_num, uint8_t *pbuf, uint32_t buf_size) {
Assert(module_inst);
Assert(module_inst->hw);
Assert(ep_num < USB_EPT_NUM);
uint8_t flag;
flag = (uint8_t)(module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.bit.EPTYPE1);
if ((enum usb_device_endpoint_type)(flag) == USB_DEVICE_ENDPOINT_TYPE_DISABLE) {
return STATUS_ERR_DENIED;
};
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].ADDR.reg = (uint32_t)pbuf;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT = buf_size;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK1RDY;
return STATUS_OK;
}
enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst, uint8_t ep_num, uint8_t *pbuf, uint32_t buf_size) {
Assert(module_inst);
Assert(module_inst->hw);
Assert(ep_num < USB_EPT_NUM);
uint8_t flag;
flag = (uint8_t)(module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.bit.EPTYPE0);
if ((enum usb_device_endpoint_type)(flag) == USB_DEVICE_ENDPOINT_TYPE_DISABLE) {
return STATUS_ERR_DENIED;
};
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].ADDR.reg = (uint32_t)pbuf;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = buf_size;
usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY;
return STATUS_OK;
}
enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst, uint8_t *pbuf) {
Assert(module_inst);
Assert(module_inst->hw);
usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].ADDR.reg = (uint32_t)pbuf;
usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = 8;
usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
module_inst->hw->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY;
return STATUS_OK;
}
static void _usb_device_interrupt_handler(void) {
uint16_t ep_inst;
uint16_t flags, flags_run;
ep_inst = _usb_instances->hw->DEVICE.EPINTSMRY.reg;
if (0 == ep_inst) {
int i;
flags = _usb_instances->hw->DEVICE.INTFLAG.reg;
flags_run = flags & _usb_instances->device_enabled_callback_mask & _usb_instances->device_registered_callback_mask;
for (i = 0; i < USB_DEVICE_CALLBACK_N; i++) {
if (flags & _usb_device_irq_bits[i]) {
_usb_instances->hw->DEVICE.INTFLAG.reg = _usb_device_irq_bits[i];
}
if (flags_run & _usb_device_irq_bits[i]) {
if (i == USB_DEVICE_CALLBACK_LPMSUSP) {
device_callback_lpm_wakeup_enable = usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].EXTREG.bit.VARIABLE & USB_LPM_ATTRIBUT_REMOTEWAKE_MASK;
}
(_usb_instances->device_callback[i])(_usb_instances, &device_callback_lpm_wakeup_enable);
}
}
} else {
for (uint8_t i = 0; i < USB_EPT_NUM; i++) {
if (ep_inst & (1 << i)) {
flags = _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg;
flags_run = flags & _usb_instances->device_endpoint_enabled_callback_mask[i] & _usb_instances->device_endpoint_registered_callback_mask[i];
if (flags & USB_DEVICE_EPINTFLAG_STALL_Msk) {
if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_STALL1) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL1;
ep_callback_para.endpoint_address = USB_EP_DIR_IN | i;
} else if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_STALL0) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL0;
ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i;
}
if (flags_run & USB_DEVICE_EPINTFLAG_STALL_Msk) {
(_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_STALL])(_usb_instances, &ep_callback_para);
}
return;
}
if (flags & USB_DEVICE_EPINTFLAG_RXSTP) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP;
if (_usb_instances->device_endpoint_enabled_callback_mask[i] & _usb_endpoint_irq_bits[USB_DEVICE_ENDPOINT_CALLBACK_RXSTP]) {
ep_callback_para.received_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT);
(_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_RXSTP])(_usb_instances, &ep_callback_para);
}
return;
}
if (flags & USB_DEVICE_EPINTFLAG_TRCPT_Msk) {
if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
ep_callback_para.endpoint_address = USB_EP_DIR_IN | i;
ep_callback_para.sent_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT);
} else if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT0) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i;
ep_callback_para.received_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT);
ep_callback_para.out_buffer_size = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE);
}
if (flags_run & USB_DEVICE_EPINTFLAG_TRCPT_Msk) {
(_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_TRCPT])(_usb_instances, &ep_callback_para);
}
return;
}
if (flags & USB_DEVICE_EPINTFLAG_TRFAIL_Msk) {
if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRFAIL1) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRFAIL1;
if (usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[1].STATUS_BK.reg & USB_DEVICE_STATUS_BK_ERRORFLOW) {
usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[1].STATUS_BK.reg &= ~USB_DEVICE_STATUS_BK_ERRORFLOW;
}
ep_callback_para.endpoint_address = USB_EP_DIR_IN | i;
if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1) {
return;
}
} else if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRFAIL0) {
_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRFAIL0;
if (usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].STATUS_BK.reg & USB_DEVICE_STATUS_BK_ERRORFLOW) {
usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].STATUS_BK.reg &= ~USB_DEVICE_STATUS_BK_ERRORFLOW;
}
ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i;
if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT0) {
return;
}
}
if (flags_run & USB_DEVICE_EPINTFLAG_TRFAIL_Msk) {
(_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL])(_usb_instances, &ep_callback_para);
}
return;
}
}
}
}
}
void usb_enable(struct usb_module *module_inst) {
Assert(module_inst);
Assert(module_inst->hw);
module_inst->hw->DEVICE.CTRLA.reg |= USB_CTRLA_ENABLE;
while (module_inst->hw->DEVICE.SYNCBUSY.reg == USB_SYNCBUSY_ENABLE)
;
}
void usb_disable(struct usb_module *module_inst) {
Assert(module_inst);
Assert(module_inst->hw);
module_inst->hw->DEVICE.INTENCLR.reg = USB_DEVICE_INTENCLR_MASK;
module_inst->hw->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_MASK;
module_inst->hw->DEVICE.CTRLA.reg &= ~USB_CTRLA_ENABLE;
while (module_inst->hw->DEVICE.SYNCBUSY.reg == USB_SYNCBUSY_ENABLE)
;
}
void USB_0_Handler(void) {
if (_usb_instances->hw->DEVICE.CTRLA.bit.MODE) {
} else {
_usb_device_interrupt_handler();
}
}
void USB_1_Handler(void) { _usb_device_interrupt_handler(); }
void USB_2_Handler(void) { _usb_device_interrupt_handler(); }
void USB_3_Handler(void) { _usb_device_interrupt_handler(); }
void usb_get_config_defaults(struct usb_config *module_config) {
Assert(module_config);
Assert(module_config);
module_config->select_host_mode = 0;
module_config->run_in_standby = 1;
module_config->source_generator = 0;
module_config->speed_mode = USB_SPEED_FULL;
}
#define NVM_USB_PAD_TRANSN_POS 45
#define NVM_USB_PAD_TRANSN_SIZE 5
#define NVM_USB_PAD_TRANSP_POS 50
#define NVM_USB_PAD_TRANSP_SIZE 5
#define NVM_USB_PAD_TRIM_POS 55
#define NVM_USB_PAD_TRIM_SIZE 3
#define GCLK_USB 10
enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, struct usb_config *module_config) {
Assert(hw);
Assert(module_inst);
Assert(module_config);
uint32_t i, j;
uint32_t pad_transn, pad_transp, pad_trim;
Gclk * pgclk = GCLK;
Mclk * pmclk = MCLK;
Port * pport = PORT;
Oscctrl *posc = OSCCTRL;
_usb_instances = module_inst;
module_inst->hw = hw;
pmclk->AHBMASK.bit.USB_ = 1;
pmclk->APBBMASK.bit.USB_ = 1;
pport->Group[0].PMUX[12].reg = 0x77; pport->Group[0].PINCFG[24].bit.PMUXEN = 1;
pport->Group[0].PINCFG[25].bit.PMUXEN = 1;
pport->Group[1].PMUX[11].bit.PMUXE = 7; pport->Group[1].PINCFG[22].bit.PMUXEN = 1;
posc->DFLLCTRLA.bit.ENABLE = 0;
while (posc->DFLLSYNC.bit.ENABLE)
;
while (posc->DFLLSYNC.bit.DFLLCTRLB)
;
posc->DFLLCTRLB.bit.USBCRM = 1;
while (posc->DFLLSYNC.bit.DFLLCTRLB)
;
posc->DFLLCTRLB.bit.MODE = 1;
while (posc->DFLLSYNC.bit.DFLLCTRLB)
;
posc->DFLLCTRLB.bit.QLDIS = 0;
while (posc->DFLLSYNC.bit.DFLLCTRLB)
;
posc->DFLLCTRLB.bit.CCDIS = 1;
posc->DFLLMUL.bit.MUL = 0xbb80; while (posc->DFLLSYNC.bit.DFLLMUL)
;
posc->DFLLCTRLA.bit.ENABLE = 1;
while (posc->DFLLSYNC.bit.ENABLE)
;
pgclk->PCHCTRL[GCLK_USB].bit.GEN = 0;
pgclk->PCHCTRL[GCLK_USB].bit.CHEN = 1;
hw->DEVICE.CTRLA.bit.SWRST = 1;
while (hw->DEVICE.SYNCBUSY.bit.SWRST) {
}
USB->DEVICE.QOSCTRL.bit.CQOS = 2;
USB->DEVICE.QOSCTRL.bit.DQOS = 2;
pad_transn = (USB_FUSES_TRANSN_ADDR >> USB_FUSES_TRANSN_Pos) & USB_FUSES_TRANSN_Msk;
if (pad_transn == 0x1F) {
pad_transn = 5;
}
hw->DEVICE.PADCAL.bit.TRANSN = pad_transn;
pad_transp = (USB_FUSES_TRANSP_ADDR >> USB_FUSES_TRANSP_Pos) & USB_FUSES_TRANSP_Msk;
if (pad_transp == 0x1F) {
pad_transp = 29;
}
hw->DEVICE.PADCAL.bit.TRANSP = pad_transp;
pad_trim = (USB_FUSES_TRIM_ADDR >> USB_FUSES_TRIM_Pos) & USB_FUSES_TRIM_Msk;
if (pad_trim == 0x07) {
pad_trim = 3;
}
hw->DEVICE.PADCAL.bit.TRIM = pad_trim;
hw->DEVICE.CTRLA.bit.MODE = module_config->select_host_mode;
hw->DEVICE.CTRLA.bit.RUNSTDBY = module_config->run_in_standby;
hw->DEVICE.DESCADD.reg = (uint32_t)(&usb_descriptor_table.usb_endpoint_table[0]);
if (USB_SPEED_FULL == module_config->speed_mode) {
module_inst->hw->DEVICE.CTRLB.bit.SPDCONF = USB_DEVICE_CTRLB_SPDCONF_FS_Val;
} else if (USB_SPEED_LOW == module_config->speed_mode) {
module_inst->hw->DEVICE.CTRLB.bit.SPDCONF = USB_DEVICE_CTRLB_SPDCONF_LS_Val;
}
memset((uint8_t *)(&usb_descriptor_table.usb_endpoint_table[0]), 0, sizeof(usb_descriptor_table.usb_endpoint_table));
for (i = 0; i < USB_DEVICE_CALLBACK_N; i++) {
module_inst->device_callback[i] = NULL;
}
for (i = 0; i < USB_EPT_NUM; i++) {
for (j = 0; j < USB_DEVICE_EP_CALLBACK_N; j++) {
module_inst->device_endpoint_callback[i][j] = NULL;
}
}
module_inst->device_registered_callback_mask = 0;
module_inst->device_enabled_callback_mask = 0;
for (j = 0; j < USB_EPT_NUM; j++) {
module_inst->device_endpoint_registered_callback_mask[j] = 0;
module_inst->device_endpoint_enabled_callback_mask[j] = 0;
}
NVIC_EnableIRQ(USB_0_IRQn);
NVIC_EnableIRQ(USB_2_IRQn);
NVIC_EnableIRQ(USB_3_IRQn);
return STATUS_OK;
}