This commit is contained in:
2024-07-25 21:13:42 +08:00
parent bcc787a68f
commit 66c6558488
17 changed files with 702 additions and 39 deletions

View File

@@ -2,7 +2,10 @@ from building import *
import os
cwd = GetCurrentDir()
src = Glob('*.c')
# src = Glob('*.c')
src = [
'main.c',
]
CPPPATH = [cwd]
if GetDepend(['PKG_USING_RTDUINO']) and not GetDepend(['RTDUINO_NO_SETUP_LOOP']):

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);

View File

@@ -0,0 +1,31 @@
//未完成
#include <rtthread.h>
#include <rtdevice.h>
#include <drv_spi.h>
#include <drv_gpio.h>
static int spi_attach(void)
{
return rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(B,12));
}
// INIT_DEVICE_EXPORT(spi_attach);
static int spi_transfer_one_data(void)
{
rt_err_t ret =RT_EOK;
struct rt_spi_device *spi_d20 = (struct rt_spi_device *)rt_device_find("spi20");
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
cfg.max_hz =1 *1000 *1000;
rt_spi_configure(spi20,&cfg);
rt_uint8_t sendBuff = 0xDA;
rt_uint8_t recvBuff = 0xF1;
ret =rt_spi_transfer(spi20,&sendBuff,&recvBuff,1);
rt_kprintf("sret = %d\n",ret);
}
MSH_CMD_EXPORT(spi_transfer_one_data, spi transfer one data);

View File

@@ -0,0 +1,19 @@
#include <rtthread.h>
#include <rtdevice.h>
#define LOG_TAG "drv.test"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
static int dev_test_app(void)
[
rt_device_t test_dev = rt_device_find("test_dev");
if(test_dev == RT_NULL)
{
LOG_E("Cannot find test_dev");
return -RT_ERROR;
}
rt_device_open(test_dev, RT_DEVICE_OFLAG_RDWR);
rt_device_close(test_dev);
]