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

SPI long data transfer

Hello Everyone,

I am newbie to nordic BLE environment. This maybe a silly question.

Currently I am using nrf51 DK with SDK11, S130.

Data to be sent through SPI:

I checked the example code in SDK11, and there is example code and m_tx_buf is variable for sending data, but not sure how should I send all the data mentioned in above image. Data is very long to be sent as data is of type long long and m_tx_buf is of type const uin8_t *. 

This link embed entire initialisation values of LCD init command in one buffer, do I need to follow the same?

Thanks in advance.

Parents
  • Hello Einar,

    Thank you for your reply. I made this function "sendDataToSPI" which accepts the address and data (as mentioned in the above image). Total 40 bits are transferred via SPI in each data transfer. The first byte is send directly and later 32 byte are split in to 4 byte and sent separately. 

    Is this function Ok?

    void sendDataToSPI(uint8_t address, unsigned long datagram)
    {					
    				APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &address, m_length, m_rx_buf, m_length));
    	      NRF_LOG_PRINTF(" address: %x\r\n",address);
    
    				val0 = (datagram & 0xFF);
    
    				datagram >>= 8;
    				val1 =  (datagram & 0xFF);
    
    				datagram >>= 8;
    				val2 = (datagram & 0xFF);
    
    				datagram >>= 8;
    				val3 =  (datagram & 0xFF);
    	
    				APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &val3, m_length, m_rx_buf, m_length));
            NRF_LOG_PRINTF(" val3: %x\r\n",val3);
    	
    				APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &val2, m_length, m_rx_buf, m_length));
            NRF_LOG_PRINTF(" val2: %x\r\n",val2);
    		
    				APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &val1, m_length, m_rx_buf, m_length));
            NRF_LOG_PRINTF(" val1: %x\r\n",val1);
    	
    				APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &val0, m_length, m_rx_buf, m_length));
            NRF_LOG_PRINTF(" val0: %x\r\n",val0);
    
    
    }

  • Hi,

    Why do you split the transaction up like this? You can send up to 255 bytes in one transfer. Just let the puffer pointer point to the start of the data, and set the length field to a multiple of the bytes you have to send.

  • Hi,

    Assuming you still want a separate write for the address you can do it like this (assuming blocking mode):

    void sendDataToSPI(uint8_t address, unsigned long datagram)
    {
        static length = sizeof(address); // 1
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &address, length, m_rx_buf, length));
    
        length = sizeof(datagram); // 4
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, (uint8_t *)&datagram, length, m_rx_buf, length));
    }

    If you don't want to send the address in a separate transaction (I don't see any reason why), then you can make an array containing both so that you only have one transaction.

Reply
  • Hi,

    Assuming you still want a separate write for the address you can do it like this (assuming blocking mode):

    void sendDataToSPI(uint8_t address, unsigned long datagram)
    {
        static length = sizeof(address); // 1
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, &address, length, m_rx_buf, length));
    
        length = sizeof(datagram); // 4
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, (uint8_t *)&datagram, length, m_rx_buf, length));
    }

    If you don't want to send the address in a separate transaction (I don't see any reason why), then you can make an array containing both so that you only have one transaction.

Children
  • Hi Einar,

    Thanks for the reply again.

    Is this correct if I transfer complete date in one go?

    
    void sendSPI(unsigned long datagram) 
    { 
        static int length= sizeof(datagram); 
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, (uint8_t *) &datagram, length, m_rx_buf, length)); 
    }
    .
    
    .
    int main()
    {
        sendSPI(0x80000008); sendSPI(0xEC0100C5); sendSPI(0xB0011F05); sendSPI(0xAC002710); sendSPI(0x900401C8); sendSPI(0xB2061A80); sendSPI(0xB1007530); sendSPI(0xA6001388); sendSPI(0xA7004E20); sendSPI(0xA0000001); sendSPI(0x21000000);
    }
    
    

    One more question:

    if (m_rx_buf[0] != 0) { NRF_LOG_PRINTF(" Received: %x\r\n",m_rx_buf); 

    this part is printing 

    0>  Received: 20000034

    0>  Received: 20000034

    0>  Received: 20000034

    0>  Received: 20000034

    No matter what's the data send by SPI.

  • Hi,

    That looks OK.

    The way you print the RX buffer you print it as if it was a variable, but the number looks like it is an address (in RAM). So most likely you are just printing a pointer address. How have you declared m_rx_buf?

  • Hi,

    The m_rx_buf is declared as the following:

    #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. */
    long long MISO = 0x8000000008;
    #define TEST_STRING "0x8000000008"
    
    unsigned char val0, val1, val2, val3, val4;
    
    static uint8_t       m_tx_buf[] = {0x80};           /**< TX buffer. */
    static uint8_t       m_rx_buf[sizeof(TEST_STRING)+1];    /**< RX buffer. */
    static uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
    
    
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
    {
        spi_xfer_done = true;
        //NRF_LOG_PRINTF(" Transfer completed.\r\n");
    	  //NRF_LOG_PRINTF(" address: %x %x %x %x\n", val3, val2, val1, val0);
    
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_PRINTF(" Received: %x\r\n",m_rx_buf);
        }
    }

  • I see. Then it is as I assumed, you just print the address of the buffer. A simple way to print the buffer as a hexadecimal string is to use NRF_LOG_HEXDUMP_INFO.

  • I am using SDK11, Is "NRF_LOG_HEXDUMP_INFO" available in SDK11?

Related