25
25
#include <zephyr/net_buf.h>
26
26
#include <zephyr/shell/shell.h>
27
27
#include <zephyr/sys/__assert.h>
28
+ #include <zephyr/sys/clock.h>
28
29
#include <zephyr/sys/ring_buffer.h>
29
30
#include <zephyr/sys/util.h>
30
31
#include <zephyr/sys/util_macro.h>
@@ -51,8 +52,6 @@ LOG_MODULE_REGISTER(bap_usb, CONFIG_BT_BAP_STREAM_LOG_LEVEL);
51
52
#define USB_MONO_FRAME_SIZE (USB_SAMPLE_CNT * USB_BYTES_PER_SAMPLE)
52
53
#define USB_CHANNELS 2U
53
54
#define USB_STEREO_FRAME_SIZE (USB_MONO_FRAME_SIZE * USB_CHANNELS)
54
- #define USB_OUT_RING_BUF_SIZE (CONFIG_BT_ISO_RX_BUF_COUNT * LC3_MAX_NUM_SAMPLES_STEREO)
55
- #define USB_IN_RING_BUF_SIZE (USB_MONO_FRAME_SIZE * USB_ENQUEUE_COUNT)
56
55
57
56
#define IN_TERMINAL_ID UAC2_ENTITY_ID(DT_NODELABEL(in_terminal))
58
57
#define OUT_TERMINAL_ID UAC2_ENTITY_ID(DT_NODELABEL(out_terminal))
@@ -85,6 +84,8 @@ static void usb_sof_cb(const struct device *dev, void *user_data)
85
84
}
86
85
87
86
#if defined CONFIG_BT_AUDIO_RX
87
+ #define USB_IN_RING_BUF_SIZE (CONFIG_BT_ISO_RX_BUF_COUNT * LC3_MAX_NUM_SAMPLES_STEREO)
88
+
88
89
struct decoded_sdu {
89
90
int16_t right_frames [MAX_CODEC_FRAMES_PER_SDU ][LC3_MAX_NUM_SAMPLES_MONO ];
90
91
int16_t left_frames [MAX_CODEC_FRAMES_PER_SDU ][LC3_MAX_NUM_SAMPLES_MONO ];
@@ -94,8 +95,8 @@ struct decoded_sdu {
94
95
uint32_t ts ;
95
96
} decoded_sdu ;
96
97
97
- RING_BUF_DECLARE (usb_out_ring_buf , USB_OUT_RING_BUF_SIZE );
98
- K_MEM_SLAB_DEFINE_STATIC (usb_out_buf_pool , ROUND_UP (USB_STEREO_FRAME_SIZE , UDC_BUF_GRANULARITY ),
98
+ RING_BUF_DECLARE (usb_in_ring_buf , USB_IN_RING_BUF_SIZE );
99
+ K_MEM_SLAB_DEFINE_STATIC (usb_in_buf_pool , ROUND_UP (USB_STEREO_FRAME_SIZE , UDC_BUF_GRANULARITY ),
99
100
USB_ENQUEUE_COUNT , UDC_BUF_ALIGN );
100
101
101
102
/* USB consumer callback, called every 1ms, consumes data from ring-buffer */
@@ -110,14 +111,14 @@ static void usb_data_request(const struct device *dev)
110
111
return ;
111
112
}
112
113
113
- err = k_mem_slab_alloc (& usb_out_buf_pool , & pcm_buf , K_NO_WAIT );
114
+ err = k_mem_slab_alloc (& usb_in_buf_pool , & pcm_buf , K_NO_WAIT );
114
115
if (err != 0 ) {
115
116
LOG_WRN ("Could not allocate pcm_buf: %d" , err );
116
117
return ;
117
118
}
118
119
119
120
/* This may fail without causing issues since usb_audio_data is 0-initialized */
120
- size = ring_buf_get (& usb_out_ring_buf , pcm_buf , USB_STEREO_FRAME_SIZE );
121
+ size = ring_buf_get (& usb_in_ring_buf , pcm_buf , USB_STEREO_FRAME_SIZE );
121
122
if (size != USB_STEREO_FRAME_SIZE ) {
122
123
/* If we could not fill the buffer, zero-fill the rest (possibly all) */
123
124
memset (((uint8_t * )pcm_buf ) + size , 0 , USB_STEREO_FRAME_SIZE - size );
@@ -146,14 +147,14 @@ static void usb_data_request(const struct device *dev)
146
147
LOG_ERR ("Failed to send USB audio: %d (%zu)" , err , cnt );
147
148
}
148
149
149
- k_mem_slab_free (& usb_out_buf_pool , pcm_buf );
150
+ k_mem_slab_free (& usb_in_buf_pool , pcm_buf );
150
151
}
151
152
}
152
153
153
154
static void usb_buf_release_cb (const struct device * dev , uint8_t terminal , void * buf ,
154
155
void * user_data )
155
156
{
156
- k_mem_slab_free (& usb_out_buf_pool , buf );
157
+ k_mem_slab_free (& usb_in_buf_pool , buf );
157
158
}
158
159
159
160
static void bap_usb_send_frames_to_usb (void )
@@ -180,7 +181,7 @@ static void bap_usb_send_frames_to_usb(void)
180
181
uint32_t rb_size ;
181
182
182
183
/* Not enough space to store data */
183
- if (ring_buf_space_get (& usb_out_ring_buf ) < sizeof (stereo_frame )) {
184
+ if (ring_buf_space_get (& usb_in_ring_buf ) < sizeof (stereo_frame )) {
184
185
if ((fail_cnt % bap_get_stats_interval ()) == 0U ) {
185
186
LOG_WRN ("[%zu] Could not send more than %zu frames to USB" ,
186
187
fail_cnt , i );
@@ -218,7 +219,7 @@ static void bap_usb_send_frames_to_usb(void)
218
219
}
219
220
}
220
221
221
- rb_size = ring_buf_put (& usb_out_ring_buf , (uint8_t * )stereo_frame ,
222
+ rb_size = ring_buf_put (& usb_in_ring_buf , (uint8_t * )stereo_frame ,
222
223
sizeof (stereo_frame ));
223
224
if (rb_size != sizeof (stereo_frame )) {
224
225
LOG_WRN ("Failed to put frame on USB ring buf" );
@@ -356,14 +357,15 @@ void bap_usb_clear_frames_to_usb(void)
356
357
#endif /* CONFIG_BT_AUDIO_RX */
357
358
358
359
#if defined(CONFIG_BT_AUDIO_TX )
360
+ #define USB_OUT_RING_BUF_SIZE (USB_MONO_FRAME_SIZE * USB_ENQUEUE_COUNT)
361
+
359
362
/* Allocate 3: 1 for USB to receive data to and 2 additional buffers to prevent out of memory
360
363
* errors when USB host decides to perform rapid terminal enable/disable cycles.
361
364
*/
362
- K_MEM_SLAB_DEFINE_STATIC (usb_in_buf_pool , USB_STEREO_FRAME_SIZE , 3 , UDC_BUF_ALIGN );
365
+ K_MEM_SLAB_DEFINE_STATIC (usb_out_buf_pool , USB_STEREO_FRAME_SIZE , 3 , UDC_BUF_ALIGN );
363
366
364
- BUILD_ASSERT ((USB_IN_RING_BUF_SIZE % USB_MONO_FRAME_SIZE ) == 0 );
365
- static int16_t usb_in_left_ring_buffer [USB_IN_RING_BUF_SIZE ];
366
- static int16_t usb_in_right_ring_buffer [USB_IN_RING_BUF_SIZE ];
367
+ static int16_t usb_out_left_ring_buffer [USB_OUT_RING_BUF_SIZE ];
368
+ static int16_t usb_out_right_ring_buffer [USB_OUT_RING_BUF_SIZE ];
367
369
static size_t write_index ; /* Points to the oldest/uninitialized data */
368
370
369
371
size_t bap_usb_get_read_cnt (const struct shell_stream * sh_stream )
@@ -425,7 +427,7 @@ static void *usb_get_recv_buf_cb(const struct device *dev, uint8_t terminal, uin
425
427
426
428
__ASSERT (size <= USB_STEREO_FRAME_SIZE , "%u was not <= %d" , size , USB_STEREO_FRAME_SIZE );
427
429
428
- ret = k_mem_slab_alloc (& usb_in_buf_pool , & buf , K_NO_WAIT );
430
+ ret = k_mem_slab_alloc (& usb_out_buf_pool , & buf , K_NO_WAIT );
429
431
if (ret != 0 ) {
430
432
LOG_WRN ("Failed to allocate buffer: %d" , ret );
431
433
}
@@ -441,7 +443,7 @@ static void usb_data_recv_cb(const struct device *dev, uint8_t terminal, void *b
441
443
int16_t * pcm ;
442
444
443
445
if (!out_terminal_enabled || buf == NULL || size == 0U ) {
444
- k_mem_slab_free (& usb_in_buf_pool , buf );
446
+ k_mem_slab_free (& usb_out_buf_pool , buf );
445
447
return ;
446
448
}
447
449
@@ -454,13 +456,13 @@ static void usb_data_recv_cb(const struct device *dev, uint8_t terminal, void *b
454
456
* can be done once afterwards
455
457
*/
456
458
for (size_t i = 0U , j = 0U ; i < USB_SAMPLE_CNT ; i ++ , j += USB_CHANNELS ) {
457
- usb_in_left_ring_buffer [write_index + i ] = pcm [j ];
458
- usb_in_right_ring_buffer [write_index + i ] = pcm [j + 1 ];
459
+ usb_out_left_ring_buffer [write_index + i ] = pcm [j ];
460
+ usb_out_right_ring_buffer [write_index + i ] = pcm [j + 1 ];
459
461
}
460
462
461
463
write_index += USB_SAMPLE_CNT ;
462
464
463
- if (write_index == USB_IN_RING_BUF_SIZE ) {
465
+ if (write_index == USB_OUT_RING_BUF_SIZE ) {
464
466
/* Overflow so that we start overwriting oldest */
465
467
write_index = 0U ;
466
468
}
@@ -474,7 +476,7 @@ static void usb_data_recv_cb(const struct device *dev, uint8_t terminal, void *b
474
476
LOG_DBG ("USB Data received (count = %d)" , cnt );
475
477
}
476
478
477
- k_mem_slab_free (& usb_in_buf_pool , buf );
479
+ k_mem_slab_free (& usb_out_buf_pool , buf );
478
480
}
479
481
480
482
bool bap_usb_can_get_full_sdu (struct shell_stream * sh_stream )
@@ -505,14 +507,14 @@ bool bap_usb_can_get_full_sdu(struct shell_stream *sh_stream)
505
507
buffer_cnt = write_index - read_idx ;
506
508
} else {
507
509
/* Handle the case where the read spans across the end of the buffer */
508
- buffer_cnt = write_index + (USB_IN_RING_BUF_SIZE - read_idx );
510
+ buffer_cnt = write_index + (USB_OUT_RING_BUF_SIZE - read_idx );
509
511
}
510
512
511
513
if (buffer_cnt < retrieve_cnt ) {
512
514
/* Not enough for a frame yet */
513
515
if (!failed_last_time ) {
514
516
LOG_WRN ("Ring buffer (%u/%u) does not contain enough for an entire SDU %u" ,
515
- buffer_cnt , USB_IN_RING_BUF_SIZE , retrieve_cnt );
517
+ buffer_cnt , USB_OUT_RING_BUF_SIZE , retrieve_cnt );
516
518
}
517
519
518
520
failed_last_time = true;
@@ -536,19 +538,19 @@ static size_t usb_ring_buf_get(int16_t dest[], int16_t src[], size_t idx, size_t
536
538
{
537
539
size_t new_idx ;
538
540
539
- if (idx >= USB_IN_RING_BUF_SIZE ) {
541
+ if (idx >= USB_OUT_RING_BUF_SIZE ) {
540
542
LOG_ERR ("Invalid idx %zu" , idx );
541
543
542
544
return 0 ;
543
545
}
544
546
545
- if ((idx + cnt ) < USB_IN_RING_BUF_SIZE ) {
547
+ if ((idx + cnt ) < USB_OUT_RING_BUF_SIZE ) {
546
548
/* Simply copy of the data and increment the index*/
547
549
memcpy (dest , & src [idx ], cnt * USB_BYTES_PER_SAMPLE );
548
550
new_idx = idx + cnt ;
549
551
} else {
550
552
/* Handle wrapping */
551
- const size_t first_read_cnt = USB_IN_RING_BUF_SIZE - idx ;
553
+ const size_t first_read_cnt = USB_OUT_RING_BUF_SIZE - idx ;
552
554
const size_t second_read_cnt = cnt - first_read_cnt ;
553
555
554
556
memcpy (dest , & src [idx ], first_read_cnt * USB_BYTES_PER_SAMPLE );
@@ -570,10 +572,10 @@ void bap_usb_get_frame(struct shell_stream *sh_stream, enum bt_audio_location ch
570
572
571
573
if (is_left || is_mono ) {
572
574
sh_stream -> tx .left_read_idx = usb_ring_buf_get (
573
- buffer , usb_in_left_ring_buffer , sh_stream -> tx .left_read_idx , read_cnt );
575
+ buffer , usb_out_left_ring_buffer , sh_stream -> tx .left_read_idx , read_cnt );
574
576
} else if (is_right ) {
575
577
sh_stream -> tx .right_read_idx = usb_ring_buf_get (
576
- buffer , usb_in_right_ring_buffer , sh_stream -> tx .right_read_idx , read_cnt );
578
+ buffer , usb_out_right_ring_buffer , sh_stream -> tx .right_read_idx , read_cnt );
577
579
}
578
580
}
579
581
#endif /* CONFIG_BT_AUDIO_TX */
0 commit comments