基于CC2640的SPI通讯

软件开发平台:IAR

硬件开发平台:CC2640 board

操作系统:TI-RTOS SYS/BIOS

工程基础:SimpleBLEPeripheral

 目的:实现CC2640的SPI接口在阻塞和回调两种模式下的通讯

具体步骤:

1. 定义硬件引脚:

//SPI Board
#define Board_SPI0_MISO IOID_0
#define Board_SPI0_MOSI IOID_2
#define Board_SPI0_CLK IOID_1
#define Board_SPI0_CSN IOID_3 

2. 可新建一个.c文件SPIDriver.c,阻塞模式,添加代码:

PIN_Config SPICSN_GpioInitTable[] = {
Board_SPI0_CSN      | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW| PIN_PUSHPULL | PIN_DRVSTR_MIN,  
PIN_TERMINATE  
};
PIN_Handle SPICSN_Pin_Handle = NULL;
PIN_State  SPICSN_Pin_State;
static SPI_Handle spiHandle = NULL;
static SPI_Params spiParams;

void SPI_Initialize(void) //SPI阻塞模式的初始化函数
{
if(SPICSN_Pin_Handle == NULL)
{
SPICSN_Pin_Handle = PIN_open(&SPICSN_Pin_State, SPICSN_GpioInitTable);
}
SPI_Params_init(&spiParams);
    spiParams.bitRate = 4000000;
spiParams.mode = SPI_MASTER;
     spiParams.transferMode = SPI_MODE_BLOCKING;
// spiParams.frameFormat = SPI_POL1_PHA1;//总线空闲时的极性
spiHandle = SPI_open(Board_SPI0, &spiParams);
if (spiHandle == NULL)
    {
      Task_exit();
    }
}

void bspSpiClose(void) //关闭SPI函数
{
  if (spiHandle != NULL)
  {
    SPI_close(spiHandle);
    spiHandle = NULL;
  }
}

void bspSpiFlush(void) //这个函数是监控SPI硬件接口已完成相应动作
{
    /* make sure SPI hardware module is done  */
    while(SSIBusy(((SPICC26XX_HWAttrs*)spiHandle->hwAttrs)->baseAddr))
    { };
}

uint8_t bspSpiWriteRead(uint8_t *txBuf, uint8_t *rxBuf, uint8_t wlen)//SPI写/读函数
{
  SPI_Transaction masterTransaction;
  bool success;
  masterTransaction.count  = wlen;  //所要写的数据长度,字节单位
  masterTransaction.txBuf  = txBuf; //所要被写数据的地址
  masterTransaction.rxBuf  = rxBuf; //接收数据所存放的地址
  success = SPI_transfer(spiHandle, &masterTransaction);
  return success;
}

3.回调Callback模式下的SPI与阻塞模式只在初始化部分有点区别,另外需要增加个回调函数,以便处理相应代码:

void SPI_Initialize(void)//SPI Callback模式的初始化函数
{
if(SPICSN_Pin_Handle == NULL)
{
SPICSN_Pin_Handle = PIN_open(&SPICSN_Pin_State, SPICSN_GpioInitTable);
}
SPI_Params_init(&spiParams);
    spiParams.bitRate = 4000000;
spiParams.mode = SPI_MASTER;
spiParams.transferMode = SPI_MODE_CALLBACK;
spiParams.transferCallbackFxn = transferCallback;
 //回调函数,(后面有定义)
//spiParams.frameFormat = SPI_POL1_PHA1;//总线空闲时的极性
spiHandle = SPI_open(Board_SPI0, &spiParams);
if (spiHandle == NULL)
    {
      Task_exit();
    }
}

static void transferCallback(SPI_Handle handle, SPI_Transaction *transaction)
{
//这里添加回调后的相应处理代码
}

其他函数相同。

你可能感兴趣的:(基于CC2640的SPI通讯)