diff options
| author | Dien-Nhung Nguyen-Phu <kein@kienlab.com> | 2024-07-11 20:53:54 +0700 |
|---|---|---|
| committer | Dien-Nhung Nguyen-Phu <kein@kienlab.com> | 2024-07-11 22:11:21 +0700 |
| commit | a57090ead973037c0de38aa67b55283e8dd776cb (patch) | |
| tree | 5b425b37c27ccba612f516fd5206c23d14ae3624 /src | |
| parent | b25d62d1362cfd711e6617a9212e3eddc4e9fddd (diff) | |
usb: enable debug log over uart
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 12 | ||||
| -rw-r--r-- | src/usb/debug.c | 82 | ||||
| -rw-r--r-- | src/usb/debug.h | 14 |
3 files changed, 108 insertions, 0 deletions
@@ -123,10 +123,22 @@ void handle_mode_transition() prev_mode = mode;
}
+static void debug_init()
+{
+ GPIOA_SetBits(GPIO_Pin_9);
+ GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);
+ GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);
+ UART1_DefInit();
+ UART1_BaudRateCfg(921600);
+}
+
int main()
{
SetSysClock(CLK_SOURCE_PLL_60MHz);
+ debug_init();
+ PRINT("\nDebug console is on UART%d\n", DEBUG);
+
led_init();
TMR0_TimerInit(SCAN_T / 2);
TMR0_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
diff --git a/src/usb/debug.c b/src/usb/debug.c new file mode 100644 index 0000000..0c9f443 --- /dev/null +++ b/src/usb/debug.c @@ -0,0 +1,82 @@ +#include "CH58x_common.h" + +void print_setuppk(USB_SETUP_REQ *request) +{ + PRINT("Setup request:\n"); + + char *dir[] = {"OUT", "IN"}; + char *type[] = {"Standard", "Class", "Vendor", "Reserved Type"}; + char *recipient[] = { + "Device", "Interface", "Endpoint", "Other","Reserved Recipient" + }; + char *bRequest[] = { + "GET_STATUS", + "CLEAR_FEATURE", + "Reserved", + "SET_FEATURE", + "Reserved", + "SET_ADDRESS", + "GET_DESCRIPTOR", + "SET_DESCRIPTOR", + "GET_CONFIGURATION", + "SET_CONFIGURATION", + "GET_INTERFACE", + "SET_INTERFACE", + "SYNCH_FRAME", + }; + + uint8_t t = request->bRequestType; + uint8_t req = request->bRequest; + uint8_t type_val = (t & USB_REQ_TYP_MASK) >> 5; + uint8_t recip_val = t & USB_REQ_RECIP_MASK; + if (recip_val > 4) + recip_val = 4; + + PRINT("\t- bRequestType: 0x%02x (%s|%s|%s)\n", t, dir[t>>7], + type[type_val], + recipient[recip_val]); + + PRINT("\t- bRequest: 0x%02x (%s)\n", req, req > 12 ? + "N/A" : bRequest[req]); + PRINT("\t- wValue: 0x%04x\n", request->wValue); + + PRINT("\t- wIndex%s: 0x%04x\n", + recip_val == USB_REQ_RECIP_INTERF ? " (wInterfaceNumber)" : "", + request->wIndex); + + PRINT("\t- wLength: 0x%04x\n", request->wLength); +} + +void print_status_reg() +{ + char *token_type[] = { + "OUT", "SOF","IN", "SETUP", + }; + uint8_t reg = R8_USB_INT_ST; + uint8_t is_setup = reg & RB_UIS_SETUP_ACT; + uint8_t toggle = reg & RB_UIS_TOG_OK; + uint8_t token = (reg & MASK_UIS_TOKEN) >> 4; + uint8_t ep_num = reg & MASK_UIS_ENDP; + PRINT("usb: Status reg: 0x%02x (%s|%s|%s|EP:%d)\n", reg, + is_setup ? "SETUP??" : "0", + toggle ? "TOGGLE OK" : "0", token_type[token], ep_num); +} + +void print_intflag_reg() +{ + uint8_t reg = R8_USB_INT_FG; + uint8_t is_nak = reg & RB_U_IS_NAK; + uint8_t tog_ok = reg & RB_U_TOG_OK; + uint8_t sie = reg & RB_U_SIE_FREE; + uint8_t overflow = reg & RB_UIF_FIFO_OV; + uint8_t suspend = reg & RB_UIF_SUSPEND; + uint8_t xfer_complete = reg & RB_UIF_TRANSFER; + uint8_t bus_reset = reg & RB_UIF_BUS_RST; + PRINT("usb: Interrupt reg: 0x%02x (%s|%s|%s|%s|%s|%s)\n", reg, + is_nak ? "NAK received" : "0", + tog_ok ? "Toggle ok" : "0", + sie ? "SIE" : "0", + overflow ? "FIFO overflow" : "0", + suspend ? "Suspend" : "0", + xfer_complete ? "Xfer completed" : "0"); +}
\ No newline at end of file diff --git a/src/usb/debug.h b/src/usb/debug.h new file mode 100644 index 0000000..1279115 --- /dev/null +++ b/src/usb/debug.h @@ -0,0 +1,14 @@ +#ifndef __DEBUG_H__ +#define __DEBUG_H__ + +#include "CH58x_common.h" + + +#define _TRACE() PRINT("> "); PRINT(__FILE__);PRINT(": "); PRINT(__func__); \ + PRINT("()\n") + +void print_setuppk(USB_SETUP_REQ *setup_req_pk); +void print_status_reg(); +void print_intflag_reg(); + +#endif /* __DEBUG_H__ */ |