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

how to drive spi CS line low for 32 bits.

Hi,

Can you please direct me to the document that explains about generating more than 8 bits of clock in SPI and keeping CS line low for that entire period.

Also, if i have to send two 16bit data, would i have to send them in lots of 8bit ? or i can send them as 16bits using nrf_drv_spi_transfer API?

I am modifying the SPI example found under SDK15/example/peripheral/spi.

And i have to achieve the sequence attached in the image. 

Thank you  

  • Hi Hakon,

    So my rxbuffer sjould be of 4 bytes, right? rx_buffer[4]. Is it a violation or i can directly increase the size? 

    Switching mode is not required as suggested by MICROCHIP. All transactions will happen using single mode only.

    Thanks 

  • rx_buffer should be equal (or larger) to the length that you are receiving.

    Declare it like this:

    static uint8_t m_rx_buf[4];

    The salaea logic analyzer trace shows that your send buffer and the actual byte endianess on the bus does not correspond, as the READ bit is not set.

    Could you try setting your transfer buffer like this and see if the sensor responds?

    uint8_t m_tx_buf4[] = {0x80, 0x30};

    Kind regards,

    Håkon

  • Hi Hakon,

    The problems were endianess and also the frequency. I had to decrease the frequency according to the peripheral. I am able to communicate with the IC now. But I am facing another problem now. 

    In the attached code. I am placing the break point at every nrf_drv_spi_transfer. But it is not stopping on  2nd and last transfer. This is strange. If I change the order of transfers, the debugger will not stop at some other location. What could be causing this? Is the delay not enough to wait for the completion of previous transfer ? Is it getting stuck somewhere? Can you please have a quick look at this simple code snippet and advise. Thanks,

    #define SPI_INSTANCE  0 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    
    
    static uint32_t       config_start =  0b01111000010101100011000000000000;           //0X30H
    static uint32_t       cal_start =     0b01111000010101100100000000000000; 
    static uint32_t       harm_start =    0b01111000010101100110000000000000;           //0X50H
    static uint32_t       adj_start =     0b01111000010101100110000000000000;           //0X60H
    static uint32_t       mmode0 =        0b11000010000010000011001100000000;           //0X60H
    static uint32_t       test =          0b0011000010000000;
    static uint16_t       urmsA =         0b1101100110000000;                           //0XBCH
    static uint16_t       urmsB =         0b1101101010000000;                           //0XBCH
    static uint16_t       urmsC =         0b1101101110000000;                           //0XBCH
    static uint16_t       irmsA =         0b1101110110000000;                           //0XBCH
    static uint16_t       irmsB =         0b1101111010000000;                           //0XBCH
    static uint16_t       irmsC =         0b1101111110000000;                           //0XBCH
    static uint32_t       uoffsetA =      0b00000000000000000110001100000000;           //0X60H
    static uint32_t       uoffsetB =      0b00000000000000000110011100000000;           //0X60H
    static uint32_t       uoffsetC =      0b00000000000000000110101100000000;           //0X60H
    static uint32_t       ioffsetA =      0b00000000000000000110100000000000;           //0X60H
    static uint32_t       ioffsetB =      0b00000000000000000110110000000000;           //0X60H
    static uint32_t       ioffsetC =      0b00000000000000000110110000000000;           //0X60H
    static uint32_t       igainA =        {0x30, 0x75, 0x62, 0x00};                     //0X60H
    static uint32_t       igainA_r =      0b00110000011101010110001000000000;                   //0X60H
               //0X30H
     
    
    static uint8_t       m_rx_buf[4];                                               /**< RX buffer. */
    static const uint8_t  m_length = 4;                                            /**< Transfer length. */
    
    /**
     * @brief SPI user event handler.
     * @param event
     */
    
    
    
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                           void *                    p_context)
    {
        spi_xfer_done = true;
        NRF_LOG_INFO("Transfer completed.");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
            nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
            spi_config.ss_pin   = 47 ;
            spi_config.miso_pin = 43;
            spi_config.mosi_pin = 46;
            spi_config.sck_pin  = 42;
            spi_config.mode = NRF_DRV_SPI_MODE_0;
            spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
            APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    
            NRF_LOG_INFO("SPI example started.");
    
    
                           
    
            while(1){
          
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
           
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &config_start, m_length, m_rx_buf, m_length));
            
                  while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
    
            bsp_board_led_invert(0);
            nrf_delay_ms(200);  
    
              memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
           
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &cal_start, m_length, m_rx_buf, m_length));
            
                  while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
    
            bsp_board_led_invert(0);
            nrf_delay_ms(200);
    
             memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
           
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &harm_start, m_length, m_rx_buf, m_length));
            
                  while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
    
            bsp_board_led_invert(0);
            nrf_delay_ms(200);
    
             memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
           
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &adj_start, m_length, m_rx_buf, m_length));
            
                  while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
    
            bsp_board_led_invert(0);
            nrf_delay_ms(200);
    
             memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
           
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &igainA_r, m_length, m_rx_buf, m_length));
            
                  while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
    
            bsp_board_led_invert(0);
            nrf_delay_ms(200);
    
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
           
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &irmsA, m_length, m_rx_buf, m_length));
            
                  while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
    
            bsp_board_led_invert(0);
            nrf_delay_ms(200);
        
    }
    }
    
     

  • Hi,

     

    Is there anything going out on the SPI lines when this occurs?

    When debugging, It is recommend that you keep the optimization level low, if not; the compiler will try to find patterns in your generated code and thus the "normal flow" will not be followed.

    Kind regards,

    Håkon

Related