#include #include #include #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/debug.h" #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/i2c.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/rom.h" #include "driverlib/rom_map.h" #include "driverlib/sysctl.h" #include "driverlib/timer.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" void ConfigureGPIO(void); void ConfigureI2C0(void); //***************************************************************************** // Define MSP430 I2C Address. // //***************************************************************************** #define SLAVE_ADDRESS 0x48 //***************************************************************************** // // Counter to count the number of interrupts that have been called. // //***************************************************************************** static volatile uint32_t g_ui32Counter = 0; static uint32_t g_ui32DataRx; static bool g_bIntFlag = false; //**************************************************************************** // // System clock rate in Hz. // //**************************************************************************** uint32_t g_ui32SysClock; uint32_t pui32DataRx[4] = {0}; //***************************************************************************** // // Flags that contain the current value of the interrupt indicator as displayed // on the UART. // //***************************************************************************** //uint32_t g_ui32Flags; volatile uint_fast8_t g_ui32Flags; //***************************************************************************** // // The error routine that is called if the driver library encounters an error. // //***************************************************************************** #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif //***************************************************************************** // // Configure the UART and its pins. This must be called before UARTprintf(). // //***************************************************************************** void ConfigureUART(void) { // // Enable the GPIO Peripheral used by the UART. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Enable UART0. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // // Configure GPIO Pins for UART mode. // MAP_GPIOPinConfigure(GPIO_PA0_U0RX); MAP_GPIOPinConfigure(GPIO_PA1_U0TX); MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Initialize the UART for console I/O. // UARTStdioConfig(0, 115200, g_ui32SysClock); } void I2C0IntHandler(void) { UARTprintf("\n I2C0MASTERIntHandler........."); // // Clear the I2C0 interrupt flag. // I2CMasterIntClear(I2C0_BASE); // // Read the data from the slave. // g_ui32DataRx = I2CMasterDataGet(I2C0_BASE); // // Set a flag to indicate that the interrupt occurred. // g_bIntFlag = true; } //***************************************************************************** // // This example application demonstrates the use of the timers to generate // periodic interrupts. // //***************************************************************************** int main(void) { // // Run from the PLL at 120 MHz. // Note: SYSCTL_CFG_VCO_240 is a new setting provided in TivaWare 2.2.x and // later to better reflect the actual VCO speed due to SYSCTL#22. // g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); // // Initialize the UART and write status. // ConfigureUART(); ConfigureGPIO(); ConfigureI2C0(); g_ui32Counter = 0; // Enable processor interrupts. // MAP_IntMasterEnable(); // Loop forever while the timers run. // while(1) { // // Modify the data direction to true, so that seeing the address will // indicate that the I2C Master is initiating a read from the slave. // MAP_I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true); // // Setup for first read. Use I2C_MASTER_CMD_BURST_RECEIVE_START // to start a burst mode read. The I2C master continues to own // the bus at the end of this transaction. // MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); // // Read the first byte data from the slave. // pui32DataRx[0] = MAP_I2CMasterDataGet(I2C0_BASE); // while(I2CMasterBusy(I2C0_BASE)); // // Setup for the second read. Use I2C_MASTER_CMD_BURST_RECEIVE_CONT // to continue the burst mode read. The I2C master continues to own // the bus at the end of this transaction. // MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // // Read the second byte data from the slave. // pui32DataRx[1] = MAP_I2CMasterDataGet(I2C0_BASE); MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // // Read the third byte data from the slave // pui32DataRx[2] = MAP_I2CMasterDataGet(I2C0_BASE); MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // // Read the third byte data from the slave // pui32DataRx[3] = MAP_I2CMasterDataGet(I2C0_BASE); // // Setup for the third read. Use I2C_MASTER_CMD_BURST_RECEIVE_FINISH // to terminate the I2C transaction. At the end of this transaction, // the STOP bit will be issued and the I2C bus is returned to the // Idle state. // MAP_I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } } //***************************************************************************** // // Configures I2C port // //***************************************************************************** void ConfigureI2C0(void) { // // The I2C0 peripheral must be enabled before use. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);// // // Wait for the I2C0 module to be ready. // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)) { } // Configure the pin muxing for I2C0 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // MAP_GPIOPinConfigure(GPIO_PB2_I2C0SCL); MAP_GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. // MAP_GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); MAP_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // IntEnable(INT_I2C0); I2CMasterIntEnableEx(I2C0_BASE, I2C_MASTER_INT_DATA); // // Enable and initialize the I2C0 master module. Use the system clock for // the I2C0 module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. For this example we will use a data rate of 100kbps. // MAP_I2CMasterInitExpClk(I2C0_BASE, g_ui32SysClock, false); // // Enable the I2C0 MASTER module. // I2CMasterEnable(I2C0_BASE); }