2018年7月20日
nRF52832中使用SPI
只是最簡(jiǎn)單TWI初始化,沒有用SDK中提供的TWI transaction manager及TWI Sensor module.
SPI初始化比較簡(jiǎn)單,直接nrf_drv_spi_init函數(shù)初始化即可:
#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 )函數(shù)中,參數(shù)nrf_drv_spi_evt_handler_t handler如果為NULL,則SPI會(huì)運(yùn)行為阻塞模式,讀寫函數(shù)完成后才會(huì)返回值,如果不為NULL,則調(diào)用讀寫函數(shù)后會(huì)立刻返回,在回調(diào)函數(shù)中返回執(zhí)行結(jié)果
SPI讀寫SDK直接封裝好了接口函數(shù):nrf_drv_spi_start_task_get,很簡(jiǎn)單了,不多說(shuō)了.
__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的配置