Skip to content

Commit

Permalink
Experimental: send interrupt to host on control change.
Browse files Browse the repository at this point in the history
Currently crashes if volume is changed via encoder and then adjusted from the system.
  • Loading branch information
Gadgetoid committed Mar 12, 2024
1 parent 492de18 commit 84c127f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ int main(void)
// Device callbacks
//--------------------------------------------------------------------+

bool tud_audio_int_ctr_done_cb(uint8_t rhport, uint16_t n_bytes_copied)
{
system_led(128, 128, 0);
return true;
}

// Invoked when device is mounted
void tud_mount_cb(void)
{
Expand Down Expand Up @@ -425,6 +431,54 @@ bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, u
// AUDIO Task
//--------------------------------------------------------------------+

#define STATUS_INTERRUPT_INFO_ORIGIN_INTERFACE 0x00
#define STATUS_INTERRUPT_CONTROL_MASTER 0x00

// Cribbed from https://github.com/hathach/tinyusb/pull/1702
// UAC2 §6.1 "interrupt data message"
typedef struct TU_ATTR_PACKED
{
uint8_t bInfo;
uint8_t bAttribute;
union TU_ATTR_PACKED
{
uint16_t wValue;
struct TU_ATTR_PACKED
{
uint8_t wValue_cn_or_mcn;
uint8_t wValue_cs;
};
};
union TU_ATTR_PACKED
{
uint16_t wIndex;
struct TU_ATTR_PACKED
{
uint8_t wIndex_ep_or_int;
uint8_t wIndex_entity_id;
};
};
} audio_status_update_t;

static const audio_status_update_t status_message_queue[2] = {
{
.bInfo = STATUS_INTERRUPT_INFO_ORIGIN_INTERFACE,
.bAttribute = AUDIO_CS_REQ_CUR,
.wValue_cn_or_mcn = STATUS_INTERRUPT_CONTROL_MASTER,
.wValue_cs = AUDIO_FU_CTRL_VOLUME,
.wIndex_ep_or_int = ITF_NUM_AUDIO_CONTROL,
.wIndex_entity_id = UAC2_ENTITY_SPK_INPUT_TERMINAL,
},
{
.bInfo = STATUS_INTERRUPT_INFO_ORIGIN_INTERFACE,
.bAttribute = AUDIO_CS_REQ_CUR,
.wValue_cn_or_mcn = STATUS_INTERRUPT_CONTROL_MASTER,
.wValue_cs = AUDIO_FU_CTRL_MUTE,
.wIndex_ep_or_int = ITF_NUM_AUDIO_CONTROL,
.wIndex_entity_id = UAC2_ENTITY_SPK_INPUT_TERMINAL,
},
};

void audio_task(void)
{
static uint32_t start_ms = 0;
Expand Down Expand Up @@ -456,6 +510,9 @@ void audio_task(void)

volume[0] = system_volume * 100;
volume[1] = system_volume * 100;

const uint8_t *buf = (const uint8_t *)&status_message_queue[0];
tud_audio_int_ctr_n_write(0, buf, 6);
}

start_ms += volume_interval_ms;
Expand Down
2 changes: 2 additions & 0 deletions src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ extern "C" {
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif

#define CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN 6

//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
Expand Down
11 changes: 11 additions & 0 deletions src/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ enum
ITF_NUM_TOTAL
};

#define TUD_AUDIO_DESC_AC_INT_LEN 7
#define TUD_AUDIO_DESC_AC_INT(ep) \
TUD_AUDIO_DESC_AC_INT_LEN /* Bytes */, \
TUSB_DESC_ENDPOINT, \
ep /* Endpoint */, \
TUSB_XFER_INTERRUPT /* Interrupt type */, \
U16_TO_U8S_LE(6) /* Buffer size as per standard */, \
10

#define TUD_AUDIO_HEADSET_STEREO_DESC_LEN (TUD_AUDIO_DESC_IAD_LEN\
+ TUD_AUDIO_DESC_AC_INT_LEN\
+ TUD_AUDIO_DESC_STD_AC_LEN\
+ TUD_AUDIO_DESC_CS_AC_LEN\
+ TUD_AUDIO_DESC_CLK_SRC_LEN\
Expand Down Expand Up @@ -81,6 +91,7 @@ enum
/* Output Terminal Descriptor(4.7.2.5) */\
TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ UAC2_ENTITY_SPK_OUTPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_OUT_GENERIC_SPEAKER, /*_assocTerm*/ 0x00, /*_srcid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\
/* Standard AS Interface Descriptor(4.9.1) */\
TUD_AUDIO_DESC_AC_INT(_epin),\
/* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */\
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x05),\
/* Standard AS Interface Descriptor(4.9.1) */\
Expand Down

0 comments on commit 84c127f

Please sign in to comment.