4133 views|0 replies

118

Posts

1

Resources
The OP
 

RT-Thread device framework learning I2C device [Copy link]

RT-Thread learning record

1. New Studio Project

2. Brief description of console usage

3. PIN device framework learning

4.UART device framework learning

I2C Devices

RT-Thread's I2C uses software simulation, which increases flexibility.

First, check the I2C driver framework under the Components setting. Since only software simulation is currently supported, you need to check the Use GPIO to simulate I2C option. It is not recommended to check Use I2C debug message. If the driver fails, you can turn it on or hardware debug, as shown below

Then check the i2c device routine under the Samples setting and save. As shown in the figure below, you can see that the i2c routine has been added to the project

Open the example file, you can see that the function of this example is to drive AHT10 through I2C. Because my board uses ATT's Pandora, it has this peripheral and can be tested directly

The example uses i2c2. Because it is software simulation, the suffix can be changed to 1, 3, 5, etc., but it needs to be consistent with the driver settings. The specific pins are set in the I2C CONFIG section under the board.h file, and the corresponding I2C definitions and onboard pins are enabled.

Save, compile, and download. Enter the routine in the console and you can see the relevant information printed.

/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date Author Notes
 * 2018-08-15 misonyo first implementation.
 */
/*
 * Program Listing: This is an I2C device usage routine
 * The routine exports the i2c_aht10_sample command to the control terminal
 * Command calling format: i2c_aht10_sample i2c1
 * Command explanation: The second parameter of the command is the name of the I2C bus device to be used. If it is empty, the default I2C bus device will be used.
 * Program function: read the temperature and humidity data of the temperature and humidity sensor aht10 through the I2C device and print it
*/

#include <rtthread.h>
#include <rtdevice.h>

#define AHT10_I2C_BUS_NAME "i2c2" /* I2C bus device name to which the sensor is connected*/
#define AHT10_ADDR 0x38 /* Slave address*/
#define AHT10_CALIBRATION_CMD 0xE1 /* Calibration command */
#define AHT10_NORMAL_CMD 0xA8 /* General command */
#define AHT10_GET_DATA 0xAC /* Get data command*/

static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C bus device handle */
static rt_bool_t initialized = RT_FALSE; /* Sensor initialization status*/

/* Write sensor register */
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
  rt_uint8_t buf[3];
  struct rt_i2c_msg msgs;

  buf[0] = reg; //cmd
  buf[1] = data[0];
  buf[2] = data[1];

  msgs.addr = AHT10_ADDR;
  msgs.flags = RT_I2C_WR;
  msgs.buf = buf;
  msgs.len = 3;

  /* Call I2C device interface to transfer data*/
  if (rt_i2c_transfer(bus, &msgs, 1) == 1)
   {
    return RT_EOK;
   }
  else
   {
    return -RT_ERROR;
   }
}

/* Read sensor register data */
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
{
  struct rt_i2c_msg msgs;

  msgs.addr = AHT10_ADDR;
  msgs.flags = RT_I2C_RD;
  msgs.buf = buf;
  msgs.len = len;

  /* Call I2C device interface to transfer data*/
  if (rt_i2c_transfer(bus, &msgs, 1) == 1)
   {
    return RT_EOK;
   }
  else
   {
    return -RT_ERROR;
   }
}

static void read_temp_humi(float *cur_temp, float *cur_humi)
{
  rt_uint8_t temp[6];

  write_reg(i2c_bus, AHT10_GET_DATA, 0); /* Send command */
  rt_thread_mdelay(400);
  read_regs(i2c_bus, 6, temp); /* Get sensor data*/

  /* Humidity data conversion */
  *cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
  /* Temperature data conversion*/
  *cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
}

static void aht10_init(const char *name)
{
  rt_uint8_t temp[2] = {0, 0};

  /* Find the I2C bus device and get the I2C bus device handle*/
  i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);

  if (i2c_bus == RT_NULL)
   {
    rt_kprintf("can't find %s device!\n", name);
   }
  else
   {
    write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
    rt_thread_mdelay(400);

    temp[0] = 0x08;
    temp[1] = 0x00;
    write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
    rt_thread_mdelay(400);
    initialized = RT_TRUE;
   }
}

static void i2c_aht10_sample(int argc, char *argv[])
{
  float humidity, temperature;
  char name[RT_NAME_MAX];

  humidity = 0.0;
  temperature = 0.0;

  if (argc == 2)
   {
    rt_strncpy(name, argv[1], RT_NAME_MAX);
   }
  else
   {
    rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
   }

  if (!initialized)
   {
    /* Sensor initialization */
    aht10_init(name);
   }
  if (initialized)
   {
    /* Read temperature and humidity data*/
    read_temp_humi(&temperature, &humidity);

    rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
    rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
   }
  else
   {
    rt_kprintf("initialize sensor failed!\n");
   }
}
/* Export to msh command list */
MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);

The above is the instruction for using the I2C driver framework.




This content is originally created by EEWORLD forum user ID.LODA. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

This post is from Embedded System

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list