This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
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

Problem with nrf51822 in spi slave mode

Hello all

I making some device with ARM core CPU with nRF51822.

I set CPU to SPI master and set nRF51822 to slave. And then I read some data from nRF51822, that was always "0x55" or "0xAA". That depend on configuration of POL and PHA in master side.

Do you guess what is wrong?

B. Regards,

Parents
  • Dear Stefan Birnir Sverrisson

    I set 1 ms delay before and after CSN HIGH.

    Here is my using code in master and slave.

    Could you find something wrong in this?

    For Master (just pseudocode)

    spi_test()
    {
    spi_init();
    spi_cs_enable(); // LOW
    spi_write( 0x33, 1 );
    delay( 1ms );
    spi_cs_disable(); // HIGH
    delay( 1ms );
    
    spi_cs_enable();
    read = spi_read( 1 );
    delay( 1ms );
    spi_cs_disable();
    delay(1ms);
    }
    

    For slave

    static void spi_slave_event_handle(spi_slave_evt_t event)
    {
    uint32_t err_code;
    
    if (event.evt_type == SPI_SLAVE_XFER_DONE)
    { 
        led_toggle();
        
        //Check if buffer size is the same as amount of received data.
        APP_ERROR_CHECK_BOOL(event.rx_amount == RX_BUF_SIZE);
    
        // loop back
        for( i=0 ; i<event.rx_amount ; i++ )
        {
          m_tx_buf[i] = m_rx_buf[i];
        }
    
        //Set buffers.
        err_code = spi_slave_buffers_set(m_tx_buf, m_rx_buf, sizeof(m_tx_buf), sizeof(m_rx_buf));
        APP_ERROR_CHECK(err_code);          
    }
    }
    
  • My question is also, what data is in the m_tx_buf buffer. When spi_slave_event_handle is entered, the SPI transaction is finished and you have already sent the data in the m_tx_buf buffer to the SPI master. Your loopback function writes the data into the m_tx_buf buffer but that will only be sent to the master next time the SPI master clocks out a byte. So my guess is that if you clock out another byte, then you might get 0x33 back to the SPI master. Also, is the RX_BUF_SIZE == 1? If not, code execution will not exceed the error check APP_ERROR_CHECK_BOOL.

Reply
  • My question is also, what data is in the m_tx_buf buffer. When spi_slave_event_handle is entered, the SPI transaction is finished and you have already sent the data in the m_tx_buf buffer to the SPI master. Your loopback function writes the data into the m_tx_buf buffer but that will only be sent to the master next time the SPI master clocks out a byte. So my guess is that if you clock out another byte, then you might get 0x33 back to the SPI master. Also, is the RX_BUF_SIZE == 1? If not, code execution will not exceed the error check APP_ERROR_CHECK_BOOL.

Children
No Data
Related