Tiva C I2C slave example code

//*****************************************************************************
//
//! \addtogroup i2c_examples_list
//! <h1>Slave Receive Interrupt (slave_receive_int)</h1>
//!
//! This example shows how to configure a receive interrupt on the slave
//! module.  This includes setting up the I2C0 module for loopback mode as well
//! as configuring the master and slave modules.  Loopback mode internally
//! connects the master and slave data and clock lines together.  The address
//! of the slave module is set to a value so it can receive data from the
//! master.
//!
//! This example uses the following peripherals and I/O signals.  You must
//! review these and change as needed for your own board:
//! - I2C0 peripheral
//! - GPIO Port B peripheral (for I2C0 pins)
//! - I2C0SCL - PB2
//! - I2C0SDA - PB3
//!
//! The following UART signals are configured only for displaying console
//! messages for this example.  These are not required for operation of I2C.
//! - UART0 peripheral
//! - GPIO Port A peripheral (for UART0 pins)
//! - UART0RX - PA0
//! - UART0TX - PA1
//!
//! This example uses the following interrupt handlers.  To use this example
//! in your own application you must add these interrupt handlers to your
//! vector table.
//! - INT_I2C0 - I2C0SlaveIntHandler
//
//*****************************************************************************

#define verisayisi      12
#define SLAVE_ADDRESS   0x3C

void
uart_hazirla(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTStdioConfig(0, 115200, 16000000);
}

int main(void)
{
    uint32_t pui32DataTx[verisayisi]={'s','e','r','h','a','t',' ','s','e','f','e','r'};
    uint32_t pui32DataRx[verisayisi];
    uint32_t ui32Index;

    SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    GPIOPinConfigure(GPIO_PB3_I2C0SDA);
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
    I2CLoopbackEnable(I2C0_BASE);
    I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
    I2CSlaveEnable(I2C0_BASE);
    I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);
    I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);

    uart_hazirla();

    UARTprintf(" I2C Ornegi ->");
    UARTprintf("\n Hiz = 100kbps\n\n");

    for(ui32Index = 0; ui32Index < verisayisi; ui32Index++)
    {
        pui32DataRx[ui32Index] = 0;
    }

    UARTprintf("\nMaster'dan Slave'e Transfer Ediliyor.\n");
    for(ui32Index = 0; ui32Index < verisayisi; ui32Index++)
    {
        UARTprintf(" Gönderiliyor: '%c' ", pui32DataTx[ui32Index]);
        I2CMasterDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

        while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ)){}

        pui32DataRx[ui32Index] = I2CSlaveDataGet(I2C0_BASE);
        while(I2CMasterBusy(I2C0_BASE))
        {
        }

        UARTprintf("Alinan: '%c'\n", pui32DataRx[ui32Index]);
    }

    for(ui32Index = 0; ui32Index < verisayisi; ui32Index++)
    {
        pui32DataRx[ui32Index] = 0;
        UARTprintf("\n\n Slave'den Master'a Transfer Ediliyor\n");
        I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

        while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ)){}

        for(ui32Index = 0; ui32Index < verisayisi; ui32Index++)
        {
            UARTprintf(" Gonderiliyor: '%c' ", pui32DataTx[ui32Index]);
            I2CSlaveDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
            I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

            while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ)){}
            pui32DataRx[ui32Index] = I2CMasterDataGet(I2C0_BASE);
            UARTprintf("Alinan: '%c'\n", pui32DataRx[ui32Index]);
        }
    }

    UARTprintf("\nTamamlandi.\n\n");
    return(0);
}

留言

這個網誌中的熱門文章

python serial 模組使用方法 #1

USB HID 教學 #1(轉載)

USB HID 教學 #2 (轉載)