nRF52832 SPI使用

2018年7月20日

nRF52832中使用SPI
只是最简单TWI初始化,没有用SDK中提供的TWI transaction managerTWI Sensor module.

SPI初始化比较简单,直接nrf_drv_spi_init函数初始化即可:

#define SPI_INSTANCE  1 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
   nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = NRF_DRV_SPI_PIN_NOT_USED;
    spi_config.miso_pin = NRF_DRV_SPI_PIN_NOT_USED;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin  = SPI_SCK_PIN;
    

    spi_config.frequency=NRF_DRV_SPI_FREQ_8M;  //速度
    spi_config.mode=NRF_DRV_SPI_MODE_2;          //模式

    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config,NULL /*spi_event_handler*/, NULL));

需要注意的是,ret_code_t nrf_drv_spi_init ( nrf_drv_spi_t const *const p_instance,nrf_drv_spi_config_t const * p_config,nrf_drv_spi_evt_handler_t handler,void * p_context )函数中,参数nrf_drv_spi_evt_handler_t handler如果为NULL,则SPI会运行为阻塞模式,读写函数完成后才会返回值,如果不为NULL,则调用读写函数后会立刻返回,在回调函数中返回执行结果

SPI读写SDK直接封装好了接口函数:nrf_drv_spi_start_task_get,很简单了,不多说了.

__STATIC_INLINE ret_code_t nrf_drv_spi_transfer (   nrf_drv_spi_t const *const  p_instance,uint8_t const *  p_tx_buffer,uint8_t     tx_buffer_length,uint8_t *  p_rx_buffer,uint8_t     rx_buffer_length )

[in]    p_instance  Pointer to the driver instance structure.
[in]    p_tx_buffer Pointer to the transmit buffer. Can be NULL if there is nothing to send.
        tx_buffer_length    Length of the transmit buffer.
[in]    p_rx_buffer Pointer to the receive buffer. Can be NULL if there is nothing to receive.
        rx_buffer_length    Length of the receive buffer.

注意sdk_config.h的配置

你可能感兴趣的:(nRF52832 SPI使用)