nRF Connect SDK Bare Metal API 2.0.99
Loading...
Searching...
No Matches
bm_spi_mngr.h
1/*
2 * Copyright (c) 2026 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
30#ifndef BM_SPI_MNGR_H__
31#define BM_SPI_MNGR_H__
32
33#include <stdbool.h>
34#include <stddef.h>
35#include <stdint.h>
36
37#include <nrfx_spim.h>
38#include <zephyr/sys/ring_buffer.h>
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
52#define BM_SPI_MNGR_TRANSFER(_tx_data, _tx_length, _rx_data, _rx_length) \
53 { \
54 .tx_data = (const uint8_t *)(_tx_data), \
55 .tx_length = (uint8_t)(_tx_length), \
56 .rx_data = (uint8_t *)(_rx_data), \
57 .rx_length = (uint8_t)(_rx_length), \
58 }
59
68typedef void (*bm_spi_mngr_callback_end_t)(int result, void *user_data);
69
76typedef void (*bm_spi_mngr_callback_begin_t)(void *user_data);
77
88 const uint8_t *tx_data;
92 uint8_t tx_length;
96 uint8_t *rx_data;
100 uint8_t rx_length;
101};
102
148
158 struct {
167 nrfx_spim_config_t default_configuration;
171 const nrfx_spim_config_t *current_configuration;
175 uint8_t volatile current_transfer_idx;
176 } cb;
180 struct {
185 size_t size;
189 struct ring_buf ring_buf;
196 uint8_t *byte_storage;
201 nrfx_spim_t *spim;
202};
203
218#define BM_SPI_MNGR_DEF(_name, _queue_size, _spim_inst) \
219 static uint8_t _name##_queue_bytes[(_queue_size) * \
220 sizeof(const struct bm_spi_mngr_transaction *)]; \
221 static nrfx_spim_t _name##_spim = NRFX_SPIM_INSTANCE(_spim_inst); \
222 static struct bm_spi_mngr _name = { \
223 .queue = { \
224 .size = (_queue_size), \
225 .byte_storage = _name##_queue_bytes, \
226 }, \
227 .spim = &_name##_spim, \
228 }
229
246int bm_spi_mngr_init(struct bm_spi_mngr *mgr, const nrfx_spim_config_t *default_spim_cfg);
247
257
283 const struct bm_spi_mngr_transaction *transaction);
284
293bool bm_spi_mngr_is_idle(const struct bm_spi_mngr *mgr);
294
295#ifdef __cplusplus
296}
297#endif
298
299#endif /* BM_SPI_MNGR_H__ */
300
int bm_spi_mngr_init(struct bm_spi_mngr *mgr, const nrfx_spim_config_t *default_spim_cfg)
Initialize the SPI manager and the underlying SPIM driver.
bool bm_spi_mngr_is_idle(const struct bm_spi_mngr *mgr)
Get the current state of an SPI transaction manager instance.
int bm_spi_mngr_schedule(struct bm_spi_mngr *mgr, const struct bm_spi_mngr_transaction *transaction)
Schedule an SPI transaction.
void(* bm_spi_mngr_callback_begin_t)(void *user_data)
Transaction begin callback, runs from the SPIM event handler context.
Definition bm_spi_mngr.h:76
void(* bm_spi_mngr_callback_end_t)(int result, void *user_data)
Transaction end callback.
Definition bm_spi_mngr.h:68
int bm_spi_mngr_uninit(struct bm_spi_mngr *mgr)
Uninitialize the SPI manager and SPIM.
bm_spi_mngr_callback_begin_t begin_callback
User function invoked before the first transfer of the transaction starts.
Definition bm_spi_mngr.h:120
const nrfx_spim_config_t * required_spim_cfg
Optional SPIM configuration for this transaction, NULL selects the default.
Definition bm_spi_mngr.h:146
uint8_t number_of_transfers
Number of entries in transfers.
Definition bm_spi_mngr.h:140
const struct bm_spi_mngr_transfer * transfers
Array of transfers that make up the transaction.
Definition bm_spi_mngr.h:136
bm_spi_mngr_callback_end_t end_callback
User function invoked when the transaction completes or aborts after an error.
Definition bm_spi_mngr.h:124
void * user_data
Opaque pointer passed to the optional begin and end callbacks.
Definition bm_spi_mngr.h:130
SPI transaction descriptor.
Definition bm_spi_mngr.h:116
const uint8_t * tx_data
Pointer to data to send.
Definition bm_spi_mngr.h:88
uint8_t * rx_data
Pointer to buffer for received data.
Definition bm_spi_mngr.h:96
uint8_t tx_length
Number of bytes to send.
Definition bm_spi_mngr.h:92
uint8_t rx_length
Number of bytes to receive.
Definition bm_spi_mngr.h:100
SPI transfer descriptor, one segment of a transaction.
Definition bm_spi_mngr.h:84
struct bm_spi_mngr::@125 cb
Control block (writable runtime state) for this instance.
const nrfx_spim_config_t * current_configuration
Pointer to the SPIM configuration currently applied to the instance.
Definition bm_spi_mngr.h:171
uint8_t volatile current_transfer_idx
Index of the active transfer within current_transaction.
Definition bm_spi_mngr.h:175
nrfx_spim_config_t default_configuration
Default SPIM configuration (copy of the argument to bm_spi_mngr_init)
Definition bm_spi_mngr.h:167
struct bm_spi_mngr::@126 queue
Pending transaction queue (buffer + Zephyr ring_buf).
nrfx_spim_t * spim
nrfx SPIM driver instance.
Definition bm_spi_mngr.h:201
const struct bm_spi_mngr_transaction *volatile current_transaction
Transaction currently being executed (NULL when idle).
Definition bm_spi_mngr.h:162
uint8_t * byte_storage
Buffer that ring_buf uses to store the queued transaction pointers.
Definition bm_spi_mngr.h:196
size_t size
Maximum number of pending transactions (not counting the one in progress).
Definition bm_spi_mngr.h:185
SPI transaction manager instance.
Definition bm_spi_mngr.h:154