Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
gps_ubx_i2c.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 ENAC, Pascal Brisset, Michel Gorraz,Gautier Hattenberger,
3 * 2016 Michael Sierra <sierramichael.a@gmail.com>
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 */
23
31
32#include "mcu_periph/i2c.h"
35#include <math.h>
36#include <string.h>
37
38// ublox i2c address
39#define GPS_I2C_SLAVE_ADDR (0x42 << 1)
40
41#ifndef GPS_UBX_I2C_DEV
42#error "GPS_UBX_I2C_DEV needs to be defined (e.g. to i2c1)"
43#endif
44PRINT_CONFIG_VAR(GPS_UBX_I2C_DEV)
45
46#define GPS_I2C_ADDR_NB_AVAIL_BYTES 0xFD
47#define GPS_I2C_ADDR_DATA 0xFF
48
49// Global variables
51
52// Local variables
55
60
66int gps_i2c_check_free_space(struct GpsUbxI2C *p, long *fd, uint16_t len);
67
73void gps_i2c_put_byte(struct GpsUbxI2C *p, long fd, uint8_t data);
74
80void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len);
81
85void gps_i2c_msg_ready(struct GpsUbxI2C *p, long fd);
86
91
96
98{
99#if GPS_UBX_UCENTER
101#else
103#endif
104
105 gps_i2c.read_state = gps_i2c_read_standby;
106 gps_i2c.write_state = gps_i2c_write_standby;
107
108 gps_i2c.trans.status = I2CTransDone;
109 gps_i2c.rx_buf_avail = 0;
110 gps_i2c.rx_buf_idx = 0;
111 gps_i2c.tx_buf_idx = 0;
112 gps_i2c.tx_rdy = TRUE;
113
114 gps_i2c.device.periph = (void *)&gps_i2c;
115 gps_i2c.device.check_free_space = (check_free_space_t)gps_i2c_check_free_space;
116 gps_i2c.device.put_byte = (put_byte_t)gps_i2c_put_byte;
117 gps_i2c.device.put_buffer = (put_buffer_t)gps_i2c_put_buffer;
118 gps_i2c.device.send_message = (send_message_t)gps_i2c_msg_ready;
119 gps_i2c.device.char_available = (char_available_t)gps_i2c_char_available;
120 gps_i2c.device.get_byte = (get_byte_t)gps_i2c_getch;
121 gps_i2c.device.set_baudrate = (set_baudrate_t)null_function;
122}
123
124void null_function(struct GpsUbxI2C *p __attribute__((unused)), uint32_t baudrate __attribute__((unused))) {}
125
126int gps_i2c_check_free_space(struct GpsUbxI2C *p __attribute__((unused)), long *fd __attribute__((unused)), uint16_t len)
127{
128 return (GPS_I2C_BUF_SIZE - gps_i2c.tx_buf_idx) >= len;
129}
130
131void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len)
132{
133 int i = 0;
134 for (i = 0; i < len; i++) {
135 gps_i2c_put_byte(p, fd, data[i]);
136 }
137}
138
139void gps_i2c_put_byte(struct GpsUbxI2C *p __attribute__((unused)), long fd __attribute__((unused)), uint8_t data)
140{
141 gps_i2c.tx_buf[gps_i2c.tx_buf_idx++] = data;
142}
143
144void gps_i2c_msg_ready(struct GpsUbxI2C *p __attribute__((unused)), long fd __attribute__((unused)))
145{
146 gps_i2c.write_state = gps_i2c_write_cfg;
147 gps_i2c.tx_rdy = FALSE;
148}
149
150int gps_i2c_char_available(struct GpsUbxI2C *p __attribute__((unused)))
151{
152 return (((int)gps_i2c.rx_buf_avail - (int)gps_i2c.rx_buf_idx) > 0);
153}
154
156{
157 return gps_i2c.tx_rdy;
158}
159
161{
163}
164
165uint8_t gps_i2c_getch(struct GpsUbxI2C *p __attribute__((unused)))
166{
167 return gps_i2c.rx_buf[gps_i2c.rx_buf_idx++];
168}
169
171{
172 if (gps_i2c.trans.status == I2CTransDone)
173 {
174 switch(gps_i2c.write_state)
175 {
178 {
180 {
182 } else {
183 gps_i2c.tx_rdy = TRUE;
184 }
185 }
186 break;
187
190 i2c_transceive(&GPS_UBX_I2C_DEV, &gps_i2c.trans, GPS_I2C_SLAVE_ADDR, 1, 2);
191 gps_i2c.write_state = gps_i2c_write_standby;
192 gps_i2c.read_state = gps_i2c_read_sizeof;
193 break;
194
196 memcpy((uint8_t *) & (gps_i2c.trans.buf), gps_i2c.tx_buf, gps_i2c.tx_buf_idx);
197 i2c_transmit(&GPS_UBX_I2C_DEV, &gps_i2c.trans, GPS_I2C_SLAVE_ADDR, gps_i2c.tx_buf_idx);
198 gps_i2c.tx_buf_idx = 0;
199 gps_i2c.read_state = gps_i2c_read_standby;
201 break;
202
203 default:
204 break;
205 }
206 }
207}
208
210{
211 switch(gps_i2c.read_state)
212 {
214 break;
215 break;
216
217 // how many bytes are available
219 gps_ubx_i2c_bytes_to_read = ((uint16_t)(gps_i2c.trans.buf[0]) << 7) | (uint16_t)(gps_i2c.trans.buf[1]);
220 gps_i2c.trans.status = I2CTransDone;
221 if (gps_ubx_i2c_bytes_to_read == 0xFFFF || gps_ubx_i2c_bytes_to_read == 0x0000)
222 {
224 return;
226 {
227 gps_i2c.trans.buf[0] = GPS_I2C_ADDR_DATA;
228 i2c_transceive(&GPS_UBX_I2C_DEV, &gps_i2c.trans, GPS_I2C_SLAVE_ADDR, 1, GPS_I2C_BUF_SIZE);
229 gps_i2c.read_state = gps_i2c_read_data;
230 } else
231 {
232 gps_i2c.trans.buf[0] = GPS_I2C_ADDR_DATA;
234 gps_i2c.read_state = gps_i2c_read_data;
235 return;
236 }
237 break;
240 {
241 memcpy(&gps_i2c.rx_buf, (uint8_t *) & (gps_i2c.trans.buf), GPS_I2C_BUF_SIZE);
242 gps_i2c.rx_buf_idx = 0;
243 gps_i2c.rx_buf_avail = GPS_I2C_BUF_SIZE;
244 } else
245 {
246 memcpy(&gps_i2c.rx_buf, (uint8_t *) & (gps_i2c.trans.buf), gps_ubx_i2c_bytes_to_read);
247 gps_i2c.rx_buf_idx = 0;
248 gps_i2c.rx_buf_avail = gps_ubx_i2c_bytes_to_read;
249 }
250
251 gps_i2c.write_state = gps_i2c_write_standby;
252 gps_i2c.read_state = gps_i2c_read_standby;
253 break;
254
255 default:
256 break;
257 }
258
259 // Transaction has been read
260 gps_i2c.trans.status = I2CTransDone;
261}
void gps_ubx_i2c_init(void)
init function
Definition gps_ubx_i2c.c:97
#define GPS_I2C_ADDR_DATA
data stream register
Definition gps_ubx_i2c.c:47
void gps_i2c_begin(void)
config is done, begin reading messages
int gps_i2c_check_free_space(struct GpsUbxI2C *p, long *fd, uint16_t len)
Check available space in transmit buffer.
uint8_t gps_i2c_getch(struct GpsUbxI2C *p)
get a new char
struct GpsUbxI2C gps_i2c
Definition gps_ubx_i2c.c:50
bool gps_ubx_i2c_ucenter_done
ucenter finished configuring flag
Definition gps_ubx_i2c.c:53
uint16_t gps_ubx_i2c_bytes_to_read
ublox bytes to read
Definition gps_ubx_i2c.c:54
bool gps_i2c_tx_is_ready(void)
is driver ready to send a message
#define GPS_I2C_ADDR_NB_AVAIL_BYTES
number of bytes available register
Definition gps_ubx_i2c.c:46
void gps_ubx_i2c_periodic(void)
handle message sending
#define GPS_I2C_SLAVE_ADDR
Definition gps_ubx_i2c.c:39
void gps_ubx_i2c_read_event(void)
handle message reception
void gps_i2c_put_byte(struct GpsUbxI2C *p, long fd, uint8_t data)
Put byte into transmit buffer.
void gps_i2c_msg_ready(struct GpsUbxI2C *p, long fd)
send buffer when ready
int gps_i2c_char_available(struct GpsUbxI2C *p)
check if a new character is available
void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len)
Put bytes into transmit buffer.
void null_function(struct GpsUbxI2C *p, uint32_t baudrate)
null function
pprz link device for Ublox over I2C
@ gps_i2c_read_data
read data from ubx buffer
Definition gps_ubx_i2c.h:47
@ gps_i2c_read_standby
dont read anything
Definition gps_ubx_i2c.h:45
@ gps_i2c_read_sizeof
read size of ubx buffer
Definition gps_ubx_i2c.h:46
#define GPS_I2C_BUF_SIZE
Definition gps_ubx_i2c.h:39
@ gps_i2c_write_cfg
send a config msg and get reply
Definition gps_ubx_i2c.h:56
@ gps_i2c_write_request_size
request size of ubx buffer
Definition gps_ubx_i2c.h:55
@ gps_i2c_write_standby
wait for gps_ubx to read buffer or ucenter to transmit
Definition gps_ubx_i2c.h:54
int baudrate
baudrate, unused
Definition gps_ubx_i2c.h:77
ubx_i2c state
Definition gps_ubx_i2c.h:62
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
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
static float p[2][2]
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
int fd
Definition serial.c:26
#define TRUE
Definition std.h:4
#define FALSE
Definition std.h:5
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.