diff --git a/stm32/aioc-fw/Src/main.c b/stm32/aioc-fw/Src/main.c index 5636dc0..5eb8001 100644 --- a/stm32/aioc-fw/Src/main.c +++ b/stm32/aioc-fw/Src/main.c @@ -14,10 +14,6 @@ static void SystemClock_Config(void) .OscillatorType = RCC_OSCILLATORTYPE_HSE, .HSEState = RCC_HSE_ON, .HSEPredivValue = RCC_HSE_PREDIV_DIV1, - .LSEState = RCC_LSE_OFF, - .HSIState = RCC_HSI_OFF, - .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT, - .LSIState = RCC_LSI_ON, .PLL = { .PLLState = RCC_PLL_ON, .PLLSource = RCC_CFGR_PLLSRC_HSE_PREDIV, diff --git a/stm32/aioc-fw/Src/usb_descriptors.c b/stm32/aioc-fw/Src/usb_descriptors.c index 5492d24..669e7f3 100644 --- a/stm32/aioc-fw/Src/usb_descriptors.c +++ b/stm32/aioc-fw/Src/usb_descriptors.c @@ -42,89 +42,105 @@ //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ -tusb_desc_device_t const desc_device = -{ - .bLength = sizeof(tusb_desc_device_t), +tusb_desc_device_t const desc_device = { + .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = USB_BCD, - - // Use Interface Association Descriptor (IAD) for CDC - // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) .bDeviceClass = TUSB_CLASS_MISC, .bDeviceSubClass = MISC_SUBCLASS_COMMON, .bDeviceProtocol = MISC_PROTOCOL_IAD, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - .idVendor = USB_VID, .idProduct = USB_PID, .bcdDevice = 0x0100, - .iManufacturer = STR_IDX_MANUFACTURER, .iProduct = STR_IDX_PRODUCT, .iSerialNumber = STR_IDX_SERIAL, - .bNumConfigurations = 0x01 }; // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor -uint8_t const * tud_descriptor_device_cb(void) -{ - return (uint8_t const *) &desc_device; +uint8_t const* tud_descriptor_device_cb(void) { + return (uint8_t const*) &desc_device; } //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ -#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN + 1 * TUD_AUDIO_IO_DESC_LEN) +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN + CFG_TUD_AUDIO * TUD_AUDIO_IO_DESC_LEN) -uint8_t const desc_fs_configuration[] = -{ - // Config number, interface count, string index, total length, attribute, power in mA - TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, STR_IDX_CDCITF, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, CFG_TUD_CDC_EP_BUFSIZE), - TUD_AUDIO_IO_DESCRIPTOR(/*_itfnum*/ ITF_NUM_AUDIO_CONTROL, /*_stridx*/ STR_IDX_AUDIOITF, /*_nBytesPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE, /*_nBitsUsedPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE*8, /*_epin*/ EPNUM_AUDIO_IN, /*_epinsize*/ CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX, /*_epout*/ EPNUM_AUDIO_OUT, /*_epoutsize*/ CFG_TUD_AUDIO_FUNC_1_EP_OUT_SZ_MAX, /*_epfb*/ EPNUM_AUDIO_FB) +uint8_t const desc_fs_configuration[] = { + // Config number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR( + /* config_num */ 1, + /* _itfcount */ ITF_NUM_TOTAL, + /* _stridx */ 0x00, + /* _total_len */ CONFIG_TOTAL_LEN, + /* _attribute */ 0x00, + /* _power_ma */ 100 + ), + + TUD_CDC_DESCRIPTOR( + /* _itfnum */ ITF_NUM_CDC_0, + /* _stridx */ STR_IDX_CDCITF, + /* _ep_notif */ EPNUM_CDC_0_NOTIF, + /* _ep_notif_size */ 8, + /* _epout */ EPNUM_CDC_0_OUT, + /* _epin */ EPNUM_CDC_0_IN, + /* _epsize */ CFG_TUD_CDC_EP_BUFSIZE + ), + + TUD_AUDIO_IO_DESCRIPTOR( + /*_itfnum*/ ITF_NUM_AUDIO_CONTROL, + /*_stridx*/ STR_IDX_AUDIOITF, + /*_nBytesPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE, + /*_nBitsUsedPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE*8, + /*_epin*/ EPNUM_AUDIO_IN, + /*_epinsize*/ CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX, + /*_epout*/ EPNUM_AUDIO_OUT, + /*_epoutsize*/ CFG_TUD_AUDIO_FUNC_1_EP_OUT_SZ_MAX, + /*_epfb*/ EPNUM_AUDIO_FB + ) }; // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete -uint8_t const * tud_descriptor_configuration_cb(uint8_t index) -{ - (void) index; // for multiple configurations +uint8_t const* tud_descriptor_configuration_cb(uint8_t index) { + (void) index; // for multiple configurations - TU_ASSERT(!TUD_OPT_HIGH_SPEED); - return desc_fs_configuration; + TU_ASSERT(!TUD_OPT_HIGH_SPEED); + return desc_fs_configuration; } //--------------------------------------------------------------------+ // String Descriptors //--------------------------------------------------------------------+ -static uint32_t crc32(const uint8_t * buf_ptr, uint32_t buf_len) -{ +static uint32_t calc_crc32(const uint8_t *buf_ptr, uint32_t buf_len) { /* Generate a serial number from processor UID using CRC32 */ const uint32_t crc32_init = 0xFFFFFFFFUL; const uint32_t crc32_poly = 0xEDB88320UL; + const uint32_t crc32_final = 0xFFFFFFFFUL; uint32_t serial = crc32_init; /* CRC32 Initial value */ while (buf_len-- > 0) { - uint8_t byte = *buf_ptr++; - serial = serial ^ byte; - for (uint8_t j=0; j<8; j++) { - uint32_t mask = -(serial & 1); - serial = (serial >> 1) ^ (crc32_poly & mask); - } + uint8_t byte = *buf_ptr++; + serial = serial ^ byte; + for (uint8_t j = 0; j < 8; j++) { + uint32_t mask = -(serial & 1); + serial = (serial >> 1) ^ (crc32_poly & mask); + } } - return ~serial; + return serial ^ crc32_final; } -static const char * get_serial(void) -{ +static const char * get_serial(void) { static char serial_str[sizeof(uint32_t) * 2 + 1]; - uint32_t serial_num = crc32((uint8_t *) UID_BASE, 12); + uint32_t serial_num = calc_crc32((uint8_t *) UID_BASE, 12); - for (uint8_t i=0; i<(sizeof(uint32_t) * 2); i++) { + for (uint8_t i = 0; i < (sizeof(uint32_t) * 2); i++) { uint8_t nibble = ((serial_num & 0xF0000000UL) >> 28); serial_str[i] = nibble < 0xA ? nibble + '0' : nibble - 0xA + 'a'; serial_num <<= 4; @@ -133,11 +149,10 @@ static const char * get_serial(void) return serial_str; } -static uint8_t ascii_to_utf16(uint8_t * buffer, uint32_t size, const char * str) -{ +static uint8_t ascii_to_utf16(uint8_t *buffer, uint32_t size, const char *str) { uint8_t len = 0; - while ( (*str != '\0') && (size > 0) ) { + while ((*str != '\0') && (size > 0)) { *buffer++ = *str++; *buffer++ = 0x00; size -= 2; @@ -149,70 +164,71 @@ static uint8_t ascii_to_utf16(uint8_t * buffer, uint32_t size, const char * str) // Invoked when received GET STRING DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete -uint8_t const * tud_descriptor_string_cb(uint8_t index, uint16_t langid) -{ - (void) langid; - static uint8_t buffer[64]; - uint8_t * ptr = &buffer[2]; - uint8_t len = sizeof(buffer) - 2; +const uint8_t * tud_descriptor_string_cb(uint8_t index, uint16_t langid) { + static uint8_t buffer[64]; + uint8_t *ptr = &buffer[2]; + uint8_t len = sizeof(buffer) - 2; - switch (index) { - case STR_IDX_LANGUAGE: - ptr[0] = 0x09; ptr[1] = 0x04; - len = 2; - break; + (void) langid; - case STR_IDX_MANUFACTURER: - len = ascii_to_utf16(ptr, len, USB_STRING_MANUFACTURER); - break; + switch (index) { + case STR_IDX_LANGUAGE: + ptr[0] = 0x09; + ptr[1] = 0x04; + len = 2; + break; - case STR_IDX_PRODUCT: - len = ascii_to_utf16(ptr, len, USB_STRING_PRODUCT); - break; + case STR_IDX_MANUFACTURER: + len = ascii_to_utf16(ptr, len, USB_STRING_MANUFACTURER); + break; - case STR_IDX_SERIAL: - len = ascii_to_utf16(ptr, len, get_serial()); - break; + case STR_IDX_PRODUCT: + len = ascii_to_utf16(ptr, len, USB_STRING_PRODUCT); + break; - case STR_IDX_CDCITF: - len = ascii_to_utf16(ptr, len, USB_STRING_CDCITF); - break; + case STR_IDX_SERIAL: + len = ascii_to_utf16(ptr, len, get_serial()); + break; - case STR_IDX_AUDIOITF: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOITF); - break; + case STR_IDX_CDCITF: + len = ascii_to_utf16(ptr, len, USB_STRING_CDCITF); + break; - case STR_IDX_AUDIOIN: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOIN); - break; + case STR_IDX_AUDIOITF: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOITF); + break; - case STR_IDX_AUDIOOUT: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUT); - break; + case STR_IDX_AUDIOIN: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOIN); + break; - case STR_IDX_AUDIOINVOL: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOINVOL); - break; + case STR_IDX_AUDIOOUT: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUT); + break; - case STR_IDX_AUDIOOUTVOL: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUTVOL); - break; + case STR_IDX_AUDIOINVOL: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOINVOL); + break; - case STR_IDX_AUDIOINCHAN: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOINCHAN); - break; + case STR_IDX_AUDIOOUTVOL: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUTVOL); + break; - case STR_IDX_AUDIOOUTCHAN: - len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUTCHAN); - break; + case STR_IDX_AUDIOINCHAN: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOINCHAN); + break; - default: - TU_ASSERT(0, NULL); - break; - } + case STR_IDX_AUDIOOUTCHAN: + len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUTCHAN); + break; - buffer[0] = len + 2; - buffer[1] = TUSB_DESC_STRING; + default: + TU_ASSERT(0, NULL); + break; + } - return buffer; + buffer[0] = len + 2; + buffer[1] = TUSB_DESC_STRING; + + return buffer; } diff --git a/stm32/aioc-fw/Src/usb_descriptors.h b/stm32/aioc-fw/Src/usb_descriptors.h index eae39ed..637f708 100644 --- a/stm32/aioc-fw/Src/usb_descriptors.h +++ b/stm32/aioc-fw/Src/usb_descriptors.h @@ -3,28 +3,28 @@ /* Interfaces */ enum USB_DESCRIPTORS_ITF { - ITF_NUM_CDC_0 = 0, - ITF_NUM_CDC_0_DATA, - ITF_NUM_AUDIO_CONTROL, - ITF_NUM_AUDIO_STREAMING_OUT, - ITF_NUM_AUDIO_STREAMING_IN, - ITF_NUM_TOTAL + ITF_NUM_CDC_0 = 0, + ITF_NUM_CDC_0_DATA, + ITF_NUM_AUDIO_CONTROL, + ITF_NUM_AUDIO_STREAMING_OUT, + ITF_NUM_AUDIO_STREAMING_IN, + ITF_NUM_TOTAL }; /* Strings */ enum USB_STRING_IDX { - STR_IDX_LANGUAGE = 0, - STR_IDX_MANUFACTURER, - STR_IDX_PRODUCT, - STR_IDX_SERIAL, - STR_IDX_CDCITF, - STR_IDX_AUDIOITF, - STR_IDX_AUDIOIN, - STR_IDX_AUDIOOUT, - STR_IDX_AUDIOINVOL, - STR_IDX_AUDIOOUTVOL, - STR_IDX_AUDIOINCHAN, - STR_IDX_AUDIOOUTCHAN, + STR_IDX_LANGUAGE = 0, + STR_IDX_MANUFACTURER, + STR_IDX_PRODUCT, + STR_IDX_SERIAL, + STR_IDX_CDCITF, + STR_IDX_AUDIOITF, + STR_IDX_AUDIOIN, + STR_IDX_AUDIOOUT, + STR_IDX_AUDIOINVOL, + STR_IDX_AUDIOOUTVOL, + STR_IDX_AUDIOINCHAN, + STR_IDX_AUDIOOUTCHAN, }; #define USB_STRING_MANUFACTURER "TinyUSB" @@ -48,18 +48,18 @@ enum USB_STRING_IDX { /* Custom Audio Descriptor. * Courtesy of https://github.com/hathach/tinyusb/issues/1249#issuecomment-1148727765 */ -#define AUDIO_CTRL_ID_SPK_INPUT_STREAM 0x01 -#define AUDIO_CTRL_ID_SPK_FUNIT 0x02 -#define AUDIO_CTRL_ID_SPK_OUTPUT 0x03 -#define AUDIO_CTRL_ID_SPK_CLOCK 0x08 -#define AUDIO_CTRL_ID_MIC_INPUT 0x11 -#define AUDIO_CTRL_ID_MIC_FUNIT 0x12 +#define AUDIO_CTRL_ID_SPK_INPUT_STREAM 0x01 +#define AUDIO_CTRL_ID_SPK_FUNIT 0x02 +#define AUDIO_CTRL_ID_SPK_OUTPUT 0x03 +#define AUDIO_CTRL_ID_SPK_CLOCK 0x08 +#define AUDIO_CTRL_ID_MIC_INPUT 0x11 +#define AUDIO_CTRL_ID_MIC_FUNIT 0x12 #define AUDIO_CTRL_ID_MIC_OUTPUT_STREAM 0x13 -#define AUDIO_CTRL_ID_MIC_CLOCK 0x18 +#define AUDIO_CTRL_ID_MIC_CLOCK 0x18 -#define AUDIO_NUM_INTERFACES 0x03 -#define AUDIO_NUM_INCHANNELS 0x01 -#define AUDIO_NUM_OUTCHANNELS 0x01 +#define AUDIO_NUM_INTERFACES 0x03 +#define AUDIO_NUM_INCHANNELS 0x01 +#define AUDIO_NUM_OUTCHANNELS 0x01 #define TUD_AUDIO_CTRL_TOTAL_LEN ( \ TUD_AUDIO_DESC_CLK_SRC_LEN + \