CC2640R2F ble蓝牙 I2C解析


协议栈:simplelink_cc2640r2_sdk_1_40_00_45

-----------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------

 *  | Generic API Function | API Function             | Description                                                     |
 *  |---------------------  |------------------------- |---------------------------------------------------|
 *  | I2C_init()                 | I2CCC26XX_init()         | Initialize I2C driver                                             |
 *  | I2C_open()              | I2CCC26XX_open()       | Initialize I2C HW and set system dependencies     |
 *  | I2C_close()              | I2CCC26XX_close()       | Disable I2C HW and release system dependencies    |
 *  | I2C_transfer()         | I2CCC26XX_transfer()    | Start I2C transfer         

--------------------------------------------------------------------------------I2C.h

typedef struct I2C_Config_    *I2C_Handle;

typedef struct I2C_Transaction_ {
    void    *writeBuf;    /*!< Buffer containing data to be written */
    size_t   writeCount;  /*!< Number of bytes to be written to the slave */
    void    *readBuf;     /*!< Buffer to which data is to be read into */
    size_t   readCount;   /*!< Number of bytes to be read from the slave */
    uint_least8_t slaveAddress; /*!< Address of the I2C slave peripheral */
    void    *arg;         /*!< Argument to be passed to the callback function */
    void    *nextPtr;     /*!< Used for queuing in I2C_MODE_CALLBACK mode */

I2C_Transaction;

typedef enum I2C_TransferMode_ {
    I2C_MODE_BLOCKING,  /*!< I2C_transfer() blocks execution */
    I2C_MODE_CALLBACK   /*!< I2C_transfer() does not block */

} I2C_TransferMode;

typedef void (*I2C_CallbackFxn)(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus);

typedef enum I2C_BitRate_ {
    I2C_100kHz = 0,
    I2C_400kHz = 1
} I2C_BitRate;

typedef struct I2C_Params_ {
    I2C_TransferMode transferMode;        /*!< Blocking or Callback mode */
    I2C_CallbackFxn  transferCallbackFxn; /*!< Callback function pointer */
    I2C_BitRate      bitRate;                       /*!< I2C bus bit rate */
    void            *custom;                       /*!< Custom argument used by driver implementation */

I2C_Params;

typedef struct I2C_FxnTable_ {
    /*! Cancel all I2C data transfers */
    I2C_CancelFxn   cancelFxn;
    /*! Close the specified peripheral */
    I2C_CloseFxn    closeFxn;
    /*! Implementation-specific control function */
    I2C_ControlFxn  controlFxn;
    /*! Initialize the given data object */
    I2C_InitFxn     initFxn;
    /*! Open the specified peripheral */
    I2C_OpenFxn     openFxn;
    /*! Initiate an I2C data transfer */
    I2C_TransferFxn transferFxn;

I2C_FxnTable;

typedef struct I2C_Config_ {
    /*! Pointer to a table of driver-specific implementations of I2C APIs */
    I2C_FxnTable const *fxnTablePtr;
    /*! Pointer to a driver-specific data object */
    void               *object;
    /*! Pointer to a driver-specific hardware attributes structure */
    void         const *hwAttrs;
I2C_Config;

--------------------------------------------------------------------------------I2C.c

/* Default I2C parameters structure */
const I2C_Params I2C_defaultParams = {
    I2C_MODE_BLOCKING,  /* transferMode */
    NULL,               /* transferCallbackFxn */
    I2C_100kHz,         /* bitRate */
    NULL                /* custom */
};

void I2C_cancel(I2C_Handle handle)

void I2C_close(I2C_Handle handle)

int_fast16_t I2C_control(I2C_Handle handle, uint_fast16_t cmd, void *controlArg)

void I2C_init(void)

I2C_Handle I2C_open(uint_least8_t index, I2C_Params *params)

void I2C_Params_init(I2C_Params *params)

bool I2C_transfer(I2C_Handle handle, I2C_Transaction *transaction)

--------------------------------------------------------------------------------I2CCC2640XX.h

typedef struct I2CCC26XX_I2CPinCfg {
    uint8_t pinSDA;
    uint8_t pinSCL;

} I2CCC26XX_I2CPinCfg;

typedef enum I2CCC26XX_Mode {
    I2CCC26XX_IDLE_MODE = 0,  /* I2C is not performing a transaction */
    I2CCC26XX_WRITE_MODE,     /* I2C is currently performing write operations */
    I2CCC26XX_READ_MODE,      /* I2C is currently performing read operations */
    I2CCC26XX_BUSBUSY_MODE,   /* I2C Bus is currently busy */
    I2CCC26XX_ERROR = 0xFF    /* I2C error has occurred, exit gracefully */

} I2CCC26XX_Mode;

typedef struct I2CCC26XX_HWAttrsV1 {
    I2CBaseAddrType     baseAddr;     /*! I2C peripheral's base address */
    unsigned long       powerMngrId;  /*! I2C peripheral's Power driver ID */
    int                 intNum;   /*! I2C peripheral's interrupt number */

    /*! @brief I2C Peripheral's interrupt priority. The CC26xx uses three of the priority bits,  meaning 

      ~0 has the same effect as (7 << 5).

        (7 << 5) will apply the lowest priority.
        (1 << 5) will apply the highest priority.
        Setting the priority to 0 is not supported by this driver.
       Hwi's with priority 0 ignore the Hwi dispatcher to support zero-latency interrupts, thus invalidating the critical sections in this driver.*/
    uint8_t             intPriority;
    /*! @brief I2C Swi priority.The higher the number, the higher the priority.The minimum is 0 and the maximum is 15 by default. The maximum can be reduced to save RAM by adding or modifying Swi.numPriorities in the kernel configuration file. */
    uint32_t            swiPriority;
    uint8_t             sdaPin; /*! I2C SDA pin mapping */
    uint8_t             sclPin;/*! I2C SCL pin mapping */

} I2CCC26XX_HWAttrsV1;

typedef struct I2CCC26XX_Object {
    /* I2C control variables */
    I2C_TransferMode    transferMode;        /*!< Blocking or Callback mode */
    I2C_CallbackFxn     transferCallbackFxn; /*!< Callback function pointer */
    volatile I2CCC26XX_Mode mode;            /*!< Stores the I2C state */
    uint32_t            bitRate;             /*!< Bitrate of the I2C module */
    /* I2C SYS/BIOS objects */
    HwiP_Struct hwi;/*!< Hwi object handle */
    SwiP_Struct          swi;                /*!< Swi object */
    SemaphoreP_Struct    mutex;              /*!< Grants exclusive access to I2C */
    SemaphoreP_Struct    transferComplete;   /*!< Signal I2C transfer complete */
    /* PIN driver state object and handle */
    PIN_State           pinState;
    PIN_Handle          hPin;
    /* I2C current transaction */
    I2C_Transaction     *currentTransaction; /*!< Ptr to current I2C transaction */
    uint8_t             *writeBufIdx;        /*!< Internal inc. writeBuf index */
    unsigned int        writeCountIdx;       /*!< Internal dec. writeCounter */
    uint8_t             *readBufIdx;         /*!< Internal inc. readBuf index */
    unsigned int        readCountIdx;        /*!< Internal dec. readCounter */
    /* I2C transaction pointers for I2C_MODE_CALLBACK */
    I2C_Transaction     *headPtr;            /*!< Head ptr for queued transactions */
    I2C_Transaction     *tailPtr;            /*!< Tail ptr for queued transactions */
    /* I2C power notification */
    void                *i2cPostFxn;        /*!< I2C post-notification Function pointer */
    Power_NotifyObj     i2cPostObj;         /*!< I2C post-notification object */
    bool                isOpen;             /*!< flag to indicate module is open */
} I2CCC26XX_Object;

--------------------------------------------------------------------------------I2CCC2640XX.c

/* I2C function table for I2CCC26XX implementation */
const I2C_FxnTable I2CCC26XX_fxnTable = {
    I2CCC26XX_cancel,  //  void I2CCC26XX_cancel(I2C_Handle handle)
    I2CCC26XX_close,   //  void I2CCC26XX_close(I2C_Handle handle)
    I2CCC26XX_control, //  int_fast16_t I2CCC26XX_control(I2C_Handle handle, uint_fast16_t cmd, void *arg)
    I2CCC26XX_init,     //  void I2CCC26XX_init(I2C_Handle handle)
    I2CCC26XX_open,    //  I2C_Handle I2CCC26XX_open(I2C_Handle handle, I2C_Params *params)
    I2CCC26XX_transfer //  bool I2CCC26XX_transfer(I2C_Handle handle, I2C_Transaction *transaction)

};

void         I2CCC26XX_init(I2C_Handle handle);
I2C_Handle   I2CCC26XX_open(I2C_Handle handle, I2C_Params *params);
bool         I2CCC26XX_transfer(I2C_Handle handle, I2C_Transaction *transaction);
void         I2CCC26XX_cancel(I2C_Handle handle);
void         I2CCC26XX_close(I2C_Handle handle);

int_fast16_t I2CCC26XX_control(I2C_Handle handle, uint_fast16_t cmd, void *arg);

==============================硬件平台特性========================

--------------------------------------------------------------------------------CC2640R2_LAUNCHXL.h

typedef enum CC2640R2_LAUNCHXL_I2CName {
    CC2640R2_LAUNCHXL_I2C0 = 0,
    CC2640R2_LAUNCHXL_I2CCOUNT

} CC2640R2_LAUNCHXL_I2CName;

/* I2C */
#define CC2640R2_LAUNCHXL_I2C0_SCL0              IOID_4
#define CC2640R2_LAUNCHXL_I2C0_SDA0             IOID_5

--------------------------------------------------------------------------------CC2640R2_LAUNCHXL.c

I2CCC26XX_Object i2cCC26xxObjects[CC2640R2_LAUNCHXL_I2CCOUNT];

const I2CCC26XX_HWAttrsV1 i2cCC26xxHWAttrs[CC2640R2_LAUNCHXL_I2CCOUNT] = {
    {
        .baseAddr    = I2C0_BASE,
        .powerMngrId = PowerCC26XX_PERIPH_I2C0,
        .intNum      = INT_I2C_IRQ,
        .intPriority = ~0,
        .swiPriority = 0,
        .sdaPin      = CC2640R2_LAUNCHXL_I2C0_SDA0,
        .sclPin      = CC2640R2_LAUNCHXL_I2C0_SCL0,
    }

};

const I2C_Config I2C_config[CC2640R2_LAUNCHXL_I2CCOUNT] = {
    {
        .fxnTablePtr = &I2CCC26XX_fxnTable,
        .object      = &i2cCC26xxObjects[CC2640R2_LAUNCHXL_I2C0],
        .hwAttrs     = &i2cCC26xxHWAttrs[CC2640R2_LAUNCHXL_I2C0]
    },
};

const uint_least8_t I2C_count = CC2640R2_LAUNCHXL_I2CCOUNT;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值