Paparazzi UAS  v6.2_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 {
55 };
56 
67 };
68 
76 };
77 
85 };
86 
92 };
93 
102 };
103 
106 enum SPIStatus {
109 };
110 
114 };
115 
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;
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 
268 extern void spi_init(struct spi_periph *p);
269 
272 extern void spi_init_slaves(void);
273 
280 extern bool spi_submit(struct spi_periph *p, struct spi_transaction *t);
281 
287 static inline bool spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t) {
288  if (!spi_submit(p, t)) {
289  return false;
290  }
291  // Wait for transaction to complete
292  float start_t = get_sys_time_float();
293  while (t->status == SPITransPending || t->status == SPITransRunning) {
294  if (get_sys_time_float() - start_t > SPI_BLOCKING_TIMEOUT) {
295  break;
296  }
297  }
298  return true;
299 }
300 
304 extern void spi_slave_select(uint8_t slave);
305 
309 extern void spi_slave_unselect(uint8_t slave);
310 
319 extern bool spi_lock(struct spi_periph *p, uint8_t slave);
320 
327 extern bool spi_resume(struct spi_periph *p, uint8_t slave);
328 
329 #endif /* SPI_MASTER */
330 
331 #if SPI_SLAVE
332 
333 #if USE_SPI0_SLAVE
334 
335 extern struct spi_periph spi0;
336 extern void spi0_slave_init(void);
337 
341 extern void spi0_slave_arch_init(void);
342 
343 #endif
344 
345 #if USE_SPI1_SLAVE
346 
347 extern struct spi_periph spi1;
348 extern void spi1_slave_init(void);
349 
353 extern void spi1_slave_arch_init(void);
354 
355 #endif
356 
357 #if USE_SPI2_SLAVE
358 
359 extern struct spi_periph spi2;
360 extern void spi2_slave_init(void);
361 
365 extern void spi2_slave_arch_init(void);
366 
367 #endif
368 
369 #if USE_SPI3_SLAVE
370 
371 extern struct spi_periph spi3;
372 extern void spi3_slave_init(void);
373 
377 extern void spi3_slave_arch_init(void);
378 
379 #endif
380 
384 extern void spi_slave_init(struct spi_periph *p);
385 
392 extern bool spi_slave_register(struct spi_periph *p, struct spi_transaction *t);
393 
400 extern bool spi_slave_wait(struct spi_periph *p);
401 
402 #endif /* SPI_SLAVE */
403 
407 #if SPI_SLAVE_HS
408 #include "mcu_periph/spi_slave_hs_arch.h"
409 extern void spi_slave_hs_init(void);
410 #endif
411 
412 #endif /* SPI_H */
SPIRunning
@ SPIRunning
Definition: spi.h:108
spi_periph::trans_insert_idx
uint8_t trans_insert_idx
Definition: spi.h:177
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
spi0_init
void spi0_init(void)
Definition: spi.c:37
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
spi2_init
void spi2_init(void)
Definition: spi.c:59
SPITransPending
@ SPITransPending
Definition: spi.h:97
SPIClockDiv
SPIClockDiv
Peripheral clock divider.
Definition: spi.h:119
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
get_sys_time_float
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
spi_periph::reg_addr
void * reg_addr
Definition: spi.h:183
spi_transaction
SPI transaction structure.
Definition: spi.h:148
spi_init
void spi_init(struct spi_periph *p)
Initialize a spi peripheral.
Definition: spi.c:89
SPIClockPhase
SPIClockPhase
SPI CPHA (clock phase) options.
Definition: spi.h:73
spi0
struct spi_periph spi0
Definition: spi.c:35
spi_slave_register
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
spi_periph::init_struct
void * init_struct
Definition: spi.h:184
spi_periph::suspend
volatile uint8_t suspend
control for stop/resume of the fifo
Definition: spi.h:187
spi_blocking_transceive
static bool spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t)
Perform a spi transaction (blocking).
Definition: spi.h:287
SPISlave
@ SPISlave
Definition: spi.h:54
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
spi2
struct spi_periph spi2
receive transferred over DMA
Definition: spi_arch.c:1004
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
spi_transaction::output_buf
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
spi_periph
SPI peripheral structure.
Definition: spi.h:174
spi_transaction::select
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
spi2_arch_init
void spi2_arch_init(void)
Architecture dependent SPI2 initialization.
Definition: spi_arch.c:442
SPICphaEdge1
@ SPICphaEdge1
CPHA = 0.
Definition: spi.h:74
SPIUnselect
@ SPIUnselect
slave is not selected but unselected after transaction
Definition: spi.h:65
SPIDiv4
@ SPIDiv4
Definition: spi.h:121
std.h
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
SPICpolIdleLow
@ SPICpolIdleLow
CPOL = 0.
Definition: spi.h:83
SPIMaster
@ SPIMaster
Definition: spi.h:53
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
spi1_init
void spi1_init(void)
Definition: spi.c:48
spi_transaction::after_cb
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
spi_submit
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:508
SPIMode
SPIMode
Definition: spi.h:52
SPITransFailed
@ SPITransFailed
Definition: spi.h:100
spi_resume
bool spi_resume(struct spi_periph *p, uint8_t slave)
Resume the SPI fifo.
Definition: spi_arch.c:670
spi_periph::tx_idx_buf
volatile uint8_t tx_idx_buf
Definition: spi.h:181
SPINoSelect
@ SPINoSelect
slave is not selected nor unselected
Definition: spi.h:66
SPIDiv128
@ SPIDiv128
Definition: spi.h:126
spi_slave_unselect
void spi_slave_unselect(uint8_t slave)
Unselect a slave.
Definition: spi_arch.c:598
SPIStatus
SPIStatus
SPI peripheral status.
Definition: spi.h:106
sys_time.h
Architecture independent timing functions.
SPICpolIdleHigh
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
spi_slave_wait
bool spi_slave_wait(struct spi_periph *p)
Initialized and wait for the next transaction.
spi_periph::status
enum SPIStatus status
internal state of the peripheral
Definition: spi.h:180
spi_slave_init
void spi_slave_init(struct spi_periph *p)
Initialize a spi peripheral in slave mode.
Definition: spi.c:147
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
SPICallback
void(* SPICallback)(struct spi_transaction *trans)
SPI Callback function.
Definition: spi.h:136
SPIDiv32
@ SPIDiv32
Definition: spi.h:124
SPIDiv64
@ SPIDiv64
Definition: spi.h:125
spi_slave_select
void spi_slave_select(uint8_t slave)
Select a slave.
Definition: spi_arch.c:541
spi_lock
bool spi_lock(struct spi_periph *p, uint8_t slave)
Lock the SPI fifo.
Definition: spi_arch.c:656
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
SPIBitOrder
SPIBitOrder
Definition: spi.h:111
SPITransDone
@ SPITransDone
Definition: spi.h:101
SPISelect
@ SPISelect
slave is selected before transaction but not unselected
Definition: spi.h:64
SPITransRunning
@ SPITransRunning
Definition: spi.h:98
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
SPILSBFirst
@ SPILSBFirst
Definition: spi.h:113
SPICphaEdge2
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
spi_periph::trans_extract_idx
uint8_t trans_extract_idx
Definition: spi.h:178
SPI_BLOCKING_TIMEOUT
#define SPI_BLOCKING_TIMEOUT
Definition: spi.h:40
spi_periph::mode
enum SPIMode mode
Definition: spi.h:185
spi0_arch_init
void spi0_arch_init(void)
Architecture dependent SPI0 initialization.
Definition: spi_arch.c:137
spi1
struct spi_periph spi1
receive transferred over DMA
Definition: spi_arch.c:967
SPISlaveSelect
SPISlaveSelect
SPI slave selection behavior options.
Definition: spi.h:62
spi1_arch_init
void spi1_arch_init(void)
Architecture dependent SPI1 initialization.
Definition: spi_arch.c:423
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
SPITransactionStatus
SPITransactionStatus
SPI transaction status.
Definition: spi.h:96
SPIIdle
@ SPIIdle
Definition: spi.h:107
spi_periph::rx_idx_buf
volatile uint8_t rx_idx_buf
Definition: spi.h:182
uint16_t
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
SPIDiv16
@ SPIDiv16
Definition: spi.h:123
SPIDiv256
@ SPIDiv256
Definition: spi.h:127
spi_init_slaves
void spi_init_slaves(void)
Initialize all used slaves and unselect them.
Definition: spi_arch.c:684
SPI_TRANSACTION_QUEUE_LEN
#define SPI_TRANSACTION_QUEUE_LEN
SPI transaction queue length.
Definition: spi.h:169
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
SPIDiv2
@ SPIDiv2
Definition: spi.h:120
SPIDss16bit
@ SPIDss16bit
Definition: spi.h:91
SPIClockPolarity
SPIClockPolarity
SPI CPOL (clock polarity) options.
Definition: spi.h:82
p
static float p[2][2]
Definition: ins_alt_float.c:257
SPIDiv8
@ SPIDiv8
Definition: spi.h:122
spi_periph::trans
struct spi_transaction * trans[SPI_TRANSACTION_QUEUE_LEN]
circular buffer holding transactions
Definition: spi.h:176
SPIDataSizeSelect
SPIDataSizeSelect
SPI data word size of transfer.
Definition: spi.h:89