Skip to content
This repository was archived by the owner on Mar 31, 2024. It is now read-only.

Commit 270c127

Browse files
committed
added CDC UART to RP2040 (Pico)
1 parent 687b3fe commit 270c127

File tree

7 files changed

+99
-5
lines changed

7 files changed

+99
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if(FAMILY STREQUAL "rp2040")
2626
${CMAKE_CURRENT_SOURCE_DIR}/CMSIS_5/CMSIS/DAP/Firmware/Source/DAP_vendor.c
2727
${CMAKE_CURRENT_SOURCE_DIR}/CMSIS_5/CMSIS/DAP/Firmware/Source/SWO.c
2828
${CMAKE_CURRENT_SOURCE_DIR}/CMSIS_5/CMSIS/DAP/Firmware/Source/SW_DP.c
29+
${CMAKE_CURRENT_SOURCE_DIR}/bsp/${FAMILY}/cdc_uart.c
2930
)
3031

3132
# Example include

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SRC_C += \
1717
./CMSIS_5/CMSIS/DAP/Firmware/Source/JTAG_DP.c \
1818
./CMSIS_5/CMSIS/DAP/Firmware/Source/DAP_vendor.c \
1919
./CMSIS_5/CMSIS/DAP/Firmware/Source/SWO.c \
20-
./CMSIS_5/CMSIS/DAP/Firmware/Source/SW_DP.c
20+
./CMSIS_5/CMSIS/DAP/Firmware/Source/SW_DP.c \
21+
./bsp/$(BOARD)/cdc_uart.c
2122

2223
include ./tinyusb/examples/rules.mk

bsp/rp2040/cdc_uart.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <pico/stdlib.h>
27+
28+
#include "tusb.h"
29+
30+
#include "picoprobe_config.h"
31+
32+
void cdc_uart_init(void) {
33+
gpio_set_function(PICOPROBE_UART_TX, GPIO_FUNC_UART);
34+
gpio_set_function(PICOPROBE_UART_RX, GPIO_FUNC_UART);
35+
uart_init(PICOPROBE_UART_INTERFACE, PICOPROBE_UART_BAUDRATE);
36+
}
37+
38+
void cdc_task(void) {
39+
uint8_t rx_buf[CFG_TUD_CDC_RX_BUFSIZE];
40+
uint8_t tx_buf[CFG_TUD_CDC_TX_BUFSIZE];
41+
42+
// Consume uart fifo regardless even if not connected
43+
uint rx_len = 0;
44+
while(uart_is_readable(PICOPROBE_UART_INTERFACE) && (rx_len < sizeof(rx_buf))) {
45+
rx_buf[rx_len++] = uart_getc(PICOPROBE_UART_INTERFACE);
46+
}
47+
48+
if (tud_cdc_connected()) {
49+
// Do we have anything to display on the host's terminal?
50+
if (rx_len) {
51+
for (uint i = 0; i < rx_len; i++) {
52+
tud_cdc_write_char(rx_buf[i]);
53+
}
54+
tud_cdc_write_flush();
55+
}
56+
57+
if (tud_cdc_available()) {
58+
// Is there any data from the host for us to tx
59+
uint tx_len = tud_cdc_read(tx_buf, sizeof(tx_buf));
60+
uart_write_blocking(PICOPROBE_UART_INTERFACE, tx_buf, tx_len);
61+
}
62+
}
63+
}
64+
65+
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* line_coding) {
66+
picoprobe_info("New baud rate %d\n", line_coding->bit_rate);
67+
uart_init(PICOPROBE_UART_INTERFACE, line_coding->bit_rate);
68+
}

bsp/stm32f072disco/cdc_uart.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void cdc_uart_init(void)
2+
{
3+
}
4+
5+
void cdc_task(void)
6+
{
7+
}

main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@
3333
#include "DAP_config.h" /* ARM code *assumes* this is included prior to DAP.h */
3434
#include "DAP.h"
3535

36+
void cdc_uart_init(void);
37+
void cdc_task(void);
38+
3639
int main(void)
3740
{
3841
board_init();
42+
cdc_uart_init();
3943
DAP_Setup();
4044

4145
tusb_init();
4246

4347
while (1)
4448
{
4549
tud_task(); // tinyusb device task
50+
cdc_task();
4651
}
4752

4853
return 0;

tusb_config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
#endif
9595

9696
//------------- CLASS -------------//
97-
#define CFG_TUD_CDC 0
97+
#define CFG_TUD_CDC 1
9898
#define CFG_TUD_MSC 0
9999
#define CFG_TUD_HID 1
100100
#define CFG_TUD_MIDI 0
@@ -103,6 +103,10 @@
103103

104104
#define CFG_TUD_HID_EP_BUFSIZE 64
105105

106+
// CDC FIFO size of TX and RX
107+
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
108+
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
109+
106110
#ifdef __cplusplus
107111
}
108112
#endif

usb_descriptors.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,28 @@ uint8_t const * tud_hid_descriptor_report_cb(void)
100100
enum
101101
{
102102
ITF_NUM_HID,
103+
ITF_NUM_CDC_COM,
104+
ITF_NUM_CDC_DATA,
103105
ITF_NUM_TOTAL
104106
};
105107

106-
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_INOUT_DESC_LEN)
108+
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_HID_INOUT_DESC_LEN)
107109

108-
#define EPNUM_HID 0x01
110+
#define EPNUM_HID 0x01
111+
#define EPNUM_CDC_NOTIF 0x83
112+
#define EPNUM_CDC_OUT 0x02
113+
#define EPNUM_CDC_IN 0x82
109114

110115
uint8_t const desc_configuration[] =
111116
{
112117
// Config number, interface count, string index, total length, attribute, power in mA
113118
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
114119

115120
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
116-
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 1)
121+
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 1),
122+
123+
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
124+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_COM, 0, EPNUM_CDC_NOTIF, 64, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
117125
};
118126

119127
// Invoked when received GET CONFIGURATION DESCRIPTOR

0 commit comments

Comments
 (0)