add i2c support
authorOlivier Matz <zer0@droids-corp.org>
Thu, 17 Jul 2014 17:55:13 +0000 (19:55 +0200)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 17 Jul 2014 17:55:13 +0000 (19:55 +0200)
Makefile
i2c_config.h
i2c_helper.c
i2c_protocol.c [new file with mode: 0644]
i2c_protocol.h [new file with mode: 0644]
i2cm_sw.c
i2cm_sw.h
imu.c
main.c

index 50e2137..91a2763 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,7 @@ SRC += partition.c
 SRC += sd_raw.c
 SRC += gps_venus.c
 SRC += sd_log.c
+SRC += i2c_protocol.c
 
 
 CFLAGS += -W -Wall -Werror
index 2634073..93ce0ec 100644 (file)
@@ -1,6 +1,6 @@
-/*
+/*  
  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
- *
+ * 
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- *  Revision : $Id: i2c_config.h,v 1.2 2009-03-05 23:01:32 zer0 Exp $
+ *  Revision : $Id: i2c_config.h,v 1.1 2009-03-05 22:52:35 zer0 Exp $
  *
  */
 
 
-#define I2C_BITRATE 1 // divider dor i2c baudrate, see TWBR in doc
+#define I2C_BITRATE 1 // divider dor i2c baudrate, see TWBR in doc 
 #define I2C_PRESCALER 3 // prescaler config, rate = 2^(n*2)
 
 /* Size of transmission buffer */
index b6e304c..255aaec 100644 (file)
@@ -5,25 +5,23 @@
 
 #include <aversive/wait.h>
 #include <uart.h>
-#include <i2c.h>
-
-
 
+#include "i2cm_sw.h"
 
 uint8_t read_reg(uint8_t address_dev, uint8_t address_reg, uint8_t * value)
 {
        uint8_t err = 0;
-       err = i2c_send(address_dev, &address_reg, 1, I2C_CTRL_SYNC);
+       err = i2cm_send(address_dev, &address_reg, 1);
        if (err) {
                printf("read reg: i2c error send\r\n");
                return err;
        }
-       err = i2c_recv(address_dev, 1, I2C_CTRL_SYNC);
+       err = i2cm_recv(address_dev, 1);
        if (err) {
                printf("read reg: i2c error recv\r\n");
                return err;
        }
-       err = i2c_get_recv_buffer(value, 1);
+       err = i2cm_get_recv_buffer(value, 1);
        if (err != 1) {
                printf("read reg: i2c error get recv\r\n");
                return 0xff;
@@ -33,20 +31,21 @@ uint8_t read_reg(uint8_t address_dev, uint8_t address_reg, uint8_t * value)
 }
 
 
-uint8_t read_reg_len(uint8_t address_dev, uint8_t address_reg, uint8_t * values, uint8_t len)
+uint8_t read_reg_len(uint8_t address_dev, uint8_t address_reg,
+       uint8_t *values, uint8_t len)
 {
        uint8_t err = 0;
-       err = i2c_send(address_dev, &address_reg, 1, I2C_CTRL_SYNC);
+       err = i2cm_send(address_dev, &address_reg, 1);
        if (err) {
                printf("read reg len: i2c error send\r\n");
                return err;
        }
-       err = i2c_recv(address_dev, len, I2C_CTRL_SYNC);
+       err = i2cm_recv(address_dev, len);
        if (err) {
                printf("read reg len: i2c error recv\r\n");
                return err;
        }
-       err = i2c_get_recv_buffer(values, len);
+       err = i2cm_get_recv_buffer(values, len);
        if (err != len) {
                printf("read reg len: i2c error get recv\r\n");
                return 0xFF;
diff --git a/i2c_protocol.c b/i2c_protocol.c
new file mode 100644 (file)
index 0000000..f62bfb2
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2014, Olivier MATZ <zer0@droids-corp.org>
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the University of California, Berkeley nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <string.h>
+
+#include <aversive.h>
+#include <aversive/error.h>
+
+#include <i2c.h>
+
+#include "../fpv-common/i2c_commands.h"
+#include "main.h"
+
+void i2c_protocol_init(void)
+{
+}
+
+/*** LED CONTROL ***/
+void i2c_led_control(uint8_t l, uint8_t state)
+{
+       switch(l) {
+       case 1:
+               state? LED1_ON():LED1_OFF();
+               break;
+       case 2:
+               state? LED2_ON():LED2_OFF();
+               break;
+       default:
+               break;
+       }
+}
+
+#ifdef notyet
+static void i2c_test(uint16_t val)
+{
+       static uint16_t prev=0;
+
+       if ( (val-prev) != 1 ) {
+               WARNING(E_USER_I2C_PROTO, "Dupplicated msg %d", val);
+       }
+       prev = val;
+       ext.nb_test_cmd ++;
+}
+#endif
+
+static void i2c_send_status(void)
+{
+       struct i2c_ans_imuboard_status ans;
+       static uint8_t x;
+
+       i2c_flush();
+       ans.hdr.cmd =  I2C_ANS_IMUBOARD_STATUS;
+
+       /* status */
+       ans.test = x++; /* XXX */
+#if 0
+       ans.mode = state_get_mode();
+       ans.status = state_get_status();
+
+       ans.left_cobroller_speed = imuboard.left_cobroller_speed;
+       ans.right_cobroller_speed = imuboard.right_cobroller_speed;
+
+       ans.cob_count = state_get_cob_count();
+#endif
+
+       i2c_send(I2C_ADD_MASTER, (uint8_t *) &ans,
+                sizeof(ans), I2C_CTRL_GENERIC);
+}
+
+void i2c_recvevent(uint8_t * buf, int8_t size)
+{
+       void *void_cmd = buf;
+
+#if 0 /* XXX */
+       static uint8_t a = 0;
+
+       a++;
+       if (a & 0x10)
+               LED2_TOGGLE();
+
+       if (size <= 0) {
+               goto error;
+       }
+#endif
+
+       switch (buf[0]) {
+
+       /* Commands (no answer needed) */
+       case I2C_CMD_GENERIC_LED_CONTROL:
+               {
+                       struct i2c_cmd_led_control *cmd = void_cmd;
+                       if (size != sizeof (*cmd))
+                               goto error;
+                       i2c_led_control(cmd->led_num, cmd->state);
+                       break;
+               }
+
+
+       /* Add other commands here ...*/
+
+       case I2C_REQ_IMUBOARD_STATUS:
+               {
+                       //struct i2c_req_imuboard_status *cmd = void_cmd;
+                       if (size != sizeof (struct i2c_req_imuboard_status))
+                               goto error;
+
+                       i2c_send_status();
+                       break;
+               }
+
+       default:
+               goto error;
+       }
+
+ error:
+       /* log error on a led ? */
+       return;
+}
+
+void i2c_recvbyteevent(__attribute__((unused)) uint8_t hwstatus,
+                      __attribute__((unused)) uint8_t i, 
+                      __attribute__((unused)) int8_t c)
+{
+}
+
+void i2c_sendevent(__attribute__((unused)) int8_t size)
+{
+}
+
+
diff --git a/i2c_protocol.h b/i2c_protocol.h
new file mode 100644 (file)
index 0000000..7f022ef
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ *  Copyright Droids Corporation (2007)
+ * 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  Revision : $Id: i2c_protocol.h,v 1.1 2009-03-05 22:52:35 zer0 Exp $
+ *
+ */
+
+#include <aversive.h>
+
+void i2c_protocol_init(void);
+
+void i2c_recvevent(uint8_t * buf, int8_t size);
+void i2c_recvbyteevent(uint8_t hwstatus, uint8_t i, uint8_t c);
+void i2c_sendevent(int8_t size);
+
+int debug_send(char c, FILE* f);
index e6a6748..a7da2b5 100644 (file)
--- a/i2cm_sw.c
+++ b/i2cm_sw.c
@@ -279,14 +279,11 @@ uint8_t i2cm_receive_byte(uint8_t last)
 }
 
 
-
-uint8_t i2c_send(uint8_t addr, uint8_t* data, uint8_t len, uint8_t ctrl)
+uint8_t i2cm_send(uint8_t addr, uint8_t* data, uint8_t len)
 {
        uint8_t i;
        uint8_t err = 0;
 
-       (void)ctrl; /* ignored */
-
        err = i2cm_send_start((addr<<1) | 0);
        if (err)
                return err;
@@ -302,15 +299,13 @@ uint8_t i2c_send(uint8_t addr, uint8_t* data, uint8_t len, uint8_t ctrl)
 }
 
 
-uint8_t i2c_buf[0x20];
+uint8_t i2c_buf[0x20]; /* XXX */
 
-uint8_t i2c_recv(uint8_t addr, uint8_t len, uint8_t ctrl)
+uint8_t i2cm_recv(uint8_t addr, uint8_t len)
 {
        uint8_t i;
        uint8_t err = 0;
 
-       (void)ctrl; /* ignored */
-
        err = i2cm_send_start((addr<<1) | 1);
        if (err)
                return err;
@@ -328,12 +323,10 @@ uint8_t i2c_recv(uint8_t addr, uint8_t len, uint8_t ctrl)
 }
 
 
-uint8_t i2c_get_recv_buffer(uint8_t* buf, uint8_t len)
+uint8_t i2cm_get_recv_buffer(uint8_t* buf, uint8_t len)
 {
        uint8_t i;
        for (i=0; i<len; i++)
                buf[i] = i2c_buf[i];
        return len;
 }
-
-
index 4a51e57..c206591 100644 (file)
--- a/i2cm_sw.h
+++ b/i2cm_sw.h
@@ -42,7 +42,9 @@ uint8_t i2cm_send_start(uint8_t sla_w);
 uint8_t i2cm_send_stop(void);
 uint8_t i2cm_receive_byte(uint8_t last);
 
-#define I2C_CTRL_SYNC 0
+uint8_t i2cm_send(uint8_t addr, uint8_t* data, uint8_t len);
+uint8_t i2cm_recv(uint8_t addr, uint8_t len);
+uint8_t i2cm_get_recv_buffer(uint8_t* buf, uint8_t len);
 
 
 #define I2C_ERR_SEND_START 1
diff --git a/imu.c b/imu.c
index 86b34b4..d5a29cb 100644 (file)
--- a/imu.c
+++ b/imu.c
@@ -56,17 +56,6 @@ int mag_y;
 int mag_z;
 
 
-void i2c_recvevent(uint8_t *buf, int8_t size)
-{
-       (void)buf;
-       (void)size;
-}
-
-void i2c_sendevent(int8_t size)
-{
-       (void)size;
-}
-
 #if 0
 static void main_timer_interrupt(void)
 {
diff --git a/main.c b/main.c
index 9b801f5..8502044 100644 (file)
--- a/main.c
+++ b/main.c
 #include <rdline.h>
 #include <timer.h>
 #include <i2cm_sw.h>
+#include <i2c.h>
 
 #include "eeprom_config.h"
 #include "gps_venus.h"
 #include "sd_log.h"
+#include "../fpv-common/i2c_commands.h"
+#include "i2c_protocol.h"
 #include "main.h"
 
 struct imuboard imuboard;
@@ -164,6 +167,12 @@ int main(void)
        /* communication with mpu6050 */
        i2cm_init();
 
+       /* i2c hw with mainboard */
+       i2c_init(I2C_MODE_SLAVE, I2C_IMUBOARD_ADDR);
+       i2c_protocol_init();
+       i2c_register_recv_event(i2c_recvevent);
+       i2c_register_send_event(i2c_sendevent);
+
        sei();
 
        eeprom_load_config();