Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
usb_buf.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
11
12#ifndef ZEPHYR_INCLUDE_USB_BUF_H
13#define ZEPHYR_INCLUDE_USB_BUF_H
14
15#include <zephyr/kernel.h>
16#include <zephyr/net_buf.h>
17
26
30#if defined(CONFIG_UDC_BUF_FORCE_NOCACHE) || defined(CONFIG_UHC_BUF_FORCE_NOCACHE)
31#define USB_BUF_FORCE_NOCACHE 1
32#else
33#define USB_BUF_FORCE_NOCACHE 0
34#endif
35
36#if defined(CONFIG_DCACHE) && !USB_BUF_FORCE_NOCACHE
37/*
38 * Here we try to get DMA-safe buffers, but we lack a consistent source of
39 * information about data cache properties, such as line cache size, and a
40 * consistent source of information about what part of memory is DMA'able.
41 * For now, we simply assume that all available memory is DMA'able and use
42 * Kconfig option DCACHE_LINE_SIZE for alignment and granularity.
43 */
44#define USB_PRIV_BUFALIGN CONFIG_DCACHE_LINE_SIZE
45#define USB_PRIV_BUFGRANULARITY CONFIG_DCACHE_LINE_SIZE
46#else
47/*
48 * Default alignment and granularity to pointer size if the platform does not
49 * have a data cache or buffers are placed in nocache memory region.
50 */
51#define USB_PRIV_BUFALIGN sizeof(void *)
52#define USB_PRIV_BUFGRANULARITY sizeof(void *)
53#endif
54
55
56#if USB_BUF_FORCE_NOCACHE
57/*
58 * The usb transfer buffer needs to be in __nocache section
59 */
60#define USB_PRIV_BUFSECTION __nocache
61#else
62#define USB_PRIV_BUFSECTION
63#endif
65
67#define USB_BUF_ALIGN USB_PRIV_BUFALIGN
68
70#define USB_BUF_GRANULARITY USB_PRIV_BUFGRANULARITY
71
73#define USB_BUF_ROUND_UP(size) ROUND_UP(size, USB_PRIV_BUFGRANULARITY)
74
84#define USB_STATIC_BUF_DEFINE(name, size) \
85 static uint8_t USB_PRIV_BUFSECTION __aligned(USB_BUF_ALIGN) \
86 name[USB_BUF_ROUND_UP(size)];
87
95#define IS_USB_BUF_ALIGNED(buf) IS_ALIGNED(buf, USB_BUF_ALIGN)
96
100#define USB_BUF_HEAP_DEFINE(name, bytes, in_section) \
101 uint8_t in_section __aligned(USB_BUF_ALIGN) \
102 kheap_##name[MAX(bytes, Z_HEAP_MIN_SIZE)]; \
103 STRUCT_SECTION_ITERABLE(k_heap, name) = { \
104 .heap = { \
105 .init_mem = kheap_##name, \
106 .init_bytes = MAX(bytes, Z_HEAP_MIN_SIZE), \
107 }, \
108 }
109
110#define USB_BUF_K_HEAP_DEFINE(name, size) \
111 COND_CODE_1(USB_BUF_FORCE_NOCACHE, \
112 (USB_BUF_HEAP_DEFINE(name, size, __nocache)), \
113 (USB_BUF_HEAP_DEFINE(name, size, __noinit)))
114
115extern const struct net_buf_data_cb net_buf_dma_cb;
117
132#define USB_BUF_POOL_VAR_DEFINE(pname, count, size, ud_size, fdestroy) \
133 _NET_BUF_ARRAY_DEFINE(pname, count, ud_size); \
134 USB_BUF_K_HEAP_DEFINE(net_buf_mem_pool_##pname, size); \
135 static const struct net_buf_data_alloc net_buf_data_alloc_##pname = { \
136 .cb = &net_buf_dma_cb, \
137 .alloc_data = &net_buf_mem_pool_##pname, \
138 .max_alloc_size = 0, \
139 }; \
140 static STRUCT_SECTION_ITERABLE(net_buf_pool, pname) = \
141 NET_BUF_POOL_INITIALIZER(pname, &net_buf_data_alloc_##pname, \
142 _net_buf_##pname, count, ud_size, \
143 fdestroy)
144
159#define USB_BUF_POOL_DEFINE(pname, count, size, ud_size, fdestroy) \
160 _NET_BUF_ARRAY_DEFINE(pname, count, ud_size); \
161 BUILD_ASSERT((USB_BUF_GRANULARITY) % (USB_BUF_ALIGN) == 0, \
162 "Code assumes granurality is multiple of alignment"); \
163 static uint8_t USB_PRIV_BUFSECTION __aligned(USB_BUF_ALIGN) \
164 net_buf_data_##pname[count][USB_BUF_ROUND_UP(size)]; \
165 static const struct net_buf_pool_fixed net_buf_fixed_##pname = { \
166 .data_pool = (uint8_t *)net_buf_data_##pname, \
167 }; \
168 static const struct net_buf_data_alloc net_buf_fixed_alloc_##pname = { \
169 .cb = &net_buf_fixed_cb, \
170 .alloc_data = (void *)&net_buf_fixed_##pname, \
171 .max_alloc_size = USB_BUF_ROUND_UP(size), \
172 }; \
173 static STRUCT_SECTION_ITERABLE(net_buf_pool, pname) = \
174 NET_BUF_POOL_INITIALIZER(pname, &net_buf_fixed_alloc_##pname, \
175 _net_buf_##pname, count, ud_size, \
176 fdestroy)
177
182#define UDC_BUF_ALIGN USB_BUF_ALIGN
183
188#define UDC_BUF_GRANULARITY USB_BUF_GRANULARITY
189
194#define UDC_ROUND_UP(size) USB_BUF_ROUND_UP(size)
195
200#define UDC_STATIC_BUF_DEFINE(name, size) USB_STATIC_BUF_DEFINE(name, size)
201
206#define IS_UDC_ALIGNED(buf) IS_USB_BUF_ALIGNED(buf)
207
212#define UDC_BUF_POOL_VAR_DEFINE(pname, count, size, ud_size, fdestroy) \
213 USB_BUF_POOL_VAR_DEFINE(pname, count, size, ud_size, fdestroy)
214
219#define UDC_BUF_POOL_DEFINE(pname, count, size, ud_size, fdestroy) \
220 USB_BUF_POOL_DEFINE(pname, count, size, ud_size, fdestroy)
221
224
225#endif /* ZEPHYR_INCLUDE_UDC_BUF_H */
Public kernel APIs.
Buffer management.