Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
lis302dl_spi.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Felix Ruess <felix.ruess@gmail.com>
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 
29 
30 void lis302dl_spi_init(struct Lis302dl_Spi *lis, struct spi_periph *spi_p, uint8_t slave_idx)
31 {
32  /* set spi_peripheral */
33  lis->spi_p = spi_p;
34 
35  /* configure spi transaction */
38  lis->spi_trans.dss = SPIDss8bit;
40  lis->spi_trans.cdiv = SPIDiv64;
41 
43  lis->spi_trans.slave_idx = slave_idx;
44  lis->spi_trans.output_length = 2;
45  lis->spi_trans.input_length = 8;
46  // callback currently unused
47  lis->spi_trans.before_cb = NULL;
48  lis->spi_trans.after_cb = NULL;
49  lis->spi_trans.input_buf = &(lis->rx_buf[0]);
50  lis->spi_trans.output_buf = &(lis->tx_buf[0]);
51 
52  /* set inital status: Success or Done */
54 
55  /* set default LIS302DL config options */
57 
58  lis->initialized = FALSE;
59  lis->data_available = FALSE;
61 }
62 
63 
64 static void lis302dl_spi_write_to_reg(struct Lis302dl_Spi *lis, uint8_t _reg, uint8_t _val) {
65  lis->spi_trans.output_length = 2;
66  lis->spi_trans.input_length = 0;
67  lis->tx_buf[0] = _reg;
68  lis->tx_buf[1] = _val;
69  spi_submit(lis->spi_p, &(lis->spi_trans));
70 }
71 
72 // Configuration function called once before normal use
73 static void lis302dl_spi_send_config(struct Lis302dl_Spi *lis)
74 {
75  uint8_t reg_val = 0;
76 
77  switch (lis->init_status) {
78  case LIS_CONF_WHO_AM_I:
79  /* query device id */
80  lis->spi_trans.output_length = 1;
81  lis->spi_trans.input_length = 2;
82  /* set read bit then reg address */
83  lis->tx_buf[0] = (1<<7 | LIS302DL_REG_WHO_AM_I);
84  if (spi_submit(lis->spi_p, &(lis->spi_trans)))
85  lis->init_status++;
86  break;
87  case LIS_CONF_REG2:
88  /* set SPI mode, Filtered Data Selection */
89  reg_val = (lis->config.spi_3_wire << 7) | (lis->config.filt_data << 4);
91  lis->init_status++;
92  break;
93  case LIS_CONF_REG3:
94  /* Interrupt active high/low */
96  lis->init_status++;
97  break;
98  case LIS_CONF_ENABLE:
99  /* set data rate, range, enable measurement, is in standby after power up */
100  reg_val = (lis->config.rate << 7) |
101  (1 << 6) | // Power Down Control to active mode
102  (lis->config.range << 5) |
103  0x5; // enable z,y,x axes
105  lis->init_status++;
106  break;
107  case LIS_CONF_DONE:
108  lis->initialized = TRUE;
110  break;
111  default:
112  break;
113  }
114 }
115 
117 {
118  if (lis->init_status == LIS_CONF_UNINIT) {
119  lis->init_status++;
120  if (lis->spi_trans.status == SPITransSuccess || lis->spi_trans.status == SPITransDone) {
122  }
123  }
124 }
125 
127 {
128  if (lis->initialized && lis->spi_trans.status == SPITransDone) {
129  lis->spi_trans.output_length = 1;
130  lis->spi_trans.input_length = 8;
131  /* set read bit and multiple byte bit, then address */
132  lis->tx_buf[0] = (1<<7|1<<6|LIS302DL_REG_STATUS);
133  spi_submit(lis->spi_p, &(lis->spi_trans));
134  }
135 }
136 
138 {
139  if (lis->initialized) {
140  if (lis->spi_trans.status == SPITransFailed) {
142  }
143  else if (lis->spi_trans.status == SPITransSuccess) {
144  // Successfull reading
145  if (bit_is_set(lis->rx_buf[1], 3)) {
146  // new xyz data available
147  lis->data.vect.x = lis->rx_buf[3];
148  lis->data.vect.y = lis->rx_buf[5];
149  lis->data.vect.z = lis->rx_buf[7];
150  lis->data_available = TRUE;
151  }
153  }
154  }
155  else if (lis->init_status != LIS_CONF_UNINIT) { // Configuring but not yet initialized
156  switch (lis->spi_trans.status) {
157  case SPITransFailed:
158  lis->init_status--; // Retry config (TODO max retry)
159  case SPITransSuccess:
160  if (lis->init_status == LIS_CONF_WHO_AM_I_OK) {
161  if (lis->rx_buf[1] == LIS302DL_WHO_AM_I)
162  lis->init_status++;
163  else
165  }
166  case SPITransDone:
169  break;
170  default:
171  break;
172  }
173  }
174 }
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
#define LIS302DL_REG_STATUS
Definition: lis302dl_regs.h:36
uint8_t input_length
number of data words to read
Definition: spi.h:145
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
CPHA = 1.
Definition: spi.h:69
static void lis302dl_set_default_config(struct Lis302dlConfig *c)
Definition: lis302dl.h:56
volatile uint8_t tx_buf[2]
Definition: lis302dl_spi.h:42
static void lis302dl_spi_write_to_reg(struct Lis302dl_Spi *lis, uint8_t _reg, uint8_t _val)
Definition: lis302dl_spi.c:64
#define LIS302DL_WHO_AM_I
LIS302DL device identifier contained in LIS302DL_REG_WHO_AM_I.
Definition: lis302dl_regs.h:42
enum Lis302dlConfStatus init_status
init status
Definition: lis302dl_spi.h:44
enum Lis302dlRates rate
Data Output Rate.
Definition: lis302dl.h:53
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
CPOL = 1.
Definition: spi.h:78
void lis302dl_spi_init(struct Lis302dl_Spi *lis, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: lis302dl_spi.c:30
#define LIS302DL_REG_CTRL_REG2
Definition: lis302dl_regs.h:34
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
#define LIS302DL_REG_CTRL_REG1
Definition: lis302dl_regs.h:33
#define FALSE
Definition: imu_chimu.h:141
union Lis302dl_Spi::@33 data
enum SPITransactionStatus status
Definition: spi.h:156
void lis302dl_spi_read(struct Lis302dl_Spi *lis)
Definition: lis302dl_spi.c:126
enum Lis302dlRanges range
g Range
Definition: lis302dl.h:52
struct spi_transaction spi_trans
Definition: lis302dl_spi.h:41
static void lis302dl_spi_send_config(struct Lis302dl_Spi *lis)
Definition: lis302dl_spi.c:73
Definition: spi.h:84
bool_t int_invert
Invert Interrupt FALSE: active high, TRUE: active low.
Definition: lis302dl.h:45
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
bool_t spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:470
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t output_length
number of data words to write
Definition: spi.h:146
#define LIS302DL_REG_WHO_AM_I
Definition: lis302dl_regs.h:32
Driver for LIS302DL 3-axis accelerometer from ST using SPI.
volatile uint8_t rx_buf[8]
Definition: lis302dl_spi.h:43
volatile bool_t data_available
data ready flag
Definition: lis302dl_spi.h:46
bool_t filt_data
Filtered Data Selection.
Definition: lis302dl.h:51
#define TRUE
Definition: imu_chimu.h:144
SPI peripheral structure.
Definition: spi.h:168
bool_t spi_3_wire
Set 3-wire SPI mode, if FALSE: 4-wire SPI mode.
Definition: lis302dl.h:46
unsigned char uint8_t
Definition: types.h:14
struct Lis302dlConfig config
Definition: lis302dl_spi.h:51
slave is selected before transaction and unselected after
Definition: spi.h:57
void lis302dl_spi_start_configure(struct Lis302dl_Spi *lis)
Definition: lis302dl_spi.c:116
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
bool_t initialized
config done flag
Definition: lis302dl_spi.h:45
Definition: spi.h:119
#define LIS302DL_REG_CTRL_REG3
Definition: lis302dl_regs.h:35
struct Int8Vect3 vect
data vector in accel coordinate system
Definition: lis302dl_spi.h:48
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
void lis302dl_spi_event(struct Lis302dl_Spi *lis)
Definition: lis302dl_spi.c:137
struct spi_periph * spi_p
Definition: lis302dl_spi.h:40
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155