CC2640R2F学习笔记(22)——SPI接口使用

一、硬件连接

功能口 引脚
MISO DIO_8
MOSI DIO_9
CLK DIO_10
CSN DIO_11

修改引脚配置: 例如 SDK2.4 multi_role工程 Startup目录下 board.c 跳转到 cc2640r2em_board.c

#if defined(CC2640R2DK_CXS) || defined (CC2640R2DK_4XS) \
      || defined (CC2640R2DK_5XD) ||  defined (CC2640R2DK_7ID)
    #include "./cc2640r2em/cc2640r2em_board.c"

芯片为5x5时,跳转 CC2640R2DK_5XD.c
芯片为7x7时,跳转 CC2640R2DK_7ID.c

#elif defined(CC2640R2DK_5XD)
    #include "../../boards/CC2640R2DK_5XD/Board.h"
    #include "../../boards/CC2640R2DK_5XD/CC2640R2DK_5XD.c"
#elif defined(CC2640R2DK_7ID)
    #include "../../boards/CC2640R2DK_7ID/Board.h"
    #include "../../boards/CC2640R2DK_7ID/CC2640R2DK_7ID.c"

找到 SPI 结构体

/*
 *  =============================== SPI DMA ===============================
 */
#include 
#include 

SPICC26XXDMA_Object spiCC26XXDMAObjects[CC2640R2DK_5XD_SPICOUNT];

const SPICC26XXDMA_HWAttrsV1 spiCC26XXDMAHWAttrs[CC2640R2DK_5XD_SPICOUNT] = {
    {
        .baseAddr           = SSI0_BASE,
        .intNum             = INT_SSI0_COMB,
        .intPriority        = ~0,
        .swiPriority        = 0,
        .powerMngrId        = PowerCC26XX_PERIPH_SSI0,
        .defaultTxBufValue  = 0,
        .rxChannelBitMask   = 1<<UDMA_CHAN_SSI0_RX,
        .txChannelBitMask   = 1<<UDMA_CHAN_SSI0_TX,
        .mosiPin            = CC2640R2DK_5XD_SPI0_MOSI,
        .misoPin            = CC2640R2DK_5XD_SPI0_MISO,
        .clkPin             = CC2640R2DK_5XD_SPI0_CLK,
        .csnPin             = CC2640R2DK_5XD_SPI0_CSN
    }
};

跳转修改宏 CC2640R2DK_5XD_SPI0_MOSI、CC2640R2DK_5XD_SPI0_MISO、CC2640R2DK_5XD_SPI0_CLK

/* SPI Board */
#define CC2640R2DK_5XD_SPI0_MISO                     IOID_8//IOID_12          /* P1.20 */
#define CC2640R2DK_5XD_SPI0_MOSI                     IOID_9//IOID_11          /* P1.18 */
#define CC2640R2DK_5XD_SPI0_CLK                      IOID_10         	/* P1.16 */
#define CC2640R2DK_5XD_SPI0_CSN                      PIN_UNASSIGNED

二、移植文件

链接:https://pan.baidu.com/s/1FyY_NgHTzJ5bI1oWxkr6iQ 提取码:cydx
board_spi.cboard_spi.h 两个文件拖拽至CCS工程的Application文件夹下
CC2640R2F学习笔记(22)——SPI接口使用_第1张图片

添加文件过程中,选项选择如下

2.1 board_spi.c

/*********************************************************************
 * INCLUDES
 */
//#include 

#include 
#include 
#include 
#include 

#include "board_spi.h"

/*********************************************************************
 * LOCAL VARIABLES
 */
// SPI句柄及参数
static SPI_Handle s_spiHandle;
static SPI_Params s_spiParams;
static SPI_Transaction s_spiTransaction;
// SPI片选端口
static PIN_State s_spiCsnState;
static PIN_Handle s_spiCsnHandle;
static PIN_Config s_spiCsnPinsCfg[] =
{
    BOARD_SPI0_CSN_IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MIN,
    PIN_TERMINATE
};

/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/**
 @brief SPI的初始化函数
 @param 无
 @return 无
*/
void SPI_Init(void)
{
    SPI_init();

    SPI_Params_init(&s_spiParams);
    s_spiParams.dataSize = 8;

    s_spiCsnHandle = PIN_open(&s_spiCsnState, s_spiCsnPinsCfg);

    s_spiHandle = SPI_open(Board_SPI0, &s_spiParams);
    if(s_spiHandle == NULL)
    {
        while(1);                                                   // SPI_open() failed
    }
}

/**
 @brief SPI片选信号设置
 @param pinId -[in] 引脚号
 @param pinState -[in] 引脚状态
 @return 无
*/
void SPI_CsnSet(PIN_Id pinId, bool pinState)
{
    if(pinState == BOARD_SPI_CS_OFF)
    {
        PIN_setOutputValue(s_spiCsnHandle, pinId, 1);
    }
    else if(pinState == BOARD_SPI_CS_ON)
    {
        PIN_setOutputValue(s_spiCsnHandle, pinId, 0);
    }
}

/**
 @brief SPI读出写入数据
 @param pWriteData -[in] 写入数据
 @param pReadData -[out] 读出数据
 @param writeDataLen -[in] 写入数据长度
 @return 无
*/
void SPI_ReadWriteData(uint8_t *pWriteData, uint8_t *pReadData, uint8_t writeDataLen)
{
    bool transferOk;
    s_spiTransaction.count = writeDataLen;                          // 写入的数据长度
    s_spiTransaction.arg = NULL;
    s_spiTransaction.txBuf = pWriteData;                            // 发送缓冲区
    s_spiTransaction.rxBuf = pReadData;                             // 接收缓冲区

    transferOk = SPI_transfer(s_spiHandle, &s_spiTransaction);      // 启动SPI传输
    if(!transferOk)
    {
                                                                    // Error in SPI or transfer already in progress.
    }
}

/*************************************END OF FILE*************************************/

2.2 board_spi.h

#ifndef _BOARD_SPI_H_
#define _BOARD_SPI_H_

/*********************************************************************
 * INCLUDES
 */
#include "board.h"

/*********************************************************************
 * DEFINITIONS
 */
#define BOARD_SPI_CS_ON             0
#define BOARD_SPI_CS_OFF            1

#define BOARD_SPI0_CSN_IO           IOID_11

#define SPI_CS_HIGH                 SPI_CsnSet(BOARD_SPI0_CSN_IO, BOARD_SPI_CS_OFF)
#define SPI_CS_LOW                  SPI_CsnSet(BOARD_SPI0_CSN_IO, BOARD_SPI_CS_ON)

/*********************************************************************
 * API FUNCTIONS
 */
void SPI_Init(void);
void SPI_CsnSet(PIN_Id pinId, bool pinState);
void SPI_ReadWriteData(uint8_t *pWriteData, uint8_t *pReadData, uint8_t writeDataLen);

#endif /* _BOARD_SPI_H_ */

三、API调用

需包含头文件 board_spi.h

SPI_Init

功能 初始化SPI驱动
函数定义 void SPI_Init(void)
参数
返回

SPI_CsnSet

功能 SPI片选信号设置
函数定义 void SPI_CsnSet(PIN_Id pinId, bool pinState)
参数 pinId:引脚号
pinState:引脚状态
返回

SPI_ReadWriteData

功能 SPI读出写入数据
函数定义 void SPI_ReadWriteData(uint8_t *pWriteData, uint8_t *pReadData, uint8_t writeDataLen)
参数 pWriteData:写入数据
pReadData:读出数据
writeDataLen:写入数据长度
返回

四、使用例子

1)添加头文件(例multi_role.c中)

#include "board_spi.h"

2)添加初始化代码(multi_role.c的multi_role_init函数末尾中)

// SPI初始化
SPI_Init(); 

3)写入数据

uint8_t temp[1] = {0x01};
SPI_ReadWriteData(temp, NULL, sizeof(uint8_t));

4)查看示波器
CLK
CC2640R2F学习笔记(22)——SPI接口使用_第2张图片

MOSI
CC2640R2F学习笔记(22)——SPI接口使用_第3张图片


• 由 Leung 写于 2019 年 8 月 26 日

• 参考:CC2640R2F BLE5.0 CC2640R2F SPI驱动实现

你可能感兴趣的:(CC2640R2F,CC2640R2F,BLE,SPI)