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

SPI configuration for nrf52832

I've configured the SPI with DMA module as in the code below.

I've loaded the tx buffer with sample data and start the transaction; however, I'm not reading anything either on the MOSI pin or the MISO pin. The SCK pin also stays low. The tx and rx end events do go from 0 to 1 correctly.

Any ideas what I'm doing wrong?

Thanksspi code.rtf

Parents
  • I think my problem might be that I'm not using a supported type of buffer for tx and rx. In my code I've created a uint8_t array with 2 elements for each. I've populated the tx buffer with data and set the TXD.PTR to the address of this array. This is what works for me when I use the UARTE with DMA.

    I've updated my code using hmolesworths suggestions and updated TXD.PTR per the data sheet at each loop. But I still don't have anything on the MOSI pin.

    #define CSPIN 11  //spi chip select
    #define SCKPIN 12  //spi clock
    #define MOSIPIN 13  //spi mosi
    #define MISOPIN 14  //spi miso
    
    int main()
    {
      NRF_GPIO->PIN_CNF[CSPIN] = 1;  //output
      NRF_GPIO->PIN_CNF[SCKPIN] = 1;  //output
      NRF_GPIO->PIN_CNF[MOSIPIN] = 1;  //output
      NRF_GPIO->PIN_CNF[MISOPIN] = 0;  //input pin, input buffer connected, no pull, S0S1, sense disabled
      NRF_SPIM0->PSEL.SCK = SCKPIN;
      NRF_SPIM0->PSEL.MOSI = MOSIPIN;
      NRF_SPIM0->PSEL.MISO = MISOPIN;
      NRF_SPIM0->CONFIG = 0b010;  //CPOL 0 -- clock polarity active high, CPHA 1 -- sample on trailing clock edge, send Msb first
      NRF_SPIM0->FREQUENCY = 0x02000000;  //250 kbps
      NRF_SPIM0->TXD.MAXCNT = 2;
      NRF_SPIM0->RXD.MAXCNT = 2;
      NRF_SPIM0->ENABLE = 7;  //enabled
      uint8_t spiTXBuf[2];  //2 byte tx buffer
      uint8_t spiRXBuf[2];  //2 byte rx buffer
      spiTXBuf[0] = 0b10101010;  //example byte
      spiTXBuf[1] = 0b10101010;  //example byte
      NRF_SPIM0->TXD.PTR = (uint32_t)((uint8_t *)spiTXBuf);
      NRF_SPIM0->RXD.PTR = (uint32_t)((uint8_t *)spiRXBuf);
      while(1)
      {
        NRF_SPIM0->EVENTS_ENDTX = 0;
        NRF_SPIM0->EVENTS_ENDRX = 0;
        NRF_SPIM0->EVENTS_STARTED = 0;
        NRF_GPIO->OUTCLR = 1 << CSPIN;  //drive cs low to initiate spi comm
        for(uint8_t i = 0; i < 21; i++) __asm__("nop\n\t");  //low for 21 machine instructions
        NRF_SPIM0->TASKS_START = 1;
        while(!NRF_SPIM0->EVENTS_STARTED);
        NRF_SPIM0->TXD.PTR = (uint32_t)((uint8_t *)spiTXBuf);
        NRF_SPIM0->RXD.PTR = (uint32_t)((uint8_t *)spiRXBuf);
        while(!NRF_SPIM0->EVENTS_ENDTX);  //last byte transmitted
        while(!NRF_SPIM0->EVENTS_ENDRX);  //last byte received
        NRF_SPIM0->TASKS_STOP = 1;
        for(uint8_t i = 0; i < 3; i++) __asm__("nop\n\t");
        NRF_GPIO->OUTSET = 1 << CSPIN;
        for(uint8_t i = 0; i < 21; i++) __asm__("nop\n\t");
      }
    }

      

  • Edit: I just tested your last code posted and it fails the first time through the loop as you start the transfer before setting the pointers, but second time through it works correctly.

    Did using the exact code I posted (with your pin numbers) not work? Is this on an PCA10040 by any chance? Just that P0.13 is affected by U4, SHIELD_DETECT and BUTTON1 in non-obvious ways ..

  • It's not the PCA10040. I have SoftDevice 132 loaded. I'm wondering if I have some hardware issues, since your code is working. I'll check on another board when I can. In the meantime I'll move some pins around.

  • What is a slave chip?
    Accelerometer? Is that an Invensense chip?
    In the case of ICM-20948 or MPU-9250 when I look at SPI Bus Timing Diagram of SPI, I think it is the best timing when it is CPOL=1, CPHA=1.

Reply Children
No Data
Related