1 /**@brief Function for initializing a SPI master driver. 2 * 3 * @param[in] p_instance Pointer to SPI master driver instance. 4 */ 5 static void spi_master_init(nrf_drv_spi_t const * p_instance) 6 { 7 uint32_t err_code = NRF_SUCCESS; 8 9 nrf_drv_spi_config_t config = 10 { 11 12 .irq_priority = APP_IRQ_PRIORITY_LOW, 13 .orc = 0x80, 14 .frequency = NRF_DRV_SPI_FREQ_1M, 15 .mode = NRF_DRV_SPI_MODE_0, 16 .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, 17 }; 18 19 #if (SPI0_ENABLED == 1) 20 if (p_instance == &m_spi_master_0) 21 { 22 config.ss_pin = SPIM0_SS_PIN, 23 config.sck_pin = SPIM0_SCK_PIN; 24 config.mosi_pin = SPIM0_MOSI_PIN; 25 config.miso_pin = SPIM0_MISO_PIN; 26 err_code = nrf_drv_spi_init(p_instance, 27 &config, 28 spi_master_0_event_handler); 29 } 30 else 31 #endif // (SPI0_ENABLED == 1) 32 33 APP_ERROR_CHECK(err_code); 34 }
1 uint8_t spi_write_reg(uint8_t ucRegAddr, uint8_t ucRegVal) 2 { 3 uint8_t buf[2]; 4 uint32_t err_code; 5 6 spi_xfer_done = false; 7 8 buf[0] = ucRegAddr<<1; 9 buf[1] = ucRegVal; 10 11 err_code = nrf_drv_spi_transfer(&m_spi_master_0, buf, 2, NULL, 0); 12 APP_ERROR_CHECK(err_code); 13 14 while (!spi_xfer_done) 15 { 16 __WFE(); 17 } 18 return true; 19 } 20 21 22 uint8_t spi_read_reg(uint8_t ucRegAddr) 23 { 24 uint8_t buf[2]; 25 uint8_t rx[2]; 26 uint32_t err_code; 27 28 spi_xfer_done = false; 29 30 buf[0] =( ucRegAddr<<1 ) | 0x80; 31 buf[1] = 0x00; 32 33 err_code = nrf_drv_spi_transfer(&m_spi_master_0,buf,2,rx,2); 34 APP_ERROR_CHECK(err_code); 35 36 while(!spi_xfer_done) 37 { 38 __WFE(); 39 } 40 41 return rx[1]; 42 }
现在我们对spi_write_reg(uint8_t ucRegAddr, uint8_t ucRegVal)进行测试
更据我们的代码在Saleae Logic 1.1.16C中进行设置如下图所示
什么看不懂上面的配置?请右转补课 http://www.cnblogs.com/libra13179/p/5740716.html
spi_write_reg(0x00,0x00)
spi_write_reg(0x01,0x02) 故意留下思考?为何下图显示是0X02而不是0x01,有什么作用呢?
时间: 2024-11-05 16:31:22