Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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  /* Falls through. */
149  case SPITransSuccess:
150  case SPITransDone:
151  adxl->spi_trans.status = SPITransDone;
153  break;
154  default:
155  break;
156  }
157  }
158 }
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
adxl345_spi_start_configure
void adxl345_spi_start_configure(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:108
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
Adxl345Config::drdy_int_enable
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: adxl345.h:46
ADXL_CONF_INT
@ ADXL_CONF_INT
Definition: adxl345.h:39
ADXL345_REG_INT_ENABLE
#define ADXL345_REG_INT_ENABLE
Definition: adxl345_regs.h:38
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
Adxl345_Spi::init_status
enum Adxl345ConfStatus init_status
init status
Definition: adxl345_spi.h:44
Adxl345_Spi
Definition: adxl345_spi.h:39
Adxl345_Spi::rx_buf
volatile uint8_t rx_buf[7]
Definition: adxl345_spi.h:43
ADXL_CONF_FORMAT
@ ADXL_CONF_FORMAT
Definition: adxl345.h:40
Adxl345Config::rate
enum Adxl345Rates rate
Data Output Rate.
Definition: adxl345.h:53
Adxl345_Spi::spi_trans
struct spi_transaction spi_trans
Definition: adxl345_spi.h:41
adxl345_spi.h
ADXL_CONF_DONE
@ ADXL_CONF_DONE
Definition: adxl345.h:42
Adxl345_Spi::data_available
volatile bool data_available
data ready flag
Definition: adxl345_spi.h:46
ADXL_CONF_ENABLE
@ ADXL_CONF_ENABLE
Definition: adxl345.h:41
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
adxl345_spi_write_to_reg
static void adxl345_spi_write_to_reg(struct Adxl345_Spi *adxl, uint8_t _reg, uint8_t _val)
Definition: adxl345_spi.c:69
ADXL_CONF_UNINIT
@ ADXL_CONF_UNINIT
Definition: adxl345.h:37
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
adxl345_spi_event
void adxl345_spi_event(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:131
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
ADXL345_REG_DATA_FORMAT
#define ADXL345_REG_DATA_FORMAT
Definition: adxl345_regs.h:39
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
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 SPI transaction.
Definition: spi_arch.c:511
SPITransFailed
@ SPITransFailed
Definition: spi.h:100
ADXL345_REG_BW_RATE
#define ADXL345_REG_BW_RATE
Definition: adxl345_regs.h:36
SPICpolIdleHigh
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
Adxl345_Spi::spi_p
struct spi_periph * spi_p
Definition: adxl345_spi.h:40
uint8_t
unsigned char uint8_t
Definition: types.h:14
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
ADXL345_REG_POWER_CTL
#define ADXL345_REG_POWER_CTL
Definition: adxl345_regs.h:37
SPIDiv64
@ SPIDiv64
Definition: spi.h:125
Adxl345_Spi::data
union Adxl345_Spi::@305 data
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
adxl345_spi_init
void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: adxl345_spi.c:35
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
SPITransDone
@ SPITransDone
Definition: spi.h:101
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
ADXL345_REG_DATA_X0
#define ADXL345_REG_DATA_X0
Definition: adxl345_regs.h:40
SPICphaEdge2
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
adxl345_spi_send_config
static void adxl345_spi_send_config(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:79
ADXL_CONF_RATE
@ ADXL_CONF_RATE
Definition: adxl345.h:38
adxl345_spi_read
void adxl345_spi_read(struct Adxl345_Spi *adxl)
Definition: adxl345_spi.c:118
Adxl345_Spi::tx_buf
volatile uint8_t tx_buf[7]
Definition: adxl345_spi.h:42
adxl345_set_default_config
static void adxl345_set_default_config(struct Adxl345Config *c)
Definition: adxl345.h:56
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
Adxl345_Spi::config
struct Adxl345Config config
Definition: adxl345_spi.h:51
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: adxl345_spi.c:129
adxl345_data_format
static uint8_t adxl345_data_format(struct Adxl345Config *c)
Definition: adxl345.h:69
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
Adxl345_Spi::initialized
bool initialized
config done flag
Definition: adxl345_spi.h:45