Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
27
28#include "peripherals/ist8310.h"
29#include "std.h"
30
34void 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
46void 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
98void ist8310_read(struct IST8310 *ist)
99{
100 if (ist->status != IST_STATUS_IDLE) {
101 return;
102 }
103
104 // Read the status and the measurement triggered at the previous call (the conversion takes several ms)
106 i2c_transceive(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 1, 7);
107 ist->status = IST_STATUS_READ;
108}
109
111static void ist8310_trigger(struct IST8310 *ist)
112{
115 i2c_transmit(ist->i2c_p, &(ist->i2c_trans), ist->i2c_trans.slave_addr, 2);
116 ist->status = IST_STATUS_DONE;
117}
118
119#define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
120void ist8310_event(struct IST8310 *ist)
121{
122 if (!ist->initialized) {
123 return;
124 }
125
126 switch (ist->status) {
127 case IST_STATUS_IDLE:
128 break;
129
130 case IST_STATUS_READ:
131 if (ist->i2c_trans.status == I2CTransSuccess) {
132 // Copy the data when the measurement is ready
133 if (ist->i2c_trans.buf[0] & IST8310_STAT1_DRDY) {
134 ist->data.vect.x = Int16FromBuf(ist->i2c_trans.buf, 1);
135 ist->data.vect.y = Int16FromBuf(ist->i2c_trans.buf, 3);
136 ist->data.vect.z = Int16FromBuf(ist->i2c_trans.buf, 5);
137 ist->data_available = true;
138 }
139 ist8310_trigger(ist);
140 } else if(ist->i2c_trans.status == I2CTransFailed) {
141 ist8310_trigger(ist);
142 }
143 break;
144
145 default:
146 // Measurement triggered, goto idle
149 ist->status = IST_STATUS_IDLE;
150 }
151 break;
152 }
153}
154
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:123
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:127
uint8_t slave_addr
Slave address.
Definition i2c.h:105
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:202
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:222
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
@ I2CTransFailed
transaction failed
Definition i2c.h:59
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
static void ist8310_trigger(struct IST8310 *ist)
Trigger a single measurement, read at the next ist8310_read.
Definition ist8310.c:111
void ist8310_read(struct IST8310 *ist)
Definition ist8310.c:98
void ist8310_event(struct IST8310 *ist)
Definition ist8310.c:120
void ist8310_init(struct IST8310 *ist, struct i2c_periph *i2c_p, uint8_t addr)
Initialize IST8310 struct.
Definition ist8310.c:34
void ist8310_configure(struct IST8310 *ist)
Definition ist8310.c:46
#define Int16FromBuf(_buf, _idx)
Definition ist8310.c:119
struct i2c_transaction i2c_trans
i2c transaction used for communication with the ist8310
Definition ist8310.h:57
bool initialized
config done flag
Definition ist8310.h:58
enum IST8310ConfStatus init_status
init status
Definition ist8310.h:61
enum IST8310Status status
main status
Definition ist8310.h:60
@ IST_STATUS_READ
Definition ist8310.h:50
@ IST_STATUS_DONE
Definition ist8310.h:51
@ IST_STATUS_IDLE
Definition ist8310.h:49
struct i2c_periph * i2c_p
peripheral used for communcation
Definition ist8310.h:56
union IST8310::@141061146375326332372216374104355333056204007377 data
volatile bool data_available
data ready flag
Definition ist8310.h:63
@ IST_CONF_CNTL3
Definition ist8310.h:42
@ IST_CONF_CNTL1
Definition ist8310.h:41
@ IST_CONF_UNINIT
Definition ist8310.h:40
@ IST_CONF_CNTL4
Definition ist8310.h:43
Default IST8310 structure.
Definition ist8310.h:55
#define IST8310_REG_CNTL3
#define IST8310_REG_STAT1
#define IST8310_REG_CNTL4
#define IST8310_STAT1_DRDY
#define IST8310_CNTL1_ODR_SINGLE
#define IST8310_CNTL3_SAMPAVG_16
#define IST8310_REG_CNTL1
#define IST8310_CNTL4_SRPD
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.