From 8b0bb4bbb9455afd1f76d071ecacd15050fb0dbf Mon Sep 17 00:00:00 2001 From: Martin Guy Date: Mon, 19 Mar 2012 12:12:29 +0100 Subject: [PATCH] Ensure Mizar32 i2c pins are initialized if i2c.setup() is not called --- src/platform/avr32/i2c.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/platform/avr32/i2c.c b/src/platform/avr32/i2c.c index 25bab83e..aa689323 100644 --- a/src/platform/avr32/i2c.c +++ b/src/platform/avr32/i2c.c @@ -159,11 +159,14 @@ static void ARBITRATION_LOST(void); // Bus control was lost // ************************ // The bitbanger itself, taken from http://en.wikipedia.org/wiki/I2C -// We don't use GPIO open-drain mode, which is not available on all hardware -// models. Instead, we use two modes to simulate open-drain: -// output of 0 and input. -u32 i2c_setup( u32 speed ) +// One-time initialization of the I2C hardware. +// Should be called at all valid initial i2c entry points. +static void i2c_init() { + static bool i2c_is_initialized = 0; + + if( i2c_is_initialized ) return; + // First, set both pins as high-impedance inputs to avoid startup glitches sda_regs->oderc = SDA_PINMASK; scl_regs->oderc = SCL_PINMASK; @@ -176,6 +179,16 @@ u32 i2c_setup( u32 speed ) sda_regs->gpers = SDA_PINMASK; scl_regs->gpers = SCL_PINMASK; + i2c_is_initialized++; // set to true +} + +// We don't use GPIO open-drain mode, which is not available on all hardware +// models. Instead, we use two modes to simulate open-drain: +// output of 0 and input. +u32 i2c_setup( u32 speed ) +{ + i2c_init(); + // Limit range to possible values, to avoid divisions by zero below. if (speed == 0) speed = 1; if (speed > REQ_CPU_FREQ / 2) speed = REQ_CPU_FREQ / 2; @@ -193,6 +206,8 @@ static int started = 0; void i2c_start_cond(void) { + i2c_init(); + if (started) { // if started, do a restart cond // set SDA to 1