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>
33 #include <clock_time.h>
37 #include <control_system_manager.h>
38 #include <trajectory_manager.h>
39 #include <vect_base.h>
42 #include <obstacle_avoidance.h>
43 #include <blocking_detection_manager.h>
44 #include <robot_system.h>
45 #include <position_manager.h>
49 #include <parse_string.h>
50 #include <parse_num.h>
52 #include "../common/i2c_commands.h"
55 #include "i2c_protocol.h"
57 #define I2C_STATE_MAX 4
59 #define I2C_TIMEOUT 100 /* ms */
60 #define I2C_MAX_ERRORS 40
62 static volatile uint8_t i2c_poll_num = 0;
63 static volatile uint8_t i2c_state = 0;
64 static volatile uint16_t i2c_errors = 0;
66 #define OP_READY 0 /* no i2c op running */
67 #define OP_POLL 1 /* a user command is running */
68 #define OP_CMD 2 /* a polling (req / ans) is running */
70 static volatile uint8_t running_op = OP_READY;
73 static uint8_t error_log = 0;
75 /* used for commands */
76 uint8_t command_buf[I2C_SEND_BUFFER_SIZE];
77 volatile int8_t command_dest=-1;
78 volatile uint8_t command_size=0;
80 static int8_t i2c_req_cobboard_status(void);
81 static int8_t i2c_req_ballboard_status(void);
83 #define I2C_ERROR(args...) do { \
84 if (error_log < I2C_MAX_LOG) { \
85 ERROR(E_USER_I2C_PROTO, args); \
87 if (error_log == I2C_MAX_LOG) { \
88 ERROR(E_USER_I2C_PROTO, \
89 "i2c logs are now warnings"); \
93 WARNING(E_USER_I2C_PROTO, args); \
96 void i2c_protocol_init(void)
100 void i2c_protocol_debug(void)
102 printf_P(PSTR("I2C protocol debug infos:\r\n"));
103 printf_P(PSTR(" i2c_state=%d\r\n"), i2c_state);
104 printf_P(PSTR(" i2c_errors=%d\r\n"), i2c_errors);
105 printf_P(PSTR(" running_op=%d\r\n"), running_op);
106 printf_P(PSTR(" command_size=%d\r\n"), command_size);
107 printf_P(PSTR(" command_dest=%d\r\n"), command_dest);
108 printf_P(PSTR(" i2c_status=%x\r\n"), i2c_status());
111 static void i2cproto_next_state(uint8_t inc)
114 if (i2c_state >= I2C_STATE_MAX) {
120 void i2cproto_wait_update(void)
123 poll_num = i2c_poll_num;
124 WAIT_COND_OR_TIMEOUT((i2c_poll_num-poll_num) > 1, 150);
127 /* called periodically : the goal of this 'thread' is to send requests
128 * and read answers on i2c slaves in the correct order. */
129 void i2c_poll_slaves(void *dummy)
133 static uint8_t a = 0;
139 /* already running */
141 if (running_op != OP_READY) {
146 /* if a command is ready to be sent, so send it */
149 err = i2c_send(command_dest, command_buf, command_size,
157 /* no command, so do the polling */
158 running_op = OP_POLL;
162 /* poll status of cobboard */
163 #define I2C_REQ_COBBOARD 0
164 case I2C_REQ_COBBOARD:
165 if ((err = i2c_req_cobboard_status()))
169 #define I2C_ANS_COBBOARD 1
170 case I2C_ANS_COBBOARD:
171 if ((err = i2c_recv(I2C_COBBOARD_ADDR,
172 sizeof(struct i2c_ans_cobboard_status),
177 /* poll status of ballboard */
178 #define I2C_REQ_BALLBOARD 2
179 case I2C_REQ_BALLBOARD:
180 if ((err = i2c_req_ballboard_status()))
184 #define I2C_ANS_BALLBOARD 3
185 case I2C_ANS_BALLBOARD:
186 if ((err = i2c_recv(I2C_BALLBOARD_ADDR,
187 sizeof(struct i2c_ans_ballboard_status),
192 /* nothing, go to the first request */
195 running_op = OP_READY;
202 running_op = OP_READY;
205 if (i2c_errors > I2C_MAX_ERRORS) {
206 I2C_ERROR("I2C send is_cmd=%d proto_state=%d "
207 "err=%d i2c_status=%x", !!command_size, i2c_state, err, i2c_status());
213 /* called when the xmit is finished */
214 void i2c_sendevent(int8_t size)
217 if (running_op == OP_POLL) {
218 i2cproto_next_state(1);
225 NOTICE(E_USER_I2C_PROTO, "send error state=%d size=%d "
226 "op=%d", i2c_state, size, running_op);
227 if (i2c_errors > I2C_MAX_ERRORS) {
228 I2C_ERROR("I2C error, slave not ready");
233 if (running_op == OP_POLL) {
234 /* skip associated answer */
235 i2cproto_next_state(2);
238 running_op = OP_READY;
241 /* called rx event */
242 void i2c_recvevent(uint8_t * buf, int8_t size)
244 if (running_op == OP_POLL)
245 i2cproto_next_state(1);
247 /* recv is only trigged after a poll */
248 running_op = OP_READY;
256 case I2C_ANS_COBBOARD_STATUS: {
257 struct i2c_ans_cobboard_status * ans =
258 (struct i2c_ans_cobboard_status *)buf;
260 if (size != sizeof (*ans))
264 cobboard.mode = ans->mode;
265 cobboard.status = ans->status;
266 cobboard.left_cobroller_speed = ans->left_cobroller_speed;
267 cs_set_consign(&mainboard.left_cobroller.cs, cobboard.left_cobroller_speed);
268 cobboard.right_cobroller_speed = ans->right_cobroller_speed;
269 cs_set_consign(&mainboard.right_cobroller.cs, cobboard.right_cobroller_speed);
274 case I2C_ANS_BALLBOARD_STATUS: {
275 struct i2c_ans_ballboard_status * ans =
276 (struct i2c_ans_ballboard_status *)buf;
278 if (size != sizeof (*ans))
280 ballboard.mode = ans->mode;
281 ballboard.status = ans->status;
282 ballboard.ball_count = ans->ball_count;
293 NOTICE(E_USER_I2C_PROTO, "recv error state=%d op=%d",
294 i2c_state, running_op);
295 if (i2c_errors > I2C_MAX_ERRORS) {
296 I2C_ERROR("I2C error, slave not ready");
302 void i2c_recvbyteevent(uint8_t hwstatus, uint8_t i, uint8_t c)
306 /* ******** ******** ******** ******** */
308 /* ******** ******** ******** ******** */
312 i2c_send_command(uint8_t addr, uint8_t * buf, uint8_t size)
315 microseconds us = time_get_us2();
317 while ((time_get_us2() - us) < (I2C_TIMEOUT)*1000L) {
319 if (command_size == 0) {
320 memcpy(command_buf, buf, size);
328 /* this should not happen... except if we are called from an
329 * interrupt context, but it's forbidden */
330 I2C_ERROR("I2C command send failed");
334 static int8_t i2c_req_cobboard_status(void)
336 struct i2c_req_cobboard_status buf;
339 buf.hdr.cmd = I2C_REQ_COBBOARD_STATUS;
340 err = i2c_send(I2C_COBBOARD_ADDR, (uint8_t*)&buf,
341 sizeof(buf), I2C_CTRL_GENERIC);
346 static int8_t i2c_req_ballboard_status(void)
348 struct i2c_req_ballboard_status buf;
350 buf.hdr.cmd = I2C_REQ_BALLBOARD_STATUS;
351 return i2c_send(I2C_BALLBOARD_ADDR, (uint8_t*)&buf,
352 sizeof(buf), I2C_CTRL_GENERIC);
355 int8_t i2c_set_color(uint8_t addr, uint8_t color)
357 struct i2c_cmd_generic_color buf;
359 if (addr == I2C_BALLBOARD_ADDR)
360 return 0; /* XXX disabled for now */
361 buf.hdr.cmd = I2C_CMD_GENERIC_SET_COLOR;
363 return i2c_send_command(addr, (uint8_t*)&buf, sizeof(buf));
366 int8_t i2c_led_control(uint8_t addr, uint8_t led, uint8_t state)
368 struct i2c_cmd_led_control buf;
369 buf.hdr.cmd = I2C_CMD_GENERIC_LED_CONTROL;
372 return i2c_send_command(addr, (uint8_t*)&buf, sizeof(buf));
375 int8_t i2c_cobboard_mode_eject(void)
377 struct i2c_cmd_cobboard_set_mode buf;
378 buf.hdr.cmd = I2C_CMD_COBBOARD_SET_MODE;
379 buf.mode = cobboard.mode | I2C_COBBOARD_MODE_EJECT;
380 return i2c_send_command(I2C_COBBOARD_ADDR, (uint8_t*)&buf, sizeof(buf));
383 int8_t i2c_cobboard_mode_harvest(uint8_t side)
385 struct i2c_cmd_cobboard_set_mode buf;
386 uint8_t mode = cobboard.mode;
388 buf.hdr.cmd = I2C_CMD_COBBOARD_SET_MODE;
389 if (side == I2C_LEFT_SIDE) {
390 mode |= I2C_COBBOARD_MODE_L_DEPLOY;
391 mode |= I2C_COBBOARD_MODE_L_HARVEST;
394 mode |= I2C_COBBOARD_MODE_R_DEPLOY;
395 mode |= I2C_COBBOARD_MODE_R_HARVEST;
398 return i2c_send_command(I2C_COBBOARD_ADDR, (uint8_t*)&buf, sizeof(buf));
401 int8_t i2c_cobboard_mode_deploy(uint8_t side)
403 struct i2c_cmd_cobboard_set_mode buf;
404 uint8_t mode = cobboard.mode;
406 buf.hdr.cmd = I2C_CMD_COBBOARD_SET_MODE;
407 if (side == I2C_LEFT_SIDE) {
408 mode &= ~(I2C_COBBOARD_MODE_L_DEPLOY | I2C_COBBOARD_MODE_L_HARVEST);
409 mode |= I2C_COBBOARD_MODE_L_DEPLOY;
412 mode &= ~(I2C_COBBOARD_MODE_R_DEPLOY | I2C_COBBOARD_MODE_R_HARVEST);
413 mode |= I2C_COBBOARD_MODE_R_DEPLOY;
416 return i2c_send_command(I2C_COBBOARD_ADDR, (uint8_t*)&buf, sizeof(buf));
419 int8_t i2c_cobboard_mode_pack(uint8_t side)
421 struct i2c_cmd_cobboard_set_mode buf;
422 uint8_t mode = cobboard.mode;
424 buf.hdr.cmd = I2C_CMD_COBBOARD_SET_MODE;
425 if (side == I2C_LEFT_SIDE)
426 mode &= ~(I2C_COBBOARD_MODE_L_DEPLOY | I2C_COBBOARD_MODE_L_HARVEST);
428 mode &= ~(I2C_COBBOARD_MODE_R_DEPLOY | I2C_COBBOARD_MODE_R_HARVEST);
430 return i2c_send_command(I2C_COBBOARD_ADDR, (uint8_t*)&buf, sizeof(buf));
433 int8_t i2c_cobboard_mode_init(void)
435 struct i2c_cmd_cobboard_set_mode buf;
436 buf.hdr.cmd = I2C_CMD_COBBOARD_SET_MODE;
437 buf.mode = I2C_COBBOARD_MODE_INIT;
438 return i2c_send_command(I2C_COBBOARD_ADDR, (uint8_t*)&buf, sizeof(buf));
441 int8_t i2c_ballboard_set_mode(uint8_t mode)
443 struct i2c_cmd_ballboard_set_mode buf;
444 buf.hdr.cmd = I2C_CMD_BALLBOARD_SET_MODE;
446 return i2c_send_command(I2C_COBBOARD_ADDR, (uint8_t*)&buf, sizeof(buf));