day4 驱动

This commit is contained in:
james 2024-07-31 17:30:28 +08:00
parent 18b595ae19
commit 8b95bb5575
14 changed files with 228 additions and 24 deletions

14
.config
View File

@ -428,7 +428,7 @@ CONFIG_RT_USING_ULOG=y
# CONFIG_ULOG_OUTPUT_LVL_I is not set # CONFIG_ULOG_OUTPUT_LVL_I is not set
CONFIG_ULOG_OUTPUT_LVL_D=y CONFIG_ULOG_OUTPUT_LVL_D=y
CONFIG_ULOG_OUTPUT_LVL=7 CONFIG_ULOG_OUTPUT_LVL=7
# CONFIG_ULOG_USING_ISR_LOG is not set CONFIG_ULOG_USING_ISR_LOG=y
CONFIG_ULOG_ASSERT_ENABLE=y CONFIG_ULOG_ASSERT_ENABLE=y
CONFIG_ULOG_LINE_BUF_SIZE=128 CONFIG_ULOG_LINE_BUF_SIZE=128
# CONFIG_ULOG_USING_ASYNC_OUTPUT is not set # CONFIG_ULOG_USING_ASYNC_OUTPUT is not set
@ -1072,7 +1072,11 @@ CONFIG_PKG_AHT10_VER="latest"
# CONFIG_PKG_USING_MAX17048 is not set # CONFIG_PKG_USING_MAX17048 is not set
# CONFIG_PKG_USING_AS7341 is not set # CONFIG_PKG_USING_AS7341 is not set
# CONFIG_PKG_USING_CW2015 is not set # CONFIG_PKG_USING_CW2015 is not set
# CONFIG_PKG_USING_ICM20608 is not set CONFIG_PKG_USING_ICM20608=y
CONFIG_PKG_ICM20608_PATH="/packages/peripherals/sensors/icm20608"
# CONFIG_PKG_USING_ICM20608_V100 is not set
CONFIG_PKG_USING_ICM20608_LATEST_VERSION=y
CONFIG_PKG_ICM20608_VER="latest"
# CONFIG_PKG_USING_PAJ7620 is not set # CONFIG_PKG_USING_PAJ7620 is not set
# CONFIG_PKG_USING_STHS34PF80 is not set # CONFIG_PKG_USING_STHS34PF80 is not set
# end of sensors drivers # end of sensors drivers
@ -1561,7 +1565,7 @@ CONFIG_BSP_USING_FAL=y
CONFIG_BSP_USING_RW007_WLAN=y CONFIG_BSP_USING_RW007_WLAN=y
CONFIG_BSP_USING_AHT21=y CONFIG_BSP_USING_AHT21=y
# CONFIG_BSP_USING_AP3216C is not set # CONFIG_BSP_USING_AP3216C is not set
# CONFIG_BSP_USING_ICM20608 is not set CONFIG_BSP_USING_ICM20608=y
# CONFIG_BSP_USING_USB_MOUSE is not set # CONFIG_BSP_USING_USB_MOUSE is not set
# CONFIG_BSP_USING_CAN is not set # CONFIG_BSP_USING_CAN is not set
# CONFIG_BSP_USING_AUDIO is not set # CONFIG_BSP_USING_AUDIO is not set
@ -1598,7 +1602,9 @@ CONFIG_BSP_USING_SPI2=y
# CONFIG_BSP_USING_ADC is not set # CONFIG_BSP_USING_ADC is not set
CONFIG_BSP_USING_I2C=y CONFIG_BSP_USING_I2C=y
# CONFIG_BSP_USING_I2C1 is not set # CONFIG_BSP_USING_I2C1 is not set
# CONFIG_BSP_USING_I2C2 is not set CONFIG_BSP_USING_I2C2=y
CONFIG_BSP_I2C2_SCL_PIN=81
CONFIG_BSP_I2C2_SDA_PIN=80
CONFIG_BSP_USING_I2C3=y CONFIG_BSP_USING_I2C3=y
CONFIG_BSP_I2C3_SCL_PIN=64 CONFIG_BSP_I2C3_SCL_PIN=64
CONFIG_BSP_I2C3_SDA_PIN=65 CONFIG_BSP_I2C3_SDA_PIN=65

View File

@ -11,6 +11,7 @@
I/O设备显示屏、串口通信、flash、SD卡、以太网接口…… I/O设备显示屏、串口通信、flash、SD卡、以太网接口……
接口open,close... 接口open,close...
![I/O框架图](image-4.png) ![I/O框架图](image-4.png)
![I/O框架图演进](image-7.png)
### 派生设备种类 ### 派生设备种类
![alt text](image.png) ![alt text](image.png)
``` c ``` c
@ -38,39 +39,144 @@ RT_Device_Class_Miscellaneous /* 杂类设备 */
### 为什么分类设备 ### 为什么分类设备
一类的控制相同 一类的控制相同
### 例子 ### 例子
RT_D ![alt text](image-5.png)
## ## api
[点击查看官网文档api介绍](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/device?id=%e5%88%9b%e5%bb%ba%e5%92%8c%e6%b3%a8%e5%86%8c-io-%e8%ae%be%e5%a4%87)
### 创建销毁设备 ### 创建销毁设备
### 注册销毁 ### 注册销毁
### flags ### flags
分行? ……_stream :向串口终端输出字符串
### 实验1注册 `\n ``\r\n`
### 实验1注册字符设备
``` c
#include <rtthread.h>
#include <rtdevice.h>
static int rt_device_test_init(void)
{
rt_device_t test_dev = rt_device_create(RT_Device_Class_Char,0);
if(!test_dev)
{
rt_kprintf("test_dev create failed\n");
return -RT_ERROR;
}
if(rt_device_register(test_dev,"test_dev",RT_DEVICE_FLAG_RDWR)!=RT_EOK)
{
rt_kprintf("test_dev register failed\n");
}
return RT_EOK;
}
MSH_CMD_EXPORT_ALIAS(rt_device_test_init,devtest, test device init);
```
![alt text](image-6.png)
### 访问设备
open,close...
### ……
### 访问
### 查找、初始化
### 打开、关闭
### 打开标志位
### 控制设备
### 读写设备
### 回调
### 调用关系图 ### 调用关系图
IO设备管理层 IO设备管理层
PIN设备驱动框架层 PIN设备驱动框架层
PIN设备驱动层 PIN设备驱动层
## PIN设备
## GPIO ### GPIO
引脚电源、时钟、控制、I/O 芯片上的引脚分为 4 类电源、时钟、控制、I/O
GPIO,功能复用I/O I/O 口使用模式:
可编程控制中断 - GPIOGeneral Purpose Input Output通用输入 / 输出)),
- 功能复用I/O如 SPI/I2C/UART 等)
### 可编程控制中断
![alt text](image-1.png) ![alt text](image-1.png)
rt_pin_mode() ### 常用接口
rt_pin_write() - rt_pin_mode()
rt_pin_read() - rt_pin_write()
- rt_pin_read()
- rt_pin_attach_irq() :绑定引脚中断回调函数
- rt_pin_irq_enable() :使能引脚中断
- rt_pin_detach_irq() :脱离引脚中断回调函数
……
![alt text](image-2.png) ![alt text](image-2.png)
Assert断言判断是否为真假则报错终止运行
### 外部中断 ### 实验:外部中断
LOG 不用\n
``` c
#include <drv_gpio.h>
#include <rtdevice.h>
#include <rtthread.h>
#define LOG_TAG "pin.irq"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#define KEY_UP GET_PIN(C, 5)
#define KEY_DOWN GET_PIN(C, 1)
#define KEY_LEFT GET_PIN(C, 0)
#define KEY_RIGHT GET_PIN(C, 4)
void key_up_callback(void *args)
{
int value = rt_pin_read(KEY_UP);
LOG_I("key up value: %d\n", value);
}
void key_down_callback(void *args)
{
int value = rt_pin_read(KEY_DOWN);
LOG_I("key down value: %d\n", value);
}
void key_left_callback(void *args)
{
int value = rt_pin_read(KEY_LEFT);
LOG_I("key left value: %d\n", value);
}
void key_right_callback(void *args)
{
int value = rt_pin_read(KEY_RIGHT);
LOG_I("key right value: %d\n", value);
}
static int rt_pin_irq_example(void)
{
rt_pin_mode(KEY_UP, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(KEY_DOWN, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(KEY_LEFT, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(KEY_RIGHT, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(KEY_UP, PIN_IRQ_MODE_FALLING, key_up_callback, RT_NULL);
rt_pin_attach_irq(KEY_DOWN, PIN_IRQ_MODE_FALLING, key_down_callback, RT_NULL);
rt_pin_attach_irq(KEY_LEFT, PIN_IRQ_MODE_FALLING, key_left_callback, RT_NULL);
rt_pin_attach_irq(KEY_RIGHT, PIN_IRQ_MODE_FALLING, key_right_callback, RT_NULL);
rt_pin_irq_enable(KEY_UP,PIN_IRQ_ENABLE);
rt_pin_irq_enable(KEY_DOWN,PIN_IRQ_ENABLE);
rt_pin_irq_enable(KEY_LEFT,PIN_IRQ_ENABLE);
rt_pin_irq_enable(KEY_RIGHT,PIN_IRQ_ENABLE);
return RT_EOK;
}
MSH_CMD_EXPORT_ALIAS(rt_pin_irq_example,irq, pin_irq_example);
```
![irq](image-8.png)
### FlexibleButton 按键库(可用)
![fexible button config](image-9.png)
## I2C总线 ## I2C总线
[官方文档链接](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/i2c/i2c)
I2C 是 Inter-Integrated Circuit 的简称读作I-squared-C
I2C 总线传输数据时只需两根信号线,一根是双向数据线 **SDA**serial data另一根是双向时钟线 **SCL**serial clock。SPI 总线有两根线分别用于主从设备之间接收数据和发送数据,而 I2C 总线只使用一根线进行数据收发。不同于 SPI 一主多从的结构,它允许同时有多个主设备存在
RTT用GPIO模拟I2C
![alt text](image-10.png)
应答信号: 每传输完成一个字节的数据,接收方就需要回复一个 ACKacknowledge。写数据时由从机发送 ACK读数据时由主机发送 ACK。当主机读到最后一个字节数据时可发送 NACKNot acknowledge然后跟停止条件。
![alt text](image-11.png)
![alt text](image-12.png)
![alt text](image-13.png)
手动添加
Kconfig → drv_soft_i2c.c&h
list device 查看
六轴***
![alt text](image-14.png)
可尝试i2c-tools 软件包

85
Day4/icm_20608_sample.c Normal file
View File

@ -0,0 +1,85 @@
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <icm20608.h>
#define LOG_TAG "icm.app"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
static void icm_thread_entry(void *parameter)
{
icm20608_device_t dev = RT_NULL;
const char *i2c_bus_name = "i2c2";
int count = 0;
rt_err_t result;
/* 初始化 icm20608 传感器 */
dev = icm20608_init(i2c_bus_name);
if (dev == RT_NULL)
{
LOG_E("The sensor initializes failure");
}
else
{
LOG_D("The sensor initializes success");
}
/* 对 icm20608 进行零值校准:采样 10 次,求取平均值作为零值 */
result = icm20608_calib_level(dev, 10);
if (result == RT_EOK)
{
LOG_D("The sensor calibrates success");
LOG_D("accel_offset: X%6d Y%6d Z%6d", dev->accel_offset.x, dev->accel_offset.y, dev->accel_offset.z);
LOG_D("gyro_offset : X%6d Y%6d Z%6d", dev->gyro_offset.x, dev->gyro_offset.y, dev->gyro_offset.z);
}
else
{
LOG_E("The sensor calibrates failure");
icm20608_deinit(dev);
}
while (count++ < 100)
{
rt_int16_t accel_x, accel_y, accel_z;
rt_int16_t gyros_x, gyros_y, gyros_z;
/* 读取三轴加速度 */
result = icm20608_get_accel(dev, &accel_x, &accel_y, &accel_z);
if (result == RT_EOK)
{
LOG_D("current accelerometer: accel_x%6d, accel_y%6d, accel_z%6d", accel_x, accel_y, accel_z);
}
else
{
LOG_E("The sensor does not work");
}
/* 读取三轴陀螺仪 */
result = icm20608_get_gyro(dev, &gyros_x, &gyros_y, &gyros_z);
if (result == RT_EOK)
{
LOG_D("current gyroscope : gyros_x%6d, gyros_y%6d, gyros_z%6d", gyros_x, gyros_y, gyros_z);
}
else
{
LOG_E("The sensor does not work");
break;
}
rt_thread_mdelay(1000);
}
}
static int icm_app(void)
{
rt_thread_t res = rt_thread_create("icm", icm_thread_entry, RT_NULL, 1024, 20, 50);
if(res == RT_NULL)
{
return -RT_ERROR;
}
rt_thread_startup(res);
return RT_EOK;
}
MSH_CMD_EXPORT(icm_app, icm_app);

BIN
Day4/image-10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Day4/image-11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

BIN
Day4/image-12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

BIN
Day4/image-13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

BIN
Day4/image-14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
Day4/image-5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

BIN
Day4/image-6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
Day4/image-7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 KiB

BIN
Day4/image-8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Day4/image-9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 KiB

View File

@ -270,6 +270,7 @@
#define RT_USING_ULOG #define RT_USING_ULOG
#define ULOG_OUTPUT_LVL_D #define ULOG_OUTPUT_LVL_D
#define ULOG_OUTPUT_LVL 7 #define ULOG_OUTPUT_LVL 7
#define ULOG_USING_ISR_LOG
#define ULOG_ASSERT_ENABLE #define ULOG_ASSERT_ENABLE
#define ULOG_LINE_BUF_SIZE 128 #define ULOG_LINE_BUF_SIZE 128
@ -463,6 +464,8 @@
#define PKG_USING_AHT10 #define PKG_USING_AHT10
#define PKG_USING_AHT10_LATEST_VERSION #define PKG_USING_AHT10_LATEST_VERSION
#define PKG_USING_ICM20608
#define PKG_USING_ICM20608_LATEST_VERSION
/* end of sensors drivers */ /* end of sensors drivers */
/* touch drivers */ /* touch drivers */
@ -567,6 +570,7 @@
#define BSP_USING_FAL #define BSP_USING_FAL
#define BSP_USING_RW007_WLAN #define BSP_USING_RW007_WLAN
#define BSP_USING_AHT21 #define BSP_USING_AHT21
#define BSP_USING_ICM20608
/* end of Onboard Peripheral Drivers */ /* end of Onboard Peripheral Drivers */
/* On-chip Peripheral Drivers */ /* On-chip Peripheral Drivers */
@ -581,6 +585,9 @@
#define BSP_USING_SPI #define BSP_USING_SPI
#define BSP_USING_SPI2 #define BSP_USING_SPI2
#define BSP_USING_I2C #define BSP_USING_I2C
#define BSP_USING_I2C2
#define BSP_I2C2_SCL_PIN 81
#define BSP_I2C2_SDA_PIN 80
#define BSP_USING_I2C3 #define BSP_USING_I2C3
#define BSP_I2C3_SCL_PIN 64 #define BSP_I2C3_SCL_PIN 64
#define BSP_I2C3_SDA_PIN 65 #define BSP_I2C3_SDA_PIN 65