VSF Documented
spi.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 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 __HAL_DRIVER_${SERIES/SPI_IP}_SPI_H__
19#define __HAL_DRIVER_${SERIES/SPI_IP}_SPI_H__
20
21/*============================ INCLUDES ======================================*/
22
23#include "hal/vsf_hal_cfg.h"
24
25#if VSF_HAL_USE_SPI == ENABLED
26
27// HW/IPCore
32// HW/IPCore end
33
34/*\note Refer to template/README.md for usage cases.
35 * For peripheral drivers, blackbox mode is recommended but not required, reimplementation part MUST be open.
36 * For IPCore drivers, class structure, MULTI_CLASS configuration, reimplementation and class APIs should be open to user.
37 * For emulated drivers, **** No reimplementation ****.
38 */
39
40/*\note Includes CAN ONLY be put here. */
41/*\note If current header is for a peripheral driver(hw driver), and inherit from an IPCore driver, include IPCore header here. */
42
43// IPCore
44#if defined(__VSF_HAL_${SPI_IP}_SPI_CLASS_IMPLEMENT)
45# define __VSF_CLASS_IMPLEMENT__
46#elif defined(__VSF_HAL_${SPI_IP}_SPI_CLASS_INHERIT__)
47# define __VSF_CLASS_INHERIT__
48#endif
49
50#include "utilities/ooc_class.h"
51// IPCore end
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/*============================ MACROS ========================================*/
58
59/*\note VSF_${SPI_IP}_SPI_CFG_MULTI_CLASS should be implemented for IP drives and open to user,
60 * while VSF_HW_SPI_CFG_MULTI_CLASS should be in spi.c.
61 */
62
63// IPCore
64#ifndef VSF_${SPI_IP}_SPI_CFG_MULTI_CLASS
65# define VSF_${SPI_IP}_SPI_CFG_MULTI_CLASS VSF_SPI_CFG_MULTI_CLASS
66#endif
67// IPCore end
68
69// HW
70/*\note hw SPI driver can reimplement following types:
71 * To enable reimplementation, please enable macro below:
72 * VSF_SPI_CFG_REIMPLEMENT_TYPE_MODE for vsf_spi_mode_t
73 * VSF_SPI_CFG_REIMPLEMENT_TYPE_STATUS for vsf_spi_status_t
74 * VSF_SPI_CFG_REIMPLEMENT_TYPE_IRQ_MASK for vsf_spi_irq_mask_t
75 * VSF_SPI_CFG_REIMPLEMENT_TYPE_CTRL for vsf_spi_ctrl_t
76 * VSF_SPI_CFG_REIMPLEMENT_TYPE_CFG for vsf_spi_cfg_t
77 * VSF_SPI_CFG_REIMPLEMENT_TYPE_CAPABILITY for vsf_spi_capability_t
78 * Reimplementation is used for optimization hw/IPCore drivers, reimplement the bit mask according to hw registers.
79 * *** DO NOT reimplement these in emulated drivers. ***
80 */
81
82#define VSF_SPI_CFG_REIMPLEMENT_TYPE_MODE ENABLED
83#define VSF_SPI_CFG_REIMPLEMENT_TYPE_IRQ_MASK ENABLED
84#define VSF_SPI_CFG_REIMPLEMENT_TYPE_STATUS ENABLED
85#define VSF_SPI_CFG_REIMPLEMENT_TYPE_CTRL ENABLED
86#define VSF_SPI_CFG_REIMPLEMENT_TYPE_CFG ENABLED
87#define VSF_SPI_CFG_REIMPLEMENT_TYPE_CAPABILITY ENABLED
88
89#define VSF_SPI_CFG_REIMPLEMENT_MODE_TO_DATA_BITS ENABLED
90#define VSF_SPI_CFG_REIMPLEMENT_DATA_BITS_TO_MODE ENABLED
91#define VSF_SPI_CFG_REIMPLEMENT_MODE_TO_BYTES ENABLED
92// HW end
93
94/*============================ MACROFIED FUNCTIONS ===========================*/
95/*============================ TYPES =========================================*/
96
97// IPCore
98vsf_class(vsf_${spi_ip}_spi_t) {
99#if VSF_${SPI_IP}_CFG_MULTI_CLASS == ENABLED
100 public_member(
102 )
103#endif
104
105/*\note You can add more member in vsf_${spi_ip}_spi_t instance.
106 * For members accessible from child, put in protected_member.
107 * Else, put in private_member.
108 */
109
110 protected_member(
111 vsf_${spi_ip}_spi_reg_t *reg;
114};
115// IPCore end
116
117// HW/IPCore, not for emulated drivers
118#if VSF_SPI_CFG_REIMPLEMENT_TYPE_MODE == ENABLED
119typedef enum vsf_spi_mode_t {
120 VSF_SPI_MASTER = 0x00ul << 0,
121 VSF_SPI_SLAVE = 0x01ul << 0,
122 VSF_SPI_MSB_FIRST = 0x00ul << 1,
123 VSF_SPI_LSB_FIRST = 0x01ul << 1,
124
125 // SPI mode (bits 2-3: CPOL and CPHA)
126 VSF_SPI_MODE_0 = 0x00ul << 2,
127 VSF_SPI_MODE_1 = 0x01ul << 2,
128 VSF_SPI_MODE_2 = 0x02ul << 2,
129 VSF_SPI_MODE_3 = 0x03ul << 2,
130
133 VSF_SPI_DATASIZE_8 = 0x00ul << 8,
134 VSF_SPI_DATASIZE_16 = 0x01ul << 8,
135 VSF_SPI_DATASIZE_32 = 0x02ul << 8,
136
137 // Standard Optional: only include these if hardware supports them
138 //VSF_SPI_CPOL_LOW = 0x00ul << 2,
139 //VSF_SPI_CPOL_HIGH = 0x01ul << 2,
140 //VSF_SPI_CPHA_LOW = 0x00ul << 3,
141 //VSF_SPI_CPHA_HIGH = 0x01ul << 3,
142
143 // more vendor specified modes can be added here
145#endif
146
147#if VSF_SPI_CFG_REIMPLEMENT_TYPE_IRQ_MASK == ENABLED
148typedef enum vsf_spi_irq_mask_t {
149 VSF_SPI_IRQ_MASK_TX = 0x01ul << 0,
150 VSF_SPI_IRQ_MASK_RX = 0x01ul << 1,
154
155 // more vendor specified irq_masks can be added here
157#endif
158
159#if VSF_SPI_CFG_REIMPLEMENT_TYPE_STATUS == ENABLED
160typedef struct vsf_spi_status_t {
161 union {
162 struct {
163 uint32_t is_busy : 1;
164 };
165 };
166
167 // more vendor specified status can be added here
169#endif
170
171#if VSF_SPI_CFG_REIMPLEMENT_TYPE_CAPABILITY == ENABLED
172typedef struct vsf_spi_capability_t {
176 uint8_t cs_count : 6;
179
180 // more vendor specified capability can be added here
182#endif
183
184#if VSF_SPI_CFG_REIMPLEMENT_TYPE_CFG == ENABLED
188typedef struct vsf_spi_t vsf_spi_t;
189typedef void vsf_spi_isr_handler_t(void *target_ptr,
190 vsf_spi_t *spi_ptr,
192typedef struct vsf_spi_isr_t {
194 void *target_ptr;
197typedef struct vsf_spi_cfg_t {
202
203 // more vendor specified cfg can be added here
205#endif
206
207#if VSF_SPI_CFG_REIMPLEMENT_TYPE_CTRL == ENABLED
208typedef enum vsf_spi_ctrl_t {
211#endif
212// HW/IPCore end
213
214/*============================ INLINE FUNCTIONS ==============================*/
215
216#if VSF_SPI_CFG_REIMPLEMENT_MODE_TO_DATA_BITS == ENABLED
217// Ensure VSF_SPI_DATASIZE_MASK and related macros are defined before using them
218// They will be defined in vsf_template_spi.h, but we need them here for the inline function
219// Use macro definitions if template header hasn't been included yet
220#ifndef VSF_SPI_DATASIZE_MASK
221# define VSF_SPI_DATASIZE_MASK (VSF_SPI_DATASIZE_8 | VSF_SPI_DATASIZE_16 | VSF_SPI_DATASIZE_32)
222#endif
223#ifndef VSF_SPI_DATASIZE_BIT_OFFSET
224# define VSF_SPI_DATASIZE_BIT_OFFSET 8
225#endif
226#ifndef VSF_SPI_DATASIZE_VALUE_OFFSET
227# define VSF_SPI_DATASIZE_VALUE_OFFSET 1
228#endif
229
240static inline uint8_t vsf_spi_mode_to_data_bits(vsf_spi_mode_t mode)
241{
243 return bits + VSF_SPI_DATASIZE_VALUE_OFFSET;
244}
245#endif
246
247#if VSF_SPI_CFG_REIMPLEMENT_DATA_BITS_TO_MODE == ENABLED
248// Ensure VSF_SPI_DATASIZE_BIT_OFFSET and VSF_SPI_DATASIZE_VALUE_OFFSET are defined
249#ifndef VSF_SPI_DATASIZE_BIT_OFFSET
250# define VSF_SPI_DATASIZE_BIT_OFFSET 8
251#endif
252#ifndef VSF_SPI_DATASIZE_VALUE_OFFSET
253# define VSF_SPI_DATASIZE_VALUE_OFFSET 1
254#endif
255
266static inline vsf_spi_mode_t vsf_spi_data_bits_to_mode(uint8_t data_bits)
267{
269}
270#endif
271
272#if VSF_SPI_CFG_REIMPLEMENT_MODE_TO_BYTES == ENABLED
283static inline uint8_t vsf_spi_mode_to_data_bytes(vsf_spi_mode_t mode)
284{
285 uint8_t bits = vsf_spi_mode_to_data_bits(mode);
286 if (bits == 0) {
287 return 0; // Error: invalid data bits
288 } else if (bits <= 8) {
289 return 1;
290 } else if (bits <= 16) {
291 return 2;
292 } else {
293 return 4;
294 }
295}
296#endif
297
298/*============================ INCLUDES ======================================*/
299
300// IPCore
301/*\note Extern APIs for ip core diriver.
302 * There is no requirement about how APIs of IPCore drivers should be implemented.
303 * Just consider the simplicity for actual peripheral drivers.
304 */
305// IPCore end
306
307#ifdef __cplusplus
308}
309#endif
310
311// IPCore
312#undef __VSF_HAL_${SPI_IP}_SPI_CLASS_IMPLEMENT
313#undef __VSF_HAL_${SPI_IP}_SPI_CLASS_INHERIT__
314// IPCore end
315
316#endif // VSF_HAL_USE_SPI
317#endif // __HAL_DRIVER_${SERIES/SPI_IP}_SPI_H__
318/* EOF */
vsf_spi_mode_t
Definition spi.h:33
@ VSF_SPI_SLAVE
Definition spi.h:34
@ VSF_SPI_DATASIZE_16
Definition spi.h:57
@ VSF_SPI_MODE_1
Definition spi.h:41
@ VSF_SPI_DATASIZE_8
datasize is 8 bits
Definition spi.h:49
@ VSF_SPI_MASTER
Definition spi.h:35
@ VSF_SPI_DATASIZE_BIT_OFFSET
Definition spi.h:47
@ VSF_SPI_DATASIZE_32
Definition spi.h:73
@ VSF_SPI_MODE_0
Definition spi.h:39
@ VSF_SPI_MODE_3
Definition spi.h:45
@ VSF_SPI_MODE_2
Definition spi.h:43
vsf_spi_irq_mask_t
Definition spi.h:159
@ VSF_SPI_LSB_FIRST
Definition spi.h:78
@ VSF_SPI_MSB_FIRST
Definition spi.h:77
@ VSF_SPI_CS_SOFTWARE_MODE
Definition spi.h:87
@ VSF_SPI_CS_HARDWARE_MODE
Definition spi.h:89
struct vsf_spi_isr_t vsf_spi_isr_t
void vsf_spi_isr_handler_t(void *target_ptr, vsf_spi_t *spi_ptr, vsf_spi_irq_mask_t irq_mask)
Definition spi.h:153
struct vsf_spi_cfg_t vsf_spi_cfg_t
struct vsf_spi_capability_t vsf_spi_capability_t
@ VSF_SPI_IRQ_MASK_RX_OVERFLOW_ERR
Definition spi.h:117
@ VSF_SPI_IRQ_MASK_RX
Definition spi.h:114
@ VSF_SPI_IRQ_MASK_TX
Definition spi.h:113
struct vsf_spi_status_t vsf_spi_status_t
vsf_spi_mode_t
Definition spi.h:79
vsf_spi_ctrl_t
Definition spi.h:172
@ __VSF_SPI_CTRL_DUMMY
Definition spi.h:173
vsf_spi_irq_mask_t
Definition spi.h:41
@ VSF_SPI_IRQ_MASK_RX_CPL
Definition spi.h:43
@ VSF_SPI_IRQ_MASK_TX_CPL
Definition spi.h:42
@ VSF_SPI_DATASIZE_MASK
Definition spi.h:87
Definition adc.h:98
vsf_arch_prio_t
Definition cortex_a_generic.h:88
#define vsf_class(__name)
Definition ooc_class.h:52
const i_spi_t vsf_spi_irq_mask_t irq_mask
Definition spi_interface.h:38
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
Predefined VSF SPI capability that can be reimplemented in specific HAL drivers. Even if the hardware...
Definition vsf_template_spi.h:739
uint8_t support_software_cs
Hardware chip select support (1: supported, 0: not supported)
Definition vsf_template_spi.h:746
uint32_t max_clock_hz
Number of available chip select lines (0-63)
Definition vsf_template_spi.h:749
uint8_t cs_count
Software chip select support (1: supported, 0: not supported)
Definition vsf_template_spi.h:747
vsf_spi_irq_mask_t irq_mask
Definition spi.h:137
uint8_t support_hardware_cs
Supported interrupt masks for SPI operations.
Definition vsf_template_spi.h:745
uint32_t min_clock_hz
Maximum supported SPI clock frequency in Hz.
Definition vsf_template_spi.h:750
Configuration structure for SPI.
Definition vsf_template_spi.h:797
uint32_t clock_hz
SPI operating mode (master/slave, CPOL/CPHA, bit order, data size)
Definition vsf_template_spi.h:799
vsf_spi_isr_t isr
SPI clock frequency in Hz (must be between min_clock_hz and max_clock_hz)
Definition vsf_template_spi.h:800
vsf_spi_mode_t mode
Definition vsf_template_spi.h:798
uint8_t auto_cs_index
Interrupt configuration (handler, target pointer, priority)
Definition vsf_template_spi.h:803
SPI interrupt service routine configuration structure.
Definition vsf_template_spi.h:785
vsf_spi_isr_handler_t * handler_fn
Definition vsf_template_spi.h:786
void * target_ptr
Interrupt handler function (NULL to disable interrupts)
Definition vsf_template_spi.h:787
vsf_arch_prio_t prio
User context pointer passed to handler.
Definition vsf_template_spi.h:788
Predefined VSF SPI status that can be reimplemented in specific HAL drivers. Even if the hardware doe...
Definition vsf_template_spi.h:719
uint32_t is_busy
Definition spi.h:127
SPI instance structure, used for SPI Multi Class support, not needed in non Multi Class mode.
Definition vsf_template_spi.h:1131
void vsf_spi_isr_handler_t(void *target_ptr, vsf_spi_t *spi_ptr, vsf_spi_irq_mask_t irq_mask)
Definition spi.h:189
class vsf_$ * reg
#define VSF_SPI_DATASIZE_VALUE_OFFSET
Definition spi.h:227
vsf_spi_isr_t isr
Definition spi.h:113
vsf_spi_t vsf_spi[SPI_COUNT]
Definition vsf_spi.c:3
vsf_spi_ctrl_t
Predefined VSF SPI control commands that can be reimplemented in specific HAL drivers.
Definition vsf_template_spi.h:842
Generated from commit: vsfteam/vsf@b2e9e8a