Zephyr API Documentation  3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
spsc_pbuf.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_SPSC_PBUF_H_
8#define ZEPHYR_INCLUDE_SYS_SPSC_PBUF_H_
9
10#include <zephyr/cache.h>
11#include <zephyr/devicetree.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
33#define SPSC_PBUF_CACHE BIT(0)
34
36#define SPSC_PBUF_UTILIZATION_BITS 24
37
39#define SPSC_PBUF_UTILIZATION_OFFSET 8
40
43#if CONFIG_DCACHE_LINE_SIZE != 0
44#define Z_SPSC_PBUF_LOCAL_DCACHE_LINE CONFIG_DCACHE_LINE_SIZE
45#else
46#define Z_SPSC_PBUF_LOCAL_DCACHE_LINE DT_PROP_OR(CPU, d_cache_line_size, 0)
47#endif
48
49#ifndef CONFIG_SPSC_PBUF_REMOTE_DCACHE_LINE
50#define CONFIG_SPSC_PBUF_REMOTE_DCACHE_LINE 0
51#endif
52
53#define Z_SPSC_PBUF_DCACHE_LINE \
54 MAX(CONFIG_SPSC_PBUF_REMOTE_DCACHE_LINE, Z_SPSC_PBUF_LOCAL_DCACHE_LINE)
55
57#define SPSC_PBUF_MAX_LEN 0xFF00
58
67 uint32_t len; /* Length of data[] in bytes. */
68 uint32_t flags; /* Flags. See @ref SPSC_PBUF_FLAGS */
69 uint32_t rd_idx; /* Index of the first valid byte in data[] */
70};
71
72/* Padding to fill cache line. */
73#define Z_SPSC_PBUF_PADDING \
74 MAX(0, Z_SPSC_PBUF_DCACHE_LINE - (int)sizeof(struct spsc_pbuf_common))
75
82 uint8_t reserved[Z_SPSC_PBUF_PADDING];
83 uint32_t wr_idx; /* Index of the first free byte in data[] */
84 uint8_t data[]; /* Buffer data. */
85};
86
89 uint32_t wr_idx; /* Index of the first free byte in data[] */
90 uint8_t data[]; /* Buffer data. */
91};
92
105struct spsc_pbuf {
107 union {
109 struct spsc_pbuf_ext_nocache nocache;
111};
112
123static inline uint32_t spsc_pbuf_capacity(struct spsc_pbuf *pb)
124{
125 return pb->common.len - sizeof(uint32_t);
126}
127
147struct spsc_pbuf *spsc_pbuf_init(void *buf, size_t blen, uint32_t flags);
148
162int spsc_pbuf_write(struct spsc_pbuf *pb, const char *buf, uint16_t len);
163
192int spsc_pbuf_alloc(struct spsc_pbuf *pb, uint16_t len, char **buf);
193
203void spsc_pbuf_commit(struct spsc_pbuf *pb, uint16_t len);
204
222int spsc_pbuf_read(struct spsc_pbuf *pb, char *buf, uint16_t len);
223
241uint16_t spsc_pbuf_claim(struct spsc_pbuf *pb, char **buf);
242
251void spsc_pbuf_free(struct spsc_pbuf *pb, uint16_t len);
252
269#ifdef __cplusplus
270}
271#endif
272
273#endif /* ZEPHYR_INCLUDE_SYS_SPSC_PBUF_H_ */
cache API interface
Devicetree main header.
static uint32_t spsc_pbuf_capacity(struct spsc_pbuf *pb)
Get buffer capacity.
Definition: spsc_pbuf.h:123
int spsc_pbuf_read(struct spsc_pbuf *pb, char *buf, uint16_t len)
Read specified amount of data from the packet buffer.
void spsc_pbuf_free(struct spsc_pbuf *pb, uint16_t len)
Free the packet to the buffer.
int spsc_pbuf_alloc(struct spsc_pbuf *pb, uint16_t len, char **buf)
Allocate space in the packet buffer.
int spsc_pbuf_write(struct spsc_pbuf *pb, const char *buf, uint16_t len)
Write specified amount of data to the packet buffer.
void spsc_pbuf_commit(struct spsc_pbuf *pb, uint16_t len)
Commit packet to the buffer.
uint16_t spsc_pbuf_claim(struct spsc_pbuf *pb, char **buf)
Claim packet from the buffer.
struct spsc_pbuf * spsc_pbuf_init(void *buf, size_t blen, uint32_t flags)
Initialize the packet buffer.
int spsc_pbuf_get_utilization(struct spsc_pbuf *pb)
Get maximum utilization of the packet buffer.
flags
Definition: parser.h:96
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
First part of packet buffer control block.
Definition: spsc_pbuf.h:66
uint32_t len
Definition: spsc_pbuf.h:67
uint32_t flags
Definition: spsc_pbuf.h:68
uint32_t rd_idx
Definition: spsc_pbuf.h:69
Remaining part of a packet buffer when cache is used.
Definition: spsc_pbuf.h:81
uint32_t wr_idx
Definition: spsc_pbuf.h:83
uint8_t data[]
Definition: spsc_pbuf.h:84
uint8_t reserved[MAX(0, MAX(0, DT_PROP_OR(CPU, d_cache_line_size, 0)) -(int) sizeof(struct spsc_pbuf_common))]
Definition: spsc_pbuf.h:82
Remaining part of a packet buffer when cache is not used.
Definition: spsc_pbuf.h:88
uint8_t data[]
Definition: spsc_pbuf.h:90
uint32_t wr_idx
Definition: spsc_pbuf.h:89
Single producer, single consumer packet buffer.
Definition: spsc_pbuf.h:105
struct spsc_pbuf_common common
Definition: spsc_pbuf.h:106
union spsc_pbuf::@404 ext