Add second USB serial device for SUMP protocol (logic analyzer)

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2021-04-26 09:03:14 +02:00
parent 0ff8b5530b
commit f3e7eb1a94
7 changed files with 87 additions and 15 deletions

View File

@ -12,6 +12,7 @@ add_executable(picoprobe
src/usb_descriptors.c
src/probe.c
src/cdc_uart.c
src/cdc_sump.c
src/get_serial.c
)

51
src/cdc_sump.c Normal file
View File

@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Jaroslav Kysela <perex@perex.cz>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <pico/stdlib.h>
#include "tusb.h"
#include "picoprobe_config.h"
#include "cdc_sump.h"
void cdc_sump_init(void)
{
}
#define MAX_UART_PKT 64
void cdc_sump_task(void) {
uint8_t tx_buf[MAX_UART_PKT];
const char itf = 1;
if (tud_cdc_n_connected(itf)) {
if (tud_cdc_n_available(itf)) {
tud_cdc_n_read(itf, tx_buf, sizeof(tx_buf));
}
}
}
void cdc_sump_line_coding(cdc_line_coding_t const* line_coding) {
picoprobe_info("Sump new baud rate %d\n", line_coding->bit_rate);
}

View File

@ -28,6 +28,7 @@
#include "tusb.h"
#include "picoprobe_config.h"
#include "cdc_uart.h"
void cdc_uart_init(void) {
gpio_set_function(PICOPROBE_UART_TX, GPIO_FUNC_UART);
@ -36,9 +37,10 @@ void cdc_uart_init(void) {
}
#define MAX_UART_PKT 64
void cdc_task(void) {
void cdc_uart_task(void) {
uint8_t rx_buf[MAX_UART_PKT];
uint8_t tx_buf[MAX_UART_PKT];
const char itf = 0;
// Consume uart fifo regardless even if not connected
uint rx_len = 0;
@ -46,24 +48,22 @@ void cdc_task(void) {
rx_buf[rx_len++] = uart_getc(PICOPROBE_UART_INTERFACE);
}
if (tud_cdc_connected()) {
if (tud_cdc_n_connected(itf)) {
// Do we have anything to display on the host's terminal?
if (rx_len) {
for (uint i = 0; i < rx_len; i++) {
tud_cdc_write_char(rx_buf[i]);
}
tud_cdc_write_flush();
tud_cdc_n_write(itf, rx_buf, rx_len);
tud_cdc_n_write_flush(itf);
}
if (tud_cdc_available()) {
if (tud_cdc_n_available(itf)) {
// Is there any data from the host for us to tx
uint tx_len = tud_cdc_read(tx_buf, sizeof(tx_buf));
uint tx_len = tud_cdc_n_read(itf, tx_buf, sizeof(tx_buf));
uart_write_blocking(PICOPROBE_UART_INTERFACE, tx_buf, tx_len);
}
}
}
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* line_coding) {
void cdc_uart_line_coding(cdc_line_coding_t const* line_coding) {
picoprobe_info("New baud rate %d\n", line_coding->bit_rate);
uart_init(PICOPROBE_UART_INTERFACE, line_coding->bit_rate);
}

View File

@ -27,6 +27,7 @@
#define CDC_UART_H
void cdc_uart_init(void);
void cdc_task(void);
void cdc_uart_task(void);
void cdc_uart_line_coding(cdc_line_coding_t const* line_coding);
#endif
#endif

View File

@ -33,17 +33,26 @@
#include "picoprobe_config.h"
#include "probe.h"
#include "cdc_uart.h"
#include "cdc_sump.h"
#include "get_serial.h"
#include "led.h"
// UART0 for Picoprobe debug
// UART1 for picoprobe to target device
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* line_coding) {
if (itf == 0)
cdc_uart_line_coding(line_coding);
else if (itf == 1)
cdc_sump_line_coding(line_coding);
}
int main(void) {
board_init();
usb_serial_init();
cdc_uart_init();
cdc_sump_init();
tusb_init();
probe_init();
led_init();
@ -52,7 +61,8 @@ int main(void) {
while (1) {
tud_task(); // tinyusb device task
cdc_task();
cdc_uart_task();
cdc_sump_task();
probe_task();
led_task();
}

View File

@ -63,7 +63,7 @@
//------------- CLASS -------------//
#define CFG_TUD_HID 0
#define CFG_TUD_CDC 1
#define CFG_TUD_CDC 2
#define CFG_TUD_MSC 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 1

View File

@ -65,6 +65,8 @@ enum
ITF_NUM_CDC_COM,
ITF_NUM_CDC_DATA,
ITF_NUM_PROBE,
ITF_NUM_CDC_SUMP_COM,
ITF_NUM_CDC_SUMP_DATA,
ITF_NUM_TOTAL
};
@ -73,8 +75,12 @@ enum
#define CDC_DATA_IN_EP_NUM 0x83
#define PROBE_OUT_EP_NUM 0x04
#define PROBE_IN_EP_NUM 0x85
#define CDC_SUMP_NOTIFICATION_EP_NUM 0x86
#define CDC_SUMP_DATA_OUT_EP_NUM 0x07
#define CDC_SUMP_DATA_IN_EP_NUM 0x88
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_VENDOR_DESC_LEN)
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + \
TUD_VENDOR_DESC_LEN + TUD_CDC_DESC_LEN)
uint8_t const desc_configuration[] =
{
@ -84,8 +90,11 @@ uint8_t const desc_configuration[] =
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_COM, 0, CDC_NOTIFICATION_EP_NUM, 64, CDC_DATA_OUT_EP_NUM, CDC_DATA_IN_EP_NUM, 64),
// Interface 2
TUD_VENDOR_DESCRIPTOR(ITF_NUM_PROBE, 0, PROBE_OUT_EP_NUM, PROBE_IN_EP_NUM, 64)
TUD_VENDOR_DESCRIPTOR(ITF_NUM_PROBE, 0, PROBE_OUT_EP_NUM, PROBE_IN_EP_NUM, 64),
// Interface 3 + 4
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_SUMP_COM, 0, CDC_SUMP_NOTIFICATION_EP_NUM, 64,
CDC_SUMP_DATA_OUT_EP_NUM, CDC_SUMP_DATA_IN_EP_NUM, 64)
};
// Invoked when received GET CONFIGURATION DESCRIPTOR