Paparazzi UAS  v5.18.0_stable
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 
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 */
SPIRunning
@ SPIRunning
Definition: spi.h:108
uint16_t
unsigned short uint16_t
Definition: types.h:16
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
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:129
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:679
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:284
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:405
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:511
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:643
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:586
SPIStatus
SPIStatus
SPI peripheral status.
Definition: spi.h:106
sys_time.h
Architecture independent timing functions.
SPICpolIdleHigh
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
uint8_t
unsigned char uint8_t
Definition: types.h:14
spi_slave_wait
bool spi_slave_wait(struct spi_periph *p)
Initialized and wait for the next transaction.
Definition: spi_arch.c:703
spi_slave_hs_init
void spi_slave_hs_init(void)
Definition: spi_slave_hs_arch.c:145
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:544
spi_lock
bool spi_lock(struct spi_periph *p, uint8_t slave)
Lock the SPI fifo.
Definition: spi_arch.c:629
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
spi_slave_hs_arch.h
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:366
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
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:657
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:268
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