make whitespace & indentation etc. consistent, make usb descriptors a bit nicer

This commit is contained in:
Triss 2021-06-14 01:32:21 +02:00
parent c6fdc53fdd
commit 4c93827bed
20 changed files with 709 additions and 800 deletions

View File

@ -51,6 +51,9 @@ This information includes:
#include "cmsis_compiler.h"
#include "bsp/board.h"
#include "protos.h"
#include "unique.h"
/// Processor Clock of the Cortex-M MCU used in the Debug Unit.
/// This value is used to calculate the SWD/JTAG clock speed.
#define CPU_CLOCK 100000000U ///< Specifies the CPU Clock in Hz.
@ -132,8 +135,9 @@ This information includes:
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
(void)str;
return (0U);
const static char vnd[] = INFO_MANUFACTURER;
for (size_t i = 0; i < sizeof(vnd); ++i) str[i] = vnd[i];
return sizeof(vnd)-1;
}
/** Get Product ID string.
@ -141,8 +145,9 @@ __STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
(void)str;
return (0U);
const static char prd[] = INFO_PRODUCT(INFO_BOARDNAME);
for (size_t i = 0; i < sizeof(prd); ++i) str[i] = prd[i];
return sizeof(prd)-1;
}
/** Get Serial Number string.
@ -150,8 +155,7 @@ __STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetSerNumString (char *str) {
(void)str;
return (0U);
return get_unique_id_u8(str);
}
///@}

View File

@ -7,5 +7,7 @@
/*#define DBOARD_HAS_SERPROG*/
/*#define DBOARD_HAS_TINYI2C*/
#define INFO_BOARDNAME "unknown"
#endif

View File

@ -3,17 +3,23 @@
/* in the absence of the board-specific directory providing a unique ID, we provide a canned one */
static uint8_t get_unique_id(uint16_t *desc_str)
{
const char canned[] = "123456";
uint8_t i;
static uint8_t get_unique_id_u8(uint8_t *desc_str) {
static const char canned[] = "123456";
for(i=0; i<TU_ARRAY_SIZE(canned); i++)
{
for (int i=0; i<TU_ARRAY_SIZE(canned); i++) {
desc_str[i] = canned[i];
}
return i;
}
static uint8_t get_unique_id_u16(uint16_t *desc_str) {
static const char canned[] = "123456";
for (int i=0; i<TU_ARRAY_SIZE(canned); i++) {
desc_str[i] = canned[i];
}
return i;
}
#define PRODUCT_PREFIX ""

View File

@ -60,6 +60,8 @@ This information includes:
#include <pico/binary_info.h>
#include "picoprobe_config.h"
#include "protos.h"
#include "unique.h"
#define PROBE_PIN_SWCLK_MASK (1UL << (PROBE_PIN_SWCLK))
#define PROBE_PIN_SWDIO_MASK (1UL << (PROBE_PIN_SWDIO))
@ -156,11 +158,9 @@ This information includes:
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
const static char vnd[] = "Raspberry Pi";
const static char vnd[] = INFO_MANUFACTURER;
for (size_t i = 0; i < sizeof(vnd); ++i) str[i] = vnd[i];
return sizeof(vnd)-1;
//(void)str;
//return (0U);
}
/** Get Product ID string.
@ -168,11 +168,9 @@ __STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
const static char prd[] = "RP2040 DapperMime-JTAG";
const static char prd[] = INFO_PRODUCT(INFO_BOARDNAME);
for (size_t i = 0; i < sizeof(prd); ++i) str[i] = prd[i];
return sizeof(prd)-1;
//(void)str;
//return (0U);
}
/** Get Serial Number string.
@ -180,11 +178,7 @@ __STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetSerNumString (char *str) {
const static char ser[] = "1337";
for (size_t i = 0; i < sizeof(ser); ++i) str[i] = ser[i];
return sizeof(ser)-1;
//(void)str;
//return (0U);
return get_unique_id_u8(str);
}
///@}
@ -234,7 +228,6 @@ Configures the DAP Hardware I/O pins for JTAG mode:
- TDO to input mode.
*/
__STATIC_INLINE void PORT_JTAG_SETUP (void) {
resets_hw->reset &= ~(RESETS_RESET_IO_BANK0_BITS | RESETS_RESET_PADS_BANK0_BITS);
/* set to default high level */
@ -281,7 +274,6 @@ Configures the DAP Hardware I/O pins for Serial Wire Debug (SWD) mode:
- TDI, nTRST to HighZ mode (pins are unused in SWD mode).
*/
__STATIC_INLINE void PORT_SWD_SETUP (void) {
resets_hw->reset &= ~(RESETS_RESET_IO_BANK0_BITS | RESETS_RESET_PADS_BANK0_BITS);
/* set to default high level */
@ -586,5 +578,4 @@ __STATIC_INLINE uint8_t RESET_TARGET (void) {
///@}
#endif /* __DAP_CONFIG_H__ */

View File

@ -28,7 +28,7 @@
#define BOARD_H_MOD
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
#ifdef PICO_DEFAULT_LED_PIN
@ -47,7 +47,7 @@
#endif
#ifdef __cplusplus
}
}
#endif
#endif /* BOARD_H_ */

View File

@ -16,5 +16,13 @@
#define CDC_N_STDIO 2
#endif
/*#define USB_VID 0x2e8a*/ /* Raspberry Pi */
#define USB_VID 0xcafe /* TinyUSB */
/*#define USB_VID 0x1209*/ /* Generic */
/*#define USB_VID 0x1d50*/ /* OpenMoko */
// TODO: other RP2040 boards
#define INFO_BOARDNAME "RP2040 Pico"
#endif

View File

@ -1,21 +1,40 @@
#include <stdint.h>
#include "pico/stdlib.h"
#include "pico/unique_id.h"
#include <pico/stdlib.h>
#include <pico/unique_id.h>
#include "tusb.h"
static uint8_t get_unique_id(uint16_t *desc_str)
{
static inline char nyb2hex(int x) {
if (x < 0xa) return '0'+x;
else return 'A'+x;
}
static uint8_t get_unique_id_u8(uint8_t *desc_str) {
pico_unique_board_id_t uid;
uint8_t chr_count = 0;
pico_get_unique_board_id(&uid);
for (int byte = 0; byte < TU_ARRAY_SIZE(uid.id); byte++)
{
for (int byte = 0; byte < TU_ARRAY_SIZE(uid.id); byte++) {
uint8_t tmp = uid.id[byte];
for (int digit = 0; digit < 2; digit++)
{
desc_str[chr_count++] = "0123456789ABCDEF"[tmp & 0xf];
for (int digit = 0; digit < 2; digit++) {
desc_str[chr_count++] = nyb2hex(tmp & 0xf);
tmp >>= 4;
}
}
return chr_count;
}
static uint8_t get_unique_id_u16(uint16_t *desc_str) {
pico_unique_board_id_t uid;
uint8_t chr_count = 0;
pico_get_unique_board_id(&uid);
for (int byte = 0; byte < TU_ARRAY_SIZE(uid.id); byte++) {
uint8_t tmp = uid.id[byte];
for (int digit = 0; digit < 2; digit++) {
desc_str[chr_count++] = nyb2hex(tmp & 0xf);
tmp >>= 4;
}
}
@ -23,4 +42,3 @@ static uint8_t get_unique_id(uint16_t *desc_str)
return chr_count;
}
#define PRODUCT_PREFIX "RP2040 "

View File

@ -51,6 +51,9 @@ This information includes:
#include <stm32f0xx_hal.h>
#include "protos.h"
#include "unique.h"
/// Processor Clock of the Cortex-M MCU used in the Debug Unit.
/// This value is used to calculate the SWD/JTAG clock speed.
#define CPU_CLOCK 48000000U ///< Specifies the CPU Clock in Hz.
@ -134,8 +137,9 @@ This information includes:
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
(void)str;
return (0U);
const static char vnd[] = INFO_MANUFACTURER;
for (size_t i = 0; i < sizeof(vnd); ++i) str[i] = vnd[i];
return sizeof(vnd)-1;
}
/** Get Product ID string.
@ -143,8 +147,9 @@ __STATIC_INLINE uint8_t DAP_GetVendorString (char *str) {
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
(void)str;
return (0U);
const static char prd[] = INFO_PRODUCT(INFO_BOARDNAME);
for (size_t i = 0; i < sizeof(prd); ++i) str[i] = prd[i];
return sizeof(prd)-1;
}
/** Get Serial Number string.
@ -152,8 +157,7 @@ __STATIC_INLINE uint8_t DAP_GetProductString (char *str) {
\return String length.
*/
__STATIC_INLINE uint8_t DAP_GetSerNumString (char *str) {
(void)str;
return (0U);
return get_unique_id_u8(str);
}
///@}

View File

@ -1,7 +1,6 @@
void cdc_uart_init(void)
{
void cdc_uart_init(void) {
}
void cdc_uart_task(void)
{
void cdc_uart_task(void) {
}

View File

@ -7,5 +7,11 @@
/*#define DBOARD_HAS_SERPROG*/
/*#define DBOARD_HAS_TINYI2C*/
#define USB_VID 0xcafe /* TinyUSB */
/*#define USB_VID 0x1209*/ /* Generic */
/*#define USB_VID 0x1d50*/ /* OpenMoko */
#define INFO_BOARDNAME "STM32F072 Disco"
#endif

View File

@ -1,20 +1,36 @@
#include <stdint.h>
#include "tusb.h"
static uint8_t get_unique_id(uint16_t *desc_str)
{
static inline char nyb2hex(int x) {
if (x < 0xa) return '0'+x;
else return 'A'+x;
}
static uint8_t get_unique_id_u8(uint8_t *desc_str) {
const uint32_t *idpnt = (uint32_t*)(0x1FFFF7AC); /*DEVICE_ID1*/
uint32_t tmp = 0;
uint8_t chr_count = 0;
for (int digit = 0; digit < 24; digit++)
{
for (int digit = 0; digit < 24; digit++) {
if (0 == (digit & 7)) tmp = *idpnt++;
desc_str[chr_count++] = "0123456789ABCDEF"[tmp & 0xf];
desc_str[chr_count++] = nyb2hex(tmp & 0xf);
tmp >>= 4;
}
return chr_count;
}
static uint8_t get_unique_id_u16(uint16_t *desc_str) {
const uint32_t *idpnt = (uint32_t*)(0x1FFFF7AC); /*DEVICE_ID1*/
uint32_t tmp = 0;
uint8_t chr_count = 0;
for (int digit = 0; digit < 24; digit++) {
if (0 == (digit & 7)) tmp = *idpnt++;
desc_str[chr_count++] = nyb2hex(tmp & 0xf);
tmp >>= 4;
}
return chr_count;
}
#define PRODUCT_PREFIX "STM32F072 "

View File

@ -130,7 +130,7 @@ static u32 usb_func(struct i2c_adapter *adapter)
/* get functionality from adapter */
if (!pfunc || (i=usb_read(adapter, CMD_GET_FUNC, 0, 0, pfunc,
sizeof(*pfunc))) != sizeof(*pfunc)) {
dev_err(&adapter->dev, "failure reading functionality: %i\n");
dev_err(&adapter->dev, "failure reading functionality: %i\n", i);
ret = 0;
goto out;
}
@ -160,7 +160,7 @@ static const struct i2c_algorithm usb_algorithm = {
static const struct usb_device_id i2c_tiny_usb_table[] = {
{ USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
{ USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
{ USB_DEVICE_INTERFACE_CLASS(0xcafe, 0x4017, 0/*255*/) }, /* TinyUSB DapperMime: we want the Vendor interface */
{ USB_DEVICE_INTERFACE_CLASS(0x2e8a, 0x6043, 0/*255*/) }, /* TinyUSB DapperMime: we want the Vendor interface */
{ } /* Terminating entry */
};
@ -171,7 +171,6 @@ struct i2c_tiny_usb {
struct usb_device *usb_dev; /* the usb device for this device */
struct usb_interface *interface; /* the interface for this device */
struct i2c_adapter adapter; /* i2c related things */
bool is_tusb_dmime;
};
static int usb_read(struct i2c_adapter *adapter, int cmd,
@ -179,39 +178,8 @@ static int usb_read(struct i2c_adapter *adapter, int cmd,
{
struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
uint8_t *dmadata;
int ret, actual_len = 0;
int ret;
if (dev->is_tusb_dmime) {
// actual_len = len;
// if (actual_len < 7) actual_len = 7; /* cmd(1), value(2), index(2), len(2) */
//
// dmadata = (uint8_t*)kzalloc(actual_len, GFP_KERNEL);
// if (!dmadata)
// return -ENOMEM;
//
// dmadata[0] = cmd;
// dmadata[1] = value & 0xff;
// dmadata[2] = (value >> 8) & 0xff;
// dmadata[3] = index & 0xff;
// dmadata[4] = (index >> 8) & 0xff;
// dmadata[5] = len & 0xff;
// dmadata[6] = (len >> 8) & 0xff;
//
// /* send command */
// ret = usb_bulk_msg(dev->usb_dev, usb_sndbulkpipe(dev->usb_dev, 9),
// dmadata, 7, &actual_len, 2000);
//
// dev_warn(&dev->usb_dev->dev, "read bulk msg retval=%i\n", ret);
//
// /* command received correctly */
// if (ret == 7) {
// actual_len = 0;
// /* receive reply */
// ret = usb_bulk_msg(dev->usb_dev, usb_rcvbulkpipe(dev->usb_dev, 9),
// dmadata, len, &actual_len, 2000);
//
// memcpy(data, dmadata, len);
// }
dmadata = kmalloc(len, GFP_KERNEL);
if (!dmadata)
@ -223,19 +191,6 @@ static int usb_read(struct i2c_adapter *adapter, int cmd,
value, index, dmadata, len, 2000);
memcpy(data, dmadata, len);
} else {
dmadata = kmalloc(len, GFP_KERNEL);
if (!dmadata)
return -ENOMEM;
/* do control transfer */
ret = usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_IN,
value, index, dmadata, len, 2000);
memcpy(data, dmadata, len);
}
kfree(dmadata);
return ret;
@ -246,27 +201,8 @@ static int usb_write(struct i2c_adapter *adapter, int cmd,
{
struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
uint8_t *dmadata;
int ret, actual_len = 0;
int ret;
if (dev->is_tusb_dmime) {
// dmadata = (uint8_t*)kzalloc(len + 7, GFP_KERNEL); /* cmd(1), value(2), index(2), len(2) */
// if (!dmadata)
// return -ENOMEM;
//
// dmadata[0] = cmd;
// dmadata[1] = value & 0xff;
// dmadata[2] = (value >> 8) & 0xff;
// dmadata[3] = index & 0xff;
// dmadata[4] = (index >> 8) & 0xff;
// dmadata[5] = len & 0xff;
// dmadata[6] = (len >> 8) & 0xff;
//
// if (data && len)
// memcpy(&dmadata[7], data, len);
//
// /* send command data */
// ret = usb_bulk_msg(dev->usb_dev, usb_sndbulkpipe(dev->usb_dev, 9),
// dmadata, len+7, &actual_len, 2000);
dmadata = (uint8_t*)kmemdup(data, len, GFP_KERNEL);
if (!dmadata)
return -ENOMEM;
@ -275,16 +211,6 @@ static int usb_write(struct i2c_adapter *adapter, int cmd,
ret = usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
value, index, dmadata, len, 2000);
} else {
dmadata = (uint8_t*)kmemdup(data, len, GFP_KERNEL);
if (!dmadata)
return -ENOMEM;
/* do control transfer */
ret = usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
value, index, dmadata, len, 2000);
}
kfree(dmadata);
return ret;
@ -315,10 +241,6 @@ static int i2c_tiny_usb_probe(struct usb_interface *interface,
dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
dev->interface = interface;
/* TinyUSB DapperMime needs different treatment because it has MANY endpoints */
dev->is_tusb_dmime = le16_to_cpu(dev->usb_dev->descriptor.idVendor ) == 0xcafe
&& le16_to_cpu(dev->usb_dev->descriptor.idProduct) == 0x4017;
/* save our data pointer in this interface device */
usb_set_intfdata(interface, dev);

View File

@ -1,4 +1,3 @@
// vim: set et:
#include <stdio.h>
@ -70,60 +69,51 @@ static uint8_t read_byte(void) {
uint8_t rv = rx_buf[rxpos];
++rxpos;
--rxavail;
//printf("r %02x\n",rv);
return rv;
}
static void handle_cmd(void) {
uint32_t nresp = 0;
uint32_t nresp = 0;
uint8_t cmd = read_byte();
uint8_t cmd = read_byte();
switch (cmd) {
case S_CMD_NOP:
//printf("nop\n");
tx_buf[0] = S_ACK;
nresp = 1;
break;
case S_CMD_SYNCNOP:
//printf("snop\n");
tx_buf[0] = S_NAK;
tx_buf[1] = S_ACK;
nresp = 2;
break;
case S_CMD_Q_IFACE:
//printf("q_if\n");
tx_buf[0] = S_ACK;
tx_buf[1] = SERPROG_IFACE_VERSION & 0xff;
tx_buf[2] = (SERPROG_IFACE_VERSION >> 8) & 0xff;
nresp = 3;
break;
case S_CMD_Q_CMDMAP:
//printf("q_cmap\n");
tx_buf[0] = S_ACK;
memcpy(&tx_buf[1], serprog_cmdmap, sizeof serprog_cmdmap);
nresp = sizeof(serprog_cmdmap) + 1;
break;
case S_CMD_Q_PGMNAME:
//printf("q_pgm\n");
tx_buf[0] = S_ACK;
memcpy(&tx_buf[1], serprog_pgmname, sizeof serprog_pgmname);
nresp = sizeof(serprog_pgmname) + 1;
break;
case S_CMD_Q_SERBUF:
//printf("q_sbuf\n");
tx_buf[0] = S_ACK;
tx_buf[1] = sizeof(rx_buf) & 0xff;
tx_buf[2] = (sizeof(rx_buf) >> 8) & 0xff;
nresp = 3;
break;
case S_CMD_Q_BUSTYPE:
//printf("q_btyp\n");
tx_buf[0] = S_ACK;
tx_buf[1] = 1<<3; // SPI only
nresp = 2;
break;
case S_CMD_Q_WRNMAXLEN:
//printf("q_wlen\n");
tx_buf[0] = S_ACK;
tx_buf[1] = (sizeof(tx_buf)-1) & 0xff;
tx_buf[2] = ((sizeof(tx_buf)-1) >> 8) & 0xff;
@ -131,7 +121,6 @@ static void handle_cmd(void) {
nresp = 4;
break;
case S_CMD_Q_RDNMAXLEN:
//printf("q_rlen\n");
tx_buf[0] = S_ACK;
tx_buf[1] = (sizeof(rx_buf)-1) & 0xff;
tx_buf[2] = ((sizeof(rx_buf)-1) >> 8) & 0xff;
@ -139,8 +128,6 @@ static void handle_cmd(void) {
nresp = 4;
break;
case S_CMD_S_BUSTYPE:
//printf("s_btyp\n");
if (read_byte()/* bus type to set */ == (1<<3)) {
tx_buf[0] = S_ACK;
} else {
@ -150,8 +137,6 @@ static void handle_cmd(void) {
break;
case S_CMD_SPIOP: {
//printf("spiop\n");
uint32_t slen, rlen;
slen = (uint32_t)read_byte();
slen |= (uint32_t)read_byte() << 8;
@ -202,7 +187,6 @@ static void handle_cmd(void) {
}
break;
case S_CMD_S_SPI_FREQ: {
//printf("s_spi_freq\n");
uint32_t freq;
freq = (uint32_t)read_byte();
freq |= (uint32_t)read_byte() << 8;
@ -219,8 +203,6 @@ static void handle_cmd(void) {
}
break;
case S_CMD_S_PINSTATE: {
//printf("s_pins\n");
if (read_byte() == 0) sp_spi_cs_deselect();
else sp_spi_cs_select();
@ -240,7 +222,6 @@ static void handle_cmd(void) {
break;
default:
//printf("ill %d\n", cmd);
tx_buf[0] = S_NAK;
nresp = 1;
break;
@ -254,7 +235,6 @@ static void handle_cmd(void) {
void cdc_serprog_task(void) {
handle_cmd();
//printf("d\n");
}
#endif /* DBOARD_HAS_SERPROG */

View File

@ -81,14 +81,18 @@ static bool iub_ctl_req(uint8_t rhport, uint8_t stage, tusb_control_request_t co
case 4: case 5: case 6: case 7: // I2C_IO
{
if (req->wValue & 1) { // read: we need to return shit
printf("read!%c%c%c%c%c\n", ' ', ' ', ' ', ' ', ' ');
//printf("read!%c%c%c%c%c\n", ' ', ' ', ' ', ' ', ' ');
// so, we'll return some garbage
uint8_t buf[req->wLength];
return tud_control_xfer(rhport, req, buf, req->wLength);
} else {
//printf("write!%c%c%c%c%c\n", ' ', ' ', ' ', ' ', ' ');
// ????
return tud_control_status(rhport, req);
uint8_t buf[req->wLength];
bool rv = tud_control_xfer(rhport, req, buf, req->wLength);
printf("rv %c len %04x buf %02x %02x %02x ...\n",
(rv?'t':'f'), req->wLength, buf[0], buf[1], buf[2]);
return rv;
}
}
}

View File

@ -45,23 +45,18 @@
#include <pico/binary_info.h>
#endif
static cothread_t mainthread
#ifdef DBOARD_HAS_UART
, uartthread
#endif
#ifdef DBOARD_HAS_SERPROG
, serprogthread
#endif
#ifdef DBOARD_HAS_I2C
//, ituthread
#endif
;
static cothread_t mainthread;
void thread_yield(void) {
co_switch(mainthread);
}
#define DEFAULT_STACK_SIZE 1024
#ifdef DBOARD_HAS_UART
static cothread_t uartthread;
static uint8_t uartstack[DEFAULT_STACK_SIZE];
static void uart_thread_fn(void) {
cdc_uart_init();
thread_yield();
@ -73,6 +68,9 @@ static void uart_thread_fn(void) {
#endif
#ifdef DBOARD_HAS_SERPROG
static cothread_t serprogthread;
static uint8_t serprogstack[DEFAULT_STACK_SIZE];
static void serprog_thread_fn(void) {
cdc_serprog_init();
thread_yield();
@ -83,43 +81,13 @@ static void serprog_thread_fn(void) {
}
#endif
#ifdef DBOARD_HAS_I2C
/*static void itu_thread_fn(void) {
itu_init();
thread_yield();
while (1) {
itu_task();
thread_yield();
}
}*/
#endif
#ifdef DBOARD_HAS_UART
static uint8_t uartstack[4096];
#endif
#ifdef DBOARD_HAS_SERPROG
static uint8_t serprogstack[4096];
#endif
#ifdef DBOARD_HAS_I2C
static uint8_t itustack[4096];
#endif
// FIXME
extern uint32_t co_active_buffer[64];
uint32_t co_active_buffer[64];
extern cothread_t co_active_handle;
cothread_t co_active_handle;
extern char msgbuf[256];
extern volatile bool msgflag;
static void domsg(void) {
if (msgflag) {
printf("%s", msgbuf);
//msgflag = false;
}
}
int main(void)
{
int main(void) {
mainthread = co_active();
// TODO: split this out in a bsp-specific file
@ -137,10 +105,6 @@ int main(void)
serprogthread = co_derive(serprogstack, sizeof serprogstack, serprog_thread_fn);
co_switch(serprogthread); // will call cdc_serprog_init() on correct thread
#endif
#ifdef DBOARD_HAS_I2C
//ituthread = co_derive(itustack, sizeof itustack, itu_thread_fn);
//co_switch(ituthread);
#endif
#ifdef DBOARD_HAS_CMSISDAP
DAP_Setup();
#endif
@ -151,31 +115,16 @@ int main(void)
stdio_usb_init();
#endif
while (1)
{
//printf("hi\n");
domsg();
while (1) {
tud_task(); // tinyusb device task
#ifdef DBOARD_HAS_UART
//cdc_uart_task();
co_switch(uartthread);
#endif
domsg();
tud_task(); // tinyusb device task
#ifdef DBOARD_HAS_SERPROG
//cdc_serprog_task();
co_switch(serprogthread);
#endif
//domsg();
//tud_task();
#ifdef DBOARD_HAS_I2C
//co_switch(ituthread);
#endif
//printf("hi2\n");
}
return 0;
@ -188,8 +137,8 @@ int main(void)
// Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
{
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id,
hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
// TODO not Implemented
(void) instance;
(void) report_id;
@ -200,8 +149,8 @@ uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_t
return 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)
{
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);
@ -216,3 +165,4 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_
tud_hid_report(0, TxDataBuffer, response_size);
}

View File

@ -6,6 +6,9 @@
#include "protocfg.h"
#define INFO_MANUFACTURER "BLAHAJ CTF"
#define INFO_PRODUCT(board) "Dragnbus (" board ")"
#ifdef DBOARD_HAS_UART
void cdc_uart_init(void);
void cdc_uart_task(void);

View File

@ -7,8 +7,6 @@
#include "rtconf.h"
uint8_t rtconf_do(uint8_t a, uint8_t b) {
//printf("rtconf %02x,%02x\n", a, b);
switch ((enum rtconf_opt)a) {
#ifdef DBOARD_HAS_UART
case opt_uart_hwfc_endis:

View File

@ -27,7 +27,7 @@
#define _TUSB_CONFIG_H__
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
//--------------------------------------------------------------------
@ -115,8 +115,6 @@
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#ifdef __cplusplus
}

View File

@ -25,45 +25,55 @@
#include "tusb.h"
#include "unique.h"
#include "protos.h"
#include "protocfg.h"
// TODO: actually do this properly
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
*
* Auto ProductID layout's Bitmap:
* [MSB] HID | MSC | CDC [LSB]
*/
#ifdef DBOARD_HAS_I2C
#define USB_PID_BASE 0x6000
#else
#define USB_PID_BASE 0x4000
#endif
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
#define USB_PID (USB_PID_BASE | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 3) | _PID_MAP(HID, 6) | \
_PID_MAP(MIDI, 9) | _PID_MAP(VENDOR, 12) ) \
// String Descriptor Index
enum
{
enum {
STRID_LANGID = 0,
STRID_MANUFACTURER,
STRID_PRODUCT,
STRID_SERIAL,
STRID_CONFIG,
STRID_IF_HID_CMSISDAP,
STRID_IF_VND_I2CTINYUSB,
STRID_IF_CDC_UART,
STRID_IF_CDC_SERPROG,
STRID_IF_CDC_STDIO,
};
//--------------------------------------------------------------------+
// Device Descriptors
//--------------------------------------------------------------------+
tusb_desc_device_t const desc_device =
{
tusb_desc_device_t const desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0110, // FIXME: 0x0200 ?
.bcdUSB = 0x0110, // TODO: 0x0200 ?
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = 0xCafe, // TODO
.idVendor = USB_VID,
.idProduct = USB_PID,
.bcdDevice = 0x0101,
.bcdDevice = 0x0101, // TODO
.iManufacturer = STRID_MANUFACTURER,
.iProduct = STRID_PRODUCT,
@ -74,8 +84,7 @@ tusb_desc_device_t const desc_device =
// Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor
uint8_t const * tud_descriptor_device_cb(void)
{
uint8_t const * tud_descriptor_device_cb(void) {
return (uint8_t const *) &desc_device;
}
@ -83,16 +92,14 @@ uint8_t const * tud_descriptor_device_cb(void)
// HID Report Descriptor
//--------------------------------------------------------------------+
static uint8_t const desc_hid_report[] =
{
static uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_GENERIC_INOUT(CFG_TUD_HID_EP_BUFSIZE)
};
// Invoked when received GET HID REPORT DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance)
{
uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) {
(void) instance;
return desc_hid_report;
@ -102,8 +109,7 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance)
// Configuration Descriptor
//--------------------------------------------------------------------+
enum
{
enum {
ITF_NUM_HID_CMSISDAP,
ITF_NUM_CDC_UART_COM,
ITF_NUM_CDC_UART_DATA,
@ -121,10 +127,10 @@ enum
9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, 0, 0, 0, _stridx \
/*enum {*/ static const int CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN
enum {
CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN
#ifdef DBOARD_HAS_I2C
+ TUD_I2CTINYUSB_LEN
//+ TUD_VENDOR_DESC_LEN
#endif
#ifdef DBOARD_HAS_UART
+ TUD_CDC_DESC_LEN
@ -138,60 +144,49 @@ enum
#ifdef USE_USBCDC_FOR_STDIO
+ TUD_CDC_DESC_LEN
#endif
/*}*/;
};
#define EPNUM_CDC_UART_OUT 0x02 // 2
#define EPNUM_CDC_UART_IN 0x82 // 83
#define EPNUM_CDC_UART_NOTIF 0x83 // 1
#define EPNUM_HID_CMSISDAP 0x04 // 4,5?
#define EPNUM_CDC_SERPROG_OUT 0x05 // 7
#define EPNUM_CDC_SERPROG_IN 0x85 // 8
#define EPNUM_CDC_SERPROG_NOTIF 0x86 // 6
#define EPNUM_CDC_UART_OUT 0x02
#define EPNUM_CDC_UART_IN 0x82
#define EPNUM_CDC_UART_NOTIF 0x83
#define EPNUM_HID_CMSISDAP 0x04
#define EPNUM_CDC_SERPROG_OUT 0x05
#define EPNUM_CDC_SERPROG_IN 0x85
#define EPNUM_CDC_SERPROG_NOTIF 0x86
#define EPNUM_CDC_STDIO_OUT 0x07
#define EPNUM_CDC_STDIO_IN 0x87
#define EPNUM_CDC_STDIO_NOTIF 0x88
#define EPNUM_VND_I2C_OUT 0x09
#define EPNUM_VND_I2C_IN 0x89
// NOTE: if you modify this table, don't forget to keep tusb_config.h up to date as well!
// TODO: maybe add some strings to all these interfaces
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),
uint8_t const desc_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, STRID_CONFIG, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
#ifdef DBOARD_HAS_CMSISDAP
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID_CMSISDAP, 0, 0/*HID_PROTOCOL_NONE*/, sizeof(desc_hid_report), EPNUM_HID_CMSISDAP, 0x80 | (EPNUM_HID_CMSISDAP+0), CFG_TUD_HID_EP_BUFSIZE, 1),
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID_CMSISDAP, STRID_IF_HID_CMSISDAP, 0/*HID_PROTOCOL_NONE*/, sizeof(desc_hid_report), EPNUM_HID_CMSISDAP, 0x80 | (EPNUM_HID_CMSISDAP+0), CFG_TUD_HID_EP_BUFSIZE, 1),
#endif
#ifdef DBOARD_HAS_I2C
// ???
TUD_I2CTINYUSB_DESCRIPTOR(ITF_NUM_VND_I2CTINYUSB, 0),
//TUD_VENDOR_DESCRIPTOR(ITF_NUM_VND_I2CTINYUSB, 0, EPNUM_VND_I2C_OUT, EPNUM_VND_I2C_IN, 64),
TUD_I2CTINYUSB_DESCRIPTOR(ITF_NUM_VND_I2CTINYUSB, STRID_IF_VND_I2CTINYUSB),
#endif
#ifdef DBOARD_HAS_UART
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_UART_COM, 0, EPNUM_CDC_UART_NOTIF, 64, EPNUM_CDC_UART_OUT, EPNUM_CDC_UART_IN, 64),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_UART_COM, STRID_IF_CDC_UART, EPNUM_CDC_UART_NOTIF, 64, EPNUM_CDC_UART_OUT, EPNUM_CDC_UART_IN, 64),
#endif
#ifdef DBOARD_HAS_SERPROG
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_SERPROG_COM, 0, EPNUM_CDC_SERPROG_NOTIF, 64, EPNUM_CDC_SERPROG_OUT, EPNUM_CDC_SERPROG_IN, 64),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_SERPROG_COM, STRID_IF_CDC_SERPROG, EPNUM_CDC_SERPROG_NOTIF, 64, EPNUM_CDC_SERPROG_OUT, EPNUM_CDC_SERPROG_IN, 64),
#endif
#ifdef USE_USBCDC_FOR_STDIO
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_STDIO_COM, 0, EPNUM_CDC_STDIO_NOTIF, 64, EPNUM_CDC_STDIO_OUT, EPNUM_CDC_STDIO_IN, 64),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_STDIO_COM, STRID_IF_CDC_STDIO, EPNUM_CDC_STDIO_NOTIF, 64, EPNUM_CDC_STDIO_OUT, EPNUM_CDC_STDIO_IN, 64),
#endif
};
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
{
uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
(void) index; // for multiple configurations
return desc_configuration;
}
@ -201,35 +196,40 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
//--------------------------------------------------------------------+
// array of pointer to string descriptors
char const* string_desc_arr [] =
{
char const* string_desc_arr [] = {
[STRID_LANGID] = (const char[]) { 0x09, 0x04 }, // supported language is English (0x0409)
[STRID_MANUFACTURER] = "TinyUSB", // Manufacturer // TODO
[STRID_PRODUCT] = PRODUCT_PREFIX "CMSIS-DAP", // Product // TODO
[STRID_MANUFACTURER] = INFO_MANUFACTURER, // Manufacturer
[STRID_PRODUCT] = INFO_PRODUCT(INFO_BOARDNAME), // Product
[STRID_CONFIG] = "Configuration descriptor",
// max string length check: |||||||||||||||||||||||||||||||
[STRID_IF_HID_CMSISDAP] = "CMSIS-DAP HID interface",
[STRID_IF_VND_I2CTINYUSB] = "I2C-Tiny-USB interface",
[STRID_IF_CDC_UART] = "UART CDC interface",
[STRID_IF_CDC_SERPROG] = "Serprog CDC interface",
[STRID_IF_CDC_STDIO] = "stdio CDC interface (debug)",
};
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)
{
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
static uint16_t _desc_str[32];
(void) langid;
uint8_t chr_count = 0;
if (STRID_LANGID == index)
{
if (STRID_LANGID == index) {
memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2);
chr_count = 1;
} else if (STRID_SERIAL == index)
{
chr_count = get_unique_id(_desc_str + 1);
} else
{
} else if (STRID_SERIAL == index) {
chr_count = get_unique_id_u16(_desc_str + 1);
} else {
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
if (!(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])))
return NULL;
const char* str = string_desc_arr[index];
@ -237,14 +237,14 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
chr_count = TU_MIN(strlen(str), 31);
// Convert ASCII string into UTF-16
for(uint8_t i=0; i<chr_count; i++)
{
for (int 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] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
_desc_str[0] = (TUSB_DESC_STRING << 8) | (2*chr_count + 2);
return _desc_str;
}