mirror of
https://github.com/Fishwaldo/bl_mcu_sdk.git
synced 2025-07-07 13:28:48 +00:00
[feat][examples/usb] add usb audio with mic and speaker dmo,add usb cdc shell demo
This commit is contained in:
parent
fae3c81a98
commit
d8a91f4313
8 changed files with 126082 additions and 0 deletions
7
examples/usb/usb_audio_mic_speaker/CMakeLists.txt
Normal file
7
examples/usb/usb_audio_mic_speaker/CMakeLists.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
set(BSP_COMMON_DIR ${CMAKE_SOURCE_DIR}/bsp/bsp_common)
|
||||
set(TARGET_REQUIRED_LIBS usb_stack)
|
||||
# set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/usb_audio.ld)
|
||||
set(TARGET_REQUIRED_PRIVATE_INCLUDE ${BSP_COMMON_DIR}/usb ${BSP_COMMON_DIR}/es8388)
|
||||
set(TARGET_REQUIRED_SRCS ${BSP_COMMON_DIR}/usb/usb_dc.c ${BSP_COMMON_DIR}/es8388/bsp_es8388.c)
|
||||
set(mains main.c)
|
||||
generate_bin()
|
652
examples/usb/usb_audio_mic_speaker/main.c
Normal file
652
examples/usb/usb_audio_mic_speaker/main.c
Normal file
|
@ -0,0 +1,652 @@
|
|||
/**
|
||||
* @file main.c
|
||||
* @brief
|
||||
*
|
||||
* Copyright (c) 2021 Bouffalolab team
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "hal_usb.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_audio.h"
|
||||
#include "hal_dma.h"
|
||||
#include "hal_i2s.h"
|
||||
#include "hal_dma.h"
|
||||
#include "bsp_es8388.h"
|
||||
|
||||
#define USBD_VID 0xffff
|
||||
#define USBD_PID 0xffff
|
||||
#define USBD_MAX_POWER 100
|
||||
#define USBD_LANGID_STRING 1033
|
||||
|
||||
#define AUDIO_IN_EP 0x81
|
||||
#define AUDIO_OUT_EP 0x02
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 34
|
||||
#define HID_MOUSE_REPORT_DESC_SIZE 74
|
||||
|
||||
/* AUDIO Class Config */
|
||||
#define USBD_AUDIO_FREQ 16000U
|
||||
|
||||
#define AUDIO_SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))
|
||||
|
||||
/* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */
|
||||
#define AUDIO_OUT_PACKET ((uint32_t)((USBD_AUDIO_FREQ * 2 * 2) / 1000))
|
||||
/* 16bit(2 Bytes) 双声道(Mono:2) */
|
||||
#define AUDIO_IN_PACKET ((uint32_t)((USBD_AUDIO_FREQ * 2 * 2) / 1000))
|
||||
|
||||
#define MIC_IT_ID 0x01
|
||||
#define MIC_FU_ID 0x02
|
||||
#define MIC_OT_ID 0x03
|
||||
|
||||
#define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 + \
|
||||
9 + \
|
||||
10 + \
|
||||
12 + \
|
||||
12 + \
|
||||
9 + \
|
||||
9 + \
|
||||
10 + \
|
||||
9 + \
|
||||
9 + \
|
||||
9 + \
|
||||
7 + \
|
||||
11 + \
|
||||
9 + \
|
||||
7 + \
|
||||
9 + \
|
||||
9 + \
|
||||
7 + \
|
||||
11 + \
|
||||
9 + \
|
||||
7)
|
||||
|
||||
USB_DESC_SECTION const uint8_t audio_descriptor[] = {
|
||||
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01),
|
||||
USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x03, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
|
||||
|
||||
/* ------------------ AudioControl Interface ------------------ */
|
||||
/* USB Microphone Standard AC Interface Descriptor */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */
|
||||
0x00, /* bInterfaceNumber */
|
||||
0x00, /* bAlternateSetting */
|
||||
0x00, /* bNumEndpoints */
|
||||
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */
|
||||
AUDIO_SUBCLASS_AUDIOCONTROL, /* bInterfaceSubClass */
|
||||
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Class-specific AC Interface Descriptor */
|
||||
0x0a, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_HEADER, /* bDescriptorSubtype */
|
||||
0x00, /* 1.00 */ /* bcdADC */
|
||||
0x01,
|
||||
0x47, /* wTotalLength */
|
||||
0x00,
|
||||
0x02, /* bInCollection */
|
||||
0x01, /* baInterfaceNr */
|
||||
0x02, /* baInterfaceNr */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Input Terminal Descriptor */
|
||||
0x0C, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_INPUT_TERMINAL, /* bDescriptorSubtype */
|
||||
0x02, /* bTerminalID */
|
||||
0x01, /* wTerminalType : Microphone 0x0201 */
|
||||
0x02,
|
||||
0x00, /* bAssocTerminal */
|
||||
0x02, /* bNrChannels */
|
||||
0x01, /* wChannelConfig : Mono sets no position bits */
|
||||
0x00,
|
||||
0x00, /* iChannelNames */
|
||||
0x00, /* iTerminal */
|
||||
/* 12 byte*/
|
||||
|
||||
/* USB Microphone Input Terminal Descriptor */
|
||||
0x0C, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_INPUT_TERMINAL, /* bDescriptorSubtype */
|
||||
0x01, /* bTerminalID */
|
||||
0x01, /* wTerminalType : Microphone 0x0101 */
|
||||
0x01,
|
||||
0x00, /* bAssocTerminal */
|
||||
0x02, /* bNrChannels */
|
||||
0x01, /* wChannelConfig : Mono sets no position bits */
|
||||
0x00,
|
||||
0x00, /* iChannelNames */
|
||||
0x00, /* iTerminal */
|
||||
/* 12 byte*/
|
||||
|
||||
/*USB Microphone Output Terminal Descriptor */
|
||||
0x09, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_OUTPUT_TERMINAL, /* bDescriptorSubtype */
|
||||
0x06, /* bTerminalID */
|
||||
0x01, /* wTerminalType : speaker */
|
||||
0x03,
|
||||
0x00, /* bAssocTerminal */
|
||||
0x09, /* bSourceID */
|
||||
0x00, /* iTerminal */
|
||||
/* 09 byte*/
|
||||
/*USB Microphone Output Terminal Descriptor */
|
||||
0x09, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_OUTPUT_TERMINAL, /* bDescriptorSubtype */
|
||||
0x07, /* bTerminalID */
|
||||
0x01, /* wTerminalType : speaker */
|
||||
0x01,
|
||||
0x00, /* bAssocTerminal */
|
||||
0x0a, /* bSourceID */
|
||||
0x00, /* iTerminal */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Audio Feature Unit Descriptor */
|
||||
0x0a, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_FEATURE_UNIT, /* bDescriptorSubtype */
|
||||
0x09, /* bUnitID */
|
||||
0x01, /* bSourceID */
|
||||
0x01, /* bControlSize */
|
||||
0x01, /* bmaControls(0) Mute */
|
||||
0x02, /* bmaControls(1) Volume */
|
||||
0x02, /* bmaControls(2) Volume */
|
||||
0x00, /* iFeature */
|
||||
/* 10 byte*/
|
||||
/* USB Microphone Audio Feature Unit Descriptor */
|
||||
0x09, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_CONTROL_FEATURE_UNIT, /* bDescriptorSubtype */
|
||||
0x0a, /* bUnitID */
|
||||
0x02, /* bSourceID */
|
||||
0x01, /* bControlSize */
|
||||
0x43, /* bmaControls(0) Mute */
|
||||
0x00, /* bmaControls(1) Volume */
|
||||
0x00, /* iFeature */
|
||||
/* 9 byte*/
|
||||
|
||||
/* --------------- AudioStreaming Interface --------------- */
|
||||
/* USB Microphone Standard AS Interface Descriptor - Audio Streaming Zero Bandwith */
|
||||
/* Interface 1, Alternate Setting 0 */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */
|
||||
0x01, /* bInterfaceNumber */
|
||||
0x00, /* bAlternateSetting */
|
||||
0x00, /* bNumEndpoints */
|
||||
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */
|
||||
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubClass */
|
||||
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Standard AS Interface Descriptor - Audio Streaming Operational */
|
||||
/* Interface 1, Alternate Setting 1 */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */
|
||||
0x01, /* bInterfaceNumber */
|
||||
0x01, /* bAlternateSetting */
|
||||
0x01, /* bNumEndpoints */
|
||||
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */
|
||||
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubClass */
|
||||
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Class-specific AS General Interface Descriptor */
|
||||
0X07, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_STREAMING_GENERAL, /* bDescriptorSubtype */
|
||||
0x01, /* bTerminalLink : Unit ID of the Output Terminal*/
|
||||
0x01, /* bDelay */
|
||||
0x01, /* wFormatTag : AUDIO_FORMAT_PCM */
|
||||
0x00,
|
||||
/* 07 byte*/
|
||||
|
||||
/* USB Microphone Audio Type I Format Type Descriptor */
|
||||
0x0b, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_STREAMING_FORMAT_TYPE, /* bDescriptorSubtype */
|
||||
AUDIO_FORMAT_TYPE_I, /* bFormatType */
|
||||
0x02, /* bNrChannels */
|
||||
0x02, /* bSubFrameSize : 2 Bytes per audio subframe */
|
||||
0x10, /* bBitResolution : 16 bits per sample */
|
||||
0x01, /* bSamFreqType : only one frequency supported */
|
||||
AUDIO_SAMPLE_FREQ(USBD_AUDIO_FREQ), /* tSamFreq : Audio sampling frequency coded on 3 bytes */
|
||||
/* 11 byte*/
|
||||
|
||||
/* USB Microphone Standard AS Audio Data Endpoint Descriptor */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */
|
||||
AUDIO_OUT_EP, /* bEndpointAddress : IN endpoint 1 */
|
||||
0x01, /* bmAttributes */
|
||||
LO_BYTE(AUDIO_OUT_PACKET), /* wMaxPacketSize */
|
||||
HI_BYTE(AUDIO_OUT_PACKET),
|
||||
0x01, /* bInterval : one packet per frame */
|
||||
0x00, /* bRefresh */
|
||||
0x00, /* bSynchAddress */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Class-specific Isoc. Audio Data Endpoint Descriptor */
|
||||
0x07, /* bLength */
|
||||
AUDIO_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_ENDPOINT_GENERAL, /* bDescriptor */
|
||||
0x00, /* bmAttributes AUDIO_SAMPLING_FREQ_CONTROL */
|
||||
0x00, /* bLockDelayUnits */
|
||||
0x00, /* wLockDelay */
|
||||
0x00,
|
||||
|
||||
/* 07 byte*/
|
||||
/* --------------- AudioStreaming Interface --------------- */
|
||||
/* USB Microphone Standard AS Interface Descriptor - Audio Streaming Zero Bandwith */
|
||||
/* Interface 1, Alternate Setting 0 */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */
|
||||
0x02, /* bInterfaceNumber */
|
||||
0x00, /* bAlternateSetting */
|
||||
0x00, /* bNumEndpoints */
|
||||
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */
|
||||
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubClass */
|
||||
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Standard AS Interface Descriptor - Audio Streaming Operational */
|
||||
/* Interface 1, Alternate Setting 1 */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */
|
||||
0x02, /* bInterfaceNumber */
|
||||
0x01, /* bAlternateSetting */
|
||||
0x01, /* bNumEndpoints */
|
||||
USB_DEVICE_CLASS_AUDIO, /* bInterfaceClass */
|
||||
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubClass */
|
||||
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Class-specific AS General Interface Descriptor */
|
||||
0X07, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_STREAMING_GENERAL, /* bDescriptorSubtype */
|
||||
0x07, /* bTerminalLink : Unit ID of the Output Terminal*/
|
||||
0x01, /* bDelay */
|
||||
0x01, /* wFormatTag : AUDIO_FORMAT_PCM */
|
||||
0x00,
|
||||
/* 07 byte*/
|
||||
|
||||
/* USB Microphone Audio Type I Format Type Descriptor */
|
||||
0x0b, /* bLength */
|
||||
AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_STREAMING_FORMAT_TYPE, /* bDescriptorSubtype */
|
||||
AUDIO_FORMAT_TYPE_I, /* bFormatType */
|
||||
0x02, /* bNrChannels */
|
||||
0x02, /* bSubFrameSize : 2 Bytes per audio subframe */
|
||||
0x10, /* bBitResolution : 16 bits per sample */
|
||||
0x01, /* bSamFreqType : only one frequency supported */
|
||||
AUDIO_SAMPLE_FREQ(USBD_AUDIO_FREQ), /* tSamFreq : Audio sampling frequency coded on 3 bytes */
|
||||
/* 11 byte*/
|
||||
|
||||
/* USB Microphone Standard AS Audio Data Endpoint Descriptor */
|
||||
0x09, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */
|
||||
AUDIO_IN_EP, /* bEndpointAddress : IN endpoint 1 */
|
||||
0x01, /* bmAttributes */
|
||||
LO_BYTE(AUDIO_IN_PACKET), /* wMaxPacketSize */
|
||||
HI_BYTE(AUDIO_IN_PACKET),
|
||||
0x01, /* bInterval : one packet per frame */
|
||||
0x00, /* bRefresh */
|
||||
0x00, /* bSynchAddress */
|
||||
/* 09 byte*/
|
||||
|
||||
/* USB Microphone Class-specific Isoc. Audio Data Endpoint Descriptor */
|
||||
0x07, /* bLength */
|
||||
AUDIO_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */
|
||||
AUDIO_ENDPOINT_GENERAL, /* bDescriptor */
|
||||
0x00, /* bmAttributes AUDIO_SAMPLING_FREQ_CONTROL */
|
||||
0x00, /* bLockDelayUnits */
|
||||
0x00, /* wLockDelay */
|
||||
0x00,
|
||||
/* 07 byte*/
|
||||
|
||||
///////////////////////////////////////
|
||||
/// string0 descriptor
|
||||
///////////////////////////////////////
|
||||
USB_LANGID_INIT(USBD_LANGID_STRING),
|
||||
///////////////////////////////////////
|
||||
/// string1 descriptor
|
||||
///////////////////////////////////////
|
||||
0x12, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
|
||||
'B', 0x00, /* wcChar0 */
|
||||
'o', 0x00, /* wcChar1 */
|
||||
'u', 0x00, /* wcChar2 */
|
||||
'f', 0x00, /* wcChar3 */
|
||||
'f', 0x00, /* wcChar4 */
|
||||
'a', 0x00, /* wcChar5 */
|
||||
'l', 0x00, /* wcChar6 */
|
||||
'o', 0x00, /* wcChar7 */
|
||||
///////////////////////////////////////
|
||||
/// string2 descriptor
|
||||
///////////////////////////////////////
|
||||
0x28, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
|
||||
'B', 0x00, /* wcChar0 */
|
||||
'o', 0x00, /* wcChar1 */
|
||||
'u', 0x00, /* wcChar2 */
|
||||
'f', 0x00, /* wcChar3 */
|
||||
'f', 0x00, /* wcChar4 */
|
||||
'a', 0x00, /* wcChar5 */
|
||||
'l', 0x00, /* wcChar6 */
|
||||
'o', 0x00, /* wcChar7 */
|
||||
' ', 0x00, /* wcChar8 */
|
||||
'A', 0x00, /* wcChar9 */
|
||||
'U', 0x00, /* wcChar10 */
|
||||
'D', 0x00, /* wcChar11 */
|
||||
'I', 0x00, /* wcChar12 */
|
||||
'O', 0x00, /* wcChar13 */
|
||||
' ', 0x00, /* wcChar14 */
|
||||
'D', 0x00, /* wcChar15 */
|
||||
'E', 0x00, /* wcChar16 */
|
||||
'M', 0x00, /* wcChar17 */
|
||||
'O', 0x00, /* wcChar18 */
|
||||
///////////////////////////////////////
|
||||
/// string3 descriptor
|
||||
///////////////////////////////////////
|
||||
0x16, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
|
||||
'2', 0x00, /* wcChar0 */
|
||||
'0', 0x00, /* wcChar1 */
|
||||
'2', 0x00, /* wcChar2 */
|
||||
'1', 0x00, /* wcChar3 */
|
||||
'0', 0x00, /* wcChar4 */
|
||||
'3', 0x00, /* wcChar5 */
|
||||
'1', 0x00, /* wcChar6 */
|
||||
'0', 0x00, /* wcChar7 */
|
||||
'0', 0x00, /* wcChar8 */
|
||||
'0', 0x00, /* wcChar9 */
|
||||
///////////////////////////////////////
|
||||
/// device qualifier descriptor
|
||||
///////////////////////////////////////
|
||||
0x0a,
|
||||
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
|
||||
0x00,
|
||||
0x02,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x40,
|
||||
0x01,
|
||||
0x00,
|
||||
|
||||
0x00
|
||||
};
|
||||
|
||||
struct device *usb_fs;
|
||||
static uint8_t play_status = 0;
|
||||
|
||||
static usbd_class_t audio_class;
|
||||
static usbd_interface_t audio_control_intf;
|
||||
static usbd_interface_t audio_stream_intf;
|
||||
static usbd_interface_t audio_stream_intf2;
|
||||
|
||||
struct device *i2s;
|
||||
struct device *dma_ch2_i2s_tx, *dma_ch3_i2s_rx;
|
||||
|
||||
#define BUFF_SIZE (512)
|
||||
|
||||
volatile static uint8_t record_buff_using_num = 0, record_updata_flag = 0;
|
||||
static uint8_t record_data_buff[2][BUFF_SIZE + 128] __attribute__((section(".system_ram"), aligned(4)));
|
||||
|
||||
volatile static uint8_t play_buff_using_num = 0, play_updata_flag = 0;
|
||||
static uint8_t play_data_buff[2][BUFF_SIZE + 128] __attribute__((section(".system_ram"), aligned(4)));
|
||||
|
||||
static ES8388_Cfg_Type ES8388Cfg = {
|
||||
.work_mode = ES8388_CODEC_MDOE, /*!< ES8388 work mode */
|
||||
.role = ES8388_SLAVE, /*!< ES8388 role */
|
||||
.mic_input_mode = ES8388_DIFF_ENDED_MIC, /*!< ES8388 mic input mode */
|
||||
.mic_pga = ES8388_MIC_PGA_9DB, /*!< ES8388 mic PGA */
|
||||
.i2s_frame = ES8388_LEFT_JUSTIFY_FRAME, /*!< ES8388 I2S frame */
|
||||
.data_width = ES8388_DATA_LEN_16, /*!< ES8388 I2S dataWitdh */
|
||||
};
|
||||
|
||||
static uint32_t usb_data_offset = 0;
|
||||
static uint8_t out_buffer[64];
|
||||
|
||||
void usbd_audio_out_callback(uint8_t ep)
|
||||
{
|
||||
uint32_t actual_read_length = 0;
|
||||
int n;
|
||||
|
||||
if (!play_updata_flag) {
|
||||
n = usbd_ep_read(ep, &play_data_buff[!play_buff_using_num][usb_data_offset], 64, &actual_read_length);
|
||||
} else {
|
||||
n = usbd_ep_read(ep, out_buffer, 64, &actual_read_length);
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
usbd_ep_set_stall(ep);
|
||||
return;
|
||||
} else {
|
||||
if (!play_updata_flag) {
|
||||
usb_data_offset += actual_read_length;
|
||||
/* The data is enough */
|
||||
if (usb_data_offset >= BUFF_SIZE) {
|
||||
play_updata_flag = 1;
|
||||
usb_data_offset = 0;
|
||||
}
|
||||
} else {
|
||||
/* too much data */
|
||||
MSG("wait_flag\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void usbd_audio_set_interface_callback(uint8_t value)
|
||||
{
|
||||
if (value) {
|
||||
play_status = 1;
|
||||
MSG("OPEN\r\n");
|
||||
device_control(usb_fs, DEVICE_CTRL_USB_DC_SET_ACK, (void *)0x81);
|
||||
device_control(usb_fs, DEVICE_CTRL_USB_DC_SET_ACK, (void *)0x2);
|
||||
} else {
|
||||
play_status = 0;
|
||||
usb_data_offset = 0;
|
||||
MSG("CLOSE\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static usbd_endpoint_t audio_out_ep = {
|
||||
.ep_cb = usbd_audio_out_callback,
|
||||
.ep_addr = AUDIO_OUT_EP
|
||||
};
|
||||
|
||||
// static usbd_endpoint_t audio_in_ep = {
|
||||
// .ep_cb = NULL,
|
||||
// .ep_addr = AUDIO_IN_EP
|
||||
// };
|
||||
void usbd_audio_set_volume(uint8_t vol)
|
||||
{
|
||||
ES8388_Set_Voice_Volume(vol);
|
||||
}
|
||||
extern struct device *usb_dc_init(void);
|
||||
struct device *dma_ch4_usb_tx;
|
||||
|
||||
uint32_t audio_pos = 0;
|
||||
uint32_t musci_size = 0;
|
||||
uint32_t frame_count = 0;
|
||||
uint32_t frame_cnt = 0;
|
||||
uint32_t last_frame_size = 0;
|
||||
|
||||
static void dma_ch2_i2s_tx_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
|
||||
{
|
||||
// if (play_updata_flag) {
|
||||
// MSG("data ok\r\n");
|
||||
// play_buff_using_num = !play_buff_using_num;
|
||||
// device_write(i2s, 0, play_data_buff[play_buff_using_num], BUFF_SIZE);
|
||||
// play_updata_flag = 0;
|
||||
// play_wait_data_flag = 0;
|
||||
|
||||
// } else {
|
||||
// play_wait_data_flag = 1;
|
||||
// MSG("data err\r\n");
|
||||
// }
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void dma_ch3_i2s_rx_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
|
||||
{
|
||||
record_updata_flag = 1;
|
||||
|
||||
// record_buff_using_num = !record_buff_using_num;
|
||||
|
||||
// device_read(i2s, 0, record_data_buff[record_buff_using_num], BUFF_SIZE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void audio_init()
|
||||
{
|
||||
/* init ES8388 Codec */
|
||||
ES8388_Init(&ES8388Cfg);
|
||||
//ES8388_Reg_Dump();
|
||||
ES8388_Set_Voice_Volume(60);
|
||||
|
||||
/* register & open i2s device */
|
||||
i2s_register(I2S0_INDEX, "I2S");
|
||||
i2s = device_find("I2S");
|
||||
if (i2s) {
|
||||
I2S_DEV(i2s)->iis_mode = I2S_MODE_MASTER;
|
||||
I2S_DEV(i2s)->interface_mode = I2S_MODE_LEFT;
|
||||
I2S_DEV(i2s)->sampl_freq_hz = 16 * 1000;
|
||||
I2S_DEV(i2s)->channel_num = I2S_FS_CHANNELS_NUM_2;
|
||||
I2S_DEV(i2s)->frame_size = I2S_FRAME_LEN_16;
|
||||
I2S_DEV(i2s)->data_size = I2S_DATA_LEN_16;
|
||||
I2S_DEV(i2s)->fifo_threshold = 3;
|
||||
device_open(i2s, DEVICE_OFLAG_DMA_TX | DEVICE_OFLAG_DMA_RX);
|
||||
}
|
||||
|
||||
/* register & open dma device */
|
||||
dma_register(DMA0_CH2_INDEX, "dma_ch2_i2s_tx");
|
||||
dma_ch2_i2s_tx = device_find("dma_ch2_i2s_tx");
|
||||
if (dma_ch2_i2s_tx) {
|
||||
DMA_DEV(dma_ch2_i2s_tx)->direction = DMA_MEMORY_TO_PERIPH;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->transfer_mode = DMA_LLI_ONCE_MODE;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->src_req = DMA_REQUEST_NONE;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->dst_req = DMA_REQUEST_I2S_TX;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->src_width = DMA_TRANSFER_WIDTH_32BIT;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->dst_width = DMA_TRANSFER_WIDTH_32BIT;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->dst_burst_size = DMA_BURST_4BYTE;
|
||||
DMA_DEV(dma_ch2_i2s_tx)->src_burst_size = DMA_BURST_4BYTE;
|
||||
device_open(dma_ch2_i2s_tx, 0);
|
||||
|
||||
/* connect i2s device and dma device */
|
||||
device_control(i2s, DEVICE_CTRL_ATTACH_TX_DMA, (void *)dma_ch2_i2s_tx);
|
||||
|
||||
/* Set the interrupt function, for double buffering*/
|
||||
device_set_callback(dma_ch2_i2s_tx, dma_ch2_i2s_tx_irq_callback);
|
||||
device_control(dma_ch2_i2s_tx, DEVICE_CTRL_SET_INT, NULL);
|
||||
}
|
||||
|
||||
dma_register(DMA0_CH3_INDEX, "dma_ch3_i2s_rx");
|
||||
dma_ch3_i2s_rx = device_find("dma_ch3_i2s_rx");
|
||||
if (dma_ch3_i2s_rx) {
|
||||
DMA_DEV(dma_ch3_i2s_rx)->direction = DMA_PERIPH_TO_MEMORY;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->transfer_mode = DMA_LLI_ONCE_MODE;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->src_req = DMA_REQUEST_I2S_RX;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->dst_req = DMA_REQUEST_NONE;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->src_width = DMA_TRANSFER_WIDTH_32BIT;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->dst_width = DMA_TRANSFER_WIDTH_32BIT;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->dst_burst_size = DMA_BURST_4BYTE;
|
||||
DMA_DEV(dma_ch3_i2s_rx)->src_burst_size = DMA_BURST_4BYTE;
|
||||
device_open(dma_ch3_i2s_rx, 0);
|
||||
|
||||
/* connect i2s device and dma device */
|
||||
device_control(i2s, DEVICE_CTRL_ATTACH_RX_DMA, (void *)dma_ch3_i2s_rx);
|
||||
|
||||
/* Set the interrupt function, for double buffering*/
|
||||
device_set_callback(dma_ch3_i2s_rx, dma_ch3_i2s_rx_irq_callback);
|
||||
device_control(dma_ch3_i2s_rx, DEVICE_CTRL_SET_INT, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
bflb_platform_init(0);
|
||||
|
||||
audio_init();
|
||||
|
||||
usbd_desc_register(audio_descriptor);
|
||||
usbd_audio_add_interface(&audio_class, &audio_control_intf);
|
||||
usbd_audio_add_interface(&audio_class, &audio_stream_intf);
|
||||
usbd_audio_add_interface(&audio_class, &audio_stream_intf2);
|
||||
usbd_interface_add_endpoint(&audio_stream_intf, &audio_out_ep);
|
||||
|
||||
usb_fs = usb_dc_init();
|
||||
|
||||
if (usb_fs) {
|
||||
device_control(usb_fs, DEVICE_CTRL_SET_INT, (void *)(USB_EP2_DATA_OUT_IT));
|
||||
}
|
||||
dma_register(DMA0_CH4_INDEX, "dma_ch4_usb_tx");
|
||||
dma_ch4_usb_tx = device_find("dma_ch4_usb_tx");
|
||||
|
||||
if (dma_ch4_usb_tx) {
|
||||
DMA_DEV(dma_ch4_usb_tx)->direction = DMA_MEMORY_TO_PERIPH;
|
||||
DMA_DEV(dma_ch4_usb_tx)->transfer_mode = DMA_LLI_ONCE_MODE;
|
||||
DMA_DEV(dma_ch4_usb_tx)->src_req = DMA_REQUEST_NONE;
|
||||
DMA_DEV(dma_ch4_usb_tx)->dst_req = DMA_REQUEST_USB_EP1;
|
||||
DMA_DEV(dma_ch4_usb_tx)->src_width = DMA_TRANSFER_WIDTH_8BIT;
|
||||
DMA_DEV(dma_ch4_usb_tx)->dst_width = DMA_TRANSFER_WIDTH_8BIT;
|
||||
DMA_DEV(dma_ch4_usb_tx)->src_burst_size = DMA_BURST_16BYTE;
|
||||
DMA_DEV(dma_ch4_usb_tx)->dst_burst_size = DMA_BURST_1BYTE;
|
||||
|
||||
device_open(dma_ch4_usb_tx, 0);
|
||||
// device_set_callback(dma_ch4_usb_tx, dma2_irq_callback);
|
||||
// device_control(dma_ch4_usb_tx, DEVICE_CTRL_SET_INT, NULL);
|
||||
device_control(usb_fs, DEVICE_CTRL_ATTACH_TX_DMA, dma_ch4_usb_tx);
|
||||
device_control(usb_fs, DEVICE_CTRL_USB_DC_SET_TX_DMA, (void *)AUDIO_IN_EP);
|
||||
}
|
||||
|
||||
/* i2s record start */
|
||||
device_read(i2s, 0, record_data_buff[0], BUFF_SIZE);
|
||||
|
||||
while (1) {
|
||||
/* Waiting for record data update */
|
||||
if (record_updata_flag) {
|
||||
if (!device_control(dma_ch4_usb_tx, DMA_CHANNEL_GET_STATUS, NULL)) {
|
||||
device_write(usb_fs, AUDIO_IN_EP, record_data_buff[!record_buff_using_num], BUFF_SIZE);
|
||||
record_updata_flag = 0;
|
||||
record_buff_using_num = !record_buff_using_num;
|
||||
device_read(i2s, 0, record_data_buff[record_buff_using_num], BUFF_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
if (play_updata_flag) {
|
||||
device_control(dma_ch2_i2s_tx, DMA_CHANNEL_STOP, NULL);
|
||||
play_buff_using_num = !play_buff_using_num;
|
||||
device_write(i2s, 0, play_data_buff[play_buff_using_num], BUFF_SIZE);
|
||||
play_updata_flag = 0;
|
||||
usb_data_offset = 0;
|
||||
}
|
||||
|
||||
__asm volatile("nop");
|
||||
__asm volatile("nop");
|
||||
}
|
||||
}
|
125002
examples/usb/usb_audio_mic_speaker/music.h
Normal file
125002
examples/usb/usb_audio_mic_speaker/music.h
Normal file
File diff suppressed because it is too large
Load diff
5
examples/usb/usb_audio_mic_speaker/readme.md
Normal file
5
examples/usb/usb_audio_mic_speaker/readme.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
```bash
|
||||
|
||||
$ make APP=usb_audio_mic_speaker BOARD=bl706_iot
|
||||
|
||||
```
|
216
examples/usb/usb_audio_mic_speaker/usb_audio.ld
Normal file
216
examples/usb/usb_audio_mic_speaker/usb_audio.ld
Normal file
|
@ -0,0 +1,216 @@
|
|||
/****************************************************************************************
|
||||
* @file usb_audio.ld
|
||||
*
|
||||
* @brief This file is the map file (gnuarm or armgcc).
|
||||
*
|
||||
* Copyright (C) BouffaloLab 2021
|
||||
*
|
||||
****************************************************************************************
|
||||
*/
|
||||
|
||||
/* configure the CPU type */
|
||||
OUTPUT_ARCH( "riscv" )
|
||||
/* link with the standard c library */
|
||||
/* INPUT(-lc) */
|
||||
/* link with the standard GCC library */
|
||||
/* INPUT(-lgcc) */
|
||||
/* configure the entry point */
|
||||
ENTRY(_enter)
|
||||
|
||||
StackSize = 0x1000; /* 4KB */
|
||||
|
||||
MEMORY
|
||||
{
|
||||
xip_memory (rx) : ORIGIN = 0x23000000, LENGTH = 4096K
|
||||
itcm_memory (rx) : ORIGIN = 0x22014000, LENGTH = 16K
|
||||
dtcm_memory (rx) : ORIGIN = 0x42018000, LENGTH = 32K
|
||||
ram_memory (!rx) : ORIGIN = 0x42020000, LENGTH = 48K
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
PROVIDE(__metal_chicken_bit = 0);
|
||||
|
||||
.text :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__text_code_start__ = .;
|
||||
|
||||
KEEP (*(.text.metal.init.enter))
|
||||
KEEP (*(SORT_NONE(.init)))
|
||||
/* section information for shell */
|
||||
. = ALIGN(4);
|
||||
__fsymtab_start = .;
|
||||
KEEP(*(FSymTab))
|
||||
__fsymtab_end = .;
|
||||
|
||||
. = ALIGN(4);
|
||||
__vsymtab_start = .;
|
||||
KEEP(*(VSymTab))
|
||||
__vsymtab_end = .;
|
||||
|
||||
/* section information for usb desc */
|
||||
. = ALIGN(4);
|
||||
_usb_desc_start = .;
|
||||
KEEP(*(usb_desc))
|
||||
. = ALIGN(4);
|
||||
_usb_desc_end = .;
|
||||
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
|
||||
/*put .rodata**/
|
||||
*(EXCLUDE_FILE( *bl702_glb.o \
|
||||
*bl702_pds.o \
|
||||
*bl702_common.o \
|
||||
*bl702_sf_cfg.o \
|
||||
*bl702_sf_ctrl.o \
|
||||
*bl702_sflash.o \
|
||||
*bl702_xip_sflash.o \
|
||||
*bl702_ef_ctrl.o) .rodata*)
|
||||
*(.rodata)
|
||||
*(.rodata.*)
|
||||
|
||||
*(.srodata)
|
||||
*(.srodata.*)
|
||||
|
||||
. = ALIGN(4);
|
||||
__text_code_end__ = .;
|
||||
} > xip_memory
|
||||
|
||||
. = ALIGN(4);
|
||||
__itcm_load_addr = .;
|
||||
|
||||
.itcm_region : AT (__itcm_load_addr)
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__tcm_code_start__ = .;
|
||||
|
||||
*(.tcm_code)
|
||||
*(.tcm_const)
|
||||
*(.sclock_rlt_code)
|
||||
*(.sclock_rlt_const)
|
||||
|
||||
*bl702_glb.o*(.rodata*)
|
||||
*bl702_pds.o*(.rodata*)
|
||||
*bl702_common.o*(.rodata*)
|
||||
*bl702_sf_cfg.o*(.rodata*)
|
||||
*bl702_sf_ctrl.o*(.rodata*)
|
||||
*bl702_sflash.o*(.rodata*)
|
||||
*bl702_xip_sflash.o*(.rodata*)
|
||||
*bl702_ef_ctrl.o*(.rodata*)
|
||||
|
||||
. = ALIGN(4);
|
||||
__tcm_code_end__ = .;
|
||||
} > itcm_memory
|
||||
|
||||
__dtcm_load_addr = __itcm_load_addr + SIZEOF(.itcm_region);
|
||||
|
||||
.dtcm_region : AT (__dtcm_load_addr)
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__tcm_data_start__ = .;
|
||||
|
||||
*(.tcm_data)
|
||||
/* *finger_print.o(.data*) */
|
||||
|
||||
. = ALIGN(4);
|
||||
__tcm_data_end__ = .;
|
||||
} > dtcm_memory
|
||||
|
||||
/*************************************************************************/
|
||||
/* .stack_dummy section doesn't contains any symbols. It is only
|
||||
* used for linker to calculate size of stack sections, and assign
|
||||
* values to stack symbols later */
|
||||
.stack_dummy (NOLOAD):
|
||||
{
|
||||
. = ALIGN(0x4);
|
||||
. = . + StackSize;
|
||||
. = ALIGN(0x4);
|
||||
} > dtcm_memory
|
||||
|
||||
/* Set stack top to end of RAM, and stack limit move down by
|
||||
* size of stack_dummy section */
|
||||
__StackTop = ORIGIN(dtcm_memory) + LENGTH(dtcm_memory);
|
||||
PROVIDE( __freertos_irq_stack_top = __StackTop);
|
||||
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
|
||||
|
||||
/* Check if data + heap + stack exceeds RAM limit */
|
||||
ASSERT(__StackLimit >= __tcm_data_end__, "region RAM overflowed with stack")
|
||||
/*************************************************************************/
|
||||
|
||||
__system_ram_load_addr = __dtcm_load_addr + SIZEOF(.dtcm_region);
|
||||
|
||||
.system_ram_data_region : AT (__system_ram_load_addr)
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__system_ram_data_start__ = .;
|
||||
|
||||
*(.system_ram)
|
||||
|
||||
. = ALIGN(4);
|
||||
__system_ram_data_end__ = .;
|
||||
} > ram_memory
|
||||
|
||||
__ram_load_addr = __system_ram_load_addr + SIZEOF(.system_ram_data_region);
|
||||
|
||||
/* Data section */
|
||||
RAM_DATA : AT (__ram_load_addr)
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__ram_data_start__ = .;
|
||||
|
||||
PROVIDE( __global_pointer$ = . + 0x800 );
|
||||
|
||||
*(.data)
|
||||
*(.data.*)
|
||||
*(.sdata)
|
||||
*(.sdata.*)
|
||||
*(.sdata2)
|
||||
*(.sdata2.*)
|
||||
|
||||
. = ALIGN(4);
|
||||
__ram_data_end__ = .;
|
||||
} > ram_memory
|
||||
|
||||
.bss (NOLOAD) :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__bss_start__ = .;
|
||||
|
||||
*(.bss*)
|
||||
*(.sbss*)
|
||||
*(COMMON)
|
||||
|
||||
. = ALIGN(4);
|
||||
__bss_end__ = .;
|
||||
} > ram_memory
|
||||
|
||||
.noinit_data (NOLOAD) :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__noinit_data_start__ = .;
|
||||
|
||||
*(.noinit_data*)
|
||||
|
||||
. = ALIGN(4);
|
||||
__noinit_data_end__ = .;
|
||||
} > ram_memory
|
||||
|
||||
.heap (NOLOAD):
|
||||
{
|
||||
. = ALIGN(4);
|
||||
__HeapBase = .;
|
||||
|
||||
KEEP(*(.heap*))
|
||||
|
||||
. = ALIGN(4);
|
||||
__HeapLimit = .;
|
||||
} > ram_memory
|
||||
|
||||
PROVIDE (__heap_min_size = 0x400);
|
||||
__HeapLimit = ORIGIN(ram_memory) + LENGTH(ram_memory);
|
||||
|
||||
ASSERT((__HeapLimit - __HeapBase ) >= __heap_min_size, "heap size is too short.")
|
||||
|
||||
}
|
8
examples/usb/usb_cdc_shell/CMakeLists.txt
Normal file
8
examples/usb/usb_cdc_shell/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
set(BSP_COMMON_DIR ${CMAKE_SOURCE_DIR}/bsp/bsp_common)
|
||||
set(TARGET_REQUIRED_LIBS usb_stack shell)
|
||||
set(TARGET_REQUIRED_PRIVATE_INCLUDE ${BSP_COMMON_DIR}/usb)
|
||||
set(TARGET_REQUIRED_SRCS ${BSP_COMMON_DIR}/usb/usb_dc.c ${BSP_COMMON_DIR}/usb/uart_interface.c)
|
||||
set(mains main.c)
|
||||
generate_bin()
|
||||
|
||||
|
187
examples/usb/usb_cdc_shell/main.c
Normal file
187
examples/usb/usb_cdc_shell/main.c
Normal file
|
@ -0,0 +1,187 @@
|
|||
/**
|
||||
* @file main.c
|
||||
* @brief
|
||||
*
|
||||
* Copyright (c) 2021 Bouffalolab team
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
#include "hal_usb.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_cdc.h"
|
||||
#include "shell.h"
|
||||
|
||||
#define CDC_IN_EP 0x82
|
||||
#define CDC_OUT_EP 0x01
|
||||
#define CDC_INT_EP 0x83
|
||||
|
||||
#define USBD_VID 0xFFFF
|
||||
#define USBD_PID 0xFFFF
|
||||
#define USBD_MAX_POWER 100
|
||||
#define USBD_LANGID_STRING 1033
|
||||
|
||||
#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN)
|
||||
|
||||
USB_DESC_SECTION const uint8_t cdc_descriptor[] = {
|
||||
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x02, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
|
||||
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
|
||||
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, 0x02),
|
||||
///////////////////////////////////////
|
||||
/// string0 descriptor
|
||||
///////////////////////////////////////
|
||||
USB_LANGID_INIT(USBD_LANGID_STRING),
|
||||
///////////////////////////////////////
|
||||
/// string1 descriptor
|
||||
///////////////////////////////////////
|
||||
0x12, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
|
||||
'B', 0x00, /* wcChar0 */
|
||||
'o', 0x00, /* wcChar1 */
|
||||
'u', 0x00, /* wcChar2 */
|
||||
'f', 0x00, /* wcChar3 */
|
||||
'f', 0x00, /* wcChar4 */
|
||||
'a', 0x00, /* wcChar5 */
|
||||
'l', 0x00, /* wcChar6 */
|
||||
'o', 0x00, /* wcChar7 */
|
||||
///////////////////////////////////////
|
||||
/// string2 descriptor
|
||||
///////////////////////////////////////
|
||||
0x24, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
|
||||
'B', 0x00, /* wcChar0 */
|
||||
'o', 0x00, /* wcChar1 */
|
||||
'u', 0x00, /* wcChar2 */
|
||||
'f', 0x00, /* wcChar3 */
|
||||
'f', 0x00, /* wcChar4 */
|
||||
'a', 0x00, /* wcChar5 */
|
||||
'l', 0x00, /* wcChar6 */
|
||||
'o', 0x00, /* wcChar7 */
|
||||
' ', 0x00, /* wcChar8 */
|
||||
'C', 0x00, /* wcChar9 */
|
||||
'D', 0x00, /* wcChar10 */
|
||||
'C', 0x00, /* wcChar11 */
|
||||
' ', 0x00, /* wcChar13 */
|
||||
'D', 0x00, /* wcChar14 */
|
||||
'E', 0x00, /* wcChar15 */
|
||||
'M', 0x00, /* wcChar16 */
|
||||
'O', 0x00, /* wcChar17 */
|
||||
///////////////////////////////////////
|
||||
/// string3 descriptor
|
||||
///////////////////////////////////////
|
||||
0x16, /* bLength */
|
||||
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
|
||||
'2', 0x00, /* wcChar0 */
|
||||
'0', 0x00, /* wcChar1 */
|
||||
'2', 0x00, /* wcChar2 */
|
||||
'1', 0x00, /* wcChar3 */
|
||||
'0', 0x00, /* wcChar4 */
|
||||
'3', 0x00, /* wcChar5 */
|
||||
'1', 0x00, /* wcChar6 */
|
||||
'0', 0x00, /* wcChar7 */
|
||||
'0', 0x00, /* wcChar8 */
|
||||
'0', 0x00, /* wcChar9 */
|
||||
///////////////////////////////////////
|
||||
/// device qualifier descriptor
|
||||
///////////////////////////////////////
|
||||
0x0a,
|
||||
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
|
||||
0x00,
|
||||
0x02,
|
||||
0x02,
|
||||
0x02,
|
||||
0x01,
|
||||
0x40,
|
||||
0x01,
|
||||
0x00,
|
||||
|
||||
0x00
|
||||
};
|
||||
|
||||
void usbd_cdc_acm_bulk_out(uint8_t ep)
|
||||
{
|
||||
uint32_t actual_read_length = 0;
|
||||
uint8_t out_buffer[64];
|
||||
|
||||
if (usbd_ep_read(ep, out_buffer, 64, &actual_read_length) < 0) {
|
||||
MSG("Read DATA Packet failed\r\n");
|
||||
usbd_ep_set_stall(ep);
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < actual_read_length; i++) {
|
||||
shell_handler(out_buffer[i]);
|
||||
}
|
||||
usbd_ep_read(ep, NULL, 0, NULL);
|
||||
}
|
||||
|
||||
void usbd_cdc_acm_bulk_in(uint8_t ep)
|
||||
{
|
||||
}
|
||||
|
||||
void cdc_acm_printf(char *fmt, ...)
|
||||
{
|
||||
char print_buf[64];
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(print_buf, sizeof(print_buf) - 1, fmt, ap);
|
||||
va_end(ap);
|
||||
usbd_ep_write(CDC_IN_EP, (uint8_t *)print_buf, strlen(print_buf), NULL);
|
||||
}
|
||||
|
||||
usbd_class_t cdc_class;
|
||||
usbd_interface_t cdc_cmd_intf;
|
||||
usbd_interface_t cdc_data_intf;
|
||||
|
||||
usbd_endpoint_t cdc_out_ep = {
|
||||
.ep_addr = CDC_OUT_EP,
|
||||
.ep_cb = usbd_cdc_acm_bulk_out
|
||||
};
|
||||
|
||||
usbd_endpoint_t cdc_in_ep = {
|
||||
.ep_addr = CDC_IN_EP,
|
||||
.ep_cb = NULL
|
||||
};
|
||||
|
||||
struct device *usb_fs;
|
||||
|
||||
extern struct device *usb_dc_init(void);
|
||||
int main(void)
|
||||
{
|
||||
bflb_platform_init(0);
|
||||
|
||||
shell_init();
|
||||
shell_set_print(cdc_acm_printf);
|
||||
|
||||
usbd_desc_register(cdc_descriptor);
|
||||
|
||||
usbd_cdc_add_acm_interface(&cdc_class, &cdc_cmd_intf);
|
||||
usbd_cdc_add_acm_interface(&cdc_class, &cdc_data_intf);
|
||||
usbd_interface_add_endpoint(&cdc_data_intf, &cdc_out_ep);
|
||||
usbd_interface_add_endpoint(&cdc_data_intf, &cdc_in_ep);
|
||||
|
||||
usb_fs = usb_dc_init();
|
||||
|
||||
if (usb_fs) {
|
||||
device_control(usb_fs, DEVICE_CTRL_SET_INT, (void *)(USB_EP1_DATA_OUT_IT));
|
||||
}
|
||||
|
||||
while (!usb_device_is_configured()) {
|
||||
}
|
||||
|
||||
while (1) {
|
||||
}
|
||||
}
|
5
examples/usb/usb_cdc_shell/readme.md
Normal file
5
examples/usb/usb_cdc_shell/readme.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
```bash
|
||||
|
||||
$ make APP=usb_cdc_shell BOARD=bl706_iot
|
||||
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue