Skip to content

Commit

Permalink
Initial changes to support host mode on USB1
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Apr 15, 2021
1 parent 819050c commit 49fbdee
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 353 deletions.
83 changes: 48 additions & 35 deletions USBHost_t36.h
Expand Up @@ -272,47 +272,56 @@ struct Transfer_struct {

class USBHost {
public:
static void begin();
static void Task();
void begin();
void Task();
void isr();
void enumeration(const Transfer_t *transfer);
static void countFree(uint32_t &devices, uint32_t &pipes, uint32_t &trans, uint32_t &strs);
protected:
static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
uint32_t direction, uint32_t maxlen, uint32_t interval=0);
static bool queue_Control_Transfer(Device_t *dev, setup_t *setup,
bool queue_Control_Transfer(Device_t *dev, setup_t *setup,
void *buf, USBDriver *driver);
static bool queue_Data_Transfer(Pipe_t *pipe, void *buffer,
bool queue_Data_Transfer(Pipe_t *pipe, void *buffer,
uint32_t len, USBDriver *driver);
static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
static void disconnect_Device(Device_t *dev);
static void enumeration(const Transfer_t *transfer);
static void driver_ready_for_device(USBDriver *driver);
static volatile bool enumeration_busy;
Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
void disconnect_Device(Device_t *dev);
void driver_ready_for_device(USBDriver *driver);
// Pointer to an array of two bools, one for each of USB1 and USB2
static bool * volatile enumeration_busy;
public: // Maybe others may want/need to contribute memory example HID devices may want to add transfers.
static void contribute_Devices(Device_t *devices, uint32_t num);
static void contribute_Pipes(Pipe_t *pipes, uint32_t num);
void contribute_Devices(Device_t *devices, uint32_t num);
void contribute_Pipes(Pipe_t *pipes, uint32_t num);
static void contribute_Transfers(Transfer_t *transfers, uint32_t num);
static void contribute_String_Buffers(strbuf_t *strbuf, uint32_t num);
void contribute_String_Buffers(strbuf_t *strbuf, uint32_t num);
// The standard host port is port 2; the port that is usually a device port
// (but can also be used as a host port) is port 1
uint8_t host_port;
private:
static void isr();
static void convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer);
static void claim_drivers(Device_t *dev);
static uint32_t assign_address(void);
static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
static void init_Device_Pipe_Transfer_memory(void);
static Device_t * allocate_Device(void);
static void delete_Pipe(Pipe_t *pipe);
static void free_Device(Device_t *q);
static Pipe_t * allocate_Pipe(void);
static void free_Pipe(Pipe_t *q);
static Transfer_t * allocate_Transfer(void);
void convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer);
void claim_drivers(Device_t *dev);
uint32_t assign_address(void);
bool address_in_use(uint32_t addr);
bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
void init_Device_Pipe_Transfer_memory(void);
Device_t * allocate_Device(void);
void delete_Pipe(Pipe_t *pipe);
void free_Device(Device_t *q);
Pipe_t * allocate_Pipe(void);
void free_Pipe(Pipe_t *q);
Transfer_t * allocate_Transfer(void);
static void free_Transfer(Transfer_t *q);
static strbuf_t * allocate_string_buffer(void);
static void free_string_buffer(strbuf_t *strbuf);
static bool allocate_interrupt_pipe_bandwidth(Pipe_t *pipe,
strbuf_t * allocate_string_buffer(void);
void free_string_buffer(strbuf_t *strbuf);
bool allocate_interrupt_pipe_bandwidth(Pipe_t *pipe,
uint32_t maxlen, uint32_t interval);
static void add_qh_to_periodic_schedule(Pipe_t *pipe);
static bool followup_Transfer(Transfer_t *transfer);
static void followup_Error(void);
void add_qh_to_periodic_schedule(Pipe_t *pipe);
bool followup_Transfer(Transfer_t *transfer);
void followup_Error(void);
void add_to_async_followup_list(Transfer_t *, Transfer_t *);
void remove_from_async_followup_list(Transfer_t *);
void add_to_periodic_followup_list(Transfer_t *, Transfer_t *);
void remove_from_periodic_followup_list(Transfer_t *);
protected:
#ifdef USBHOST_PRINT_DEBUG
static void print_(const Transfer_t *transfer);
Expand Down Expand Up @@ -487,7 +496,7 @@ class USBDriver : public USBHost {
// Device drivers may create these timer objects to schedule a timer call
class USBDriverTimer {
public:
USBDriverTimer() { }
USBDriverTimer(uint8_t host_port) : host_port(host_port) { }
USBDriverTimer(USBDriver *d) : driver(d) { }
USBDriverTimer(USBHIDInput *hd) : driver(nullptr), hidinput(hd) { }

Expand All @@ -504,6 +513,7 @@ class USBDriverTimer {
USBDriverTimer *next;
USBDriverTimer *prev;
friend class USBHost;
uint8_t host_port;
};

// Device drivers may inherit from this base class, if they wish to receive
Expand Down Expand Up @@ -656,7 +666,8 @@ class USBHub : public USBDriver {
portbitmask_t send_pending_clearstatus_reset;
portbitmask_t send_pending_setreset;
portbitmask_t debounce_in_use;
static volatile bool reset_busy;
// Pointer to an array of two bools, one for each of USB1 and USB2
static bool * volatile reset_busy;
};

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -1414,7 +1425,8 @@ class USBSerialBase: public USBDriver, public Stream {
_bigBuffer(big_buffer),
_big_buffer_size(buffer_size),
_min_rxtx(min_pipe_rxtx),
_max_rxtx(max_pipe_rxtx)
_max_rxtx(max_pipe_rxtx),
host_port(host.host_port)
{

init();
Expand Down Expand Up @@ -1456,7 +1468,8 @@ class USBSerialBase: public USBDriver, public Stream {
uint16_t _big_buffer_size;
uint16_t _min_rxtx;
uint16_t _max_rxtx;

// Standard host port is port 2 (USB2); the port that is usually a device port is port 1 (USB1)
uint8_t host_port;

setup_t setup;
uint8_t setupdata[16]; //
Expand Down
8 changes: 4 additions & 4 deletions antplus.cpp
Expand Up @@ -195,9 +195,9 @@ size_t AntPlus::write(const void *data, const size_t size)
//print("head=", txhead);
//println(", tail=", txtail);
//print_hexbytes(txbuffer, 60);
NVIC_DISABLE_IRQ(IRQ_USBHS);
NVIC_DISABLE_IRQ(IRQ_USBHS(host_port));
transmit();
NVIC_ENABLE_IRQ(IRQ_USBHS);
NVIC_ENABLE_IRQ(IRQ_USBHS(host_port));
return size;
}

Expand Down Expand Up @@ -252,10 +252,10 @@ void AntPlus::Task()
uint32_t len = rxlen;
if (len) {
handleMessages(rxpacket, len);
NVIC_DISABLE_IRQ(IRQ_USBHS);
NVIC_DISABLE_IRQ(IRQ_USBHS(host_port));
queue_Data_Transfer(rxpipe, rxpacket, 64, this);
rxlen = 0;
NVIC_ENABLE_IRQ(IRQ_USBHS);
NVIC_ENABLE_IRQ(IRQ_USBHS(host_port));
}
if (do_polling) {
do_polling = false;
Expand Down

0 comments on commit 49fbdee

Please sign in to comment.