2 * Copyright Droids Corporation (2009)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: i2c_protocol.c,v 1.8 2009-11-08 17:24:33 zer0 Exp $
25 #include <aversive/pgmspace.h>
26 #include <aversive/wait.h>
27 #include <aversive/error.h>
32 #include "../common/i2c_commands.h"
33 #include "i2c_protocol.h"
37 #define I2C_STATE_MAX 2
38 #define I2C_PERIOD_MS 50
40 #define I2C_TIMEOUT 100 /* ms */
41 #define I2C_MAX_ERRORS 40
43 static volatile uint8_t i2c_poll_num = 0;
44 static volatile uint8_t i2c_state = 0;
45 static volatile uint8_t i2c_rx_count = 0;
46 static volatile uint8_t i2c_tx_count = 0;
47 static volatile uint16_t i2c_errors = 0;
49 static uint8_t gps_ok = 0;
51 #define OP_READY 0 /* no i2c op running */
52 #define OP_POLL 1 /* a user command is running */
53 #define OP_CMD 2 /* a polling (req / ans) is running */
55 static volatile uint8_t running_op = OP_READY;
58 static uint8_t error_log = 0;
60 static struct callout i2c_timer;
62 static int8_t i2c_req_imuboard_status(void);
64 /* latest received imuboard_status */
65 struct i2c_ans_imuboard_status imuboard_status;
67 /* used for commands */
68 uint8_t command_buf[I2C_SEND_BUFFER_SIZE];
69 volatile int8_t command_dest=-1;
70 volatile uint8_t command_size=0;
72 #define I2C_ERROR(args...) do { \
73 if (error_log < I2C_MAX_LOG) { \
74 ERROR(E_USER_I2C_PROTO, args); \
76 if (error_log == I2C_MAX_LOG) { \
77 ERROR(E_USER_I2C_PROTO, \
78 "i2c logs are now warnings"); \
82 WARNING(E_USER_I2C_PROTO, args); \
85 void i2c_protocol_debug(void)
87 printf_P(PSTR("I2C protocol debug infos:\r\n"));
88 printf_P(PSTR(" i2c_send=%d\r\n"), i2c_tx_count);
89 printf_P(PSTR(" i2c_recv=%d\r\n"), i2c_rx_count);
90 printf_P(PSTR(" i2c_state=%d\r\n"), i2c_state);
91 printf_P(PSTR(" i2c_errors=%d\r\n"), i2c_errors);
92 printf_P(PSTR(" running_op=%d\r\n"), running_op);
93 printf_P(PSTR(" command_size=%d\r\n"), command_size);
94 printf_P(PSTR(" command_dest=%d\r\n"), command_dest);
95 printf_P(PSTR(" i2c_status=%x\r\n"), i2c_status());
98 static void i2cproto_next_state(uint8_t inc)
101 if (i2c_state >= I2C_STATE_MAX) {
107 void i2cproto_wait_update(void)
110 poll_num = i2c_poll_num;
112 //WAIT_COND_OR_TIMEOUT((i2c_poll_num-poll_num) > 1, 150); /* XXX todo */
115 /* called periodically : the goal of this 'thread' is to send requests
116 * and read answers on i2c slaves in the correct order. */
117 static void i2c_poll_slaves(struct callout_mgr *cm, struct callout *tim, void *arg)
127 static uint8_t a = 0;
134 /* already running */
136 if (running_op != OP_READY) {
141 /* if a command is ready to be sent, so send it */
144 err = i2c_send(command_dest, command_buf, command_size,
152 /* no command, so do the polling */
153 running_op = OP_POLL;
157 /* poll status of imuboard */
158 #define I2C_REQ_IMUBOARD 0
159 case I2C_REQ_IMUBOARD:
160 if ((err = i2c_req_imuboard_status()))
164 #define I2C_ANS_IMUBOARD 1
165 case I2C_ANS_IMUBOARD:
166 if ((err = i2c_recv(I2C_IMUBOARD_ADDR,
167 sizeof(struct i2c_ans_imuboard_status),
172 /* sync with I2C_STATE_MAX */
174 /* nothing, go to the first request */
177 running_op = OP_READY;
184 running_op = OP_READY;
187 if (i2c_errors > I2C_MAX_ERRORS) {
188 I2C_ERROR("I2C send is_cmd=%d proto_state=%d "
189 "err=%d i2c_status=%x", !!command_size, i2c_state, err, i2c_status());
196 callout_reschedule(cm, tim, I2C_PERIOD_MS);
199 /* called when the xmit is finished */
200 void i2c_sendevent(int8_t size)
203 if (running_op == OP_POLL) {
204 i2cproto_next_state(1);
212 NOTICE(E_USER_I2C_PROTO, "send error state=%d size=%d "
213 "op=%d", i2c_state, size, running_op);
214 if (i2c_errors > I2C_MAX_ERRORS) {
215 I2C_ERROR("I2C error, slave not ready");
220 if (running_op == OP_POLL) {
221 /* skip associated answer */
222 i2cproto_next_state(2);
225 running_op = OP_READY;
228 /* called rx event */
229 void i2c_recvevent(uint8_t * buf, int8_t size)
231 if (running_op == OP_POLL)
232 i2cproto_next_state(1);
234 /* recv is only trigged after a poll */
235 running_op = OP_READY;
245 case I2C_ANS_IMUBOARD_STATUS: {
246 struct i2c_ans_imuboard_status *ans =
247 (struct i2c_ans_imuboard_status *)buf;
249 if (size != sizeof (*ans))
252 /* copy status in a global struct */
253 memcpy(&imuboard_status, ans, sizeof(imuboard_status));
256 (imuboard_status.flags & I2C_IMUBOARD_STATUS_GPS_OK)) {
272 NOTICE(E_USER_I2C_PROTO, "recv error state=%d op=%d",
273 i2c_state, running_op);
274 if (i2c_errors > I2C_MAX_ERRORS) {
275 I2C_ERROR("I2C error, slave not ready");
281 void i2c_recvbyteevent(uint8_t hwstatus, uint8_t i, uint8_t c)
288 /* ******** ******** ******** ******** */
290 /* ******** ******** ******** ******** */
294 i2c_send_command(uint8_t addr, uint8_t * buf, uint8_t size)
297 uint16_t ms = get_time_ms();
299 while ((get_time_ms() - ms) < I2C_TIMEOUT) {
301 if (command_size == 0) {
302 memcpy(command_buf, buf, size);
310 /* this should not happen... except if we are called from an
311 * interrupt context, but it's forbidden */
312 I2C_ERROR("I2C command send failed");
316 static int8_t i2c_req_imuboard_status(void)
318 struct i2c_req_imuboard_status buf;
321 buf.hdr.cmd = I2C_REQ_IMUBOARD_STATUS;
322 err = i2c_send(I2C_IMUBOARD_ADDR, (uint8_t*)&buf,
323 sizeof(buf), I2C_CTRL_GENERIC);
328 int8_t i2c_led_control(uint8_t addr, uint8_t led, uint8_t state)
330 struct i2c_cmd_led_control buf;
331 buf.hdr.cmd = I2C_CMD_GENERIC_LED_CONTROL;
334 return i2c_send_command(addr, (uint8_t*)&buf, sizeof(buf));
337 void i2c_protocol_init(void)
339 callout_init(&i2c_timer, i2c_poll_slaves, NULL, I2C_PRIO);
340 callout_schedule(&xbeeboard.intr_cm, &i2c_timer, I2C_PERIOD_MS);