1.目的
nrf51822 spi通信
2.分析
在实际应用中经常要用到spi通信,比如度flash
3.平台:
协议栈版本:SDK10.0.0
编译软件:keil 5.12
硬件平台:微雪开发板nrf51822
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_hrs\pca10028\s110\arm4
4.步骤
1.打开\SDK10.0\components\drivers_nrf\twi_master\deprecated目录 2.把nrf_drv_spi.c文件加入到工程里面。并设置路径
3.在main.c里面添加头文件
#include "nrf_drv_config.h" //20160419
#include "nrf_drv_spi.h"
在主函数里面初始化代码
uint8_t Tx_Buf[6]={0x90,0x00,0x00,0x00,0xff,0xff},Rx_Buf[6];
/*********************************************/
static const nrf_drv_spi_t m_spi_master_0 = NRF_DRV_SPI_INSTANCE(0);
nrf_drv_spi_config_t config = NRF_DRV_SPI_DEFAULT_CONFIG(0);
err_code = nrf_drv_spi_init(&m_spi_master_0, &config, NULL); // NULL event handler = blocking mode
if (err_code != NRF_SUCCESS)
{
return -1;
}
#define SPI_CS_PIN 5
nrf_gpio_cfg_output(SPI_CS_PIN);
nrf_gpio_pin_clear(SPI_CS_PIN);
nrf_drv_spi_transfer(&m_spi_master_0,Tx_Buf,sizeof(Tx_Buf),Rx_Buf,sizeof(Rx_Buf));
nrf_gpio_pin_set(SPI_CS_PIN);
nrf_drv_spi_config_t config = NRF_DRV_SPI_DEFAULT_CONFIG(0); //定义如下面
/**
* @brief SPI master instance default configuration.
*/
#define NRF_DRV_SPI_DEFAULT_CONFIG(id) \
{ \
.sck_pin = CONCAT_3(SPI, id, _CONFIG_SCK_PIN), \ //配置SCK
.mosi_pin = CONCAT_3(SPI, id, _CONFIG_MOSI_PIN), \ //配置MOSI
.miso_pin = CONCAT_3(SPI, id, _CONFIG_MISO_PIN), \ //配置MISO
.ss_pin = NRF_DRV_SPI_PIN_NOT_USED, \ //芯片使能脚 没有使用
.irq_priority = CONCAT_3(SPI, id, _CONFIG_IRQ_PRIORITY), \ //SPI中断优先级
.orc = 0xFF, \ //
.frequency = NRF_DRV_SPI_FREQ_1M, \ //spi时钟频率
.mode = NRF_DRV_SPI_MODE_0, \ //spi的模式 (相位 极性选择)
.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, \ //传送哪位在前
}
/* SPI */
#define SPI0_ENABLED 1 //必须开启这个选择SPI
#if (SPI0_ENABLED == 1)
#define SPI0_USE_EASY_DMA 0
//管脚选择
#define SPI0_CONFIG_SCK_PIN 3
#define SPI0_CONFIG_MOSI_PIN 4
#define SPI0_CONFIG_MISO_PIN 6
#define SPI0_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
0x90是芯片ID的寄存器(具体查看w25x40的datasheet)
仿真结果如下:
结果一样 所以 硬件spi调试成功。。