Page 1 of 1

SPI communication code in C and converting into Arduino

Posted: Sat Aug 10, 2019 10:56 am
by insanoff
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:

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;
}
It uses off-frame protocol, word length is 32 bits, SPI mode 0.
SPI.JPG
SPI communication
SPI.JPG (60.54 KiB) Viewed 4373 times
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?

Re: SPI communication code in C and converting into Arduino

Posted: Mon Aug 12, 2019 7:26 am
by insanoff
I would specify my questions:
  • 1. SPI messages are 32 bit messages, I am not sure what is the best way to do it in Arduino IDE. If you have another suggestion other that Arduino, welcome. So far I have not used anything else, but would like to.
  • 2. Peripherial mapping for ESP32 - where to start? I found the header file "LPC11xx.h" for "NXP LPC11U14 Cortex-M0", but usage of registers are confusing.
    • a. LPC_GPIO->CLR[PORT0] = PIN_CSB; how chip select low could be implemented for ESP32 in this way?
    • b. LPC_GPIO &= ~(1 << PORT0); would it work too fro CS?
    • c. Response = LPC_SSP0->DR; how it can read RX buffer?
    • d. LPC_SSP0->DR = Request >> 16; how it writes to TX buffer? Looks to me the same as RX, what is different here?
  • 3. Where is header file for ESP32 in Arduino IDE located? I would like to se the posrt implementations.
  • 4. How can I learn more about pin mapping and how header files are organized?

Re: SPI communication code in C and converting into Arduino

Posted: Mon Aug 19, 2019 7:51 am
by insanoff
insanoff wrote:
Mon Aug 12, 2019 7:26 am
  • 1. SPI messages are 32 bit messages, I am not sure what is the best way to do it in Arduino IDE. If you have another suggestion other that Arduino, welcome. So far I have not used anything else, but would like to.
My answer to my question 1, 32 bit SPI read and write in Arduino:

Code: Select all

uint32_t SendRequest(uint32_t Request)
{
    uint32_t Response = 0x00;
    digitalWrite(PIN_CSB, LOW);
    // Send the request in 8-bit sections
    SPI.transfer(Request >> 24);
    SPI.transfer(Request >> 16);
    SPI.transfer(Request >> 8);
    SPI.transfer(Request);
    digitalWrite(PIN_CSB, HIGH);
    delay(1);
    // Capture the response in 8-bit sections
    digitalWrite(PIN_CSB, LOW);
    Response = SPI.transfer(0xFF);
    Response = (Response << 8) | SPI.transfer(0xFF);
    Response = (Response << 8) | SPI.transfer(0xFF);
    Response = (Response << 8) | SPI.transfer(0xFF);
    digitalWrite(PIN_CSB, HIGH);
    delay(1);
    return (Response);
}