Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ist8310.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Freek van Tienen <freek.v.tienen@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 
28 #include "peripherals/ist8310.h"
29 #include "std.h"
30 
34 void ist8310_init(struct IST8310 *ist, struct i2c_periph *i2c_p, uint8_t addr)
35 {
36  /* set i2c_peripheral */
37  ist->i2c_p = i2c_p;
38  /* set i2c address */
39  ist->i2c_trans.slave_addr = addr;
41  ist->initialized = false;
43  ist->data_available = false;
44 }
45 
46 void ist8310_configure(struct IST8310 *ist)
47 {
48  // Only configure when not busy
50  && ist->i2c_trans.status != I2CTransDone) {
51  return;
52  }
53 
54  // Only when succesfull continue with next
55  if (ist->i2c_trans.status == I2CTransSuccess) {
56  ist->init_status++;
57  }
58 
60  switch (ist->init_status) {
61 
62  // Reset the device
63  case IST_CONF_UNINIT:
64  //ist->i2c_trans.buf[0] = IST8310_REG_CNTL2;
65  //ist->i2c_trans.buf[1] = IST8310_CNTL2_DREN | IST8310_CNTL2_DRP;
66  //i2c_transmit(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 2);
68  break;
69 
70  // Configure for 50 Hz output
71  case IST_CONF_CNTL1:
74  i2c_transmit(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 2);
75  break;
76 
77  // Average 16 samples
78  case IST_CONF_CNTL3:
81  i2c_transmit(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 2);
82  break;
83 
84  // Set reset pulse duration to normal
85  case IST_CONF_CNTL4:
88  i2c_transmit(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 2);
89  break;
90 
91  // Initialization done
92  default:
93  ist->initialized = true;
94  break;
95  }
96 }
97 
98 void ist8310_read(struct IST8310 *ist)
99 {
100  if (ist->status != IST_STATUS_IDLE) {
101  return;
102  }
103 
104  // Start a single read
105  ist->i2c_trans.buf[0] = IST8310_REG_CNTL1;
107  i2c_transceive(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 2, 0);
108 }
109 
110 #define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
111 void ist8310_event(struct IST8310 *ist)
112 {
113  if (!ist->initialized) {
114  return;
115  }
116 
117  switch (ist->status) {
118  case IST_STATUS_IDLE:
119  // When succesfully send single read
120  if (ist->i2c_trans.status == I2CTransSuccess) {
122  i2c_transceive(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 1, 6);
123  ist->status++;
124  }
125  break;
126 
127  case IST_STATUS_READ:
128  if (ist->i2c_trans.status == I2CTransSuccess) {
129  // Copy the data
130  ist->data.vect.x = Int16FromBuf(ist->i2c_trans.buf, 0);
131  ist->data.vect.y = Int16FromBuf(ist->i2c_trans.buf, 2);
132  ist->data.vect.z = Int16FromBuf(ist->i2c_trans.buf, 4);
133  ist->data_available = true;
134 
136  ist->status = IST_STATUS_IDLE;
137  } else if(ist->i2c_trans.status == I2CTransFailed) {
139  ist->status = IST_STATUS_IDLE;
140  }
141  break;
142 
143  default:
144  // Goto idle
146  ist->status = IST_STATUS_IDLE;
147  break;
148  }
149 }
150 
#define IST8310_CNTL1_ODR_SINGLE
Definition: ist8310_regs.h:77
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
transaction successfully finished by I2C driver
Definition: i2c.h:57
void ist8310_configure(struct IST8310 *ist)
Definition: ist8310.c:46
#define IST8310_CNTL3_SAMPAVG_16
Definition: ist8310_regs.h:92
void ist8310_init(struct IST8310 *ist, struct i2c_periph *i2c_p, uint8_t addr)
Initialize IST8310 struct.
Definition: ist8310.c:34
Default IST8310 structure.
Definition: ist8310.h:55
#define IST8310_REG_CNTL1
Definition: ist8310_regs.h:43
bool initialized
config done flag
Definition: ist8310.h:58
enum IST8310ConfStatus init_status
init status
Definition: ist8310.h:61
#define IST8310_CNTL4_SRPD
Definition: ist8310_regs.h:99
#define Int16FromBuf(_buf, _idx)
Definition: ist8310.c:110
transaction set to done by user level
Definition: i2c.h:59
void ist8310_event(struct IST8310 *ist)
Definition: ist8310.c:111
transaction failed
Definition: i2c.h:58
struct i2c_transaction i2c_trans
i2c transaction used for communication with the ist8310
Definition: ist8310.h:57
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:344
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
union IST8310::@316 data
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
#define IST8310_REG_DATA_XL
Definition: ist8310_regs.h:36
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
unsigned char uint8_t
Definition: types.h:14
volatile bool data_available
data ready flag
Definition: ist8310.h:63
void ist8310_read(struct IST8310 *ist)
Definition: ist8310.c:98
struct i2c_periph * i2c_p
peripheral used for communcation
Definition: ist8310.h:56
enum IST8310Status status
main status
Definition: ist8310.h:60
#define IST8310_REG_CNTL3
Definition: ist8310_regs.h:48
#define IST8310_REG_CNTL4
Definition: ist8310_regs.h:49