Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rev2b eeprom #125

Merged
merged 8 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions common/i2c_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// This array helps us simplify the use of different I2C devices in the board.
extern uint32_t I2C_BASE[];

void initI2C0(const uint32_t sysclockfreq); // EEProm and I2C slave for master.
void initI2C1(const uint32_t sysclockfreq); // power supply I2C bus
void initI2C2(const uint32_t sysclockfreq); // clocks I2C bus
void initI2C0(const uint32_t sysclockfreq); // EEPROM(Rev1) + I2C slave for master
void initI2C1(const uint32_t sysclockfreq); // power supply I2C bus
void initI2C2(const uint32_t sysclockfreq); // EEPROM(Rev2) + clocks I2C bus
void initI2C3(const uint32_t sysclockfreq); // F2 optics I2C bus
void initI2C4(const uint32_t sysclockfreq); // F1 optics I2C bus
void initI2C5(const uint32_t sysclockfreq); // FPGA I2C bus (Rev2)
Expand Down
4 changes: 2 additions & 2 deletions projects/cm_mcu/CommandLineTask.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,11 @@ static struct command_t commands[] = {
{"i2cr", i2c_ctl_r,
"args: <dev> <address> <number of bytes>\r\nRead I2C controller. Addr in hex.\r\n", 3},
{"i2crr", i2c_ctl_reg_r,
"i2crr <dev> <address> <reg> <number of bytes>\r\n Read I2C controller. Addr in hex\r\n", 4},
"i2crr <dev> <address> <number of reg bytes> <reg> <number of bytes>\r\n Read I2C controller. Addr in hex\r\n", 6},
{"i2cw", i2c_ctl_w, "i2cw <dev> <address> <number of bytes> <value>\r\n Write I2C controller.\r\n",
4},
{"i2cwr", i2c_ctl_reg_w,
"args: <dev> <address> <reg> <number of bytes>\r\nWrite I2C controller.\r\n", 5},
"args: <dev> <address> <number of reg bytes> <reg> <number of bytes>\r\nWrite I2C controller.\r\n", 6},
{
"i2c_scan",
i2c_scan,
Expand Down
18 changes: 9 additions & 9 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, uint16_t packed_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, packed_reg_addr, size, *((uint32_t*)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, reg, size, (uint32_t)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 @@ -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, (uint16_t)FF_TEMP_COMMAND_REG, 2, *((uint32_t*)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, (uint16_t)FF_STATUS_COMMAND_REG, 2, *((uint32_t*)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, (uint16_t)i, 1, *((uint32_t*)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, (uint16_t)los_regs[reg_i], 1, *((uint32_t*)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, (uint16_t)cdr_lol_regs[reg_i], 1, *((uint32_t*)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, (uint16_t)i, 1, *((uint32_t*)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
41 changes: 24 additions & 17 deletions projects/cm_mcu/I2CCommunication.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ SemaphoreHandle_t (*getSemaphore[7])(void) = {
NULL,
};

#define MAX_BYTES_ADDR 2
#define MAX_BYTES 4
int apollo_i2c_ctl_r(uint8_t device, uint8_t address, uint8_t nbytes, uint8_t data[MAX_BYTES])
{
Expand Down Expand Up @@ -107,17 +108,20 @@ 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, uint16_t packed_reg_address, uint8_t nbytes, uint32_t packed_data)
{
tSMBus* smbus = pSMBus[device];
tSMBusStatus* p_status = eStatus[device];

configASSERT(smbus != NULL);

memset(data, 0, nbytes * sizeof(data[0]));
if (nbytes > MAX_BYTES)
nbytes = MAX_BYTES;

uint8_t reg_address[MAX_BYTES_ADDR];
for (int i = 0; i < nbytes_addr; ++i) {
reg_address[i] = (packed_reg_address >> i * 8) & 0xFF;
}
uint8_t data[MAX_BYTES];
for (int i = 0; i < MAX_BYTES; ++i) {
data[i] = (packed_data >> i * 8) & 0xFF;
}
// get the semaphore
SemaphoreHandle_t s = NULL;
if ( getSemaphore[device] != NULL ) {
Expand All @@ -126,7 +130,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 +147,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, uint16_t packed_reg_address, uint8_t nbytes, uint32_t 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 clock chips) or two bytes (if write to EEPROM) is the register, others are the data
uint8_t data[MAX_BYTES_ADDR + MAX_BYTES];
for (int i = 0; i < nbytes_addr; ++i){
data[i] = (packed_reg_address >> (nbytes_addr - 1 - i) * 8) & 0xFF; // the first byte is high byte in EEPROM's two-byte reg address
}
nbytes += nbytes_addr;
// 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 = nbytes_addr; i < MAX_BYTES + nbytes_addr; ++i) {
data[i] = (packed_data >> (i - nbytes_addr) * 8) & 0xFF;
}
nbytes++; // to account for the register address
if (nbytes > MAX_BYTES)
nbytes = MAX_BYTES;

if (nbytes > MAX_BYTES+nbytes_addr)
nbytes = MAX_BYTES+nbytes_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, uint16_t packed_reg_address, uint8_t nbytes, uint32_t packed_data);
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, uint16_t packed_reg_address, uint8_t nbytes, uint32_t 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