fixed some (minor) warnings
This commit is contained in:
@@ -12,18 +12,19 @@
|
|||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
char * buf;
|
{
|
||||||
int head;
|
char * buf;
|
||||||
int tail;
|
int head;
|
||||||
int size;
|
int tail;
|
||||||
|
int size;
|
||||||
} fifo_t;
|
} fifo_t;
|
||||||
|
|
||||||
void fifo_init(fifo_t*,char*,int);
|
void fifo_init(fifo_t*, char*, int);
|
||||||
void fifo_advance(fifo_t*,int*);
|
void fifo_advance(fifo_t*, int*);
|
||||||
|
|
||||||
uint8_t fifo_get(fifo_t*);
|
uint8_t fifo_get(fifo_t*);
|
||||||
void fifo_put(fifo_t*,uint8_t);
|
void fifo_put(fifo_t*, uint8_t);
|
||||||
|
|
||||||
void fifo_clear(fifo_t*);
|
void fifo_clear(fifo_t*);
|
||||||
|
|
||||||
@@ -33,7 +34,7 @@ int fifo_unused(fifo_t*);
|
|||||||
int fifo_full(fifo_t*);
|
int fifo_full(fifo_t*);
|
||||||
int fifo_empty(fifo_t*);
|
int fifo_empty(fifo_t*);
|
||||||
|
|
||||||
int fifo_read(fifo_t*,char *,int);
|
int fifo_read(fifo_t*, unsigned char *, int);
|
||||||
int fifo_write(fifo_t*,const char*,int);
|
int fifo_write(fifo_t*, const unsigned char*, int);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ void fifo_advance(fifo_t *f, int *ix)
|
|||||||
|
|
||||||
//This reads at most nbytes bytes from the FIFO
|
//This reads at most nbytes bytes from the FIFO
|
||||||
//The number of bytes read is returned
|
//The number of bytes read is returned
|
||||||
int fifo_read(fifo_t *f, char *buf, int nbytes){
|
int fifo_read(fifo_t *f, unsigned char *buf, int nbytes){
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
while( n < nbytes && !fifo_empty(f) )
|
while( n < nbytes && !fifo_empty(f) )
|
||||||
@@ -95,7 +95,7 @@ int fifo_read(fifo_t *f, char *buf, int nbytes){
|
|||||||
//This writes up to nbytes bytes to the FIFO
|
//This writes up to nbytes bytes to the FIFO
|
||||||
//If the head runs in to the tail, not all bytes are written
|
//If the head runs in to the tail, not all bytes are written
|
||||||
//The number of bytes written is returned
|
//The number of bytes written is returned
|
||||||
int fifo_write(fifo_t *f, const char *buf, int nbytes){
|
int fifo_write(fifo_t *f, const unsigned char *buf, int nbytes){
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
while( n < nbytes && !fifo_full(f) )
|
while( n < nbytes && !fifo_full(f) )
|
||||||
|
|||||||
@@ -8,13 +8,14 @@
|
|||||||
|
|
||||||
#include <fifo.h>
|
#include <fifo.h>
|
||||||
|
|
||||||
static struct {
|
static struct
|
||||||
uint8_t state;
|
{
|
||||||
long delay;
|
uint8_t state;
|
||||||
uint16_t len;
|
long delay;
|
||||||
fifo_t fifo;
|
uint16_t len;
|
||||||
unsigned char *_buf;
|
fifo_t fifo;
|
||||||
uint16_t device;
|
unsigned char *_buf;
|
||||||
|
uint16_t device;
|
||||||
} i2c_param;
|
} i2c_param;
|
||||||
|
|
||||||
char local_buf[BUFSIZ];
|
char local_buf[BUFSIZ];
|
||||||
@@ -30,156 +31,174 @@ char local_buf[BUFSIZ];
|
|||||||
*/
|
*/
|
||||||
void I2C_Init()
|
void I2C_Init()
|
||||||
{
|
{
|
||||||
i2c_param.fifo.buf = local_buf;
|
i2c_param.fifo.buf = local_buf;
|
||||||
i2c_param.fifo.size = BUFSIZ;
|
i2c_param.fifo.size = BUFSIZ;
|
||||||
i2c_param.delay = 133*10L; // We can safely ignore this
|
i2c_param.delay = 133 * 10L; // We can safely ignore this
|
||||||
i2c_param.len = 0;
|
i2c_param.len = 0;
|
||||||
I2C_ioctl(0,0);
|
I2C_ioctl(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __attribute__ ((interrupt)) I2C_InterruptHandler(void)
|
void __attribute__ ((interrupt)) I2C_InterruptHandler(void)
|
||||||
{
|
{
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
clear_bit(MCF_I2C_I2SR,MCF_I2C_I2SR_IIF);
|
clear_bit(MCF_I2C_I2SR, MCF_I2C_I2SR_IIF);
|
||||||
if( MCF_I2C_I2CR & MCF_I2C_I2CR_MSTA )
|
if (MCF_I2C_I2CR & MCF_I2C_I2CR_MSTA)
|
||||||
{ // Masters of the known universe
|
{ // Masters of the known universe
|
||||||
if( MCF_I2C_I2SR & MCF_I2C_I2SR_ICF )
|
if (MCF_I2C_I2SR & MCF_I2C_I2SR_ICF)
|
||||||
{
|
{
|
||||||
switch( i2c_param.state )
|
switch (i2c_param.state)
|
||||||
{
|
{
|
||||||
case I2C_MXRX:
|
case I2C_MXRX:
|
||||||
if( fifo_used(&i2c_param.fifo)+1 == i2c_param.len ) {
|
if (fifo_used(&i2c_param.fifo) + 1 == i2c_param.len)
|
||||||
clear_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_MSTA);
|
{
|
||||||
i2c_param.state = I2C_READY;
|
clear_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_MSTA);
|
||||||
} else if( fifo_used(&i2c_param.fifo)+2 == i2c_param.len ) {
|
i2c_param.state = I2C_READY;
|
||||||
set_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_TXAK);
|
}
|
||||||
i2c_param.state = I2C_READY;
|
else if (fifo_used(&i2c_param.fifo) + 2 == i2c_param.len)
|
||||||
}
|
{
|
||||||
fifo_put(&i2c_param.fifo,MCF_I2C_I2DR);
|
set_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_TXAK);
|
||||||
break;
|
i2c_param.state = I2C_READY;
|
||||||
case I2C_ADDR:
|
}
|
||||||
if( fifo_empty(&i2c_param.fifo) )
|
fifo_put(&i2c_param.fifo, MCF_I2C_I2DR );
|
||||||
{
|
break;
|
||||||
i2c_param.state = I2C_MXRX;
|
case I2C_ADDR:
|
||||||
clear_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_MTX); // Receive mode
|
if (fifo_empty(&i2c_param.fifo))
|
||||||
set_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_RSTA);
|
{
|
||||||
ch = MCF_I2C_I2DR; // Dummy read
|
i2c_param.state = I2C_MXRX;
|
||||||
break;
|
clear_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_MTX); // Receive mode
|
||||||
}
|
set_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_RSTA);
|
||||||
case I2C_MXTX:
|
ch = MCF_I2C_I2DR; // Dummy read
|
||||||
if( fifo_empty(&i2c_param.fifo) || (MCF_I2C_I2SR&MCF_I2C_I2SR_RXAK) ) {
|
break;
|
||||||
clear_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_MSTA);
|
}
|
||||||
i2c_param.state = I2C_READY;
|
case I2C_MXTX:
|
||||||
} else
|
if (fifo_empty(&i2c_param.fifo)
|
||||||
MCF_I2C_I2DR = fifo_get(&i2c_param.fifo);
|
|| (MCF_I2C_I2SR & MCF_I2C_I2SR_RXAK))
|
||||||
break;
|
{
|
||||||
}
|
clear_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_MSTA);
|
||||||
}
|
i2c_param.state = I2C_READY;
|
||||||
} else { // Slave mode.
|
}
|
||||||
int set;
|
else
|
||||||
|
MCF_I2C_I2DR = fifo_get(&i2c_param.fifo);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // Slave mode.
|
||||||
|
int set;
|
||||||
|
|
||||||
if( set = (MCF_I2C_I2SR & MCF_I2C_I2SR_IAL) )
|
if ((set = (MCF_I2C_I2SR & MCF_I2C_I2SR_IAL)))
|
||||||
clear_bit(MCF_I2C_I2SR,MCF_I2C_I2SR_IAL);
|
clear_bit(MCF_I2C_I2SR, MCF_I2C_I2SR_IAL);
|
||||||
if( MCF_I2C_I2SR & MCF_I2C_I2SR_IAAS )
|
if (MCF_I2C_I2SR & MCF_I2C_I2SR_IAAS)
|
||||||
{
|
{
|
||||||
if( MCF_I2C_I2SR & MCF_I2C_I2SR_SRW )
|
if (MCF_I2C_I2SR & MCF_I2C_I2SR_SRW)
|
||||||
{
|
{
|
||||||
set_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_MTX);
|
set_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_MTX);
|
||||||
fifo_put(&i2c_param.fifo,MCF_I2C_I2DR);
|
fifo_put(&i2c_param.fifo, MCF_I2C_I2DR );
|
||||||
} else {
|
}
|
||||||
clear_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_MTX);
|
else
|
||||||
ch = MCF_I2C_I2DR;
|
{
|
||||||
}
|
clear_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_MTX);
|
||||||
} else if( !set ) {
|
ch = MCF_I2C_I2DR;
|
||||||
if( MCF_I2C_I2CR&MCF_I2C_I2CR_MTX )
|
}
|
||||||
{
|
}
|
||||||
if( MCF_I2C_I2SR&MCF_I2C_I2SR_RXAK )
|
else if (!set)
|
||||||
MCF_I2C_I2DR = fifo_get(&i2c_param.fifo);
|
{
|
||||||
else {
|
if (MCF_I2C_I2CR & MCF_I2C_I2CR_MTX)
|
||||||
clear_bit(MCF_I2C_I2CR,MCF_I2C_I2CR_MTX);
|
{
|
||||||
ch = MCF_I2C_I2DR;
|
if (MCF_I2C_I2SR & MCF_I2C_I2SR_RXAK)
|
||||||
}
|
MCF_I2C_I2DR = fifo_get(&i2c_param.fifo);
|
||||||
} else {
|
else
|
||||||
fifo_put(&i2c_param.fifo,MCF_I2C_I2DR);
|
{
|
||||||
}
|
clear_bit(MCF_I2C_I2CR, MCF_I2C_I2CR_MTX);
|
||||||
}
|
ch = MCF_I2C_I2DR;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fifo_put(&i2c_param.fifo, MCF_I2C_I2DR );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2C_send(unsigned short device, unsigned char *buf, unsigned short len)
|
void I2C_send(unsigned short device, unsigned char *buf, unsigned short len)
|
||||||
{
|
{
|
||||||
if( len > i2c_param.fifo.size )
|
if (len > i2c_param.fifo.size)
|
||||||
return;
|
return;
|
||||||
i2c_param.len = len;
|
i2c_param.len = len;
|
||||||
fifo_clear(&i2c_param.fifo);
|
fifo_clear(&i2c_param.fifo);
|
||||||
if( device > 127 ) // Use I2C 10 bit address
|
if (device > 127) // Use I2C 10 bit address
|
||||||
{
|
{
|
||||||
fifo_put(FIFO,0b11110|((device>>5)&6)|I2C_WRITE);
|
fifo_put(FIFO, 0b11110 | ((device >> 5) & 6) | I2C_WRITE);
|
||||||
fifo_put(FIFO,device&255);
|
fifo_put(FIFO, device & 255);
|
||||||
} else
|
}
|
||||||
fifo_put(FIFO,((device<<1)&0xFE)|I2C_WRITE);
|
else
|
||||||
fifo_write(FIFO,buf,len);
|
fifo_put(FIFO, ((device << 1) & 0xFE) | I2C_WRITE);
|
||||||
i2c_param.state = I2C_MXTX;
|
fifo_write(FIFO, buf, len);
|
||||||
|
i2c_param.state = I2C_MXTX;
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2C_receive(unsigned short device, unsigned char *buf, unsigned short len)
|
void I2C_receive(unsigned short device, unsigned char *buf, unsigned short len)
|
||||||
{
|
{
|
||||||
if( len > i2c_param.fifo.size )
|
if (len > i2c_param.fifo.size)
|
||||||
return;
|
return;
|
||||||
i2c_param.len = len;
|
i2c_param.len = len;
|
||||||
fifo_clear(FIFO);
|
fifo_clear(FIFO);
|
||||||
if( device > 127 ) // Use I2C 10 bit address
|
if (device > 127) // Use I2C 10 bit address
|
||||||
{
|
{
|
||||||
fifo_put(FIFO,0b11110|((device>>5)&6)|I2C_READ);
|
fifo_put(FIFO, 0b11110 | ((device >> 5) & 6) | I2C_READ);
|
||||||
fifo_put(FIFO,device&255);
|
fifo_put(FIFO, device & 255);
|
||||||
} else
|
}
|
||||||
fifo_put(FIFO,((device<<1)&0xFE)|I2C_READ);
|
else
|
||||||
i2c_param.state = I2C_ADDR;
|
fifo_put(FIFO, ((device << 1) & 0xFE) | I2C_READ);
|
||||||
|
i2c_param.state = I2C_ADDR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int I2C_ioctl(unsigned int index, unsigned long val)
|
int I2C_ioctl(unsigned int index, unsigned long val)
|
||||||
{
|
{
|
||||||
unsigned short len = val;
|
unsigned short len = val;
|
||||||
uint8_t temp;
|
uint8_t temp;
|
||||||
|
|
||||||
// Set device as slave or
|
// Set device as slave or
|
||||||
switch( index )
|
switch (index)
|
||||||
{
|
{
|
||||||
case I2CTLINIT: // make me master
|
case I2CTLINIT: // make me master
|
||||||
/* set the frequency near 400KHz */
|
/* set the frequency near 400KHz */
|
||||||
MCF_I2C_I2FDR = MCF_I2C_I2FDR_IC(0x10);
|
MCF_I2C_I2FDR = MCF_I2C_I2FDR_IC(0x10);
|
||||||
/* start the module */
|
/* start the module */
|
||||||
MCF_I2C_I2CR = MCF_I2C_I2CR_IIEN | MCF_I2C_I2CR_IEN;
|
MCF_I2C_I2CR = MCF_I2C_I2CR_IIEN | MCF_I2C_I2CR_IEN;
|
||||||
/* if bit busy set, send a stop condition to slave module */
|
/* if bit busy set, send a stop condition to slave module */
|
||||||
if(MCF_I2C_I2SR & MCF_I2C_I2SR_IBB)
|
if (MCF_I2C_I2SR & MCF_I2C_I2SR_IBB)
|
||||||
{
|
{
|
||||||
MCF_I2C_I2CR = 0; /* clear control register */
|
MCF_I2C_I2CR = 0; /* clear control register */
|
||||||
MCF_I2C_I2CR = MCF_I2C_I2CR_IIEN|MCF_I2C_I2CR_IEN|MCF_I2C_I2CR_MSTA; /* enable module & send a START condition */
|
MCF_I2C_I2CR = MCF_I2C_I2CR_IIEN | MCF_I2C_I2CR_IEN
|
||||||
temp = MCF_I2C_I2DR; /* dummy read */
|
| MCF_I2C_I2CR_MSTA; /* enable module & send a START condition */
|
||||||
MCF_I2C_I2SR = 0; /* clear status register */
|
temp = MCF_I2C_I2DR; /* dummy read */
|
||||||
MCF_I2C_I2CR = 0; /* clear control register */
|
MCF_I2C_I2SR = 0; /* clear status register */
|
||||||
MCF_I2C_I2CR = MCF_I2C_I2CR_IIEN|MCF_I2C_I2CR_IEN; /* enable the module again */
|
MCF_I2C_I2CR = 0; /* clear control register */
|
||||||
}
|
MCF_I2C_I2CR = MCF_I2C_I2CR_IIEN | MCF_I2C_I2CR_IEN; /* enable the module again */
|
||||||
i2c_param.state = I2C_READY;
|
}
|
||||||
i2c_param._buf = 0;
|
i2c_param.state = I2C_READY;
|
||||||
break;
|
i2c_param._buf = 0;
|
||||||
case I2CTLSBUF:
|
break;
|
||||||
i2c_param._buf = (char *)val;
|
case I2CTLSBUF:
|
||||||
break;
|
i2c_param._buf = (unsigned char *) val;
|
||||||
case I2CTLDEV:
|
break;
|
||||||
i2c_param.device = val;
|
case I2CTLDEV:
|
||||||
break;
|
i2c_param.device = val;
|
||||||
case I2CTLREAD:
|
break;
|
||||||
if( i2c_param._buf == 0 || i2c_param.device == 0 )
|
case I2CTLREAD:
|
||||||
return -1;
|
if (i2c_param._buf == 0 || i2c_param.device == 0)
|
||||||
I2C_receive(i2c_param.device,i2c_param._buf,len);
|
return -1;
|
||||||
break;
|
I2C_receive(i2c_param.device, i2c_param._buf, len);
|
||||||
case I2CTLWRITE:
|
break;
|
||||||
if( i2c_param._buf == 0 || i2c_param.device == 0 )
|
case I2CTLWRITE:
|
||||||
return -1;
|
if (i2c_param._buf == 0 || i2c_param.device == 0)
|
||||||
I2C_send(i2c_param.device,i2c_param._buf,len);
|
return -1;
|
||||||
break;
|
I2C_send(i2c_param.device, i2c_param._buf, len);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user