Instant SoC Class Library  3.1
FC_IO_I2C Class Reference

I2C Master. More...

Inheritance diagram for FC_IO_I2C:
FC_Base

Public Member Functions

 FC_IO_I2C (int bitrate=400000, int fifo_depth=16)
 
int WaitReady ()
 
bool IsReady ()
 
bool IsAddressAck ()
 
bool StartWrite (U32 addr, const U8 *pD, int nD)
 
bool StartWrite (U32 addr, U8 d)
 
bool ContinueWrite (U32 addr, const U8 *pD, int nD)
 
bool StartRead (U32 addr, int nD)
 
bool ContinueRead (U32 addr, int nD)
 
bool GetReadData (U8 *pD, int nD)
 
U8 GetNext ()
 
U32 GetNumValid ()
 
U32 GetNumReady ()
 
void ResetReadFIFO ()
 

Detailed Description

I2C Master.

Adds to interface:
<name>_SCL : inout std_logic;
<name>_SDA : inout std_logic
where <name> is c++ object name.
Any number of instances are allowed.

// This example show how to use FC_IO_I2C class to read data from an I2C device.
// The I2C device in this example is ADT7420. First it reads the the device id.
// Then it reads the temperature every second. The id and temperature with 1 decimal
// is printed on an UART. The temperature in Celsius.
#include <stdlib.h>
#include "fc_io.h"
#include "fc_system.h"
// Function to stream an int
template<class T>
T& operator<<(T& os, int v)
{
char b[16];
itoa(v,b,10);
os << b;
return os;
}
int main(void)
{
//% hw_begin
FC_IO_Clk Clk(100);
FC_IO_UART_TX uart_tx(115200,64);
FC_IO_I2C temp_sens(400000); // 400 kbps
//% hw_end
// Read ADT7420 ID
// The ADT7420 has I2C address 0x4B and id reg is 0x0b
temp_sens.StartWrite(0x4B,0x0B);
temp_sens.ContinueRead(0x4B,1); // Repeat start
temp_sens.WaitReady();
if( temp_sens.IsAddressAck())
uart_tx << "ADT7420 Id:" << int(temp_sens.GetNext()) << "\r\n";
else
uart_tx << "0x4B Addr not ok\r\n";
for(;;)
{
timer.Sleep(1000, TU_ms);
// Read temperature
temp_sens.StartWrite(0x4B,0x00);
temp_sens.ContinueRead(0x4B,2);
temp_sens.WaitReady();
int t = (temp_sens.GetNext() << 8); // MSB read first
t |= temp_sens.GetNext();
t *= 10; // Make t in 0.1 deg
t >>= (3+4); // Default 13 bit mode
uart_tx << "Temp: " << t/10 << "." << t%10 << "\r\n";
}
}
FC_IO_Clk
System clock input.
Definition: fc_io.h:55
FC_IO_UART_TX
UART Transmitter.
Definition: fc_io.h:348
FC_System_Timer
Timer.
Definition: fc_system.h:78
FC_IO_I2C
I2C Master.
Definition: fc_io.h:561