3 months ago.

Custom STM32L4 board with Nordic nrf52811 via SPI

I have an mbed application that was running on a DRAGONFLY_L471QG. We now have a new STM32L4 based board which has a nrf52811 connected via SPI. I think I want to use nrf_drv_spi_init() to init the device, but can't logistically figure out how the 'proper' way to get access to the code, which resides in: mbed-os/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/spi_api.c

My target board is an override of the original DRAGONFLY_L471QG, so the target is not Nordic, but I want to be a SPI master to the Nordic, so I want access to the TARGET_NORDIC code.

I could copy the files I need into a library folder and build them there but that seems like the wrong approach.

Seems like there should be some entries made in my mbed_app.json to facilitate this, but I'm too new to this to really figure it out. What, for example, does this accomplish: "target.features_add": ["BLE"],

What is the proper procedure to get access to the Nordic spi_api.c code from my STM32 based application?

Thank you!

Mark

Comment on this question

1 Answer

2 months, 2 weeks ago.

Hello Mark,

The code provided in the spi_api.c file is specific for the given target so it won't work with other chips. It represents the hardware abstraction layer (HAL) and those C functions are not designed to be called directly from the application program but rather from Mbed's SPI or SPISlave class methods. But that's already done by Mbed. This way Mbed's SPI Master API and SPI Slave API could be same for any target supported by Mbed. The only target specific things in an application program are SPI pin names.

For example, in your case the SPI Master test program for the STM target could look like:

SPI Master - main.cpp

#include "mbed.h"

SPI     spiMaster(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // mosi, miso, sck, ssel pins
Serial  pc(USBTX, USBRX);                               // tx, rx pins

int main()
{
    int dataToSend = 0;
    int dataReceived;

    pc.printf("SPI Master\r\n");
    spiMaster.format(8, 3);       // Both, Master and Slave have to use the same format
    spiMaster.frequency(1000000); // and same frequency

    while (1) {
        dataReceived = spiMaster.write(dataToSend);
        pc.printf("Sent:%d \t\t\t Received:%d\r\n", dataToSend, dataReceived);
        dataToSend++;
        wait(5);
    }
}

and the SPI Slave test program for the nrf52811:

SPI Slave - main.cpp

#include "mbed.h"

SPISlave    spiSlave(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);  // mosi, miso, sck, ssel pins
Serial      pc(USBTX, USBRX);                               // tx, rx pins

int main()
{
    int dataToSend = 0;
    int dataReceived;

    pc.printf("SPI Slave\r\n");
    spiSlave.format(8, 3);       // Both, Master and Slave have to use the same format
    spiSlave.frequency(1000000); // and same frequency
    while (1) {
        if (spiSlave.receive()) {
            spiSlave.reply(dataToSend);     // pre-load SPI with next reply data
            dataReceived = spiSlave.read(); // receive data from Master + send reply data back to Master
            pc.printf("Sent:%d /t/t/t Received:%d\r\n", dataToSend, dataReceived);
            dataToSend++;
        }
    }
}

Substitute the pin names in the codes above with those used on your custom board and compile the SPI Master for the STM target and the SPI Slave for the Nordic chip. Mbed will automatically take care that the correct spi_api.c file is used for the given target. Make sure that Master's MOSI pin is connected to Slave's MISO pin and Master's MISO pin to Slave's MOSI pin. The SSEL pins have to be connect one another. The SCK pins shall be connected to each other too.

Quote:

What, for example, does this accomplish: "target.features_add": ["BLE"]

That will make the build system aware of additional directories it must scan for BLE resources when compiling for the given target. Please read here for more details.


You need to log in to post a question