Skip to content

Commit d75ca19

Browse files
committed
samples: update to new ring_buffer api
samples to use new ring_buffer api. Signed-off-by: Måns Ansgariusson <[email protected]>
1 parent d60ba07 commit d75ca19

File tree

10 files changed

+36
-45
lines changed

10 files changed

+36
-45
lines changed

samples/bluetooth/bap_broadcast_sink/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ config USE_USB_AUDIO_OUTPUT
5858
depends on ENABLE_LC3
5959
select USB_DEVICE_STACK_NEXT
6060
select USBD_AUDIO2_CLASS
61-
select RING_BUFFER
6261
help
6362
Enables USB audio as output as a USB peripheral. This does not support providing USB
6463
audio to e.g. speakers that are also USB peripherals, but can be connected to e.g. a

samples/bluetooth/bap_broadcast_sink/src/usb.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct decoded_sdu {
5959
uint32_t ts;
6060
} decoded_sdu;
6161

62-
RING_BUF_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE);
62+
RING_BUFFER_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE);
6363
K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY),
6464
USB_ENQUEUE_COUNT, UDC_BUF_ALIGN);
6565
static volatile bool terminal_enabled;
@@ -73,7 +73,7 @@ static void uac2_sof_cb(const struct device *dev, void *user_data)
7373

7474
if (!terminal_enabled) {
7575
/* Simply discard the data then */
76-
(void)ring_buf_get(&usb_in_ring_buf, NULL, USB_STEREO_FRAME_SIZE);
76+
(void)ring_buffer_read(&usb_in_ring_buf, NULL, USB_STEREO_FRAME_SIZE);
7777
return;
7878
}
7979

@@ -83,7 +83,7 @@ static void uac2_sof_cb(const struct device *dev, void *user_data)
8383
return;
8484
}
8585

86-
size = ring_buf_get(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE);
86+
size = ring_buffer_read(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE);
8787
if (size != USB_STEREO_FRAME_SIZE) {
8888
/* If we could not fill the buffer, zero-fill the rest (possibly all) */
8989
memset(((uint8_t *)pcm_buf) + size, 0, USB_STEREO_FRAME_SIZE - size);
@@ -155,7 +155,7 @@ static void usb_send_frames_to_usb(void)
155155
uint32_t rb_size;
156156

157157
/* Not enough space to store data */
158-
if (ring_buf_space_get(&usb_in_ring_buf) < sizeof(stereo_frame)) {
158+
if (ring_buffer_space(&usb_in_ring_buf) < sizeof(stereo_frame)) {
159159
if (CONFIG_INFO_REPORTING_INTERVAL > 0 &&
160160
(fail_cnt % CONFIG_INFO_REPORTING_INTERVAL) == 0U) {
161161
LOG_WRN("[%zu] Could not send more than %zu frames to USB",
@@ -194,7 +194,7 @@ static void usb_send_frames_to_usb(void)
194194
}
195195
}
196196

197-
rb_size = ring_buf_put(&usb_in_ring_buf, (uint8_t *)stereo_frame,
197+
rb_size = ring_buffer_write(&usb_in_ring_buf, (uint8_t *)stereo_frame,
198198
sizeof(stereo_frame));
199199
if (rb_size != sizeof(stereo_frame)) {
200200
LOG_WRN("Failed to put frame on USB ring buf");

samples/bluetooth/bap_broadcast_source/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ config USE_USB_AUDIO_INPUT
4141
depends on ENABLE_LC3
4242
select USB_DEVICE_STACK_NEXT
4343
select USBD_AUDIO2_CLASS
44-
select RING_BUFFER
4544

4645
config BROADCAST_CODE
4746
string "The broadcast code (if any) to use for encrypted broadcast"

samples/bluetooth/bap_broadcast_source/src/main.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static struct broadcast_source_stream {
142142
lc3_encoder_mem_48k_t lc3_encoder_mem;
143143
#endif
144144
#if defined(CONFIG_USE_USB_AUDIO_INPUT)
145-
struct ring_buf audio_ring_buf;
145+
struct ring_buffer audio_ring_buf;
146146
uint8_t _ring_buffer_memory[AUDIO_RING_BUF_BYTES];
147147
#endif /* defined(CONFIG_USE_USB_AUDIO_INPUT) */
148148
#endif /* defined(CONFIG_LIBLC3) */
@@ -197,7 +197,7 @@ static void send_data(struct broadcast_source_stream *source_stream)
197197
}
198198

199199
#if defined(CONFIG_USE_USB_AUDIO_INPUT)
200-
uint32_t size = ring_buf_get(&source_stream->audio_ring_buf, (uint8_t *)send_pcm_data,
200+
uint32_t size = ring_buffer_read(&source_stream->audio_ring_buf, (uint8_t *)send_pcm_data,
201201
sizeof(send_pcm_data));
202202

203203
if (size < sizeof(send_pcm_data)) {
@@ -373,13 +373,14 @@ static void data_recv_cb(const struct device *dev, uint8_t terminal, void *buf,
373373

374374
for (size_t i = 0U; i < MIN(ARRAY_SIZE(streams), 2); i++) {
375375
const uint32_t size_put =
376-
ring_buf_put(&(streams[i].audio_ring_buf), (uint8_t *)(usb_pcm_data[i]),
377-
nsamples * USB_BYTES_PER_SAMPLE);
376+
ring_buffer_write(&(streams[i].audio_ring_buf),
377+
(uint8_t *)(usb_pcm_data[i]),
378+
nsamples * USB_BYTES_PER_SAMPLE);
378379
if (size_put < nsamples * USB_BYTES_PER_SAMPLE) {
379380
printk("Not enough room for samples in %s buffer: %u < %u, total capacity: "
380381
"%u\n",
381382
i == 0 ? "left" : "right", size_put, nsamples * USB_BYTES_PER_SAMPLE,
382-
ring_buf_capacity_get(&(streams[i].audio_ring_buf)));
383+
ring_buffer_capacity(&(streams[i].audio_ring_buf)));
383384
}
384385
}
385386

@@ -540,10 +541,11 @@ int main(void)
540541
(void)memset(streams, 0, sizeof(streams));
541542

542543
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
543-
ring_buf_init(&(streams[i].audio_ring_buf), sizeof(streams[i]._ring_buffer_memory),
544-
streams[i]._ring_buffer_memory);
544+
ring_buffer_init(&(streams[i].audio_ring_buf),
545+
streams[i]._ring_buffer_memory,
546+
sizeof(streams[i]._ring_buffer_memory));
545547
printk("Initialized ring buf %zu: capacity: %u\n", i,
546-
ring_buf_capacity_get(&(streams[i].audio_ring_buf)));
548+
ring_buffer_capacity(&(streams[i].audio_ring_buf)));
547549
}
548550

549551
usbd_uac2_set_ops(broadcaster_dev, &usb_audio_ops, NULL);
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
CONFIG_SERIAL=y
22
CONFIG_UART_ASYNC_API=y
3-
CONFIG_RING_BUFFER=y

samples/boards/st/uart/circular_dma/src/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const struct uart_config uart_cfg = {.baudrate = 115200,
3131
.flow_ctrl = UART_CFG_FLOW_CTRL_NONE};
3232

3333
/* define a ring buffer to get raw bytes*/
34-
RING_BUF_DECLARE(ring_buf, RING_BUF_SIZE);
34+
RING_BUFFER_DECLARE(ring_buf, RING_BUF_SIZE);
3535

3636
/* define uart rx buffer */
3737
static uint8_t rx_buffer[RX_BUF_SIZE];
@@ -63,7 +63,7 @@ static void uart_rx_thread(void *p1, void *p2, void *p3)
6363
while (1) {
6464

6565
/* Check if there's data in the ring buffer */
66-
len = ring_buf_get(&ring_buf, rx_data, sizeof(rx_data));
66+
len = ring_buffer_read(&ring_buf, rx_data, sizeof(rx_data));
6767

6868
if (len > 0) {
6969

@@ -78,7 +78,8 @@ void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
7878
switch (evt->type) {
7979
case UART_RX_RDY:
8080
/* Data received; place into ring buffer */
81-
ring_buf_put(&ring_buf, evt->data.rx.buf + evt->data.rx.offset, evt->data.rx.len);
81+
ring_buffer_write(&ring_buf, evt->data.rx.buf + evt->data.rx.offset,
82+
evt->data.rx.len);
8283

8384
break;
8485

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
CONFIG_SERIAL=y
2-
CONFIG_RING_BUFFER=y
32
CONFIG_UART_INTERRUPT_DRIVEN=y

samples/drivers/uart/passthrough/src/main.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct patch_info {
1616
const uint8_t * const name;
1717

1818
const struct device *rx_dev;
19-
struct ring_buf *rx_ring_buf;
19+
struct ring_buffer *rx_ring_buf;
2020
bool rx_error;
2121
bool rx_overflow;
2222

@@ -28,7 +28,7 @@ struct patch_info {
2828

2929
#define RING_BUF_SIZE 64
3030

31-
RING_BUF_DECLARE(rb_console, RING_BUF_SIZE);
31+
RING_BUFFER_DECLARE(rb_console, RING_BUF_SIZE);
3232
struct patch_info patch_c2o = {
3333
.name = "c2o",
3434

@@ -40,7 +40,7 @@ struct patch_info patch_c2o = {
4040
.tx_dev = DEV_OTHER,
4141
};
4242

43-
RING_BUF_DECLARE(rb_other, RING_BUF_SIZE);
43+
RING_BUFFER_DECLARE(rb_other, RING_BUF_SIZE);
4444
struct patch_info patch_o2c = {
4545
.name = "o2c",
4646

@@ -68,7 +68,7 @@ static void uart_cb(const struct device *dev, void *ctx)
6868
break;
6969
}
7070

71-
len = ring_buf_put_claim(patch->rx_ring_buf, &buf, RING_BUF_SIZE);
71+
len = ring_buffer_write_ptr(patch->rx_ring_buf, &buf);
7272
if (len == 0) {
7373
/* no space for Rx, disable the IRQ */
7474
uart_irq_rx_disable(patch->rx_dev);
@@ -84,12 +84,7 @@ static void uart_cb(const struct device *dev, void *ctx)
8484
break;
8585
}
8686
len = ret;
87-
88-
ret = ring_buf_put_finish(patch->rx_ring_buf, len);
89-
if (ret != 0) {
90-
patch->rx_error = true;
91-
break;
92-
}
87+
ring_buffer_commit(patch->rx_ring_buf, len);
9388
}
9489
}
9590

@@ -109,7 +104,7 @@ static void passthrough(struct patch_info *patch)
109104
patch->rx_overflow = false;
110105
}
111106

112-
len = ring_buf_get_claim(patch->rx_ring_buf, &buf, RING_BUF_SIZE);
107+
len = ring_buffer_read_ptr(patch->rx_ring_buf, &buf);
113108
if (len == 0) {
114109
goto done;
115110
}
@@ -120,10 +115,7 @@ static void passthrough(struct patch_info *patch)
120115
}
121116
len = ret;
122117

123-
ret = ring_buf_get_finish(patch->rx_ring_buf, len);
124-
if (ret < 0) {
125-
goto error;
126-
}
118+
ring_buffer_consume(patch->rx_ring_buf, len);
127119

128120
done:
129121
uart_irq_rx_enable(patch->rx_dev);

samples/subsys/usb/cdc_acm/src/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const struct device *const uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
2222
#define RING_BUF_SIZE 1024
2323
uint8_t ring_buffer[RING_BUF_SIZE];
2424

25-
struct ring_buf ringbuf;
25+
struct ring_buffer ringbuf;
2626

2727
static bool rx_throttled;
2828

@@ -105,7 +105,7 @@ static void interrupt_handler(const struct device *dev, void *user_data)
105105
if (!rx_throttled && uart_irq_rx_ready(dev)) {
106106
int recv_len, rb_len;
107107
uint8_t buffer[64];
108-
size_t len = MIN(ring_buf_space_get(&ringbuf),
108+
size_t len = MIN(ring_buffer_space(&ringbuf),
109109
sizeof(buffer));
110110

111111
if (len == 0) {
@@ -121,7 +121,7 @@ static void interrupt_handler(const struct device *dev, void *user_data)
121121
recv_len = 0;
122122
};
123123

124-
rb_len = ring_buf_put(&ringbuf, buffer, recv_len);
124+
rb_len = ring_buffer_write(&ringbuf, buffer, recv_len);
125125
if (rb_len < recv_len) {
126126
LOG_ERR("Drop %u bytes", recv_len - rb_len);
127127
}
@@ -136,7 +136,7 @@ static void interrupt_handler(const struct device *dev, void *user_data)
136136
uint8_t buffer[64];
137137
int rb_len, send_len;
138138

139-
rb_len = ring_buf_get(&ringbuf, buffer, sizeof(buffer));
139+
rb_len = ring_buffer_read(&ringbuf, buffer, sizeof(buffer));
140140
if (!rb_len) {
141141
LOG_DBG("Ring buffer empty, disable TX IRQ");
142142
uart_irq_tx_disable(dev);
@@ -173,7 +173,7 @@ int main(void)
173173
return 0;
174174
}
175175

176-
ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer);
176+
ring_buffer_init(&ringbuf, ring_buffer, sizeof(ring_buffer));
177177

178178
LOG_INF("Wait for DTR");
179179
k_sem_take(&dtr_sem, K_FOREVER);

samples/subsys/usb/legacy/cdc_acm/src/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const struct device *const uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
2020
#define RING_BUF_SIZE 1024
2121
uint8_t ring_buffer[RING_BUF_SIZE];
2222

23-
struct ring_buf ringbuf;
23+
struct ring_buffer ringbuf;
2424

2525
static bool rx_throttled;
2626

@@ -45,7 +45,7 @@ static void interrupt_handler(const struct device *dev, void *user_data)
4545
if (!rx_throttled && uart_irq_rx_ready(dev)) {
4646
int recv_len, rb_len;
4747
uint8_t buffer[64];
48-
size_t len = MIN(ring_buf_space_get(&ringbuf),
48+
size_t len = MIN(ring_buffer_space(&ringbuf),
4949
sizeof(buffer));
5050

5151
if (len == 0) {
@@ -61,7 +61,7 @@ static void interrupt_handler(const struct device *dev, void *user_data)
6161
recv_len = 0;
6262
};
6363

64-
rb_len = ring_buf_put(&ringbuf, buffer, recv_len);
64+
rb_len = ring_buffer_write(&ringbuf, buffer, recv_len);
6565
if (rb_len < recv_len) {
6666
LOG_ERR("Drop %u bytes", recv_len - rb_len);
6767
}
@@ -76,7 +76,7 @@ static void interrupt_handler(const struct device *dev, void *user_data)
7676
uint8_t buffer[64];
7777
int rb_len, send_len;
7878

79-
rb_len = ring_buf_get(&ringbuf, buffer, sizeof(buffer));
79+
rb_len = ring_buffer_read(&ringbuf, buffer, sizeof(buffer));
8080
if (!rb_len) {
8181
LOG_DBG("Ring buffer empty, disable TX IRQ");
8282
uart_irq_tx_disable(dev);
@@ -113,7 +113,7 @@ int main(void)
113113
return 0;
114114
}
115115

116-
ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer);
116+
ring_buffer_init(&ringbuf, ring_buffer, sizeof(ring_buffer));
117117

118118
LOG_INF("Wait for DTR");
119119

0 commit comments

Comments
 (0)