Part Number: TM4C129ENCPDT
Hi all,
I'm trying to run a simple example for I2C5 on Tiva. I want to read one byte from SI7005. In datasheet, I have this description to read the register.
I started from loopback example in Tivaware and it works fine. Also, after change the source code I can see the SCL and SDA signals on oscilloscope as expected.
I'm using 400kbps for I2C clock and I have external pull-up resistors in SDA and SCL.
I can see only 0 in Watch Expressions when I try to read this register.
There is something wrong with the code?
I'm not understanding the configuration of this peripheral?
Or, is it more likely to be a hardware issue?
The code is attached.
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#define SLAVE_ADDRESS 0x40
#define ID_ADDRESS 0x11
uint32_t readI2C5(uint32_t device_address, uint32_t device_register)
{
//specify that we want to communicate to device address with an intended write to bus
I2CMasterSlaveAddrSet(I2C5_BASE, device_address, false);
//the register to be read
I2CMasterDataPut(I2C5_BASE, device_register);
//send control byte and register address byte to slave device
I2CMasterControl(I2C5_BASE, I2C_MASTER_CMD_SINGLE_SEND);
//wait for MCU to complete send transaction
while(I2CMasterBusy(I2C5_BASE));
//read from the specified slave device
I2CMasterSlaveAddrSet(I2C5_BASE, device_address, true);
//send control byte and read from the register from the MCU
I2CMasterControl(I2C5_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
//wait while checking for MCU to complete the transaction
while(I2CMasterBusy(I2C5_BASE));
//Get the data from the MCU register and return to caller
return( I2CMasterDataGet(I2C5_BASE));
}
//*****************************************************************************
//
// Configure the I2C5 master.
//
//*****************************************************************************
uint32_t test = 0;
void main(void)
{
uint32_t ui32SysClock;
// I don't have a crystal
ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_INT | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// The I2C5 peripheral must be enabled before use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C5);
//
// For this example I2C5 is used with PortB[0:1]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port B needs to be enabled so these pins can
// be used.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// 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.
//
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_0);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_1);
//
// Configure the pin muxing for I2C5 functions on port B0 and B1.
// This step is not necessary if your part does not support pin muxing.
//
GPIOPinConfigure(GPIO_PB0_I2C5SCL);
GPIOPinConfigure(GPIO_PB1_I2C5SDA);
//
// Enable and initialize the I2C5 master module. Use the system clock for
// the I2C5 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 400kbps.
//
I2CMasterInitExpClk(I2C5_BASE, ui32SysClock, true);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0); //SI7005 CS
SysCtlDelay(1200000);
for(;;) {
//GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0);
//SysCtlDelay(1200000); //10 ms
test = readI2C5(SLAVE_ADDRESS, ID_ADDRESS);
//GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, ~0);
SysCtlDelay(12000000); //100 ms
}
}