This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF51 dk 16 bit spi runtime error

Hi guys I'm sending data from an nexys 4 fpga through an spi to my nRF51 (pca10028) I have the fpga set as the master and I'm able to read data sent to the nRF51 through my tera term when I have my spi.format set to (8,2)... but for some reason when I set the format to (16,2) I'm getting a runtime error. Or at least the (led1,led3) and (led2,led4) are flashing in an alternating pattern which I think is a runtime error. I have my fpga actually set to 16 bits and running on mode 2 as well so I find it odd that it's giving me this error. They also are running at the same frequency as well.

////////// Here's the simple code I have on my Nordic 

// Reply to a SPI master as slave
 
#include "mbed.h"
 
SPISlave spi(P0_16, P0_15, P0_14, P0_13); // mosi, miso, sclk, ssel
Serial pc(USBTX, USBRX);

int main() {
    
    spi.format(8,2);
    spi.frequency(100000);
    pc.baud(9600);
    spi.reply(0x00);              // Prime SPI with first reply
  
    while(1) { 
        if(spi.receive()) {
            int v = spi.read();   // Read byte from master
           // v = (v + 1) % 0x100;    
            spi.reply(v);         // Make this the next reply
       pc.printf("Received Data = 0x%X\n\r",v);
       }
    }
}

Has anyone run into something similar?

  • Hi, sorry for the delayed response. I'm not familiar with the mbed SPI drivers, but are you trying to send 16-bit frames? The nRF51 only supports 8 bit.

  • Hi Martin , Thanks for the reply. I'm wondering when you mean the nRF51 only supports 8 bit you are talking about the Bluetooth? I should still be able to send 16bit frames to the board right? What I have coming from the FPGA is in 16 bit but I really only need to broadcast a certain portion of that 16 bits. This should be possible right? Sorry for the vague question.

  • Do you have a link to the driver? Could it be that the nRF51 mbed SPI driver does not support 16-bits format? For example I found this driver and it returns an error if you try to use any other bit widths than 8 bits.

  • Hi Martin, Thanks again for replying. I really appreciate your help. I also noticed that if I set it to bit widths smaller than 8 as well I get a run time error. Here's the driver I'm using I'm looking at the SPI.h line 83 and there's a commented line saying 4-16 should work. I saw this at one point before which is the reason why I thought I shouldn't have any problems.

  • As mentioned, I'm not very familiar with mbed, but if you look in "/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/" in the repository you linked to you will find spi_api.c. This file contains the function definitions that are actually being used. In the function spi_format() you will find the following check:

    if (bits != 8) {
        error("Only 8bits SPI supported");
    }
    

    prohibiting you from using any other bit widths than 8 bits.

    The SPI library in mbed tries to cover many MCUs, but not all MCUs have similar SPI capabilities. Some of them support various bit widths, but unfortunately nRF51 is one of those who don't. I was not able to find this documented in the mbed drivers, but as you can see in the function definition in spi_format() you will get an error if you use something else than 8 bit.

Related