Paparazzi UAS  v5.15_devel-110-g5cf27ee
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
37 // ublox i2c address
38 #define GPS_I2C_SLAVE_ADDR (0x42 << 1)
39 
40 #ifndef GPS_UBX_I2C_DEV
41 #error "GPS_UBX_I2C_DEV needs to be defined (e.g. to i2c1)"
42 #endif
43 PRINT_CONFIG_VAR(GPS_UBX_I2C_DEV)
44 
45 #define GPS_I2C_ADDR_NB_AVAIL_BYTES 0xFD
46 #define GPS_I2C_ADDR_DATA 0xFF
47 
48 // Global variables
50 
51 // Local variables
54 
58 void null_function(struct GpsUbxI2C *p);
59 
65 int gps_i2c_check_free_space(struct GpsUbxI2C *p, long *fd, uint16_t len);
66 
72 void gps_i2c_put_byte(struct GpsUbxI2C *p, long fd, uint8_t data);
73 
79 void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len);
80 
84 void gps_i2c_msg_ready(struct GpsUbxI2C *p, long fd);
85 
90 
95 
96 void gps_ubx_i2c_init(void)
97 {
98 #if GPS_UBX_UCENTER
99  gps_ubx_i2c_ucenter_done = FALSE;
100 #else
101  gps_ubx_i2c_ucenter_done = TRUE;
102 #endif
103 
106 
108  gps_i2c.rx_buf_avail = 0;
109  gps_i2c.rx_buf_idx = 0;
110  gps_i2c.tx_buf_idx = 0;
111  gps_i2c.tx_rdy = TRUE;
112 
113  gps_i2c.device.periph = (void *)&gps_i2c;
114  gps_i2c.device.check_free_space = (check_free_space_t)gps_i2c_check_free_space;
115  gps_i2c.device.put_byte = (put_byte_t)gps_i2c_put_byte;
116  gps_i2c.device.put_buffer = (put_buffer_t)gps_i2c_put_buffer;
117  gps_i2c.device.send_message = (send_message_t)gps_i2c_msg_ready;
118  gps_i2c.device.char_available = (char_available_t)gps_i2c_char_available;
119  gps_i2c.device.get_byte = (get_byte_t)gps_i2c_getch;
120  gps_i2c.device.set_baudrate = (set_baudrate_t)null_function;
121 }
122 
123 void null_function(struct GpsUbxI2C *p __attribute__((unused))) {}
124 
125 int gps_i2c_check_free_space(struct GpsUbxI2C *p __attribute__((unused)), long *fd __attribute__((unused)), uint16_t len)
126 {
127  return (GPS_I2C_BUF_SIZE - gps_i2c.tx_buf_idx) >= len;
128 }
129 
130 void gps_i2c_put_buffer(struct GpsUbxI2C *p, long fd, uint8_t *data, uint16_t len)
131 {
132  int i = 0;
133  for (i = 0; i < len; i++) {
134  gps_i2c_put_byte(p, fd, data[i]);
135  }
136 }
137 
138 void gps_i2c_put_byte(struct GpsUbxI2C *p __attribute__((unused)), long fd __attribute__((unused)), uint8_t data)
139 {
140  gps_i2c.tx_buf[gps_i2c.tx_buf_idx++] = data;
141 }
142 
143 void gps_i2c_msg_ready(struct GpsUbxI2C *p __attribute__((unused)), long fd __attribute__((unused)))
144 {
146  gps_i2c.tx_rdy = FALSE;
147 }
148 
149 uint8_t gps_i2c_char_available(struct GpsUbxI2C *p __attribute__((unused)))
150 {
151  return (((int)gps_i2c.rx_buf_avail - (int)gps_i2c.rx_buf_idx) > 0);
152 }
153 
155 {
156  return gps_i2c.tx_rdy;
157 }
158 
159 void gps_i2c_begin(void)
160 {
161  gps_ubx_i2c_ucenter_done = TRUE;
162 }
163 
164 uint8_t gps_i2c_getch(struct GpsUbxI2C *p __attribute__((unused)))
165 {
166  return gps_i2c.rx_buf[gps_i2c.rx_buf_idx++];
167 }
168 
170 {
172  {
173  switch(gps_i2c.write_state)
174  {
177  {
178  if (gps_ubx_i2c_bytes_to_read > GPS_I2C_BUF_SIZE || gps_ubx_i2c_ucenter_done)
179  {
181  } else {
182  gps_i2c.tx_rdy = TRUE;
183  }
184  }
185  break;
186 
189  i2c_transceive(&GPS_UBX_I2C_DEV, &gps_i2c.trans, GPS_I2C_SLAVE_ADDR, 1, 2);
192  break;
193 
194  case gps_i2c_write_cfg:
197  gps_i2c.tx_buf_idx = 0;
200  break;
201 
202  default:
203  break;
204  }
205  }
206 }
207 
209 {
210  switch(gps_i2c.read_state)
211  {
213  break;
214  break;
215 
216  // how many bytes are available
217  case gps_i2c_read_sizeof:
218  gps_ubx_i2c_bytes_to_read = ((uint16_t)(gps_i2c.trans.buf[0]) << 7) | (uint16_t)(gps_i2c.trans.buf[1]);
220  if (gps_ubx_i2c_bytes_to_read == 0xFFFF || gps_ubx_i2c_bytes_to_read == 0x0000)
221  {
223  return;
224  } else if (gps_ubx_i2c_bytes_to_read > GPS_I2C_BUF_SIZE)
225  {
229  } else
230  {
232  i2c_transceive(&GPS_UBX_I2C_DEV, &gps_i2c.trans, GPS_I2C_SLAVE_ADDR, 1, gps_ubx_i2c_bytes_to_read);
234  return;
235  }
236  break;
237  case gps_i2c_read_data:
238  if (gps_ubx_i2c_bytes_to_read > GPS_I2C_BUF_SIZE)
239  {
241  gps_i2c.rx_buf_idx = 0;
243  } else
244  {
245  memcpy(&gps_i2c.rx_buf, (uint8_t *) & (gps_i2c.trans.buf), gps_ubx_i2c_bytes_to_read);
246  gps_i2c.rx_buf_idx = 0;
248  }
249 
252  break;
253 
254  default:
255  break;
256  }
257 
258  // Transaction has been read
260 }
unsigned short uint16_t
Definition: types.h:16
#define GPS_I2C_BUF_SIZE
Definition: gps_ubx_i2c.h:39
uint16_t tx_buf_idx
tx buf index
Definition: gps_ubx_i2c.h:71
#define GPS_I2C_ADDR_NB_AVAIL_BYTES
number of bytes available register
Definition: gps_ubx_i2c.c:45
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
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:130
request size of ubx buffer
Definition: gps_ubx_i2c.h:55
GpsI2CReadState read_state
Definition: gps_ubx_i2c.h:63
pprz link device for Ublox over I2C
uint8_t gps_i2c_getch(struct GpsUbxI2C *p)
get a new char
Definition: gps_ubx_i2c.c:164
void gps_ubx_i2c_periodic(void)
handle message sending
Definition: gps_ubx_i2c.c:169
wait for gps_ubx to read buffer or ucenter to transmit
Definition: gps_ubx_i2c.h:54
struct i2c_transaction trans
i2c transaction
Definition: gps_ubx_i2c.h:75
uint8_t tx_buf[GPS_I2C_BUF_SIZE]
transmit buffer
Definition: gps_ubx_i2c.h:67
uint16_t rx_buf_idx
rx buf index
Definition: gps_ubx_i2c.h:70
void gps_i2c_put_byte(struct GpsUbxI2C *p, long fd, uint8_t data)
Put byte into transmit buffer.
Definition: gps_ubx_i2c.c:138
void gps_ubx_i2c_read_event(void)
handle message reception
Definition: gps_ubx_i2c.c:208
uint8_t rx_buf[GPS_I2C_BUF_SIZE]
receive buffer
Definition: gps_ubx_i2c.h:66
#define FALSE
Definition: std.h:5
void null_function(struct GpsUbxI2C *p)
null function
Definition: gps_ubx_i2c.c:123
send a config msg and get reply
Definition: gps_ubx_i2c.h:56
#define TRUE
Definition: std.h:4
#define GPS_I2C_ADDR_DATA
data stream register
Definition: gps_ubx_i2c.c:46
transaction set to done by user level
Definition: i2c.h:59
#define GPS_I2C_SLAVE_ADDR
Definition: gps_ubx_i2c.c:38
bool tx_rdy
are we ready to transmit
Definition: gps_ubx_i2c.h:73
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:328
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
dont read anything
Definition: gps_ubx_i2c.h:45
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:308
struct link_device device
ppz link device
Definition: gps_ubx_i2c.h:79
read size of ubx buffer
Definition: gps_ubx_i2c.h:46
uint16_t rx_buf_avail
how many bytes are waiting to be read
Definition: gps_ubx_i2c.h:69
uint16_t gps_ubx_i2c_bytes_to_read
ublox bytes to read
Definition: gps_ubx_i2c.c:53
unsigned char uint8_t
Definition: types.h:14
int fd
Definition: serial.c:26
void gps_ubx_i2c_init(void)
init function
Definition: gps_ubx_i2c.c:96
ubx_i2c state
Definition: gps_ubx_i2c.h:61
bool gps_i2c_tx_is_ready(void)
is driver ready to send a message
Definition: gps_ubx_i2c.c:154
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:125
static float p[2][2]
GpsI2CWriteState write_state
Definition: gps_ubx_i2c.h:64
void gps_i2c_begin(void)
config is done, begin reading messages
Definition: gps_ubx_i2c.c:159
read data from ubx buffer
Definition: gps_ubx_i2c.h:47
void gps_i2c_msg_ready(struct GpsUbxI2C *p, long fd)
send buffer when ready
Definition: gps_ubx_i2c.c:143
struct GpsUbxI2C gps_i2c
Definition: gps_ubx_i2c.c:49
uint8_t gps_i2c_char_available(struct GpsUbxI2C *p)
check if a new character is available
Definition: gps_ubx_i2c.c:149
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
bool gps_ubx_i2c_ucenter_done
ucenter finished configuring flag
Definition: gps_ubx_i2c.c:52