Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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  adxl->spi_trans.output_length = 2;
71  adxl->spi_trans.input_length = 0;
72  adxl->tx_buf[0] = _reg;
73  adxl->tx_buf[1] = _val;
74  spi_submit(adxl->spi_p, &(adxl->spi_trans));
75 }
76 
77 // Configuration function called once before normal use
78 static void adxl345_spi_send_config(struct Adxl345_Spi *adxl)
79 {
80  switch (adxl->init_status) {
81  case ADXL_CONF_RATE:
83  adxl->init_status++;
84  break;
85  case ADXL_CONF_INT:
87  adxl->init_status++;
88  break;
89  case ADXL_CONF_FORMAT:
91  adxl->init_status++;
92  break;
93  case ADXL_CONF_ENABLE:
94  /* enable measurement, is in standby after power up */
96  adxl->init_status++;
97  break;
98  case ADXL_CONF_DONE:
99  adxl->initialized = TRUE;
100  adxl->spi_trans.status = SPITransDone;
101  break;
102  default:
103  break;
104  }
105 }
106 
108 {
109  if (adxl->init_status == ADXL_CONF_UNINIT) {
110  adxl->init_status++;
111  if (adxl->spi_trans.status == SPITransSuccess || adxl->spi_trans.status == SPITransDone) {
113  }
114  }
115 }
116 
117 void adxl345_spi_read(struct Adxl345_Spi *adxl)
118 {
119  if (adxl->initialized && adxl->spi_trans.status == SPITransDone) {
120  adxl->spi_trans.output_length = 1;
121  adxl->spi_trans.input_length = 7;
122  /* set read bit and multiple byte bit, then address */
123  adxl->tx_buf[0] = (1<<7|1<<6|ADXL345_REG_DATA_X0);
124  spi_submit(adxl->spi_p, &(adxl->spi_trans));
125  }
126 }
127 
128 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
129 
130 void adxl345_spi_event(struct Adxl345_Spi *adxl)
131 {
132  if (adxl->initialized) {
133  if (adxl->spi_trans.status == SPITransFailed) {
134  adxl->spi_trans.status = SPITransDone;
135  }
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  }
145  else if (adxl->init_status != ADXL_CONF_UNINIT) { // Configuring but not yet initialized
146  switch (adxl->spi_trans.status) {
147  case SPITransFailed:
148  adxl->init_status--; // Retry config (TODO max retry)
149  case SPITransSuccess:
150  case SPITransDone:
151  adxl->spi_trans.status = SPITransDone;
153  break;
154  default:
155  break;
156  }
157  }
158 }
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
static uint8_t adxl345_data_format(struct Adxl345Config *c)
Definition: adxl345.h:69
uint8_t input_length
number of data words to read
Definition: spi.h:145
union Adxl345_Spi::@18 data
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
CPHA = 1.
Definition: spi.h:69
#define Int16FromBuf(_buf, _idx)
Definition: adxl345_spi.c:128
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
CPOL = 1.
Definition: spi.h:78
#define ADXL345_REG_INT_ENABLE
Definition: adxl345_regs.h:38
volatile bool_t data_available
data ready flag
Definition: adxl345_spi.h:46
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
struct spi_periph * spi_p
Definition: adxl345_spi.h:40
volatile uint8_t tx_buf[7]
Definition: adxl345_spi.h:42
#define FALSE
Definition: imu_chimu.h:141
enum SPITransactionStatus status
Definition: spi.h:156
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.
#define ADXL345_REG_BW_RATE
Definition: adxl345_regs.h:36
void adxl345_spi_event(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:130
Definition: spi.h:84
struct spi_transaction spi_trans
Definition: adxl345_spi.h:41
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:469
enum Adxl345ConfStatus init_status
init status
Definition: adxl345_spi.h:44
bool_t initialized
config done flag
Definition: adxl345_spi.h:45
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 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
static void adxl345_spi_send_config(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:78
#define TRUE
Definition: imu_chimu.h:144
unsigned char uint8_t
Definition: types.h:14
static void adxl345_set_default_config(struct Adxl345Config *c)
Definition: adxl345.h:56
struct Int16Vect3 vect
data vector in accel coordinate system
Definition: adxl345_spi.h:48
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
slave is selected before transaction and unselected after
Definition: spi.h:57
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
void adxl345_spi_start_configure(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:107
#define ADXL345_REG_POWER_CTL
Definition: adxl345_regs.h:37
Definition: spi.h:119
bool_t drdy_int_enable
Enable Data Ready Interrupt.
Definition: adxl345.h:46
struct Adxl345Config config
Definition: adxl345_spi.h:51
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
#define ADXL345_REG_DATA_X0
Definition: adxl345_regs.h:40
volatile uint8_t rx_buf[7]
Definition: adxl345_spi.h:43
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
void adxl345_spi_read(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:117