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.

  • 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.

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

  • No, NRF_LOG_HEXDUMP_INFO is not available in SDK 11. Then you have to do it in a loop instead. For instance:

    for (int i = 0; i < sizeof(m_rx_buf); i++)
    {
        NRF_LOG_PRINTF("%02x", m_rx_buf[i]);
    }

  • For this:

    for (int i = 0; i < sizeof(m_rx_buf); i++)
    {
    NRF_LOG_PRINTF("%02x", m_rx_buf[i]);
    }

    Output is : FFFFFFFF00000000000000000000"

    complete code:

    /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    #include "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    
    #if defined(BOARD_PCA10036) || defined(BOARD_PCA10040)
    #define SPI_CS_PIN   29 /**< SPI CS Pin.*/
    #elif defined(BOARD_PCA10028)
    #define SPI_CS_PIN   2  /**< SPI CS Pin.*/
    #define SPI_SCK_PIN 4
    #define SPI_MOSI_PIN 1
    #define SPI_MISO_PIN 3
    
    
    #else
    #error "Example is not supported on that board."
    #endif
    
    #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. */
    
    
    /**
     * @brief SPI user event handler.
     * @param event
     */
    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)
        {
    			for (int i = 0; i < sizeof(m_rx_buf); i++)
    				{
    						NRF_LOG_PRINTF("%02x", m_rx_buf[i]);
    				}
        }
    }
    
    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);
    
    
    }
    
    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(void)
    {
        LEDS_CONFIGURE(BSP_LED_0_MASK);
        LEDS_OFF(BSP_LED_0_MASK);
    
        APP_ERROR_CHECK(NRF_LOG_INIT());
        NRF_LOG_PRINTF("SPI example\r\n");
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG(SPI_INSTANCE);
        spi_config.ss_pin = SPI_CS_PIN;
    		spi_config.miso_pin=SPI_MISO_PIN;
    		spi_config.mosi_pin=SPI_MOSI_PIN;
    		spi_config.sck_pin=SPI_SCK_PIN;
    		spi_config.mode=NRF_DRV_SPI_MODE_3;
    	
    		spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler));
    				
    				/*
    				sendDataToSPI(0x80, 0x00000008);
    				sendDataToSPI(0xEC, 0x000100C5);
    				sendDataToSPI(0xB0, 0x00011F05);
    				sendDataToSPI(0xAC, 0x00002710);
    				sendDataToSPI(0x90, 0x000401C8);
    				sendDataToSPI(0xB2, 0x00061A80);
    				sendDataToSPI(0xB1, 0x00007530);
    				sendDataToSPI(0xA6, 0x00001388);
    				sendDataToSPI(0xA7, 0x00004E20);
    				sendDataToSPI(0xA0, 0x00000001);
    				sendDataToSPI(0x21, 0x00000000);
    				*/
    				
    				sendSPI(0x80000008);
    				sendSPI(0xEC0100C5);
    				sendSPI(0xB0011F05);
    				sendSPI(0xAC002710);
    				sendSPI(0x900401C8);
    				sendSPI(0xB2061A80);
    				sendSPI(0xB1007530);
    				sendSPI(0xA6001388);
    				sendSPI(0xA7004E20);
    				sendSPI(0xA0000001);
    				sendSPI(0x21000000);
    				
        while(1)
        {
    				unsigned long i_datagram;
    
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
    				/*
    				sendDataToSPI(0xA4, 0x000003E8);
    				sendDataToSPI(0xA5, 0x0000C350);
    				sendDataToSPI(0xA6, 0x000001F4);
    				sendDataToSPI(0xA7, 0x000304D0);
    				sendDataToSPI(0xA8, 0x000002BC);
    				sendDataToSPI(0xAA, 0x00000578);
    				sendDataToSPI(0xAB, 0x0000000A);
    				sendDataToSPI(0xA0, 0x00000000);
    				sendDataToSPI(0xAD, 0xFFFF3800);
    				*/
    				sendSPI(0xA40003E8);
    				sendSPI(0xA500C350);
    				sendSPI(0xA60001F4);
    				sendSPI(0xA70304D0);
    				sendSPI(0xA80002BC);
    				sendSPI(0xAA000578);
    				sendSPI(0xAB00000A);
    				sendSPI(0xA0000000);
    				sendSPI(0xADFF3800);
            while (!spi_xfer_done)
            {
                __WFE();
            }
    
            LEDS_INVERT(BSP_LED_0_MASK);
            nrf_delay_ms(200);
        }
    }
    

  • Perfect. Then that is the content of the m_rx_buf at the point where you print it. (Note that you could always beautify it if you want by for instance splitting in bytes and prefixing each byte with 0x by changing the print statement to this: "NRF_LOG_PRINTF("0x%02x, ", m_rx_buf[i]);").

  • Thanks for the reply.

    However, its always printing “FFFFFFFF000000000000000000000000”

    i assume m_rx_buf should have printed the data which is written over SPI right?

    In code, i am trying to write: 

    0x80000008

    0xEC0100C5

    and so on...

Related