美文网首页物联网loT从业者物联网相关技术研究
CC2640R2F学习笔记(22)——SPI接口使用

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

作者: Leung_ManWah | 来源:发表于2019-08-26 14:51 被阅读2次

    一、硬件连接

    功能口 引脚
    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 <ti/drivers/SPI.h>
    #include <ti/drivers/spi/SPICC26XXDMA.h>
    
    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文件夹下

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



    2.1 board_spi.c

    /*********************************************************************
     * INCLUDES
     */
    //#include <string.h>
    
    #include <ti/drivers/SPI.h>
    #include <ti/drivers/spi/SPICC26XXDMA.h>
    #include <ti/drivers/PIN.h>
    #include <ti/drivers/pin/PINCC26XX.h>
    
    #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初始化
    RC522_Init(); 
    

    3)写入数据

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

    4)查看示波器
    CLK

    MOSI


    • 由 Leung 写于 2019 年 8 月 26 日

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

    相关文章

      网友评论

        本文标题:CC2640R2F学习笔记(22)——SPI接口使用

        本文链接:https://www.haomeiwen.com/subject/sosrectx.html