代碼的獲取請翻閱到底部,或點擊“閱讀原文”,轉載請注明出處。
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 mATUD_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 intervalTUD_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
硬禾學堂
硬禾團隊一直致力于給電子工程師和相關專業的同學,帶來規范的核心技能課程,幫助大家在學習和工作的各個階段,都能有效地提升自己的職業能力。

硬禾學堂
我們一起在電子領域探索前進
關注硬禾公眾號,隨時直達課堂

點擊閱讀原文查看更多