This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Why cannot log all the printing data when receiving from the SPI?

Hi Team,

When the slave side of the SPI received data from the master side, I want to log the received data to judge If it is right. But, I want to print it by log. it can only view 20 or so characters, Also including some garbled characters. Could you tell me why?

With kind regards,

Gaosheng

  • Hi

    The issues you have with printing seems to be because you're trying to print UART messages in an SPI interrupt. Please try adding these prints after the event and interrupt is completed instead, as using a "slow" peripheral like UART with a fast one like SPI is likely to print bad data like this.

    Best regards,

    Simon

  • as using a "slow" peripheral like UART with a fast one like SPI is likely to print bad data like this.

    Hi,

    The peripheral is also SPI, do you mean should I adjust phase and polarity parameters?

    Best regards,

    Gaosheng

  • #include "spis.h"
    #include "nrf_delay.h"
    #include "advertiser.h"
    
    #define                SPIS_INSTANCE                1 /**< SPIS instance index. */
    
    bool                   SWITCH2SPI                 = true;
    static  const          nrf_drv_spis_t spis        = NRF_DRV_SPIS_INSTANCE(SPIS_INSTANCE);/**< SPIS instance. */                           /** < RX buffer. */
    uint8_t                m_rx_buf_spi[BROADCASRLEN] = {0};
    volatile uint8_t *     m_tx_buf_spi;
    volatile uint8_t       m_tx_length;
    static volatile bool   spis_xfer_done;                     /**< Flag used to indicate that SPIS instance completed the transfer. */
    volatile bool          g_null_status            = true;
    
    
    static uint8_t         front_check[3]             = {0xAA, 0xBB, 0xCC};
    static uint8_t         rear_check[3]              = {0xDD, 0xEE, 0xFF};
    
    /**
     * @brief  SPIS check completeness of data
     * 1, 3, 4, 7, 34, 37 etc. frameLen + checkNums
     */
    bool check_completeness(uint8_t * receicedData)
    {
        for (uint8_t i = 1; i < 4; i++)
        {
            if (receicedData[i] != front_check[i - 1])
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "check failed!, front[%d]: rec%d\n", i, receicedData[i]);
                return false;
            }
        }
    
        uint8_t dataStatus = receicedData[7];
        if ((dataStatus == 0x04) || (dataStatus == 0x08))
        {
           for (uint8_t j = 0; j < 3; j++)
           {
                if (receicedData[j + 37] != rear_check[j])
                {
                    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "check failed!, front[%d]: rec:%c\n", j, receicedData[j]);
                    return false;
                }
            }
        } else if (dataStatus == 0x0C)
        {
            uint8_t p_len  = receicedData[11];
            uint8_t anchor = 12 + p_len;
            for (uint8_t j = 0; j < 3; j++)
            {
                uint8_t offset = anchor + j;
                if (receicedData[offset] != rear_check[j])
                {
                    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "check failed!, front[%d]: rec:%c\n", j, receicedData[j]);
                    return false;
                }
            }
        } else {
            for (uint8_t j = 0; j < 3; j++)
            {
                uint8_t offset = 12 + j;
                if (receicedData[offset] != rear_check[j])
                {
                    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "check failed!, front[%d]: rec:%c\n", j, receicedData[j]);
                    return false;
                }
            }
        }
    
        return true;
    }
    
    
    /**
     * @brief SPIS user event handler.
     *
     * @param event
     */
    void spis_event_handler(nrf_drv_spis_event_t event)
    {
        if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
        {
            spis_xfer_done = true;
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 1,  m_rx_buf_spi[1]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 2,  m_rx_buf_spi[2]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 3,  m_rx_buf_spi[3]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 4,  m_rx_buf_spi[4]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 5,  m_rx_buf_spi[5]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 36, m_rx_buf_spi[36]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 37, m_rx_buf_spi[37]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 38, m_rx_buf_spi[38]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 39, m_rx_buf_spi[39]);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", 40, m_rx_buf_spi[40]);
            /*
            for (uint8_t i = 39; i > 0; i--)
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", i, m_rx_buf_spi[i]);
            }
            for (uint8_t i = 0; i < 40; i++)
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", i, m_rx_buf_spi[i]);
            }
            */
            bool checkResult = check_completeness(m_rx_buf_spi);
    
            if (! checkResult)
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "check failed!\n");
                return;
            }
            /**
             * @brief Begin to transport data
             * 
             */
            g_null_status = false;
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Pass data checking\n");
            uint8_t statusAction =  m_rx_buf_spi[7];
            
            switch (statusAction)
            {
                /** The first datagram **/
                case 0x04:
                    receiveData_sendout(m_rx_buf_spi);
                    send_datagram_start();
                    break;
                /** Other datagrams **/
                case 0x08:
                    /** Finish last advertiser, prepare a new advertiser for this sending **/
                    set_if_terCurrentAdvertiser(true);
                    receiveData_sendout(m_rx_buf_spi);
                    send_datagram_start();
                    break;
                /** ack **/
                case 0x10:
                    /** Finish last advertiser, prepare a new advertiser for this sending **/
                    set_if_terCurrentAdvertiser(true);
                    receiveHeader_sendout(m_rx_buf_spi);
                    send_ack_start();
                    break;
                /** source fin **/
                case 0x40:
                    /** Finish last advertiser, prepare a new advertiser for this sending **/
                    set_if_terCurrentAdvertiser(true);
                    receiveHeader_sendout(m_rx_buf_spi);
                    send_fin_start();
                    break;
                /** dst fin **/
                case 0x80:
                    /** Finish last advertiser, prepare a new advertiser for this sending **/
                    set_if_terCurrentAdvertiser(true);
                    receiveHeader_sendout(m_rx_buf_spi);
                    send_fin_start();
                    break;
                case 0xD0:
                    advertiser_disableAndFlush(); // Finishing the current advertiser 
                    break;
                default:
                    break;
            }
        }
    }
    
    void spis_start(void)
    {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "spis_start\n");
    
        nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG;
        spis_config.csn_pin               = APP_SPIS_CS_PIN;
        spis_config.miso_pin              = APP_SPIS_MISO_PIN;
        spis_config.mosi_pin              = APP_SPIS_MOSI_PIN;
        spis_config.sck_pin               = APP_SPIS_SCK_PIN;
    
        APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler));
            
        while (SWITCH2SPI)
        {
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "free: %d\n", g_null_status);
            uint8_t   *   initArr;
            if (g_null_status)
            {
                initArr = (uint8_t *) malloc (sizeof(uint8_t) * 4);
                if (!initArr)
                {
                    return;
                }
                initArr[0] = 0x01;
                initArr[1] = 0x02;
                initArr[2] = 0x03;
                initArr[3] = 0x04;
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "size: %d\n", strlen(initArr));
                spis_setfrom_slave(initArr, 4); /** Initilization,  filling characters */
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchor5\n");
            } 
            //nrf_delay_ms(10);
            set_transData();
            //nrf_delay_ms(10);
            uint8_t * t_rx_buf_spi = (uint8_t *) malloc(sizeof(uint8_t) * BROADCASRLEN);
            if (!t_rx_buf_spi)
            {
                return;
            }
            memset(t_rx_buf_spi, 0, BROADCASRLEN);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchor2\n");
            spis_xfer_done = false;
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "free: %d\n", g_null_status);
           // uint8_t testArr[3] = {'2', '3', '4'};
           __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "m_tx_buf_spi: %d\n", m_tx_buf_spi[0]);
            APP_ERROR_CHECK(nrfx_spis_buffers_set(&spis, m_tx_buf_spi, m_tx_length, m_rx_buf_spi, BROADCASRLEN));
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "m_tx_buf_spi: %d\n", m_tx_buf_spi[3]);
              for (uint8_t i = 0; i < 40; i++)
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Transfer completed. Received[%d]: %X \n", i, m_rx_buf_spi[i]);
            }
            while (!spis_xfer_done)
            {
              //  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchor3\n");
                (void)sd_app_evt_wait();;
                bsp_board_led_invert(BSP_BOARD_LED_0);
            }
            //    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchor4\n");
            bsp_board_led_invert(BSP_BOARD_LED_1);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchorX3\n");
            
            /**
             * @brief Construct a new free object
             * No matter what happend, it should free m_tx_buf_spi
             */
            free(m_tx_buf_spi);
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchorX37\n");
            m_tx_buf_spi = NULL;
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchorX37B\n");
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "anchor3\n");
        }
    }
    

    The whole code here

  • Hi

    The "Transfer completed..." messages are printed using UART or RTT, and using the __log printing inside the spis_event_handler() will likely cause this jumbled data you're seeing. Please move these logging lines outside of the event handler

    Best regards,

    Simon

  • Hi Simon,

    I move the logging lines outside of the event handler. It still has the problem.

Related