This commit is contained in:
Triss 2021-09-20 01:29:26 +02:00
parent 22e8f70e30
commit 334d45ee83
6 changed files with 115 additions and 31 deletions

View File

@ -15,17 +15,20 @@ enum {
};
enum {
#ifdef USE_USBCDC_FOR_STDIO
CDC_N_STDIO,
CDC_N_STDIO = 0,
#endif
CDC_N__NITF
};
enum {
VND_N_FTDI_IFA = 0,
VND_N_FTDI_IFB,
VND_N_CFG,
/*VND_N_FTDI_IFA = 0,
VND_N_FTDI_IFB,*/
VND_N_CFG = 0,
VND_N__NITF
};
#define VND_N_FTDI_IFA 42
#define VND_N_FTDI_IFB 69
#endif

52
bsp/rp2040/pico/assert.h Normal file
View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _PICO_ASSERT_H
#define _PICO_ASSERT_H
#include <stdbool.h>
#ifdef __cplusplus
#include <cassert>
extern "C" {
#else
#include <assert.h>
#endif
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLE_ALL, Global assert enable, type=bool, default=0, group=pico_base
// PICO_CONFIG: PARAM_ASSERTIONS_DISABLE_ALL, Global assert disable, type=bool, default=0, group=pico_base
#ifndef PARAM_ASSERTIONS_ENABLE_ALL
#define PARAM_ASSERTIONS_ENABLE_ALL 0
#endif
#ifndef PARAM_ASSERTIONS_DISABLE_ALL
#define PARAM_ASSERTIONS_DISABLE_ALL 0
#endif
#define PARAM_ASSERTIONS_ENABLED(x) ((PARAM_ASSERTIONS_ENABLED_ ## x || PARAM_ASSERTIONS_ENABLE_ALL) && !PARAM_ASSERTIONS_DISABLE_ALL)
#define invalid_params_if(x, test) ({if (PARAM_ASSERTIONS_ENABLED(x)) assert(!(test));})
#define valid_params_if(x, test) ({if (PARAM_ASSERTIONS_ENABLED(x)) assert(test);})
#define hard_assert_if(x, test) ({if (PARAM_ASSERTIONS_ENABLED(x)) hard_assert(!(test), #x);})
#ifdef NDEBUG
/*extern void hard_assertion_failure(void);
static inline void hard_assert(bool condition, ...) {
if (!condition)
hard_assertion_failure();
}*/
#define hard_assert(cond, ...) do { if (!(cond)) panic("Hard assert: " __FILE__); } while (0)
#else
#define hard_assert assert
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -32,10 +32,12 @@ class UsbConn(DevConn):
import usb, usb.core
cfg = dev.get_active_configuration()
#print("get active", cfg)
if cfg is None: # should be configured already, but eh
dev.set_configuration()
cfg = dev.get_active_configuration()
#print("set active", cfg)
if cfg is None:
return "Couldn't get or set device configuration, aaaaa"
@ -45,6 +47,7 @@ class UsbConn(DevConn):
if i.bInterfaceClass == usb.CLASS_VENDOR_SPEC and
i.bInterfaceSubClass == UsbConn._SUBCLASS and
i.bInterfaceProtocol == UsbConn._PROTOCOL]
#print("vnd itf", itf)
if len(itf) == 0:
return "No vendor control interface found for device"
@ -58,12 +61,18 @@ class UsbConn(DevConn):
epin = usb.util.find_descriptor(itf, custom_match =
lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
#print("epout", epout, "epin", epin)
try:
# try to read the version number. if it throws, it means the usbdev
# is in use by something else
#print("write")
epout.write(b'\x00')
#print("wrote")
resp = epin.read(4)
except usb.core.USBError:
#print("resp", resp)
except usb.core.USBError as e:
print("eep", e)
return "Device is busy, already used by something else? (If you use "+\
"the kernel module, use a character device from /dev instead.)"
@ -77,6 +86,7 @@ class UsbConn(DevConn):
if verno > DevConn._VER_MAX:
return "Version of device (%04x) too new, must be max. %04x" \
% (hex(verno, DevConn._VER_MAX))
#print("verno", verno)
return UsbConn(dev, cfg, itf, epin, epout)
@ -89,6 +99,8 @@ class UsbConn(DevConn):
if dev is None or len(dev) != 1:
return None
#print("devs", dev)
rv = UsbConn._open_dev(dev[0])
return None if isinstance(rv, str) else rv
@ -127,6 +139,8 @@ class UsbConn(DevConn):
if conntup is None:
return "Could not open USB device '%s': not recognised" % conn
#print("conntup", conntup)
dev = None
if conn_busdev:
if len(conntup) == 2:
@ -148,9 +162,12 @@ class UsbConn(DevConn):
return UsbConn._open_dev(dev[0])
def read_raw(self, arr) -> int:
return self._epin.read(arr)
rv = self._epin.read(arr)
print("read", arr[:rv])
return rv
def write_raw(self, b: bytes) -> int:
print("write", b)
return self._epout.write(b)
def __init__(self, dev, cfg, itf, epin, epout):
@ -168,6 +185,7 @@ class UsbConn(DevConn):
usb.util.release_interface(self._dev, self._itf)
usb.util.dispose_resources(self._dev)
#print("released & disposed")
self._epout = None
self._epin = None
self._itf = None
@ -205,6 +223,7 @@ class ChardevConn(DevConn):
"a Linux kernel module" % sys.platform
try:
#print("try char", conn)
fd = os.open(conn, os.O_RDWR)
if fd < 0:
raise OSError("Negative file descriptor returned")

View File

@ -20,16 +20,16 @@ static cothread_t ftdithread_ifa, ftdithread_ifb;
static uint8_t ftdistack_ifa[THREAD_STACK_SIZE>>1], ftdistack_ifb[THREAD_STACK_SIZE>>1];
static void ftdi_thread_fn_ifa(void) {
printf("fn ifa thread!\n");
while (1) {
printf("fn ifa thread!\n");
//ftdi_task_ifa();
thread_yield();
}
}
static void ftdi_thread_fn_ifb(void) {
printf("fn ifb thread!\n");
while (1) {
printf("fn ifb thread!\n");
//ftdi_task_ifb();
thread_yield();
}
@ -37,16 +37,16 @@ static void ftdi_thread_fn_ifb(void) {
#endif
static void enter_cb(void) {
printf("mode5 enter begin\n");
printf("mode5 enter begin VND_N_CONFIG=%d\n", VND_N_CFG);
#ifdef USE_USBCDC_FOR_STDIO
stdio_usb_set_itf_num(CDC_N_STDIO);
#endif
vnd_cfg_set_itf_num(VND_N_CFG);
//vnd_cfg_set_itf_num(VND_N_CFG);
#ifdef DBOARD_HAS_FTDI
ftdithread_ifa = co_derive(ftdistack_ifa, sizeof ftdistack_ifa, ftdi_thread_fn_ifa);
ftdithread_ifb = co_derive(ftdistack_ifb, sizeof ftdistack_ifb, ftdi_thread_fn_ifb);
/*ftdithread_ifa = co_derive(ftdistack_ifa, sizeof ftdistack_ifa, ftdi_thread_fn_ifa);
ftdithread_ifb = co_derive(ftdistack_ifb, sizeof ftdistack_ifb, ftdi_thread_fn_ifb);*/
#endif
/*if (!data_dirty) {
@ -69,10 +69,10 @@ static void leave_cb(void) {
static void task_cb(void) {
printf("mode5 task\n");
#ifdef DBOARD_HAS_FTDI
tud_task();
thread_enter(ftdithread_ifa);
tud_task();
thread_enter(ftdithread_ifb);
//tud_task();
//thread_enter(ftdithread_ifa);
//tud_task();
//thread_enter(ftdithread_ifb);
#endif
}
@ -105,8 +105,8 @@ enum {
STRID_IF_CDC_STDIO,
};
enum {
ITF_NUM_VND_FTDI_IFA,
ITF_NUM_VND_FTDI_IFB,
/*ITF_NUM_VND_FTDI_IFA,
ITF_NUM_VND_FTDI_IFB,*/
#if CFG_TUD_VENDOR > 0
ITF_NUM_VND_CFG,
@ -121,8 +121,8 @@ enum {
enum {
CONFIG_TOTAL_LEN
= TUD_CONFIG_DESC_LEN
+ TUD_VENDOR_DESC_LEN
+ TUD_VENDOR_DESC_LEN
/*+ TUD_VENDOR_DESC_LEN
+ TUD_VENDOR_DESC_LEN*/
#if CFG_TUD_VENDOR > 0
+ TUD_VENDOR_DESC_LEN
#endif
@ -131,7 +131,7 @@ enum {
#endif
};
#define EPNUM_VND_FTDI_IFA_OUT 0x02
/*#define EPNUM_VND_FTDI_IFA_OUT 0x02
#define EPNUM_VND_FTDI_IFA_IN 0x81
#define EPNUM_VND_FTDI_IFB_OUT 0x04
#define EPNUM_VND_FTDI_IFB_IN 0x83
@ -140,17 +140,23 @@ enum {
#define EPNUM_VND_CFG_IN 0x85
#define EPNUM_CDC_STDIO_OUT 0x06
#define EPNUM_CDC_STDIO_IN 0x86
#define EPNUM_CDC_STDIO_NOTIF 0x87
#define EPNUM_CDC_STDIO_NOTIF 0x87*/
#define EPNUM_VND_CFG_OUT 0x01
#define EPNUM_VND_CFG_IN 0x81
#define EPNUM_CDC_STDIO_OUT 0x02
#define EPNUM_CDC_STDIO_IN 0x82
#define EPNUM_CDC_STDIO_NOTIF 0x83
// clang-format off
static const uint8_t desc_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM__TOTAL, STRID_CONFIG, CONFIG_TOTAL_LEN,
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
TUD_VENDOR_DESCRIPTOR_EX(ITF_NUM_VND_FTDI_IFA, STRID_IF_VND_FTDI_IFA,
EPNUM_VND_FTDI_IFA_OUT, EPNUM_VND_FTDI_IFA_IN, CFG_TUD_VENDOR_RX_BUFSIZE, 255, 255),
TUD_VENDOR_DESCRIPTOR_EX(ITF_NUM_VND_FTDI_IFB, STRID_IF_VND_FTDI_IFB,
EPNUM_VND_FTDI_IFB_OUT, EPNUM_VND_FTDI_IFB_IN, CFG_TUD_VENDOR_RX_BUFSIZE, 255, 255),
/*TUD_VENDOR_DESCRIPTOR(ITF_NUM_VND_FTDI_IFA, STRID_IF_VND_FTDI_IFA,
EPNUM_VND_FTDI_IFA_OUT, EPNUM_VND_FTDI_IFA_IN, CFG_TUD_VENDOR_RX_BUFSIZE),
TUD_VENDOR_DESCRIPTOR(ITF_NUM_VND_FTDI_IFB, STRID_IF_VND_FTDI_IFB,
EPNUM_VND_FTDI_IFB_OUT, EPNUM_VND_FTDI_IFB_IN, CFG_TUD_VENDOR_RX_BUFSIZE),*/
#if CFG_TUD_VENDOR > 0
TUD_VENDOR_DESCRIPTOR_EX(ITF_NUM_VND_CFG, STRID_IF_VND_CFG, EPNUM_VND_CFG_OUT,
@ -236,12 +242,12 @@ struct mode m_05_ftdi = {
.task = task_cb,
.handle_cmd = handle_cmd_cb,
.storage = {
/*.storage = {
.stclass = mode_storage_512b,
.get_size = my_get_size,
.get_data = my_get_data,
.is_dirty = my_is_dirty
},
},*/
#ifdef DBOARD_HAS_FTDI
//.tud_descriptor_device_cb = my_descriptor_device_cb,

View File

@ -100,7 +100,7 @@ void modes_switch(uint8_t newmode) {
// maybe wait a second or so for the host to notice this
sleep_ms(500/2);
//printf("disconnect\n");
printf("disconnect\n");
if (newmode == 0) bsp_reset_bootloader();
@ -167,14 +167,14 @@ void modes_switch(uint8_t newmode) {
// clang-format on
}
//printf("reconnect\n");
printf("reconnect\n");
// and reconnect
tud_connect();
sleep_ms(500/2);
//while (!tud_mounted()) sleep_ms(5);
//printf("enter\n");
printf("enter\n");
if (mode_current) mode_current->enter();
}

View File

@ -127,6 +127,8 @@ void vnd_cfg_task(void) {
uint8_t cmd = vnd_cfg_read_byte();
uint8_t verbuf[2];
//printf("vcfg %02x\n", cmd);
if (cmd & 0xf0) {
uint8_t mode = (uint8_t)(cmd & 0xf0) >> 4;
uint8_t mcmd = cmd & 0x0f;
@ -239,6 +241,8 @@ void vnd_cfg_task(void) {
break;
}
}
//printf("vnd cfg cmd=%02x done\n", cmd);
}
#else /* CFG_TUD_VENDOR == 0 */
void vnd_cfg_init(void) { }