VSF Documented
vsf_espidf.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __VSF_ESPIDF_INTERNAL_H__
19#define __VSF_ESPIDF_INTERNAL_H__
20
21/*============================ INCLUDES ======================================*/
22
23#include "kernel/vsf_kernel.h"
24#include "hal/driver/driver.h"
25#include "./vsf_espidf_cfg.h"
26
27#if VSF_USE_ESPIDF == ENABLED
28
29#if VSF_ESPIDF_CFG_USE_HEAP_CAPS == ENABLED
30# if !defined(VSF_USE_HEAP) || VSF_USE_HEAP != ENABLED
31# error "VSF_ESPIDF_CFG_USE_HEAP_CAPS requires VSF_USE_HEAP"
32# endif
34#endif
35
36#if VSF_ESPIDF_CFG_USE_PARTITION == ENABLED
39#endif
40
41#if VSF_ESPIDF_CFG_USE_ESP_FLASH == ENABLED
43# include "./include/esp_flash.h"
44#endif
45
46#if VSF_ESPIDF_CFG_USE_USB_HOST == ENABLED
48#endif
49
50#if VSF_ESPIDF_CFG_USE_USB_DEVICE == ENABLED
52#endif
53
54#if VSF_ESPIDF_CFG_USE_APP_TRACE == ENABLED
55# include <stdio.h>
56#endif
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/*============================ MACROS ========================================*/
63/*============================ TYPES =========================================*/
64
65// Sub-system configuration (pure input). The ESP-IDF compatibility shim is
66// a process-wide singleton (esp_* APIs are global functions without an
67// instance handle), so configuration is passed by value: the caller fills
68// a cfg struct, hands it to vsf_espidf_init(), and the shim copies the
69// fields it needs into its own internal state. The cfg object does not
70// need to outlive the init call and may live on the stack.
71//
72// Field semantics:
73// rng Externally owned RNG source (commonly a vsf_multiplex_rng_t
74// instance cast to vsf_rng_t*). NULL -> the shim falls back to a
75// software PRNG for esp_random / esp_fill_random.
76#if VSF_ESPIDF_CFG_USE_PARTITION == ENABLED
77// One entry in the compile-time partition table. The shim slices the
78// configured root_mal at [offset, offset+size) into a vk_mim_mal_t on
79// boot and publishes it as a matching esp_partition_t. All offsets and
80// sizes are byte units; erase_size defaults to the root mal's natural
81// erase block when set to 0.
83 const char *label;
84 uint8_t type; // esp_partition_type_t
85 uint8_t subtype; // esp_partition_subtype_t
90 uint32_t erase_size; // 0 -> auto-detect via vk_mal_blksz
92
93// Partition sub-system configuration. root_mal is user-owned and MUST
94// outlive the sub-system. The shim never allocates, opens or closes
95// root_mal on behalf of the caller; the expected contract is that the
96// caller has already called vk_mal_init() on it from a stack-owner
97// thread (vsf_thread_t) so that it is in a usable state before being
98// handed over.
104#endif
105
106#if VSF_ESPIDF_CFG_USE_DRIVER_UART == ENABLED
107// uart sub-system configuration.
108//
109// Same pool-injection pattern as gptimer: the caller provides an array
110// of vsf_usart_t* instances. uart_port_t values (UART_NUM_0, 1, ...)
111// index directly into the pool.
112typedef struct vsf_espidf_uart_cfg_t {
116#endif
117
118#if VSF_ESPIDF_CFG_USE_DRIVER_I2C == ENABLED
119// i2c sub-system configuration.
120//
121// Same pool-injection pattern as gptimer / uart: the caller provides an
122// array of vsf_i2c_t* instances. i2c_port_num_t values (0, 1, ...)
123// index directly into the pool.
124typedef struct vsf_espidf_i2c_cfg_t {
128#endif
129
130#if VSF_ESPIDF_CFG_USE_DRIVER_SPI_MASTER == ENABLED
131// spi_master sub-system configuration.
132//
133// Same pool-injection pattern as gptimer / uart / i2c: the caller
134// provides an array of vsf_spi_t* instances. spi_host_device_t values
135// (SPI1_HOST, SPI2_HOST, ...) index directly into the pool.
136typedef struct vsf_espidf_spi_cfg_t {
140#endif
141
142#if VSF_ESPIDF_CFG_USE_DRIVER_LEDC == ENABLED
143// ledc sub-system configuration.
144//
145// Pool index equals ledc_mode_t: entry 0 -> LEDC_LOW_SPEED_MODE,
146// entry 1 (optional) -> LEDC_HIGH_SPEED_MODE. Each vsf_pwm_t is
147// shared by all channels of its speed_mode.
148typedef struct vsf_espidf_ledc_cfg_t {
152#endif
153
154#if VSF_ESPIDF_CFG_USE_DRIVER_ADC == ENABLED
155// adc (oneshot) sub-system configuration.
156//
157// Pool index equals adc_unit_t: entry 0 -> ADC_UNIT_1, entry 1 ->
158// ADC_UNIT_2. Only one-shot mode is bridged; callers requiring
159// continuous/DMA mode will receive ESP_ERR_NOT_SUPPORTED.
160typedef struct vsf_espidf_adc_cfg_t {
164#endif
165
166#if VSF_ESPIDF_CFG_USE_DRIVER_GPTIMER == ENABLED
167// gptimer sub-system configuration.
168//
169// ESP-IDF's gptimer_new_timer() allocates a timer handle opaquely
170// (the caller does not choose which hardware timer to use). To bridge
171// this onto VSF -- where each vsf_timer_t* is board/MCU-owned and
172// statically defined at BSP time -- the caller hands in an array of
173// pointers to vsf_timer_t instances that the shim may draw from.
174//
175// pool Array of vsf_timer_t* already brought up by the board
176// BSP. The shim neither initialises nor finalises these
177// instances itself; it only calls vsf_timer_init() /
178// vsf_timer_fini() on them when a gptimer handle is
179// allocated / released. Ownership stays with the caller.
180// pool_count Number of entries in pool.
181//
182// A zero-initialised cfg (pool=NULL, pool_count=0) disables gptimer_*
183// allocation entirely; gptimer_new_timer() then returns
184// ESP_ERR_INVALID_STATE.
189#endif
190
191#if VSF_ESPIDF_CFG_USE_USB_HOST == ENABLED
192// USB Host sub-system configuration.
193//
194// The board layer provides a pre-configured vk_usbh_t with the HCD
195// driver, PHY setup, interrupt routing etc. already set according to
196// the actual hardware environment. The espidf shim adds the ESP-IDF
197// bridge class driver on top and manages the client/device model.
198//
199// usbh Board-owned vk_usbh_t instance. NULL -> usb_host_install()
200// returns ESP_ERR_INVALID_STATE.
204#endif
205
206#if VSF_ESPIDF_CFG_USE_USB_DEVICE == ENABLED
207// USB Device sub-system configuration.
208//
209// The board layer provides a pre-configured vk_usbd_dev_t with the DCD
210// driver, descriptors and a bridge class interface already set according
211// to the actual hardware environment. The espidf shim wraps the ROM
212// usb_device.h API on top.
213//
214// usbd Board-owned vk_usbd_dev_t instance. NULL -> usb_enable()
215// returns failure.
217 vk_usbd_dev_t *usbd;
219#endif
220
221#if VSF_ESPIDF_CFG_USE_APP_TRACE == ENABLED
222// Application trace sub-system configuration.
223// up_stream FILE* for up-channel (target -> host) output. NULL -> stdout.
224// down_stream FILE* for down-channel (host -> target) input. NULL -> stdin.
229#endif
230
231typedef struct vsf_espidf_cfg_t {
232#if VSF_HAL_USE_RNG == ENABLED
234#endif
235#if VSF_ESPIDF_CFG_USE_PARTITION == ENABLED
237#endif
238#if VSF_ESPIDF_CFG_USE_DRIVER_GPTIMER == ENABLED
240#endif
241#if VSF_ESPIDF_CFG_USE_DRIVER_UART == ENABLED
243#endif
244#if VSF_ESPIDF_CFG_USE_DRIVER_I2C == ENABLED
246#endif
247#if VSF_ESPIDF_CFG_USE_DRIVER_SPI_MASTER == ENABLED
249#endif
250#if VSF_ESPIDF_CFG_USE_DRIVER_LEDC == ENABLED
252#endif
253#if VSF_ESPIDF_CFG_USE_DRIVER_ADC == ENABLED
255#endif
256#if VSF_ESPIDF_CFG_USE_USB_HOST == ENABLED
258#endif
259#if VSF_ESPIDF_CFG_USE_USB_DEVICE == ENABLED
261#endif
262#if VSF_ESPIDF_CFG_USE_APP_TRACE == ENABLED
264#endif
265#if VSF_ESPIDF_CFG_USE_HEAP_CAPS == ENABLED
266 /* Optional callback to map an ESP-IDF heap_caps bitmask to a VS F
267 * heap instance. When non-NULL, heap_caps_malloc() and
268 * xRingbufferCreateWithCaps() route allocations through the
269 * returned heap. If NULL, the default flat heap is used. */
270 vsf_heap_t *(*caps_to_heap)(uint32_t caps);
271#endif
273
274/*============================ GLOBAL VARIABLES ==============================*/
275/*============================ PROTOTYPES ====================================*/
276
277// Sub-system lifecycle. Idempotent: subsequent calls are no-ops. Passing
278// NULL is permitted and equivalent to a zero-initialised cfg (all
279// peripherals left unset, shim uses built-in fallbacks).
280extern void vsf_espidf_init(const vsf_espidf_cfg_t *cfg);
281
282#if VSF_HAL_USE_RNG == ENABLED
283// Accessor used by internal port code. Returns the RNG source registered
284// via vsf_espidf_init(), or NULL if none was provided (callers should
285// fall back to their own default).
286extern vsf_rng_t * vsf_espidf_get_rng(void);
287#endif
288
289#if VSF_ESPIDF_CFG_USE_USB_HOST == ENABLED
290extern vk_usbh_t * vsf_espidf_get_usbh(void);
291#endif
292
293#if VSF_ESPIDF_CFG_USE_USB_DEVICE == ENABLED
294extern vk_usbd_dev_t * vsf_espidf_get_usbd(void);
295#endif
296
297#if VSF_ESPIDF_CFG_USE_HEAP_CAPS == ENABLED
298extern vsf_heap_t *(*vsf_espidf_get_caps_to_heap(void))(uint32_t caps);
299#endif
300
301#if VSF_ESPIDF_CFG_USE_APP_TRACE == ENABLED
304#endif
305
306#ifdef __cplusplus
307}
308#endif
309
310#endif // VSF_USE_ESPIDF
311#endif // __VSF_ESPIDF_INTERNAL_H__
mal class
Definition vsf_mal.h:168
Definition vsf_usbh.h:523
Definition vsf_heap.h:139
unsigned short uint16_t
Definition stdint.h:7
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
Definition stdio.h:145
Definition vsf_template_adc.h:755
Definition vsf_espidf.h:160
vsf_adc_t *const * pool
Definition vsf_espidf.h:161
uint16_t pool_count
Definition vsf_espidf.h:162
Definition vsf_espidf.h:225
FILE * down_stream
Definition vsf_espidf.h:227
FILE * up_stream
Definition vsf_espidf.h:226
Definition vsf_espidf.h:231
vsf_espidf_i2c_cfg_t i2c
Definition vsf_espidf.h:245
vsf_espidf_ledc_cfg_t ledc
Definition vsf_espidf.h:251
vsf_espidf_partition_cfg_t partition
Definition vsf_espidf.h:236
vsf_espidf_usb_host_cfg_t usb_host
Definition vsf_espidf.h:257
vsf_espidf_app_trace_cfg_t app_trace
Definition vsf_espidf.h:263
vsf_rng_t * rng
Definition vsf_espidf.h:233
vsf_espidf_adc_cfg_t adc
Definition vsf_espidf.h:254
vsf_espidf_gptimer_cfg_t gptimer
Definition vsf_espidf.h:239
vsf_espidf_uart_cfg_t uart
Definition vsf_espidf.h:242
vsf_espidf_spi_cfg_t spi
Definition vsf_espidf.h:248
vsf_espidf_usb_device_cfg_t usb_device
Definition vsf_espidf.h:260
Definition vsf_espidf.h:185
uint16_t pool_count
Definition vsf_espidf.h:187
vsf_timer_t *const * pool
Definition vsf_espidf.h:186
Definition vsf_espidf.h:124
uint16_t pool_count
Definition vsf_espidf.h:126
vsf_i2c_t *const * pool
Definition vsf_espidf.h:125
Definition vsf_espidf.h:148
vsf_pwm_t *const * pool
Definition vsf_espidf.h:149
uint16_t pool_count
Definition vsf_espidf.h:150
Definition vsf_espidf.h:99
const vsf_espidf_partition_entry_t * entries
Definition vsf_espidf.h:101
uint16_t entry_count
Definition vsf_espidf.h:102
vk_mal_t * root_mal
Definition vsf_espidf.h:100
Definition vsf_espidf.h:82
uint32_t erase_size
Definition vsf_espidf.h:90
uint8_t type
Definition vsf_espidf.h:84
const char * label
Definition vsf_espidf.h:83
uint8_t encrypted
Definition vsf_espidf.h:86
uint8_t readonly
Definition vsf_espidf.h:87
uint8_t subtype
Definition vsf_espidf.h:85
uint32_t offset
Definition vsf_espidf.h:88
uint32_t size
Definition vsf_espidf.h:89
Definition vsf_espidf.h:136
vsf_spi_t *const * pool
Definition vsf_espidf.h:137
uint16_t pool_count
Definition vsf_espidf.h:138
Definition vsf_espidf.h:112
vsf_usart_t *const * pool
Definition vsf_espidf.h:113
uint16_t pool_count
Definition vsf_espidf.h:114
Definition vsf_espidf.h:216
vk_usbd_dev_t * usbd
Definition vsf_espidf.h:217
Definition vsf_espidf.h:201
vk_usbh_t * usbh
Definition vsf_espidf.h:202
I2C instance structure for multi-class support.
Definition vsf_template_i2c.h:1056
Definition vsf_template_pwm.h:274
Definition vsf_template_rng.h:197
SPI instance structure, used for SPI Multi Class support, not needed in non Multi Class mode.
Definition vsf_template_spi.h:1131
Definition vsf_template_timer.h:780
USART instance structure Used for USART Multi Class support.
Definition vsf_template_usart.h:1138
vsf_rng_t * vsf_espidf_get_rng(void)
Definition vsf_espidf.c:152
void vsf_espidf_init(const vsf_espidf_cfg_t *cfg)
Definition vsf_espidf.c:57
vk_usbh_t * vsf_espidf_get_usbh(void)
Definition vsf_espidf.c:159
vk_usbd_dev_t * vsf_espidf_get_usbd(void)
Definition vsf_espidf.c:166
void vsf_espidf_app_trace_init(const vsf_espidf_app_trace_cfg_t *cfg)
Definition esp_app_trace_port.c:377
vsf_heap_t *(*)(uint32_t caps) vsf_espidf_get_caps_to_heap(void)
Definition vsf_espidf.c:173
const vsf_espidf_app_trace_cfg_t * vsf_espidf_get_app_trace_cfg(void)
Definition esp_app_trace_port.c:396
Generated from commit: vsfteam/vsf@c3767bf