5151
5252#define USB_DESC_TYPE_ASSOCIATION (0x0b)
5353
54- #define CDC_CMD_PACKET_SIZE 8 // Control Endpoint Packet size
55- #define CDC_DATA_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
56- #define CDC_DATA_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
57-
58- #define MSC_MAX_PACKET 0x40
59- #define USB_MSC_CONFIG_DESC_SIZ 32
60- #define BOT_GET_MAX_LUN 0xFE
61- #define BOT_RESET 0xFF
62-
63- #define HID_DESCRIPTOR_TYPE 0x21
64- #define HID_REPORT_DESC 0x22
65- #define HID_REQ_SET_PROTOCOL 0x0B
66- #define HID_REQ_GET_PROTOCOL 0x03
67- #define HID_REQ_SET_IDLE 0x0A
68- #define HID_REQ_GET_IDLE 0x02
54+ #define CDC_CMD_PACKET_SIZE (8) // Control Endpoint Packet size
55+ #define CDC_DATA_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
56+ #define CDC_DATA_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
57+
58+ #define MSC_MAX_PACKET (0x40)
59+ #define BOT_GET_MAX_LUN (0xfe)
60+ #define BOT_RESET (0xff)
61+
62+ #define HID_DESCRIPTOR_TYPE (0x21)
63+ #define HID_REPORT_DESC (0x22)
64+ #define HID_REQ_SET_PROTOCOL (0x0b)
65+ #define HID_REQ_GET_PROTOCOL (0x03)
66+ #define HID_REQ_SET_IDLE (0x0a)
67+ #define HID_REQ_GET_IDLE (0x02)
6968
7069typedef enum {
71- HID_IDLE = 0 ,
72- HID_BUSY ,
70+ HID_IDLE = 0 ,
71+ HID_BUSY ,
7372} HID_StateTypeDef ;
7473
7574typedef struct {
76- uint32_t Protocol ;
77- uint32_t IdleState ;
78- uint32_t AltSetting ;
79- HID_StateTypeDef state ;
75+ uint32_t Protocol ;
76+ uint32_t IdleState ;
77+ uint32_t AltSetting ;
78+ HID_StateTypeDef state ;
8079} USBD_HID_HandleTypeDef ;
8180
8281static uint8_t usbd_mode ;
@@ -784,17 +783,17 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp
784783 break ;
785784
786785 case BOT_RESET :
787- if ((req -> wValue == 0 ) && (req -> wLength == 0 ) && ((req -> bmRequest & 0x80 ) != 0x80 )) {
788- MSC_BOT_Reset (pdev );
789- } else {
790- USBD_CtlError (pdev , req );
791- return USBD_FAIL ;
792- }
793- break ;
786+ if ((req -> wValue == 0 ) && (req -> wLength == 0 ) && ((req -> bmRequest & 0x80 ) != 0x80 )) {
787+ MSC_BOT_Reset (pdev );
788+ } else {
789+ USBD_CtlError (pdev , req );
790+ return USBD_FAIL ;
791+ }
792+ break ;
794793
795794 default :
796- USBD_CtlError (pdev , req );
797- return USBD_FAIL ;
795+ USBD_CtlError (pdev , req );
796+ return USBD_FAIL ;
798797 }
799798 } else if ((usbd_mode & USBD_MODE_HID ) && req -> wIndex == hid_iface_num ) {
800799 switch (req -> bRequest ) {
@@ -834,46 +833,46 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp
834833 break ;
835834
836835 case USB_REQ_CLEAR_FEATURE :
837- /* Flush the FIFO and Clear the stall status */
836+ // Flush the FIFO and Clear the stall status
838837 USBD_LL_FlushEP (pdev , (uint8_t )req -> wIndex );
839838
840- /* Re-activate the EP */
841- USBD_LL_CloseEP (pdev , (uint8_t )req -> wIndex );
839+ // Re-activate the EP
840+ USBD_LL_CloseEP (pdev , (uint8_t )req -> wIndex );
842841 if ((((uint8_t )req -> wIndex ) & 0x80 ) == 0x80 ) {
843- /* Open EP IN */
842+ // Open EP IN
844843 USBD_LL_OpenEP (pdev , MSC_IN_EP , USBD_EP_TYPE_BULK , MSC_MAX_PACKET );
845844 } else {
846- /* Open EP OUT */
845+ // Open EP OUT
847846 USBD_LL_OpenEP (pdev , MSC_OUT_EP , USBD_EP_TYPE_BULK , MSC_MAX_PACKET );
848847 }
849- /* Handle BOT error */
848+ // Handle BOT error
850849 MSC_BOT_CplClrFeature (pdev , (uint8_t )req -> wIndex );
851850 break ;
852851 }
853852 } else if ((usbd_mode & USBD_MODE_HID ) && req -> wIndex == hid_iface_num ) {
854853 switch (req -> bRequest ) {
855854 case USB_REQ_GET_DESCRIPTOR : {
856- uint16_t len = 0 ;
857- const uint8_t * pbuf = NULL ;
858- if (req -> wValue >> 8 == HID_REPORT_DESC ) {
859- len = hid_desc [HID_DESC_OFFSET_REPORT_DESC_LEN ];
860- len = MIN (len , req -> wLength );
861- pbuf = hid_report_desc ;
862- } else if (req -> wValue >> 8 == HID_DESCRIPTOR_TYPE ) {
863- len = MIN (HID_SUBDESC_LEN , req -> wLength );
864- pbuf = hid_desc + HID_DESC_OFFSET_SUBDESC ;
865- }
866- USBD_CtlSendData (pdev , (uint8_t * )pbuf , len );
867- break ;
868- }
855+ uint16_t len = 0 ;
856+ const uint8_t * pbuf = NULL ;
857+ if (req -> wValue >> 8 == HID_REPORT_DESC ) {
858+ len = hid_desc [HID_DESC_OFFSET_REPORT_DESC_LEN ];
859+ len = MIN (len , req -> wLength );
860+ pbuf = hid_report_desc ;
861+ } else if (req -> wValue >> 8 == HID_DESCRIPTOR_TYPE ) {
862+ len = MIN (HID_SUBDESC_LEN , req -> wLength );
863+ pbuf = hid_desc + HID_DESC_OFFSET_SUBDESC ;
864+ }
865+ USBD_CtlSendData (pdev , (uint8_t * )pbuf , len );
866+ break ;
867+ }
869868
870869 case USB_REQ_GET_INTERFACE :
871- USBD_CtlSendData (pdev , (uint8_t * )& HID_ClassData .AltSetting , 1 );
872- break ;
870+ USBD_CtlSendData (pdev , (uint8_t * )& HID_ClassData .AltSetting , 1 );
871+ break ;
873872
874873 case USB_REQ_SET_INTERFACE :
875- HID_ClassData .AltSetting = (uint8_t )(req -> wValue );
876- break ;
874+ HID_ClassData .AltSetting = (uint8_t )(req -> wValue );
875+ break ;
877876 }
878877 }
879878 break ;
@@ -887,11 +886,12 @@ static uint8_t EP0_TxSent(USBD_HandleTypeDef *pdev) {
887886*/
888887
889888static uint8_t USBD_CDC_MSC_HID_EP0_RxReady (USBD_HandleTypeDef * pdev ) {
890- if ((CDC_fops != NULL ) && (CDC_ClassData .CmdOpCode != 0xFF )) {
891- CDC_fops -> Control (CDC_ClassData .CmdOpCode , (uint8_t * )CDC_ClassData .data , CDC_ClassData .CmdLength );
892- CDC_ClassData .CmdOpCode = 0xFF ;
893- }
894- return USBD_OK ;
889+ if ((CDC_fops != NULL ) && (CDC_ClassData .CmdOpCode != 0xff )) {
890+ CDC_fops -> Control (CDC_ClassData .CmdOpCode , (uint8_t * )CDC_ClassData .data , CDC_ClassData .CmdLength );
891+ CDC_ClassData .CmdOpCode = 0xff ;
892+ }
893+
894+ return USBD_OK ;
895895}
896896
897897static uint8_t USBD_CDC_MSC_HID_DataIn (USBD_HandleTypeDef * pdev , uint8_t epnum ) {
@@ -953,81 +953,40 @@ uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef
953953 }
954954}
955955
956- /**
957- * @brief USBD_CDC_SetTxBuffer
958- * @param pdev: device instance
959- * @param pbuff: Tx Buffer
960- * @retval status
961- */
962- uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef * pdev ,
963- uint8_t * pbuff ,
964- uint16_t length )
965- {
966- CDC_ClassData .TxBuffer = pbuff ;
967- CDC_ClassData .TxLength = length ;
968-
969- return USBD_OK ;
956+ uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef * pdev , uint8_t * pbuff , uint16_t length ) {
957+ CDC_ClassData .TxBuffer = pbuff ;
958+ CDC_ClassData .TxLength = length ;
959+ return USBD_OK ;
970960}
971961
972-
973- /**
974- * @brief USBD_CDC_SetRxBuffer
975- * @param pdev: device instance
976- * @param pbuff: Rx Buffer
977- * @retval status
978- */
979- uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef * pdev ,
980- uint8_t * pbuff )
981- {
982- CDC_ClassData .RxBuffer = pbuff ;
983-
984- return USBD_OK ;
962+ uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef * pdev , uint8_t * pbuff ) {
963+ CDC_ClassData .RxBuffer = pbuff ;
964+ return USBD_OK ;
985965}
986966
987- /**
988- * @brief USBD_CDC_DataOut
989- * Data received on non-control Out endpoint
990- * @param pdev: device instance
991- * @param epnum: endpoint number
992- * @retval status
993- */
994- uint8_t USBD_CDC_TransmitPacket (USBD_HandleTypeDef * pdev ) {
995- if (CDC_ClassData .TxState == 0 ) {
996-
997- /* Transmit next packet */
998- USBD_LL_Transmit (pdev ,
999- CDC_IN_EP ,
1000- CDC_ClassData .TxBuffer ,
1001- CDC_ClassData .TxLength );
1002-
1003- /* Tx Transfer in progress */
1004- CDC_ClassData .TxState = 1 ;
1005- return USBD_OK ;
1006- }
1007- else
1008- {
1009- return USBD_BUSY ;
967+ // data received on non-control OUT endpoint
968+ uint8_t USBD_CDC_TransmitPacket (USBD_HandleTypeDef * pdev ) {
969+ if (CDC_ClassData .TxState == 0 ) {
970+ // transmit next packet
971+ USBD_LL_Transmit (pdev , CDC_IN_EP , CDC_ClassData .TxBuffer , CDC_ClassData .TxLength );
972+
973+ // Tx transfer in progress
974+ CDC_ClassData .TxState = 1 ;
975+ return USBD_OK ;
976+ } else {
977+ return USBD_BUSY ;
1010978 }
1011979}
1012980
1013-
1014- /**
1015- * @brief USBD_CDC_ReceivePacket
1016- * prepare OUT Endpoint for reception
1017- * @param pdev: device instance
1018- * @retval status
1019- */
981+ // prepare OUT endpoint for reception
1020982uint8_t USBD_CDC_ReceivePacket (USBD_HandleTypeDef * pdev ) {
1021983 // Suspend or Resume USB Out process
1022984 if (pdev -> dev_speed == USBD_SPEED_HIGH ) {
1023985 return USBD_FAIL ;
1024986 }
1025987
1026- // Prepare Out endpoint to receive next packet */
1027- USBD_LL_PrepareReceive (pdev ,
1028- CDC_OUT_EP ,
1029- CDC_ClassData .RxBuffer ,
1030- CDC_DATA_OUT_PACKET_SIZE );
988+ // Prepare Out endpoint to receive next packet
989+ USBD_LL_PrepareReceive (pdev , CDC_OUT_EP , CDC_ClassData .RxBuffer , CDC_DATA_OUT_PACKET_SIZE );
1031990
1032991 return USBD_OK ;
1033992}
0 commit comments