Skip to content

Commit

Permalink
Fix FRU reading / writing operations
Browse files Browse the repository at this point in the history
Add a non zero timeout value so FRU read operations fail less often,
fix FRU writing (at24mac) function to wait until the EEPROM is not
busy before proceeding to the next block write.
  • Loading branch information
gustavosr8 committed Jan 29, 2024
1 parent 7a4d221 commit 7b323e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
28 changes: 22 additions & 6 deletions modules/at24mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ size_t at24mac_read( uint8_t id, uint16_t address, uint8_t *rx_data, size_t buf_
uint8_t i2c_interface;
uint8_t rx_len = 0;

if (rx_data == NULL) {
return 0;
}

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) && ( rx_data != NULL ) ) {
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr, address, rx_data, buf_len );
i2c_give( i2c_interface );
Expand Down Expand Up @@ -88,11 +92,12 @@ size_t at24mac_write( uint8_t id, uint16_t address, uint8_t *tx_data, size_t buf
uint8_t i2c_interface;
uint8_t bytes_to_write;
uint8_t curr_addr;
uint8_t i2c_written = 0;
uint8_t page_buf[17];

size_t tx_len = 0;

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) && ( tx_data != NULL ) ) {
if (tx_data != NULL) {
curr_addr = address;

while (tx_len < buf_len) {
Expand All @@ -104,12 +109,23 @@ size_t at24mac_write( uint8_t id, uint16_t address, uint8_t *tx_data, size_t buf
page_buf[0] = curr_addr;
memcpy(&page_buf[1], tx_data+tx_len, bytes_to_write);

/* Write the data */
tx_len += xI2CMasterWrite( i2c_interface, i2c_addr, &page_buf[0] , bytes_to_write+1 );
tx_len -= 1; /* Remove byte address from data written size */
curr_addr += bytes_to_write;
if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout )) {
/* Write the data */
i2c_written = xI2CMasterWrite( i2c_interface, i2c_addr, &page_buf[0] , bytes_to_write+1 );
/* When trying to write, the EEPROM will reply with NACKs if it's busy.
* If the i2c receives a NACK when trying to write, the function returns only
* the number of bytes successfully written, and we should increment only this value
* in tx_len and in curr_addr */

if (i2c_written) {
tx_len += i2c_written - 1; /* Remove byte address from data written size */
curr_addr += i2c_written - 1;
i2c_written = 0;
}
i2c_give( i2c_interface );
}
vTaskDelay(pdMS_To_TICKS(1)); /* Avoid too much unnecessary I2C trafic*/
}
i2c_give( i2c_interface );
}

return tx_len;
Expand Down
18 changes: 9 additions & 9 deletions modules/fru.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ uint8_t fru_check_integrity( uint8_t id, size_t *fru_size )
if (fru[id].runtime) {
memcpy( &common_header[0], &fru[id].buffer[0], 8);
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, 0x00, &common_header[0], 8, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, 0x00, &common_header[0], 8, 10 );
}

printf("[FRU][%s] Asserting FRU information integrity\n", id == FRU_AMC ? "AMC" : "RTM");
Expand All @@ -91,14 +91,14 @@ uint8_t fru_check_integrity( uint8_t id, size_t *fru_size )
if (fru[id].runtime) {
rec_len = fru[id].buffer[chassis_off+1];
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, chassis_off+1, &rec_len, 1, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, chassis_off+1, &rec_len, 1, 10 );
}
rec_len *= 8;
if (rec_len > 0) {
if (fru[id].runtime) {
memcpy( &rec_buff[0], &fru[id].buffer[chassis_off], rec_len);
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, chassis_off, &rec_buff[0], rec_len, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, chassis_off, &rec_buff[0], rec_len, 10 );
}
if ( !((calculate_chksum( &rec_buff[0], rec_len ) == 0) && rec_buff[0] == 1) ) {
/* Wrong checksum */
Expand All @@ -116,15 +116,15 @@ uint8_t fru_check_integrity( uint8_t id, size_t *fru_size )
if (fru[id].runtime) {
rec_len = fru[id].buffer[board_off+1];
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, board_off+1, &rec_len, 1, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, board_off+1, &rec_len, 1, 10 );
}
rec_len *= 8;

if (rec_len > 0) {
if (fru[id].runtime) {
memcpy( &rec_buff[0], &fru[id].buffer[board_off], rec_len);
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, board_off, &rec_buff[0], rec_len, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, board_off, &rec_buff[0], rec_len, 10 );
}
if ( !((calculate_chksum( &rec_buff[0], rec_len ) == 0) && rec_buff[0] == 1) ) {
/* Wrong checksum */
Expand All @@ -142,15 +142,15 @@ uint8_t fru_check_integrity( uint8_t id, size_t *fru_size )
if (fru[id].runtime) {
rec_len = fru[id].buffer[product_off+1];
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, product_off+1, &rec_len, 1, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, product_off+1, &rec_len, 1, 10 );
}
rec_len *= 8;

if (rec_len > 0) {
if (fru[id].runtime) {
memcpy( &rec_buff[0], &fru[id].buffer[product_off], rec_len);
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, product_off, &rec_buff[0], rec_len, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, product_off, &rec_buff[0], rec_len, 10 );
}
if ( !((calculate_chksum( &rec_buff[0], rec_len ) == 0 ) && rec_buff[0] == 1) ) {
/* Wrong checksum */
Expand All @@ -172,7 +172,7 @@ uint8_t fru_check_integrity( uint8_t id, size_t *fru_size )
if (fru[id].runtime) {
memcpy( &rec_buff[0], &fru[id].buffer[multirec_off], 5);
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, multirec_off, &rec_buff[0], 5, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, multirec_off, &rec_buff[0], 5, 10 );
}
/* Calculate Multirecord header checksum */
if ( !(calculate_chksum( &rec_buff[0], 5 ) == 0) ) {
Expand All @@ -190,7 +190,7 @@ uint8_t fru_check_integrity( uint8_t id, size_t *fru_size )
if (fru[id].runtime) {
memcpy( &rec_buff[0], &fru[id].buffer[multirec_off], rec_len );
} else {
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, multirec_off, &rec_buff[0], rec_len, 0 );
fru[id].cfg.read_f( fru[id].cfg.eeprom_id, multirec_off, &rec_buff[0], rec_len, 10 );
}
if ( !((calculate_chksum( &rec_buff[0], rec_len ) == rec_chksum)) ) {
/* Wrong checksum */
Expand Down

0 comments on commit 7b323e1

Please sign in to comment.