代码的获取请翻阅到底部,或点击“阅读原文”,转载请注明出处。
1
任务需求及分析
2
练手项目:USB-CDC
// USB->LINK回调
void tud_cdc_rx_cb(uint8_t itf) {
char buf[CFG_TUD_CDC_RX_BUFSIZE] = {0};
tud_cdc_read(buf, CFG_TUD_CDC_RX_BUFSIZE);
uart_puts(PICO_LINK_UART_ID, buf);
}
// MCU->LINK回调
void on_uart_rx() {
while (uart_is_readable(PICO_LINK_UART_ID)) {
uint8_t ch = uart_getc(PICO_LINK_UART_ID);
tud_cdc_write_char(ch);
}
tud_cdc_write_flush();
}
void VCOM_Init() {
cdc_line_coding_t uart_config;
tud_cdc_get_line_coding(&uart_config);
uart_init(PICO_LINK_UART_ID, uart_config.bit_rate);
gpio_set_function(PICO_LINK_UART_RX, GPIO_FUNC_UART);
gpio_set_function(PICO_LINK_UART_TX, GPIO_FUNC_UART);
irq_set_exclusive_handler(PICO_LINK_UART_IRQ, on_uart_rx);
irq_set_enabled(PICO_LINK_UART_IRQ, true);
uart_set_irq_enables(PICO_LINK_UART_ID, true, false);
}
void VCOM_SendString(char *str) {
while (*str) {
tud_cdc_write_char(*str++);
}
tud_cdc_write_char('\r');
tud_cdc_write_char('\n');
tud_cdc_write_flush();
}
// 检测到上位机串口助手开启串口回调
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
VCOM_Init();
}
3
移植DAPLink
//--------------------------------------------------------------------+
// Configuration Descriptor
//--------------------------------------------------------------------+
enum {
ITF_NUM_HID,
ITF_NUM_CDC_COM,
ITF_NUM_CDC_DATA,
ITF_NUM_TOTAL
};
uint8_t const desc_configuration[] = {
// Config number, interface count, string index, total length, attribute, power in mA
TUD_CONFIG_DESCRIPTOR(
1,
ITF_NUM_TOTAL,
0,
CONFIG_TOTAL_LEN,
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP,
100
),
// Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
TUD_HID_INOUT_DESCRIPTOR(
ITF_NUM_HID,
0,
HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report),
EPNUM_HID,
0x80 | EPNUM_HID,
CFG_TUD_HID_EP_BUFSIZE,
1
),
TUD_CDC_DESCRIPTOR(
ITF_NUM_CDC_COM,
0,
EPNUM_CDC_NOTIF,
64,
EPNUM_CDC_OUT,
EPNUM_CDC_IN,
64
)
};
__STATIC_FORCEINLINE void PIN_SWDIO_OUT(uint32_t bit) {
if (bit & 1) {
gpio_set_mask(PICO_LINK_SWDIO_MASK);
} else {
gpio_clr_mask(PICO_LINK_SWDIO_MASK);
}
}
// Invoked when received SET_REPORT control request or
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
void tud_hid_set_report_cb(
uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const *RxDataBuffer,
uint16_t bufsize) {
static uint8_t TxDataBuffer[CFG_TUD_HID_EP_BUFSIZE];
uint32_t response_size = TU_MIN(CFG_TUD_HID_EP_BUFSIZE, bufsize);
DAP_ExecuteCommand(RxDataBuffer, TxDataBuffer);
tud_hid_report(0, TxDataBuffer, response_size);
}
4
实现结果
5
心得体会
END
硬禾学堂
硬禾团队一直致力于给电子工程师和相关专业的同学,带来规范的核心技能课程,帮助大家在学习和工作的各个阶段,都能有效地提升自己的职业能力。
硬禾学堂
我们一起在电子领域探索前进
关注硬禾公众号,随时直达课堂
点击阅读原文查看更多