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
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 
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
enum Ads1220ConvMode conv
conversion mode
Definition: ads1220.h:135
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
uint16_t output_length
number of data words to write
Definition: spi.h:146
void ads1220_event(struct Ads1220 *ads)
Definition: ads1220.c:130
enum Ads1220Imux i2mux
IDAC routing 2.
Definition: ads1220.h:139
volatile bool data_available
data ready flag
Definition: ads1220.h:161
enum Ads1220Mux mux
input multiplexer
Definition: ads1220.h:131
CPHA = 1.
Definition: spi.h:69
void ads1220_configure(struct Ads1220 *ads)
Definition: ads1220.c:102
#define ADS1220_RESET
Definition: ads1220.c:36
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:458
volatile uint8_t rx_buf[ADS1220_BUFFER_LEN]
receive buffer
Definition: ads1220.h:156
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
struct spi_transaction spi_trans
spi transaction
Definition: ads1220.h:154
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
struct spi_periph * spi_p
spi peripheral
Definition: ads1220.h:153
struct Ads1220Config config
configuration
Definition: ads1220.h:158
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
Driver for the ADS1220 24-bits ADC from TI SPI communication.
SPI peripheral structure.
Definition: spi.h:168
enum Ads1220Gain gain
gain
Definition: ads1220.h:132
Definition: spi.h:84
static void ads1220_send_config(struct Ads1220 *ads)
Definition: ads1220.c:80
bool pga_bypass
bypass PGA (PGA enabled = 0)
Definition: ads1220.h:133
enum Ads1220VRef vref
voltage ref
Definition: ads1220.h:136
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
unsigned long uint32_t
Definition: types.h:18
CPOL = 0.
Definition: spi.h:77
uint16_t input_length
number of data words to read
Definition: spi.h:145
#define ADS1220_CONF0
Definition: ads1220.c:42
void ads1220_read(struct Ads1220 *ads)
Definition: ads1220.c:120
enum Ads1220SampleRate rate
data output rate
Definition: ads1220.h:134
enum Ads1220ConfStatus status
config status
Definition: ads1220.h:130
unsigned char uint8_t
Definition: types.h:14
uint32_t data
raw ADC value
Definition: ads1220.h:160
slave is selected before transaction and unselected after
Definition: spi.h:57
enum Ads1220Idac idac
IDAC config.
Definition: ads1220.h:137
void ads1220_init(struct Ads1220 *ads, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ads1220.c:49
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
#define ADS1220_WREG(_reg, _nb)
Definition: ads1220.c:34
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
enum Ads1220Imux i1mux
IDAC routing 1.
Definition: ads1220.h:138
Definition: spi.h:120
enum SPITransactionStatus status
Definition: spi.h:156
volatile uint8_t tx_buf[ADS1220_BUFFER_LEN]
transmit buffer
Definition: ads1220.h:155