1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#include <FreeRTOS.h>
#include "semphr.h"
#include "usbh_core.h"
#include "usbh_hid.h"
#include "usbh_serial.h"
#include "bflb_uart.h"
#include "board.h"
#include "shell.h"
#ifdef CONFIG_USB_EHCI_ISO
#include "usbh_uvc_stream.h"
#include "usbh_uac_stream.h"
#endif
#include "lwip/tcpip.h"
static struct bflb_device_s *uart0;
extern void shell_init_with_task(struct bflb_device_s *shell);
#ifdef CONFIG_USB_EHCI_ISO
static ATTR_NOINIT_PSRAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer1[640 * 480 * 2];
static ATTR_NOINIT_PSRAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer2[640 * 480 * 2];
static struct usbh_videoframe frame_pool[2];
#define AUDIP_MIC_POOL_SIZE (10)
static ATTR_NOINIT_PSRAM_SECTION USB_MEM_ALIGNX uint8_t frame_mic_buffer[AUDIO_MIC_EP_MAX_MPS * AUDIO_MIC_ISO_PACKETS * AUDIP_MIC_POOL_SIZE];
static struct usbh_audioframe frame_mic_pool[AUDIP_MIC_POOL_SIZE];
void usbh_video_run(struct usbh_video *video_class)
{
usbh_video_stream_start(640, 480, USBH_VIDEO_FORMAT_MJPEG);
}
void usbh_video_stop(struct usbh_video *video_class)
{
usbh_video_stream_stop();
}
static void usbh_video_frame_thread(void *argument)
{
int ret;
struct usbh_videoframe *frame;
while (1) {
ret = usbh_video_stream_dequeue(&frame, 0xfffffff);
if (ret < 0) {
continue;
}
USB_LOG_RAW("frame buf:%p,frame len:%d\r\n", frame->frame_buf, frame->frame_size);
usbh_video_stream_enqueue(frame);
}
}
static void usbh_audio_mic_frame_thread(void *argument)
{
int ret;
struct usbh_audioframe *frame;
while (1) {
ret = usbh_audio_mic_stream_dequeue(&frame, 0xfffffff);
if (ret < 0) {
continue;
}
USB_LOG_RAW("frame buf:%p,frame len:%d\r\n", frame->frame_buf, frame->frame_size);
usbh_audio_mic_stream_enqueue(frame);
}
}
#endif
int main(void)
{
board_init();
uart0 = bflb_device_get_by_name("uart0");
shell_init_with_task(uart0);
/* Initialize the LwIP stack */
tcpip_init(NULL, NULL);
printf("Starting usb host task...\r\n");
#ifdef CONFIG_USB_EHCI_ISO
frame_pool[0].frame_buf = frame_buffer1;
frame_pool[0].frame_bufsize = 640 * 480 * 2;
frame_pool[1].frame_buf = frame_buffer2;
frame_pool[1].frame_bufsize = 640 * 480 * 2;
usbh_video_stream_create(frame_pool, 2);
usb_osal_thread_create("uvc_frame", 3072, 5, usbh_video_frame_thread, NULL);
extern void usbh_video_fps_init(void);
usbh_video_fps_init();
for (uint8_t i = 0; i < AUDIP_MIC_POOL_SIZE; i++) {
frame_mic_pool[i].frame_buf = frame_mic_buffer + i * AUDIO_MIC_EP_MAX_MPS * AUDIO_MIC_ISO_PACKETS;
frame_mic_pool[i].frame_bufsize = AUDIO_MIC_EP_MAX_MPS * AUDIO_MIC_ISO_PACKETS;
}
usbh_audio_mic_stream_create(frame_mic_pool, AUDIP_MIC_POOL_SIZE);
usb_osal_thread_create("uac_mic", 3072, 5, usbh_audio_mic_frame_thread, NULL);
#endif
vTaskStartScheduler();
while (1) {
}
}
SHELL_CMD_EXPORT_ALIAS(lsusb, lsusb, ls usb);
SHELL_CMD_EXPORT_ALIAS(lshid, lshid, ls hid);
SHELL_CMD_EXPORT_ALIAS(usbh_serial, usbh_serial, usbh_serial);
int usbh_deinit(int argc, char **argv)
{
printf("usbh_deinit\r\n");
usbh_deinitialize(0);
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_deinit, usbh_deinit, usbh deinit);
int usbh_init(int argc, char **argv)
{
printf("usbh_init\r\n");
usbh_initialize(0, 0x20072000, NULL);
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_init, usbh_init, usbh init);
// int uvcinit(int argc, char **argv)
// {
// video_init(0, 0x20072000);
// return 0;
// }
// SHELL_CMD_EXPORT_ALIAS(uvcinit, uvcinit, usbh init);
// int uvcsend(int argc, char **argv)
// {
// extern void video_test(uint8_t busid);
// video_test(0);
// return 0;
// }
// SHELL_CMD_EXPORT_ALIAS(uvcsend, uvcsend, usbh init);
#ifdef CONFIG_USB_EHCI_ISO
int usbh_uvc_start(int argc, char **argv)
{
uint8_t type;
if (argc < 2) {
USB_LOG_ERR("please input correct command: usbh_uvc_start type\r\n");
USB_LOG_ERR("type 0:yuyv, type 1:mjpeg\r\n");
return -1;
}
type = atoi(argv[1]);
usbh_video_stream_start(640, 480, type);
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_uvc_start, usbh_uvc_start, usbh_uvc_start);
int usbh_uvc_stop(int argc, char **argv)
{
usbh_video_stream_stop();
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_uvc_stop, usbh_uvc_stop, usbh_uvc_stop);
int usbh_uac_start(int argc, char **argv)
{
uint32_t freq;
if (argc < 2) {
USB_LOG_ERR("please input correct command: usbh_uac_start freq\r\n");
return -1;
}
freq = atoi(argv[1]);
usbh_audio_mic_stream_start(freq);
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_uac_start, usbh_uac_start, usbh_uac_start);
int usbh_uac_stop(int argc, char **argv)
{
usbh_audio_mic_stream_stop();
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_uac_stop, usbh_uac_stop, usbh_uac_stop);
int usbh_uac_volume(int argc, char **argv)
{
struct usbh_audio *audio_class;
if (argc < 3) {
USB_LOG_ERR("please input correct command: usbh_uac_volume volume is_tx\r\n");
return -1;
}
audio_class = (struct usbh_audio *)usbh_find_class_instance("/dev/audio0");
usbh_audio_set_mute(audio_class, false, false);
int ret = usbh_audio_set_volume(audio_class, atoi(argv[1]), atoi(argv[2]));
if (ret < 0) {
USB_LOG_ERR("set volume failed, ret: %d\r\n", ret);
}
return 0;
}
SHELL_CMD_EXPORT_ALIAS(usbh_uac_volume, usbh_uac_volume, usbh_uac_volume);
#endif
|