6626的I2C操作实例

yangzh · 238次点击 · 6个月前

6626配置I2C的方法介绍

  1. main函数配置初始化

    int main(void)
    {
        struct ob_stack_param param = {
            .max_connection = OB_LE_HOST_CONNECTION_NB,
            .max_ext_adv_set = OB_LE_HOST_ADV_SET_NUM,
            .max_att_mtu = OB_LE_HOST_ATT_MTU,
            .max_gatt_serv_num = OB_LE_HOST_MAX_GATT_SERV_NUM,
            .max_gatt_write_cache = OB_LE_HOST_ATT_WRITE_CACHE_SIZE,
            .smp_sc_support = OB_LE_HOST_SC_PAIRING,
        };
       
        drv_pin_init(pin_config, sizeof(pin_config) / sizeof(pin_config[0]));
        drv_gpio_init(gpio_config, sizeof(gpio_config) / sizeof(gpio_config[0]));
        // trigger: both edge
        drv_gpio_register_isr_callback(OM_GPIO0, drv_gpio_isr_handler);

       
        drv_pmu_pin_io13_reset_mode_enable(true);

        drv_wdt_init(0);
        board_init();
        hardware_init();
        drv_rf_init();
        evt_init();
        evt_timer_init();
        shell_init(NULL);
        nvds_init(0);
        omble_init(&param);

        app_adv_init();
        app_sec_init();
        service_common_init();
        app_onmicro_dfu_init();
        ancs_client_init();
        //app_media_hid_init();
        app_tspp_init();

        uint32_t address, length;
        uint16_t crc16;
        mbr_read_part(PART_TYPE_APP, &address, &length, &crc16);
        OM_LOG_DEBUG("running(%d): 0x%08X, len: %d, crc: 0x%04X\n", drv_pmu_reboot_reason(), address, length, crc16);
        OM_LOG_DEBUG("temperature=%dC\n", (int)drv_adc_control(ADC_CONTROL_READ_TEMPERATURE, NULL));
          
        example_i2c();// I2C初始化
        // enable sleep
        pm_sleep_enable(false);
        pm_sleep_allow(PM_ID_SHELL);

        __enable_irq();

        // Main loop
        while (1)
        {
            // do event schedule
            evt_schedule();

            OM_CRITICAL_BEGIN();

            // if no event, do power manage
            if (evt_get_all() == 0)
            {
                pm_power_manage();
            }

            OM_CRITICAL_END();
        }

   2.   初始化配置(端口定义)

/// Test pad i2c scl
#define PAD_I2C0_SCL                    16
/// Test pad mux i2c scl
#define MUX_I2C0_SCL                    PINMUX_PAD16_I2C0_SCK_CFG
/// Test pad i2c sda
#define PAD_I2C0_SDA                    9
/// Test pad mux i2c sda
#define MUX_I2C0_SDA                    PINMUX_PAD9_I2C0_SDA_CFG

/* Use AT24C02 */
/// Address of eeprom
#define EEPROM_ADDR                     0x50U
/// Capacity of eeprom
#define EEPROM_CAPACITY                 256U
/// Test address in eeprom
#define TEST_ADDR                       0x40


/// Test pad for gpio input trigger
#define PAD_GPIO_TRIG           15

/*********************************************************************
 * LOCAL VARIABLES
 */
static evt_timer_t evt_timer_0;
/// Buffer that stored the data to be written
static uint8_t write_buf[100];
/// Buffer that stored the data to be read
static uint8_t read_buf[100];
/// Buffer that stores data to be sent
static uint8_t eeprom_tx_buf[EEPROM_CAPACITY];
/// Buffer that stores data to be read
static uint8_t eeprom_rx_buf[EEPROM_CAPACITY];
/// I2C pin configuration
static const pin_config_t pin_cfg [] = {
        {PAD_I2C0_SCL, {MUX_I2C0_SCL}, PMU_PIN_MODE_PU, PMU_PIN_DRIVER_CURRENT_NORMAL},
        {PAD_I2C0_SDA, {MUX_I2C0_SDA}, PMU_PIN_MODE_PU, PMU_PIN_DRIVER_CURRENT_NORMAL},
};

/// Pinmux Configuration
static pin_config_t pin_config[] = {
    {PAD_GPIO_TRIG,  {PINMUX_GPIO_MODE_CFG}, PMU_PIN_MODE_PU, PMU_PIN_DRIVER_CURRENT_NORMAL},
};

/// GPIO Configuration
static gpio_config_t gpio_config[] = {
    {OM_GPIO0, PAD_GPIO_TRIG,   GPIO_DIR_INPUT,  GPIO_LEVEL_HIGH, GPIO_TRIG_RISING_FAILING_EDGE},
};

static gpio_config_t gpio_config_2[] = {
    {OM_GPIO0, PAD_GPIO_TRIG,   GPIO_DIR_INPUT,  GPIO_LEVEL_HIGH, GPIO_TRIG_NONE},
};

void example_i2c(void)
{
    drv_pin_init(pin_cfg, sizeof(pin_cfg) / sizeof(pin_cfg[0]));

    i2c_config_t cfg = {
        .mode  = I2C_MODE_MASTER,        // 7-bit addressing mode
        .speed = I2C_SPEED_400K,
    };
    drv_i2c_init(OM_I2C0, &cfg);

}


   3.写I2C数据示例

// 调用后就能进入中断
void set_ppg_cod(void)
{
    //{寄存器地址,16bit数据的高8位,16bit数据的低8位}
    uint8_t data1[3] = {0x00, 0x00, 0x01};// 0x00
    uint8_t data2[3] = {0x04, 0x01, 0x40};// 0x04
    uint8_t data3[3] = {0x0A, 0x00, 0x00};// 0x0A
    uint8_t data4[3] = {0x0B, 0x00, 0x41};// 0x0B
    uint8_t data5[3] = {0x0F, 0x00, 0x01};// 0x0F
    uint8_t data6[3] = {0x27, 0x38, 0x17};// 0x27
    uint8_t data7[3] = {0x2D, 0x00, 0x00};// 0x2D
    uint8_t data8[3] = {0x2E, 0x03, 0xFF};// 0x2E
    uint8_t data9[3] = {0x2F, 0x10, 0x13};// 0x2F
    uint8_t data10[3] = {0x10, 0x00, 0x02};// 0x10
    uint8_t data11[3] = {0x19, 0x43, 0x43};// 0x19
    uint8_t data12[3] = {0x1A, 0x43, 0x43};// 0x1A
    uint8_t data13[3] = {0x1B, 0x41, 0x41};// 0x1B
    uint8_t data14[3] = {0x1C, 0x41, 0x41};// 0x1C
    uint8_t data15[3] = {0x25, 0x0F, 0xF0};// 0x25
    uint8_t data16[3] = {0x02, 0x00, 0x91};// 0x02

    uint8_t recv[2] = {0};
    uint16_t tmp = 0;
    uint8_t rd_addr = 0x00;

   

    // 设备地址0x5B,缓冲,字节数
    drv_i2c_master_write(OM_I2C0, 0x5B, data1, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data2, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data3, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data4, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data5, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data6, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data7, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data8, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data9, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data10, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data11, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data12, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data13, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data14, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data15, 3);
    drv_i2c_master_write(OM_I2C0, 0x5B, data16, 3);
   
}

   

  4.读示例

void ZSDataHandle(void)
{
    uint16_t tmp;

    uint8_t recv[2];
    memset(recv, 0, sizeof(recv));

    uint8_t rd_addr = 0x06;
    drv_i2c_master_read(OM_I2C0, 0x5B, &rd_addr, 1, recv, 2); // 读出中断寄存器标志位

    tmp = (recv[0] << 8) | recv[1]; // 转为16位数据
    if (tmp & 0x0001)
    {
        // TSA溢出中断
        memset(recv, 0, sizeof(recv));
        rd_addr = 0x51;
        // 读取带读取FIFO的字节个数长度//FIFO_BYTE_COUNT_TSA[10:0]
        drv_i2c_master_read(OM_I2C0, 0x5B, &rd_addr, 1, recv, 2);
    }

    rd_addr = 0x61;
    uint8_t fifo[20] = {0};
    memset(fifo, 0, sizeof(fifo));

    tmp = (recv[0] << 8) | recv[1];                            // 转为16位数据
    drv_i2c_master_read(OM_I2C0, 0x5B, &rd_addr, 1, fifo, 20); // 读取FIFO内容,一次读取20个字节

    OM_LOG_DEBUG_ARRAY_EX("FIFO_BYTE_COUNT51", fifo, tmp); // 打印fifo的数据
}


5.测试波形

image.png




被收藏 0  ∙  0 赞  
加入收藏
点赞
0 回复  
善言善语 (您需要 登录 后才能回复 没有账号 ?)

请先登录网站