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  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * - Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  * - Neither the name of Analog Devices, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  * - The use of this software may or may not infringe the patent rights
22  * of one or more patent holders. This license does not release you
23  * from the requirement that you obtain separate licenses from these
24  * patent holders to use this software.
25  * - Use of the software either in source or binary form, must be run
26  * on or directly connected to an Analog Devices Inc. component.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31  * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39 
40 #ifndef SPI_ENGINE_H
41 #define SPI_ENGINE_H
42 
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 
47 #include <stdint.h>
48 
49 #include "xilinx_spi.h"
50 #include "spi_engine_private.h"
51 #include "axi_dmac.h"
52 
53 /******************************************************************************/
54 /********************** Macros and Constants Definitions **********************/
55 /******************************************************************************/
56 
57 #define OFFLOAD_DISABLED 0x00
58 #define OFFLOAD_TX_EN NO_OS_BIT(0)
59 #define OFFLOAD_RX_EN NO_OS_BIT(1)
60 #define OFFLOAD_TX_RX_EN OFFLOAD_TX_EN | OFFLOAD_RX_EN
61 
62 #define SPI_ENGINE_MSG_QUEUE_END 0xFFFFFFFF
63 
64 /* Spi engine commands */
65 #define WRITE(no_bytes) ((SPI_ENGINE_INST_TRANSFER << 12) |\
66  (SPI_ENGINE_INSTRUCTION_TRANSFER_W << 8) | no_bytes)
67 
68 #define READ(no_bytes) ((SPI_ENGINE_INST_TRANSFER << 12) |\
69  (SPI_ENGINE_INSTRUCTION_TRANSFER_R << 8) | no_bytes)
70 
71 #define WRITE_READ(no_bytes) ((SPI_ENGINE_INST_TRANSFER << 12) |\
72  (SPI_ENGINE_INSTRUCTION_TRANSFER_RW << 8) | no_bytes)
73 
74 #define SLEEP(time) SPI_ENGINE_CMD_SLEEP(time & 0xF)
75 
76 /* The delay and chip select parmeters are passed over the engine descriptor
77 and will be used inside the function call */
78 #define CS_HIGH SPI_ENGINE_CMD_ASSERT(0x03, 0xFF)
79 #define CS_LOW SPI_ENGINE_CMD_ASSERT(0x03, 0x00)
80 
81 /******************************************************************************/
82 /*************************** Types Declarations *******************************/
83 /******************************************************************************/
84 
91  uint32_t ref_clk_hz;
97  uint32_t cs_delay;
99  uint8_t data_width;
100 };
101 
102 
109  uint32_t ref_clk_hz;
119  uint8_t offload_config;
121  uint8_t offload_tx_len;
123  uint8_t offload_rx_len;
127  uint32_t rx_dma_baseaddr;
129  uint32_t tx_dma_baseaddr;
131  uint8_t cs_delay;
133  uint32_t clk_div;
135  uint8_t data_width;
137  uint8_t max_data_width;
138 };
139 
140 
147  uint32_t rx_dma_baseaddr;
149  uint32_t tx_dma_baseaddr;
151  uint32_t *dma_flags;
153  uint8_t offload_config;
154 };
155 
165  uint32_t *commands;
167  uint32_t no_commands;
169  uint32_t *commands_data;
171  uint32_t tx_addr;
173  uint32_t rx_addr;
174 };
175 
176 /******************************************************************************/
177 /************************ Functions Declarations ******************************/
178 /******************************************************************************/
179 
183 extern const struct no_os_spi_platform_ops xil_spi_ops;
184 
185 /* Write SPI Engine's axi registers */
186 int32_t spi_engine_write(struct spi_engine_desc *desc,
187  uint32_t reg_addr,
188  uint32_t reg_data);
189 
190 /* Read SPI Engine's axi registers */
191 int32_t spi_engine_read(struct spi_engine_desc *desc,
192  uint32_t reg_addr,
193  uint32_t *reg_data);
194 /* Initialize the SPI engine device */
195 int32_t spi_engine_init(struct no_os_spi_desc **desc,
196  const struct no_os_spi_init_param *param);
197 
198 /* Write and read data over SPI using the SPI engine */
199 int32_t spi_engine_write_and_read(struct no_os_spi_desc *desc,
200  uint8_t *data,
201  uint16_t bytes_number);
202 
203 /* Free the resources used by the SPI engine device */
204 int32_t spi_engine_remove(struct no_os_spi_desc *desc);
205 
206 /* Initialize the SPI engine offload module */
207 int32_t spi_engine_offload_init(struct no_os_spi_desc *desc,
208  const struct spi_engine_offload_init_param *param);
209 
210 /* Write and read data over SPI using the offload module */
211 int32_t spi_engine_offload_transfer(struct no_os_spi_desc *desc,
212  struct spi_engine_offload_message msg,
213  uint32_t no_samples);
214 
215 /* Set SPI transfer width */
216 int32_t spi_engine_set_transfer_width(struct no_os_spi_desc *desc,
217  uint8_t data_wdith);
218 
219 /* Set SPI transfer speed */
220 void spi_engine_set_speed(struct no_os_spi_desc *desc,
221  uint32_t speed_hz);
222 
223 #endif // SPI_ENGINE_H
spi_engine_offload_init_param::offload_config
uint8_t offload_config
Definition: spi_engine.h:153
axi_dmac_init::base
uint32_t base
Definition: axi_dmac.h:131
spi_engine_offload_message::no_commands
uint32_t no_commands
Definition: spi_engine.h:167
spi_engine_msg::cmds
struct spi_engine_cmd_queue * cmds
Definition: spi_engine_private.h:150
spi_engine_cmd_queue
Definition: spi_engine_private.h:141
SPI_ENGINE_REG_SDI_DATA_FIFO
#define SPI_ENGINE_REG_SDI_DATA_FIFO
Definition: spi_engine_private.h:65
spi_engine_cmd_queue::next
struct spi_engine_cmd_queue * next
Definition: spi_engine_private.h:143
spi_engine_desc::ref_clk_hz
uint32_t ref_clk_hz
Definition: spi_engine.h:109
SPI_ENGINE_REG_OFFLOAD_SDO_MEM
#define SPI_ENGINE_REG_OFFLOAD_SDO_MEM(x)
Definition: spi_engine_private.h:109
axi_dmac_init
Definition: axi_dmac.h:129
SPI_ENGINE_CMD_ASSERT
#define SPI_ENGINE_CMD_ASSERT(delay, cs)
Definition: spi_engine_private.h:119
axi_dmac_init::name
const char * name
Definition: axi_dmac.h:130
CS_LOW
#define CS_LOW
Definition: spi_engine.h:79
spi_engine_desc
Structure representing an SPI engine device.
Definition: spi_engine.h:107
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:757
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:150
axi_dmac_transfer_wait_completion
int32_t axi_dmac_transfer_wait_completion(struct axi_dmac *dmac, uint32_t timeout_ms)
Definition: axi_dmac.c:506
spi_engine_init_param::data_width
uint8_t data_width
Definition: spi_engine.h:99
spi_engine_offload_message::commands_data
uint32_t * commands_data
Definition: spi_engine.h:169
no_os_spi_platform_ops
Structure holding SPI function pointers that point to the platform specific function.
Definition: no_os_spi.h:166
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:92
spi_engine_desc::offload_tx_dma
struct axi_dmac * offload_tx_dma
Definition: spi_engine.h:113
SPI_ENGINE_REG_VERSION
#define SPI_ENGINE_REG_VERSION
Definition: spi_engine_private.h:53
no_os_spi_init_param::mode
enum no_os_spi_mode mode
Definition: no_os_spi.h:129
spi_engine_offload_message
Structure representing an offload message.
Definition: spi_engine.h:160
spi_engine_desc::offload_tx_len
uint8_t offload_tx_len
Definition: spi_engine.h:121
spi_engine_desc::cs_delay
uint8_t cs_delay
Definition: spi_engine.h:131
spi_engine_desc::data_width
uint8_t data_width
Definition: spi_engine.h:135
spi_engine_cmd_queue::cmd
uint32_t cmd
Definition: spi_engine_private.h:142
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:622
spi_engine_msg::tx_buf
uint32_t * tx_buf
Definition: spi_engine_private.h:147
no_os_axi_io.h
Header file of AXI IO.
SPI_ENGINE_INST_SYNC_SLEEP
#define SPI_ENGINE_INST_SYNC_SLEEP
Definition: spi_engine_private.h:75
SPI_ENGINE_REG_OFFLOAD_CMD_MEM
#define SPI_ENGINE_REG_OFFLOAD_CMD_MEM(x)
Definition: spi_engine_private.h:108
spi_engine_offload_init_param::tx_dma_baseaddr
uint32_t tx_dma_baseaddr
Definition: spi_engine.h:149
WRITE_READ
#define WRITE_READ(no_bytes)
Definition: spi_engine.h:71
SPI_ENGINE_REG_DATA_WIDTH
#define SPI_ENGINE_REG_DATA_WIDTH
Definition: spi_engine_private.h:54
axi_dmac_remove
int32_t axi_dmac_remove(struct axi_dmac *dmac)
Definition: axi_dmac.c:360
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:893
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:110
no_os_spi_init_param::chip_select
uint8_t chip_select
Definition: no_os_spi.h:127
axi_dma_transfer
Definition: axi_dmac.h:106
no_os_spi_desc::chip_select
uint8_t chip_select
Definition: no_os_spi.h:149
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:757
spi_engine_init_param::type
enum xil_spi_type type
Definition: spi_engine.h:93
spi_engine_offload_init_param::dma_flags
uint32_t * dma_flags
Definition: spi_engine.h:151
CS_HIGH
#define CS_HIGH
Definition: spi_engine.h:78
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:150
spi_engine_desc::clk_div
uint32_t clk_div
Definition: spi_engine.h:133
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:79
SPI_ENGINE_CMD_SLEEP
#define SPI_ENGINE_CMD_SLEEP(delay)
Definition: spi_engine_private.h:127
SPI_ENGINE_CMD_TRANSFER
#define SPI_ENGINE_CMD_TRANSFER(readwrite, n)
Definition: spi_engine_private.h:115
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:803
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:691
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:129
spi_engine.h
SPI_ENGINE_REG_OFFLOAD_CTRL
#define SPI_ENGINE_REG_OFFLOAD_CTRL(x)
Definition: spi_engine_private.h:105
SPI_ENGINE_REG_OFFLOAD_RESET
#define SPI_ENGINE_REG_OFFLOAD_RESET(x)
Definition: spi_engine_private.h:107
SPI_ENGINE_CMD_CONFIG
#define SPI_ENGINE_CMD_CONFIG(reg, val)
Definition: spi_engine_private.h:123
spi_engine_msg::rx_buf
uint32_t * rx_buf
Definition: spi_engine_private.h:148
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:123
spi_engine_msg::length
uint32_t length
Definition: spi_engine_private.h:149
spi_engine_desc::spi_engine_baseaddr
uint32_t spi_engine_baseaddr
Definition: spi_engine.h:125
spi_engine_offload_message::tx_addr
uint32_t tx_addr
Definition: spi_engine.h:171
no_os_spi_desc
Structure holding SPI descriptor.
Definition: no_os_spi.h:143
SPI_ENGINE_REG_SDO_DATA_FIFO
#define SPI_ENGINE_REG_SDO_DATA_FIFO
Definition: spi_engine_private.h:64
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:59
OFFLOAD_TX_EN
#define OFFLOAD_TX_EN
Definition: spi_engine.h:58
spi_engine_desc::offload_config
uint8_t offload_config
Definition: spi_engine.h:119
cyclic_transfer
cyclic_transfer
Definition: axi_dmac.h:101
SPI_ENGINE_REG_CMD_FIFO
#define SPI_ENGINE_REG_CMD_FIFO
Definition: spi_engine_private.h:63
spi_engine_offload_message::commands
uint32_t * commands
Definition: spi_engine.h:165
spi_engine_offload_init_param::rx_dma_baseaddr
uint32_t rx_dma_baseaddr
Definition: spi_engine.h:147
OFFLOAD_DISABLED
#define OFFLOAD_DISABLED
Definition: spi_engine.h:57
spi_engine_desc::type
enum xil_spi_type type
Definition: spi_engine.h:111
no_os_spi_desc::extra
void * extra
Definition: no_os_spi.h:156
SPI_ENGINE_CMD_SYNC
#define SPI_ENGINE_CMD_SYNC(id)
Definition: spi_engine_private.h:132
xil_spi_type
xil_spi_type
Xilinx platform architecture sections.
Definition: xilinx_spi.h:64
SPI_ENGINE_INST_ASSERT
#define SPI_ENGINE_INST_ASSERT
Definition: spi_engine_private.h:73
no_os_spi_init_param::max_speed_hz
uint32_t max_speed_hz
Definition: no_os_spi.h:125
OFFLOAD_RX_EN
#define OFFLOAD_RX_EN
Definition: spi_engine.h:59
NO_OS_BIT
#define NO_OS_BIT(x)
Definition: no_os_util.h:51
spi_engine_desc::max_data_width
uint8_t max_data_width
Definition: spi_engine.h:137
NO
@ NO
Definition: axi_dmac.h:102
SPI_ENGINE_CMD_REG_CONFIG
#define SPI_ENGINE_CMD_REG_CONFIG
Definition: spi_engine_private.h:78
DMA_CYCLIC
@ DMA_CYCLIC
Definition: axi_dmac.h:95
axi_dmac_init
int32_t axi_dmac_init(struct axi_dmac **dmac_core, const struct axi_dmac_init *init)
Definition: axi_dmac.c:331
CYCLIC
@ CYCLIC
Definition: axi_dmac.h:103
SPI_ENGINE_INST_TRANSFER
#define SPI_ENGINE_INST_TRANSFER
Definition: spi_engine_private.h:72
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:456
spi_engine_desc::tx_dma_baseaddr
uint32_t tx_dma_baseaddr
Definition: spi_engine.h:129
spi_engine_init_param
Structure containing the init parameters needed by the SPI engine.
Definition: spi_engine.h:89
SPI_ENGINE_INST_CONFIG
#define SPI_ENGINE_INST_CONFIG
Definition: spi_engine_private.h:74
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:63
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:803
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:73
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:129
spi_engine_offload_init_param
Structure containing the init parameters needed by the offload module.
Definition: spi_engine.h:145
spi_engine_init_param::spi_engine_baseaddr
uint32_t spi_engine_baseaddr
Definition: spi_engine.h:95
SPI_ENGINE_REG_RESET
#define SPI_ENGINE_REG_RESET
Definition: spi_engine_private.h:55
no_os_spi_desc::max_speed_hz
uint32_t max_speed_hz
Definition: no_os_spi.h:147
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:622
axi_dmac_transfer_start
int32_t axi_dmac_transfer_start(struct axi_dmac *dmac, struct axi_dma_transfer *dma_transfer)
Definition: axi_dmac.c:378
SPI_ENGINE_REG_SYNC_ID
#define SPI_ENGINE_REG_SYNC_ID
Definition: spi_engine_private.h:59
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:168
spi_engine_desc::rx_dma_baseaddr
uint32_t rx_dma_baseaddr
Definition: spi_engine.h:127
SPI_ENGINE_CMD_REG_CLK_DIV
#define SPI_ENGINE_CMD_REG_CLK_DIV
Definition: spi_engine_private.h:77
spi_engine_init_param::ref_clk_hz
uint32_t ref_clk_hz
Definition: spi_engine.h:91
no_os_spi_init_param::extra
void * extra
Definition: no_os_spi.h:134
spi_engine_offload_message::rx_addr
uint32_t rx_addr
Definition: spi_engine.h:173
spi_engine_private.h
xilinx_spi.h
no_os_spi_desc::mode
enum no_os_spi_mode mode
Definition: no_os_spi.h:151
spi_engine_msg
Definition: spi_engine_private.h:146
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:893
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:110
axi_dma_transfer::size
uint32_t size
Definition: axi_dmac.h:107
spi_engine_desc::cyclic
enum cyclic_transfer cyclic
Definition: spi_engine.h:117
axi_dmac
Definition: axi_dmac.h:114
spi_engine_init_param::cs_delay
uint32_t cs_delay
Definition: spi_engine.h:97
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:92
no_os_spi_init_param
Structure holding the parameters for SPI initialization.
Definition: no_os_spi.h:121
spi_engine_desc::offload_rx_dma
struct axi_dmac * offload_rx_dma
Definition: spi_engine.h:115
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:691