Skip to content

Commit

Permalink
Fix Issue #123 and the code complies
Browse files Browse the repository at this point in the history
  • Loading branch information
pkotamnives committed Feb 23, 2022
1 parent 4479f34 commit 2ad9e54
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Auto detect text files and perform LF normalization
# https://www.davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/
* text=auto
#* text=auto
*.yml text
.gitattributes export-ignore
.gitignore export-ignore
Expand Down
22 changes: 11 additions & 11 deletions projects/cm_mcu/FireFlyTask.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ bool isEnabledFF(int ff)
return true;
}

static int read_ff_register(const char *name, uint8_t reg_addr, uint8_t *value, size_t size)
static int read_ff_register(const char *name, uint8_t *reg_addr, uint8_t *value, size_t size)
{
memset(value, 0, size);
// find the appropriate information for this FF device
Expand Down Expand Up @@ -373,7 +373,7 @@ static int read_ff_register(const char *name, uint8_t reg_addr, uint8_t *value,

if (! res ) {
// Read from register.
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, reg_addr, size, value);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, reg_addr, size, value);

//r = SMBusMasterI2CWriteRead(smbus, ff_i2c_addrs[ff].dev_addr, &reg_addr, 1, value, size);
if (res != 0) {
Expand Down Expand Up @@ -426,7 +426,7 @@ static int write_ff_register(const char *name, uint8_t reg, uint16_t value, int
// write to register. First word is reg address, then the data.
// increment size to account for the register address
if ( ! res ) {
res = apollo_i2c_ctl_reg_w(i2c_device, ff_i2c_addrs[ff].dev_addr, reg, size, (int)value);
res = apollo_i2c_ctl_reg_w(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,reg}, size, (int)value);
if (res != 0) {
char tmp[64];
snprintf(tmp, 64, "%s: FF writing error %d (ff=%s) ...\r\n", __func__, res,
Expand Down Expand Up @@ -535,7 +535,7 @@ static uint16_t read_arbitrary_ff_register(uint16_t regnumber, int num_ff, uint8
if (num_ff >= NFIREFLIES) {
return -1;
}
int ret = read_ff_register(ff_i2c_addrs[num_ff].name, regnumber, value, 1);
int ret = read_ff_register(ff_i2c_addrs[num_ff].name, (uint8_t[]){0,regnumber}, value, 1);
return ret;
}

Expand Down Expand Up @@ -667,7 +667,7 @@ void FireFlyTask(void *parameters)
uint8_t regdata[CHARLENGTH];
memset(regdata, 'x', CHARLENGTH);
regdata[theSZ - 1] = '\0';
int ret = read_ff_register(ff_i2c_addrs[theFF].name, theReg, &regdata[0], theSZ);
int ret = read_ff_register(ff_i2c_addrs[theFF].name, (uint8_t[]){0,theReg}, &regdata[0], theSZ);
if (ret != 0) {
snprintf(tmp, CHARLENGTH, "read_ff_reg failed with %d\r\n", ret);
Print(tmp);
Expand Down Expand Up @@ -759,7 +759,7 @@ void FireFlyTask(void *parameters)

#define ERRSTR "FIF: %s: Error %d, break loop (ff=%d,c=%d) ...\r\n"
// Read the temperature
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, FF_TEMP_COMMAND_REG, 2, data);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,FF_TEMP_COMMAND_REG}, 2, data);
if (res != 0) {
snprintf(tmp, 64, ERRSTR, __func__, res, ff, 1);
Print(tmp);
Expand All @@ -775,7 +775,7 @@ void FireFlyTask(void *parameters)
#endif // DEBUG_FIF

// read the status register
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, FF_STATUS_COMMAND_REG, 2, data);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,FF_STATUS_COMMAND_REG}, 2, data);
if (res != 0) {
snprintf(tmp, 64, ERRSTR, __func__, res, ff, 1);
Print(tmp);
Expand All @@ -794,7 +794,7 @@ void FireFlyTask(void *parameters)
data[0] = 0x0U;
data[1] = 0x0U;
for (uint8_t i = 189; i < 205; i++) {// change from 171-185 to 189-198 or 189-204 or 196-211
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, &i, 1, data);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,i}, 1, data);
if (res == -1) {
snprintf(tmp, 64, "FIF: %s: SMBUS failed (master/bus busy, ps=%d,c=%d)\r\n", __func__, ff,
1);
Expand Down Expand Up @@ -830,7 +830,7 @@ void FireFlyTask(void *parameters)
// TODO: single multi-byte read rather than multiple reads
int reg_i=0;
while(reg_i<2 && los_regs[reg_i] != 0){
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, los_regs[reg_i], 1, data);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,los_regs[reg_i]}, 1, data);
if (res != 0) {
snprintf(tmp, 64, ERRSTR, __func__, res, ff, 3);
DPRINT(tmp);
Expand All @@ -856,7 +856,7 @@ void FireFlyTask(void *parameters)

reg_i=0;
while(reg_i<2 && cdr_lol_regs[reg_i] != 0){
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, cdr_lol_regs[reg_i], 1, data);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,cdr_lol_regs[reg_i]}, 1, data);
if (res != 0) {
snprintf(tmp, 64, ERRSTR, __func__, res, ff, 5);
DPRINT(tmp);
Expand All @@ -875,7 +875,7 @@ void FireFlyTask(void *parameters)
data[1] = 0x0U;

for (uint8_t i = 148; i < 164; i++) {
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, i, 1, data);
res = apollo_i2c_ctl_reg_r(i2c_device, ff_i2c_addrs[ff].dev_addr, 1, (uint8_t[]){0,i}, 1, data);
if (res != 0){
snprintf(tmp, 64, "FIF: %s: Error %d, break loop (ps=%d,c=%d) ...\r\n", __func__, *p_status,
ff, 1);
Expand Down
32 changes: 19 additions & 13 deletions projects/cm_mcu/I2CCommunication.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ SemaphoreHandle_t (*getSemaphore[7])(void) = {
};

#define MAX_BYTES 4
#define MAX_BYTES_ADDR 2
int apollo_i2c_ctl_r(uint8_t device, uint8_t address, uint8_t nbytes, uint8_t data[MAX_BYTES])
{
tSMBus* p_sMaster = pSMBus[device];
Expand Down Expand Up @@ -107,13 +108,15 @@ int apollo_i2c_ctl_r(uint8_t device, uint8_t address, uint8_t nbytes, uint8_t da
return retval;
}

int apollo_i2c_ctl_reg_r(uint8_t device, uint8_t address, uint8_t reg_address, uint8_t nbytes, uint8_t data[MAX_BYTES])
int apollo_i2c_ctl_reg_r(uint8_t device, uint8_t address, uint8_t nbytes_addr, uint8_t *reg_address, uint8_t nbytes, uint8_t data[MAX_BYTES])
{
tSMBus* smbus = pSMBus[device];
tSMBusStatus* p_status = eStatus[device];

configASSERT(smbus != NULL);

memset(reg_address, 0, nbytes_addr * sizeof(reg_address[0]));
if (nbytes_addr > MAX_BYTES_ADDR)
nbytes_addr = MAX_BYTES_ADDR;
memset(data, 0, nbytes * sizeof(data[0]));
if (nbytes > MAX_BYTES)
nbytes = MAX_BYTES;
Expand All @@ -126,7 +129,7 @@ int apollo_i2c_ctl_reg_r(uint8_t device, uint8_t address, uint8_t reg_address, u
}
int retval = 0;

tSMBusStatus r = SMBusMasterI2CWriteRead(smbus, address, &reg_address, 1, data, nbytes);
tSMBusStatus r = SMBusMasterI2CWriteRead(smbus, address, reg_address, nbytes_addr, data, nbytes);
if (r != SMBUS_OK) {
retval = -1;
}
Expand All @@ -143,24 +146,27 @@ int apollo_i2c_ctl_reg_r(uint8_t device, uint8_t address, uint8_t reg_address, u
return *p_status;
}

int apollo_i2c_ctl_reg_w(uint8_t device, uint8_t address, uint8_t reg_address, uint8_t nbytes, int packed_data)
int apollo_i2c_ctl_reg_w(uint8_t device, uint8_t address, uint8_t nbytes_addr, uint8_t *reg_address, uint8_t nbytes, int packed_data)
{
tSMBus* p_sMaster = pSMBus[device];
tSMBusStatus* p_eStatus = eStatus[device];

configASSERT(p_sMaster != NULL);

// first byte is the register, others are the data
uint8_t data[MAX_BYTES+1];
data[0] = reg_address;
// first byte (if write to one of five clcok chips) or two bytes (if write to EEPROM) is the register, others are the data
uint8_t data[MAX_BYTES+nbytes_addr];
for (int i = 0; i < MAX_BYTES_ADDR; ++i){
data[i] = reg_address[i];
if (data[i] != 0) ++nbytes; // to account for the register address
}
// pack the bytes into the data array, offset by
// one due to the address
for (int i = 1; i < MAX_BYTES + 1; ++i) {
data[i] = (packed_data >> (i - 1) * 8) & 0xFFUL;
// one or two due to the address
for (int i = MAX_BYTES_ADDR; i < MAX_BYTES + MAX_BYTES_ADDR; ++i) {
data[i] = (packed_data >> (i - MAX_BYTES_ADDR) * 8) & 0xFFUL;
}
nbytes++; // to account for the register address
if (nbytes > MAX_BYTES)
nbytes = MAX_BYTES;

if (nbytes > MAX_BYTES+MAX_BYTES_ADDR)
nbytes = MAX_BYTES+MAX_BYTES_ADDR;
// get the semaphore
SemaphoreHandle_t s = NULL;
if ( getSemaphore[device] != NULL ) {
Expand Down
4 changes: 2 additions & 2 deletions projects/cm_mcu/I2CCommunication.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
// Command line interface
//int apollo_i2c_ctl_set_dev(uint8_t base);
int apollo_i2c_ctl_r(uint8_t device, uint8_t address, uint8_t nbytes, uint8_t data[4]);
int apollo_i2c_ctl_reg_r(uint8_t device, uint8_t address, uint8_t reg_address, uint8_t nbytes, uint8_t data[4]);
int apollo_i2c_ctl_reg_r(uint8_t device, uint8_t address, uint8_t nbytes_addr, uint8_t *reg_address, uint8_t nbytes, uint8_t data[4]);
int apollo_i2c_ctl_w(uint8_t device, uint8_t address, uint8_t nbytes, int value);
int apollo_i2c_ctl_reg_w(uint8_t device, uint8_t address, uint8_t reg_address, uint8_t nbytes, int packed_data);
int apollo_i2c_ctl_reg_w(uint8_t device, uint8_t address, uint8_t nbytes_addr, uint8_t *reg_address, uint8_t nbytes, int packed_data);
int apollo_pmbus_rw(tSMBus *smbus, volatile tSMBusStatus *smbus_status, bool read,
struct dev_i2c_addr_t *add, struct pm_command_t *cmd, uint8_t *value);

Expand Down
Loading

0 comments on commit 2ad9e54

Please sign in to comment.