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

Why my SPIM can not work?

Hi:

I used nrf52840 to drive a 240 * 240 LCD screen, when i use SPI, it's work normal. 

#define SPI_INSTANCE  0 /**< SPI instance index. */
static const nrf_drv_spi_t lcd_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 uint8_t       m_rx_buf[1];    /**< RX buffer. */

static void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void *  p_context);

ret_code_t lcd_spi_init(void)
{
    nrf_drv_spi_config_t const spi_config =
    {
        .sck_pin        = LCD_SCL,
        .mosi_pin       = LCD_SDA,
        .miso_pin       = NRF_DRV_SPI_PIN_NOT_USED,
        .ss_pin         = LCD_CS,
        .irq_priority   = APP_IRQ_PRIORITY_LOWEST,
        .orc            = 0xFF,
        .frequency      = NRF_DRV_SPI_FREQ_8M,
        .mode           = NRF_DRV_SPI_MODE_0,
        .bit_order      = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST
    };
    return nrf_drv_spi_init(&lcd_spi, &spi_config, NULL, NULL);
}

void lcd_spi_uninit(void)
{
        nrf_drv_spi_uninit(&lcd_spi);
}

/**
 * @brief SPI user event handler.
 * @param event
 */
static void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void *  p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO("Transfer completed.");
}

void lcd_spi_write(uint8_t* m_tx_buf, uint32_t m_length)
{
        while(true)
        {
            if(m_length <= 255)
            {
                nrf_drv_spi_transfer(&lcd_spi, m_tx_buf, m_length, m_rx_buf, 0);
                break;
            }
            else
            {
                nrf_drv_spi_transfer(&lcd_spi, m_tx_buf, 255, m_rx_buf, 0);
                m_tx_buf += 255;
                m_length -= 255;
            }
        }
}

when i used SPIM, it's work wrong.

#define SPI_INSTANCE  3 /**< SPI instance index. */
static const nrfx_spim_t lcd_spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
static uint8_t       m_rx_buf[1];    /**< RX buffer. */
static nrfx_spim_xfer_desc_t driver_spim_xfer;

static void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void *  p_context);
void spi_event_handler_m(nrfx_spim_evt_t const * p_event, void * p_context);//中断函数


ret_code_t lcd_spi_init(void)
{
    nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
    spi_config.frequency      = NRF_SPIM_FREQ_8M;
    spi_config.ss_pin         = NRFX_SPIM_PIN_NOT_USED;
    spi_config.miso_pin       = NRFX_SPIM_PIN_NOT_USED;
    spi_config.mosi_pin       = LCD_SDA;
    spi_config.sck_pin        = LCD_SCL;
    spi_config.dcx_pin        = NRFX_SPIM_PIN_NOT_USED;
    spi_config.orc            = 0xFF;
    spi_config.bit_order      = NRF_SPIM_BIT_ORDER_MSB_FIRST;//高位在前
    spi_config.mode           = NRF_SPIM_MODE_0;
    spi_config.ss_active_high = false;
    return nrfx_spim_init(&lcd_spi, &spi_config, spi_event_handler_m, NULL);
}


void spi_event_handler_m(nrfx_spim_evt_t const * p_event, void * p_context)//中断函数
{
    spi_xfer_done = true;
}


void lcd_spi_write(uint8_t* m_tx_buf, uint32_t m_length)
{       
        while(true)
        {
            if(m_length <= 255)
            {
                driver_spim_xfer.tx_length   = m_length;
                driver_spim_xfer.p_tx_buffer = m_tx_buf;
                driver_spim_xfer.rx_length   = 0;
                driver_spim_xfer.p_rx_buffer = m_rx_buf;
               
                spi_xfer_done = false;
                nrfx_spim_xfer(&lcd_spi, &driver_spim_xfer, 0);
                while(!spi_xfer_done)
                {
                        __WFE();
                }

                break;
            }
            else
            {
                driver_spim_xfer.tx_length   = 255;
                driver_spim_xfer.p_tx_buffer = m_tx_buf;
                driver_spim_xfer.rx_length   = 0;
                driver_spim_xfer.p_rx_buffer = m_rx_buf;
                
                spi_xfer_done = false;
                nrfx_spim_xfer(&lcd_spi, &driver_spim_xfer, 0);
               while(!spi_xfer_done)
                {
                        __WFE();
                }
                        
                m_tx_buf += 255;
                m_length -= 255;
            }
        }
}

Did I do something wrong?

Related