// Public Methods //////////////////////////////////////////////////////////////
void TwoWire::begin(uint8_t address)
{
    rxBufferIndex = 0;
    rxBufferLength = 0;

    txBufferIndex = 0;
    txBufferLength = 0;

    slaveBufferIndex = 0;
    slaveBufferLength = 0;

    transmitting_master = false;
    transmitting_slave = false;
    receiving_slave = false;

    SIM_HAL_EnableClock(SIM, gate_name);

    PORT_CLOCK_ENABLE(sda);
    PORT_CLOCK_ENABLE(scl);
    PORT_BWR_PCR_ODE(PERIPH_PORT(sda), PINS_PIN(sda), true); //set as open drain
    PORT_BWR_PCR_ODE(PERIPH_PORT(scl), PINS_PIN(scl), true); //set as open drain
    PORT_SET_MUX_I2C(sda);
    PORT_SET_MUX_I2C(scl);
    I2C_HAL_Init(instance);

    I2C_HAL_SetAddress7bit(instance, address);
    I2C_HAL_SetStartStopIntCmd(instance, true);
    I2C_HAL_SetIntCmd(instance, true);
    NVIC_EnableIRQ(irqNumber);
    I2C_HAL_Enable(instance);
    setClock(100000);
}
/* This function is called for configuring pinmux for i2c module
 * This function only support switching default or gpio or fixed-ALTx mode on fixed pins
 * (Although there are many ALTx-pinmux configuration choices on various pins for the same
 * peripheral module) */
void i2c_pinmux_config(unsigned int instance, pinmux_type_t pinmux)
{
    switch(instance)
    {
        case 0:
            break;
#if BL_CONFIG_I2C            
        case 1:
            switch(pinmux)
            {
                case kPinmuxType_Default:
                    PORT_BWR_PCR_MUX(PORTC, 10, 0);
                    PORT_BWR_PCR_MUX(PORTC, 11, 0);
                    PORT_BWR_PCR_ODE(PORTC, 10, 0);
                    PORT_BWR_PCR_ODE(PORTC, 11, 0);
                    break;
                case kPinmuxType_Peripheral:
                    // Enable pins for I2C1.
                    PORT_BWR_PCR_MUX(PORTC, 10, 2);  // I2C1_SCL is ALT2 for pin PTC10
                    PORT_BWR_PCR_ODE(PORTC, 10, 1);  // I2C1_SCL set for open drain
                    PORT_BWR_PCR_MUX(PORTC, 11, 2);  // I2C1_SDA is ALT2 for pin PTC11
                    PORT_BWR_PCR_ODE(PORTC, 11, 1);  // I2C1_SDA set for open drain
                    break;
                default:
                    break;
            }
            break;
#endif // BL_CONFIG_I2C            
        case 2:
            break;
        default:
            break;
    }
}
/* This function is called for configuring pinmux for i2c module
 * This function only support switching default or gpio or fixed-ALTx mode on fixed pins
 * (Although there are many ALTx-pinmux configuration choices on various pins for the same
 * peripheral module) */
void i2c_pinmux_config(unsigned int instance, pinmux_type_t pinmux)
{
    switch(instance)
    {
#if BL_CONFIG_I2C      
        case 0:
            switch(pinmux)
            {
                case kPinmuxType_Default:
                    PORT_BWR_PCR_MUX(PORTD, 8, 0);
                    PORT_BWR_PCR_ODE(PORTD, 8, 0);  // I2C0_SCL set for open drain
                    PORT_BWR_PCR_MUX(PORTD, 9, 0);
                    PORT_BWR_PCR_ODE(PORTD, 9, 0);  // I2C0_SDA set for open drain
                    break;
                case kPinmuxType_Peripheral:
                    // Enable pins for I2C0.
                    PORT_BWR_PCR_MUX(PORTD, 8, 2);  // I2C0_SCL is ALT5 for pin PTE24
                    PORT_BWR_PCR_ODE(PORTD, 8, 1); // I2C0_SCL set for open drain
                    PORT_BWR_PCR_MUX(PORTD, 9, 2);  // I2C0_SDA is ALT5 for pin PTE25
                    PORT_BWR_PCR_ODE(PORTD, 9, 1); // I2C0_SDA set for open drain
                    break;
                default:
                    break;
            }          
            break;
#endif // BL_CONFIG_I2C            
        default:
            break;
    }
}
/* This function is called for configurating pinmux for i2c module
 * This function only support switching default or gpio or fixed-ALTx mode on fixed pins
 * (Although there are many ALTx-pinmux configuration choices on various pins for the same
 * peripheral module) 
 */
void i2c_pinmux_config(unsigned int instance, pinmux_type_t pinmux)
{
    switch(pinmux)
    {
    case kPinmuxType_Default:
        PORT_BWR_PCR_MUX(I2C_SCL_PORT, I2C_SCL_PIN, 0);
        PORT_BWR_PCR_MUX(I2C_SCL_PORT, I2C_SCL_PIN, 0);
        PORT_BWR_PCR_ODE(I2C_SDA_PORT, I2C_SDA_PIN, 0);
        PORT_BWR_PCR_ODE(I2C_SDA_PORT, I2C_SDA_PIN, 0);
        break;
    case kPinmuxType_Peripheral:
        // Enable pins for I2C.
        PORT_BWR_PCR_MUX(I2C_SCL_PORT, I2C_SCL_PIN, I2C_SCL_ALT);
        PORT_BWR_PCR_MUX(I2C_SCL_PORT, I2C_SCL_PIN, I2C_SDA_ALT);
        PORT_BWR_PCR_ODE(I2C_SDA_PORT, I2C_SDA_PIN, 0);
        PORT_BWR_PCR_ODE(I2C_SDA_PORT, I2C_SDA_PIN, 0);
        break;
    }
       
}