int i2c_init_master(i2c_t dev, i2c_speed_t speed) { I2C_TypeDef *i2c; GPIO_TypeDef *port_scl; GPIO_TypeDef *port_sda; int pin_scl = 0, pin_sda = 0; int ccr; /* read speed configuration */ switch (speed) { case I2C_SPEED_NORMAL: ccr = I2C_APBCLK / 200000; break; case I2C_SPEED_FAST: ccr = I2C_APBCLK / 800000; break; default: return -2; } /* read static device configuration */ switch (dev) { #if I2C_0_EN case I2C_0: i2c = I2C_0_DEV; port_scl = I2C_0_SCL_PORT; pin_scl = I2C_0_SCL_PIN; port_sda = I2C_0_SDA_PORT; pin_sda = I2C_0_SDA_PIN; I2C_0_CLKEN(); I2C_0_SCL_CLKEN(); I2C_0_SDA_CLKEN(); NVIC_SetPriority(I2C_0_ERR_IRQ, I2C_IRQ_PRIO); NVIC_EnableIRQ(I2C_0_ERR_IRQ); break; #endif #if I2C_1_EN case I2C_1: i2c = I2C_1_DEV; port_scl = I2C_1_SCL_PORT; pin_scl = I2C_1_SCL_PIN; port_sda = I2C_1_SDA_PORT; pin_sda = I2C_1_SDA_PIN; I2C_1_CLKEN(); I2C_1_SCL_CLKEN(); I2C_1_SDA_CLKEN(); NVIC_SetPriority(I2C_1_ERR_IRQ, I2C_IRQ_PRIO); NVIC_EnableIRQ(I2C_1_ERR_IRQ); break; #endif default: return -1; } /* configure pins */ _pin_config(port_scl, port_sda, pin_scl, pin_sda); /* configure device */ _i2c_init(i2c, ccr); /* make sure the analog filters don't hang -> see errata sheet 2.14.7 */ if (i2c->SR2 & I2C_SR2_BUSY) { DEBUG("LINE BUSY AFTER RESET -> toggle pins now\n"); /* disable peripheral */ i2c->CR1 &= ~I2C_CR1_PE; /* toggle both pins to reset analog filter */ _toggle_pins(port_scl, port_sda, pin_scl, pin_sda); /* reset pins for alternate function */ _pin_config(port_scl, port_sda, pin_scl, pin_sda); /* make peripheral soft reset */ i2c->CR1 |= I2C_CR1_SWRST; i2c->CR1 &= ~I2C_CR1_SWRST; /* enable device */ _i2c_init(i2c, ccr); } return 0; }
int i2c_init_master(i2c_t dev, i2c_speed_t speed) { I2C_TypeDef *i2c; GPIO_TypeDef *port_scl; GPIO_TypeDef *port_sda; int pin_scl, pin_sda; int ccr; /* read speed configuration */ switch (speed) { case I2C_SPEED_NORMAL: ccr = I2C_APBCLK / 200000; break; case I2C_SPEED_FAST: ccr = I2C_APBCLK / 800000; break; default: return -2; } /* read static device configuration */ switch (dev) { #if I2C_0_EN case I2C_0: i2c = I2C_0_DEV; port_scl = I2C_0_SCL_PORT; pin_scl = I2C_0_SCL_PIN; port_sda = I2C_0_SDA_PORT; pin_sda = I2C_0_SDA_PIN; I2C_0_CLKEN(); I2C_0_SCL_CLKEN(); I2C_0_SDA_CLKEN(); break; #endif default: return -1; } /* configure pins, alternate output, open-drain, output mode with 50MHz */ if (pin_scl < 8) { port_scl->CRL |= (0xf << (pin_scl * 4)); } else { port_scl->CRH |= (0xf << ((pin_scl - 8) * 4)); } if (pin_sda < 8) { port_sda->CRL |= (0xf << (pin_sda * 4)); } else { port_sda->CRH |= (0xf << ((pin_sda - 8) * 4)); } /* disable device and set ACK bit */ i2c->CR1 = I2C_CR1_ACK; /* configure I2C clock */ i2c->CR2 = (I2C_APBCLK / 1000000); i2c->CCR = ccr; i2c->TRISE = (I2C_APBCLK / 1000000) + 1; /* configure device */ i2c->OAR1 = 0; /* makes sure we are in 7-bit address mode */ /* enable device */ i2c->CR1 |= I2C_CR1_PE; return 0; }