Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
adxl345_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 
33 
34 
35 void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_t slave_idx)
36 {
37  /* set spi_peripheral */
38  adxl->spi_p = spi_p;
39 
40  /* configure spi transaction */
42  adxl->spi_trans.cpha = SPICphaEdge2;
43  adxl->spi_trans.dss = SPIDss8bit;
45  adxl->spi_trans.cdiv = SPIDiv64;
46 
48  adxl->spi_trans.slave_idx = slave_idx;
49  adxl->spi_trans.output_length = 7;
50  adxl->spi_trans.input_length = 7;
51  // callback currently unused
52  adxl->spi_trans.before_cb = NULL;
53  adxl->spi_trans.after_cb = NULL;
54  adxl->spi_trans.input_buf = &(adxl->rx_buf[0]);
55  adxl->spi_trans.output_buf = &(adxl->tx_buf[0]);
56 
57  /* set inital status: Success or Done */
59 
60  /* set default ADXL345 config options */
62 
63  adxl->initialized = false;
64  adxl->data_available = false;
66 }
67 
68 
69 static void adxl345_spi_write_to_reg(struct Adxl345_Spi *adxl, uint8_t _reg, uint8_t _val)
70 {
71  adxl->spi_trans.output_length = 2;
72  adxl->spi_trans.input_length = 0;
73  adxl->tx_buf[0] = _reg;
74  adxl->tx_buf[1] = _val;
75  spi_submit(adxl->spi_p, &(adxl->spi_trans));
76 }
77 
78 // Configuration function called once before normal use
79 static void adxl345_spi_send_config(struct Adxl345_Spi *adxl)
80 {
81  switch (adxl->init_status) {
82  case ADXL_CONF_RATE:
84  adxl->init_status++;
85  break;
86  case ADXL_CONF_INT:
88  adxl->init_status++;
89  break;
90  case ADXL_CONF_FORMAT:
92  adxl->init_status++;
93  break;
94  case ADXL_CONF_ENABLE:
95  /* enable measurement, is in standby after power up */
97  adxl->init_status++;
98  break;
99  case ADXL_CONF_DONE:
100  adxl->initialized = true;
101  adxl->spi_trans.status = SPITransDone;
102  break;
103  default:
104  break;
105  }
106 }
107 
109 {
110  if (adxl->init_status == ADXL_CONF_UNINIT) {
111  adxl->init_status++;
112  if (adxl->spi_trans.status == SPITransSuccess || adxl->spi_trans.status == SPITransDone) {
114  }
115  }
116 }
117 
118 void adxl345_spi_read(struct Adxl345_Spi *adxl)
119 {
120  if (adxl->initialized && adxl->spi_trans.status == SPITransDone) {
121  adxl->spi_trans.output_length = 1;
122  adxl->spi_trans.input_length = 7;
123  /* set read bit and multiple byte bit, then address */
124  adxl->tx_buf[0] = (1 << 7 | 1 << 6 | ADXL345_REG_DATA_X0);
125  spi_submit(adxl->spi_p, &(adxl->spi_trans));
126  }
127 }
128 
129 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
130 
131 void adxl345_spi_event(struct Adxl345_Spi *adxl)
132 {
133  if (adxl->initialized) {
134  if (adxl->spi_trans.status == SPITransFailed) {
135  adxl->spi_trans.status = SPITransDone;
136  } else if (adxl->spi_trans.status == SPITransSuccess) {
137  // Successfull reading
138  adxl->data.vect.x = Int16FromBuf(adxl->rx_buf, 1);
139  adxl->data.vect.y = Int16FromBuf(adxl->rx_buf, 3);
140  adxl->data.vect.z = Int16FromBuf(adxl->rx_buf, 5);
141  adxl->data_available = true;
142  adxl->spi_trans.status = SPITransDone;
143  }
144  } else if (adxl->init_status != ADXL_CONF_UNINIT) { // Configuring but not yet initialized
145  switch (adxl->spi_trans.status) {
146  case SPITransFailed:
147  adxl->init_status--; // Retry config (TODO max retry)
148  case SPITransSuccess:
149  case SPITransDone:
150  adxl->spi_trans.status = SPITransDone;
152  break;
153  default:
154  break;
155  }
156  }
157 }
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
struct Adxl345Config config
Definition: adxl345_spi.h:51
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
static uint8_t adxl345_data_format(struct Adxl345Config *c)
Definition: adxl345.h:69
uint16_t output_length
number of data words to write
Definition: spi.h:146
volatile uint8_t tx_buf[7]
Definition: adxl345_spi.h:42
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: adxl345.h:46
CPHA = 1.
Definition: spi.h:69
volatile uint8_t rx_buf[7]
Definition: adxl345_spi.h:43
#define Int16FromBuf(_buf, _idx)
Definition: adxl345_spi.c:129
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
union Adxl345_Spi::@294 data
enum Adxl345ConfStatus init_status
init status
Definition: adxl345_spi.h:44
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:458
CPOL = 1.
Definition: spi.h:78
volatile bool data_available
data ready flag
Definition: adxl345_spi.h:46
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
#define ADXL345_REG_INT_ENABLE
Definition: adxl345_regs.h:38
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
struct spi_transaction spi_trans
Definition: adxl345_spi.h:41
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
static void adxl345_spi_write_to_reg(struct Adxl345_Spi *adxl, uint8_t _reg, uint8_t _val)
Definition: adxl345_spi.c:69
Driver for the accelerometer ADXL345 from Analog Devices using SPI.
SPI peripheral structure.
Definition: spi.h:168
#define ADXL345_REG_BW_RATE
Definition: adxl345_regs.h:36
void adxl345_spi_event(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:131
Definition: spi.h:84
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
struct spi_periph * spi_p
Definition: adxl345_spi.h:40
#define ADXL345_REG_DATA_FORMAT
Definition: adxl345_regs.h:39
void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: adxl345_spi.c:35
uint16_t input_length
number of data words to read
Definition: spi.h:145
static void adxl345_spi_send_config(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:79
unsigned char uint8_t
Definition: types.h:14
static void adxl345_set_default_config(struct Adxl345Config *c)
Definition: adxl345.h:56
slave is selected before transaction and unselected after
Definition: spi.h:57
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
void adxl345_spi_start_configure(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:108
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
#define ADXL345_REG_POWER_CTL
Definition: adxl345_regs.h:37
Definition: spi.h:119
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
#define ADXL345_REG_DATA_X0
Definition: adxl345_regs.h:40
bool initialized
config done flag
Definition: adxl345_spi.h:45
enum SPITransactionStatus status
Definition: spi.h:156
void adxl345_spi_read(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:118