You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
1.7 KiB
105 lines
1.7 KiB
14 years ago
|
|
||
|
#include "i2c.h"
|
||
|
|
||
|
#define I2CBASE 0xE001C000
|
||
|
|
||
|
#define I2CONSET 0x00
|
||
|
#define I2STAT 0x04
|
||
|
#define I2DAT 0x08
|
||
|
#define I2ADR 0x0c
|
||
|
#define I2SCLH 0x10
|
||
|
#define I2SCLL 0x14
|
||
|
#define I2CONCLR 0x18
|
||
|
|
||
|
#define IREG(x) (((volatile unsigned char *)I2CBASE)[x])
|
||
|
#define IWREG(x) (((volatile unsigned int *)I2CBASE)[(x)/sizeof(unsigned int)])
|
||
|
|
||
|
#define AAFLAG (1<<2)
|
||
|
#define SIFLAG (1<<3)
|
||
|
#define STOFLAG (1<<4)
|
||
|
#define STAFLAG (1<<5)
|
||
|
|
||
|
#define SI (IREG(I2CONSET) & SIFLAG)
|
||
|
|
||
|
void init_i2c(void)
|
||
|
{
|
||
|
IREG(I2CONSET) = 0x40; /* Enable I2C ready for Master Tx */
|
||
|
/* Set up for just under 400kHz */
|
||
|
#if 0
|
||
|
IWREG(I2SCLL) = (25 * 100);
|
||
|
IWREG(I2SCLH) = (12 * 100);
|
||
|
#else
|
||
|
IWREG(I2SCLL) = 1475; /* ~5kHz */
|
||
|
IWREG(I2SCLH) = 1475;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
int i2cstat;
|
||
|
|
||
|
int i2c_wait(void)
|
||
|
{
|
||
|
int stat;
|
||
|
while (!SI);
|
||
|
stat = IREG(I2STAT);
|
||
|
i2cstat = stat;
|
||
|
return stat;
|
||
|
}
|
||
|
|
||
|
void i2c_go(void)
|
||
|
{
|
||
|
IREG(I2CONCLR) = SIFLAG;
|
||
|
}
|
||
|
|
||
|
int i2c_conreg(void)
|
||
|
{
|
||
|
return IREG(I2CONSET);
|
||
|
}
|
||
|
|
||
|
int i2c_statreg(void)
|
||
|
{
|
||
|
return IREG(I2STAT);
|
||
|
}
|
||
|
|
||
|
bool i2c_send_start(void)
|
||
|
{
|
||
|
IREG(I2CONCLR) = STOFLAG | STAFLAG;
|
||
|
IREG(I2CONSET) = STAFLAG;
|
||
|
/* i2c_go(); */
|
||
|
switch (i2c_wait()) {
|
||
|
case 0x08:
|
||
|
case 0x10:
|
||
|
/* IREG(I2CONCLR) = STAFLAG; */
|
||
|
return TRUE;
|
||
|
default:
|
||
|
/* IREG(I2CONCLR) = STAFLAG; */
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool i2c_send_address(int addr, bool write)
|
||
|
{
|
||
|
return i2c_send_data((addr<<1) + (write?0:1));
|
||
|
}
|
||
|
|
||
|
bool i2c_send_data(int data)
|
||
|
{
|
||
|
IREG(I2DAT) = data;
|
||
|
IREG(I2CONCLR) = STAFLAG | SIFLAG;
|
||
|
switch (i2c_wait()) {
|
||
|
case 0x18:
|
||
|
case 0x28:
|
||
|
return TRUE;
|
||
|
default:
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void i2c_send_stop(void)
|
||
|
{
|
||
|
IREG(I2CONCLR) = STAFLAG | AAFLAG;
|
||
|
IREG(I2CONSET) = STOFLAG;
|
||
|
i2c_go();
|
||
|
/* Don't think we need to wait here. Could be wrong. */
|
||
|
}
|
||
|
|