no-OS
spi_engine.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * @file spi_engine.h
3  * @brief Header file of SPI Engine core features.
4  * @author Sergiu Cuciurean (sergiu.cuciurean@analog.com)
5 ********************************************************************************
6  * Copyright 2019(c) Analog Devices, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of Analog Devices, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
25  * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33 
34 #ifndef SPI_ENGINE_H
35 #define SPI_ENGINE_H
36 
37 /******************************************************************************/
38 /***************************** Include Files **********************************/
39 /******************************************************************************/
40 
41 #include <stdint.h>
42 
43 #include "xilinx_spi.h"
44 #include "spi_engine_private.h"
45 #include "axi_dmac.h"
46 
47 /******************************************************************************/
48 /********************** Macros and Constants Definitions **********************/
49 /******************************************************************************/
50 
51 #define OFFLOAD_DISABLED 0x00
52 #define OFFLOAD_TX_EN NO_OS_BIT(0)
53 #define OFFLOAD_RX_EN NO_OS_BIT(1)
54 #define OFFLOAD_TX_RX_EN OFFLOAD_TX_EN | OFFLOAD_RX_EN
55 
56 #define SPI_ENGINE_MSG_QUEUE_END 0xFFFFFFFF
57 
58 /* Spi engine commands */
59 #define WRITE(no_bytes) ((SPI_ENGINE_INST_TRANSFER << 12) |\
60  (SPI_ENGINE_INSTRUCTION_TRANSFER_W << 8) | no_bytes)
61 
62 #define READ(no_bytes) ((SPI_ENGINE_INST_TRANSFER << 12) |\
63  (SPI_ENGINE_INSTRUCTION_TRANSFER_R << 8) | no_bytes)
64 
65 #define WRITE_READ(no_bytes) ((SPI_ENGINE_INST_TRANSFER << 12) |\
66  (SPI_ENGINE_INSTRUCTION_TRANSFER_RW << 8) | no_bytes)
67 
68 #define SLEEP(time) SPI_ENGINE_CMD_SLEEP(time & 0xF)
69 
70 /* The delay and chip select parmeters are passed over the engine descriptor
71 and will be used inside the function call */
72 #define CS_HIGH SPI_ENGINE_CMD_ASSERT(0x03, 0xFF)
73 #define CS_LOW SPI_ENGINE_CMD_ASSERT(0x03, 0x00)
74 
75 /******************************************************************************/
76 /*************************** Types Declarations *******************************/
77 /******************************************************************************/
78 
85  uint32_t ref_clk_hz;
91  uint32_t cs_delay;
93  uint8_t data_width;
95  uint8_t sdo_idle_state;
96 };
97 
98 
105  uint32_t ref_clk_hz;
115  uint8_t offload_config;
117  uint8_t offload_tx_len;
119  uint8_t offload_rx_len;
123  uint32_t rx_dma_baseaddr;
125  uint32_t tx_dma_baseaddr;
127  uint8_t cs_delay;
129  uint32_t clk_div;
131  uint8_t data_width;
133  uint8_t max_data_width;
135  uint8_t sdo_idle_state;
136 };
137 
138 
145  uint32_t rx_dma_baseaddr;
147  uint32_t tx_dma_baseaddr;
149  uint32_t dma_flags;
151  uint8_t offload_config;
152 };
153 
163  uint32_t *commands;
165  uint32_t no_commands;
167  uint32_t *commands_data;
169  uint32_t tx_addr;
171  uint32_t rx_addr;
172 };
173 
174 /******************************************************************************/
175 /************************ Functions Declarations ******************************/
176 /******************************************************************************/
177 
181 extern const struct no_os_spi_platform_ops xil_spi_ops;
182 
183 /* Write SPI Engine's axi registers */
184 int32_t spi_engine_write(struct spi_engine_desc *desc,
185  uint32_t reg_addr,
186  uint32_t reg_data);
187 
188 /* Read SPI Engine's axi registers */
189 int32_t spi_engine_read(struct spi_engine_desc *desc,
190  uint32_t reg_addr,
191  uint32_t *reg_data);
192 /* Initialize the SPI engine device */
193 int32_t spi_engine_init(struct no_os_spi_desc **desc,
194  const struct no_os_spi_init_param *param);
195 
196 /* Write and read data over SPI using the SPI engine */
197 int32_t spi_engine_write_and_read(struct no_os_spi_desc *desc,
198  uint8_t *data,
199  uint16_t bytes_number);
200 
201 /* Free the resources used by the SPI engine device */
202 int32_t spi_engine_remove(struct no_os_spi_desc *desc);
203 
204 /* Initialize the SPI engine offload module */
205 int32_t spi_engine_offload_init(struct no_os_spi_desc *desc,
206  const struct spi_engine_offload_init_param *param);
207 
208 /* Write and read data over SPI using the offload module */
209 int32_t spi_engine_offload_transfer(struct no_os_spi_desc *desc,
210  struct spi_engine_offload_message msg,
211  uint32_t no_samples);
212 
213 /* Set SPI transfer width */
214 int32_t spi_engine_set_transfer_width(struct no_os_spi_desc *desc,
215  uint8_t data_wdith);
216 
217 /* Set SPI transfer speed */
218 void spi_engine_set_speed(struct no_os_spi_desc *desc,
219  uint32_t speed_hz);
220 
221 #endif // SPI_ENGINE_H
spi_engine_offload_init_param::offload_config
uint8_t offload_config
Definition: spi_engine.h:151
no_os_alloc.h
axi_dmac_init::base
uint32_t base
Definition: axi_dmac.h:129
spi_engine_offload_message::no_commands
uint32_t no_commands
Definition: spi_engine.h:165
spi_engine_msg::cmds
struct spi_engine_cmd_queue * cmds
Definition: spi_engine_private.h:149
spi_engine_cmd_queue
Definition: spi_engine_private.h:140
SPI_ENGINE_REG_SDI_DATA_FIFO
#define SPI_ENGINE_REG_SDI_DATA_FIFO
Definition: spi_engine_private.h:59
spi_engine_cmd_queue::next
struct spi_engine_cmd_queue * next
Definition: spi_engine_private.h:142
spi_engine_desc::ref_clk_hz
uint32_t ref_clk_hz
Definition: spi_engine.h:105
SPI_ENGINE_REG_OFFLOAD_SDO_MEM
#define SPI_ENGINE_REG_OFFLOAD_SDO_MEM(x)
Definition: spi_engine_private.h:108
axi_dmac_init
Definition: axi_dmac.h:127
SPI_ENGINE_CMD_ASSERT
#define SPI_ENGINE_CMD_ASSERT(delay, cs)
Definition: spi_engine_private.h:118
axi_dmac_init::name
const char * name
Definition: axi_dmac.h:128
CS_LOW
#define CS_LOW
Definition: spi_engine.h:73
spi_engine_desc
Structure representing an SPI engine device.
Definition: spi_engine.h:103
spi_engine_init_param::sdo_idle_state
uint8_t sdo_idle_state
Definition: spi_engine.h:95
spi_engine_offload_init
int32_t spi_engine_offload_init(struct no_os_spi_desc *desc, const struct spi_engine_offload_init_param *param)
Initialize the SPI engine's offload module.
Definition: spi_engine.c:763
spi_engine_set_speed
void spi_engine_set_speed(struct no_os_spi_desc *desc, uint32_t speed_hz)
Set SPI engine clock frequency.
Definition: spi_engine.c:148
axi_dmac_transfer_wait_completion
int32_t axi_dmac_transfer_wait_completion(struct axi_dmac *dmac, uint32_t timeout_ms)
Definition: axi_dmac.c:525
spi_engine_init_param::data_width
uint8_t data_width
Definition: spi_engine.h:93
spi_engine_offload_message::commands_data
uint32_t * commands_data
Definition: spi_engine.h:167
no_os_spi_platform_ops
Structure holding SPI function pointers that point to the platform specific function.
Definition: no_os_spi.h:222
spi_engine_write
int32_t spi_engine_write(struct spi_engine_desc *desc, uint32_t reg_addr, uint32_t reg_data)
Write SPI Engine's axi registers.
Definition: spi_engine.c:90
spi_engine_desc::offload_tx_dma
struct axi_dmac * offload_tx_dma
Definition: spi_engine.h:109
SPI_ENGINE_REG_VERSION
#define SPI_ENGINE_REG_VERSION
Definition: spi_engine_private.h:47
no_os_spi_init_param::mode
enum no_os_spi_mode mode
Definition: no_os_spi.h:148
spi_engine_offload_message
Structure representing an offload message.
Definition: spi_engine.h:158
spi_engine_desc::offload_tx_len
uint8_t offload_tx_len
Definition: spi_engine.h:117
spi_engine_desc::cs_delay
uint8_t cs_delay
Definition: spi_engine.h:127
spi_engine_desc::data_width
uint8_t data_width
Definition: spi_engine.h:131
spi_engine_cmd_queue::cmd
uint32_t cmd
Definition: spi_engine_private.h:141
spi_engine_init
int32_t spi_engine_init(struct no_os_spi_desc **desc, const struct no_os_spi_init_param *param)
Initialize the spi engine.
Definition: spi_engine.c:626
spi_engine_msg::tx_buf
uint32_t * tx_buf
Definition: spi_engine_private.h:146
no_os_axi_io.h
Header file of AXI IO.
no_os_calloc
void * no_os_calloc(size_t nitems, size_t size)
Allocate memory and return a pointer to it, set memory to 0.
Definition: chibios_alloc.c:54
SPI_ENGINE_INST_SYNC_SLEEP
#define SPI_ENGINE_INST_SYNC_SLEEP
Definition: spi_engine_private.h:73
SPI_ENGINE_REG_OFFLOAD_CMD_MEM
#define SPI_ENGINE_REG_OFFLOAD_CMD_MEM(x)
Definition: spi_engine_private.h:107
spi_engine_offload_init_param::tx_dma_baseaddr
uint32_t tx_dma_baseaddr
Definition: spi_engine.h:147
WRITE_READ
#define WRITE_READ(no_bytes)
Definition: spi_engine.h:65
SPI_ENGINE_REG_DATA_WIDTH
#define SPI_ENGINE_REG_DATA_WIDTH
Definition: spi_engine_private.h:48
axi_dmac_remove
int32_t axi_dmac_remove(struct axi_dmac *dmac)
Definition: axi_dmac.c:367
spi_engine_remove
int32_t spi_engine_remove(struct no_os_spi_desc *desc)
Free the resources allocated by no_os_spi_init().
Definition: spi_engine.c:905
spi_engine_read
int32_t spi_engine_read(struct spi_engine_desc *desc, uint32_t reg_addr, uint32_t *reg_data)
Read SPI Engine's axi registers.
Definition: spi_engine.c:108
no_os_spi_init_param::chip_select
uint8_t chip_select
Definition: no_os_spi.h:146
axi_dma_transfer
Definition: axi_dmac.h:102
no_os_spi_desc::chip_select
uint8_t chip_select
Definition: no_os_spi.h:200
spi_engine_offload_init
int32_t spi_engine_offload_init(struct no_os_spi_desc *desc, const struct spi_engine_offload_init_param *param)
Initialize the SPI engine's offload module.
Definition: spi_engine.c:763
spi_engine_init_param::type
enum xil_spi_type type
Definition: spi_engine.h:87
CS_HIGH
#define CS_HIGH
Definition: spi_engine.h:72
spi_engine_set_speed
void spi_engine_set_speed(struct no_os_spi_desc *desc, uint32_t speed_hz)
Set SPI engine clock frequency.
Definition: spi_engine.c:148
spi_engine_desc::clk_div
uint32_t clk_div
Definition: spi_engine.h:129
no_os_error.h
Error codes definition.
SPI_ENGINE_CMD_DATA_TRANSFER_LEN
#define SPI_ENGINE_CMD_DATA_TRANSFER_LEN
Definition: spi_engine_private.h:77
NO_OS_DIV_ROUND_UP
#define NO_OS_DIV_ROUND_UP(x, y)
Definition: no_os_util.h:52
SPI_ENGINE_CMD_SLEEP
#define SPI_ENGINE_CMD_SLEEP(delay)
Definition: spi_engine_private.h:126
SPI_ENGINE_CMD_TRANSFER
#define SPI_ENGINE_CMD_TRANSFER(readwrite, n)
Definition: spi_engine_private.h:114
spi_engine_offload_transfer
int32_t spi_engine_offload_transfer(struct no_os_spi_desc *desc, struct spi_engine_offload_message msg, uint32_t no_samples)
Initiate a SPI transfer in offload mode.
Definition: spi_engine.c:809
spi_engine_write_and_read
int32_t spi_engine_write_and_read(struct no_os_spi_desc *desc, uint8_t *data, uint16_t bytes_number)
Write/read on the spi interface.
Definition: spi_engine.c:697
spi_engine_set_transfer_width
int32_t spi_engine_set_transfer_width(struct no_os_spi_desc *desc, uint8_t data_wdith)
Set width of the transfered word over SPI.
Definition: spi_engine.c:127
IRQ_DISABLED
@ IRQ_DISABLED
Definition: axi_dmac.h:79
spi_engine.h
SPI_ENGINE_REG_OFFLOAD_CTRL
#define SPI_ENGINE_REG_OFFLOAD_CTRL(x)
Definition: spi_engine_private.h:104
SPI_ENGINE_REG_OFFLOAD_RESET
#define SPI_ENGINE_REG_OFFLOAD_RESET(x)
Definition: spi_engine_private.h:106
SPI_ENGINE_CMD_CONFIG
#define SPI_ENGINE_CMD_CONFIG(reg, val)
Definition: spi_engine_private.h:122
spi_engine_msg::rx_buf
uint32_t * rx_buf
Definition: spi_engine_private.h:147
axi_dmac.h
Driver for the Analog Devices AXI-DMAC core.
spi_engine_desc::offload_rx_len
uint8_t offload_rx_len
Definition: spi_engine.h:119
spi_engine_msg::length
uint32_t length
Definition: spi_engine_private.h:148
spi_engine_desc::spi_engine_baseaddr
uint32_t spi_engine_baseaddr
Definition: spi_engine.h:121
spi_engine_offload_message::tx_addr
uint32_t tx_addr
Definition: spi_engine.h:169
no_os_spi_desc
Structure holding SPI descriptor.
Definition: no_os_spi.h:192
SPI_ENGINE_REG_SDO_DATA_FIFO
#define SPI_ENGINE_REG_SDO_DATA_FIFO
Definition: spi_engine_private.h:58
axi_dmac::width_src
uint32_t width_src
Definition: axi_dmac.h:118
no_os_axi_io_read
int32_t no_os_axi_io_read(uint32_t base, uint32_t offset, uint32_t *data)
AXI IO Altera specific read function.
Definition: altera_axi_io.c:53
OFFLOAD_TX_EN
#define OFFLOAD_TX_EN
Definition: spi_engine.h:52
spi_engine_desc::offload_config
uint8_t offload_config
Definition: spi_engine.h:115
cyclic_transfer
cyclic_transfer
Definition: axi_dmac.h:97
SPI_ENGINE_CONFIG_SDO_IDLE
#define SPI_ENGINE_CONFIG_SDO_IDLE
Definition: spi_engine_private.h:91
axi_dmac_init::irq_option
enum use_irq irq_option
Definition: axi_dmac.h:130
SPI_ENGINE_REG_CMD_FIFO
#define SPI_ENGINE_REG_CMD_FIFO
Definition: spi_engine_private.h:57
spi_engine_offload_message::commands
uint32_t * commands
Definition: spi_engine.h:163
spi_engine_offload_init_param::rx_dma_baseaddr
uint32_t rx_dma_baseaddr
Definition: spi_engine.h:145
OFFLOAD_DISABLED
#define OFFLOAD_DISABLED
Definition: spi_engine.h:51
spi_engine_desc::type
enum xil_spi_type type
Definition: spi_engine.h:107
no_os_spi_desc::extra
void * extra
Definition: no_os_spi.h:212
SPI_ENGINE_CMD_SYNC
#define SPI_ENGINE_CMD_SYNC(id)
Definition: spi_engine_private.h:131
xil_spi_type
xil_spi_type
Xilinx platform architecture sections.
Definition: xilinx_spi.h:58
SPI_ENGINE_INST_ASSERT
#define SPI_ENGINE_INST_ASSERT
Definition: spi_engine_private.h:71
no_os_malloc
void * no_os_malloc(size_t size)
Allocate memory and return a pointer to it.
Definition: chibios_alloc.c:43
no_os_spi_init_param::max_speed_hz
uint32_t max_speed_hz
Definition: no_os_spi.h:144
OFFLOAD_RX_EN
#define OFFLOAD_RX_EN
Definition: spi_engine.h:53
no_os_field_get
uint32_t no_os_field_get(uint32_t mask, uint32_t word)
NO_OS_BIT
#define NO_OS_BIT(x)
Definition: no_os_util.h:45
spi_engine_desc::max_data_width
uint8_t max_data_width
Definition: spi_engine.h:133
NO
@ NO
Definition: axi_dmac.h:98
no_os_free
void no_os_free(void *ptr)
Deallocate memory previously allocated by a call to no_os_calloc or no_os_malloc.
Definition: chibios_alloc.c:69
spi_engine_offload_init_param::dma_flags
uint32_t dma_flags
Definition: spi_engine.h:149
SPI_ENGINE_CMD_REG_CONFIG
#define SPI_ENGINE_CMD_REG_CONFIG
Definition: spi_engine_private.h:76
DMA_CYCLIC
@ DMA_CYCLIC
Definition: axi_dmac.h:91
axi_dmac_init
int32_t axi_dmac_init(struct axi_dmac **dmac_core, const struct axi_dmac_init *init)
Definition: axi_dmac.c:334
CYCLIC
@ CYCLIC
Definition: axi_dmac.h:99
SPI_ENGINE_INST_TRANSFER
#define SPI_ENGINE_INST_TRANSFER
Definition: spi_engine_private.h:70
NULL
#define NULL
Definition: wrapper.h:64
xil_spi_ops
const struct no_os_spi_platform_ops xil_spi_ops
Spi engine platform specific SPI platform ops structure.
Definition: xilinx_spi.c:453
spi_engine_desc::tx_dma_baseaddr
uint32_t tx_dma_baseaddr
Definition: spi_engine.h:125
spi_engine_init_param
Structure containing the init parameters needed by the SPI engine.
Definition: spi_engine.h:83
SPI_ENGINE_INST_CONFIG
#define SPI_ENGINE_INST_CONFIG
Definition: spi_engine_private.h:72
spi_eng_platform_ops
const struct no_os_spi_platform_ops spi_eng_platform_ops
Spi engine platform specific SPI platform ops structure.
Definition: spi_engine.c:61
SPI_ENGINE_REG_DATA_WIDTH_MSK
#define SPI_ENGINE_REG_DATA_WIDTH_MSK
Definition: spi_engine_private.h:64
spi_engine_offload_transfer
int32_t spi_engine_offload_transfer(struct no_os_spi_desc *desc, struct spi_engine_offload_message msg, uint32_t no_samples)
Initiate a SPI transfer in offload mode.
Definition: spi_engine.c:809
no_os_axi_io_write
int32_t no_os_axi_io_write(uint32_t base, uint32_t offset, uint32_t data)
AXI IO Altera specific write function.
Definition: altera_axi_io.c:67
spi_engine_set_transfer_width
int32_t spi_engine_set_transfer_width(struct no_os_spi_desc *desc, uint8_t data_wdith)
Set width of the transfered word over SPI.
Definition: spi_engine.c:127
spi_engine_offload_init_param
Structure containing the init parameters needed by the offload module.
Definition: spi_engine.h:143
spi_engine_init_param::spi_engine_baseaddr
uint32_t spi_engine_baseaddr
Definition: spi_engine.h:89
SPI_ENGINE_REG_RESET
#define SPI_ENGINE_REG_RESET
Definition: spi_engine_private.h:49
no_os_spi_desc::max_speed_hz
uint32_t max_speed_hz
Definition: no_os_spi.h:198
spi_engine_init
int32_t spi_engine_init(struct no_os_spi_desc **desc, const struct no_os_spi_init_param *param)
Initialize the spi engine.
Definition: spi_engine.c:626
axi_dmac_transfer_start
int32_t axi_dmac_transfer_start(struct axi_dmac *dmac, struct axi_dma_transfer *dma_transfer)
Definition: axi_dmac.c:385
SPI_ENGINE_REG_SYNC_ID
#define SPI_ENGINE_REG_SYNC_ID
Definition: spi_engine_private.h:53
no_os_util.h
Header file of utility functions.
no_os_spi_platform_ops::init
int32_t(* init)(struct no_os_spi_desc **, const struct no_os_spi_init_param *)
Definition: no_os_spi.h:224
spi_engine_desc::rx_dma_baseaddr
uint32_t rx_dma_baseaddr
Definition: spi_engine.h:123
SPI_ENGINE_CMD_REG_CLK_DIV
#define SPI_ENGINE_CMD_REG_CLK_DIV
Definition: spi_engine_private.h:75
spi_engine_init_param::ref_clk_hz
uint32_t ref_clk_hz
Definition: spi_engine.h:85
no_os_spi_init_param::extra
void * extra
Definition: no_os_spi.h:158
spi_engine_offload_message::rx_addr
uint32_t rx_addr
Definition: spi_engine.h:171
spi_engine_private.h
xilinx_spi.h
no_os_spi_desc::mode
enum no_os_spi_mode mode
Definition: no_os_spi.h:202
spi_engine_msg
Definition: spi_engine_private.h:145
spi_engine_remove
int32_t spi_engine_remove(struct no_os_spi_desc *desc)
Free the resources allocated by no_os_spi_init().
Definition: spi_engine.c:905
spi_engine_read
int32_t spi_engine_read(struct spi_engine_desc *desc, uint32_t reg_addr, uint32_t *reg_data)
Read SPI Engine's axi registers.
Definition: spi_engine.c:108
axi_dma_transfer::size
uint32_t size
Definition: axi_dmac.h:103
spi_engine_desc::cyclic
enum cyclic_transfer cyclic
Definition: spi_engine.h:113
spi_engine_desc::sdo_idle_state
uint8_t sdo_idle_state
Definition: spi_engine.h:135
axi_dmac
Definition: axi_dmac.h:110
spi_engine_init_param::cs_delay
uint32_t cs_delay
Definition: spi_engine.h:91
spi_engine_write
int32_t spi_engine_write(struct spi_engine_desc *desc, uint32_t reg_addr, uint32_t reg_data)
Write SPI Engine's axi registers.
Definition: spi_engine.c:90
no_os_spi_init_param
Structure holding the parameters for SPI initialization.
Definition: no_os_spi.h:140
spi_engine_desc::offload_rx_dma
struct axi_dmac * offload_rx_dma
Definition: spi_engine.h:111
spi_engine_write_and_read
int32_t spi_engine_write_and_read(struct no_os_spi_desc *desc, uint8_t *data, uint16_t bytes_number)
Write/read on the spi interface.
Definition: spi_engine.c:697