FX3学习笔记3-i2c

一、实验环境

  • 硬件平台:CYUSB3KIT-003 EZ-USB® FX3™ SuperSpeed Explorer Kit

  • sdk版本:EZ-USB FX3 SDK1.3 / SuperSpeed Explorer Kit 1.0

  • 实验例程:cyfxuvc_an75779(cypress官网下载demo)

  • i2c设备:lsm6ds3

二、实验目的

  • 测试CYUSB3KIT-003的i2c接口,本例采用st的一颗六轴lsm6ds3来验证i2c接口

三、硬件连接

  • 将lsm6ds3的vcc、gnd、scl和sda四根线接到CYUSB3KIT-003对应管脚,CYUSB3KIT-003板子有丝印,可以通过丝印查找scl、sda、gnd和v3p3。

四、程序添加

  • 1、在sensor.h中添加lsm6ds3的器件地址
//add by allen
#define LSM6DS3_ADDR_WR 0xD6             /* Slave address used to write sensor registers. */
#define LSM6DS3_ADDR_RD 0xD7             /* Slave address used to write sensor registers. */
  • 2、在sensor.c中添加lsm6ds3的读寄存器函数
//add by allen
CyU3PReturnStatus_t allenI2cRead (
        uint8_t slaveAddr,
        uint8_t Addr,
        uint8_t *buf)
{
    CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
    CyU3PI2cPreamble_t preamble;

    /* Validate the parameters. */
    if (slaveAddr != LSM6DS3_ADDR_RD)
    {
        CyU3PDebugPrint (4, "I2C Slave address is not valid!\n");
        return 1;
    }
  

    preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK;        /*  Mask out the transfer type bit. */
    preamble.buffer[1] = Addr;
    preamble.buffer[2] = slaveAddr;
    preamble.length    = 3;
    preamble.ctrlMask  = 0x0002;                                /*  Send start bit after two byte of preamble. */

    apiRetStatus = CyU3PI2cReceiveBytes (&preamble, buf, 1, 0);
    SensorI2CAccessDelay (apiRetStatus);

    return apiRetStatus;
}
- 说明:按官方接口修改为8位寄存器地址、读取一个字节的的读取函数
  • 3、在sensor.h中添加函数的声明
extern CyU3PReturnStatus_t allenI2cRead (
        uint8_t slaveAddr,
        uint8_t Addr,
        uint8_t *buf);

  • 4、在uvc.c中的函数UVCAppThread_Entry (uint32_t input)中添加读取lsm6ds3的who am i寄存器的接口
/*
 * Entry function for the UVC Application Thread
 */
void
UVCAppThread_Entry (
        uint32_t input)
{
    CyU3PReturnStatus_t apiRetStatus;
    uint32_t            flag;

    /* Initialize the Uart Debug Module */
    CyFxUVCApplnDebugInit ();

    /* Initialize the I2C interface */
    CyFxUVCApplnI2CInit ();

    /* Initialize the UVC Application */
    CyFxUVCApplnInit ();

    /*
       The actual data forwarding from sensor to USB host is done from the DMA and GPIF callback
       functions. The thread is only responsible for checking for streaming start/stop conditions.
      
       The CY_FX_UVC_STREAM_EVENT event flag will indicate that the UVC video stream should be started.

       The CY_FX_UVC_STREAM_ABORT_EVENT event indicates that we need to abort the video streaming. This
       only happens when we receive a CLEAR_FEATURE request indicating that streaming is to be stopped,
       or when we have a critical error in the data path.
     */
    for (;;)
    {
        uint8_t my_id = 0;
        allenI2cRead(LSM6DS3_ADDR_RD, 0x0f, &my_id);
        CyU3PDebugPrint (4, "who am i = %d\r\n", my_id);
        ……
   }
   ……
}     

五、实验结果

  • 将改好的程序编译后烧到CYUSB3KIT-003中可以看到读取结果为105,也就是0x69,i2c测试完成。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路人 假

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值