Hello everyone,
I have a problem that i couldn't init the I2C on TM4C. Here is the my code. I guess that i2c send function is ok but I thought that my problem is about to initialize the i2c3_base. I searched on some other codes (examples or other ones) but i couldn't make it true. Help please.
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.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"
typedef enum I2CResultTypes
{
ACK_RECEIVED,
ACK_ERROR
}I2CResultType;
typedef enum I2CTypes
{
I2C0,
I2C1,
I2C2,
I2C3
}I2CType;
typedef enum I2cRegisters
{
VREG_A = 0x11,
VREG_B,
NVREG_A = 0x21,
NVREG_B,
}I2cRegister;
typedef enum I2cAddresses
{
ADDRESS0 = 0x28,
ADDRESS1 = 0x29,
ADDRESS2 = 0x2A,
ADDRESS3 = 0x2B,
ADDRESS4 = 0x2C,
ADDRESS5 = 0x2D,
ADDRESS6 = 0x2E,
ADDRESS7 = 0x2F
}I2cAddress;
I2CResultType I2C_Send(I2CType i2cPort, unsigned char slaveAddress, unsigned char regAddress, unsigned char data)
{
int result;
I2CMasterIntClear(i2cPort);
I2CMasterSlaveAddrSet(i2cPort, slaveAddress, 0);
I2CMasterDataPut(i2cPort, regAddress);
I2CMasterControl(i2cPort, I2C_MASTER_CMD_BURST_SEND_FINISH);
while (I2CMasterBusy(i2cPort));
result = I2CMasterErr(i2cPort);
if (result != I2C_MASTER_ERR_NONE)
{
return ACK_ERROR;
}
I2CMasterDataPut(i2cPort, data);
I2CMasterControl(i2cPort, I2C_MASTER_CMD_BURST_SEND_FINISH);
while (I2CMasterBusy(i2cPort));
result = I2CMasterErr(i2cPort);
if (result != I2C_MASTER_ERR_NONE)
{
return ACK_ERROR;
}
return ACK_RECEIVED;
}
void MAX5479_SetValue2( unsigned char value)
{
I2CResultType result = ACK_ERROR;
I2CType I2c = I2C3;
I2cAddress address = 0x29;
I2cRegister regAddress =0x11 ;
result = I2C_Send(I2C3, 0x29, 0x11, value);
if (result == ACK_RECEIVED)
{
asm("NOP");
}
else
{
asm("NOP");
}
}
void I2CInit (void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
//SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOG);
GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_0);
GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_1);
GPIOPinConfigure(GPIO_PG0_I2C3SCL);
GPIOPinConfigure(GPIO_PG1_I2C3SDA);
I2CMasterInitExpClk(I2C3_BASE, SysCtlClockGet(), false);
}
int
main(void)
{
volatile uint32_t ui32Loop;
SysCtlClockSet(SYSCTL_SYSDIV_3 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_25MHZ);
I2CInit();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK);
//
// Check if the peripheral access is enabled.
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOK))
{
}
GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_3);
while(1)
{
MAX5479_SetValue2(20);
}
}