rt-thread i2c 使用教程
rt-thread i2c 使用教程
rt-thread studio
1. 创建基础工程
使用芯片级的基础工程作为环境。
2. 配置i2c
打开i2c
。
配置驱动。
CTRL+S
保存配置后,会重新生成工程。
修改board.h
文件。取消 BSP_USING_I2C1
的注释。
/** if you want to use i2c bus(soft simulate) you can use the following instructions.** STEP 1, open i2c driver framework(soft simulate) support in the RT-Thread Settings file** STEP 2, define macro related to the i2c bus* such as #define BSP_USING_I2C1** STEP 3, according to the corresponding pin of i2c port, modify the related i2c port and pin information* such as #define BSP_I2C1_SCL_PIN GET_PIN(port, pin) -> GET_PIN(C, 11)* #define BSP_I2C1_SDA_PIN GET_PIN(port, pin) -> GET_PIN(C, 12)*/#define BSP_USING_I2C1
#ifdef BSP_USING_I2C1
#define BSP_I2C1_SCL_PIN GET_PIN(B, 10)
#define BSP_I2C1_SDA_PIN GET_PIN(B, 11)
#endif
3. 代码
使用i2c
读取BMP280温湿度计。使用rt_i2c_transfer
来读取和写入数据到传感器芯片。这里的温湿度驱动是我自己实现的。
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include "bme280_i2c.h"#define BMP280_I2C_BUS_NAME "i2c1"
#define BME280_ADDR 0X76static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
static rt_bool_t initialized = RT_FALSE;static int write_bytes(struct rt_i2c_bus_device *bus, uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t len)
{uint8_t buffer[128] = { 0 };buffer[0] = reg_addr;memcpy(buffer + 1, data, len);struct rt_i2c_msg msgs;msgs.addr = BME280_ADDR;msgs.flags = RT_I2C_WR;msgs.buf = buffer;msgs.len = len + 1;if (rt_i2c_transfer(bus, &msgs, 1) == 1){return RT_EOK;}elsereturn -RT_ERROR;
}static int read_bytes(struct rt_i2c_bus_device *bus, uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t len)
{struct rt_i2c_msg msgs[2];msgs[0].addr = BME280_ADDR;msgs[0].flags = RT_I2C_WR;msgs[0].buf = ®_addr;msgs[0].len = 1;msgs[1].addr = BME280_ADDR;msgs[1].flags = RT_I2C_RD;msgs[1].buf = data;msgs[1].len = len;if (rt_i2c_transfer(bus, msgs, 2) == 2){return RT_EOK;}elsereturn -RT_ERROR;
}static int write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *bytes, uint32_t len)
{return write_bytes(i2c_bus, BME280_ADDR, reg_addr, bytes, len);
}static int read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *bytes, uint32_t len)
{return read_bytes(i2c_bus, BME280_ADDR, reg_addr, bytes, len);
}static void delay_us(uint32_t us)
{rt_thread_mdelay(us / 1000);
}static int i2c_test(void)
{i2c_bus = (struct rt_i2c_bus_device *) rt_device_find(BMP280_I2C_BUS_NAME);if (i2c_bus == RT_NULL){rt_kprintf("can't find %s device!\n", BMP280_I2C_BUS_NAME);return RT_ERROR;}bme280_init(write, read, delay_us, NULL, 0);int32_t temperature = 0;bme280_read_temperature(&temperature);rt_kprintf("temp: %d\n", temperature);return RT_EOK;
}MSH_CMD_EXPORT(i2c_test, i2c test);
4. 测试
使用i2c_test
进行demo测试,读取温度为27.28℃。
KEIL
暂时无。
rt-thread i2c 使用教程相关推荐
- 基于rt thread smart构建EtherCAT主站
我把源码开源到到了gitee,https://gitee.com/rathon/rt-thread-smart-soem 有兴趣的去可以下载下来跑一下 软件工程推荐用vscode 打开.rt thre ...
- 使用RT Thread设备框架封装一个I2C设备——DS3231
使用RT Thread设备框架封装一个I2C设备--DS3231 前言 ENV配置 I2C测试 将ds3231封装成一个字符设备 结语 前言 学习rt thread的I2C的时候,恰巧手上的板子留了d ...
- RT Thread根据开发板制作BSP方法
之前一直不懂怎么使用RT Thread的软件包,感谢网上的大神,看了你们的博客后大概了解一些,在此做下记录.用RT Thread软件包需要RT Thread的系统,但是RT Thread和RT Thr ...
- RT Thread Studio 配置IIC并读取AS5600角度
RT Thread Studio 配置IIC并读取AS5600角度 一.RT Thread Studio 配置IIC 1.在RT Thread Seting 中开启IIC功能 并保存 一定要保存才能更 ...
- 正点原子delay函数移植到rt thread操作系统(HAL库)
正点原子教程中涉及到的操作系统只涉及了UCOS的教程,其中例程的system文件夹中的delay.c函数只是适配了UCOS. 下面将delay.c函数移植到rt thread中,使用的bsp是rt t ...
- 基于GD32F103C8T6添加RT Thread nano设备框架并添加串口设备(以控制台console( uart0 )为例)
最近没事琢磨了一下使用设备框架的问题.因为将串口注册到设备框架可以应用十分丰富的软件包. 于是就整理了一下手上的工程,重新将工程梳理了一遍. 像这样是十分清爽了,其中RTOS是操作系统源代码 并且学习 ...
- 关于RT thread系统节拍时钟的配置
关于RT thread系统节拍时钟的配置 -----本文基于rt-thread-3.1.3版本编写 首先,使用RTthread OS时,要配置(或者明白)它的系统节拍 ...
- rt thread studio使用QBOOT和片外flash实现OTA升级
我们这里要使用单片机外部flash作为OTA的下载分区,外部flash硬件连接关系 PB3-->SPI3_CLK PB4-->SPI3_MISO PB5-->SPI3_MOSI PE ...
- rt thread 使用FAL遇到fal_init() undefined reference
rt thread FAL 0.5版,之前有没有不知道,遇到一个坑. 在main.cpp里面已经 #include <fal.h> fal_init() 编译报错,说 fal_init() ...
最新文章
- T extends Serializable这是什么意思呢?看明白这个,你的问题就自然而然的明白了!...
- Fiddler常用功能介绍
- SQL Server 中系统视图sysobjects中type字段的说明
- jQuery1.3.2 源码学习 -2 两个重要的正则表达式
- Gym - 101755G Underpalindromity (树状数组)
- 解决Zend OPcache huge_code_pages: mmap(HUGETLB) fail
- v380云存储如何查询_使用 JuiceFS 在云上优化 Kylin 的存储性能
- 北鼎机器人_北鼎T752已入手,我的感受
- Datawhale 零基础入门数据挖掘-Task4 建模调参
- php jquery 实例教程,php jquery 多文件上传简单实例
- windows 远程桌面连接(mstsc)删除下拉框的记录
- Axure原型图入门
- word文档中在文字的右上角加标注快捷键
- 基于深度学习的13种通用图像分类模型及其实现
- Codeforces 934C - A Twisty Movement (dp)
- A - Round decimals
- 《大话数据结构》读书笔记-树
- C语言之结构体就这样被攻克了!(绝对值得收藏的文章)
- 【软考】高级系统架构设计师学习经验分享
- 关于Oracle导出到csv文件的脚本-spool方法