Пример #1
0
int i2c_init_master(i2c_t dev, i2c_speed_t speed)
{
    int ccr;

    if ((unsigned int)dev >= I2C_NUMOF) {
        return -1;
    }

    /* 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;
    }
    I2C_TypeDef *i2c = i2c_config[dev].dev;

    /* enable I2C clock */
    i2c_poweron(dev);

    /* set IRQn priority */
    NVIC_SetPriority(i2c_config[dev].er_irqn, I2C_IRQ_PRIO);

    /* enable IRQn */
    NVIC_EnableIRQ(i2c_config[dev].er_irqn);

    /* configure pins */
    gpio_init(i2c_config[dev].scl, GPIO_DIR_OUT, GPIO_PULLUP);
    gpio_init_af(i2c_config[dev].scl, i2c_config[dev].af);
    gpio_init(i2c_config[dev].sda, GPIO_DIR_OUT, GPIO_PULLUP);
    gpio_init_af(i2c_config[dev].sda, i2c_config[dev].af);

    /* configure device */
    _i2c_init(i2c, ccr);

    return 0;
}
Пример #2
0
int i2c_init_master(i2c_t dev, i2c_speed_t speed)
{
    /* TWI Bit Rate Register - division factor for the bit rate generator */
    unsigned long twibrr;
    /* TWI Prescaler Bits - default 0 */
    uint8_t twipb = 0;

    /* check if the line is valid */
    if (dev >= I2C_NUMOF) {
        return -1;
    }

    /* calculate speed configuration */
    switch (speed) {

        case I2C_SPEED_LOW:
            if ((CLOCK_CORECLOCK > 20000000UL)
                || (CLOCK_CORECLOCK < 1000000UL)) {
                return -2;
            }
            twibrr = ((CLOCK_CORECLOCK / 10000UL) - 16) / (2 * 4);  /* CLK Prescaler 4 */
            twipb = 1;
            break;

        case I2C_SPEED_NORMAL:
            if ((CLOCK_CORECLOCK > 50000000UL)
                || (CLOCK_CORECLOCK < 2000000UL)) {
                return -2;
            }
            twibrr = ((CLOCK_CORECLOCK / 100000UL) - 16) / 2;
            break;

        case I2C_SPEED_FAST:
            if (CLOCK_CORECLOCK < 7500000UL) {
                return -2;
            }
            twibrr = ((CLOCK_CORECLOCK / 400000UL) - 16) / 2;
            break;

        case I2C_SPEED_FAST_PLUS:
            if (CLOCK_CORECLOCK < 18000000UL) {
                return -2;
            }
            twibrr = ((CLOCK_CORECLOCK / 1000000UL) - 16) / 2;
            break;

        case I2C_SPEED_HIGH:
            if (CLOCK_CORECLOCK < 62000000UL) {
                return -2;
            }
            twibrr = ((CLOCK_CORECLOCK / 3400000UL) - 16) / 2;
            break;

        default:
            return -2;
    }

    /* set pull-up on SCL and SDA */
    I2C_PORT_REG |= (I2C_PIN_MASK);

    /* enable I2C clock */
    i2c_poweron(dev);

    /* disable device */
    TWCR &= ~(1 << TWEN);
    /* configure I2C clock */
    TWBR = (uint8_t)twibrr;     /* Set TWI Bit Rate Register */
    TWSR &= ~(0x03);            /* Reset TWI Prescaler Bits */
    TWSR |= twipb;              /* Set TWI Prescaler Bits */
    /* enable device */
    TWCR |= (1 << TWEN);

    return 0;
}