summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2022-01-15 17:07:54 +0800
committersakumisu <[email protected]>2022-01-15 17:07:54 +0800
commit95f3b81cfd375ba82cca90b601c5d835b7d2fddd (patch)
treea74d3e7a8836fd312a559445c82fdad171e913b5
parent89c0714fd4e6339f16de04519aa6f2b32da4881c (diff)
add usb debug level control and color display
-rw-r--r--class/audio/usbd_audio.c28
-rw-r--r--class/cdc/usbd_cdc.c12
-rw-r--r--class/hid/usbd_hid.c14
-rw-r--r--class/msc/usbd_msc.c64
-rw-r--r--class/video/usbd_video.c6
-rw-r--r--common/usb_util.h67
-rw-r--r--core/usbd_core.c100
7 files changed, 159 insertions, 132 deletions
diff --git a/class/audio/usbd_audio.c b/class/audio/usbd_audio.c
index 522497c1..521209c2 100644
--- a/class/audio/usbd_audio.c
+++ b/class/audio/usbd_audio.c
@@ -1,25 +1,3 @@
-/**
- * @file usbd_audio.c
- * @brief
- *
- * Copyright (c) 2021 sakumisu
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership. The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- */
#include "usbd_core.h"
#include "usbd_audio.h"
@@ -27,7 +5,7 @@ struct usbd_audio_control_info audio_control_info = { 0xdb00, 0x0000, 0x0100, 0x
int audio_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("AUDIO Class request: "
+ USB_LOG_DBG("AUDIO Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
@@ -46,7 +24,7 @@ int audio_class_request_handler(struct usb_setup_packet *setup, uint8_t **data,
vol = (audio_control_info.vol_current - 0xDB00 + 1) * 100 / (0xFFFF - 0xDB00);
}
usbd_audio_set_volume(vol);
- USBD_LOG_INFO("current audio volume:%d\r\n", vol);
+ USB_LOG_INFO("current audio volume:%d\r\n", vol);
}
}
@@ -89,7 +67,7 @@ int audio_class_request_handler(struct usb_setup_packet *setup, uint8_t **data,
break;
default:
- USBD_LOG_WRN("Unhandled Audio Class bRequest 0x%02x\r\n", setup->bRequest);
+ USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
diff --git a/class/cdc/usbd_cdc.c b/class/cdc/usbd_cdc.c
index 7c049e1c..c93e9f86 100644
--- a/class/cdc/usbd_cdc.c
+++ b/class/cdc/usbd_cdc.c
@@ -2,7 +2,7 @@
* @file usbd_cdc.c
* @brief
*
- * Copyright (c) 2021 sakumisu
+ * Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -65,7 +65,7 @@ static void usbd_cdc_acm_reset(void)
*/
static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("CDC Class request: "
+ USB_LOG_DBG("CDC Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
@@ -95,7 +95,7 @@ static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t
}
memcpy(&usbd_cdc_acm_cfg.line_coding, *data, sizeof(usbd_cdc_acm_cfg.line_coding));
- USBD_LOG_DBG("CDC_SET_LINE_CODING <%d %d %s %s>\r\n",
+ USB_LOG_DBG("CDC_SET_LINE_CODING <%d %d %s %s>\r\n",
usbd_cdc_acm_cfg.line_coding.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bDataBits,
parity_name[usbd_cdc_acm_cfg.line_coding.bParityType],
@@ -108,7 +108,7 @@ static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t
usbd_cdc_acm_cfg.line_state = (uint8_t)setup->wValue;
bool dtr = (setup->wValue & 0x01);
bool rts = (setup->wValue & 0x02);
- USBD_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n",
+ USB_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n",
dtr, rts);
usbd_cdc_acm_set_dtr(dtr);
usbd_cdc_acm_set_rts(rts);
@@ -117,7 +117,7 @@ static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t
case CDC_REQUEST_GET_LINE_CODING:
*data = (uint8_t *)(&usbd_cdc_acm_cfg.line_coding);
*len = sizeof(usbd_cdc_acm_cfg.line_coding);
- USBD_LOG_DBG("CDC_GET_LINE_CODING %d %d %d %d\r\n",
+ USB_LOG_DBG("CDC_GET_LINE_CODING %d %d %d %d\r\n",
usbd_cdc_acm_cfg.line_coding.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bCharFormat,
usbd_cdc_acm_cfg.line_coding.bParityType,
@@ -125,7 +125,7 @@ static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t
break;
default:
- USBD_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest);
+ USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
diff --git a/class/hid/usbd_hid.c b/class/hid/usbd_hid.c
index e55e30cb..e7b34fff 100644
--- a/class/hid/usbd_hid.c
+++ b/class/hid/usbd_hid.c
@@ -2,7 +2,7 @@
* @file usbd_hid.c
* @brief
*
- * Copyright (c) 2021 sakumisu
+ * Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -63,7 +63,7 @@ static void usbd_hid_reset(void)
int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("HID Custom request: "
+ USB_LOG_DBG("HID Custom request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
@@ -90,19 +90,19 @@ int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, u
switch (value) {
case HID_DESCRIPTOR_TYPE_HID:
- USBD_LOG_INFO("get HID Descriptor\r\n");
+ USB_LOG_INFO("get HID Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_descriptor;
*len = current_hid_intf->hid_descriptor[0];
break;
case HID_DESCRIPTOR_TYPE_HID_REPORT:
- USBD_LOG_INFO("get Report Descriptor\r\n");
+ USB_LOG_INFO("get Report Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_report_descriptor;
*len = current_hid_intf->hid_report_descriptor_len;
break;
case HID_DESCRIPTOR_TYPE_HID_PHYSICAL:
- USBD_LOG_INFO("get PHYSICAL Descriptor\r\n");
+ USB_LOG_INFO("get PHYSICAL Descriptor\r\n");
break;
@@ -118,7 +118,7 @@ int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, u
int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("HID Class request: "
+ USB_LOG_DBG("HID Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
@@ -180,7 +180,7 @@ int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, ui
break;
default:
- USBD_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
+ USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
diff --git a/class/msc/usbd_msc.c b/class/msc/usbd_msc.c
index 71a9c03c..98f820fa 100644
--- a/class/msc/usbd_msc.c
+++ b/class/msc/usbd_msc.c
@@ -2,7 +2,7 @@
* @file usbd_msc.c
* @brief
*
- * Copyright (c) 2021 sakumisu
+ * Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -102,16 +102,16 @@ static void usbd_msc_reset(void)
*/
static int msc_storage_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("MSC Class request: "
+ USB_LOG_DBG("MSC Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case MSC_REQUEST_RESET:
- USBD_LOG_DBG("MSC_REQUEST_RESET\r\n");
+ USB_LOG_DBG("MSC_REQUEST_RESET\r\n");
if (setup->wLength) {
- USBD_LOG_WRN("Invalid length\r\n");
+ USB_LOG_WRN("Invalid length\r\n");
return -1;
}
@@ -119,10 +119,10 @@ static int msc_storage_class_request_handler(struct usb_setup_packet *setup, uin
break;
case MSC_REQUEST_GET_MAX_LUN:
- USBD_LOG_DBG("MSC_REQUEST_GET_MAX_LUN\r\n");
+ USB_LOG_DBG("MSC_REQUEST_GET_MAX_LUN\r\n");
if (setup->wLength != 1) {
- USBD_LOG_WRN("Invalid length\r\n");
+ USB_LOG_WRN("Invalid length\r\n");
return -1;
}
@@ -131,7 +131,7 @@ static int msc_storage_class_request_handler(struct usb_setup_packet *setup, uin
break;
default:
- USBD_LOG_WRN("Unhandled MSC Class bRequest 0x%02x\r\n", setup->bRequest);
+ USB_LOG_WRN("Unhandled MSC Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
@@ -158,7 +158,7 @@ static void sendCSW(uint8_t CSW_Status)
if (usbd_ep_write(mass_ep_data[MSD_IN_EP_IDX].ep_addr, (uint8_t *)&usbd_msc_cfg.csw,
sizeof(struct CSW), NULL) != 0) {
- USBD_LOG_ERR("usb write failure\r\n");
+ USB_LOG_ERR("usb write failure\r\n");
}
}
@@ -172,7 +172,7 @@ static void sendLastData(uint8_t *buffer, uint8_t size)
usbd_msc_cfg.stage = MSC_SEND_CSW;
if (usbd_ep_write(mass_ep_data[MSD_IN_EP_IDX].ep_addr, buffer, size, NULL) != 0) {
- USBD_LOG_ERR("USB write failed\r\n");
+ USB_LOG_ERR("USB write failed\r\n");
}
usbd_msc_cfg.csw.dDataResidue -= size;
@@ -215,7 +215,7 @@ static bool SCSI_processRead(void)
{
uint32_t transfer_len;
- USBD_LOG_DBG("read addr:%d\r\n", usbd_msc_cfg.scsi_blk_addr);
+ USB_LOG_DBG("read addr:%d\r\n", usbd_msc_cfg.scsi_blk_addr);
transfer_len = MIN(usbd_msc_cfg.scsi_blk_len, MASS_STORAGE_BULK_EP_MPS);
@@ -244,7 +244,7 @@ static bool SCSI_processRead(void)
static bool SCSI_processWrite()
{
uint32_t bytes_read;
- USBD_LOG_DBG("write addr:%d\r\n", usbd_msc_cfg.scsi_blk_addr);
+ USB_LOG_DBG("write addr:%d\r\n", usbd_msc_cfg.scsi_blk_addr);
/* we fill an array in RAM of 1 block before writing it in memory */
usbd_ep_read(mass_ep_data[MSD_OUT_EP_IDX].ep_addr, &usbd_msc_cfg.block_buffer[usbd_msc_cfg.scsi_blk_addr % usbd_msc_cfg.scsi_blk_size], MASS_STORAGE_BULK_EP_MPS,
@@ -275,7 +275,7 @@ static bool SCSI_processVerify()
uint32_t bytes_read;
uint8_t out_buffer[MASS_STORAGE_BULK_EP_MPS];
- USBD_LOG_DBG("verify addr:%d\r\n", usbd_msc_cfg.scsi_blk_addr);
+ USB_LOG_DBG("verify addr:%d\r\n", usbd_msc_cfg.scsi_blk_addr);
/* we fill an array in RAM of 1 block before writing it in memory */
usbd_ep_read(mass_ep_data[MSD_OUT_EP_IDX].ep_addr, out_buffer, MASS_STORAGE_BULK_EP_MPS,
@@ -292,7 +292,7 @@ static bool SCSI_processVerify()
/* info are in RAM -> no need to re-read memory */
for (uint16_t i = 0U; i < bytes_read; i++) {
if (usbd_msc_cfg.block_buffer[usbd_msc_cfg.scsi_blk_addr % usbd_msc_cfg.scsi_blk_size + i] != out_buffer[i]) {
- USBD_LOG_DBG("Mismatch sector %d offset %d",
+ USB_LOG_DBG("Mismatch sector %d offset %d",
usbd_msc_cfg.scsi_blk_addr / usbd_msc_cfg.scsi_blk_size, i);
memOK = false;
break;
@@ -321,7 +321,7 @@ static bool SCSI_CBWDecode()
&bytes_read);
if (bytes_read != sizeof(struct CBW)) {
- USBD_LOG_ERR("size != sizeof(cbw)\r\n");
+ USB_LOG_ERR("size != sizeof(cbw)\r\n");
SCSI_SenseCode(SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB);
return false;
}
@@ -379,7 +379,7 @@ static bool SCSI_CBWDecode()
default:
SCSI_SenseCode(SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INVALID_CDB);
- USBD_LOG_WRN("unsupported cmd:0x%02x\r\n", usbd_msc_cfg.cbw.CB[0]);
+ USB_LOG_WRN("unsupported cmd:0x%02x\r\n", usbd_msc_cfg.cbw.CB[0]);
ret = false;
break;
}
@@ -706,7 +706,7 @@ static bool SCSI_read10(uint8_t **data, uint32_t *len)
}
lba = GET_BE32(&usbd_msc_cfg.cbw.CB[2]);
- USBD_LOG_DBG("lba: 0x%x\r\n", lba);
+ USB_LOG_DBG("lba: 0x%x\r\n", lba);
usbd_msc_cfg.scsi_blk_addr = lba * usbd_msc_cfg.scsi_blk_size;
@@ -717,12 +717,12 @@ static bool SCSI_read10(uint8_t **data, uint32_t *len)
if ((lba + blk_num) > usbd_msc_cfg.scsi_blk_nbr) {
SCSI_SenseCode(SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ADDRESS_OUT_OF_RANGE);
- USBD_LOG_ERR("LBA out of range\r\n");
+ USB_LOG_ERR("LBA out of range\r\n");
return false;
}
if (usbd_msc_cfg.cbw.dDataLength != usbd_msc_cfg.scsi_blk_len) {
- USBD_LOG_ERR("scsi_blk_len does not match with dDataLength\r\n");
+ USB_LOG_ERR("scsi_blk_len does not match with dDataLength\r\n");
return false;
}
usbd_msc_cfg.stage = MSC_DATA_IN;
@@ -740,24 +740,24 @@ static bool SCSI_read12(uint8_t **data, uint32_t *len)
}
lba = GET_BE32(&usbd_msc_cfg.cbw.CB[2]);
- USBD_LOG_DBG("lba: 0x%x\r\n", lba);
+ USB_LOG_DBG("lba: 0x%x\r\n", lba);
usbd_msc_cfg.scsi_blk_addr = lba * usbd_msc_cfg.scsi_blk_size;
/* Number of Blocks to transfer */
blk_num = GET_BE32(&usbd_msc_cfg.cbw.CB[6]);
- USBD_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
+ USB_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
usbd_msc_cfg.scsi_blk_len = blk_num * usbd_msc_cfg.scsi_blk_size;
if ((lba + blk_num) > usbd_msc_cfg.scsi_blk_nbr) {
SCSI_SenseCode(SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ADDRESS_OUT_OF_RANGE);
- USBD_LOG_ERR("LBA out of range\r\n");
+ USB_LOG_ERR("LBA out of range\r\n");
return false;
}
if (usbd_msc_cfg.cbw.dDataLength != usbd_msc_cfg.scsi_blk_len) {
- USBD_LOG_ERR("scsi_blk_len does not match with dDataLength\r\n");
+ USB_LOG_ERR("scsi_blk_len does not match with dDataLength\r\n");
return false;
}
usbd_msc_cfg.stage = MSC_DATA_IN;
@@ -775,18 +775,18 @@ static bool SCSI_write10(uint8_t **data, uint32_t *len)
}
lba = GET_BE32(&usbd_msc_cfg.cbw.CB[2]);
- USBD_LOG_DBG("lba: 0x%x\r\n", lba);
+ USB_LOG_DBG("lba: 0x%x\r\n", lba);
usbd_msc_cfg.scsi_blk_addr = lba * usbd_msc_cfg.scsi_blk_size;
/* Number of Blocks to transfer */
blk_num = GET_BE16(&usbd_msc_cfg.cbw.CB[7]);
- USBD_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
+ USB_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
usbd_msc_cfg.scsi_blk_len = blk_num * usbd_msc_cfg.scsi_blk_size;
if ((lba + blk_num) > usbd_msc_cfg.scsi_blk_nbr) {
- USBD_LOG_ERR("LBA out of range\r\n");
+ USB_LOG_ERR("LBA out of range\r\n");
return false;
}
@@ -808,18 +808,18 @@ static bool SCSI_write12(uint8_t **data, uint32_t *len)
}
lba = GET_BE32(&usbd_msc_cfg.cbw.CB[2]);
- USBD_LOG_DBG("lba: 0x%x\r\n", lba);
+ USB_LOG_DBG("lba: 0x%x\r\n", lba);
usbd_msc_cfg.scsi_blk_addr = lba * usbd_msc_cfg.scsi_blk_size;
/* Number of Blocks to transfer */
blk_num = GET_BE32(&usbd_msc_cfg.cbw.CB[6]);
- USBD_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
+ USB_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
usbd_msc_cfg.scsi_blk_len = blk_num * usbd_msc_cfg.scsi_blk_size;
if ((lba + blk_num) > usbd_msc_cfg.scsi_blk_nbr) {
- USBD_LOG_ERR("LBA out of range\r\n");
+ USB_LOG_ERR("LBA out of range\r\n");
return false;
}
@@ -851,18 +851,18 @@ static bool SCSI_verify10(uint8_t **data, uint32_t *len)
}
lba = GET_BE32(&usbd_msc_cfg.cbw.CB[2]);
- USBD_LOG_DBG("lba: 0x%x\r\n", lba);
+ USB_LOG_DBG("lba: 0x%x\r\n", lba);
usbd_msc_cfg.scsi_blk_addr = lba * usbd_msc_cfg.scsi_blk_size;
/* Number of Blocks to transfer */
blk_num = GET_BE16(&usbd_msc_cfg.cbw.CB[7]);
- USBD_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
+ USB_LOG_DBG("num (block) : 0x%x\r\n", blk_num);
usbd_msc_cfg.scsi_blk_len = blk_num * usbd_msc_cfg.scsi_blk_size;
if ((lba + blk_num) > usbd_msc_cfg.scsi_blk_nbr) {
- USBD_LOG_ERR("LBA out of range\r\n");
+ USB_LOG_ERR("LBA out of range\r\n");
return false;
}
@@ -880,7 +880,7 @@ static void mass_storage_bulk_out(uint8_t ep)
switch (usbd_msc_cfg.stage) {
case MSC_READ_CBW:
if (SCSI_CBWDecode() == false) {
- USBD_LOG_ERR("Command:0x%02x decode err\r\n", usbd_msc_cfg.cbw.CB[0]);
+ USB_LOG_ERR("Command:0x%02x decode err\r\n", usbd_msc_cfg.cbw.CB[0]);
usbd_msc_bot_abort();
return;
}
diff --git a/class/video/usbd_video.c b/class/video/usbd_video.c
index a8ed3daf..337b87cb 100644
--- a/class/video/usbd_video.c
+++ b/class/video/usbd_video.c
@@ -1,7 +1,7 @@
/**
* @file usbd_video.c
*
- * Copyright (c) 2021 sakumisu
+ * Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -27,7 +27,7 @@ extern struct video_probe_and_commit_controls commit;
int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("Video Class request: "
+ USB_LOG_DBG("Video Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
@@ -90,7 +90,7 @@ int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data,
break;
default:
- USBD_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
+ USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
diff --git a/common/usb_util.h b/common/usb_util.h
index 4c2f6743..16f5a5c1 100644
--- a/common/usb_util.h
+++ b/common/usb_util.h
@@ -205,16 +205,65 @@
#define USB_DESC_SECTION __attribute__((section("usb_desc"))) __USED __ALIGNED(1)
-#if 0
-#define USBD_LOG_INFO(a, ...) printf(a, ##__VA_ARGS__)
-#define USBD_LOG_DBG(a, ...) printf(a, ##__VA_ARGS__)
-#define USBD_LOG_WRN(a, ...) printf(a, ##__VA_ARGS__)
-#define USBD_LOG_ERR(a, ...) printf(a, ##__VA_ARGS__)
+/* DEBUG level */
+#define USB_DBG_ERROR 0
+#define USB_DBG_WARNING 1
+#define USB_DBG_INFO 2
+#define USB_DBG_LOG 3
+
+#ifndef USB_DBG_LEVEL
+#define USB_DBG_LEVEL USB_DBG_INFO
+#endif
+
+#ifndef USB_DBG_TAG
+#define USB_DBG_TAG "USB"
+#endif
+/*
+ * The color for terminal (foreground)
+ * BLACK 30
+ * RED 31
+ * GREEN 32
+ * YELLOW 33
+ * BLUE 34
+ * PURPLE 35
+ * CYAN 36
+ * WHITE 37
+ */
+#define _USB_DBG_COLOR(n) printf("\033[" #n "m")
+#define _USB_DBG_LOG_HDR(lvl_name, color_n) \
+ printf("\033[" #color_n "m[" lvl_name "/" USB_DBG_TAG "] ")
+#define _USB_DBG_LOG_X_END \
+ printf("\033[0m")
+
+#define usb_dbg_log_line(lvl, color_n, fmt, ...) \
+ do { \
+ _USB_DBG_LOG_HDR(lvl, color_n); \
+ printf(fmt, ##__VA_ARGS__); \
+ _USB_DBG_LOG_X_END; \
+ } while (0)
+
+#if (USB_DBG_LEVEL >= USB_DBG_LOG)
+#define USB_LOG_DBG(fmt, ...) usb_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
+#else
+#define USB_LOG_DBG(...)
+#endif
+
+#if (USB_DBG_LEVEL >= USB_DBG_INFO)
+#define USB_LOG_INFO(fmt, ...) usb_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
+#else
+#define USB_LOG_INFO(...)
+#endif
+
+#if (USB_DBG_LEVEL >= USB_DBG_WARNING)
+#define USB_LOG_WRN(fmt, ...) usb_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
+#else
+#define USB_LOG_WRN(...)
+#endif
+
+#if (USB_DBG_LEVEL >= USB_DBG_ERROR)
+#define USB_LOG_ERR(fmt, ...) usb_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
#else
-#define USBD_LOG_INFO(a, ...) printf(a, ##__VA_ARGS__)
-#define USBD_LOG_DBG(a, ...)
-#define USBD_LOG_WRN(a, ...) printf(a, ##__VA_ARGS__)
-#define USBD_LOG_ERR(a, ...) printf(a, ##__VA_ARGS__)
+#define USB_LOG_ERR(...)
#endif
#endif
diff --git a/core/usbd_core.c b/core/usbd_core.c
index dd8166e7..100842d3 100644
--- a/core/usbd_core.c
+++ b/core/usbd_core.c
@@ -2,7 +2,7 @@
* @file usbd_core.c
* @brief
*
- * Copyright (c) 2021 sakumisu
+ * Copyright (c) 2021 Bouffalolab team
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -85,7 +85,7 @@ static struct usb_bos_descriptor *bos_desc;
*/
static void usbd_print_setup(struct usb_setup_packet *setup)
{
- USBD_LOG_INFO("Setup: "
+ USB_LOG_INFO("Setup: "
"bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
setup->bmRequestType,
setup->bRequest,
@@ -199,7 +199,7 @@ static bool usbd_set_endpoint(const struct usb_endpoint_descriptor *ep_desc)
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK;
- USBD_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n",
+ USB_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n",
ep_cfg.ep_addr, ep_cfg.ep_type, ep_cfg.ep_mps);
usbd_ep_open(&ep_cfg);
@@ -225,7 +225,7 @@ static bool usbd_reset_endpoint(const struct usb_endpoint_descriptor *ep_desc)
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK;
- USBD_LOG_INFO("Close endpoint:0x%x type:%u\r\n",
+ USB_LOG_INFO("Close endpoint:0x%x type:%u\r\n",
ep_cfg.ep_addr, ep_cfg.ep_type);
usbd_ep_close(ep_cfg.ep_addr);
@@ -257,7 +257,7 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l
index = GET_DESC_INDEX(type_index);
if ((type == USB_DESCRIPTOR_TYPE_STRING) && (index == USB_OSDESC_STRING_DESC_INDEX)) {
- USBD_LOG_INFO("read MS OS 2.0 descriptor string\r\n");
+ USB_LOG_INFO("read MS OS 2.0 descriptor string\r\n");
if (!msosv1_desc) {
return false;
@@ -268,7 +268,7 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l
return true;
} else if (type == USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE) {
- USBD_LOG_INFO("read BOS descriptor string\r\n");
+ USB_LOG_INFO("read BOS descriptor string\r\n");
if (!bos_desc) {
return false;
@@ -327,7 +327,7 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l
}
} else {
/* nothing found */
- USBD_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
+ USB_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
}
return found;
@@ -354,7 +354,7 @@ static bool usbd_set_configuration(uint8_t config_index, uint8_t alt_setting)
if (config_index == 0U) {
/* TODO: unconfigure device */
- USBD_LOG_ERR("Device not configured - invalid configuration\r\n");
+ USB_LOG_ERR("Device not configured - invalid configuration\r\n");
return true;
}
@@ -414,7 +414,7 @@ static bool usbd_set_interface(uint8_t iface, uint8_t alt_setting)
uint8_t cur_iface = 0xFF;
bool ret = false;
- USBD_LOG_DBG("iface %u alt_setting %u\r\n", iface, alt_setting);
+ USB_LOG_DBG("iface %u alt_setting %u\r\n", iface, alt_setting);
while (p[DESC_bLength] != 0U) {
switch (p[DESC_bDescriptorType]) {
@@ -428,7 +428,7 @@ static bool usbd_set_interface(uint8_t iface, uint8_t alt_setting)
if_desc = (void *)p;
}
- USBD_LOG_DBG("Current iface %u alt setting %u",
+ USB_LOG_DBG("Current iface %u alt setting %u",
cur_iface, cur_alt_setting);
break;
@@ -475,7 +475,7 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
- USBD_LOG_DBG("REQ_GET_STATUS\r\n");
+ USB_LOG_DBG("REQ_GET_STATUS\r\n");
/* bit 0: self-powered */
/* bit 1: remote wakeup */
*data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
@@ -484,7 +484,7 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
break;
case USB_REQUEST_CLEAR_FEATURE:
- USBD_LOG_DBG("REQ_CLEAR_FEATURE\r\n");
+ USB_LOG_DBG("REQ_CLEAR_FEATURE\r\n");
ret = false;
if (value == USB_FEATURE_REMOTE_WAKEUP) {
@@ -496,7 +496,7 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
break;
case USB_REQUEST_SET_FEATURE:
- USBD_LOG_DBG("REQ_SET_FEATURE\r\n");
+ USB_LOG_DBG("REQ_SET_FEATURE\r\n");
ret = false;
if (value == USB_FEATURE_REMOTE_WAKEUP) {
@@ -512,22 +512,22 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
break;
case USB_REQUEST_SET_ADDRESS:
- USBD_LOG_DBG("REQ_SET_ADDRESS, addr 0x%x\r\n", value);
+ USB_LOG_DBG("REQ_SET_ADDRESS, addr 0x%x\r\n", value);
usbd_set_address(value);
break;
case USB_REQUEST_GET_DESCRIPTOR:
- USBD_LOG_DBG("REQ_GET_DESCRIPTOR\r\n");
+ USB_LOG_DBG("REQ_GET_DESCRIPTOR\r\n");
ret = usbd_get_descriptor(value, data, len);
break;
case USB_REQUEST_SET_DESCRIPTOR:
- USBD_LOG_DBG("Device req 0x%02x not implemented\r\n", setup->bRequest);
+ USB_LOG_DBG("Device req 0x%02x not implemented\r\n", setup->bRequest);
ret = false;
break;
case USB_REQUEST_GET_CONFIGURATION:
- USBD_LOG_DBG("REQ_GET_CONFIGURATION\r\n");
+ USB_LOG_DBG("REQ_GET_CONFIGURATION\r\n");
/* indicate if we are configured */
*data = (uint8_t *)&usbd_core_cfg.configuration;
*len = 1;
@@ -535,10 +535,10 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
case USB_REQUEST_SET_CONFIGURATION:
value &= 0xFF;
- USBD_LOG_DBG("REQ_SET_CONFIGURATION, conf 0x%x\r\n", value);
+ USB_LOG_DBG("REQ_SET_CONFIGURATION, conf 0x%x\r\n", value);
if (!usbd_set_configuration(value, 0)) {
- USBD_LOG_DBG("USB Set Configuration failed\r\n");
+ USB_LOG_DBG("USB Set Configuration failed\r\n");
ret = false;
} else {
/* configuration successful,
@@ -557,7 +557,7 @@ static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t
break;
default:
- USBD_LOG_ERR("Illegal device req 0x%02x\r\n", setup->bRequest);
+ USB_LOG_ERR("Illegal device req 0x%02x\r\n", setup->bRequest);
ret = false;
break;
}
@@ -610,12 +610,12 @@ static bool usbd_std_interface_req_handler(struct usb_setup_packet *setup,
break;
case USB_REQUEST_SET_INTERFACE:
- USBD_LOG_DBG("REQ_SET_INTERFACE\r\n");
+ USB_LOG_DBG("REQ_SET_INTERFACE\r\n");
usbd_set_interface(setup->wIndex, setup->wValue);
break;
default:
- USBD_LOG_ERR("Illegal interface req 0x%02x\r\n", setup->bRequest);
+ USB_LOG_ERR("Illegal interface req 0x%02x\r\n", setup->bRequest);
return false;
}
@@ -671,7 +671,7 @@ static bool usbd_std_endpoint_req_handler(struct usb_setup_packet *setup, uint8_
* to accept the request.
*/
if (((ep & 0x7f) == 0) || is_device_configured()) {
- USBD_LOG_ERR("ep:%x clear halt\r\n", ep);
+ USB_LOG_ERR("ep:%x clear halt\r\n", ep);
usbd_ep_clear_stall(ep);
usbd_event_notify_handler(USBD_EVENT_CLEAR_HALT, NULL);
break;
@@ -692,7 +692,7 @@ static bool usbd_std_endpoint_req_handler(struct usb_setup_packet *setup, uint8_
*/
if (((ep & 0x7f) == 0) || is_device_configured()) {
/* set HALT by stalling */
- USBD_LOG_ERR("ep:%x set halt\r\n", ep);
+ USB_LOG_ERR("ep:%x set halt\r\n", ep);
usbd_ep_set_stall(ep);
usbd_event_notify_handler(USBD_EVENT_SET_HALT, NULL);
break;
@@ -707,13 +707,13 @@ static bool usbd_std_endpoint_req_handler(struct usb_setup_packet *setup, uint8_
/* For Synch Frame request the device must be configured */
if (is_device_configured()) {
/* Not supported, return false anyway */
- USBD_LOG_DBG("ep req 0x%02x not implemented\r\n", setup->bRequest);
+ USB_LOG_DBG("ep req 0x%02x not implemented\r\n", setup->bRequest);
}
return false;
default:
- USBD_LOG_ERR("Illegal ep req 0x%02x\r\n", setup->bRequest);
+ USB_LOG_ERR("Illegal ep req 0x%02x\r\n", setup->bRequest);
return false;
}
@@ -777,7 +777,7 @@ static int usbd_standard_request_handler(struct usb_setup_packet *setup, uint8_t
*/
static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
+ USB_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_INTERFACE) {
return -1;
@@ -813,7 +813,7 @@ static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t **
*/
static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("bRequest 0x%02x, wValue0x%04x, wIndex 0x%04x\r\n", setup->bRequest, setup->wValue, setup->wIndex);
+ USB_LOG_DBG("bRequest 0x%02x, wValue0x%04x, wIndex 0x%04x\r\n", setup->bRequest, setup->wValue, setup->wIndex);
// if((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_DEVICE)
// {
@@ -824,19 +824,19 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t *
if (setup->bRequest == msosv1_desc->vendor_code) {
switch (setup->wIndex) {
case 0x04:
- USBD_LOG_INFO("get Compat ID\r\n");
+ USB_LOG_INFO("get Compat ID\r\n");
*data = (uint8_t *)msosv1_desc->compat_id;
*len = msosv1_desc->compat_id_len;
return 0;
case 0x05:
- USBD_LOG_INFO("get Compat id properties\r\n");
+ USB_LOG_INFO("get Compat id properties\r\n");
*data = (uint8_t *)msosv1_desc->comp_id_property;
*len = msosv1_desc->comp_id_property_len;
return 0;
default:
- USBD_LOG_ERR("unknown vendor code\r\n");
+ USB_LOG_ERR("unknown vendor code\r\n");
return -1;
}
}
@@ -844,12 +844,12 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t *
if (setup->bRequest == msosv2_desc->vendor_code) {
switch (setup->wIndex) {
case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
- USBD_LOG_INFO("GET MS OS 2.0 Descriptor\r\n");
+ USB_LOG_INFO("GET MS OS 2.0 Descriptor\r\n");
*data = (uint8_t *)msosv2_desc->compat_id;
*len = msosv2_desc->compat_id_len;
return 0;
default:
- USBD_LOG_ERR("unknown vendor code\r\n");
+ USB_LOG_ERR("unknown vendor code\r\n");
return -1;
}
}
@@ -887,7 +887,7 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t *
*/
static int usbd_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
- USBD_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
+ USB_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_INTERFACE) {
return -1;
@@ -935,19 +935,19 @@ static bool usbd_setup_request_handler(struct usb_setup_packet *setup, uint8_t *
if (type == USB_REQUEST_STANDARD) {
if (usbd_standard_request_handler(setup, data, len) < 0) {
- USBD_LOG_ERR("Handler Error %d\r\n", type);
+ USB_LOG_ERR("Handler Error %d\r\n", type);
usbd_print_setup(setup);
return false;
}
} else if (type == USB_REQUEST_CLASS) {
if (usbd_class_request_handler(setup, data, len) < 0) {
- USBD_LOG_ERR("Handler Error %d\r\n", type);
+ USB_LOG_ERR("Handler Error %d\r\n", type);
usbd_print_setup(setup);
return false;
}
} else if (type == USB_REQUEST_VENDOR) {
if (usbd_vendor_request_handler(setup, data, len) < 0) {
- USBD_LOG_ERR("Handler Error %d\r\n", type);
+ USB_LOG_ERR("Handler Error %d\r\n", type);
usbd_print_setup(setup);
return false;
}
@@ -970,7 +970,7 @@ static void usbd_send_to_host(uint16_t len)
chunk = usbd_core_cfg.ep0_data_buf_residue;
if (usbd_ep_write(USB_CONTROL_IN_EP0, usbd_core_cfg.ep0_data_buf, usbd_core_cfg.ep0_data_buf_residue, &chunk) < 0) {
- USBD_LOG_ERR("USB write data failed\r\n");
+ USB_LOG_ERR("USB write data failed\r\n");
return;
}
@@ -988,9 +988,9 @@ static void usbd_send_to_host(uint16_t len)
}
} else {
usbd_core_cfg.zlp_flag = false;
- USBD_LOG_DBG("send zlp\r\n");
+ USB_LOG_DBG("send zlp\r\n");
if (usbd_ep_write(USB_CONTROL_IN_EP0, NULL, 0, NULL) < 0) {
- USBD_LOG_ERR("USB write zlp failed\r\n");
+ USB_LOG_ERR("USB write zlp failed\r\n");
return;
}
}
@@ -1006,7 +1006,7 @@ static void usbd_ep0_setup_handler(void)
*/
if (usbd_ep_read(USB_CONTROL_OUT_EP0, (uint8_t *)setup,
sizeof(struct usb_setup_packet), NULL) < 0) {
- USBD_LOG_ERR("Read Setup Packet failed\r\n");
+ USB_LOG_ERR("Read Setup Packet failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
@@ -1015,7 +1015,7 @@ static void usbd_ep0_setup_handler(void)
if (setup->wLength > USB_REQUEST_BUFFER_SIZE) {
if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
- USBD_LOG_ERR("Request buffer too small\r\n");
+ USB_LOG_ERR("Request buffer too small\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
@@ -1029,13 +1029,13 @@ static void usbd_ep0_setup_handler(void)
/* this maybe set code in class request code */
if (setup->wLength &&
(setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
- USBD_LOG_DBG("prepare to out data\r\n");
+ USB_LOG_DBG("prepare to out data\r\n");
return;
}
/* Ask installed handler to process request */
if (!usbd_setup_request_handler(setup, &usbd_core_cfg.ep0_data_buf, &usbd_core_cfg.ep0_data_buf_len)) {
- USBD_LOG_ERR("usbd_setup_request_handler failed\r\n");
+ USB_LOG_ERR("usbd_setup_request_handler failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
@@ -1055,12 +1055,12 @@ static void usbd_ep0_out_handler(void)
/* OUT transfer, status packets */
if (usbd_core_cfg.ep0_data_buf_residue == 0) {
/* absorb zero-length status message */
- USBD_LOG_DBG("recv status\r\n");
+ USB_LOG_DBG("recv status\r\n");
if (usbd_ep_read(USB_CONTROL_OUT_EP0,
NULL,
0, NULL) < 0) {
- USBD_LOG_ERR("Read DATA Packet failed\r\n");
+ USB_LOG_ERR("Read DATA Packet failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
}
@@ -1073,7 +1073,7 @@ static void usbd_ep0_out_handler(void)
if (usbd_ep_read(USB_CONTROL_OUT_EP0,
usbd_core_cfg.ep0_data_buf,
usbd_core_cfg.ep0_data_buf_residue, &chunk) < 0) {
- USBD_LOG_ERR("Read DATA Packet failed\r\n");
+ USB_LOG_ERR("Read DATA Packet failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
@@ -1086,7 +1086,7 @@ static void usbd_ep0_out_handler(void)
usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
if (!usbd_setup_request_handler(setup, &usbd_core_cfg.ep0_data_buf, &usbd_core_cfg.ep0_data_buf_len)) {
- USBD_LOG_ERR("usbd_setup_request_handler1 failed\r\n");
+ USB_LOG_ERR("usbd_setup_request_handler1 failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
@@ -1094,7 +1094,7 @@ static void usbd_ep0_out_handler(void)
/*Send status to host*/
usbd_send_to_host(setup->wLength);
} else {
- USBD_LOG_ERR("ep0_data_buf_residue is not zero\r\n");
+ USB_LOG_ERR("ep0_data_buf_residue is not zero\r\n");
}
}
@@ -1247,7 +1247,7 @@ void usbd_event_notify_handler(uint8_t event, void *arg)
break;
default:
- USBD_LOG_ERR("USB unknown event: %d\r\n", event);
+ USB_LOG_ERR("USB unknown event: %d\r\n", event);
break;
}
}