#include "driverlib.h"
#include "device.h"
#include "i2cLib_FIFO_polling.h"
//
// Globals
//
struct I2CHandle RTC;
struct I2CHandle *currentMsgPtr; // Used in interrupt
//! --------------------------------
//! Signal | I2CA | RTC
//! --------------------------------
//! SCL | DEVICE_GPIO_PIN_SCLB | SCL
//! SDA | DEVICE_GPIO_PIN_SDAB | SDA
//! --------------------------------
uint16_t passCount = 0;
uint16_t failCount = 0;
uint16_t AvailableI2C_targets[20];
uint16_t TX_MsgBuffer[MAX_BUFFER_SIZE];
uint16_t RX_MsgBuffer[MAX_BUFFER_SIZE];
uint32_t ControlAddr;
uint16_t status;
uint8_t set_time[7];
typedef struct{
unsigned int seconds;
unsigned int minutes ;
unsigned int hour ;
unsigned int dayofweek;
unsigned int dayofmonth;
unsigned int month ;
unsigned int year ;
}TIME;
TIME time;
void fail(void);
void pass(void);
void I2C_GPIO_init(void);
void I2Cinit(void);
void verifyRTCRead(void);
unsigned int decToBcd( unsigned int );
unsigned int bcdToDec(unsigned int );
void Set_Time (uint8_t, uint8_t , uint8_t , uint8_t , uint8_t , uint8_t , uint8_t );
void Get_Time (void);
//
// Main
//
void main(void)
{
//
// Initialize device clock and peripherals
//
Device_init();
//
// Disable pin locks and enable internal pullups.
//
Device_initGPIO();
//
// Initialize I2C pins
//
I2C_GPIO_init();
//
// Initialize PIE and clear PIE registers. Disable CPU interrupts.
//
Interrupt_initModule();
//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
//
Interrupt_initVectorTable();
I2Cinit();
//AvailableI2C_targets[0]=0xD0;
//I2Cs connected to I2CA will be found in AvailableI2C_targets buffer
//after you run I2CBusScan function.
uint16_t *pAvailableI2C_targets = AvailableI2C_targets;
status = I2CBusScan(I2CB_BASE, pAvailableI2C_targets);
uint16_t i;
for(i=0;i<MAX_BUFFER_SIZE;i++)
{
TX_MsgBuffer[i] = 0;
RX_MsgBuffer[i] = 0;
}
RTC.TargetAddr = 0xD0;
RTC.base = I2CB_BASE;
RTC.pControlAddr = &ControlAddr;
RTC.NumOfAddrBytes = 2;
RTC.pTX_MsgBuffer = TX_MsgBuffer;
RTC.pRX_MsgBuffer = RX_MsgBuffer;
RTC.NumOfAttempts = 5;
RTC.Delay_us = 10;
RTC.WriteCycleTime_in_us = 6000; //10ms forRTC this code was tested
//Example 1:RTC Byte Write
//Write 11 toRTC address 0x0
/* ControlAddr = 0;
RTC.NumOfDataBytes = 7;
TX_MsgBuffer[0]= 11;
status = I2C_ControllerTransmitter(&RTC);
//Wait forRTC write cycle time
//This delay is not mandatory. User can run their application code instead.
//It is however important to wait forRTC write cycle time before you initiate
//another read / write transaction
DEVICE_DELAY_US(RTC.WriteCycleTime_in_us);
//Example 2:RTC Byte Read
//Make sure 11 is written toRTC address 0x0
ControlAddr = 0;
RTC.pControlAddr = &ControlAddr;
RTC.NumOfDataBytes = 1;
status = I2C_ControllerReceiver(&RTC);
while(I2C_getStatus(RTC.base) & I2C_STS_BUS_BUSY);
verifyRTCRead();*/
//Example 3:RTC word (16-bit) write
//RTC address 0x1 = 22 & 0x2 = 33
Set_Time(00, 03, 14, 5, 3, 1, 19);
ControlAddr = 0x00; //RTC address to write
RTC.NumOfDataBytes = 7;
TX_MsgBuffer[0] = set_time[0];
TX_MsgBuffer[1] = set_time[1];
TX_MsgBuffer[2] = set_time[2];
TX_MsgBuffer[3] = set_time[3];
TX_MsgBuffer[4] = set_time[4];
TX_MsgBuffer[5] = set_time[5];
TX_MsgBuffer[6] = set_time[6];
RTC.pTX_MsgBuffer = TX_MsgBuffer;
status = I2C_ControllerTransmitter(&RTC);
//Wait forRTC write cycle time
//This delay is not mandatory. User can run their application code instead.
//It is however important to wait forRTC write cycle time before you initiate
//another read / write transaction
DEVICE_DELAY_US(RTC.WriteCycleTime_in_us);
while(1)
{
//Example 4:RTC word (16-bit) read
//Make sureRTC address 1 has 0x11 and 2 has 0x22
ControlAddr = 0x00;
RTC.pControlAddr = &ControlAddr;
RTC.pRX_MsgBuffer = RX_MsgBuffer;
RTC.NumOfDataBytes = 7;
status = I2C_ControllerReceiver(&RTC);
Get_Time();
//verifyRTCRead();
}
//Example 5:RTC Page write
//Program address = data pattern from address 64
/* for(i=0;i<MAX_BUFFER_SIZE;i++)
{
TX_MsgBuffer[i] = i+64;
}
ControlAddr = 64; //RTC address to write
RTC.NumOfDataBytes = MAX_BUFFER_SIZE;
RTC.pTX_MsgBuffer = TX_MsgBuffer;
status = I2C_ControllerTransmitter(&RTC);
//Wait forRTC write cycle time
//This delay is not mandatory. User can run their application code instead.
//It is however important to wait forRTC write cycle time before you initiate
//another read / write transaction
DEVICE_DELAY_US(RTC.WriteCycleTime_in_us);
//Example 6:RTC word Paged read
ControlAddr = 64;
RTC.pControlAddr = &ControlAddr;
RTC.pRX_MsgBuffer = RX_MsgBuffer;
RTC.NumOfDataBytes = MAX_BUFFER_SIZE;
status = I2C_ControllerReceiver(&RTC);
verifyRTCRead();
if(status)
{
fail();
}
else
{
pass();
}
if(status)
{
fail();
}
else
{
pass();
}
//////////////////
Set_Time(00, 03, 14, 5, 3, 1, 19);
while (1)
{
Get_Time();
}
}
//
// pass - Function to be called if data written matches data read
//
void
pass(void)
{
asm(" ESTOP0");
for(;;);
}
//
// fail - Function to be called if data written does NOT match data read
//
void fail(void)
{
asm(" ESTOP0");
for(;;);*/
}
void verifyRTCRead(void)
{
uint16_t i;
while(I2C_getStatus(RTC.base) & I2C_STS_BUS_BUSY);
for(i=0;i<RTC.NumOfDataBytes;i++)
{
if(RX_MsgBuffer[i] != TX_MsgBuffer[i])
{
//Transmitted data doesn't match received data
//Fail condition. PC shouldn't reach here
ESTOP0;
fail();
}
}
}
void I2C_GPIO_init(void)
{
// I2CA pins (SDAA / SCLA)
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SDAB, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(DEVICE_GPIO_PIN_SDAB, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SDAB, GPIO_QUAL_ASYNC);
GPIO_setDirectionMode(DEVICE_GPIO_PIN_SCLB, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(DEVICE_GPIO_PIN_SCLB, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(DEVICE_GPIO_PIN_SCLB, GPIO_QUAL_ASYNC);
GPIO_setPinConfig( DEVICE_GPIO_CFG_SDAB);
GPIO_setPinConfig(DEVICE_GPIO_CFG_SCLB );
}
void I2Cinit(void)
{
//myI2CA initialization
I2C_disableModule(I2CB_BASE);
I2C_initController(I2CB_BASE, DEVICE_SYSCLK_FREQ, 100000, I2C_DUTYCYCLE_50);
I2C_setConfig(I2CB_BASE, I2C_CONTROLLER_SEND_MODE);
I2C_setTargetAddress(I2CB_BASE, 0xD0);
I2C_setOwnAddress(I2CB_BASE, 96); //I2CA address
I2C_disableLoopback(I2CB_BASE);
I2C_setBitCount(I2CB_BASE, I2C_BITCOUNT_8);
I2C_setDataCount(I2CB_BASE, 2);
I2C_setAddressMode(I2CB_BASE, I2C_ADDR_MODE_7BITS);
I2C_enableFIFO(I2CB_BASE);
I2C_clearInterruptStatus(I2CB_BASE, I2C_INT_ARB_LOST | I2C_INT_NO_ACK);
I2C_setFIFOInterruptLevel(I2CB_BASE, I2C_FIFO_TXEMPTY, I2C_FIFO_RX2);
I2C_enableInterrupt(I2CB_BASE, I2C_INT_ADDR_TARGET | I2C_INT_ARB_LOST | I2C_INT_NO_ACK | I2C_INT_STOP_CONDITION);
I2C_setEmulationMode(I2CB_BASE, I2C_EMULATION_FREE_RUN);
I2C_enableModule(I2CB_BASE);
}
void Get_Time (void)
{
// uint8_t get_time[7];
//HAL_I2C_Mem_Read(&hi2c1, DS3231_ADDRESS, 0x00, 1, get_time, 7, 1000);
time.seconds = bcdToDec(RX_MsgBuffer[0]);
time.minutes = bcdToDec(RX_MsgBuffer[1]);
time.hour = bcdToDec(RX_MsgBuffer[2]);
time.dayofweek = bcdToDec(RX_MsgBuffer[3]);
time.dayofmonth = bcdToDec(RX_MsgBuffer[4]);
time.month = bcdToDec(RX_MsgBuffer[5]);
time.year = bcdToDec(RX_MsgBuffer[6]);
}
void Set_Time (uint8_t sec, uint8_t min, uint8_t hour, uint8_t dow, uint8_t dom, uint8_t month, uint8_t year)
{
set_time[0] = decToBcd(sec);
set_time[1] = decToBcd(min);
set_time[2] = decToBcd(hour);
set_time[3] = decToBcd(dow);
set_time[4] = decToBcd(dom);
set_time[5] = decToBcd(month);
set_time[6] = decToBcd(year);
//HAL_I2C_Mem_Write(&hi2c1, DS3231_ADDRESS, 0x00, 1, set_time, 7, 1000);
}
unsigned int decToBcd( unsigned int val)
{
return (uint8_t)( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
unsigned int bcdToDec(unsigned int val)
{
return (int)( (val/16*10) + (val%16) );
}
//
// End of File
//
i have implemented this code for DS3231RTC by using I2c protocol for TMS320F280025c Board by using EEPROM polling example code ....but i'm not getting output.....please check this code once...if thus code need any modifications please tell me....
Actually in this instruction (below) data is not going to slave
status = I2C_ControllerTransmitter(&RTC);
And im not getting data from below instruction also
status = I2C_ControllerReceiver(&RTC);
main problem is here Data is not senting to slave .That is main problem