WE310F5  39.00.000
m2mb_spi.h
Go to the documentation of this file.
1 /*===============================================================================================*/
2 /* >>> Copyright (C) Telit Communications S.p.A. Italy All Rights Reserved. <<< */
28 #ifndef M2M_M2MB_SPI_API_H
29 #define M2M_M2MB_SPI_API_H
30 
31 /* Global declarations ==========================================================================*/
32 #ifndef NULL
33 #define NULL 0
34 #endif
35 
36 //Uncomment below line to enable all the debug prints for SPI
37 //#define WE310_SPI_DEBUG
38 
39 #ifdef WE310_SPI_DEBUG
40 extern UINT8 dbg_log_buf[1024];
41 extern UINT16 dbg_log_cnt;
42 #define DBG_LOG(val) if(dbg_log_cnt<1023)dbg_log_buf[dbg_log_cnt++] = val;
43 #endif
44 
45 
46 /* Global enums ==============================================================================*/
47 
49 {
50  M2MB_SPI_IOCTL_SET_CFG = 0, /* set whole spi device configuration */
51  M2MB_SPI_IOCTL_GET_CFG, /* get whole spi device configuration */
52  M2MB_SPI_IOCTL_SET_SHIFT_MODE, /* set spi device shift mode */
53  M2MB_SPI_IOCTL_GET_SHIFT_MODE, /* get spi device shift mode */
54  M2MB_SPI_IOCTL_SET_CS_POLARITY, /* set spi device CS polarity */
55  M2MB_SPI_IOCTL_GET_CS_POLARITY, /* get spi device CS polarity */
56  M2MB_SPI_IOCTL_SET_CS_MODE, /* set spi device CS mode */
57  M2MB_SPI_IOCTL_GET_CS_MODE, /* get spi device CS mode */
58  M2MB_SPI_IOCTL_SET_BYTE_ORDER, /* set spi device endianness */
59  M2MB_SPI_IOCTL_GET_BYTE_ORDER, /* get spi device endianness */
60  M2MB_SPI_IOCTL_SET_CALLBACK_FN, /* set spi device callback function */
61  M2MB_SPI_IOCTL_GET_CALLBACK_FN, /* get spi device callback function */
62  M2MB_SPI_IOCTL_SET_CALLBACK_CTXT, /* set spi device callback context */
63  M2MB_SPI_IOCTL_GET_CALLBACK_CTXT, /* get spi device callback context */
64  M2MB_SPI_IOCTL_SET_CLK_FREQ_HZ, /* set spi device clock frequency in Hz */
65  M2MB_SPI_IOCTL_GET_CLK_FREQ_HZ, /* get spi device clock frequency in Hz */
66  M2MB_SPI_IOCTL_SET_BITS_PER_WORD, /* set spi device bits per word */
67  M2MB_SPI_IOCTL_GET_BITS_PER_WORD, /* get spi device bits per word */
68  M2MB_SPI_IOCTL_SET_CS_CLK_DELAY_CYCLES, /* set spi device cs_clk_delay_cycles */
69  M2MB_SPI_IOCTL_GET_CS_CLK_DELAY_CYCLES, /* get spi device cs_clk_delay_cycles */
70  M2MB_SPI_IOCTL_SET_INTER_WORD_DELAY_CYCLES, /* set spi device inter_word_delay_cycles */
71  M2MB_SPI_IOCTL_GET_INTER_WORD_DELAY_CYCLES, /* get spi device inter_word_delay_cycles */
72  M2MB_SPI_IOCTL_SET_LOOPBACK_MODE, /* set spi device loopback mode */
73  M2MB_SPI_IOCTL_GET_LOOPBACK_MODE, /* get spi device loopback mode */
74  M2MB_SPI_IOCTL_GET_WEITE_COMPLETE, /* get spi device write complete status */
75  M2MB_SPI_IOCTL_NOF_REQ /* number of m2mb ioctl requests */
76 };
77 
78 /* Global typedefs ==============================================================================*/
79 
80 /* SPI phase type.
81  This type defines the clock phase that the client can set in the
82  SPI configuration.
83 */
84 typedef enum
85 {
86  M2MB_SPI_MODE_0, /* CPOL = 0, CPHA = 0 */
87  M2MB_SPI_MODE_1, /* CPOL = 0, CPHA = 1 */
88  M2MB_SPI_MODE_2, /* CPOL = 1, CPHA = 0 */
89  M2MB_SPI_MODE_3, /* CPOL = 1, CPHA = 1 */
90  M2MB_SPI_MODE_INVALID = 0x7FFFFFFF
92 
93 /* SPI chip select polarity type.
94 */
95 typedef enum
96 {
97  M2MB_SPI_CS_ACTIVE_LOW, /* During Idle state, the CS line is held low */
98  M2MB_SPI_CS_ACTIVE_HIGH, /* During Idle state, the CS line is held high */
101 
102 /* SPI chip select assertion type.
103  This type defines how the chip select line is configured between N word cycles.
104 */
105 typedef enum
106 {
107  M2MB_SPI_CS_DEASSERT, /* CS is deasserted after transferring data for N clock cycles */
108  M2MB_SPI_CS_KEEP_ASSERTED, /* CS is asserted as long as the core is in the Run state */
111 
112 /* Order in which bytes from Tx/Rx buffer words are put on the bus.
113 */
114 typedef enum
115 {
116  M2MB_SPI_NATIVE = 0, /* Native */
117  M2MB_SPI_LITTLE_ENDIAN = 0, /* Little Endian */
118  M2MB_SPI_BIG_ENDIAN /* Big Endian (network) */
120 
121 typedef enum
122 {
126 
127 typedef void ( *m2mb_spi_ind_callback )( INT32 fd, M2MB_SPI_IND_E spi_event, UINT16 resp_size, void *resp_struct, void *userdata );
128 
129 /* SPI configuration.
130  The SPI configuration is the collection of settings specified for each SPI
131  transfer call to select the various possible SPI transfer parameters.
132 */
133 typedef struct
134 {
135  M2MB_SPI_SHIFT_MODE_T spi_mode; /* Shift mode */
139  m2mb_spi_ind_callback callback_fn; /* Callback function; if NULL, transfer operates in synchronous mode */
140  HANDLE callback_ctxt; /* Pointer to a client object that will be returned as an argument to callback_fn */
141  UINT32 clk_freq_Hz; /* Host sets the SPI clock frequency closest (>=) to the requested frequency. Must be at least 960000 */
142  UINT8 bits_per_word; /* bits per word; any value from 3 to 15, where 3 represents 4bits and 15 represent 16bits */
143  UINT8 cs_clk_delay_cycles; /* Number of clock cycles to wait after asserting CS before starting transfer */
144  UINT8 inter_word_delay_cycles; /* Number of clock cycles to wait between SPI words */
145  BOOLEAN loopback_mode; /* Normally 0. If set, the SPI controller will enable Loopback mode;
146  used primarily for testing */
147  UINT16 buffer_size; /* If non zero, then it allocates memory for sending and receiving data.
148  The size has to be multiple of 32 bytes. If user manages it, the buffer
149  has to be aligned to 32 bytes */
151 
152 /* Global functions =============================================================================*/
153 
154 /*-----------------------------------------------------------------------------------------------*/
155 /* m2mb_spi_open:
156  description:
157  open a SPI device
158  arguments:
159  path: /dev/spidevX.Y where
160  X is the HW SPI master instance index. Supports only index 1.
161  Y is the SPI slave device index. Supports only index 1.
162  Path "/dev/spidevX" is equivalent to default "/dev/spidevX.0"
163  and supports master mode.
164  Path "/dev/spidev" is equivalent to default "/dev/spidev1.0"
165  and supports master mode.
166  Path "/dev/spidev0.1" supports slave mode.
167 
168  flags: currently unused
169  return:
170  file descriptor on SUCCESS
171  -1 on FAILURE
172  i.e.: fd = m2mb_spi_open( "/dev/spidev0.1", 0 );
173 */
174 INT32 m2mb_spi_open( const CHAR *path, INT32 flags, ... );
175 
176 /*-----------------------------------------------------------------------------------------------*/
177 /* m2mb_spi_close:
178  description:
179  close a SPI device
180  arguments:
181  fd: file descriptor returned by m2mb_spi_open
182  return:
183  0 on SUCCESS
184  -1 on FAILURE
185 */
187 
188 /*-----------------------------------------------------------------------------------------------*/
189 /* m2mb_spi_ioctl:
190  description:
191  configure a SPI device
192  arguments:
193  fd: file descriptor returned by m2mb_spi_open
194  request: required operation (see M2MB_SPI_IOCTL_REQUEST)
195  cfg_ptr: pointer to the set/get val, casted to void*
196  return:
197  0 on SUCCESS
198  -1 on FAILURE
199  i.e. m2mb_spi_ioctl(fd, M2MB_SPI_IOCTL_SET_CFG, (void *)&config)
200  where config is a M2MB_SPI_CFG_T struct
201 */
202 INT32 m2mb_spi_ioctl( INT32 fd, INT32 request, ... );
203 
204 /*-----------------------------------------------------------------------------------------------*/
205 /* m2mb_spi_read:
206  description:
207  read nbyte Bytes from a SPI device into the array pointed by buf
208  arguments:
209  fd: file descriptor returned by m2mb_spi_open
210  buf: destination buffer, previously allocated
211  nbyte: length of destination buffer in Bytes
212  returns:
213  number of read Bytes on SUCCESS
214  -1 on FAILURE
215 */
216 SSIZE_T m2mb_spi_read( INT32 fd, void *buf, SIZE_T nbyte );
217 
218 /*-----------------------------------------------------------------------------------------------*/
219 /* m2mb_spi_write:
220  description:
221  write nbyte Bytes from the array pointed by buf to a SPI device
222  arguments:
223  fd: file descriptor returned by m2mb_spi_open
224  buf: source buffer, previously allocated
225  nbyte: length of source buffer in Bytes
226  returns:
227  number of written Bytes on SUCCESS
228  -1 on FAILURE
229 */
230 SSIZE_T m2mb_spi_write( INT32 fd, const void *buf, SIZE_T nbyte );
231 
232 /*-----------------------------------------------------------------------------------------------*/
233 /* m2mb_spi_write_read:
234  description:
235  perform a bi-directional (full duplex) transfer.
236  Read nbyte Bytes from a SPI device into the buffer bufRd
237  and write nbyte Bytes from the buffer bufWr to a SPI device
238  arguments:
239  fd: file descriptor returned by m2mb_spi_open
240  bufWr: source buffer, previously allocated
241  bufRd: destination buffer, previously allocated
242  nbyte: length of source and destination buffers in Bytes
243  returns:
244  number of written/read Bytes on SUCCESS
245  -1 on FAILURE
246 */
247 SSIZE_T m2mb_spi_write_read( INT32 fd, const void *bufWr, void *bufRd, SIZE_T nbyte );
248 
249 #endif /* M2M_M2MB_SPI_API_H */
250 
251 
INT32 SSIZE_T
Definition: m2mb_types.h:107
UINT8 cs_clk_delay_cycles
Definition: m2mb_spi.h:143
M2MB_SPI_IND_E
Definition: m2mb_spi.h:121
unsigned char UINT8
Definition: m2mb_types.h:86
M2MB_SPI_IOCTL_REQUEST
Definition: m2mb_spi.h:48
INT32 m2mb_spi_close(INT32 fd)
INT32 m2mb_spi_open(const CHAR *path, INT32 flags,...)
UINT16 buffer_size
Definition: m2mb_spi.h:147
unsigned short UINT16
Definition: m2mb_types.h:87
HANDLE callback_ctxt
Definition: m2mb_spi.h:140
UINT8 BOOLEAN
Definition: m2mb_types.h:95
M2MB_SPI_SHIFT_MODE_T spi_mode
Definition: m2mb_spi.h:135
m2mb_spi_ind_callback callback_fn
Definition: m2mb_spi.h:139
SSIZE_T m2mb_spi_read(INT32 fd, void *buf, SIZE_T nbyte)
SSIZE_T m2mb_spi_write(INT32 fd, const void *buf, SIZE_T nbyte)
char CHAR
Definition: m2mb_types.h:76
UINT32 clk_freq_Hz
Definition: m2mb_spi.h:141
UINT8 bits_per_word
Definition: m2mb_spi.h:142
M2MB_SPI_BYTE_ORDER_T
Definition: m2mb_spi.h:114
M2MB_SPI_BYTE_ORDER_T endianness
Definition: m2mb_spi.h:138
UINT8 inter_word_delay_cycles
Definition: m2mb_spi.h:144
UINT32 SIZE_T
Definition: m2mb_types.h:105
BOOLEAN loopback_mode
Definition: m2mb_spi.h:145
M2MB_SPI_SHIFT_MODE_T
Definition: m2mb_spi.h:84
SSIZE_T m2mb_spi_write_read(INT32 fd, const void *bufWr, void *bufRd, SIZE_T nbyte)
M2MB_SPI_CS_MODE_T cs_mode
Definition: m2mb_spi.h:137
unsigned long int UINT32
Definition: m2mb_types.h:88
signed int INT32
Definition: m2mb_types.h:82
M2MB_SPI_CS_MODE_T
Definition: m2mb_spi.h:105
void * HANDLE
Definition: m2mb_types.h:98
INT32 m2mb_spi_ioctl(INT32 fd, INT32 request,...)
M2MB_SPI_CS_POLARITY_T cs_polarity
Definition: m2mb_spi.h:136
M2MB_SPI_CS_POLARITY_T
Definition: m2mb_spi.h:95
void(* m2mb_spi_ind_callback)(INT32 fd, M2MB_SPI_IND_E spi_event, UINT16 resp_size, void *resp_struct, void *userdata)
Definition: m2mb_spi.h:127