From 6a64132d708b36c5fcbcc0af3e4e58d61b017151 Mon Sep 17 00:00:00 2001 From: Christophe De Wagter Date: Tue, 22 Nov 2011 09:55:39 +0100 Subject: [PATCH] i2c test code --- conf/airframes/CDW/debug_i2c.xml | 279 ++++++++++++++++++ conf/autopilot/rotorcraft.makefile | 3 +- .../subsystems/fixedwing/autopilot.makefile | 5 +- .../subsystems/shared/i2c_select.makefile | 12 + conf/modules/i2c_abuse_test.xml | 15 + .../modules/benchmark/i2c_abuse_test.c | 245 +++++++++++++++ .../modules/benchmark/i2c_abuse_test.h | 52 ++++ 7 files changed, 606 insertions(+), 5 deletions(-) create mode 100644 conf/airframes/CDW/debug_i2c.xml create mode 100644 conf/autopilot/subsystems/shared/i2c_select.makefile create mode 100644 conf/modules/i2c_abuse_test.xml create mode 100644 sw/airborne/modules/benchmark/i2c_abuse_test.c create mode 100644 sw/airborne/modules/benchmark/i2c_abuse_test.h diff --git a/conf/airframes/CDW/debug_i2c.xml b/conf/airframes/CDW/debug_i2c.xml new file mode 100644 index 0000000000..7dd9a616eb --- /dev/null +++ b/conf/airframes/CDW/debug_i2c.xml @@ -0,0 +1,279 @@ + + + + + + + + + + + + + + + + + + + +
+ + +
+ + +
+ + + + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + +
+ +
+ + + +
+ + +
+ + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+ + +
+ + +
+ +
+ + + + + + + + + + + + + + + + +
+ + +
+ + + + +
+ +
+ +
+ +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/conf/autopilot/rotorcraft.makefile b/conf/autopilot/rotorcraft.makefile index 27a2e084cf..11af20574b 100644 --- a/conf/autopilot/rotorcraft.makefile +++ b/conf/autopilot/rotorcraft.makefile @@ -101,8 +101,7 @@ ap.srcs += mcu_periph/uart.c ap.srcs += $(SRC_ARCH)/mcu_periph/uart_arch.c # I2C is needed for speed controllers and barometers on lisa -ap.srcs += mcu_periph/i2c.c -ap.srcs += $(SRC_ARCH)/mcu_periph/i2c_arch.c +include $(CFG_SHARED)/i2c_select.makefile ap.srcs += $(SRC_FIRMWARE)/commands.c diff --git a/conf/autopilot/subsystems/fixedwing/autopilot.makefile b/conf/autopilot/subsystems/fixedwing/autopilot.makefile index 51d94b56a0..89e5020932 100644 --- a/conf/autopilot/subsystems/fixedwing/autopilot.makefile +++ b/conf/autopilot/subsystems/fixedwing/autopilot.makefile @@ -87,9 +87,8 @@ $(TARGET).srcs += math/pprz_geodetic_int.c math/pprz_geodetic_float.c math/pprz_ # # I2C # -$(TARGET).srcs += mcu_periph/i2c.c -$(TARGET).srcs += $(SRC_ARCH)/mcu_periph/i2c_arch.c - +include $(CFG_SHARED)/i2c_select.makefile + ###################################################################### ## ## COMMON FOR ALL NON-SIMULATION TARGETS diff --git a/conf/autopilot/subsystems/shared/i2c_select.makefile b/conf/autopilot/subsystems/shared/i2c_select.makefile new file mode 100644 index 0000000000..75241931af --- /dev/null +++ b/conf/autopilot/subsystems/shared/i2c_select.makefile @@ -0,0 +1,12 @@ +#generic i2c driver + +# TODO: this file was created to be able to select different driver files. Once 1 driver is selected as the best others can be removed including this file + +$(TARGET).srcs += mcu_periph/i2c.c +ifeq ($(ARCH), stm32) +$(TARGET).srcs += $(SRC_ARCH)/mcu_periph/i2c_arch.c +#$(TARGET).srcs += $(SRC_ARCH)/mcu_periph/i2c_arch.rewritten.c +else +$(TARGET).srcs += $(SRC_ARCH)/mcu_periph/i2c_arch.c +endif + diff --git a/conf/modules/i2c_abuse_test.xml b/conf/modules/i2c_abuse_test.xml new file mode 100644 index 0000000000..c8c7039eb4 --- /dev/null +++ b/conf/modules/i2c_abuse_test.xml @@ -0,0 +1,15 @@ + + + +
+ +
+ + + + + + + +
+ diff --git a/sw/airborne/modules/benchmark/i2c_abuse_test.c b/sw/airborne/modules/benchmark/i2c_abuse_test.c new file mode 100644 index 0000000000..11b947b375 --- /dev/null +++ b/sw/airborne/modules/benchmark/i2c_abuse_test.c @@ -0,0 +1,245 @@ +/* + * $Id$ + * + * Copyright (C) 2009 Gautier Hattenberger + * + * This file is part of paparazzi. + * + * paparazzi 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, or (at your option) + * any later version. + * + * paparazzi 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 paparazzi; see the file COPYING. If not, write to + * the Free Software Foundation, 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +#include "i2c_abuse_test.h" +#include "led.h" +#include "mcu_periph/i2c.h" + +struct i2c_transaction i2c_test1; +struct i2c_transaction i2c_test2; + +uint8_t i2c_abuse_test_counter = 0; +uint16_t i2c_abuse_test_bitrate = 1000; + +void init_i2c_abuse_test(void) { + //LED_INIT(DEMO_MODULE_LED); + //LED_OFF(DEMO_MODULE_LED); + + i2c_test1.status = I2CTransSuccess; + i2c_test1.slave_addr = 0x3C; + + i2c_abuse_test_counter = 0; + i2c_abuse_test_bitrate = 10000; + + i2c_test2.status = I2CTransSuccess; + +} + +static void i2c_abuse_send_transaction(uint8_t _init) +{ + + i2c_test1.slave_addr = 0x3C; + i2c_test1.len_w = 0; + i2c_test1.len_r = 0; + + switch (_init) + { + case 1: + i2c_test1.type = I2CTransTx; + i2c_test1.buf[0] = 0x00; // set to rate to 50Hz + i2c_test1.buf[1] = 0x00 | (0x06 << 2); + i2c_test1.buf[2] = 0x01<<5; + i2c_test1.buf[3] = 0x00; + i2c_test1.len_w = 4; + i2c_submit(&i2c2,&i2c_test1); + break; + case 2: + i2c_test1.type = I2CTransTx; + i2c_test1.buf[0] = 0x01; // set to gain to 1 Gauss + i2c_test1.buf[1] = 0x01<<5; + i2c_test1.len_w = 2; + i2c_submit(&i2c2,&i2c_test1); + break; + case 3: + i2c_test1.type = I2CTransTx; + i2c_test1.buf[0] = 0x00; // set to continuous mode + i2c_test1.len_w = 1; + i2c_submit(&i2c2,&i2c_test1); + break; + case 4: + i2c_test1.type = I2CTransRx; + i2c_test1.len_r = 1; + i2c_submit(&i2c2,&i2c_test1); + break; + case 5: + i2c_test1.type = I2CTransRx; + i2c_test1.len_r = 2; + i2c_submit(&i2c2,&i2c_test1); + break; + case 6: + i2c_test1.type = I2CTransRx; + i2c_test1.len_r = 3; + i2c_submit(&i2c2,&i2c_test1); + break; + case 7: + i2c_test1.type = I2CTransRx; + i2c_test1.len_r = 4; + i2c_submit(&i2c2,&i2c_test1); + break; + case 8: + i2c_test1.type = I2CTransRx; + i2c_test1.len_r = 5; + i2c_submit(&i2c2,&i2c_test1); + break; + case 9: + // bad addr + i2c_test1.slave_addr = 0x3C + 2; + i2c_test1.type = I2CTransTx; + i2c_test1.len_w = 1; + i2c_submit(&i2c2,&i2c_test1); + break; + case 10: + // 2 consecutive + i2c_test1.type = I2CTransTx; + i2c_test1.buf[0] = 0x00; // set to continuous mode + i2c_test1.len_w = 1; + i2c_submit(&i2c2,&i2c_test1); + break; + case 11: + i2c_test1.slave_addr = 0x3C; + i2c_test1.type = I2CTransTxRx; + i2c_test1.len_r = 1; + i2c_test1.len_w = 1; + i2c_test1.buf[0] = 0x03; + i2c_submit(&i2c2, &i2c_test1); + break; + case 12: + i2c_test1.slave_addr = 0x3C; + i2c_test1.type = I2CTransTxRx; + i2c_test1.len_r = 2; + i2c_test1.len_w = 1; + i2c_test1.buf[0] = 0x03; + i2c_submit(&i2c2, &i2c_test1); + break; + case 13: + i2c_test1.slave_addr = 0x3C; + i2c_test1.type = I2CTransTxRx; + i2c_test1.len_r = 3; + i2c_test1.len_w = 1; + i2c_test1.buf[0] = 0x03; + i2c_submit(&i2c2, &i2c_test1); + break; + case 14: + i2c_test1.slave_addr = 0x3C; + i2c_test1.type = I2CTransTxRx; + i2c_test1.len_r = 4; + i2c_test1.len_w = 1; + i2c_test1.buf[0] = 0x03; + i2c_submit(&i2c2, &i2c_test1); + break; + case 15: + i2c_test1.slave_addr = 0x3C; + i2c_test1.type = I2CTransTxRx; + i2c_test1.len_r = 4; + i2c_test1.len_w = 2; + i2c_test1.buf[0] = 0x03; + i2c_submit(&i2c2, &i2c_test1); + break; + default: + i2c_test1.slave_addr = 0x3C; + i2c_test1.type = I2CTransTxRx; + i2c_test1.len_r = 5; + i2c_test1.len_w = 1; + i2c_test1.buf[0] = 0x03; + i2c_submit(&i2c2, &i2c_test1); + } +} + + +void event_i2c_abuse_test(void) +{ + if (i2c_idle(&i2c1)) + { + LED_ON(7); // green = idle + LED_OFF(6); + } + else + { + LED_ON(6); // red = busy + LED_OFF(7); + } + + if (i2c_idle(&i2c2)) + { + LED_ON(5); // green = idle + LED_OFF(4); + } + else + { + LED_ON(4); // red = busy + LED_OFF(5); + } + + // Wait for I2C transaction object to be released by the I2C driver before changing anything + if ((i2c_abuse_test_counter < 12) && (i2c_abuse_test_counter > 3)) + { + if ((i2c_test2.status == I2CTransFailed) || (i2c_test2.status == I2CTransSuccess)) + { + //i2c_test2.slave_addr = 0x90; + i2c_test2.type = I2CTransRx; + i2c_test2.slave_addr = 0x92; + i2c_test2.len_r = 2; + i2c_submit(&i2c2,&i2c_test2); + } + } + + + if ((i2c_test1.status == I2CTransFailed) || (i2c_test1.status == I2CTransSuccess)) + { + if (i2c_abuse_test_counter < 16) + { + i2c_abuse_test_counter++; + } + else + { + // wait until ready: + if (i2c_idle(&i2c2)) + { + i2c_abuse_test_counter = 1; + + i2c_setbitrate(&i2c2, i2c_abuse_test_bitrate); + + i2c_abuse_test_bitrate += 17000; + if (i2c_abuse_test_bitrate > 500000) + { + i2c_abuse_test_bitrate -= 500000; + } + LED_TOGGLE(4); + } + } + + if (i2c_abuse_test_counter < 16) + { + i2c_abuse_send_transaction( i2c_abuse_test_counter ); + LED_TOGGLE(5); + } + } +} + +void periodic_50Hz_i2c_abuse_test(void) { + // LED_TOGGLE(DEMO_MODULE_LED); +} + + + diff --git a/sw/airborne/modules/benchmark/i2c_abuse_test.h b/sw/airborne/modules/benchmark/i2c_abuse_test.h new file mode 100644 index 0000000000..e6e7b0634a --- /dev/null +++ b/sw/airborne/modules/benchmark/i2c_abuse_test.h @@ -0,0 +1,52 @@ +/* + * $Id$ + * + * Copyright (C) 2009 C. De Wagter + * + * This file is part of paparazzi. + * + * paparazzi 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, or (at your option) + * any later version. + * + * paparazzi 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 paparazzi; see the file COPYING. If not, write to + * the Free Software Foundation, 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +/** \file I2C_ABUSE_TEST_module.h + * + * Total I2C Abuse: + * + * -all transaction types: T1 T2 T3 T4 R1 R2 R3 R4 T1R1 T2R1 T1R2 T1R3 T1R4 T1R5 T2R5 + * -all bitrates: 1k (way too slow) to 1M (way to fast) + * -occasional Short circuit (simulate bus capacitance or EMI errors) + * -variable bus load: from empty to full stack + * + * -Connect LED to MosFet that pulls-down the SCL and SDA lines + */ + +#ifndef I2C_ABUSE_TEST_MODULE_H +#define I2C_ABUSE_TEST_MODULE_H + +#ifndef I2C_ABUSE_SHORT_SCL_LED +#define I2C_ABUSE_SHORT_SCL_LED 2 +#endif + +#ifndef I2C_ABUSE_SHORT_SDA_LED +#define I2C_ABUSE_SHORT_SDA_LED 3 +#endif + +void init_i2c_abuse_test(void); +void event_i2c_abuse_test(void); +void periodic_50Hz_i2c_abuse_test(void); + +#endif