SPI communication code in C and converting into Arduino
Posted: Sat Aug 10, 2019 10:56 am
I want to connect Murata gyro sensor to ESP32 via SPI. So I am trying to understand sample code written for NXP LCP11U14 Cortex-M0 microcontroller and studying the datasheet. You can find full code in PDF using this link: https://www.murata.com/~/media/webrenew ... x?la=en-us
Datasheet of sensor: https://www.murata.com/~/media/webrenew ... x?la=en-us
This portion of code is used to read responses for different requests:
It uses off-frame protocol, word length is 32 bits, SPI mode 0.
I need a help to implement this function in Arduino. I am a little familiar with bitwise operators and pointers. But I can not exactly understand how this function works. It has also controller specific definitions.
Could you please help me understand how works in steps?
Datasheet of sensor: https://www.murata.com/~/media/webrenew ... x?la=en-us
This portion of code is used to read responses for different requests:
Code: Select all
uint32_t SendRequest(uint32_t Request)
{
uint32_t Response;
LPC_GPIO->CLR[PORT0] = PIN_CSB; // digitalWrite(PIN_CSB, LOW);
Response = LPC_SSP0->DR; // Read RX buffer just to clear interrupt flag
// Move on only if NOT busy and TX FIFO not full
while ((LPC_SSP0->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF);
LPC_SSP0->DR = Request >> 16; // Write Request high word to TX buffer
while (LPC_SSP0->SR & SSPSR_BSY); // Wait until the Busy bit is cleared
Response = LPC_SSP0->DR; // Read RX buffer (Response high word)
Response <<= 16;
LPC_SSP0->DR = Request & 0x0000FFFF; // Write Request low word to TX buffer
while (LPC_SSP0->SR & SSPSR_BSY); // Wait until the Busy bit is cleared
Response |= LPC_SSP0->DR; // Read RX buffer (Response low word)
LPC_GPIO->SET[PORT0] = PIN_CSB; // digitalWrite(PIN_CSB, HIGH);
return Response;
}
Could you please help me understand how works in steps?