示例#1
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // determine the SPI to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
    obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT((int)obj->i2c != NC);
    
    // enable power
    i2c_power_enable(obj);
    
    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_conclr(obj, 1, 1, 1, 1);
    i2c_interface_enable(obj);
    
    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);

    // OpenDrain must explicitly be enabled for p0.0 and p0.1
    if (sda == P0_0) {
        pin_mode(sda, OpenDrain);
    }
    if (scl == P0_1) {
        pin_mode(scl, OpenDrain);
    }

}
示例#2
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // determine the SPI to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
    obj->i2c = (I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT((int)obj->i2c != NC);

    // enable power
    i2c_power_enable(obj);

    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);

    pin_mode(sda, OpenDrain);
    pin_mode(scl, OpenDrain);

    // Force reset if the bus is stuck in the BUSY state
    if (obj->i2c->SR2 & I2C_SR2_BUSY) {
        obj->i2c->CR1 |= I2C_CR1_SWRST;
        obj->i2c->CR1 &= ~I2C_CR1_SWRST;
    }

    // Set the peripheral clock frequency
    obj->i2c->CR2 |= 42;

    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_interface_enable(obj);
}
示例#3
0
文件: i2c_api.c 项目: nickmolo/ECE477
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
    const SWM_Map *swm;
    uint32_t regVal;
    int i2c_ch = 0;
    
    //LPC824
    //I2C0 can support FM+ but only on P0_11 and P0_10
    if (sda == I2C_SDA && scl == I2C_SCL) {
      //Select I2C mode for P0_11 and P0_10
      LPC_SWM->PINENABLE0 &= ~(0x3 << 11);
      
#if(LPC824_I2C0_FMPLUS == 1)
      // Enable FM+ mode on P0_11, P0_10    
      LPC_IOCON->PIO0_10 &= ~(0x3 << 8);
      LPC_IOCON->PIO0_10 |=  (0x2 << 8); //FM+ mode
      LPC_IOCON->PIO0_11 &= ~(0x3 << 8);
      LPC_IOCON->PIO0_11 |=  (0x2 << 8); //FM+ mode      
#endif
    }
    else {
        //Select any other pin for I2C1, I2C2 or I2C3
        i2c_ch = get_available_i2c();
        if (i2c_ch == -1)
            return;
        i2c_used |= (1 << (i2c_ch - 1));

        swm = &SWM_I2C_SDA[i2c_ch - 1];
        regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
        LPC_SWM->PINASSIGN[swm->n] = regVal |  ((sda >> PIN_SHIFT) << swm->offset);

        swm = &SWM_I2C_SCL[i2c_ch - 1];
        regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
        LPC_SWM->PINASSIGN[swm->n] = regVal |  ((scl >> PIN_SHIFT) << swm->offset);
    }

    switch(i2c_ch) {
        case 0:
            obj->i2c = (LPC_I2C0_Type *)LPC_I2C0;
            break;
        case 1:
            obj->i2c = (LPC_I2C0_Type *)LPC_I2C1;
            break;
        case 2:
            obj->i2c = (LPC_I2C0_Type *)LPC_I2C2;
            break;
        case 3:
            obj->i2c = (LPC_I2C0_Type *)LPC_I2C3;
            break;
        default:
            break;
    }

    // enable power
    i2c_power_enable(i2c_ch);
    // set default frequency at 100k
    i2c_frequency(obj, 100000);   
    i2c_interface_enable(obj);
}
示例#4
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
    const SWM_Map *swm;
    uint32_t regVal;
    int i2c_ch = 0;

    if (sda == I2C_SDA && scl == I2C_SCL) {
        LPC_SWM->PINENABLE0 &= ~(0x3 << 11);
    }
    else {
        i2c_ch = get_available_i2c();
        if (i2c_ch == -1)
            return;
        i2c_used |= (1 << (i2c_ch - 1));

        swm = &SWM_I2C_SDA[i2c_ch - 1];
        regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
        LPC_SWM->PINASSIGN[swm->n] = regVal |  ((sda >> PIN_SHIFT) << swm->offset);

        swm = &SWM_I2C_SCL[i2c_ch - 1];
        regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
        LPC_SWM->PINASSIGN[swm->n] = regVal |  ((scl >> PIN_SHIFT) << swm->offset);
    }

    switch(i2c_ch) {
    case 0:
        obj->i2c = (LPC_I2C0_Type *)LPC_I2C0;
        break;
    case 1:
        obj->i2c = (LPC_I2C0_Type *)LPC_I2C1;
        break;
    case 2:
        obj->i2c = (LPC_I2C0_Type *)LPC_I2C2;
        break;
    case 3:
        obj->i2c = (LPC_I2C0_Type *)LPC_I2C3;
        break;
    default:
        break;
    }

    // enable power
    i2c_power_enable(i2c_ch);

    uint32_t size_in_bytes = LPC_I2CD_API->i2c_get_mem_size();
    i2c_buffer = malloc(size_in_bytes);
    obj->handler = LPC_I2CD_API->i2c_setup((uint32_t)(obj->i2c), i2c_buffer);
    LPC_I2CD_API->i2c_set_bitrate(obj->handler, SystemCoreClock, 100000);
    LPC_I2CD_API->i2c_set_timeout(obj->handler, 100000);

    i2c_interface_enable(obj);
}
示例#5
0
文件: i2c_api.c 项目: nickmolo/ECE477
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // determine the SPI to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
    obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT((int)obj->i2c != NC);
    
    // enable power
    i2c_power_enable(obj);
    
    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_conclr(obj, 1, 1, 1, 1);
    i2c_interface_enable(obj);
    
    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);
}
示例#6
0
文件: i2c_api.c 项目: pan-/mbed
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    obj->i2c = (LPC_I2C_TypeDef *)LPC_I2C;
    
    const SWM_Map *swm;
    uint32_t regVal;
    
    swm = &SWM_I2C_SDA[0];
    regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
    LPC_SWM->PINASSIGN[swm->n] = regVal |  (sda   << swm->offset);
    
    swm = &SWM_I2C_SCL[0];
    regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
    LPC_SWM->PINASSIGN[swm->n] = regVal |  (scl   << swm->offset);
    
    // enable power
    i2c_power_enable(obj);
    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_interface_enable(obj);
}
示例#7
0
void twi_master_init(i2c_t *obj, PinName sda, PinName scl, int frequency)
{
    NRF_GPIO->PIN_CNF[scl] = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
                              (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
                              (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
                              (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
                              (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));

    NRF_GPIO->PIN_CNF[sda] = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
                              (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
                              (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
                              (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
                              (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));

    obj->i2c->PSELSCL = scl;
    obj->i2c->PSELSDA = sda;
    // set default frequency at 100k
    i2c_frequency(obj, frequency);
    i2c_interface_enable(obj);
}
示例#8
0
void i2c_frequency(i2c_t *obj, int hz) {
    i2c_interface_disable(obj);
    obj->i2c->CCR &= ~(I2C_CCR_CCR | I2C_CCR_FS);
    if (hz > 100000) {
        // Fast Mode
        obj->i2c->CCR |= I2C_CCR_FS;
        int result = 42000000 / (hz * 3);
        obj->i2c->CCR |= result & I2C_CCR_CCR;
        obj->i2c->TRISE = ((42 * 300) / 1000) + 1;
    }
    else {
        // Standard mode
        obj->i2c->CCR &= ~I2C_CCR_FS;
        int result = 42000000 / (hz << 1);
        result = result < 0x4 ? 0x4 : result;
        obj->i2c->CCR |= result & I2C_CCR_CCR;
        obj->i2c->TRISE = 42 + 1;
    }
    i2c_interface_enable(obj);
}
示例#9
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // determine the SPI to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
    obj->i2c = (LPC_I2C_T *)pinmap_merge(i2c_sda, i2c_scl);
    
    if ((int)obj->i2c == NC) {
        error("I2C pin mapping failed");
    }
    
    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_conclr(obj, 1, 1, 1, 1);
    i2c_interface_enable(obj);

    // If pins are not dedicated, set SCU functions
    if (sda != P_DED) {
        pinmap_pinout(sda, PinMap_I2C_SDA);
    }
    if (scl != P_DED) {
        pinmap_pinout(scl, PinMap_I2C_SCL);
    }
}
示例#10
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // determine the SPI to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
    obj->i2c = (LPC_I2C_T *)pinmap_merge(i2c_sda, i2c_scl);
    
    if ((int)obj->i2c == NC) {
        error("I2C pin mapping failed");
    }
    
    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_conclr(obj, 1, 1, 1, 1);
    i2c_interface_enable(obj);

    // Set SCU functions
    if (scl == P_DED) {
        // Enable dedicated I2C0 SDA and SCL pins (open drain)
        LPC_SCU->SFSI2C0 = (1 << 11) | (1 << 3);
    } else {
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
    }
}
示例#11
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // determine the SPI to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
    obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
#elif defined(TARGET_LPC11U24)
    obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl);
#endif
    if ((int)obj->i2c == NC) {
        error("I2C pin mapping failed");
    }

    // enable power
    i2c_power_enable(obj);

    // set default frequency at 100k
    i2c_frequency(obj, 100000);
    i2c_conclr(obj, 1, 1, 1, 1);
    i2c_interface_enable(obj);

    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);
}