SmartSnippets DA1459x SDK
ad_spi.h
Go to the documentation of this file.
1 
44 /*
45  *
46  * Terminology:
47  * Adapter: A middleware abstraction layer for using a controller. It provides the following services:
48  * - Arbitration in accessing the controller (between multiple masters, multiple tasks)
49  * - Configuration of the controller
50  * - Block sleep while the controller is in use
51  * Controller: Top level view of SPI peripheral, including the complete configuration needed for being fully functional (IOs, DMAs, Driver configuration)
52  * Driver: The Low Level Driver associated with the controller
53  * SPI: The controller name (e.g SPI1, SPI2)
54  *
55  * API example usage:
56  * ad_spi_init(): Called by the system on power up for initializing the adapter. It should NOT be called by the application
57  * ad_spi_io_config(): Called by the application for configuring the controller interface before powering up the external connected devices (e.g sensors)
58  *
59  * ad_spi_open(); Called by the application for starting a transaction. The sleep is blocked until ad_spi_close() is called. No other task or master can start a new transaction using the same controller instance
60  * ad_spi_write(); Blocking write - caller task will block until the resource becomes available
61  * ad_spi_read(); Blocking read - caller task will block until the resource becomes available
62  * ad_spi_reconfig();
63  * ad_spi_write_async(); Non blocking write - caller task should retry until function returns no error. It will then be notified when transaction is completed
64  * ad_spi_read_async(); Non blocking read - caller task should retry until function returns no error. It will then be notified when transaction is completed
65  * ad_spi_close(); Called by the application for releasing the resource. System is allowed to go to sleep (if no other module blocks sleep)
66  *
67  *
68  *
69  * */
70 
71 #ifndef AD_SPI_H_
72 #define AD_SPI_H_
73 
74 #if dg_configSPI_ADAPTER
75 
76 #include "ad.h"
77 #include "hw_spi.h"
78 #include "hw_gpio.h"
79 #include "osal.h"
80 #include "resmgmt.h"
81 #include "sdk_defs.h"
82 #include <stdbool.h>
83 #include <stdint.h>
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
95 #ifndef CONFIG_SPI_USE_ASYNC_TRANSACTIONS
96 # define CONFIG_SPI_USE_ASYNC_TRANSACTIONS (1)
97 #endif
98 
107 #ifndef CONFIG_SPI_USE_SYNC_TRANSACTIONS
108 # define CONFIG_SPI_USE_SYNC_TRANSACTIONS (1)
109 #endif
110 
111 #if (CONFIG_SPI_USE_SYNC_TRANSACTIONS == 0) && (CONFIG_SPI_USE_ASYNC_TRANSACTIONS == 0)
112 #error "At least one macro CONFIG_SPI_USE_SYNC_TRANSACTIONS or CONFIG_SPI_USE_ASYNC_TRANSACTIONS must be set"
113 #endif
114 
115 /*
116  * Data types definitions section
117  */
118 
122 typedef void *ad_spi_handle_t;
123 
133 typedef struct {
137  const uint8_t cs_cnt;
140 
141 
148 typedef struct {
151 
158 typedef struct {
159  const HW_SPI_ID id;
163 
164 
168 typedef enum {
169  AD_SPI_ERROR_NONE = 0,
170  AD_SPI_ERROR_HANDLE_INVALID = -1,
171  AD_SPI_ERROR_ADAPTER_NOT_OPEN = -2,
172  AD_SPI_ERROR_CONFIG_SPI_ROLE_INVALID = -3,
173  AD_SPI_ERROR_CONFIG_DMA_CHANNEL_INVALID = -4,
174  AD_SPI_ERROR_CONFIG_SPI_CS_INVALID = -5,
175  AD_SPI_ERROR_TRANSF_IN_PROGRESS = -6,
176  AD_SPI_ERROR_NO_SPI_CLK_PIN = -7,
177  AD_SPI_ERROR_DRIVER_CLOCK_DIV_INVALID = -8,
178  AD_SPI_ERROR_DRIVER_CONF_INVALID = -9,
179  AD_SPI_ERROR_IO_CFG_INVALID = -10,
180  AD_SPI_ERROR_CONFIG_RX_TX_TL_INVALID = -11,
181 } AD_SPI_ERROR;
182 
183 /*
184  * Adapters mandatory function prototypes section
185  *
186  * This section includes the prototypes of the functions
187  * which are mandatory for all adapters
188  */
189 
190 
191 
198 void ad_spi_init(void);
199 
200 
216 
232 int ad_spi_reconfig(ad_spi_handle_t handle, const ad_spi_driver_conf_t *drv_conf);
233 
251 int ad_spi_close(ad_spi_handle_t handle, bool force);
252 
266 int ad_spi_io_config (HW_SPI_ID id, const ad_spi_io_conf_t *io, AD_IO_CONF_STATE state);
267 
268 
269 /*
270  * Adapter specific function prototypes section
271  *
272  * This section includes the prototypes of the functions
273  * which are specific to each adapter.
274  * An example function prototype is shown below
275  */
276 
277 typedef void (*ad_spi_user_cb)(void *user_data, uint16_t transferred);
278 
279 #if (CONFIG_SPI_USE_SYNC_TRANSACTIONS == 1)
280 
353 int ad_spi_write(ad_spi_handle_t handle, const uint8_t *wbuf, size_t wlen);
354 
372 int ad_spi_read(ad_spi_handle_t handle, uint8_t *rbuf, size_t rlen);
373 
392 int ad_spi_write_read(ad_spi_handle_t handle, const uint8_t *wbuf, uint8_t *rbuf, size_t len);
393 
394 #endif /* CONFIG_SPI_USE_SYNC_TRANSACTIONS */
395 
396 
406 void ad_spi_activate_cs(ad_spi_handle_t handle, uint8_t csid);
407 
417 
427 
428 
429 #if (CONFIG_SPI_USE_ASYNC_TRANSACTIONS == 1)
430 
449 int ad_spi_write_async(ad_spi_handle_t handle, const uint8_t *wbuf, size_t wlen, ad_spi_user_cb cb,
450  void *user_data);
451 
469 int ad_spi_read_async(ad_spi_handle_t handle, const uint8_t *rbuf, size_t rlen, ad_spi_user_cb cb,
470  void *user_data);
471 
500 int ad_spi_write_read_async(ad_spi_handle_t handle, const uint8_t *wbuf, size_t wlen,
501  uint8_t *rbuf, size_t rlen, ad_spi_user_cb cb, void *user_data);
502 
503 #endif /* CONFIG_SPI_USE_ASYNC_TRANSACTIONS */
504 
505 #ifdef __cplusplus
506 }
507 #endif
508 
509 #endif /* dg_configSPI_ADAPTER */
510 
511 #endif /* AD_SPI_H_ */
512 
hw_spi_config_t
SPI configuration.
Definition: hw_spi.h:326
ad_spi_driver_conf_t::spi
hw_spi_config_t spi
Definition: ad_spi.h:149
ad_spi_io_conf_t
SPI I/O configuration.
Definition: ad_spi.h:133
ad_spi_io_conf_t::spi_clk
ad_io_conf_t spi_clk
Definition: ad_spi.h:135
ad_spi_write_read_async
int ad_spi_write_read_async(ad_spi_handle_t handle, const uint8_t *wbuf, size_t wlen, uint8_t *rbuf, size_t rlen, ad_spi_user_cb cb, void *user_data)
Perform asynchronous SPI write and then read transaction. It can be used as write command and read bu...
ad_spi_io_conf_t::cs_cnt
const uint8_t cs_cnt
Definition: ad_spi.h:137
ad_spi_activate_cs
void ad_spi_activate_cs(ad_spi_handle_t handle, uint8_t csid)
Activate chip select for a specific device.
ad_spi_io_conf_t::spi_do
ad_io_conf_t spi_do
Definition: ad_spi.h:134
sdk_defs.h
Central include header file with platform definitions.
ad_spi_write_async
int ad_spi_write_async(ad_spi_handle_t handle, const uint8_t *wbuf, size_t wlen, ad_spi_user_cb cb, void *user_data)
Perform a non blocking write transaction.
ad_spi_controller_conf_t
SPI controller configuration.
Definition: ad_spi.h:158
ad_spi_controller_conf_t::drv
const ad_spi_driver_conf_t * drv
Definition: ad_spi.h:161
ad_spi_read
int ad_spi_read(ad_spi_handle_t handle, uint8_t *rbuf, size_t rlen)
Perform a blocking read transaction.
hw_spi.h
Serial Peripheral Interface (SPI) Low Level Driver (LLD) API definition.
ad_spi_init
void ad_spi_init(void)
Initialize adapter.
resmgmt.h
Resource management API.
ad_spi_reconfig
int ad_spi_reconfig(ad_spi_handle_t handle, const ad_spi_driver_conf_t *drv_conf)
Reconfigure SPI controller.
ad_spi_close
int ad_spi_close(ad_spi_handle_t handle, bool force)
Close SPI controller.
osal.h
OS abstraction layer API.
ad_spi_driver_conf_t
SPI driver configuration.
Definition: ad_spi.h:148
ad.h
Adapters shared definitions.
AD_IO_CONF_STATE
AD_IO_CONF_STATE
Adapters IO configuration state.
Definition: ad.h:65
ad_spi_controller_conf_t::io
const ad_spi_io_conf_t * io
Definition: ad_spi.h:160
ad_spi_io_config
int ad_spi_io_config(HW_SPI_ID id, const ad_spi_io_conf_t *io, AD_IO_CONF_STATE state)
Initialize controller pins to on / off io configuration.
ad_spi_io_conf_t::spi_di
ad_io_conf_t spi_di
Definition: ad_spi.h:136
ad_spi_write
int ad_spi_write(ad_spi_handle_t handle, const uint8_t *wbuf, size_t wlen)
Perform a blocking write transaction.
ad_spi_handle_t
void * ad_spi_handle_t
SPI Handle returned by ad_spi_open()
Definition: ad_spi.h:122
hw_gpio.h
Definition of API for the GPIO Low Level Driver.
ad_spi_controller_conf_t::id
const HW_SPI_ID id
Definition: ad_spi.h:159
ad_spi_open
ad_spi_handle_t ad_spi_open(const ad_spi_controller_conf_t *conf)
Open SPI controller.
ad_spi_deactivate_cs
void ad_spi_deactivate_cs(ad_spi_handle_t handle)
Deactivate chip select for a specific device.
ad_spi_io_conf_t::spi_cs
ad_io_conf_t * spi_cs
Definition: ad_spi.h:138
ad_spi_deactivate_cs_when_spi_done
void ad_spi_deactivate_cs_when_spi_done(ad_spi_handle_t handle)
Wait for SPI interface to be idle and deactivate chip select.
ad_io_conf_t
Adapters IO configuration.
Definition: ad.h:104
ad_spi_write_read
int ad_spi_write_read(ad_spi_handle_t handle, const uint8_t *wbuf, uint8_t *rbuf, size_t len)
Perform a blocking write and read transaction.
ad_spi_read_async
int ad_spi_read_async(ad_spi_handle_t handle, const uint8_t *rbuf, size_t rlen, ad_spi_user_cb cb, void *user_data)
Perform a blocking read transaction.
AD_SPI_ERROR
AD_SPI_ERROR
enum with return values of API calls
Definition: ad_spi.h:168