3768|1

1万

帖子

28

TA的资源

裸片初长成(高级)

楼主
 

CC3200的I2C [复制链接]

CC3200的I2C
一、I2C功能
CC3200的I2C有四种模式:
主机发送;
主机接收;
从机发送;
从机接收。
它可以支持同时的主机和从机操作。
I2C支持的速度有两种:
标准模式 ------ 100Kbps
快速模式-------- 400Kbps

中断:
主机中断:发送或接收完成中断,
从机中断:发送给主机或从主机处接收完成中断,或检测到开始或停止条件中断。
使用DMA:
使用分离的通道进行发送或接收
使用FIFO的单个或突发传输。

CC3200的I2C寄存器:


二、TI例程对I2C的操作
1、PinMuxConfig()函数。设置端口功能
其主要有:
     // Enable Peripheral Clocks
   MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
    // Configure PIN_01 for I2C0 I2C_SCL
    MAP_PinTypeI2C(PIN_01, PIN_MODE_1);

    // Configure PIN_02 for I2C0 I2C_SDA
MAP_PinTypeI2C(PIN_02, PIN_MODE_1);

2、打开I2C模块

//! Enables and configures the I2C peripheral
int I2C_IF_Open(unsigned long ulMode)
{
    // Enable I2C Peripheral
    //MAP_HwSemaphoreLock(HWSEM_I2C, HWSEM_WAIT_FOR_EVER);
    PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
    PRCMPeripheralReset(PRCM_I2CA0);

    // Configure I2C module in the specified mode
    switch(ulMode)
    {
        case I2C_MASTER_MODE_STD:       /* 100000 */
            I2CMasterInitExpClk(I2C_BASE,SYS_CLK,false);
            break;

        case I2C_MASTER_MODE_FST:       /* 400000 */
            I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true);
            break;

        default:
            I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true);
            break;
    }
    // Disables the multi-master mode
    //MAP_I2CMasterDisable(I2C_BASE);
   
    return SUCCESS;
}

3、I2C读写
int I2C_IF_Read(unsigned char ucDevAddr,
        unsigned char *pucData,
        unsigned char ucLen)
{
    unsigned long ulCmdID;

    RETERR_IF_TRUE(pucData == NULL);
    RETERR_IF_TRUE(ucLen == 0);

    // Set I2C codec slave address
    MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, true);

    // Check if its a single receive or burst receive
    if(ucLen == 1)
    {
        // Configure for a single receive
        ulCmdID = I2C_MASTER_CMD_SINGLE_RECEIVE;
    }
    else
    {
        // Initiate a burst receive sequence
        ulCmdID = I2C_MASTER_CMD_BURST_RECEIVE_START;
    }
    // Initiate the transfer.
    RET_IF_ERR(I2CTransact(ulCmdID));

    // Decrement the count and increment the data pointer
    // to facilitate the next transfer
    ucLen--;

    // Loop until the completion of reception or error
    while(ucLen)
    {
        // Receive the byte over I2C
        *pucData = MAP_I2CMasterDataGet(I2C_BASE);

        // Decrement the count and increment the data pointer
        // to facilitate the next transfer
        ucLen--;
        pucData++;
        if(ucLen)
        {
            // Continue the reception
            RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_CONT));
        }
        else
        {
            // Complete the last reception
            RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_FINISH));
        }
    }

    // Receive the byte over I2C
    *pucData = MAP_I2CMasterDataGet(I2C_BASE);

    return SUCCESS;
}


int I2C_IF_Write(unsigned char ucDevAddr,
         unsigned char *pucData,
         unsigned char ucLen,
         unsigned char ucStop)
{
    RETERR_IF_TRUE(pucData == NULL);
    RETERR_IF_TRUE(ucLen == 0);

    // Set I2C codec slave address
    MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, false);

    // Write the first byte to the controller.
    MAP_I2CMasterDataPut(I2C_BASE, *pucData);

    // Initiate the transfer.
    RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_START));

    // Decrement the count and increment the data pointer
    // to facilitate the next transfer
    ucLen--;
    pucData++;

    // Loop until the completion of transfer or error
    while(ucLen)
    {
        // Write the next byte of data
        MAP_I2CMasterDataPut(I2C_BASE, *pucData);

        // Transact over I2C to send byte
        RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_CONT));

        // Decrement the count and increment the data pointer
        // to facilitate the next transfer
        ucLen--;
        pucData++;
    }

    // If stop bit is to be sent, send it.
    if(ucStop == true)
    {
        RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_STOP));
    }
    return SUCCESS;
}

4、关闭I2C
int I2C_IF_Close()
{
    //
    // Power OFF the I2C peripheral
    //
    MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);

    return SUCCESS;
}


此帖出自RF/无线论坛

最新回复

这样是不是代码好看点 1、PinMuxConfig()函数。设置端口功能、PinMuxConfig()函数。设置端口功能 其主要有: // Enable Peripheral Clocks MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK); // Configure PIN_01 for I2C0 I2C_SCL MAP_PinTypeI2C(PIN_01, PIN_MODE_1); // Configure PIN_02 for I2C0 I2C_SDA MAP_PinTypeI2C(PIN_02, PIN_MODE_1); 2、打开I2C模块 //! Enables and configures the I2C peripheral int I2C_IF_Open(unsigned long ulMode) { // Enable I2C Peripheral //MAP_HwSemaphoreLock(HWSEM_I2C, HWSEM_WAIT_FOR_EVER); PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK); PRCMPeripheralReset(PRCM_I2CA0); // Configure I2C module in the specified mode switch(ulMode) { case I2C_MASTER_MODE_STD: /* 100000 */ I2CMasterInitExpClk(I2C_BASE,SYS_CLK,false); break; case I2C_MASTER_MODE_FST: /* 400000 */ I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true); break; default: I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true); break; } // Disables the multi-master mode //MAP_I2CMasterDisable(I2C_BASE); return SUCCESS; } 3、I2C读写 int I2C_IF_Read(unsigned char ucDevAddr, unsigned char *pucData, unsigned char ucLen) { unsigned long ulCmdID; RETERR_IF_TRUE(pucData == NULL); RETERR_IF_TRUE(ucLen == 0); // Set I2C codec slave address MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, true); // Check if its a single receive or burst receive if(ucLen == 1) { // Configure for a single receive ulCmdID = I2C_MASTER_CMD_SINGLE_RECEIVE; } else { // Initiate a burst receive sequence ulCmdID = I2C_MASTER_CMD_BURST_RECEIVE_START; } // Initiate the transfer. RET_IF_ERR(I2CTransact(ulCmdID)); // Decrement the count and increment the data pointer // to facilitate the next transfer ucLen--; // Loop until the completion of reception or error while(ucLen) { // Receive the byte over I2C *pucData = MAP_I2CMasterDataGet(I2C_BASE); // Decrement the count and increment the data pointer // to facilitate the next transfer ucLen--; pucData++; if(ucLen) { // Continue the reception RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_CONT)); } else { // Complete the last reception RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_FINISH)); } } // Receive the byte over I2C *pucData = MAP_I2CMasterDataGet(I2C_BASE); return SUCCESS; } int I2C_IF_Write(unsigned char ucDevAddr, unsigned char *pucData, unsigned char ucLen, unsigned char ucStop) { RETERR_IF_TRUE(pucData == NULL); RETERR_IF_TRUE(ucLen == 0); // Set I2C codec slave address MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, false); // Write the first byte to the controller. MAP_I2CMasterDataPut(I2C_BASE, *pucData); // Initiate the transfer. RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_START)); // Decrement the count and increment the data pointer // to facilitate the next transfer ucLen--; pucData++; // Loop until the completion of transfer or error while(ucLen) { // Write the next byte of data MAP_I2CMasterDataPut(I2C_BASE, *pucData); // Transact over I2C to send byte RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_CONT)); // Decrement the count and increment the data pointer // to facilitate the next transfer ucLen--; pucData++; } // If stop bit is to be sent, send it. if(ucStop == true) { RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_STOP)); } return SUCCESS; } 4、关闭I2C int I2C_IF_Close() { // // Power OFF the I2C peripheral // MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK); return SUCCESS; } 复制代码   详情 回复 发表于 2016-10-2 11:44
点赞 关注
 

回复
举报

2774

帖子

8

TA的资源

版主

沙发
 
这样是不是代码好看点
  1. 1、PinMuxConfig()函数。设置端口功能、PinMuxConfig()函数。设置端口功能
  2. 其主要有:
  3. // Enable Peripheral Clocks
  4. MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
  5. // Configure PIN_01 for I2C0 I2C_SCL
  6. MAP_PinTypeI2C(PIN_01, PIN_MODE_1);
  7. // Configure PIN_02 for I2C0 I2C_SDA
  8. MAP_PinTypeI2C(PIN_02, PIN_MODE_1);
  9. 2、打开I2C模块
  10. //! Enables and configures the I2C peripheral
  11. int I2C_IF_Open(unsigned long ulMode)
  12. {
  13. // Enable I2C Peripheral
  14. //MAP_HwSemaphoreLock(HWSEM_I2C, HWSEM_WAIT_FOR_EVER);
  15. PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
  16. PRCMPeripheralReset(PRCM_I2CA0);
  17. // Configure I2C module in the specified mode
  18. switch(ulMode)
  19. {
  20. case I2C_MASTER_MODE_STD: /* 100000 */
  21. I2CMasterInitExpClk(I2C_BASE,SYS_CLK,false);
  22. break;
  23. case I2C_MASTER_MODE_FST: /* 400000 */
  24. I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true);
  25. break;
  26. default:
  27. I2CMasterInitExpClk(I2C_BASE,SYS_CLK,true);
  28. break;
  29. }
  30. // Disables the multi-master mode
  31. //MAP_I2CMasterDisable(I2C_BASE);
  32. return SUCCESS;
  33. }
  34. 3、I2C读写
  35. int I2C_IF_Read(unsigned char ucDevAddr,
  36. unsigned char *pucData,
  37. unsigned char ucLen)
  38. {
  39. unsigned long ulCmdID;
  40. RETERR_IF_TRUE(pucData == NULL);
  41. RETERR_IF_TRUE(ucLen == 0);
  42. // Set I2C codec slave address
  43. MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, true);
  44. // Check if its a single receive or burst receive
  45. if(ucLen == 1)
  46. {
  47. // Configure for a single receive
  48. ulCmdID = I2C_MASTER_CMD_SINGLE_RECEIVE;
  49. }
  50. else
  51. {
  52. // Initiate a burst receive sequence
  53. ulCmdID = I2C_MASTER_CMD_BURST_RECEIVE_START;
  54. }
  55. // Initiate the transfer.
  56. RET_IF_ERR(I2CTransact(ulCmdID));
  57. // Decrement the count and increment the data pointer
  58. // to facilitate the next transfer
  59. ucLen--;
  60. // Loop until the completion of reception or error
  61. while(ucLen)
  62. {
  63. // Receive the byte over I2C
  64. *pucData = MAP_I2CMasterDataGet(I2C_BASE);
  65. // Decrement the count and increment the data pointer
  66. // to facilitate the next transfer
  67. ucLen--;
  68. pucData++;
  69. if(ucLen)
  70. {
  71. // Continue the reception
  72. RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_CONT));
  73. }
  74. else
  75. {
  76. // Complete the last reception
  77. RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_RECEIVE_FINISH));
  78. }
  79. }
  80. // Receive the byte over I2C
  81. *pucData = MAP_I2CMasterDataGet(I2C_BASE);
  82. return SUCCESS;
  83. }
  84. int I2C_IF_Write(unsigned char ucDevAddr,
  85. unsigned char *pucData,
  86. unsigned char ucLen,
  87. unsigned char ucStop)
  88. {
  89. RETERR_IF_TRUE(pucData == NULL);
  90. RETERR_IF_TRUE(ucLen == 0);
  91. // Set I2C codec slave address
  92. MAP_I2CMasterSlaveAddrSet(I2C_BASE, ucDevAddr, false);
  93. // Write the first byte to the controller.
  94. MAP_I2CMasterDataPut(I2C_BASE, *pucData);
  95. // Initiate the transfer.
  96. RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_START));
  97. // Decrement the count and increment the data pointer
  98. // to facilitate the next transfer
  99. ucLen--;
  100. pucData++;
  101. // Loop until the completion of transfer or error
  102. while(ucLen)
  103. {
  104. // Write the next byte of data
  105. MAP_I2CMasterDataPut(I2C_BASE, *pucData);
  106. // Transact over I2C to send byte
  107. RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_CONT));
  108. // Decrement the count and increment the data pointer
  109. // to facilitate the next transfer
  110. ucLen--;
  111. pucData++;
  112. }
  113. // If stop bit is to be sent, send it.
  114. if(ucStop == true)
  115. {
  116. RET_IF_ERR(I2CTransact(I2C_MASTER_CMD_BURST_SEND_STOP));
  117. }
  118. return SUCCESS;
  119. }
  120. 4、关闭I2C
  121. int I2C_IF_Close()
  122. {
  123. //
  124. // Power OFF the I2C peripheral
  125. //
  126. MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK);
  127. return SUCCESS;
  128. }
复制代码
此帖出自RF/无线论坛
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
快速回复 返回顶部 返回列表