Paparazzi UAS v7.0_unstable
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
35void 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 */
41 adxl->spi_trans.cpol = SPICpolIdleHigh;
42 adxl->spi_trans.cpha = SPICphaEdge2;
43 adxl->spi_trans.dss = SPIDss8bit;
44 adxl->spi_trans.bitorder = SPIMSBFirst;
45 adxl->spi_trans.cdiv = SPIDiv64;
46
47 adxl->spi_trans.select = SPISelectUnselect;
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 */
58 adxl->spi_trans.status = SPITransDone;
59
60 /* set default ADXL345 config options */
62
63 adxl->initialized = false;
64 adxl->data_available = false;
65 adxl->init_status = ADXL_CONF_UNINIT;
66}
67
68
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
80{
81 switch (adxl->init_status) {
82 case ADXL_CONF_RATE:
84 adxl->init_status++;
85 break;
86 case ADXL_CONF_INT:
87 adxl345_spi_write_to_reg(adxl, ADXL345_REG_INT_ENABLE, (adxl->config.drdy_int_enable << 7));
88 adxl->init_status++;
89 break;
92 adxl->init_status++;
93 break;
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
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
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}
static void adxl345_set_default_config(struct Adxl345Config *c)
Definition adxl345.h:56
@ ADXL_CONF_FORMAT
Definition adxl345.h:40
@ ADXL_CONF_DONE
Definition adxl345.h:42
@ ADXL_CONF_RATE
Definition adxl345.h:38
@ ADXL_CONF_INT
Definition adxl345.h:39
@ ADXL_CONF_ENABLE
Definition adxl345.h:41
@ ADXL_CONF_UNINIT
Definition adxl345.h:37
static uint8_t adxl345_data_format(struct Adxl345Config *c)
Definition adxl345.h:69
#define ADXL345_REG_DATA_X0
#define ADXL345_REG_BW_RATE
#define ADXL345_REG_DATA_FORMAT
#define ADXL345_REG_POWER_CTL
#define ADXL345_REG_INT_ENABLE
static void adxl345_spi_send_config(struct Adxl345_Spi *adxl)
Definition adxl345_spi.c:79
void adxl345_spi_init(struct Adxl345_Spi *adxl, struct spi_periph *spi_p, uint8_t slave_idx)
Definition adxl345_spi.c:35
void adxl345_spi_start_configure(struct Adxl345_Spi *adxl)
static void adxl345_spi_write_to_reg(struct Adxl345_Spi *adxl, uint8_t _reg, uint8_t _val)
Definition adxl345_spi.c:69
void adxl345_spi_event(struct Adxl345_Spi *adxl)
void adxl345_spi_read(struct Adxl345_Spi *adxl)
#define Int16FromBuf(_buf, _idx)
Driver for the accelerometer ADXL345 from Analog Devices using SPI.
enum SPIStatus status
internal state of the peripheral
Definition spi.h:180
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition spi_arch.c:533
@ SPICphaEdge2
CPHA = 1.
Definition spi.h:75
@ SPITransFailed
Definition spi.h:100
@ SPITransSuccess
Definition spi.h:99
@ SPITransDone
Definition spi.h:101
@ SPICpolIdleHigh
CPOL = 1.
Definition spi.h:84
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition spi.h:63
@ SPIMSBFirst
Definition spi.h:112
@ SPIDiv64
Definition spi.h:125
@ SPIDss8bit
Definition spi.h:90
SPI peripheral structure.
Definition spi.h:174
uint16_t foo
Definition main_demo5.c:58
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.