From b92088052165d83d23added35246b8552c032ac7 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 2 Apr 2022 22:52:56 +0200 Subject: [PATCH 4/4] Add isochronous IN endpoint --- inc/usb_conf.h | 19 ++++++++++++------- src/hw_config.c | 8 ++++++++ src/usb_desc.c | 17 +++++++++++++++-- src/usb_endp.c | 27 ++++++++++++++++++++++++--- src/usb_prop.c | 20 +++++++++++++------- 5 files changed, 72 insertions(+), 19 deletions(-) diff --git a/inc/usb_conf.h b/inc/usb_conf.h index d68461d..1e32a0c 100644 --- a/inc/usb_conf.h +++ b/inc/usb_conf.h @@ -35,24 +35,29 @@ /* defines how many endpoints are used by the device */ /*-------------------------------------------------------------*/ -#define EP_NUM (2) +#define EP_NUM (3) /*-------------------------------------------------------------*/ /* -------------- Buffer Description Table -----------------*/ /*-------------------------------------------------------------*/ /* buffer table base address */ /* buffer table base address */ -#define BTABLE_ADDRESS (0x00) +#define BTABLE_ADDRESS (0x00) /* EPNUM (max 4) entries of 8 bytes */ /* EP0 */ /* rx/tx buffer base address */ -#define ENDP0_RXADDR (0x10) -#define ENDP0_TXADDR (0x50) +#define ENDP0_RXADDR (0x20) /* 64 bytes */ +#define ENDP0_TXADDR (0x60) /* 64 bytes */ /* EP1 */ /* buffer base address */ -#define ENDP1_BUF0Addr (0x90) -#define ENDP1_BUF1Addr (0xC0) +#define ENDP1_BUF0Addr (0xA0) /* max 48 bytes */ +#define ENDP1_BUF1Addr (0xD0) /* max 48 bytes */ + +/* EP2 */ +/* buffer base addres */ +#define ENDP2_BUF0Addr (0x100) /* max 48 bytes */ +#define ENDP2_BUF1Addr (0x130) /*-------------------------------------------------------------*/ /* ------------------- ISTR events -------------------------*/ @@ -77,7 +82,7 @@ /* CTR service routines */ /* associated to defined endpoints */ #define EP1_IN_Callback NOP_Process -#define EP2_IN_Callback NOP_Process +/* #define EP2_IN_Callback NOP_Process */ #define EP3_IN_Callback NOP_Process #define EP4_IN_Callback NOP_Process #define EP5_IN_Callback NOP_Process diff --git a/src/hw_config.c b/src/hw_config.c index 181e439..9701d24 100644 --- a/src/hw_config.c +++ b/src/hw_config.c @@ -33,6 +33,7 @@ #include "usb_desc.h" #include "usb_pwr.h" #include "usb_prop.h" +#include /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ @@ -43,6 +44,9 @@ ErrorStatus HSEStartUpStatus; EXTI_InitTypeDef EXTI_InitStructure; /* Extern variables ----------------------------------------------------------*/ +extern int8_t Return_Buff0[24]; +extern int8_t Return_Buff1[24]; + /* Private function prototypes -----------------------------------------------*/ static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len); /* Private functions ---------------------------------------------------------*/ @@ -299,6 +303,10 @@ void USB_Cable_Config (FunctionalState NewState) *******************************************************************************/ void Speaker_Config(void) { + memset(Return_Buff0, 0xAA, sizeof(Return_Buff0)); + memset(Return_Buff1, 0xBB, sizeof(Return_Buff1)); + SetEPTxValid(ENDP2); + #ifdef USE_STM32L152_EVAL DAC_InitTypeDef DAC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; diff --git a/src/usb_desc.c b/src/usb_desc.c index a0c05af..910ac19 100644 --- a/src/usb_desc.c +++ b/src/usb_desc.c @@ -64,7 +64,7 @@ const uint8_t Speaker_ConfigDescriptor[] = /* Configuration 1 */ 0x09, /* bLength */ USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */ - 0x2D, /* wTotalLength 45 bytes*/ + 0x36, /* wTotalLength 54 bytes*/ 0x00, 0x02, /* bNumInterfaces */ 0x01, /* bConfigurationValue */ @@ -157,7 +157,7 @@ const uint8_t Speaker_ConfigDescriptor[] = USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */ 0x01, /* bInterfaceNumber */ 0x01, /* bAlternateSetting */ - 0x01, /* bNumEndpoints */ + 0x02, /* bNumEndpoints */ 0x00, /* was USB_DEVICE_CLASS_AUDIO, * bInterfaceClass */ 0x00, /* AUDIO_SUBCLASS_AUDIOSTREAMING,* bInterfaceSubClass */ 0x00, /* AUDIO_PROTOCOL_UNDEFINED, * bInterfaceProtocol */ @@ -213,6 +213,19 @@ const uint8_t Speaker_ConfigDescriptor[] = 0x00, /* 07 byte*/ #endif + + /* Endpoint 2 - Standard Descriptor */ + AUDIO_STANDARD_ENDPOINT_DESC_SIZE, /* bLength */ + USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ + 0x82, /* bEndpointAddress 2 in endpoint*/ + USB_ENDPOINT_TYPE_ISOCHRONOUS, /* bmAttributes */ + 0x16, /* wMaxPacketSize 22 bytes*/ + 0x00, + 0x01, /* bInterval */ + 0x00, /* bRefresh */ + 0x00, /* bSynchAddress */ + /* 09 byte*/ + }; /* USB String Descriptor (optional) */ diff --git a/src/usb_endp.c b/src/usb_endp.c index 25521a4..7f62820 100644 --- a/src/usb_endp.c +++ b/src/usb_endp.c @@ -34,6 +34,8 @@ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint8_t Stream_Buff[24]; +uint8_t Return_Buff0[24]; +uint8_t Return_Buff1[24]; uint16_t In_Data_Offset; /* Extern variables ----------------------------------------------------------*/ @@ -51,7 +53,7 @@ void EP1_OUT_Callback(void) { uint16_t Data_Len; /* data length*/ - if (GetENDPOINT(ENDP1) & EP_DTOG_TX) + if (GetENDPOINT(ENDP1) & EP_DTOG_RX) // RX? { /*read from ENDP1_BUF0Addr buffer*/ Data_Len = GetEPDblBuf0Count(ENDP1); @@ -63,9 +65,28 @@ void EP1_OUT_Callback(void) Data_Len = GetEPDblBuf1Count(ENDP1); PMAToUserBufferCopy(Stream_Buff, ENDP1_BUF1Addr, Data_Len); } - FreeUserBuffer(ENDP1, EP_DBUF_OUT); + // FreeUserBuffer(ENDP1, EP_DBUF_OUT); // should not be used for isochronous? In_Data_Offset += Data_Len; } -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +void EP2_IN_Callback(void) +{ + uint16_t Data_Len; /* data length*/ + if (GetENDPOINT(ENDP2) & EP_DTOG_TX) /* USB hw 1 -> sw 0 */ + { + Data_Len = 0x16; // ? + UserToPMABufferCopy(Return_Buff0, ENDP2_BUF0Addr, Data_Len); + SetEPDblBuf0Count(ENDP2, EP_DBUF_IN, Data_Len); + } + else + { + Data_Len = 0x16; // ? + UserToPMABufferCopy(Return_Buff1, ENDP2_BUF1Addr, Data_Len); + SetEPDblBuf1Count(ENDP2, EP_DBUF_IN, Data_Len); + } + // FreeUserBuffer(ENDP2, EP_DBUF_IN); // not used for isochronous + // SetEPTxValid(ENDP2); not needed for isochronous, stays valid +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/src/usb_prop.c b/src/usb_prop.c index 43b0901..ba78088 100644 --- a/src/usb_prop.c +++ b/src/usb_prop.c @@ -153,19 +153,25 @@ void Speaker_Reset() SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); SetEPTxAddr(ENDP0, ENDP0_TXADDR); Clear_Status_Out(ENDP0); - SetEPRxValid(ENDP0); - /* Initialize Endpoint 1 */ + /* Initialize Endpoint 1 (unidirectional OUT) */ SetEPType(ENDP1, EP_ISOCHRONOUS); SetEPDblBuffAddr(ENDP1, ENDP1_BUF0Addr, ENDP1_BUF1Addr); - SetEPDblBuffCount(ENDP1, EP_DBUF_OUT, 0x40); - ClearDTOG_RX(ENDP1); - ClearDTOG_TX(ENDP1); - ToggleDTOG_TX(ENDP1); - SetEPRxStatus(ENDP1, EP_RX_VALID); + SetEPDblBuffCount(ENDP1, EP_DBUF_OUT, 0x16); + ClearDTOG_RX(ENDP1); /* SW starts with BUF1 */ SetEPTxStatus(ENDP1, EP_TX_DIS); + SetEPRxValid(ENDP1); + + /* Initialize Endpoint 2 (unidirectional IN) */ + SetEPType(ENDP2, EP_ISOCHRONOUS); + SetEPDblBuffAddr(ENDP2, ENDP2_BUF0Addr, ENDP2_BUF1Addr); + SetEPDblBuffCount(ENDP2, EP_DBUF_IN, 0x16); + ClearDTOG_TX(ENDP2); /* SW starts with BUF1 */ + SetEPRxStatus(ENDP2, EP_RX_DIS); + SetEPTxValid(ENDP2); SetEPRxValid(ENDP0); + /* Set this device to response on default address */ SetDeviceAddress(0); -- 2.25.1