Merge pull request #3 from hathach/PanRe-uac2-fix-build

Pan re uac2 fix build
This commit is contained in:
PanRe
2020-10-07 10:19:58 +02:00
committed by GitHub
14 changed files with 966 additions and 467 deletions
+1
View File
@@ -27,6 +27,7 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
example: example:
- 'device/audio_test'
- 'device/board_test' - 'device/board_test'
- 'device/cdc_dual_ports' - 'device/cdc_dual_ports'
- 'device/cdc_msc' - 'device/cdc_msc'
+19 -5
View File
@@ -137,6 +137,7 @@ void audio_task(void)
bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff) bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff)
{ {
(void) rhport; (void) rhport;
(void) pBuff;
// We do not support any set range requests here, only current value requests // We do not support any set range requests here, only current value requests
TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR); TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR);
@@ -146,6 +147,8 @@ bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_re
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t ep = TU_U16_LOW(p_request->wIndex); uint8_t ep = TU_U16_LOW(p_request->wIndex);
(void) channelNum; (void) ctrlSel; (void) ep;
return false; // Yet not implemented return false; // Yet not implemented
} }
@@ -153,6 +156,7 @@ bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_re
bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff) bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff)
{ {
(void) rhport; (void) rhport;
(void) pBuff;
// We do not support any set range requests here, only current value requests // We do not support any set range requests here, only current value requests
TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR); TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR);
@@ -162,6 +166,8 @@ bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_r
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t itf = TU_U16_LOW(p_request->wIndex); uint8_t itf = TU_U16_LOW(p_request->wIndex);
(void) channelNum; (void) ctrlSel; (void) itf;
return false; // Yet not implemented return false; // Yet not implemented
} }
@@ -176,19 +182,21 @@ bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const *
uint8_t itf = TU_U16_LOW(p_request->wIndex); uint8_t itf = TU_U16_LOW(p_request->wIndex);
uint8_t entityID = TU_U16_HIGH(p_request->wIndex); uint8_t entityID = TU_U16_HIGH(p_request->wIndex);
(void) itf;
// We do not support any set range requests here, only current value requests // We do not support any set range requests here, only current value requests
TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR); TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR);
// If request is for our feature unit // If request is for our feature unit
if (entityID == 2) if ( entityID == 2 )
{ {
switch (ctrlSel) switch ( ctrlSel )
{ {
case AUDIO_FU_CTRL_MUTE: case AUDIO_FU_CTRL_MUTE:
// Request uses format layout 1 // Request uses format layout 1
TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_1_t)); TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_1_t));
mute[channelNum] = ((audio_control_cur_1_t *)pBuff)->bCur; mute[channelNum] = ((audio_control_cur_1_t*) pBuff)->bCur;
TU_LOG2(" Set Mute: %d of channel: %u\r\n", mute[channelNum], channelNum); TU_LOG2(" Set Mute: %d of channel: %u\r\n", mute[channelNum], channelNum);
@@ -198,14 +206,16 @@ bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const *
// Request uses format layout 2 // Request uses format layout 2
TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_2_t)); TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_2_t));
volume[channelNum] = ((audio_control_cur_2_t *)pBuff)->bCur; volume[channelNum] = ((audio_control_cur_2_t*) pBuff)->bCur;
TU_LOG2(" Set Volume: %d dB of channel: %u\r\n", volume[channelNum], channelNum); TU_LOG2(" Set Volume: %d dB of channel: %u\r\n", volume[channelNum], channelNum);
return true; return true;
// Unknown/Unsupported control // Unknown/Unsupported control
default: TU_BREAKPOINT(); return false; default:
TU_BREAKPOINT();
return false;
} }
} }
return false; // Yet not implemented return false; // Yet not implemented
@@ -221,6 +231,8 @@ bool tud_audio_get_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_re
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t ep = TU_U16_LOW(p_request->wIndex); uint8_t ep = TU_U16_LOW(p_request->wIndex);
(void) channelNum; (void) ctrlSel; (void) ep;
// return tud_control_xfer(rhport, p_request, &tmp, 1); // return tud_control_xfer(rhport, p_request, &tmp, 1);
return false; // Yet not implemented return false; // Yet not implemented
@@ -236,6 +248,8 @@ bool tud_audio_get_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_r
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t itf = TU_U16_LOW(p_request->wIndex); uint8_t itf = TU_U16_LOW(p_request->wIndex);
(void) channelNum; (void) ctrlSel; (void) itf;
return false; // Yet not implemented return false; // Yet not implemented
} }
+1
View File
@@ -33,6 +33,7 @@ SRC_C += \
src/common/tusb_fifo.c \ src/common/tusb_fifo.c \
src/device/usbd.c \ src/device/usbd.c \
src/device/usbd_control.c \ src/device/usbd_control.c \
src/class/audio/audio_device.c \
src/class/cdc/cdc_device.c \ src/class/cdc/cdc_device.c \
src/class/dfu/dfu_rt_device.c \ src/class/dfu/dfu_rt_device.c \
src/class/hid/hid_device.c \ src/class/hid/hid_device.c \
+36 -20
View File
@@ -469,28 +469,44 @@ typedef enum
/// Additional Audio Device Class Codes - Source: Audio Data Formats /// Additional Audio Device Class Codes - Source: Audio Data Formats
/// A.1 - Audio Class-Format Type Codes UAC2 /// A.1 - Audio Class-Format Type Codes UAC2
typedef enum //typedef enum
{ //{
AUDIO_FORMAT_TYPE_UNDEFINED = 0x00, // AUDIO_FORMAT_TYPE_UNDEFINED = 0x00,
AUDIO_FORMAT_TYPE_I = 0x01, // AUDIO_FORMAT_TYPE_I = 0x01,
AUDIO_FORMAT_TYPE_II = 0x02, // AUDIO_FORMAT_TYPE_II = 0x02,
AUDIO_FORMAT_TYPE_III = 0x03, // AUDIO_FORMAT_TYPE_III = 0x03,
AUDIO_FORMAT_TYPE_IV = 0x04, // AUDIO_FORMAT_TYPE_IV = 0x04,
AUDIO_EXT_FORMAT_TYPE_I = 0x81, // AUDIO_EXT_FORMAT_TYPE_I = 0x81,
AUDIO_EXT_FORMAT_TYPE_II = 0x82, // AUDIO_EXT_FORMAT_TYPE_II = 0x82,
AUDIO_EXT_FORMAT_TYPE_III = 0x83, // AUDIO_EXT_FORMAT_TYPE_III = 0x83,
} audio_format_type_t; //} audio_format_type_t;
#define AUDIO_FORMAT_TYPE_UNDEFINED 0x00
#define AUDIO_FORMAT_TYPE_I 0x01
#define AUDIO_FORMAT_TYPE_II 0x02
#define AUDIO_FORMAT_TYPE_III 0x03
#define AUDIO_FORMAT_TYPE_IV 0x04
#define AUDIO_EXT_FORMAT_TYPE_I 0x81
#define AUDIO_EXT_FORMAT_TYPE_II 0x82
#define AUDIO_EXT_FORMAT_TYPE_III 0x83
/// A.2.1 - Audio Class-Audio Data Format Type I UAC2 /// A.2.1 - Audio Class-Audio Data Format Type I UAC2
typedef enum //typedef enum
{ //{
AUDIO_DATA_FORMAT_TYPE_I_PCM = (uint32_t) (1 << 0), // AUDIO_DATA_FORMAT_TYPE_I_PCM = (uint32_t) (1 << 0),
AUDIO_DATA_FORMAT_TYPE_I_PCM8 = (uint32_t) (1 << 1), // AUDIO_DATA_FORMAT_TYPE_I_PCM8 = (uint32_t) (1 << 1),
AUDIO_DATA_FORMAT_TYPE_I_IEEE_FLOAT = (uint32_t) (1 << 2), // AUDIO_DATA_FORMAT_TYPE_I_IEEE_FLOAT = (uint32_t) (1 << 2),
AUDIO_DATA_FORMAT_TYPE_I_ALAW = (uint32_t) (1 << 3), // AUDIO_DATA_FORMAT_TYPE_I_ALAW = (uint32_t) (1 << 3),
AUDIO_DATA_FORMAT_TYPE_I_MULAW = (uint32_t) (1 << 4), // AUDIO_DATA_FORMAT_TYPE_I_MULAW = (uint32_t) (1 << 4),
AUDIO_DATA_FORMAT_TYPE_I_RAW_DATA = 0x100000000, // AUDIO_DATA_FORMAT_TYPE_I_RAW_DATA = 0x100000000,
} audio_data_format_type_I_t; //} audio_data_format_type_I_t;
#define AUDIO_DATA_FORMAT_TYPE_I_PCM ((uint32_t) (1 << 0))
#define AUDIO_DATA_FORMAT_TYPE_I_PCM8 ((uint32_t) (1 << 1))
#define AUDIO_DATA_FORMAT_TYPE_I_IEEE_FLOAT ((uint32_t) (1 << 2))
#define AUDIO_DATA_FORMAT_TYPE_I_ALAW ((uint32_t) (1 << 3))
#define AUDIO_DATA_FORMAT_TYPE_I_MULAW ((uint32_t) (1 << 4))
#define AUDIO_DATA_FORMAT_TYPE_I_RAW_DATA 0x100000000
/// All remaining definitions are taken from the descriptor descriptions in the UAC2 main specification /// All remaining definitions are taken from the descriptor descriptions in the UAC2 main specification
+6 -2
View File
@@ -564,9 +564,10 @@ static bool audiod_tx_done_type_I_pcm_ff_cb(uint8_t rhport, audiod_interface_t*
for (cntChannel = 1; cntChannel < CFG_TUD_AUDIO_N_CHANNELS_TX; cntChannel++) for (cntChannel = 1; cntChannel < CFG_TUD_AUDIO_N_CHANNELS_TX; cntChannel++)
{ {
if (audio->tx_ff[cntChannel].count / CFG_TUD_AUDIO_TX_ITEMSIZE < nSamplesPerChannelToSend) uint16_t const count = tu_fifo_count(&audio->tx_ff[cntChannel]);
if (count / CFG_TUD_AUDIO_TX_ITEMSIZE < nSamplesPerChannelToSend)
{ {
nSamplesPerChannelToSend = audio->tx_ff[cntChannel].count * CFG_TUD_AUDIO_TX_ITEMSIZE; nSamplesPerChannelToSend = count * CFG_TUD_AUDIO_TX_ITEMSIZE;
} }
} }
@@ -782,6 +783,8 @@ void audiod_reset(uint8_t rhport)
uint16_t audiod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) uint16_t audiod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
{ {
(void) max_len;
TU_VERIFY ( TUSB_CLASS_AUDIO == itf_desc->bInterfaceClass && TU_VERIFY ( TUSB_CLASS_AUDIO == itf_desc->bInterfaceClass &&
AUDIO_SUBCLASS_CONTROL == itf_desc->bInterfaceSubClass); AUDIO_SUBCLASS_CONTROL == itf_desc->bInterfaceSubClass);
@@ -1171,6 +1174,7 @@ bool audiod_control_request(uint8_t rhport, tusb_control_request_t const * p_req
bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
{ {
(void) result; (void) result;
(void) xferred_bytes;
// Search for interface belonging to given end point address and proceed as required // Search for interface belonging to given end point address and proceed as required
uint8_t idxDriver; uint8_t idxDriver;
+8 -1
View File
@@ -33,7 +33,6 @@
#include "device/usbd.h" #include "device/usbd.h"
#include "audio.h" #include "audio.h"
#include "tusb_config.h"
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Class Driver Configuration // Class Driver Configuration
@@ -59,6 +58,10 @@
#define CFG_TUD_AUDIO_TX_FIFO_SIZE 0 // Buffer size per channel #define CFG_TUD_AUDIO_TX_FIFO_SIZE 0 // Buffer size per channel
#endif #endif
#ifndef CFG_TUD_AUDIO_TX_DMA_RINGBUFFER_SIZE
#define CFG_TUD_AUDIO_TX_DMA_RINGBUFFER_SIZE 0
#endif
#if CFG_TUD_AUDIO_TX_FIFO_SIZE && CFG_TUD_AUDIO_TX_DMA_RINGBUFFER_SIZE #if CFG_TUD_AUDIO_TX_FIFO_SIZE && CFG_TUD_AUDIO_TX_DMA_RINGBUFFER_SIZE
#error TX_FIFOs and TX_DMA_RINGBUFFER can not be used simultaneously! #error TX_FIFOs and TX_DMA_RINGBUFFER can not be used simultaneously!
#endif #endif
@@ -193,6 +196,10 @@ uint16_t tud_audio_n_write_ep_in_buffer(uint8_t itf, const void * data, uint16_t
#endif #endif
*/ */
#ifndef CFG_TUD_AUDIO_TX_FIFO_COUNT
#define CFG_TUD_AUDIO_TX_FIFO_COUNT 1
#endif
#if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE #if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE
#if CFG_TUD_AUDIO_TX_FIFO_COUNT > 1 #if CFG_TUD_AUDIO_TX_FIFO_COUNT > 1
uint16_t tud_audio_n_write (uint8_t itf, uint8_t channelId, const void * data, uint16_t len); uint16_t tud_audio_n_write (uint8_t itf, uint8_t channelId, const void * data, uint16_t len);
+4 -4
View File
@@ -47,10 +47,10 @@
#define U16_TO_U8S_BE(u16) TU_U16_HIGH(u16), TU_U16_LOW(u16) #define U16_TO_U8S_BE(u16) TU_U16_HIGH(u16), TU_U16_LOW(u16)
#define U16_TO_U8S_LE(u16) TU_U16_LOW(u16), TU_U16_HIGH(u16) #define U16_TO_U8S_LE(u16) TU_U16_LOW(u16), TU_U16_HIGH(u16)
#define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB #define U32_B1_U8(u32) ((uint8_t) ((((uint32_t) u32) >> 24) & 0x000000ff)) // MSB
#define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff)) #define U32_B2_U8(u32) ((uint8_t) ((((uint32_t) u32) >> 16) & 0x000000ff))
#define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff)) #define U32_B3_U8(u32) ((uint8_t) ((((uint32_t) u32) >> 8) & 0x000000ff))
#define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB #define U32_B4_U8(u32) ((uint8_t) (((uint32_t) u32) & 0x000000ff)) // LSB
#define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32) #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
#define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32) #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
+425 -106
View File
@@ -2,6 +2,7 @@
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2019 Ha Thach (tinyusb.org) * Copyright (c) 2019 Ha Thach (tinyusb.org)
* Copyright (c) 2020 Reinhard Panhuber - rework to unmasked pointers
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -57,6 +58,8 @@ static void tu_fifo_unlock(tu_fifo_t *f)
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable) bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
{ {
if (depth > 0x8000) return false; // Maximum depth is 2^15 items
tu_fifo_lock(f); tu_fifo_lock(f);
f->buffer = (uint8_t*) buffer; f->buffer = (uint8_t*) buffer;
@@ -64,55 +67,328 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
f->item_size = item_size; f->item_size = item_size;
f->overwritable = overwritable; f->overwritable = overwritable;
f->rd_idx = f->wr_idx = f->count = 0; f->max_pointer_idx = 2*depth - 1; // Limit index space to 2*depth - this allows for a fast "modulo" calculation but limits the maximum depth to 2^16/2 = 2^15 and buffer overflows are detectable only if overflow happens once (important for unsupervised DMA applications)
f->non_used_index_space = 0xFFFF - f->max_pointer_idx;
f->rd_idx = f->wr_idx = 0;
tu_fifo_unlock(f); tu_fifo_unlock(f);
return true; return true;
} }
// Static functions are intended to work on local variables
static inline uint16_t _ff_mod(uint16_t idx, uint16_t depth) static inline uint16_t _ff_mod(uint16_t idx, uint16_t depth)
{ {
return (idx < depth) ? idx : (idx-depth); while ( idx >= depth) idx -= depth;
return idx;
} }
// retrieve data from fifo // send one item to FIFO WITHOUT updating write pointer
static inline void _ff_pull(tu_fifo_t* f, void * buffer, uint16_t n) static inline void _ff_push(tu_fifo_t* f, void const * data, uint16_t wRel)
{ {
memcpy(buffer, memcpy(f->buffer + (wRel * f->item_size), data, f->item_size);
f->buffer + (f->rd_idx * f->item_size),
f->item_size*n);
f->rd_idx = _ff_mod(f->rd_idx + n, f->depth);
f->count -= n;
} }
// send data to fifo // send n items to FIFO WITHOUT updating write pointer
static inline void _ff_push(tu_fifo_t* f, void const * data, uint16_t n) static void _ff_push_n(tu_fifo_t* f, void const * data, uint16_t n, uint16_t wRel)
{ {
memcpy(f->buffer + (f->wr_idx * f->item_size), if(wRel + n <= f->depth) // Linear mode only
data,
f->item_size*n);
f->wr_idx = _ff_mod(f->wr_idx + n, f->depth);
if (tu_fifo_full(f))
{ {
f->rd_idx = f->wr_idx; // keep the full state (rd == wr && count = depth) memcpy(f->buffer + (wRel * f->item_size), data, n*f->item_size);
}
else // Wrap around
{
uint16_t nLin = f->depth - wRel;
// Write data to linear part of buffer
memcpy(f->buffer + (wRel * f->item_size), data, nLin*f->item_size);
// Write data wrapped around
memcpy(f->buffer, data + nLin*f->item_size, (n - nLin) * f->item_size);
}
}
// get one item from FIFO WITHOUT updating read pointer
static inline void _ff_pull(tu_fifo_t* f, void * p_buffer, uint16_t rRel)
{
memcpy(p_buffer, f->buffer + (rRel * f->item_size), f->item_size);
}
// get n items from FIFO WITHOUT updating read pointer
static void _ff_pull_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint16_t rRel)
{
if(rRel + n <= f->depth) // Linear mode only
{
memcpy(p_buffer, f->buffer + (rRel * f->item_size), n*f->item_size);
}
else // Wrap around
{
uint16_t nLin = f->depth - rRel;
// Read data from linear part of buffer
memcpy(p_buffer, f->buffer + (rRel * f->item_size), nLin*f->item_size);
// Read data wrapped part
memcpy(p_buffer + nLin*f->item_size, f->buffer, (n - nLin) * f->item_size);
}
}
// Advance an absolute pointer
static uint16_t advance_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
{
// We limit the index space of p such that a correct wrap around happens
// Check for a wrap around or if we are in unused index space - This has to be checked first!! We are exploiting the wrap around to the correct index
if ((p > p + offset) || (p + offset > f->max_pointer_idx))
{
p = (p + offset) + f->non_used_index_space;
} }
else else
{ {
f->count += n; p += offset;
} }
return p;
}
// Backward an absolute pointer
static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
{
// We limit the index space of p such that a correct wrap around happens
// Check for a wrap around or if we are in unused index space - This has to be checked first!! We are exploiting the wrap around to the correct index
if ((p < p - offset) || (p - offset > f->max_pointer_idx))
{
p = (p - offset) - f->non_used_index_space;
}
else
{
p -= offset;
}
return p;
}
// get relative from absolute pointer
static uint16_t get_relative_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
{
return _ff_mod(advance_pointer(f, p, offset), f->depth);
}
// Works on local copies of w and r
static inline uint16_t _tu_fifo_count(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
uint16_t cnt = wAbs-rAbs;
// In case we have non-power of two depth we need a further modification
if (rAbs > wAbs) cnt -= f->non_used_index_space;
return cnt;
}
// Works on local copies of w and r
static inline bool _tu_fifo_empty(uint16_t wAbs, uint16_t rAbs)
{
return wAbs == rAbs;
}
// Works on local copies of w and r
static inline bool _tu_fifo_full(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
return (_tu_fifo_count(f, wAbs, rAbs) == f->depth);
}
// Works on local copies of w and r
// BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
// Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
// write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
// write pointer wraps and you pointer states are messed up. This can only happen if you
// use DMAs, write functions do not allow such an error.
static inline bool _tu_fifo_overflowed(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
return (_tu_fifo_count(f, wAbs, rAbs) > f->depth);
}
// Works on local copies of w
// For more details see _tu_fifo_overflow()!
static inline void _tu_fifo_correct_read_pointer(tu_fifo_t* f, uint16_t wAbs)
{
f->rd_idx = backward_pointer(f, wAbs, f->depth);
}
// Works on local copies of w and r
// Must be protected by mutexes since in case of an overflow read pointer gets modified
static bool _tu_fifo_peek_at(tu_fifo_t* f, uint16_t offset, void * p_buffer, uint16_t wAbs, uint16_t rAbs)
{
uint16_t cnt = _tu_fifo_count(f, wAbs, rAbs);
// Check overflow and correct if required
if (cnt > f->depth)
{
_tu_fifo_correct_read_pointer(f, wAbs);
cnt = f->depth;
}
// Skip beginning of buffer
if (cnt == 0 || offset >= cnt) return false;
uint16_t rRel = get_relative_pointer(f, rAbs, offset);
// Peek data
_ff_pull(f, p_buffer, rRel);
return true;
}
// Works on local copies of w and r
// Must be protected by mutexes since in case of an overflow read pointer gets modified
static uint16_t _tu_fifo_peek_at_n(tu_fifo_t* f, uint16_t offset, void * p_buffer, uint16_t n, uint16_t wAbs, uint16_t rAbs)
{
uint16_t cnt = _tu_fifo_count(f, wAbs, rAbs);
// Check overflow and correct if required
if (cnt > f->depth)
{
_tu_fifo_correct_read_pointer(f, wAbs);
rAbs = f->rd_idx;
cnt = f->depth;
}
// Skip beginning of buffer
if (cnt == 0 || offset >= cnt) return 0;
// Check if we can read something at and after offset - if too less is available we read what remains
cnt -= offset;
if (cnt < n) {
if (cnt == 0) return 0;
n = cnt;
}
uint16_t rRel = get_relative_pointer(f, rAbs, offset);
// Peek data
_ff_pull_n(f, p_buffer, n, rRel);
return n;
}
// Works on local copies of w and r
static inline uint16_t _tu_fifo_remaining(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
return f->depth - _tu_fifo_count(f, wAbs, rAbs);
} }
/******************************************************************************/ /******************************************************************************/
/*! /*!
@brief Read one element out of the RX buffer. @brief Get number of items in FIFO.
As this function only reads the read and write pointers once, this function is
reentrant and thus thread and ISR save without any mutexes.
@param[in] f
Pointer to the FIFO buffer to manipulate
@returns Number of items in FIFO
*/
/******************************************************************************/
uint16_t tu_fifo_count(tu_fifo_t* f)
{
return _tu_fifo_count(f, f->wr_idx, f->rd_idx);
}
/******************************************************************************/
/*!
@brief Check if FIFO is empty.
As this function only reads the read and write pointers once, this function is
reentrant and thus thread and ISR save without any mutexes.
@param[in] f
Pointer to the FIFO buffer to manipulate
@returns Number of items in FIFO
*/
/******************************************************************************/
bool tu_fifo_empty(tu_fifo_t* f)
{
return _tu_fifo_empty(f->wr_idx, f->rd_idx);
}
/******************************************************************************/
/*!
@brief Check if FIFO is full.
As this function only reads the read and write pointers once, this function is
reentrant and thus thread and ISR save without any mutexes.
@param[in] f
Pointer to the FIFO buffer to manipulate
@returns Number of items in FIFO
*/
/******************************************************************************/
bool tu_fifo_full(tu_fifo_t* f)
{
return _tu_fifo_full(f, f->wr_idx, f->rd_idx);
}
/******************************************************************************/
/*!
@brief Get remaining space in FIFO.
As this function only reads the read and write pointers once, this function is
reentrant and thus thread and ISR save without any mutexes.
@param[in] f
Pointer to the FIFO buffer to manipulate
@returns Number of items in FIFO
*/
/******************************************************************************/
uint16_t tu_fifo_remaining(tu_fifo_t* f)
{
return _tu_fifo_remaining(f, f->wr_idx, f->rd_idx);
}
/******************************************************************************/
/*!
@brief Check if overflow happened.
BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
write pointer wraps and you pointer states are messed up. This can only happen if you
use DMAs, write functions do not allow such an error. Avoid such nasty things!
All reading functions (read, peek) check for overflows and correct read pointer on their own such
that latest items are read.
If required (e.g. for DMA use) you can also correct the read pointer by
tu_fifo_correct_read_pointer().
@param[in] f
Pointer to the FIFO buffer to manipulate
@returns True if overflow happened
*/
/******************************************************************************/
bool tu_fifo_overflowed(tu_fifo_t* f)
{
return _tu_fifo_overflowed(f, f->wr_idx, f->rd_idx);
}
// Only use in case tu_fifo_overflow() returned true!
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
{
tu_fifo_lock(f);
_tu_fifo_correct_read_pointer(f, f->wr_idx);
tu_fifo_unlock(f);
}
/******************************************************************************/
/*!
@brief Read one element out of the buffer.
This function will return the element located at the array index of the This function will return the element located at the array index of the
read pointer, and then increment the read pointer index. If the read read pointer, and then increment the read pointer index.
pointer exceeds the maximum buffer size, it will roll over to zero. This function checks for an overflow and corrects read pointer if required.
@param[in] f @param[in] f
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
@@ -120,26 +396,27 @@ static inline void _ff_push(tu_fifo_t* f, void const * data, uint16_t n)
Pointer to the place holder for data read from the buffer Pointer to the place holder for data read from the buffer
@returns TRUE if the queue is not empty @returns TRUE if the queue is not empty
*/ */
/******************************************************************************/ /******************************************************************************/
bool tu_fifo_read(tu_fifo_t* f, void * buffer) bool tu_fifo_read(tu_fifo_t* f, void * buffer)
{ {
if( tu_fifo_empty(f) ) return false; tu_fifo_lock(f); // TODO: Here we may distinguish for read and write pointer mutexes!
tu_fifo_lock(f); // Peek the data
bool ret = _tu_fifo_peek_at(f, 0, buffer, f->wr_idx, f->rd_idx); // f->rd_idx might get modified in case of an overflow so we can not use a local variable
_ff_pull(f, buffer, 1); // Advance pointer
f->rd_idx = advance_pointer(f, f->rd_idx, ret);
tu_fifo_unlock(f); tu_fifo_unlock(f);
return ret;
return true;
} }
/******************************************************************************/ /******************************************************************************/
/*! /*!
@brief This function will read n elements from the array index specified by @brief This function will read n elements from the array index specified by
the read pointer and increment the read index. If the read index the read pointer and increment the read index.
exceeds the max buffer size, then it will roll over to zero. This function checks for an overflow and corrects read pointer if required.
@param[in] f @param[in] f
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
@@ -149,76 +426,76 @@ bool tu_fifo_read(tu_fifo_t* f, void * buffer)
Number of element that buffer can afford Number of element that buffer can afford
@returns number of items read from the FIFO @returns number of items read from the FIFO
*/ */
/******************************************************************************/ /******************************************************************************/
uint16_t tu_fifo_read_n (tu_fifo_t* f, void * buffer, uint16_t count) uint16_t tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t count)
{ {
if(tu_fifo_empty(f)) return 0; tu_fifo_lock(f); // TODO: Here we may distinguish for read and write pointer mutexes!
tu_fifo_lock(f); // Peek the data
count = _tu_fifo_peek_at_n(f, 0, buffer, count, f->wr_idx, f->rd_idx); // f->rd_idx might get modified in case of an overflow so we can not use a local variable
// Limit up to fifo's count // Advance read pointer
if(count > f->count) count = f->count; f->rd_idx = advance_pointer(f, f->rd_idx, count);
if(count + f->rd_idx <= f->depth)
{
_ff_pull(f, buffer, count);
}
else
{
uint16_t const part1 = f->depth - f->rd_idx;
// Part 1: from rd_idx to end
_ff_pull(f, buffer, part1);
buffer = ((uint8_t*) buffer) + part1*f->item_size;
// Part 2: start to remaining
_ff_pull(f, buffer, count-part1);
}
tu_fifo_unlock(f); tu_fifo_unlock(f);
return count; return count;
} }
/******************************************************************************/ /******************************************************************************/
/*! /*!
@brief Read one item without removing it from the FIFO @brief Read one item without removing it from the FIFO.
This function checks for an overflow and corrects read pointer if required.
@param[in] f @param[in] f
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
@param[in] pos @param[in] offset
Position to read from in the FIFO buffer Position to read from in the FIFO buffer with respect to read pointer
@param[in] p_buffer @param[in] p_buffer
Pointer to the place holder for data read from the buffer Pointer to the place holder for data read from the buffer
@returns TRUE if the queue is not empty @returns TRUE if the queue is not empty
*/ */
/******************************************************************************/ /******************************************************************************/
bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t pos, void * p_buffer) bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t offset, void * p_buffer)
{ {
if ( pos >= f->count ) return false; tu_fifo_lock(f); // TODO: Here we may distinguish for read and write pointer mutexes!
bool ret = _tu_fifo_peek_at(f, offset, p_buffer, f->wr_idx, f->rd_idx);
tu_fifo_lock(f);
// rd_idx is pos=0
uint16_t index = _ff_mod(f->rd_idx + pos, f->depth);
memcpy(p_buffer,
f->buffer + (index * f->item_size),
f->item_size);
tu_fifo_unlock(f); tu_fifo_unlock(f);
return ret;
return true;
} }
/******************************************************************************/ /******************************************************************************/
/*! /*!
@brief Write one element into the RX buffer. @brief Read n items without removing it from the FIFO
This function checks for an overflow and corrects read pointer if required.
@param[in] f
Pointer to the FIFO buffer to manipulate
@param[in] offset
Position to read from in the FIFO buffer with respect to read pointer
@param[in] p_buffer
Pointer to the place holder for data read from the buffer
@param[in] n
Number of items to peek
@returns Number of bytes written to p_buffer
*/
/******************************************************************************/
uint16_t tu_fifo_peek_at_n(tu_fifo_t* f, uint16_t offset, void * p_buffer, uint16_t n)
{
tu_fifo_lock(f); // TODO: Here we may distinguish for read and write pointer mutexes!
bool ret = _tu_fifo_peek_at_n(f, offset, p_buffer, n, f->wr_idx, f->rd_idx);
tu_fifo_unlock(f);
return ret;
}
/******************************************************************************/
/*!
@brief Write one element into the buffer.
This function will write one element into the array index specified by This function will write one element into the array index specified by
the write pointer and increment the write index. If the write index the write pointer and increment the write index.
exceeds the max buffer size, then it will roll over to zero.
@param[in] f @param[in] f
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
@@ -227,15 +504,23 @@ bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t pos, void * p_buffer)
@returns TRUE if the data was written to the FIFO (overwrittable @returns TRUE if the data was written to the FIFO (overwrittable
FIFO will always return TRUE) FIFO will always return TRUE)
*/ */
/******************************************************************************/ /******************************************************************************/
bool tu_fifo_write (tu_fifo_t* f, const void * data) bool tu_fifo_write(tu_fifo_t* f, const void * data)
{ {
if ( tu_fifo_full(f) && !f->overwritable ) return false;
tu_fifo_lock(f); tu_fifo_lock(f);
_ff_push(f, data, 1); uint16_t w = f->wr_idx;
if ( _tu_fifo_full(f, w, f->rd_idx) && !f->overwritable ) return false;
uint16_t wRel = get_relative_pointer(f, w, 0);
// Write data
_ff_push(f, data, wRel);
// Advance pointer
f->wr_idx = advance_pointer(f, w, 1);
tu_fifo_unlock(f); tu_fifo_unlock(f);
@@ -245,8 +530,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * data)
/******************************************************************************/ /******************************************************************************/
/*! /*!
@brief This function will write n elements into the array index specified by @brief This function will write n elements into the array index specified by
the write pointer and increment the write index. If the write index the write pointer and increment the write index.
exceeds the max buffer size, then it will roll over to zero.
@param[in] f @param[in] f
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
@@ -255,46 +539,41 @@ bool tu_fifo_write (tu_fifo_t* f, const void * data)
@param[in] count @param[in] count
Number of element Number of element
@return Number of written elements @return Number of written elements
*/ */
/******************************************************************************/ /******************************************************************************/
uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * data, uint16_t count) uint16_t tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t count)
{ {
if ( count == 0 ) return 0; if ( count == 0 ) return 0;
tu_fifo_lock(f); tu_fifo_lock(f);
uint16_t w = f->wr_idx, r = f->rd_idx;
uint8_t const* buf8 = (uint8_t const*) data; uint8_t const* buf8 = (uint8_t const*) data;
if (!f->overwritable) if (!f->overwritable)
{ {
// Not overwritable limit up to full // Not overwritable limit up to full
count = tu_min16(count, tu_fifo_remaining(f)); count = tu_min16(count, _tu_fifo_remaining(f, w, r));
} }
else if (count > f->depth) else if (count > f->depth)
{ {
// Only copy last part // Only copy last part
buf8 = buf8 + (count - f->depth) * f->item_size; buf8 = buf8 + (count - f->depth) * f->item_size;
count = f->depth; count = f->depth;
f->wr_idx = 0;
f->rd_idx = 0; // We start writing at the read pointer's position since we fill the complete
f->count = 0; // buffer and we do not want to modify the read pointer within a write function!
// This would end up in a race condition with read functions!
f->wr_idx = r;
} }
if (count + f->wr_idx <= f->depth ) uint16_t wRel = get_relative_pointer(f, w, 0);
{
_ff_push(f, buf8, count);
}
else
{
uint16_t const part1 = f->depth - f->wr_idx;
// Part 1: from wr_idx to end // Write data
_ff_push(f, buf8, part1); _ff_push_n(f, buf8, count, wRel);
buf8 += part1*f->item_size;
// Part 2: start to remaining // Advance pointer
_ff_push(f, buf8, count-part1); f->wr_idx = advance_pointer(f, w, count);
}
tu_fifo_unlock(f); tu_fifo_unlock(f);
@@ -303,19 +582,59 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * data, uint16_t count)
/******************************************************************************/ /******************************************************************************/
/*! /*!
@brief Clear the fifo read and write pointers and set length to zero @brief Clear the fifo read and write pointers
@param[in] f @param[in] f
Pointer to the FIFO buffer to manipulate Pointer to the FIFO buffer to manipulate
*/ */
/******************************************************************************/ /******************************************************************************/
bool tu_fifo_clear(tu_fifo_t *f) bool tu_fifo_clear(tu_fifo_t *f)
{ {
tu_fifo_lock(f); tu_fifo_lock(f);
f->rd_idx = f->wr_idx = 0;
f->rd_idx = f->wr_idx = f->count = 0;
tu_fifo_unlock(f); tu_fifo_unlock(f);
return true; return true;
} }
/******************************************************************************/
/*!
@brief Advance write pointer - intended to be used in combination with DMA.
It is possible to fill the FIFO by use of a DMA in circular mode. Within
DMA ISRs you may update the write pointer to be able to read from the FIFO.
As long as the DMA is the only process writing into the FIFO this is safe
to use.
USE WITH CARE - WE DO NOT CONDUCT SAFTY CHECKS HERE!
@param[in] f
Pointer to the FIFO buffer to manipulate
@param[in] n
Number of items the write pointer moves forward
*/
/******************************************************************************/
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
{
f->wr_idx = advance_pointer(f, f->wr_idx, n);
}
/******************************************************************************/
/*!
@brief Advance read pointer - intended to be used in combination with DMA.
It is possible to read from the FIFO by use of a DMA in linear mode. Within
DMA ISRs you may update the read pointer to be able to again write into the
FIFO. As long as the DMA is the only process reading from the FIFO this is
safe to use.
USE WITH CARE - WE DO NOT CONDUCT SAFTY CHECKS HERE!
@param[in] f
Pointer to the FIFO buffer to manipulate
@param[in] n
Number of items the read pointer moves forward
*/
/******************************************************************************/
void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
{
f->rd_idx = advance_pointer(f, f->rd_idx, n);
}
+29 -23
View File
@@ -31,6 +31,15 @@
#ifndef _TUSB_FIFO_H_ #ifndef _TUSB_FIFO_H_
#define _TUSB_FIFO_H_ #define _TUSB_FIFO_H_
// Due to the use of unmasked pointers, this FIFO does not suffer from loosing
// one item slice. Furthermore, write and read operations are completely
// decoupled as write and read functions do not modify a common state. Henceforth,
// writing or reading from the FIFO within an ISR is safe as long as no other
// process (thread or ISR) interferes.
// Also, this FIFO is ready to be used in combination with a DMA as the write and
// read pointers can be updated from within a DMA ISR. Overflows are detectable
// within a certain number (see tu_fifo_overflow()).
// mutex is only needed for RTOS // mutex is only needed for RTOS
// for OS None, we don't get preempted // for OS None, we don't get preempted
#define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE) #define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
@@ -39,7 +48,7 @@
#include <stdbool.h> #include <stdbool.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#if CFG_FIFO_MUTEX #if CFG_FIFO_MUTEX
@@ -57,7 +66,9 @@ typedef struct
uint16_t item_size ; ///< size of each item uint16_t item_size ; ///< size of each item
bool overwritable ; bool overwritable ;
volatile uint16_t count ; ///< number of items in queue uint16_t non_used_index_space ; ///< required for non-power-of-two buffer length
uint16_t max_pointer_idx ; ///< maximum absolute pointer index
volatile uint16_t wr_idx ; ///< write pointer volatile uint16_t wr_idx ; ///< write pointer
volatile uint16_t rd_idx ; ///< read pointer volatile uint16_t rd_idx ; ///< read pointer
@@ -74,6 +85,8 @@ typedef struct
.depth = _depth, \ .depth = _depth, \
.item_size = sizeof(_type), \ .item_size = sizeof(_type), \
.overwritable = _overwritable, \ .overwritable = _overwritable, \
.max_pointer_idx = 2*_depth-1, \
.non_used_index_space = 0xFFFF - 2*_depth-1, \
} }
bool tu_fifo_clear(tu_fifo_t *f); bool tu_fifo_clear(tu_fifo_t *f);
@@ -93,39 +106,32 @@ bool tu_fifo_read (tu_fifo_t* f, void * p_buffer);
uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count); uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count);
bool tu_fifo_peek_at (tu_fifo_t* f, uint16_t pos, void * p_buffer); bool tu_fifo_peek_at (tu_fifo_t* f, uint16_t pos, void * p_buffer);
uint16_t tu_fifo_peek_at_n (tu_fifo_t* f, uint16_t pos, void * p_buffer, uint16_t n);
uint16_t tu_fifo_count (tu_fifo_t* f);
bool tu_fifo_empty (tu_fifo_t* f);
bool tu_fifo_full (tu_fifo_t* f);
uint16_t tu_fifo_remaining (tu_fifo_t* f);
bool tu_fifo_overflowed (tu_fifo_t* f);
void tu_fifo_correct_read_pointer (tu_fifo_t* f);
// Pointer modifications intended to be used in combinations with DMAs.
// USE WITH CARE - NO SAFTY CHECKS CONDUCTED HERE! NOT MUTEX PROTECTED!
void tu_fifo_advance_write_pointer (tu_fifo_t *f, uint16_t n);
void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n);
static inline bool tu_fifo_peek(tu_fifo_t* f, void * p_buffer) static inline bool tu_fifo_peek(tu_fifo_t* f, void * p_buffer)
{ {
return tu_fifo_peek_at(f, 0, p_buffer); return tu_fifo_peek_at(f, 0, p_buffer);
} }
static inline bool tu_fifo_empty(tu_fifo_t* f)
{
return (f->count == 0);
}
static inline bool tu_fifo_full(tu_fifo_t* f)
{
return (f->count == f->depth);
}
static inline uint16_t tu_fifo_count(tu_fifo_t* f)
{
return f->count;
}
static inline uint16_t tu_fifo_remaining(tu_fifo_t* f)
{
return f->depth - f->count;
}
static inline uint16_t tu_fifo_depth(tu_fifo_t* f) static inline uint16_t tu_fifo_depth(tu_fifo_t* f)
{ {
return f->depth; return f->depth;
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* _TUSB_FIFO_H_ */ #endif /* _TUSB_FIFO_H_ */
+8 -4
View File
@@ -37,6 +37,10 @@
#define CFG_TUD_TASK_QUEUE_SZ 16 #define CFG_TUD_TASK_QUEUE_SZ 16
#endif #endif
#ifndef CFG_TUD_EP_MAX
#define CFG_TUD_EP_MAX 9
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Device Data // Device Data
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@@ -57,7 +61,7 @@ typedef struct
uint8_t speed; uint8_t speed;
uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid) uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid ) uint8_t ep2drv[CFG_TUD_EP_MAX][2]; // map endpoint to driver ( 0xff is invalid )
struct TU_ATTR_PACKED struct TU_ATTR_PACKED
{ {
@@ -66,7 +70,7 @@ typedef struct
volatile bool claimed : 1; volatile bool claimed : 1;
// TODO merge ep2drv here, 4-bit should be sufficient // TODO merge ep2drv here, 4-bit should be sufficient
}ep_status[8][2]; }ep_status[CFG_TUD_EP_MAX][2];
}usbd_device_t; }usbd_device_t;
@@ -262,7 +266,7 @@ static osal_mutex_t _usbd_mutex;
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Prototypes // Prototypes
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); static void mark_interface_endpoint(uint8_t ep2drv[][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request); static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
static bool process_set_config(uint8_t rhport, uint8_t cfg_num); static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request); static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
@@ -858,7 +862,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
} }
// Helper marking endpoint of interface belongs to class driver // Helper marking endpoint of interface belongs to class driver
static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id) static void mark_interface_endpoint(uint8_t ep2drv[][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id)
{ {
uint16_t len = 0; uint16_t len = 0;
+138 -11
View File
@@ -53,6 +53,9 @@ enum
enum enum
{ {
// Endpoint number is fixed (8) for ISOOUT and ISOIN.
EP_ISO_NUM = 8,
// CBI endpoints count
EP_COUNT = 8 EP_COUNT = 8
}; };
@@ -62,11 +65,14 @@ typedef struct
uint8_t* buffer; uint8_t* buffer;
uint16_t total_len; uint16_t total_len;
volatile uint16_t actual_len; volatile uint16_t actual_len;
uint8_t mps; // max packet size uint16_t mps; // max packet size
// nrf52840 will auto ACK OUT packet after DMA is done // nrf52840 will auto ACK OUT packet after DMA is done
// indicate packet is already ACK // indicate packet is already ACK
volatile bool data_received; volatile bool data_received;
// Set to true when data was transferred from RAM to ISO IN output buffer.
// New data can be put in ISO IN output buffer after SOF.
bool iso_in_transfer_ready;
} xfer_td_t; } xfer_td_t;
@@ -74,7 +80,8 @@ typedef struct
static struct static struct
{ {
// All 8 endpoints including control IN & OUT (offset 1) // All 8 endpoints including control IN & OUT (offset 1)
xfer_td_t xfer[EP_COUNT][2]; // +1 for ISO endpoints
xfer_td_t xfer[EP_COUNT + 1][2];
// Number of pending DMA that is started but not handled yet by dcd_int_handler(). // Number of pending DMA that is started but not handled yet by dcd_int_handler().
// Since nRF can only carry one DMA can run at a time, this value is normally be either 0 or 1. // Since nRF can only carry one DMA can run at a time, this value is normally be either 0 or 1.
@@ -173,6 +180,7 @@ static void xact_out_prepare(uint8_t epnum)
{ {
// Write zero value to SIZE register will allow hw to ACK (accept data) // Write zero value to SIZE register will allow hw to ACK (accept data)
// If it is not already done by DMA // If it is not already done by DMA
// SIZE.ISOOUT can also be accessed this way
NRF_USBD->SIZE.EPOUT[epnum] = 0; NRF_USBD->SIZE.EPOUT[epnum] = 0;
} }
@@ -183,15 +191,32 @@ static void xact_out_prepare(uint8_t epnum)
static void xact_out_dma(uint8_t epnum) static void xact_out_dma(uint8_t epnum)
{ {
xfer_td_t* xfer = get_td(epnum, TUSB_DIR_OUT); xfer_td_t* xfer = get_td(epnum, TUSB_DIR_OUT);
uint32_t xact_len;
uint8_t const xact_len = NRF_USBD->SIZE.EPOUT[epnum]; if (epnum == EP_ISO_NUM)
{
xact_len = NRF_USBD->SIZE.ISOOUT;
// If ZERO bit is set, ignore ISOOUT length
if (xact_len & USBD_SIZE_ISOOUT_ZERO_Msk) xact_len = 0;
else
{
// Trigger DMA move data from Endpoint -> SRAM
NRF_USBD->ISOOUT.PTR = (uint32_t) xfer->buffer;
NRF_USBD->ISOOUT.MAXCNT = xact_len;
edpt_dma_start(&NRF_USBD->TASKS_STARTISOOUT);
}
}
else
{
xact_len = (uint8_t)NRF_USBD->SIZE.EPOUT[epnum];
// Trigger DMA move data from Endpoint -> SRAM // Trigger DMA move data from Endpoint -> SRAM
NRF_USBD->EPOUT[epnum].PTR = (uint32_t) xfer->buffer; NRF_USBD->EPOUT[epnum].PTR = (uint32_t) xfer->buffer;
NRF_USBD->EPOUT[epnum].MAXCNT = xact_len; NRF_USBD->EPOUT[epnum].MAXCNT = xact_len;
edpt_dma_start(&NRF_USBD->TASKS_STARTEPOUT[epnum]); edpt_dma_start(&NRF_USBD->TASKS_STARTEPOUT[epnum]);
}
xfer->buffer += xact_len; xfer->buffer += xact_len;
xfer->actual_len += xact_len; xfer->actual_len += xact_len;
} }
@@ -205,7 +230,7 @@ static void xact_in_prepare(uint8_t epnum)
xfer_td_t* xfer = get_td(epnum, TUSB_DIR_IN); xfer_td_t* xfer = get_td(epnum, TUSB_DIR_IN);
// Each transaction is up to Max Packet Size // Each transaction is up to Max Packet Size
uint8_t const xact_len = tu_min16(xfer->total_len - xfer->actual_len, xfer->mps); uint16_t const xact_len = tu_min16(xfer->total_len - xfer->actual_len, xfer->mps);
NRF_USBD->EPIN[epnum].PTR = (uint32_t) xfer->buffer; NRF_USBD->EPIN[epnum].PTR = (uint32_t) xfer->buffer;
NRF_USBD->EPIN[epnum].MAXCNT = xact_len; NRF_USBD->EPIN[epnum].MAXCNT = xact_len;
@@ -296,7 +321,9 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
_dcd.xfer[epnum][dir].mps = desc_edpt->wMaxPacketSize.size; _dcd.xfer[epnum][dir].mps = desc_edpt->wMaxPacketSize.size;
if ( dir == TUSB_DIR_OUT ) if (desc_edpt->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS)
{
if (dir == TUSB_DIR_OUT)
{ {
NRF_USBD->INTENSET = TU_BIT(USBD_INTEN_ENDEPOUT0_Pos + epnum); NRF_USBD->INTENSET = TU_BIT(USBD_INTEN_ENDEPOUT0_Pos + epnum);
NRF_USBD->EPOUTEN |= TU_BIT(epnum); NRF_USBD->EPOUTEN |= TU_BIT(epnum);
@@ -305,11 +332,83 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
NRF_USBD->INTENSET = TU_BIT(USBD_INTEN_ENDEPIN0_Pos + epnum); NRF_USBD->INTENSET = TU_BIT(USBD_INTEN_ENDEPIN0_Pos + epnum);
NRF_USBD->EPINEN |= TU_BIT(epnum); NRF_USBD->EPINEN |= TU_BIT(epnum);
} }
}
else
{
TU_ASSERT(epnum == EP_ISO_NUM);
if (dir == TUSB_DIR_OUT)
{
// SPLIT ISO buffer when ISO IN endpoint is already opened.
if (_dcd.xfer[EP_ISO_NUM][TUSB_DIR_IN].mps) NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;
// Clear old events
NRF_USBD->EVENTS_ENDISOOUT = 0;
// Clear SOF event in case interrupt was not enabled yet.
if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0) NRF_USBD->EVENTS_SOF = 0;
// Enable SOF and ISOOUT interrupts, and ISOOUT endpoint.
NRF_USBD->INTENSET = USBD_INTENSET_ENDISOOUT_Msk | USBD_INTENSET_SOF_Msk;
NRF_USBD->EPOUTEN |= USBD_EPOUTEN_ISOOUT_Msk;
}
else
{
NRF_USBD->EVENTS_ENDISOIN = 0;
// SPLIT ISO buffer when ISO OUT endpoint is already opened.
if (_dcd.xfer[EP_ISO_NUM][TUSB_DIR_OUT].mps) NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;
// Clear SOF event in case interrupt was not enabled yet.
if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0) NRF_USBD->EVENTS_SOF = 0;
// Enable SOF and ISOIN interrupts, and ISOIN endpoint.
NRF_USBD->INTENSET = USBD_INTENSET_ENDISOIN_Msk | USBD_INTENSET_SOF_Msk;
NRF_USBD->EPINEN |= USBD_EPINEN_ISOIN_Msk;
}
}
__ISB(); __DSB(); __ISB(); __DSB();
return true; return true;
} }
void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
{
(void) rhport;
uint8_t const epnum = tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr);
if (epnum != EP_ISO_NUM)
{
// CBI
if (dir == TUSB_DIR_OUT)
{
NRF_USBD->INTENCLR = TU_BIT(USBD_INTEN_ENDEPOUT0_Pos + epnum);
NRF_USBD->EPOUTEN &= ~TU_BIT(epnum);
}
else
{
NRF_USBD->INTENCLR = TU_BIT(USBD_INTEN_ENDEPIN0_Pos + epnum);
NRF_USBD->EPINEN &= ~TU_BIT(epnum);
}
}
else
{
_dcd.xfer[EP_ISO_NUM][dir].mps = 0;
// ISO
if (dir == TUSB_DIR_OUT)
{
NRF_USBD->INTENCLR = USBD_INTENCLR_ENDISOOUT_Msk;
NRF_USBD->EPOUTEN &= ~USBD_EPOUTEN_ISOOUT_Msk;
NRF_USBD->EVENTS_ENDISOOUT = 0;
}
else
{
NRF_USBD->INTENCLR = USBD_INTENCLR_ENDISOIN_Msk;
NRF_USBD->EPINEN &= ~USBD_EPINEN_ISOIN_Msk;
}
// One of the ISO endpoints closed, no need to split buffers any more.
NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_OneDir;
// When both ISO endpoint are close there is no need for SOF any more.
if (_dcd.xfer[EP_ISO_NUM][TUSB_DIR_IN].mps + _dcd.xfer[EP_ISO_NUM][TUSB_DIR_OUT].mps == 0) NRF_USBD->INTENCLR = USBD_INTENCLR_SOF_Msk;
}
__ISB(); __DSB();
}
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
{ {
(void) rhport; (void) rhport;
@@ -361,11 +460,12 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
{ {
(void) rhport; (void) rhport;
uint8_t const epnum = tu_edpt_number(ep_addr);
if ( tu_edpt_number(ep_addr) == 0 ) if ( epnum == 0 )
{ {
NRF_USBD->TASKS_EP0STALL = 1; NRF_USBD->TASKS_EP0STALL = 1;
}else }else if (epnum != EP_ISO_NUM)
{ {
NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_Stall << USBD_EPSTALL_STALL_Pos) | ep_addr; NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_Stall << USBD_EPSTALL_STALL_Pos) | ep_addr;
} }
@@ -376,8 +476,9 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
{ {
(void) rhport; (void) rhport;
uint8_t const epnum = tu_edpt_number(ep_addr);
if ( tu_edpt_number(ep_addr) ) if ( epnum != 0 && epnum != EP_ISO_NUM )
{ {
// clear stall // clear stall
NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep_addr; NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep_addr;
@@ -435,8 +536,31 @@ void dcd_int_handler(uint8_t rhport)
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true); dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
} }
// ISOIN: Data was moved to endpoint buffer, client will be notified in SOF
if ( int_status & USBD_INTEN_ENDISOIN_Msk )
{
xfer_td_t* xfer = get_td(EP_ISO_NUM, TUSB_DIR_IN);
xfer->actual_len = NRF_USBD->ISOIN.AMOUNT;
// Data transferred from RAM to endpoint output buffer.
// Next transfer can be scheduled after SOF.
xfer->iso_in_transfer_ready = true;
}
if ( int_status & USBD_INTEN_SOF_Msk ) if ( int_status & USBD_INTEN_SOF_Msk )
{ {
// ISOOUT: Transfer data gathered in previous frame from buffer to RAM
if (NRF_USBD->EPOUTEN & USBD_EPOUTEN_ISOOUT_Msk)
{
xact_out_dma(EP_ISO_NUM);
}
// ISOIN: Notify client that data was transferred
xfer_td_t* xfer = get_td(EP_ISO_NUM, TUSB_DIR_IN);
if ( xfer->iso_in_transfer_ready )
{
xfer->iso_in_transfer_ready = false;
dcd_event_xfer_complete(0, EP_ISO_NUM | TUSB_DIR_IN_MASK, xfer->actual_len, XFER_RESULT_SUCCESS, true);
}
dcd_event_bus_signal(0, DCD_EVENT_SOF, true); dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
} }
@@ -518,8 +642,11 @@ void dcd_int_handler(uint8_t rhport)
* Note: Since nRF controller auto ACK next packet without SW awareness * Note: Since nRF controller auto ACK next packet without SW awareness
* We must handle this stage before Host -> Endpoint just in case * We must handle this stage before Host -> Endpoint just in case
* 2 event happens at once * 2 event happens at once
* ISO OUT: Transaction must fit in single packed, it can be shorter then total
* len if Host decides to sent fewer bytes, it this case transaction is also
* complete and next transfer is not initiated here like for CBI.
*/ */
for(uint8_t epnum=0; epnum<8; epnum++) for(uint8_t epnum=0; epnum<EP_COUNT+1; epnum++)
{ {
if ( tu_bit_test(int_status, USBD_INTEN_ENDEPOUT0_Pos+epnum)) if ( tu_bit_test(int_status, USBD_INTEN_ENDEPOUT0_Pos+epnum))
{ {
@@ -530,7 +657,7 @@ void dcd_int_handler(uint8_t rhport)
xfer->data_received = false; xfer->data_received = false;
// Transfer complete if transaction len < Max Packet Size or total len is transferred // Transfer complete if transaction len < Max Packet Size or total len is transferred
if ( (xact_len == xfer->mps) && (xfer->actual_len < xfer->total_len) ) if ( (epnum != EP_ISO_NUM) && (xact_len == xfer->mps) && (xfer->actual_len < xfer->total_len) )
{ {
// Prepare for next transaction // Prepare for next transaction
xact_out_prepare(epnum); xact_out_prepare(epnum);