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
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
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;
40 ist->i2c_trans.status = I2CTransDone;
41 ist->initialized = false;
42 ist->init_status = IST_CONF_UNINIT;
43 ist->data_available = false;
44}
45
47{
48 // Only configure when not busy
49 if (ist->i2c_trans.status != I2CTransSuccess && ist->i2c_trans.status != I2CTransFailed
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
59 ist->i2c_trans.status = I2CTransDone;
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);
67 ist->i2c_trans.status = I2CTransSuccess;
68 break;
69
70 // Configure for 50 Hz output
71 case IST_CONF_CNTL1:
72 ist->i2c_trans.buf[0] = IST8310_REG_CNTL1;
73 ist->i2c_trans.buf[1] = IST8310_CNTL1_ODR_SINGLE;
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:
79 ist->i2c_trans.buf[0] = IST8310_REG_CNTL3;
80 ist->i2c_trans.buf[1] = IST8310_CNTL3_SAMPAVG_16;
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:
86 ist->i2c_trans.buf[0] = IST8310_REG_CNTL4;
87 ist->i2c_trans.buf[1] = IST8310_CNTL4_SRPD;
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
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;
106 ist->i2c_trans.buf[1] = IST8310_CNTL1_ODR_SINGLE;
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)))
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) {
121 ist->i2c_trans.buf[0] = IST8310_REG_DATA_XL;
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
135 ist->i2c_trans.status = I2CTransDone;
136 ist->status = IST_STATUS_IDLE;
137 } else if(ist->i2c_trans.status == I2CTransFailed) {
138 ist->i2c_trans.status = I2CTransDone;
139 ist->status = IST_STATUS_IDLE;
140 }
141 break;
142
143 default:
144 // Goto idle
145 ist->i2c_trans.status = I2CTransDone;
146 ist->status = IST_STATUS_IDLE;
147 break;
148 }
149}
150
enum I2CStatus status
Definition i2c.h:155
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:57
@ I2CTransFailed
transaction failed
Definition i2c.h:58
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:59
void ist8310_read(struct IST8310 *ist)
Definition ist8310.c:98
void ist8310_event(struct IST8310 *ist)
Definition ist8310.c:111
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:110
@ IST_STATUS_READ
Definition ist8310.h:50
@ IST_STATUS_IDLE
Definition ist8310.h:49
@ 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_CNTL4
#define IST8310_CNTL1_ODR_SINGLE
#define IST8310_REG_DATA_XL
#define IST8310_CNTL3_SAMPAVG_16
#define IST8310_REG_CNTL1
#define IST8310_CNTL4_SRPD
uint16_t foo
Definition main_demo5.c:58
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.