I’m trying to do an SPI multibyte transaction with CRC.
For I2C
- you sent the slave address -> register address -> first data -> first CRC -> second data -> second CRC -> third data -> third crc and so on
- first CRC is calculated using slave address, register address and first data
- second CRC is calculated using second data
- third CRC is calculated using third data
- and so on for fourth, fifth, … CRC
I tried doing the same way for SPI but it didn’t work.
From the image below, I tried setting IO_SPIMODE = 0, remove packet length from my code, and set AI = 1 and it still didn’t work.
SPI Write Transaction Example
SPI Table
- Change SPI_MODE = 0 and AI = 0 as Im doing multi byte write
- I write 0xFA, 0xEF, 0xCD, and 0x9F to reg address of 0x125, assuming SPI auto increment the reg address
- Change SPI_MODE = 1 and AI = 1 as Im doing single byte read
- I read from 0x125 and 0x126 reg address and it’s not right.
My code:
`uint8_t r_pmic_prot_spi_multiple_write(uint16_t w_addr, uint8_t w_data1, uint8_t w_data2, uint8_t w_data3, uint8_t w_data4) {
uint8_t crc_data1 = 0;
uint8_t crc_data2 = 0;
uint8_t crc_data3 = 0;
uint8_t crc_data4 = 0;
P4_BIT2_PSSL = 0;
g_spi_csi10_tx_done = 0;
status_write = 0x00;
crc_data = 0x00;
multi_write_data_crc[0] = 0x40 + (w_addr >> 8); // first byte
multi_write_data_crc[1] = (w_addr & 0xFF); // second byte
crc_data = r_calculate_i2c_crc(crc_data, multi_write_data_crc[0]);
crc_data = r_calculate_i2c_crc(crc_data, multi_write_data_crc[1]);
multi_write_data_crc[2] = w_data1; //first data
crc_data1 = r_calculate_i2c_crc(crc_data, multi_write_data_crc[2]);
multi_write_data_crc[3] = crc_data1; // first crc
crc_data = 0;
multi_write_data_crc[4] = w_data2; // second data
crc_data2 = r_calculate_i2c_crc(crc_data, multi_write_data_crc[4]);
multi_write_data_crc[5] = crc_data2; // second data
crc_data = 0;
multi_write_data_crc[6] = w_data3;
crc_data3 = r_calculate_i2c_crc(crc_data, multi_write_data_crc[6]);
multi_write_data_crc[7] = crc_data3;
crc_data = 0;
multi_write_data_crc[8] = w_data4;
crc_data4 = r_calculate_i2c_crc(crc_data, multi_write_data_crc[8]);
multi_write_data_crc[9] = crc_data4;
status_write = R_CSI10_Send_Receive(multi_write_data_crc, sizeof(multi_write_data_crc), multi_read_data_crc);
while (g_spi_csi10_tx_done == 0) {
;
}
P4_BIT2_PSSL = 1;
return status_write;
}
uint8_t r_pmic_prot_spi_read(uint16_t r_addr) {
P4_BIT2_PSSL = 0;
g_spi_csi10_tx_done = 0;
status_write = 0x00;
read_data_crc[3] = 0x00;
crc_data = 0x00;
write_data_crc[0] = 0x80 + (r_addr >> 8);
write_data_crc[1] = (r_addr & 0xFF);
write_data_crc[2] = 0x01;
write_data_crc[3] = 0x00;
write_data_crc[4] = 0x00;
status_write = R_CSI10_Send_Receive(write_data_crc,sizeof(write_data_crc), read_data_crc);
while (g_spi_csi10_tx_done == 0) {
;
}
read_value = read_data_crc[3];
P4_BIT2_PSSL = 1;
return read_value;
}`
Waveform output:
Multibyte Write Transaction
Read 0x125 and 0x126 reg address
I’m expecting to read 0xFA, 0xEF, 0xCD, and 0x9F in 0x125, 0x126, 0x127, and 0x128 respectively.