no-OS
spi.h
Go to the documentation of this file.
1 /***************************************************************************/
40 #ifndef SPI_H_
41 #define SPI_H_
42 
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 
47 #include <stdint.h>
48 
49 /******************************************************************************/
50 /********************** Macros and Constants Definitions **********************/
51 /******************************************************************************/
52 
53 #define SPI_CPHA 0x01
54 #define SPI_CPOL 0x02
55 
56 /******************************************************************************/
57 /*************************** Types Declarations *******************************/
58 /******************************************************************************/
59 
64 typedef enum spi_mode {
66  SPI_MODE_0 = (0 | 0),
73 } spi_mode;
74 
79 typedef enum spi_bit_order {
85 
90 struct spi_msg {
92  uint8_t *tx_buff;
94  uint8_t *rx_buff;
96  uint32_t bytes_number;
98  uint8_t cs_change;
99 };
100 
106 struct spi_platform_ops ;
107 
112 typedef struct spi_init_param {
114  uint32_t device_id;
116  uint32_t max_speed_hz;
118  uint8_t chip_select;
125  void *extra;
127 
132 typedef struct spi_desc {
134  uint32_t device_id;
136  uint32_t max_speed_hz;
138  uint8_t chip_select;
145  void *extra;
146 } spi_desc;
147 
155  int32_t (*init)(struct spi_desc **, const struct spi_init_param *);
157  int32_t (*write_and_read)(struct spi_desc *, uint8_t *, uint16_t);
159  int32_t (*transfer)(struct spi_desc *, struct spi_msg *, uint32_t);
161  int32_t (*remove)(struct spi_desc *);
162 };
163 
164 /******************************************************************************/
165 /************************ Functions Declarations ******************************/
166 /******************************************************************************/
167 
168 /* Initialize the SPI communication peripheral. */
169 int32_t spi_init(struct spi_desc **desc,
170  const struct spi_init_param *param);
171 
172 /* Free the resources allocated by spi_init(). */
173 int32_t spi_remove(struct spi_desc *desc);
174 
175 /* Write and read data to/from SPI. */
176 int32_t spi_write_and_read(struct spi_desc *desc,
177  uint8_t *data,
178  uint16_t bytes_number);
179 
180 /* Iterate over the spi_msg array and send all messages at once */
181 int32_t spi_transfer(struct spi_desc *desc, struct spi_msg *msgs, uint32_t len);
182 
183 
184 #endif // SPI_H_
spi_remove
int32_t spi_remove(struct spi_desc *desc)
Free the resources allocated by spi_init().
Definition: spi.c:70
spi_msg::rx_buff
uint8_t * rx_buff
Definition: spi.h:94
spi_mode
spi_mode
SPI configuration for clock phase and polarity.
Definition: spi.h:64
spi_desc::max_speed_hz
uint32_t max_speed_hz
Definition: spi.h:136
spi_desc::chip_select
uint8_t chip_select
Definition: spi.h:138
spi_desc
Structure holding SPI descriptor.
Definition: spi.h:132
spi_platform_ops::transfer
int32_t(* transfer)(struct spi_desc *, struct spi_msg *, uint32_t)
Definition: spi.h:159
SPI_MODE_1
@ SPI_MODE_1
Definition: spi.h:68
spi_init_param::extra
void * extra
Definition: spi.h:125
spi_platform_ops
Structure holding SPI function pointers that point to the platform specific function.
Definition: spi.h:153
spi_write_and_read
int32_t spi_write_and_read(struct spi_desc *desc, uint8_t *data, uint16_t bytes_number)
Write and read data to/from SPI.
Definition: spi.c:82
spi_init_param::max_speed_hz
uint32_t max_speed_hz
Definition: spi.h:116
spi_msg::tx_buff
uint8_t * tx_buff
Definition: spi.h:92
spi_desc::platform_ops
const struct spi_platform_ops * platform_ops
Definition: spi.h:143
SPI_CPHA
#define SPI_CPHA
Definition: spi.h:53
spi_msg
Definition: spi.h:90
SPI_CPOL
#define SPI_CPOL
Definition: spi.h:54
spi_desc::bit_order
enum spi_bit_order bit_order
Definition: spi.h:142
spi_bit_order
spi_bit_order
SPI configuration for bit order (MSB/LSB).
Definition: spi.h:79
spi_desc
struct spi_desc spi_desc
spi_desc::device_id
uint32_t device_id
Definition: spi.h:134
spi_platform_ops::remove
int32_t(* remove)(struct spi_desc *)
Definition: spi.h:161
spi_msg::bytes_number
uint32_t bytes_number
Definition: spi.h:96
spi_desc::mode
enum spi_mode mode
Definition: spi.h:140
spi_init_param
Structure holding the parameters for SPI initialization.
Definition: spi.h:112
SPI_MODE_0
@ SPI_MODE_0
Definition: spi.h:66
spi_platform_ops::init
int32_t(* init)(struct spi_desc **, const struct spi_init_param *)
Definition: spi.h:155
spi_init_param::mode
enum spi_mode mode
Definition: spi.h:120
SPI_MODE_3
@ SPI_MODE_3
Definition: spi.h:72
SPI_BIT_ORDER_MSB_FIRST
@ SPI_BIT_ORDER_MSB_FIRST
Definition: spi.h:81
spi_platform_ops::write_and_read
int32_t(* write_and_read)(struct spi_desc *, uint8_t *, uint16_t)
Definition: spi.h:157
spi_init_param::platform_ops
const struct spi_platform_ops * platform_ops
Definition: spi.h:123
spi_transfer
int32_t spi_transfer(struct spi_desc *desc, struct spi_msg *msgs, uint32_t len)
Iterate over head list and send all spi messages.
Definition: spi.c:96
SPI_BIT_ORDER_LSB_FIRST
@ SPI_BIT_ORDER_LSB_FIRST
Definition: spi.h:83
spi_init_param::chip_select
uint8_t chip_select
Definition: spi.h:118
spi_desc::extra
void * extra
Definition: spi.h:145
SPI_MODE_2
@ SPI_MODE_2
Definition: spi.h:70
spi_init_param::device_id
uint32_t device_id
Definition: spi.h:114
spi_init
int32_t spi_init(struct spi_desc **desc, const struct spi_init_param *param)
Initialize the SPI communication peripheral.
Definition: spi.c:51
spi_init_param
struct spi_init_param spi_init_param
spi_msg::cs_change
uint8_t cs_change
Definition: spi.h:98
spi_init_param::bit_order
enum spi_bit_order bit_order
Definition: spi.h:122