SPI Communication

Discussions and support about ChibiOS/RT, the free embedded RTOS.
karthik
Posts: 10
Joined: Mon Jun 23, 2025 10:38 am

SPI Communication

Post by karthik »

Hi.,

I am using ICM206089 SPI1 Port and MS5611 SPI4 port to get Accel, Gyro values from ICM20689 and the Pressure, Temperature values from the MS5611.
Initialization Code is
static const SPIConfig spi1cfg = {
.ssport = NULL,
.sspad = 0,
.cr1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_BR_1,
.cr2 = SPI_CR2_FRXTH | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0,
};
static const SPIConfig spicfg_ms5611 = {
.ssport = GPIOF,
.sspad = 10,
.cr1 = SPI_CR1_MSTR | SPI_CR1_BR_1 | SPI_CR1_SSM | SPI_CR1_SSI,
.cr2 = SPI_CR2_FRXTH | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0,
};
uint8_t icm20689_read_reg(uint8_t reg) {
uint8_t txbuf[2] = { reg | 0x80, 0x00 };
uint8_t rxbuf[2];
spiAcquireBus(&SPID1);
spiSelect(&SPID1);
ICM20689_CS_LOW();
spiExchange(&SPID1, 2, txbuf, rxbuf);
ICM20689_CS_HIGH();
spiUnselect(&SPID1);
spiReleaseBus(&SPID1);
ICM20689_CS_HIGH();

return rxbuf[1];
}
uint16_t ms5611_read_prom(uint8_t index) {
uint8_t cmd = MS5611_CMD_PROM_READ + (index * 2);
uint8_t rx[2];
MS5611_CS_LOW();
spiAcquireBus(&SPID4);
spiSelect(&SPID4);
spiStartSend(&SPID4, 1, &cmd);
spiStartReceive(&SPID4, 2, rx);
spiUnselect(&SPID4);
spiReleaseBus(&SPID4);
MS5611_CS_HIGH();
return (rx[0] << 8) | rx[1];
}

void ms5611_read_all_prom(void) {
for (uint8_t i = 0; i < 7; i++) {
ms5611_prom[i] = ms5611_read_prom(i);
chprintf(&SD2, "i= %d, %d\r\n",i, ms5611_prom[i]);
}
}
void ms5611_send_cmd(uint8_t cmd) {
MS5611_CS_LOW();
spiAcquireBus(&SPID4);
spiSelect(&SPID4);
spiStartSend(&SPID4, 1, &cmd);
spiUnselect(&SPID4);
spiReleaseBus(&SPID4);
MS5611_CS_HIGH();
}
void ms5611_init(void){
ms5611_send_cmd(MS5611_CMD_RESET);
chThdSleepMilliseconds(5);
ms5611_read_all_prom();
chThdSleepMilliseconds(10);
}
int main(void){
spiStart(&SPID1, &spi1cfg);
chThdSleepMilliseconds(100);
spiStart(&SPID4, &spicfg_ms5611);
uint8_t whoami_20689 = icm20689_read_reg(0x75); // WHO_AM_I
if (whoami_20689 != 0x98) {
chprintf(&SD2, "ICM20689 not found!\r\n");
icm20689= 0;
}
if(whoami_20689 == 0x98)
{
chprintf(&SD2, "ICM20689 found\r\n");
icm_20689_init();
icm20689= 1;
}
ms5611_init();
}

Result is:
ICM20689 Found
MS5611 PROM values
i=0, 65432
i=1, 65432
i=2, 65432
i=3, 65432
i=4, 65432
i=5, 65432
i=6, 65432

I am getting the MS5611 PROM values are wrong, I need to change SPI commands or my configuration is wrong. Suggest me which part is wrong
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Communication

Post by Giovanni »

Hi,

spiStartSend() is an asynchronous function, it returns before the operation is done, use spiSend() instead.

Giovanni
karthik
Posts: 10
Joined: Mon Jun 23, 2025 10:38 am

Re: SPI Communication

Post by karthik »

I checked with spiSend(), code is not executing.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Communication

Post by Giovanni »

Hi,

Also spiStartReceive() needs to become spiReceive(), hope you did that. I suggest to check your GPIO settings and look at the data traffic with a scope or an analyzer.

Giovanni
karthik
Posts: 10
Joined: Mon Jun 23, 2025 10:38 am

Re: SPI Communication

Post by karthik »

I tried individual sensor by changing pin configuration, valid data i received but when i tried 2 sensors in single code there is a problem.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Communication

Post by Giovanni »

Code: Select all

.ssport = NULL,
.sspad = 0,


This is wrong.

Giovanni
karthik
Posts: 10
Joined: Mon Jun 23, 2025 10:38 am

Re: SPI Communication

Post by karthik »

I assigned the GPIO Pins for CS Pin manually, I checked the Clock pulse for Spi1 and Spi4. I observed Spi1 clock is present and the SPI4 clock is absent. what will cause for clock pulse not working. More than 1 spi ports are using what is the configuration. How to use 2 spi ports in stm32f765 controller, provide reference.

Thank You
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Communication

Post by Giovanni »

See the SPI demo in ChibiOS: testhal/STM32/multi/SPI it does mostly what you are trying to do.

Giovanni
karthik
Posts: 10
Joined: Mon Jun 23, 2025 10:38 am

Re: SPI Communication

Post by karthik »

I checked the Demo multi SPi example code, in that only 1 spi ports has been used. i want to use 2 spi ports.

Thank You
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Communication

Post by Giovanni »

The code is the same, use 2 ports instead of one.

Giovanni
Post Reply