2019-04-08 17:38:38 -03:00
|
|
|
#include "common.h"
|
2017-07-10 01:46:52 +02:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
#include "cache.h"
|
|
|
|
#include "i2c.h"
|
|
|
|
#include "pxi.h"
|
2017-07-10 01:46:52 +02:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
/*
|
|
|
|
disgusting hack that deserves to die in hell
|
|
|
|
ideally all buffers would be able to be accessed
|
|
|
|
by the ARM11, but those in ARM9 RAM are inaccessible
|
|
|
|
(.data, .rodata & .bss)
|
2017-07-10 01:46:52 +02:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
the current hack assumes all buffers in the heap are
|
|
|
|
located in FCRAM, which is accessible to both processors
|
|
|
|
but it's horrendous, and hopefully temporary
|
|
|
|
*/
|
2017-07-10 01:46:52 +02:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
static char *i2c_fcram_buf = NULL;
|
|
|
|
bool I2C_readRegBuf(I2cDevice devId, u8 regAddr, u8 *out, u32 size)
|
2017-11-24 02:48:56 +01:00
|
|
|
{
|
2019-04-08 17:38:38 -03:00
|
|
|
if (!i2c_fcram_buf)
|
|
|
|
i2c_fcram_buf = malloc(256);
|
2017-11-24 02:48:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
int ret;
|
|
|
|
u32 args[] = {devId, regAddr, (u32)i2c_fcram_buf, size};
|
2016-02-13 17:29:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
cpu_writeback_dc_range(i2c_fcram_buf, size);
|
|
|
|
cpu_membarrier();
|
2016-02-13 17:29:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
ret = PXI_DoCMD(PXI_I2C_READ, args, 4);
|
2017-11-24 02:48:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
cpu_invalidate_dc_range(i2c_fcram_buf, size);
|
|
|
|
memcpy(out, i2c_fcram_buf, size);
|
|
|
|
|
|
|
|
return ret;
|
2016-02-13 17:29:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
bool I2C_writeRegBuf(I2cDevice devId, u8 regAddr, const u8 *in, u32 size)
|
2017-07-10 01:46:52 +02:00
|
|
|
{
|
2019-04-08 17:38:38 -03:00
|
|
|
if (!i2c_fcram_buf)
|
|
|
|
i2c_fcram_buf = malloc(256);
|
2016-02-13 17:29:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
int ret;
|
|
|
|
u32 args[] = {devId, regAddr, (u32)i2c_fcram_buf, size};
|
2016-02-13 17:29:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
memcpy(i2c_fcram_buf, in, size);
|
|
|
|
cpu_writeback_dc_range(i2c_fcram_buf, size);
|
|
|
|
cpu_membarrier();
|
2016-02-13 17:29:56 +01:00
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
ret = PXI_DoCMD(PXI_I2C_WRITE, args, 4);
|
|
|
|
return ret;
|
2016-02-13 17:29:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
/*bool I2C_readRegBuf(I2cDevice devId, u8 regAddr, u8 *out, u32 size)
|
2017-07-10 01:46:52 +02:00
|
|
|
{
|
2019-04-08 17:38:38 -03:00
|
|
|
int ret;
|
|
|
|
u32 args[] = {devId, regAddr, (u32)out, size};
|
|
|
|
cpu_writeback_invalidate_dc_range(out, size);
|
|
|
|
cpu_membarrier();
|
|
|
|
ret = PXI_DoCMD(PXI_I2C_READ, args, 4);
|
|
|
|
cpu_invalidate_dc_range(out, size);
|
|
|
|
return ret;
|
|
|
|
}*/
|
2017-11-24 02:48:56 +01:00
|
|
|
|
|
|
|
u8 I2C_readReg(I2cDevice devId, u8 regAddr)
|
|
|
|
{
|
|
|
|
u8 data;
|
2019-04-08 17:38:38 -03:00
|
|
|
if (!I2C_readRegBuf(devId, regAddr, &data, 1))
|
|
|
|
data = 0xFF;
|
2017-11-24 02:48:56 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:38:38 -03:00
|
|
|
/*bool I2C_writeRegBuf(I2cDevice devId, u8 regAddr, const u8 *in, u32 size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 args[] = {devId, regAddr, (u32)in, size};
|
|
|
|
cpu_writeback_dc_range(in, size);
|
|
|
|
cpu_membarrier();
|
|
|
|
ret = PXI_DoCMD(PXI_I2C_WRITE, args, 4);
|
|
|
|
return ret;
|
|
|
|
}*/
|
|
|
|
|
2017-11-24 02:48:56 +01:00
|
|
|
bool I2C_writeReg(I2cDevice devId, u8 regAddr, u8 data)
|
|
|
|
{
|
|
|
|
return I2C_writeRegBuf(devId, regAddr, &data, 1);
|
|
|
|
}
|