osmo-gtm900: Import/merge dual-USB-UART code

This is really just merging the codebase from
https://github.com/Noltari/pico-uart-bridge/tree/master
into the skeleton firmware we had for modem power on so far.
This commit is contained in:
Harald Welte 2023-11-29 13:58:00 +01:00
parent 025ae94a1c
commit f4ab7935a9
5 changed files with 572 additions and 20 deletions

View File

@ -1,9 +1,21 @@
add_executable(osmo-gtm900)
target_sources(osmo-gtm900 PRIVATE osmo_gtm900.c)
target_compile_options(osmo-gtm900 PRIVATE -DPARAM_ASSERTIONS_ENABLE_ALL=1)
target_sources(osmo-gtm900 PRIVATE osmo_gtm900.c
usb_descriptors.c)
# Make sure TinyUSB can find tusb_config.h
target_include_directories(osmo-gtm900 PUBLIC
${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(osmo-gtm900 PRIVATE
pico_stdlib
pico_multicore
hardware_flash
tinyusb_device
tinyusb_board
cmsis_core
)
pico_enable_stdio_usb(osmo-gtm900 1)

View File

@ -1,10 +1,19 @@
// SPDX-License-Identifier: MIT
/*
* Copyright 2021 Álvaro Fernández Rojas <noltari@gmail.com> - https://github.com/Noltari/pico-uart-bridge
* Copyright 2023 sysmocom - s.f.m.c. GmbH - adapted for Osmocom GTM900 board
*/
#include <hardware/irq.h>
#include <hardware/structs/sio.h>
#include <hardware/uart.h>
#include <pico/multicore.h>
#include <pico/stdlib.h>
#include <string.h>
#include <tusb.h>
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "tusb.h"
#define GPIO_nMODEM_LDO_ON 14 /* pico pin 19 */
#define GPIO_nPWON 6 /* pico pin 9 */
#define GPIO_UART1_TX 0 /* pico pin 1 */
@ -13,16 +22,299 @@
#define GPIO_UART1_RTS 3 /* pico pin 5 */
#define GPIO_UART2_TX 4 /* pico pin 6 */
#define GPIO_UART2_RX 5 /* pico pin 7 */
#define GPIO_LED 25
#if !defined(MIN)
#define MIN(a, b) ((a > b) ? b : a)
#endif /* MIN */
int main()
{
stdio_usb_init();
#define BUFFER_SIZE 2560
while (!tud_cdc_connected()) {
printf(".");
sleep_ms(500);
#define DEF_BIT_RATE 115200
#define DEF_STOP_BITS 1
#define DEF_PARITY 0
#define DEF_DATA_BITS 8
typedef struct {
uart_inst_t *const inst;
uint irq;
void *irq_fn;
uint8_t tx_pin;
uint8_t rx_pin;
} uart_id_t;
typedef struct {
cdc_line_coding_t usb_lc;
cdc_line_coding_t uart_lc;
mutex_t lc_mtx;
uint8_t uart_buffer[BUFFER_SIZE];
uint32_t uart_pos;
mutex_t uart_mtx;
uint8_t usb_buffer[BUFFER_SIZE];
uint32_t usb_pos;
mutex_t usb_mtx;
} uart_data_t;
void uart0_irq_fn(void);
void uart1_irq_fn(void);
const uart_id_t UART_ID[CFG_TUD_CDC] = {
{
.inst = uart0,
.irq = UART0_IRQ,
.irq_fn = &uart0_irq_fn,
.tx_pin = GPIO_UART1_TX,
.rx_pin = GPIO_UART1_RX,
}, {
.inst = uart1,
.irq = UART1_IRQ,
.irq_fn = &uart1_irq_fn,
.tx_pin = GPIO_UART2_TX,
.rx_pin = GPIO_UART2_RX,
}
};
uart_data_t UART_DATA[CFG_TUD_CDC];
static inline uint databits_usb2uart(uint8_t data_bits)
{
switch (data_bits) {
case 5:
return 5;
case 6:
return 6;
case 7:
return 7;
default:
return 8;
}
}
static inline uart_parity_t parity_usb2uart(uint8_t usb_parity)
{
switch (usb_parity) {
case 1:
return UART_PARITY_ODD;
case 2:
return UART_PARITY_EVEN;
default:
return UART_PARITY_NONE;
}
}
static inline uint stopbits_usb2uart(uint8_t stop_bits)
{
switch (stop_bits) {
case 2:
return 2;
default:
return 1;
}
}
void update_uart_cfg(uint8_t itf)
{
const uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
mutex_enter_blocking(&ud->lc_mtx);
if (ud->usb_lc.bit_rate != ud->uart_lc.bit_rate) {
uart_set_baudrate(ui->inst, ud->usb_lc.bit_rate);
ud->uart_lc.bit_rate = ud->usb_lc.bit_rate;
}
if ((ud->usb_lc.stop_bits != ud->uart_lc.stop_bits) ||
(ud->usb_lc.parity != ud->uart_lc.parity) ||
(ud->usb_lc.data_bits != ud->uart_lc.data_bits)) {
uart_set_format(ui->inst,
databits_usb2uart(ud->usb_lc.data_bits),
stopbits_usb2uart(ud->usb_lc.stop_bits),
parity_usb2uart(ud->usb_lc.parity));
ud->uart_lc.data_bits = ud->usb_lc.data_bits;
ud->uart_lc.parity = ud->usb_lc.parity;
ud->uart_lc.stop_bits = ud->usb_lc.stop_bits;
}
mutex_exit(&ud->lc_mtx);
}
void usb_read_bytes(uint8_t itf)
{
uart_data_t *ud = &UART_DATA[itf];
uint32_t len = tud_cdc_n_available(itf);
if (len &&
mutex_try_enter(&ud->usb_mtx, NULL)) {
len = MIN(len, BUFFER_SIZE - ud->usb_pos);
if (len) {
uint32_t count;
count = tud_cdc_n_read(itf, &ud->usb_buffer[ud->usb_pos], len);
ud->usb_pos += count;
}
mutex_exit(&ud->usb_mtx);
}
}
void usb_write_bytes(uint8_t itf)
{
uart_data_t *ud = &UART_DATA[itf];
if (ud->uart_pos &&
mutex_try_enter(&ud->uart_mtx, NULL)) {
uint32_t count;
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
if (count < ud->uart_pos)
memmove(ud->uart_buffer, &ud->uart_buffer[count],
ud->uart_pos - count);
ud->uart_pos -= count;
mutex_exit(&ud->uart_mtx);
if (count)
tud_cdc_n_write_flush(itf);
}
}
void usb_cdc_process(uint8_t itf)
{
uart_data_t *ud = &UART_DATA[itf];
mutex_enter_blocking(&ud->lc_mtx);
tud_cdc_n_get_line_coding(itf, &ud->usb_lc);
mutex_exit(&ud->lc_mtx);
usb_read_bytes(itf);
usb_write_bytes(itf);
}
void core1_entry(void)
{
tusb_init();
while (1) {
int itf;
int con = 0;
tud_task();
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
if (tud_cdc_n_connected(itf)) {
con = 1;
usb_cdc_process(itf);
}
}
gpio_put(GPIO_LED, con);
}
}
static inline void uart_read_bytes(uint8_t itf)
{
uart_data_t *ud = &UART_DATA[itf];
const uart_id_t *ui = &UART_ID[itf];
if (uart_is_readable(ui->inst)) {
mutex_enter_blocking(&ud->uart_mtx);
while (uart_is_readable(ui->inst) &&
(ud->uart_pos < BUFFER_SIZE)) {
ud->uart_buffer[ud->uart_pos] = uart_getc(ui->inst);
ud->uart_pos++;
}
mutex_exit(&ud->uart_mtx);
}
}
void uart0_irq_fn(void)
{
uart_read_bytes(0);
}
void uart1_irq_fn(void)
{
uart_read_bytes(1);
}
void uart_write_bytes(uint8_t itf)
{
uart_data_t *ud = &UART_DATA[itf];
if (ud->usb_pos &&
mutex_try_enter(&ud->usb_mtx, NULL)) {
const uart_id_t *ui = &UART_ID[itf];
uint32_t count = 0;
while (uart_is_writable(ui->inst) &&
count < ud->usb_pos) {
uart_putc_raw(ui->inst, ud->usb_buffer[count]);
count++;
}
if (count < ud->usb_pos)
memmove(ud->usb_buffer, &ud->usb_buffer[count],
ud->usb_pos - count);
ud->usb_pos -= count;
mutex_exit(&ud->usb_mtx);
}
}
void init_uart_data(uint8_t itf)
{
const uart_id_t *ui = &UART_ID[itf];
uart_data_t *ud = &UART_DATA[itf];
/* Pinmux */
gpio_set_function(ui->tx_pin, GPIO_FUNC_UART);
gpio_set_function(ui->rx_pin, GPIO_FUNC_UART);
/* USB CDC LC */
ud->usb_lc.bit_rate = DEF_BIT_RATE;
ud->usb_lc.data_bits = DEF_DATA_BITS;
ud->usb_lc.parity = DEF_PARITY;
ud->usb_lc.stop_bits = DEF_STOP_BITS;
/* UART LC */
ud->uart_lc.bit_rate = DEF_BIT_RATE;
ud->uart_lc.data_bits = DEF_DATA_BITS;
ud->uart_lc.parity = DEF_PARITY;
ud->uart_lc.stop_bits = DEF_STOP_BITS;
/* Buffer */
ud->uart_pos = 0;
ud->usb_pos = 0;
/* Mutex */
mutex_init(&ud->lc_mtx);
mutex_init(&ud->uart_mtx);
mutex_init(&ud->usb_mtx);
/* UART start */
uart_init(ui->inst, ud->usb_lc.bit_rate);
/* FIXME: enable this for first UART! */
uart_set_hw_flow(ui->inst, false, false);
uart_set_format(ui->inst, databits_usb2uart(ud->usb_lc.data_bits),
stopbits_usb2uart(ud->usb_lc.stop_bits),
parity_usb2uart(ud->usb_lc.parity));
uart_set_fifo_enabled(ui->inst, false);
/* UART RX Interrupt */
irq_set_exclusive_handler(ui->irq, ui->irq_fn);
irq_set_enabled(ui->irq, true);
uart_set_irq_enables(ui->inst, true, false);
}
int main(void)
{
int itf;
printf("Starting osmoGTM900 playground\n");
/* control lines as outputs */
@ -34,15 +326,22 @@ int main()
gpio_init(GPIO_nPWON);
gpio_set_dir(GPIO_nPWON, true);
gpio_put(GPIO_nPWON, 1);
/* uarts as uarts */
uart_init(uart0, 115200);
gpio_set_function(GPIO_UART1_TX, GPIO_FUNC_UART);
gpio_set_function(GPIO_UART1_RX, GPIO_FUNC_UART);
gpio_init(GPIO_LED);
gpio_set_dir(GPIO_LED, GPIO_OUT);
set_sys_clock_khz(250000, false);
usbd_serial_init();
/* Rx/Tx pins are handled inside UART */
gpio_set_function(GPIO_UART1_CTS, GPIO_FUNC_UART);
gpio_set_function(GPIO_UART1_RTS, GPIO_FUNC_UART);
uart_init(uart1, 115200);
gpio_set_function(GPIO_UART2_TX, GPIO_FUNC_UART);
gpio_set_function(GPIO_UART2_RX, GPIO_FUNC_UART);
for (itf = 0; itf < CFG_TUD_CDC; itf++)
init_uart_data(itf);
multicore_launch_core1(core1_entry);
printf("Entering main loop\n");
sleep_ms(500); /* Delay of 500ms to settle usb */
@ -57,7 +356,10 @@ int main()
printf("done\n");
while (true) {
// usb-uart handling goes here();
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
update_uart_cfg(itf);
uart_write_bytes(itf);
}
}
}

120
osmo-gtm900/tusb_config.h Normal file
View File

@ -0,0 +1,120 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* 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.
*
*/
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#include <tusb_option.h>
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
// defined by board.mk
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif
// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_DEVICE_RHPORT_NUM
#define BOARD_DEVICE_RHPORT_NUM 0
#endif
// RHPort max operational speed can defined by board.mk
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
#ifndef BOARD_DEVICE_RHPORT_SPEED
#if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 || CFG_TUSB_MCU == OPT_MCU_SAMX7X)
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED
#else
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
#endif
#endif
// Device mode with rhport and speed defined by board.mk
#if BOARD_DEVICE_RHPORT_NUM == 0
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#elif BOARD_DEVICE_RHPORT_NUM == 1
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#else
#error "Incorrect RHPort configuration"
#endif
#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE
#endif
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
//#define CFG_TUSB_DEBUG 10
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put
* into those specific section.
* e.g
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/
#ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION
#endif
#ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
#endif
//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------
#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif
//------------- CLASS -------------//
#define CFG_TUD_HID 0
#define CFG_TUD_CDC 2
#define CFG_TUD_AUDIO 0
#define CFG_TUD_MSC 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0
// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_EP_BUFSIZE 16
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_RX_BUFSIZE CFG_TUD_CDC_EP_BUFSIZE
#define CFG_TUD_CDC_TX_BUFSIZE CFG_TUD_CDC_EP_BUFSIZE
#include "usb_descriptors.h"
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_CONFIG_H_ */

View File

@ -0,0 +1,106 @@
#include "tusb.h"
#include "class/audio/audio.h"
#include "usb_descriptors.h"
/* Device Descriptor */
static tusb_desc_device_t const desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = 0x1d50,
.idProduct = 0xaaa1,
.bcdDevice = 0x0100,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1
};
/* Invoked when received GET DEVICE DESCRIPTOR */
uint8_t const* tud_descriptor_device_cb(void)
{
return (uint8_t const *) &desc_device;
}
/* Configuration Descriptor */
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN * CFG_TUD_CDC)
static uint8_t const desc_config[] = {
// 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),
TUD_CDC_DESCRIPTOR(0, 4, 0x81, 8, 0x01, 0x82, 64),
TUD_CDC_DESCRIPTOR(2, 5, 0x83, 8, 0x03, 0x84, 64),
};
/* Invoked when received GET CONFIGURATION DESCRIPTOR */
uint8_t const* tud_descriptor_configuration_cb(uint8_t index)
{
// This example use the same configuration for both high and full speed mode
return desc_config;
}
/* String Descriptors */
#define USBD_STR_SERIAL_LEN 17
static char usbd_serial[USBD_STR_SERIAL_LEN] = "000000000000";
char const *string_desc_arr[] = {
(const char []) { 0x09, 0x04}, /* 0: supported language == English */
"sysmocom - s.f.m.c. GmbH", /* 1: Manufacturer */
"osmo-gtm900", /* 2: Product */
usbd_serial, /* 3: Serial, should use chip ID */
"UART1",
"UART2",
};
static uint16_t _desc_str[32];
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
(void) langid;
uint8_t chr_count;
if (index == 0) {
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
} else {
// Convert ASCII string into UTF-16
if (!(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])))
return NULL;
const char* str = string_desc_arr[index];
// Cap at max char
chr_count = (uint8_t) strlen(str);
if (chr_count > 31)
chr_count = 31;
for (uint8_t i = 0; i < chr_count; i++) {
_desc_str[1+i] = str[i];
}
}
// first byte is length (including header), second byte is string type
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
return _desc_str;
}
void usbd_serial_init(void)
{
uint8_t id[8];
flash_get_unique_id(id);
snprintf(usbd_serial, USBD_STR_SERIAL_LEN, "%02X%02X%02X%02X%02X%02X%02X%02X",
id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
}

View File

@ -0,0 +1,12 @@
#pragma once
/* Interface numbers */
enum {
ITF_NUM_UART1 = 0,
ITF_NUM_UART1_ASSOC,
ITF_NUM_UART2,
ITF_NUM_UART2_ASSOC,
ITF_NUM_TOTAL
};
void usbd_serial_init(void);