Skip to content

Commit

Permalink
include self-powered status
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossless committed Nov 23, 2023
1 parent 581bf67 commit 69df71d
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
#define IEP0CNT_SET(COUNT) \
do { IEP0CNT_CLR; IEP0CNT |= COUNT; } while (0)

#define IEP1CNT_CLR \
do { IEP1CNT &= ~0x1f; } while(0)
#define IEP1CNT_SET(COUNT) \
do { IEP1CNT_CLR; IEP1CNT |= COUNT; } while (0)

const uint8_t hid_report_desc_keyboard[] = {
// *INDENT-OFF*
HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop Controls
Expand Down Expand Up @@ -355,28 +360,30 @@ static void usb_hid_get_protocol_handler(__xdata struct usb_req_setup *req);
*/
__xdata uint8_t scratch[512];

// TODO: hide this away or get rid of it
uint16_t ep0_xfer_bytes_left;
uint8_t *ep0_xfer_src;

// usb state
uint8_t __xdata received_usb_addr;
uint8_t __xdata active_configution;
uint8_t __xdata interface0_protocol;
uint8_t __xdata interface1_protocol;
__bit usb_remote_wakeup;
uint8_t __xdata idle_time;
uint8_t __xdata active_configution;
uint8_t __xdata ep0_xfer_state;

// TODO: remove
volatile __bit device_remote_wakeup_f;

// TODO: rework this
uint8_t __xdata ep0_xfer_state;

void usb_init()
{
received_usb_addr = 0;
idle_time = 0;
active_configution = 0;
interface0_protocol = 0;
interface1_protocol = 0;
active_configution = 0;
usb_remote_wakeup = 0;
idle_time = 0;

ep0_xfer_state = 0;
ep0_xfer_bytes_left = 0;
ep0_xfer_src = 0;
Expand All @@ -392,7 +399,7 @@ void usb_send_report(report_keyboard_t *report)
{
set_ep1_in_buffer(report->raw, KEYBOARD_REPORT_SIZE);

IEP1CNT = (1 << 3);
IEP1CNT_SET(8);
IN1_SET_READY;
}

Expand Down Expand Up @@ -635,10 +642,10 @@ void usb_interrupt_handler() __interrupt(_INT_USB)
static void usb_set_address_handler(__xdata struct usb_req_setup *req)
{
ep0_xfer_state = NEW_SETUP_PHASE;
IEP0CNT_CLR;

received_usb_addr = req->wValue;

IEP0CNT_CLR;
OUT0_SET_STALL;
IN0_SET_READY;
}
Expand All @@ -648,7 +655,9 @@ static void usb_clear_remote_wakeup_handler(__xdata struct usb_req_setup *req)
ep0_xfer_state = NEW_SETUP_PHASE;

if ((req->bmRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
device_remote_wakeup_f = 0;
if (req->wValue == USB_FEAT_DEVICE_REMOTE_WAKEUP) {
usb_remote_wakeup = 0;
}
}

IEP0CNT_CLR;
Expand All @@ -660,7 +669,9 @@ static void usb_set_remote_wakeup_handler(__xdata struct usb_req_setup *req)
ep0_xfer_state = NEW_SETUP_PHASE;

if ((req->bmRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
device_remote_wakeup_f = 1;
if (req->wValue == USB_FEAT_DEVICE_REMOTE_WAKEUP) {
usb_remote_wakeup = 1;
}
}

IEP0CNT_CLR;
Expand All @@ -672,16 +683,18 @@ static void usb_clear_endpoint_halt_handler(__xdata struct usb_req_setup *req)
ep0_xfer_state = NEW_SETUP_PHASE;

if ((req->bmRequestType & USB_RECIP_MASK) == USB_RECIP_ENDPT) {
if (req->wIndex == 0) {
IN0_CANCEL_STALL;
OUT0_CANCEL_STALL;
} else if (req->wIndex == 0x81) {
IN1_CANCEL_STALL;
} else if (req->wIndex == 0x82) {
IN2_CANCEL_STALL;
} else {
STALL_EP0();
return;
if (req->wValue == USB_FEAT_ENDPOINT_HALT) {
if (req->wIndex == 0) {
IN0_CANCEL_STALL;
OUT0_CANCEL_STALL;
} else if (req->wIndex == 0x81) {
IN1_CANCEL_STALL;
} else if (req->wIndex == 0x82) {
IN2_CANCEL_STALL;
} else {
STALL_EP0();
return;
}
}
}

Expand Down Expand Up @@ -763,7 +776,8 @@ static void usb_get_device_status_handler(__xdata struct usb_req_setup *req)
ep0_xfer_state = OUT0_STATUS_PHASE;

if ((req->bmRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
if (device_remote_wakeup_f) {
// TODO: include self powered status
if (usb_remote_wakeup) {
EP0_IN_BUF[0] = 0x02;
} else {
EP0_IN_BUF[0] = 0x00;
Expand Down

0 comments on commit 69df71d

Please sign in to comment.