Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
ads1220.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger, Alexandre Bustico
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, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
31 #include "peripherals/ads1220.h"
32 
33 // Commands
34 #define ADS1220_WREG(_reg, _nb) ((1<<6)|(_reg<<2)|(_nb-1))
35 #define ADS1220_RREG(_reg, _nb) ((1<<5)|(_reg<<2)|(_nb-1))
36 #define ADS1220_RESET 0x06
37 #define ADS1220_START_SYNC 0x08
38 #define ADS1220_POWERDOWN 0x02
39 #define ADS1220_RDATA 0x10
40 
41 // Conf registers
42 #define ADS1220_CONF0 0x0
43 #define ADS1220_CONF1 0x1
44 #define ADS1220_CONF2 0x2
45 #define ADS1220_CONF3 0x3
46 
47 
48 // Init function
49 void ads1220_init(struct Ads1220 *ads, struct spi_periph *spi_p, uint8_t slave_idx)
50 {
51  /* set spi_peripheral */
52  ads->spi_p = spi_p;
53 
54  /* configure spi transaction */
57  ads->spi_trans.dss = SPIDss8bit;
59  ads->spi_trans.cdiv = SPIDiv128; // f_PCLK / div
60 
62  ads->spi_trans.slave_idx = slave_idx;
63  ads->spi_trans.output_length = 0;
64  ads->spi_trans.input_length = 0;
65  ads->spi_trans.before_cb = NULL;
66  ads->spi_trans.after_cb = NULL;
67  ads->spi_trans.input_buf = &(ads->rx_buf[0]);
68  ads->spi_trans.output_buf = &(ads->tx_buf[0]);
69 
70  /* set inital status: Success or Done */
72 
73  ads->data = 0;
74  ads->data_available = false;
76 }
77 
78 
79 // Configuration function called once before normal use
80 static void ads1220_send_config(struct Ads1220 *ads)
81 {
82  ads->spi_trans.output_length = 5;
83  ads->spi_trans.input_length = 0;
84  ads->tx_buf[0] = ADS1220_WREG(ADS1220_CONF0, 4);
85  ads->tx_buf[1] = (
86  (ads->config.pga_bypass << 0) |
87  (ads->config.gain << 1) |
88  (ads->config.mux << 4));
89  ads->tx_buf[2] = (
90  (ads->config.conv << 2) |
91  (ads->config.rate << 5));
92  ads->tx_buf[3] = (
93  (ads->config.idac << 0) |
94  (ads->config.vref << 6));
95  ads->tx_buf[4] = (
96  (ads->config.i2mux << 2) |
97  (ads->config.i1mux << 5));
98  spi_submit(ads->spi_p, &(ads->spi_trans));
99 }
100 
101 // Configuration function called before normal use
102 void ads1220_configure(struct Ads1220 *ads)
103 {
104  if (ads->config.status == ADS1220_UNINIT) {
105  if (ads->spi_trans.status == SPITransSuccess || ads->spi_trans.status == SPITransDone) {
106  ads->spi_trans.output_length = 1;
107  ads->spi_trans.input_length = 0;
108  ads->tx_buf[0] = ADS1220_RESET;
109  spi_submit(ads->spi_p, &(ads->spi_trans));
111  }
112  } else if (ads->config.status == ADS1220_INITIALIZING) { // Configuring but not yet initialized
113  if (ads->spi_trans.status == SPITransSuccess || ads->spi_trans.status == SPITransDone) {
114  ads1220_send_config(ads); // do config
115  }
116  }
117 }
118 
119 // Read next data
120 void ads1220_read(struct Ads1220 *ads)
121 {
123  ads->spi_trans.output_length = 0;
124  ads->spi_trans.input_length = 3;
125  spi_submit(ads->spi_p, &(ads->spi_trans));
126  }
127 }
128 
129 // Check end of transaction
130 void ads1220_event(struct Ads1220 *ads)
131 {
132  if (ads->config.status == ADS1220_INITIALIZED) {
133  if (ads->spi_trans.status == SPITransFailed) {
135  } else if (ads->spi_trans.status == SPITransSuccess) {
136  // Successfull reading of 24bits adc
137  ads->data = (uint32_t)(((uint32_t)(ads->rx_buf[0]) << 16) | ((uint32_t)(ads->rx_buf[1]) << 8) | (ads->rx_buf[2]));
138  ads->data_available = true;
140  }
141  } else if (ads->config.status == ADS1220_SEND_RESET) { // Reset ads1220 before configuring
142  if (ads->spi_trans.status == SPITransFailed) {
144  ads->config.status = ADS1220_UNINIT; // config failed
145  } else if (ads->spi_trans.status == SPITransSuccess) {
148  // do config at next call of ads1220_configure() (or ads1220_periodic())
149  }
150  } else if (ads->config.status == ADS1220_INITIALIZING) { // Configuring but not yet initialized
151  if (ads->spi_trans.status == SPITransFailed) {
153  ads->config.status = ADS1220_UNINIT; // config failed
154  } else if (ads->spi_trans.status == SPITransSuccess) {
156  ads->config.status = ADS1220_INITIALIZED; // config done
157  }
158  }
159 }
160 
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
ads1220_configure
void ads1220_configure(struct Ads1220 *ads)
Definition: ads1220.c:102
ADS1220_UNINIT
@ ADS1220_UNINIT
Definition: ads1220.h:40
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
Ads1220::data_available
volatile bool data_available
data ready flag
Definition: ads1220.h:161
Ads1220Config::mux
enum Ads1220Mux mux
input multiplexer
Definition: ads1220.h:131
ads1220.h
Ads1220::tx_buf
volatile uint8_t tx_buf[ADS1220_BUFFER_LEN]
transmit buffer
Definition: ads1220.h:155
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
ads1220_event
void ads1220_event(struct Ads1220 *ads)
Definition: ads1220.c:130
Ads1220::config
struct Ads1220Config config
configuration
Definition: ads1220.h:158
Ads1220Config::i2mux
enum Ads1220Imux i2mux
IDAC routing 2.
Definition: ads1220.h:139
uint32_t
unsigned long uint32_t
Definition: types.h:18
Ads1220::spi_p
struct spi_periph * spi_p
spi peripheral
Definition: ads1220.h:153
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
ads1220_send_config
static void ads1220_send_config(struct Ads1220 *ads)
Definition: ads1220.c:80
Ads1220Config::pga_bypass
bool pga_bypass
bypass PGA (PGA enabled = 0)
Definition: ads1220.h:133
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
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
Ads1220Config::gain
enum Ads1220Gain gain
gain
Definition: ads1220.h:132
ADS1220_RESET
#define ADS1220_RESET
Definition: ads1220.c:36
ADS1220_INITIALIZED
@ ADS1220_INITIALIZED
Definition: ads1220.h:43
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
Ads1220::rx_buf
volatile uint8_t rx_buf[ADS1220_BUFFER_LEN]
receive buffer
Definition: ads1220.h:156
SPICpolIdleLow
@ SPICpolIdleLow
CPOL = 0.
Definition: spi.h:83
Ads1220::spi_trans
struct spi_transaction spi_trans
spi transaction
Definition: ads1220.h:154
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
ADS1220_CONF0
#define ADS1220_CONF0
Definition: ads1220.c:42
SPIDiv128
@ SPIDiv128
Definition: spi.h:126
Ads1220Config::vref
enum Ads1220VRef vref
voltage ref
Definition: ads1220.h:136
uint8_t
unsigned char uint8_t
Definition: types.h:14
ADS1220_SEND_RESET
@ ADS1220_SEND_RESET
Definition: ads1220.h:41
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
ads1220_init
void ads1220_init(struct Ads1220 *ads, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ads1220.c:49
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
Ads1220Config::rate
enum Ads1220SampleRate rate
data output rate
Definition: ads1220.h:134
Ads1220
Definition: ads1220.h:151
SPITransDone
@ SPITransDone
Definition: spi.h:101
ADS1220_WREG
#define ADS1220_WREG(_reg, _nb)
Definition: ads1220.c:34
Ads1220Config::status
enum Ads1220ConfStatus status
config status
Definition: ads1220.h:130
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
SPICphaEdge2
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
Ads1220Config::idac
enum Ads1220Idac idac
IDAC config.
Definition: ads1220.h:137
ads1220_read
void ads1220_read(struct Ads1220 *ads)
Definition: ads1220.c:120
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
Ads1220::data
uint32_t data
raw ADC value
Definition: ads1220.h:160
Ads1220Config::i1mux
enum Ads1220Imux i1mux
IDAC routing 1.
Definition: ads1220.h:138
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
ADS1220_INITIALIZING
@ ADS1220_INITIALIZING
Definition: ads1220.h:42
Ads1220Config::conv
enum Ads1220ConvMode conv
conversion mode
Definition: ads1220.h:135