28 # error This is a userspace-only header, not allowed by the current build.
31 #include <sifteo/abi.h>
72 return _SYS_usb_isConnected();
88 operator _SYSUsbPacket* () {
return &sys; }
89 operator const _SYSUsbPacket* ()
const {
return &sys; }
93 return _SYS_USB_PACKET_BYTES;
122 const uint8_t *
bytes()
const {
133 ASSERT(type <= 0xfffffff);
158 template <
unsigned tCapacity >
162 _SYSIoQueueHeader header;
165 _SYSUsbPacket packets[tCapacity + 1];
169 operator _SYSUsbQueue* () {
return reinterpret_cast<_SYSUsbQueue*
>(&sys); }
170 operator const _SYSUsbQueue* ()
const {
return reinterpret_cast<const _SYSUsbQueue*
>(&sys); }
172 unsigned capacity()
const {
179 unsigned lastIndex = tCapacity;
185 sys.header32 = lastIndex << 16;
187 ASSERT(sys.header.head == 0);
188 ASSERT(sys.header.tail == 0);
189 ASSERT(sys.header.last == lastIndex);
194 unsigned size = tCapacity + 1;
195 return (sys.header.tail + 1) % size == sys.header.head;
200 return sys.header.tail == sys.header.head;
233 unsigned head = sys.header.head;
234 ASSERT(head <= tCapacity);
235 return *
reinterpret_cast<const UsbPacket *
>(&sys.packets[head]);
247 unsigned head = sys.header.head + 1;
248 if (head > tCapacity)
250 sys.header.head = head;
284 unsigned tail = sys.header.tail;
285 ASSERT(tail <= tCapacity);
286 return *
reinterpret_cast<UsbPacket *
>(&sys.packets[tail]);
298 unsigned tail = sys.header.tail + 1;
299 if (tail > tCapacity)
301 sys.header.tail = tail;
314 _SYS_usb_queueWriteHint();
347 template <
unsigned tSendCapacity = 4,
unsigned tReceiveCapacity = 4 >
375 receiveQueue.
clear();
376 _SYS_usb_setPipe(sendQueue, receiveQueue);
387 _SYS_usb_setPipe(0, 0);
401 if (!sendQueue.
full()) {
402 sendQueue.
write(buffer);
416 if (!receiveQueue.
empty()) {
417 receiveQueue.
read(buffer);
425 return !receiveQueue.
empty();
430 return !sendQueue.
full();
450 _SYSUsbCounters current;
451 _SYSUsbCounters base;
460 _SYS_usb_counters(&base,
sizeof base);
467 _SYS_usb_counters(¤t,
sizeof current);
472 return current.rxPackets - base.rxPackets;
477 return current.txPackets - base.txPackets;
482 return current.rxBytes - base.rxBytes;
487 return current.txBytes - base.txBytes;
498 return current.rxUserDropped - base.rxUserDropped;
A memory buffer which holds a queue of USB packets.
Definition: usb.h:159
uint32_t sentPackets()
Total sent packets.
Definition: usb.h:476
unsigned size() const
Retrieve the size of this packet, in bytes.
Definition: usb.h:103
bool writeAvailable() const
Does this queue have room to write at least one packet?
Definition: usb.h:429
bool readAvailable() const
Does this queue have at least one packet to read?
Definition: usb.h:424
void resize(unsigned bytes)
Change the size of this packet, in bytes.
Definition: usb.h:108
void attach()
Attach this pipe to the system.
Definition: usb.h:373
bool full() const
Is the queue full?
Definition: usb.h:193
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
UsbQueue< tSendCapacity > sendQueue
Queue for packets we're waiting to send.
Definition: usb.h:351
const UsbPacket & peek() const
Access the oldest queued packet without copying it.
Definition: usb.h:230
void pop()
Dequeue the oldest packet.
Definition: usb.h:244
Diagnostic counters for the USB subsystem.
Definition: usb.h:448
void capture()
Update the state of all counters.
Definition: usb.h:466
uint32_t receivedPackets()
Total received packets.
Definition: usb.h:471
bool empty() const
Is the queue empty?
Definition: usb.h:199
A container for one Usb packet's data.
Definition: usb.h:84
unsigned type() const
Return the packet's 28-bit type code.
Definition: usb.h:127
bool write(const UsbPacket &buffer)
Write one packet to the send queue, if space is available.
Definition: usb.h:399
A memory buffer for bidirectional USB communications.
Definition: usb.h:348
void reset()
Reset all counters back to zero.
Definition: usb.h:459
uint8_t * bytes()
Return a pointer to this packet's payload bytes.
Definition: usb.h:119
void clear()
Set the packet's length and type to zero.
Definition: usb.h:97
uint32_t receivedBytes()
Total received bytes.
Definition: usb.h:481
bool read(UsbPacket &buffer)
If a packet is available in the queue, read it.
Definition: usb.h:414
static bool isConnected()
Is a device currently connected to a host via USB?
Definition: usb.h:71
void setType(unsigned type)
Set a packet's 28-bit type.
Definition: usb.h:132
static unsigned capacity()
Retrieve the capacity of a packet, in bytes. (19)
Definition: usb.h:92
Global USB operations.
Definition: usb.h:59
uint32_t sentBytes()
Total sent bytes.
Definition: usb.h:486
void write(const UsbPacket &buffer)
Copy a new packet into the queue, from a provided buffer.
Definition: usb.h:263
bool empty() const
Is this packet's payload empty?
Definition: usb.h:114
void detach()
Detach all pipes from the system.
Definition: usb.h:386
UsbQueue< tReceiveCapacity > receiveQueue
Queue for packets that have been received but not yet processed.
Definition: usb.h:354
UsbPacket & reserve()
Access a buffer slot where a new packet can be written.
Definition: usb.h:281
void read(UsbPacket &buffer)
Read the oldest queued packet into a provided buffer.
Definition: usb.h:213
uint32_t userPacketsDropped()
Total user-defined packets dropped.
Definition: usb.h:497
void clear()
Initializes this queue's header, and marks it as empty.
Definition: usb.h:177
#define STATIC_ASSERT(_x)
Definition: macros.h:342
void commit()
Finish writing a packet that was started with reserve()
Definition: usb.h:295