Paparazzi UAS  v5.17_devel-14-g4575375
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
200 //extern uint8_t spi_nb_ovrn;
201 
202 #if USE_SPI0
203 
204 extern struct spi_periph spi0;
205 extern void spi0_init(void);
206 
210 extern void spi0_arch_init(void);
211 
212 #endif // USE_SPI0
213 
214 #if USE_SPI1
215 
216 extern struct spi_periph spi1;
217 extern void spi1_init(void);
218 
222 extern void spi1_arch_init(void);
223 
224 #endif // USE_SPI1
225 
226 #if USE_SPI2
227 
228 extern struct spi_periph spi2;
229 extern void spi2_init(void);
230 
234 extern void spi2_arch_init(void);
235 
236 #endif // USE_SPI2
237 
238 #if USE_SPI3
239 
240 extern struct spi_periph spi3;
241 extern void spi3_init(void);
242 
246 extern void spi3_arch_init(void);
247 
248 #endif // USE_SPI3
249 
250 #if USE_SPI4
251 
252 extern struct spi_periph spi4;
253 extern void spi4_init(void);
254 
258 extern void spi4_arch_init(void);
259 
260 #endif // USE_SPI4
261 
265 extern void spi_init(struct spi_periph *p);
266 
269 extern void spi_init_slaves(void);
270 
277 extern bool spi_submit(struct spi_periph *p, struct spi_transaction *t);
278 
284 static inline bool spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t) {
285  if (!spi_submit(p, t)) {
286  return false;
287  }
288  // Wait for transaction to complete
289  float start_t = get_sys_time_float();
290  while (t->status == SPITransPending || t->status == SPITransRunning) {
291  if (get_sys_time_float() - start_t > SPI_BLOCKING_TIMEOUT) {
292  break;
293  }
294  }
295  return true;
296 }
297 
301 extern void spi_slave_select(uint8_t slave);
302 
306 extern void spi_slave_unselect(uint8_t slave);
307 
316 extern bool spi_lock(struct spi_periph *p, uint8_t slave);
317 
324 extern bool spi_resume(struct spi_periph *p, uint8_t slave);
325 
326 #endif /* SPI_MASTER */
327 
328 #if SPI_SLAVE
329 
330 #if USE_SPI0_SLAVE
331 
332 extern struct spi_periph spi0;
333 extern void spi0_slave_init(void);
334 
338 extern void spi0_slave_arch_init(void);
339 
340 #endif
341 
342 #if USE_SPI1_SLAVE
343 
344 extern struct spi_periph spi1;
345 extern void spi1_slave_init(void);
346 
350 extern void spi1_slave_arch_init(void);
351 
352 #endif
353 
354 #if USE_SPI2_SLAVE
355 
356 extern struct spi_periph spi2;
357 extern void spi2_slave_init(void);
358 
362 extern void spi2_slave_arch_init(void);
363 
364 #endif
365 
366 #if USE_SPI3_SLAVE
367 
368 extern struct spi_periph spi3;
369 extern void spi3_slave_init(void);
370 
374 extern void spi3_slave_arch_init(void);
375 
376 #endif
377 
381 extern void spi_slave_init(struct spi_periph *p);
382 
389 extern bool spi_slave_register(struct spi_periph *p, struct spi_transaction *t);
390 
397 extern bool spi_slave_wait(struct spi_periph *p);
398 
399 #endif /* SPI_SLAVE */
400 
404 #if SPI_SLAVE_HS
406 extern void spi_slave_hs_init(void);
407 #endif
408 
409 #endif /* SPI_H */
SPIClockPolarity
SPI CPOL (clock polarity) options.
Definition: spi.h:82
unsigned short uint16_t
Definition: types.h:16
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
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:679
Definition: spi.h:124
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
#define SPI_BLOCKING_TIMEOUT
Definition: spi.h:40
uint16_t output_length
number of data words to write
Definition: spi.h:152
SPIMode
Definition: spi.h:52
SPIDataSizeSelect
SPI data word size of transfer.
Definition: spi.h:89
struct spi_periph spi2
receive transferred over DMA
Definition: spi_arch.c:1004
slave is selected before transaction but not unselected
Definition: spi.h:64
void * reg_addr
Definition: spi.h:183
void spi1_arch_init(void)
Architecture dependent SPI1 initialization.
Definition: spi_arch.c:366
slave is not selected but unselected after transaction
Definition: spi.h:65
SPITransactionStatus
SPI transaction status.
Definition: spi.h:96
CPHA = 1.
Definition: spi.h:75
Definition: spi.h:121
SPI transaction structure.
Definition: spi.h:148
slave is not selected nor unselected
Definition: spi.h:66
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:511
void(* SPICallback)(struct spi_transaction *trans)
SPI Callback function.
Definition: spi.h:136
void spi_slave_hs_init(void)
CPOL = 1.
Definition: spi.h:84
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
SPIClockPhase
SPI CPHA (clock phase) options.
Definition: spi.h:73
void * init_struct
Definition: spi.h:184
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
volatile uint8_t suspend
control for stop/resume of the fifo
Definition: spi.h:187
static bool spi_blocking_transceive(struct spi_periph *p, struct spi_transaction *t)
Perform a spi transaction (blocking).
Definition: spi.h:284
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
void spi_init(struct spi_periph *p)
Initialize a spi peripheral.
Definition: spi.c:89
SPIStatus
SPI peripheral status.
Definition: spi.h:106
Definition: spi.h:120
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
bool spi_lock(struct spi_periph *p, uint8_t slave)
Lock the SPI fifo.
Definition: spi_arch.c:629
SPI peripheral structure.
Definition: spi.h:174
Definition: spi.h:90
void spi_slave_init(struct spi_periph *p)
Initialize a spi peripheral in slave mode.
Definition: spi.c:147
void spi_slave_unselect(uint8_t slave)
Unselect a slave.
Definition: spi_arch.c:586
Architecture independent timing functions.
struct spi_periph spi0
Definition: spi.c:35
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
void spi2_arch_init(void)
Architecture dependent SPI2 initialization.
Definition: spi_arch.c:405
volatile uint8_t tx_idx_buf
Definition: spi.h:181
bool spi_slave_wait(struct spi_periph *p)
Initialized and wait for the next transaction.
Definition: spi_arch.c:703
CPOL = 0.
Definition: spi.h:83
uint16_t input_length
number of data words to read
Definition: spi.h:151
enum SPIStatus status
internal state of the peripheral
Definition: spi.h:180
void spi1_init(void)
Definition: spi.c:48
SPIBitOrder
Definition: spi.h:111
unsigned char uint8_t
Definition: types.h:14
Definition: spi.h:107
void spi2_init(void)
Definition: spi.c:59
Definition: spi.h:54
SPIClockDiv
Peripheral clock divider.
Definition: spi.h:119
CPHA = 0.
Definition: spi.h:74
bool spi_resume(struct spi_periph *p, uint8_t slave)
Resume the SPI fifo.
Definition: spi_arch.c:643
Definition: spi.h:123
Definition: spi.h:127
slave is selected before transaction and unselected after
Definition: spi.h:63
enum SPIMode mode
Definition: spi.h:185
uint8_t trans_extract_idx
Definition: spi.h:178
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
Definition: spi.h:125
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
static float p[2][2]
Definition: spi.h:122
Highspeed SPI Slave Interface.
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
volatile uint8_t rx_idx_buf
Definition: spi.h:182
#define SPI_TRANSACTION_QUEUE_LEN
SPI transaction queue length.
Definition: spi.h:169
Definition: spi.h:53
void spi_init_slaves(void)
Initialize all used slaves and unselect them.
Definition: spi_arch.c:657
void spi_slave_select(uint8_t slave)
Select a slave.
Definition: spi_arch.c:544
void spi0_arch_init(void)
Architecture dependent SPI0 initialization.
Definition: spi_arch.c:137
struct spi_transaction * trans[SPI_TRANSACTION_QUEUE_LEN]
circular buffer holding transactions
Definition: spi.h:176
struct spi_periph spi1
receive transferred over DMA
Definition: spi_arch.c:967
SPISlaveSelect
SPI slave selection behavior options.
Definition: spi.h:62
Definition: spi.h:126
void spi0_init(void)
Definition: spi.c:37
enum SPITransactionStatus status
Definition: spi.h:162
uint8_t trans_insert_idx
Definition: spi.h:177