Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros 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 "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) {
62 #ifdef GPS_CONFIGURE
63  /* The call in main_ap.c is made before the modules init (too early) */
64  gps_configure_uart();
65 #endif
66 }
67 
68 void gps_i2c_periodic(void) {
69 /*
70  if (gps_i2c_done && gps_i2c_status == GPS_I2C_STATUS_IDLE) {
71  i2c0_buf[0] = GPS_I2C_ADDR_NB_AVAIL_BYTES;
72  i2c0_transmit_no_stop(GPS_I2C_SLAVE_ADDR, 1, &gps_i2c_done);
73  gps_i2c_done = FALSE;
74  gps_i2c_status = GPS_I2C_STATUS_ASKING_NB_AVAIL_BYTES;
75  }
76 */
77 
78 }
79 
80 void gps_i2c_event(void) {
81 /*
82  * switch (gps_i2c_status) {
83  case GPS_I2C_STATUS_IDLE:
84  if (gps_i2c_data_ready_to_transmit) {
85  // Copy data from our buffer to the i2c buffer
86  uint8_t data_size = Min(gps_i2c_tx_insert_idx-gps_i2c_tx_extract_idx, I2C0_BUF_LEN);
87  uint8_t i;
88  for(i = 0; i < data_size; i++, gps_i2c_tx_extract_idx++)
89  i2c0_buf[i] = gps_i2c_tx_buf[gps_i2c_tx_extract_idx];
90 
91  // Start i2c transmit
92  i2c0_transmit(GPS_I2C_SLAVE_ADDR, data_size, &gps_i2c_done);
93  gps_i2c_done = FALSE;
94 
95  // Reset flag if finished
96  if (gps_i2c_tx_extract_idx >= gps_i2c_tx_insert_idx) {
97  gps_i2c_data_ready_to_transmit = FALSE;
98  gps_i2c_tx_insert_idx = 0;
99  }
100  }
101  break;
102 
103  case GPS_I2C_STATUS_ASKING_NB_AVAIL_BYTES:
104  i2c0_receive(GPS_I2C_SLAVE_ADDR, 2, &gps_i2c_done);
105  gps_i2c_done = FALSE;
106  gps_i2c_status = GPS_I2C_STATUS_READING_NB_AVAIL_BYTES;
107  break;
108 
109  case GPS_I2C_STATUS_READING_NB_AVAIL_BYTES:
110  gps_i2c_nb_avail_bytes = (i2c0_buf[0]<<8) | i2c0_buf[1];
111 
112  if (gps_i2c_nb_avail_bytes)
113  goto continue_reading;
114  else
115  gps_i2c_status = GPS_I2C_STATUS_IDLE;
116  break;
117 
118  continue_reading:
119 
120  case GPS_I2C_STATUS_ASKING_DATA:
121  data_buf_len = Min(gps_i2c_nb_avail_bytes, I2C0_BUF_LEN);
122  gps_i2c_nb_avail_bytes -= data_buf_len;
123 
124  i2c0_receive(GPS_I2C_SLAVE_ADDR, data_buf_len, &gps_i2c_done);
125  gps_i2c_done = FALSE;
126  gps_i2c_status = GPS_I2C_STATUS_READING_DATA;
127  break;
128 
129  case GPS_I2C_STATUS_READING_DATA: {
130  uint8_t i;
131  for(i = 0; i < data_buf_len; i++) {
132  gps_i2c_AddCharToRxBuf(i2c0_buf[i]);
133  }
134 
135  if (gps_i2c_nb_avail_bytes)
136  goto continue_reading;
137  else
138  gps_i2c_status = GPS_I2C_STATUS_IDLE;
139  break;
140  }
141 
142  default:
143  return;
144  }
145 */
146 
147 }
static uint8_t gps_i2c_status
Definition: gps_i2c.c:51
void gps_i2c_periodic(void)
Definition: gps_i2c.c:68
void gps_i2c_init(void)
Definition: gps_i2c.c:55
#define FALSE
Definition: imu_chimu.h:141
void gps_i2c_event(void)
Definition: gps_i2c.c:80
uint8_t gps_i2c_rx_insert_idx
Definition: gps_i2c.c:31
#define GPS_I2C_BUF_SIZE
Definition: gps_i2c.h:30
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
#define TRUE
Definition: imu_chimu.h:144
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