Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
gps_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 ENAC, Pascal Brisset, Michel Gorraz,Gautier Hattenberger
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  */
22 
23 #include "modules/gps/gps_i2c.h"
24 #include "mcu_periph/i2c.h"
25 #include "subsystems/gps.h"
26 
28 
29 
35 
36 /* u-blox5 protocole, page 4 */
37 #define GPS_I2C_ADDR_NB_AVAIL_BYTES 0xFD
38 #define GPS_I2C_ADDR_DATA 0xFF
39 
40 #define GPS_I2C_STATUS_IDLE 0
41 #define GPS_I2C_STATUS_ASKING_DATA 1
42 #define GPS_I2C_STATUS_ASKING_NB_AVAIL_BYTES 2
43 #define GPS_I2C_STATUS_READING_NB_AVAIL_BYTES 3
44 #define GPS_I2C_STATUS_READING_DATA 4
45 
46 #define gps_i2c_AddCharToRxBuf(_x) { \
47  gps_i2c_rx_buf[gps_i2c_rx_insert_idx] = _x; \
48  gps_i2c_rx_insert_idx++; /* size=256, No check for buf overflow */ \
49  }
50 
52 //static uint16_t gps_i2c_nb_avail_bytes; /* size buffer =~ 12k */
53 //static uint8_t data_buf_len;
54 
55 void gps_i2c_init(void)
56 {
63 #ifdef GPS_CONFIGURE
64  /* The call in main_ap.c is made before the modules init (too early) */
65  gps_configure_uart();
66 #endif
67 }
68 
69 void gps_i2c_periodic(void)
70 {
71  /*
72  if (gps_i2c_done && gps_i2c_status == GPS_I2C_STATUS_IDLE) {
73  i2c0_buf[0] = GPS_I2C_ADDR_NB_AVAIL_BYTES;
74  i2c0_transmit_no_stop(GPS_I2C_SLAVE_ADDR, 1, &gps_i2c_done);
75  gps_i2c_done = FALSE;
76  gps_i2c_status = GPS_I2C_STATUS_ASKING_NB_AVAIL_BYTES;
77  }
78  */
79 
80 }
81 
82 void gps_i2c_event(void)
83 {
84  /*
85  * switch (gps_i2c_status) {
86  case GPS_I2C_STATUS_IDLE:
87  if (gps_i2c_data_ready_to_transmit) {
88  // Copy data from our buffer to the i2c buffer
89  uint8_t data_size = Min(gps_i2c_tx_insert_idx-gps_i2c_tx_extract_idx, I2C0_BUF_LEN);
90  uint8_t i;
91  for(i = 0; i < data_size; i++, gps_i2c_tx_extract_idx++)
92  i2c0_buf[i] = gps_i2c_tx_buf[gps_i2c_tx_extract_idx];
93 
94  // Start i2c transmit
95  i2c0_transmit(GPS_I2C_SLAVE_ADDR, data_size, &gps_i2c_done);
96  gps_i2c_done = FALSE;
97 
98  // Reset flag if finished
99  if (gps_i2c_tx_extract_idx >= gps_i2c_tx_insert_idx) {
100  gps_i2c_data_ready_to_transmit = FALSE;
101  gps_i2c_tx_insert_idx = 0;
102  }
103  }
104  break;
105 
106  case GPS_I2C_STATUS_ASKING_NB_AVAIL_BYTES:
107  i2c0_receive(GPS_I2C_SLAVE_ADDR, 2, &gps_i2c_done);
108  gps_i2c_done = FALSE;
109  gps_i2c_status = GPS_I2C_STATUS_READING_NB_AVAIL_BYTES;
110  break;
111 
112  case GPS_I2C_STATUS_READING_NB_AVAIL_BYTES:
113  gps_i2c_nb_avail_bytes = (i2c0_buf[0]<<8) | i2c0_buf[1];
114 
115  if (gps_i2c_nb_avail_bytes)
116  goto continue_reading;
117  else
118  gps_i2c_status = GPS_I2C_STATUS_IDLE;
119  break;
120 
121  continue_reading:
122 
123  case GPS_I2C_STATUS_ASKING_DATA:
124  data_buf_len = Min(gps_i2c_nb_avail_bytes, I2C0_BUF_LEN);
125  gps_i2c_nb_avail_bytes -= data_buf_len;
126 
127  i2c0_receive(GPS_I2C_SLAVE_ADDR, data_buf_len, &gps_i2c_done);
128  gps_i2c_done = FALSE;
129  gps_i2c_status = GPS_I2C_STATUS_READING_DATA;
130  break;
131 
132  case GPS_I2C_STATUS_READING_DATA: {
133  uint8_t i;
134  for(i = 0; i < data_buf_len; i++) {
135  gps_i2c_AddCharToRxBuf(i2c0_buf[i]);
136  }
137 
138  if (gps_i2c_nb_avail_bytes)
139  goto continue_reading;
140  else
141  gps_i2c_status = GPS_I2C_STATUS_IDLE;
142  break;
143  }
144 
145  default:
146  return;
147  }
148  */
149 
150 }
static uint8_t gps_i2c_status
Definition: gps_i2c.c:51
void gps_i2c_periodic(void)
Definition: gps_i2c.c:69
void gps_i2c_init(void)
Definition: gps_i2c.c:55
#define FALSE
Definition: std.h:5
#define TRUE
Definition: std.h:4
void gps_i2c_event(void)
Definition: gps_i2c.c:82
uint8_t gps_i2c_rx_insert_idx
Definition: gps_i2c.c:31
#define GPS_I2C_BUF_SIZE
Definition: gps_i2c.h:31
Device independent GPS code (interface)
uint8_t gps_i2c_rx_buf[GPS_I2C_BUF_SIZE]
Definition: gps_i2c.c:30
bool_t gps_i2c_data_ready_to_transmit
Definition: gps_i2c.c:34
uint8_t gps_i2c_tx_insert_idx
Definition: gps_i2c.c:33
I2C transaction structure.
Definition: i2c.h:93
uint8_t gps_i2c_rx_extract_idx
Definition: gps_i2c.c:31
#define GPS_I2C_STATUS_IDLE
Definition: gps_i2c.c:40
unsigned char uint8_t
Definition: types.h:14
struct i2c_transaction i2c_gps_trans
Definition: gps_i2c.c:27
bool_t gps_i2c_done
Definition: gps_i2c.c:34
uint8_t gps_i2c_tx_extract_idx
Definition: gps_i2c.c:33
uint8_t gps_i2c_tx_buf[GPS_I2C_BUF_SIZE]
Definition: gps_i2c.c:32
Architecture independent I2C (Inter-Integrated Circuit Bus) API.