Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
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
44 PRINT_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
50 struct GpsUbxI2C gps_i2c;
51 
52 // Local variables
55 
60 
66 int gps_i2c_check_free_space(struct GpsUbxI2C *p, long *fd, uint16_t len);
67 
73 void gps_i2c_put_byte(struct GpsUbxI2C *p, long fd, uint8_t data);
74 
80 void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len);
81 
85 void gps_i2c_msg_ready(struct GpsUbxI2C *p, long fd);
86 
91 
96 
97 void gps_ubx_i2c_init(void)
98 {
99 #if GPS_UBX_UCENTER
101 #else
103 #endif
104 
107 
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 
124 void null_function(struct GpsUbxI2C *p __attribute__((unused)), uint32_t baudrate __attribute__((unused))) {}
125 
126 int 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 
131 void 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 
139 void 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 
144 void gps_i2c_msg_ready(struct GpsUbxI2C *p __attribute__((unused)), long fd __attribute__((unused)))
145 {
147  gps_i2c.tx_rdy = FALSE;
148 }
149 
150 int 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 
160 void gps_i2c_begin(void)
161 {
163 }
164 
165 uint8_t gps_i2c_getch(struct GpsUbxI2C *p __attribute__((unused)))
166 {
167  return gps_i2c.rx_buf[gps_i2c.rx_buf_idx++];
168 }
169 
171 {
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);
193  break;
194 
195  case gps_i2c_write_cfg:
198  gps_i2c.tx_buf_idx = 0;
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
218  case gps_i2c_read_sizeof:
221  if (gps_ubx_i2c_bytes_to_read == 0xFFFF || gps_ubx_i2c_bytes_to_read == 0x0000)
222  {
224  return;
226  {
230  } else
231  {
235  return;
236  }
237  break;
238  case gps_i2c_read_data:
240  {
242  gps_i2c.rx_buf_idx = 0;
244  } else
245  {
247  gps_i2c.rx_buf_idx = 0;
249  }
250 
253  break;
254 
255  default:
256  break;
257  }
258 
259  // Transaction has been read
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
Definition: gps_ubx_i2c.c:160
int gps_i2c_check_free_space(struct GpsUbxI2C *p, long *fd, uint16_t len)
Check available space in transmit buffer.
Definition: gps_ubx_i2c.c:126
uint8_t gps_i2c_getch(struct GpsUbxI2C *p)
get a new char
Definition: gps_ubx_i2c.c:165
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
Definition: gps_ubx_i2c.c:155
#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
Definition: gps_ubx_i2c.c:170
#define GPS_I2C_SLAVE_ADDR
Definition: gps_ubx_i2c.c:39
void gps_ubx_i2c_read_event(void)
handle message reception
Definition: gps_ubx_i2c.c:209
void gps_i2c_put_byte(struct GpsUbxI2C *p, long fd, uint8_t data)
Put byte into transmit buffer.
Definition: gps_ubx_i2c.c:139
void gps_i2c_msg_ready(struct GpsUbxI2C *p, long fd)
send buffer when ready
Definition: gps_ubx_i2c.c:144
int gps_i2c_char_available(struct GpsUbxI2C *p)
check if a new character is available
Definition: gps_ubx_i2c.c:150
void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len)
Put bytes into transmit buffer.
Definition: gps_ubx_i2c.c:131
void null_function(struct GpsUbxI2C *p, uint32_t baudrate)
null function
Definition: gps_ubx_i2c.c:124
pprz link device for Ublox over I2C
struct i2c_transaction trans
i2c transaction
Definition: gps_ubx_i2c.h:75
uint16_t rx_buf_avail
how many bytes are waiting to be read
Definition: gps_ubx_i2c.h:69
uint16_t tx_buf_idx
tx buf index
Definition: gps_ubx_i2c.h:71
uint16_t rx_buf_idx
rx buf index
Definition: gps_ubx_i2c.h:70
@ 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
bool tx_rdy
are we ready to transmit
Definition: gps_ubx_i2c.h:73
uint8_t tx_buf[GPS_I2C_BUF_SIZE]
transmit buffer
Definition: gps_ubx_i2c.h:67
#define GPS_I2C_BUF_SIZE
Definition: gps_ubx_i2c.h:39
struct link_device device
ppz link device
Definition: gps_ubx_i2c.h:79
uint8_t rx_buf[GPS_I2C_BUF_SIZE]
receive buffer
Definition: gps_ubx_i2c.h:66
@ 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
GpsI2CReadState read_state
Definition: gps_ubx_i2c.h:63
int baudrate
baudrate, unused
Definition: gps_ubx_i2c.h:77
GpsI2CWriteState write_state
Definition: gps_ubx_i2c.h:64
ubx_i2c state
Definition: gps_ubx_i2c.h:62
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
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
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
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
static float p[2][2]
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.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98