oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
i2s.h
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
9#ifndef OVE_I2S_H
10#define OVE_I2S_H
11
29#include "ove/types.h"
30#include "ove_config.h"
31#include "ove/storage.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/* ── Enums ───────────────────────────────────────────────────────── */
38
42typedef enum {
47
48/* ── Callback ────────────────────────────────────────────────────── */
49
60typedef void (*ove_i2s_cb_t)(ove_i2s_t i2s, void *user_data);
61
62/* ── Configuration ───────────────────────────────────────────────── */
63
68 unsigned int instance;
69 uint32_t sample_rate;
70 uint8_t bit_depth;
71 uint8_t channels;
74};
75
76#ifdef CONFIG_OVE_I2S
77
78/* ── Lifecycle ───────────────────────────────────────────────────── */
79
91int ove_i2s_init(ove_i2s_t *i2s, ove_i2s_storage_t *storage,
92 void *tx_dma_buf, void *rx_dma_buf,
93 const struct ove_i2s_cfg *cfg);
94void ove_i2s_deinit(ove_i2s_t i2s);
95
96#ifdef OVE_HEAP_I2S
97int ove_i2s_create(ove_i2s_t *i2s, const struct ove_i2s_cfg *cfg);
98void ove_i2s_destroy(ove_i2s_t i2s);
99#elif !defined(__ZIG_CIMPORT__)
100#define ove_i2s_create(pi2s, cfg) \
101 ({ static ove_i2s_storage_t _ove_stor_; \
102 static uint8_t _ove_txbuf_[((cfg)->dma_buf_samples) * ((cfg)->bit_depth / 8)] \
103 __attribute__((aligned(32))); \
104 static uint8_t _ove_rxbuf_[((cfg)->dma_buf_samples) * ((cfg)->bit_depth / 8)] \
105 __attribute__((aligned(32))); \
106 ove_i2s_init((pi2s), &_ove_stor_, \
107 ((cfg)->direction & OVE_I2S_DIR_TX) ? _ove_txbuf_ : (void *)0, \
108 ((cfg)->direction & OVE_I2S_DIR_RX) ? _ove_rxbuf_ : (void *)0, \
109 (cfg)); })
110#define ove_i2s_destroy(i2s) ove_i2s_deinit(i2s)
111#endif
112
113/* ── Callbacks ───────────────────────────────────────────────────── */
114
120int ove_i2s_set_rx_callback(ove_i2s_t i2s, ove_i2s_cb_t cb,
121 void *user_data);
122
129int ove_i2s_set_tx_callback(ove_i2s_t i2s, ove_i2s_cb_t cb,
130 void *user_data);
131
132/* ── Stream control ──────────────────────────────────────────────── */
133
140int ove_i2s_start(ove_i2s_t i2s);
141
145int ove_i2s_stop(ove_i2s_t i2s);
146
150int ove_i2s_pause(ove_i2s_t i2s);
151
155int ove_i2s_resume(ove_i2s_t i2s);
156
157/* ── Buffer access ───────────────────────────────────────────────── */
158
168void *ove_i2s_rx_buf(ove_i2s_t i2s);
169
179void *ove_i2s_tx_buf(ove_i2s_t i2s);
180
187size_t ove_i2s_half_buf_size(ove_i2s_t i2s);
188
189/* ── ISR helpers (called by backend, not by application) ─────────── */
190
191void ove_i2s_rx_half_cplt_isr(ove_i2s_t i2s);
192void ove_i2s_rx_cplt_isr(ove_i2s_t i2s);
193void ove_i2s_tx_half_cplt_isr(ove_i2s_t i2s);
194void ove_i2s_tx_cplt_isr(ove_i2s_t i2s);
195
196#else /* !CONFIG_OVE_I2S */
197
198static inline int ove_i2s_create(ove_i2s_t *i, const struct ove_i2s_cfg *c) { (void)i; (void)c; return OVE_ERR_NOT_SUPPORTED; }
199static inline void ove_i2s_destroy(ove_i2s_t i) { (void)i; }
200static inline int ove_i2s_set_rx_callback(ove_i2s_t i, ove_i2s_cb_t cb, void *ud) { (void)i; (void)cb; (void)ud; return OVE_ERR_NOT_SUPPORTED; }
201static inline int ove_i2s_set_tx_callback(ove_i2s_t i, ove_i2s_cb_t cb, void *ud) { (void)i; (void)cb; (void)ud; return OVE_ERR_NOT_SUPPORTED; }
202static inline int ove_i2s_start(ove_i2s_t i) { (void)i; return OVE_ERR_NOT_SUPPORTED; }
203static inline int ove_i2s_stop(ove_i2s_t i) { (void)i; return OVE_ERR_NOT_SUPPORTED; }
204static inline int ove_i2s_pause(ove_i2s_t i) { (void)i; return OVE_ERR_NOT_SUPPORTED; }
205static inline int ove_i2s_resume(ove_i2s_t i) { (void)i; return OVE_ERR_NOT_SUPPORTED; }
206static inline void *ove_i2s_rx_buf(ove_i2s_t i) { (void)i; return (void *)0; }
207static inline void *ove_i2s_tx_buf(ove_i2s_t i) { (void)i; return (void *)0; }
208static inline size_t ove_i2s_half_buf_size(ove_i2s_t i) { (void)i; return 0; }
209
210#endif /* CONFIG_OVE_I2S */
211
212#ifdef __cplusplus
213}
214#endif
215
218#endif /* OVE_I2S_H */
void(* ove_i2s_cb_t)(ove_i2s_t i2s, void *user_data)
I2S half-buffer completion callback.
Definition i2s.h:60
ove_i2s_dir_t
I2S stream direction.
Definition i2s.h:42
@ OVE_I2S_DIR_RX
Definition i2s.h:44
@ OVE_I2S_DIR_TX
Definition i2s.h:43
@ OVE_I2S_DIR_TXRX
Definition i2s.h:45
#define OVE_ERR_NOT_SUPPORTED
The requested feature is not supported by the active backend.
Definition types.h:38
struct ove_i2s * ove_i2s_t
Opaque handle for an I2S / SAI bus controller.
Definition types.h:145
I2S bus configuration descriptor.
Definition i2s.h:67
uint32_t sample_rate
Definition i2s.h:69
size_t dma_buf_samples
Definition i2s.h:73
unsigned int instance
Definition i2s.h:68
ove_i2s_dir_t direction
Definition i2s.h:72
uint8_t bit_depth
Definition i2s.h:70
uint8_t channels
Definition i2s.h:71