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
  • This your code correctly working on an SPI accelerometer; the delays are arbitrary and not tested with other values (just there to mimic your delays). The returned value is correct:

    #define CSPIN   ACC_CS_PIN   // 11  //spi chip select
    #define SCKPIN  ACC_SCK_PIN  // 12  //spi clock
    #define MOSIPIN ACC_MOSI_PIN // 13  //spi mosi
    #define MISOPIN ACC_MISO_PIN // 14  //spi miso
    
    #define LIS2DH12_WHO_AM_I          0x0F                          // Read as 0x33
    
    uint8_t spiTXBuf[] = {0x80|LIS2DH12_WHO_AM_I, 0xFF};
    uint8_t spiRXBuf[sizeof(spiTXBuf)];
    static const uint8_t mRxTxBufLength = sizeof(spiTXBuf);
    
    void TestSPI(void)
    {
      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_GPIO->OUTSET = 1 << CSPIN;  //deactivate by setting chip select high
      NRF_SPIM0->PSEL.SCK = SCKPIN;
      NRF_SPIM0->PSEL.MOSI = MOSIPIN;
      NRF_SPIM0->PSEL.MISO = MISOPIN;
      NRF_SPIM0->CONFIG = 0; //2; // 0b010;  //CPOL 0 -- clock polarity active high, CPHA 1 -- sample on trailing clock edge, send Msb first
      NRF_SPIM0->FREQUENCY = 0x20000000;  //2 Mbps
    //uint8_t spiTXBuf[2];  //2 byte tx buffer
    //uint8_t spiRXBuf[2];  //2 byte rx buffer
    //spiTXBuf[0] = ADS_CMND_RREG; // 0b10101010;  //example byte
    //spiTXBuf[1] = 2-1; // 0b10101010;  //example byte
    
      nrf_delay_us(200);                  // Delay to allow 18 tclk minimum
      NRF_SPIM0->ENABLE = 7;  //enabled
    
      NRF_SPIM0->TXD.PTR = (uint32_t)((uint8_t *)spiTXBuf);
      NRF_SPIM0->TXD.MAXCNT = mRxTxBufLength; //2;
      NRF_SPIM0->RXD.PTR = (uint32_t)((uint8_t *)spiRXBuf);
      NRF_SPIM0->RXD.MAXCNT = mRxTxBufLength; //2;
    //  NRF_SPIM0->ENABLE = 7;  //enabled
      NRF_SPIM0->EVENTS_ENDTX = 0;
      NRF_SPIM0->EVENTS_ENDRX = 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_delay_us(200);
      NRF_SPIM0->TASKS_START = 1;
      while(!NRF_SPIM0->EVENTS_ENDTX);  //last byte transmitted
      while(!NRF_SPIM0->EVENTS_ENDRX);  //last byte received
      NRF_SPIM0->TASKS_STOP = 1;
      NRF_GPIO->OUTSET = 1 << CSPIN;
      while(1);
    }

Reply
  • This your code correctly working on an SPI accelerometer; the delays are arbitrary and not tested with other values (just there to mimic your delays). The returned value is correct:

    #define CSPIN   ACC_CS_PIN   // 11  //spi chip select
    #define SCKPIN  ACC_SCK_PIN  // 12  //spi clock
    #define MOSIPIN ACC_MOSI_PIN // 13  //spi mosi
    #define MISOPIN ACC_MISO_PIN // 14  //spi miso
    
    #define LIS2DH12_WHO_AM_I          0x0F                          // Read as 0x33
    
    uint8_t spiTXBuf[] = {0x80|LIS2DH12_WHO_AM_I, 0xFF};
    uint8_t spiRXBuf[sizeof(spiTXBuf)];
    static const uint8_t mRxTxBufLength = sizeof(spiTXBuf);
    
    void TestSPI(void)
    {
      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_GPIO->OUTSET = 1 << CSPIN;  //deactivate by setting chip select high
      NRF_SPIM0->PSEL.SCK = SCKPIN;
      NRF_SPIM0->PSEL.MOSI = MOSIPIN;
      NRF_SPIM0->PSEL.MISO = MISOPIN;
      NRF_SPIM0->CONFIG = 0; //2; // 0b010;  //CPOL 0 -- clock polarity active high, CPHA 1 -- sample on trailing clock edge, send Msb first
      NRF_SPIM0->FREQUENCY = 0x20000000;  //2 Mbps
    //uint8_t spiTXBuf[2];  //2 byte tx buffer
    //uint8_t spiRXBuf[2];  //2 byte rx buffer
    //spiTXBuf[0] = ADS_CMND_RREG; // 0b10101010;  //example byte
    //spiTXBuf[1] = 2-1; // 0b10101010;  //example byte
    
      nrf_delay_us(200);                  // Delay to allow 18 tclk minimum
      NRF_SPIM0->ENABLE = 7;  //enabled
    
      NRF_SPIM0->TXD.PTR = (uint32_t)((uint8_t *)spiTXBuf);
      NRF_SPIM0->TXD.MAXCNT = mRxTxBufLength; //2;
      NRF_SPIM0->RXD.PTR = (uint32_t)((uint8_t *)spiRXBuf);
      NRF_SPIM0->RXD.MAXCNT = mRxTxBufLength; //2;
    //  NRF_SPIM0->ENABLE = 7;  //enabled
      NRF_SPIM0->EVENTS_ENDTX = 0;
      NRF_SPIM0->EVENTS_ENDRX = 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_delay_us(200);
      NRF_SPIM0->TASKS_START = 1;
      while(!NRF_SPIM0->EVENTS_ENDTX);  //last byte transmitted
      while(!NRF_SPIM0->EVENTS_ENDRX);  //last byte received
      NRF_SPIM0->TASKS_STOP = 1;
      NRF_GPIO->OUTSET = 1 << CSPIN;
      while(1);
    }

Children
No Data
Related