Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
spi.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2012 The Paparazzi Team
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
31 #ifndef SPI_H
32 #define SPI_H
33 
34 #include "std.h"
35 
36 #include "mcu_periph/spi_arch.h"
37 #include "mcu_periph/sys_time.h"
38 
39 #ifndef SPI_BLOCKING_TIMEOUT
40 #define SPI_BLOCKING_TIMEOUT 1.f
41 #endif
42 
43 
51 // FIXME how to use this properly ?
52 enum SPIMode {
54  SPISlave
55 };
56 
67 };
68 
76 };
77 
85 };
86 
92 };
93 
102 };
103 
106 enum SPIStatus {
108  SPIRunning
109 };
110 
114 };
115 
127  SPIDiv256
128 };
129 
130 struct spi_transaction;
131 
136 typedef void (*SPICallback)(struct spi_transaction *trans);
137 
149  volatile uint8_t *input_buf;
150  volatile uint8_t *output_buf;
154  enum SPISlaveSelect select;
155  enum SPIClockPolarity cpol;
156  enum SPIClockPhase cpha;
157  enum SPIDataSizeSelect dss;
158  enum SPIBitOrder bitorder;
159  enum SPIClockDiv cdiv;
162  volatile enum SPITransactionStatus status;
163 };
164 
168 #ifndef SPI_TRANSACTION_QUEUE_LEN
169 #define SPI_TRANSACTION_QUEUE_LEN 8
170 #endif
171 
174 struct spi_periph {
180  volatile enum SPIStatus status;
181  volatile uint8_t tx_idx_buf;
182  volatile uint8_t rx_idx_buf;
183  void *reg_addr;
184  void *init_struct;
185  enum SPIMode mode;
187  volatile uint8_t suspend;
188 };
189 
190 #if SPI_MASTER
191 
192 #define SPI_SLAVE0 0
193 #define SPI_SLAVE1 1
194 #define SPI_SLAVE2 2
195 #define SPI_SLAVE3 3
196 #define SPI_SLAVE4 4
197 #define SPI_SLAVE5 5
198 #define SPI_SLAVE6 6
199 #define SPI_SLAVE7 7
200 #define SPI_SLAVE8 8
201 
203 //extern uint8_t spi_nb_ovrn;
204 
205 #if USE_SPI0
206 
207 extern struct spi_periph spi0;
208 extern void spi0_init(void);
209 
213 extern void spi0_arch_init(void);
214 
215 #endif // USE_SPI0
216 
217 #if USE_SPI1
218 
219 extern struct spi_periph spi1;
220 extern void spi1_init(void);
221 
225 extern void spi1_arch_init(void);
226 
227 #endif // USE_SPI1
228 
229 #if USE_SPI2
230 
231 extern struct spi_periph spi2;
232 extern void spi2_init(void);
233 
237 extern void spi2_arch_init(void);
238 
239 #endif // USE_SPI2
240 
241 #if USE_SPI3
242 
243 extern struct spi_periph spi3;
244 extern void spi3_init(void);
245 
249 extern void spi3_arch_init(void);
250 
251 #endif // USE_SPI3
252 
253 #if USE_SPI4
254 
255 extern struct spi_periph spi4;
256 extern void spi4_init(void);
257 
261 extern void spi4_arch_init(void);
262 
263 #endif // USE_SPI4
264 
265 #if USE_SPI6
266 
267 extern struct spi_periph spi6;
268 extern void spi6_init(void);
269 
273 extern void spi6_arch_init(void);
274 
275 #endif // USE_SPI6
276 
280 extern void spi_init(struct spi_periph *p);
281 
284 extern void spi_init_slaves(void);
285 
292 extern bool spi_submit(struct spi_periph *p, struct spi_transaction *t);
293 
299 static inline bool spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t) {
300  if (!spi_submit(p, t)) {
301  return false;
302  }
303  // Wait for transaction to complete
304  float start_t = get_sys_time_float();
305  while (t->status == SPITransPending || t->status == SPITransRunning) {
306  if (get_sys_time_float() - start_t > SPI_BLOCKING_TIMEOUT) {
307  break;
308  }
309  }
310  return true;
311 }
312 
316 extern void spi_slave_select(uint8_t slave);
317 
321 extern void spi_slave_unselect(uint8_t slave);
322 
331 extern bool spi_lock(struct spi_periph *p, uint8_t slave);
332 
339 extern bool spi_resume(struct spi_periph *p, uint8_t slave);
340 
341 #endif /* SPI_MASTER */
342 
343 #if SPI_SLAVE
344 
345 #if USE_SPI0_SLAVE
346 
347 extern struct spi_periph spi0;
348 extern void spi0_slave_init(void);
349 
353 extern void spi0_slave_arch_init(void);
354 
355 #endif
356 
357 #if USE_SPI1_SLAVE
358 
359 extern struct spi_periph spi1;
360 extern void spi1_slave_init(void);
361 
365 extern void spi1_slave_arch_init(void);
366 
367 #endif
368 
369 #if USE_SPI2_SLAVE
370 
371 extern struct spi_periph spi2;
372 extern void spi2_slave_init(void);
373 
377 extern void spi2_slave_arch_init(void);
378 
379 #endif
380 
381 #if USE_SPI3_SLAVE
382 
383 extern struct spi_periph spi3;
384 extern void spi3_slave_init(void);
385 
389 extern void spi3_slave_arch_init(void);
390 
391 #endif
392 
396 extern void spi_slave_init(struct spi_periph *p);
397 
404 extern bool spi_slave_register(struct spi_periph *p, struct spi_transaction *t);
405 
412 extern bool spi_slave_wait(struct spi_periph *p);
413 
414 #endif /* SPI_SLAVE */
415 
419 #if SPI_SLAVE_HS
420 #include "mcu_periph/spi_slave_hs_arch.h"
421 extern void spi_slave_hs_init(void);
422 #endif
423 
424 #endif /* SPI_H */
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
enum SPIStatus status
internal state of the peripheral
Definition: spi.h:180
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
struct spi_transaction * trans[SPI_TRANSACTION_QUEUE_LEN]
circular buffer holding transactions
Definition: spi.h:176
void * reg_addr
Definition: spi.h:183
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
uint16_t input_length
number of data words to read
Definition: spi.h:151
void * init_struct
Definition: spi.h:184
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
uint8_t trans_insert_idx
Definition: spi.h:177
volatile uint8_t tx_idx_buf
Definition: spi.h:181
volatile uint8_t suspend
control for stop/resume of the fifo
Definition: spi.h:187
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
enum SPIMode mode
Definition: spi.h:185
uint8_t trans_extract_idx
Definition: spi.h:178
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
uint16_t output_length
number of data words to write
Definition: spi.h:152
enum SPITransactionStatus status
Definition: spi.h:162
volatile uint8_t rx_idx_buf
Definition: spi.h:182
SPIClockPhase
SPI CPHA (clock phase) options.
Definition: spi.h:73
void spi2_init(void)
Definition: spi.c:59
void spi0_init(void)
Definition: spi.c:37
SPIStatus
SPI peripheral status.
Definition: spi.h:106
SPITransactionStatus
SPI transaction status.
Definition: spi.h:96
SPIClockPolarity
SPI CPOL (clock polarity) options.
Definition: spi.h:82
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:533
bool spi_slave_wait(struct spi_periph *p)
Initialized and wait for the next transaction.
static bool spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t)
Perform a spi transaction (blocking).
Definition: spi.h:299
void spi_init(struct spi_periph *p)
Initialize a spi peripheral.
Definition: spi.c:98
void spi_slave_init(struct spi_periph *p)
Initialize a spi peripheral in slave mode.
Definition: spi.c:156
bool spi_lock(struct spi_periph *p, uint8_t slave)
Lock the SPI fifo.
Definition: spi_arch.c:681
SPISlaveSelect
SPI slave selection behavior options.
Definition: spi.h:62
void spi1_init(void)
Definition: spi.c:48
void(* SPICallback)(struct spi_transaction *trans)
SPI Callback function.
Definition: spi.h:136
#define SPI_TRANSACTION_QUEUE_LEN
SPI transaction queue length.
Definition: spi.h:169
SPIMode
Definition: spi.h:52
void spi_slave_unselect(uint8_t slave)
Unselect a slave.
Definition: spi_arch.c:623
SPIBitOrder
Definition: spi.h:111
SPIClockDiv
Peripheral clock divider.
Definition: spi.h:119
void spi1_arch_init(void)
Architecture dependent SPI1 initialization.
Definition: spi_arch.c:423
SPIDataSizeSelect
SPI data word size of transfer.
Definition: spi.h:89
void spi0_arch_init(void)
Architecture dependent SPI0 initialization.
Definition: spi_arch.c:137
struct spi_periph spi2
receive transferred over DMA
Definition: spi_arch.c:1004
bool spi_slave_register(struct spi_periph *p, struct spi_transaction *t)
Register a spi transaction in slave mode (only one transaction can be registered).
Definition: spi_arch.c:1624
void spi_slave_select(uint8_t slave)
Select a slave.
Definition: spi_arch.c:566
void spi2_arch_init(void)
Architecture dependent SPI2 initialization.
Definition: spi_arch.c:442
struct spi_periph spi0
Definition: spi.c:35
struct spi_periph spi1
receive transferred over DMA
Definition: spi_arch.c:967
bool spi_resume(struct spi_periph *p, uint8_t slave)
Resume the SPI fifo.
Definition: spi_arch.c:695
void spi_init_slaves(void)
Initialize all used slaves and unselect them.
Definition: spi_arch.c:709
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
@ SPICphaEdge1
CPHA = 0.
Definition: spi.h:74
@ SPIIdle
Definition: spi.h:107
@ SPIRunning
Definition: spi.h:108
@ SPITransFailed
Definition: spi.h:100
@ SPITransSuccess
Definition: spi.h:99
@ SPITransRunning
Definition: spi.h:98
@ SPITransPending
Definition: spi.h:97
@ SPITransDone
Definition: spi.h:101
@ SPICpolIdleLow
CPOL = 0.
Definition: spi.h:83
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
@ SPISelect
slave is selected before transaction but not unselected
Definition: spi.h:64
@ SPINoSelect
slave is not selected nor unselected
Definition: spi.h:66
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
@ SPIUnselect
slave is not selected but unselected after transaction
Definition: spi.h:65
@ SPIMaster
Definition: spi.h:53
@ SPISlave
Definition: spi.h:54
@ SPILSBFirst
Definition: spi.h:113
@ SPIMSBFirst
Definition: spi.h:112
@ SPIDiv4
Definition: spi.h:121
@ SPIDiv8
Definition: spi.h:122
@ SPIDiv2
Definition: spi.h:120
@ SPIDiv256
Definition: spi.h:127
@ SPIDiv128
Definition: spi.h:126
@ SPIDiv32
Definition: spi.h:124
@ SPIDiv64
Definition: spi.h:125
@ SPIDiv16
Definition: spi.h:123
@ SPIDss8bit
Definition: spi.h:90
@ SPIDss16bit
Definition: spi.h:91
SPI peripheral structure.
Definition: spi.h:174
SPI transaction structure.
Definition: spi.h:148
static float p[2][2]
#define SPI_BLOCKING_TIMEOUT
Definition: spi.h:40
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98