Skip to content

Commit

Permalink
WIP upgrade to USBD firmware lib 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tlyu committed Nov 28, 2022
1 parent 30646c3 commit e3f95b2
Show file tree
Hide file tree
Showing 44 changed files with 1,174 additions and 738 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
\brief the header file of USB audio device class core functions
\version 2020-08-01, V3.0.0, firmware for GD32F30x
\version 2022-06-10, V3.1.0, firmware for GD32F30x
*/

/*
Copyright (c) 2020, GigaDevice Semiconductor Inc.
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -39,84 +40,90 @@ OF SUCH DAMAGE.

#define FORMAT_24BIT(x) (uint8_t)(x);(uint8_t)((x) >> 8);(uint8_t)((x) >> 16)

/* audio_freq * data_size (2 bytes) * num_channels (stereo: 2) */
#define DEFAULT_OUT_BIT_RESOLUTION 16U
#define DEFAULT_OUT_CHANNEL_NBR 2U /* mono = 1, stereo = 2 */
#define AUDIO_OUT_PACKET (uint32_t)(((USBD_AUDIO_FREQ_16K * \
(DEFAULT_OUT_BIT_RESOLUTION / 8U) *\
DEFAULT_OUT_CHANNEL_NBR) / 1000U))

/* number of sub-packets in the audio transfer buffer. you can modify this value but always make sure
/* number of sub-packets in the audio transfer buffer. user can modify this value but always make sure
that it is an even number and higher than 3 */
#define OUT_PACKET_NUM 4U
#define OUT_PACKET_NUM 120U

/* total size of the audio transfer buffer */
#define OUT_BUF_MARGIN 4U
#define TOTAL_OUT_BUF_SIZE ((uint32_t)((AUDIO_OUT_PACKET + OUT_BUF_MARGIN) * OUT_PACKET_NUM))
#define OUT_BUF_MARGIN 0U
#define TOTAL_OUT_BUF_SIZE ((uint32_t)((SPEAKER_OUT_PACKET + OUT_BUF_MARGIN) * OUT_PACKET_NUM))

#define AUDIO_CONFIG_DESC_SET_LEN 109U
#define AUDIO_INTERFACE_DESC_SIZE 9U
#define AD_CONFIG_DESC_SET_LEN (sizeof(usb_desc_config_set))
#define AD_INTERFACE_DESC_SIZE 9U

#define USB_AUDIO_DESC_SIZ 0x09U
#define AUDIO_STANDARD_EP_DESC_SIZE 0x09U
#define AUDIO_STREAMING_EP_DESC_SIZE 0x07U
#define USB_AD_DESC_SIZ 0x09U
#define AD_STANDARD_EP_DESC_SIZE 0x09U
#define AD_STREAMING_EP_DESC_SIZE 0x07U

/* audio interface class code */
#define USB_CLASS_AUDIO 0x01U
#define USB_CLASS_AUDIO 0x01U

/* audio interface subclass codes */
#define AUDIO_SUBCLASS_CONTROL 0x01U
#define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02U
#define AUDIO_SUBCLASS_MIDISTREAMING 0x03U
#define AD_SUBCLASS_CONTROL 0x01U
#define AD_SUBCLASS_AUDIOSTREAMING 0x02U
#define AD_SUBCLASS_MIDISTREAMING 0x03U

/* audio interface protocol codes */
#define AUDIO_PROTOCOL_UNDEFINED 0x00U
#define AUDIO_STREAMING_GENERAL 0x01U
#define AUDIO_STREAMING_FORMAT_TYPE 0x02U
#define AD_PROTOCOL_UNDEFINED 0x00U
#define AD_STREAMING_GENERAL 0x01U
#define AD_STREAMING_FORMAT_TYPE 0x02U

/* audio class-specific descriptor types */
#define AUDIO_DESCTYPE_UNDEFINED 0x20U
#define AUDIO_DESCTYPE_DEVICE 0x21U
#define AUDIO_DESCTYPE_CONFIGURATION 0x22U
#define AUDIO_DESCTYPE_STRING 0x23U
#define AUDIO_DESCTYPE_INTERFACE 0x24U
#define AUDIO_DESCTYPE_ENDPOINT 0x25U
#define AD_DESCTYPE_UNDEFINED 0x20U
#define AD_DESCTYPE_DEVICE 0x21U
#define AD_DESCTYPE_CONFIGURATION 0x22U
#define AD_DESCTYPE_STRING 0x23U
#define AD_DESCTYPE_INTERFACE 0x24U
#define AD_DESCTYPE_ENDPOINT 0x25U

/* audio control interface descriptor subtypes */
#define AUDIO_CONTROL_HEADER 0x01U
#define AUDIO_CONTROL_INPUT_TERMINAL 0x02U
#define AUDIO_CONTROL_OUTPUT_TERMINAL 0x03U
#define AUDIO_CONTROL_MIXER_UNIT 0x04U
#define AUDIO_CONTROL_SELECTOR_UNIT 0x05U
#define AUDIO_CONTROL_FEATURE_UNIT 0x06U
#define AUDIO_CONTROL_PROCESSING_UNIT 0x07U
#define AUDIO_CONTROL_EXTENSION_UNIT 0x08U

#define AUDIO_INPUT_TERMINAL_DESC_SIZE 0x0CU
#define AUDIO_OUTPUT_TERMINAL_DESC_SIZE 0x09U
#define AUDIO_STREAMING_INTERFACE_DESC_SIZE 0x07U

#define AUDIO_CONTROL_MUTE 0x0001U

#define AUDIO_FORMAT_TYPE_I 0x01U
#define AUDIO_FORMAT_TYPE_III 0x03U

#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01U
#define AUDIO_ENDPOINT_GENERAL 0x01U

#define AUDIO_REQ_GET_CUR 0x81U
#define AUDIO_REQ_SET_CUR 0x01U

#define AUDIO_OUT_STREAMING_CTRL 0x02U

#define PACKET_SIZE(freq) (((freq) * 2U) * 2U / 1000U)

#define AUDIO_PACKET_SIZE(frq) (uint8_t)(PACKET_SIZE(frq) & 0xFFU), \
(uint8_t)((PACKET_SIZE(frq) >> 8U) & 0xFFU)

#define SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq) >> 8U), \
(uint8_t)((frq) >> 16U)
#define AD_CONTROL_HEADER 0x01U
#define AD_CONTROL_INPUT_TERMINAL 0x02U
#define AD_CONTROL_OUTPUT_TERMINAL 0x03U
#define AD_CONTROL_MIXER_UNIT 0x04U
#define AD_CONTROL_SELECTOR_UNIT 0x05U
#define AD_CONTROL_FEATURE_UNIT 0x06U
#define AD_CONTROL_PROCESSING_UNIT 0x07U
#define AD_CONTROL_EXTENSION_UNIT 0x08U

#define AD_INPUT_TERMINAL_DESC_SIZE 0x0CU
#define AD_OUTPUT_TERMINAL_DESC_SIZE 0x09U
#define AD_STREAMING_INTERFACE_DESC_SIZE 0x07U

#define AD_CONTROL_MUTE 0x01U
#define AD_CONTROL_VOLUME 0x02U

#define AD_FORMAT_TYPE_I 0x01U
#define AD_FORMAT_TYPE_III 0x03U

#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01U
#define AD_ENDPOINT_GENERAL 0x01U

#define AD_REQ_UNDEFINED 0x00U
#define AD_REQ_SET_CUR 0x01U
#define AD_REQ_GET_CUR 0x81U
#define AD_REQ_SET_MIN 0x02U
#define AD_REQ_GET_MIN 0x82U
#define AD_REQ_SET_MAX 0x03U
#define AD_REQ_GET_MAX 0x83U
#define AD_REQ_SET_RES 0x04U
#define AD_REQ_GET_RES 0x84U
#define AD_REQ_SET_MEM 0x05U
#define AD_REQ_GET_MEM 0x85U
#define AD_REQ_GET_STAT 0xFFU

#define AD_OUT_STREAMING_CTRL 0x02U
#define AD_IN_STREAMING_CTRL 0x05U

/* audio stream interface number */
enum
{
SPEAK_INTERFACE_COUNT,
CONFIG_DESC_AS_ITF_COUNT,
};

#define AC_ITF_TOTAL_LEN (sizeof(usb_desc_AC_itf) + CONFIG_DESC_AS_ITF_COUNT*(sizeof(usb_desc_input_terminal) + \
sizeof(usb_desc_mono_feature_unit) + sizeof(usb_desc_output_terminal)))

#pragma pack(1)

Expand Down Expand Up @@ -246,6 +253,12 @@ typedef struct
uint8_t* isoc_out_wrptr;
uint8_t* isoc_out_rdptr;

uint16_t buf_free_size;
uint16_t dam_tx_len;

/* usb receive buffer */
uint8_t usb_rx_buffer[SPEAKER_OUT_MAX_PACKET];

/* main buffer for audio control requests transfers and its relative variables */
uint8_t audioctl[64];
uint8_t audioctl_unit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
\brief audio OUT (playback) interface header file
\version 2020-08-01, V3.0.0, firmware for GD32F30x
\version 2022-06-10, V3.1.0, firmware for GD32F30x
*/

/*
Copyright (c) 2020, GigaDevice Semiconductor Inc.
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -37,38 +38,10 @@ OF SUCH DAMAGE.

#include "usbd_conf.h"

/* audio commands enumeration */
typedef enum
{
AUDIO_CMD_PLAY = 1U,
AUDIO_CMD_PAUSE,
AUDIO_CMD_STOP,
}audio_cmd_enum;

/* mute commands */
#define AUDIO_MUTE 0x01U
#define AUDIO_UNMUTE 0x00U

/* functions return value */
#define AUDIO_OK 0x00U
#define AUDIO_FAIL 0xFFU

/* audio machine states */
#define AUDIO_STATE_INACTIVE 0x00U
#define AUDIO_STATE_ACTIVE 0x01U
#define AUDIO_STATE_PLAYING 0x02U
#define AUDIO_STATE_PAUSED 0x03U
#define AUDIO_STATE_STOPPED 0x04U
#define AUDIO_STATE_ERROR 0x05U

typedef struct {
uint8_t (*audio_init) (uint32_t audio_freq, uint32_t volume, uint32_t options);
uint8_t (*audio_init) (uint32_t audio_freq, uint32_t volume);
uint8_t (*audio_deinit) (uint32_t options);
uint8_t (*audio_cmd) (uint8_t* pbuf, uint32_t size, uint8_t cmd);
uint8_t (*audio_volume_ctl) (uint8_t vol);
uint8_t (*audio_mute_ctl) (uint8_t cmd);
uint8_t (*audio_periodic_tc) (uint8_t cmd);
uint8_t (*audio_state_get) (void);
} audio_fops_struct;

extern audio_fops_struct audio_out_fops;
Expand Down
Loading

0 comments on commit e3f95b2

Please sign in to comment.