rt-thread | rt-smart | |
---|---|---|
GPIO | √ | √ |
I2C | √ | √ |
lcd | √ | × |
#define I2C1_SCL_MUX_BASE 0x020E00B4U
#define I2C2_SCL_MUX_BASE 0x020E00BCU
#define I2C3_SCL_MUX_BASE 0x020E00E4U
#define I2C4_SCL_MUX_BASE 0x020E00ECU
#define I2C1_SCL_CFG_BASE 0x020E0340U
#define I2C2_SCL_CFG_BASE 0x020E0348U
#define I2C3_SCL_CFG_BASE 0x020E0370U
#define I2C4_SCL_CFG_BASE 0x020E0378U
#define I2C1_SCL_INPUT_BASE 0x020E05A4U
#define I2C2_SCL_INPUT_BASE 0x020E05ACU
#define I2C3_SCL_INPUT_BASE 0x020E05B4U
#define I2C4_SCL_INPUT_BASE 0x020E05BCU
#define I2C1_SDA_MUX_BASE 0x020E00B8U
#define I2C2_SDA_MUX_BASE 0x020E00C0U
#define I2C3_SDA_MUX_BASE 0x020E00E8U
#define I2C4_SDA_MUX_BASE 0x020E00F0U
#define I2C1_SDA_CFG_BASE 0x020E0344U
#define I2C2_SDA_CFG_BASE 0x020E034CU
#define I2C3_SDA_CFG_BASE 0x020E0374U
#define I2C4_SDA_CFG_BASE 0x020E037CU
#define I2C1_SDA_INPUT_BASE 0x020E05A8U
#define I2C2_SDA_INPUT_BASE 0x020E05B0U
#define I2C3_SDA_INPUT_BASE 0x020E05B8U
#define I2C4_SDA_INPUT_BASE 0x020E05C0U
struct i2c_addr_config
{
I2C_Type *i2c;
size_t i2c_scl_mux_base;
size_t i2c_scl_config_base;
size_t i2c_scl_input_base;
size_t i2c_sda_mux_base;
size_t i2c_sda_config_base;
size_t i2c_sda_input_base
};
static struct i2c_addr_config addr_config[] =
{
{I2C1, I2C1_SCL_MUX_BASE, I2C1_SCL_CFG_BASE, I2C1_SCL_INPUT_BASE, I2C1_SDA_MUX_BASE, I2C1_SDA_CFG_BASE, I2C1_SDA_INPUT_BASE},
{I2C2, I2C2_SCL_MUX_BASE, I2C2_SCL_CFG_BASE, I2C2_SCL_INPUT_BASE, I2C2_SDA_MUX_BASE, I2C2_SDA_CFG_BASE, I2C2_SDA_INPUT_BASE},
{I2C3, I2C3_SCL_MUX_BASE, I2C3_SCL_CFG_BASE, I2C3_SCL_INPUT_BASE, I2C3_SDA_MUX_BASE, I2C3_SDA_CFG_BASE, I2C3_SDA_INPUT_BASE},
{I2C4, I2C4_SCL_MUX_BASE, I2C4_SCL_CFG_BASE, I2C4_SCL_INPUT_BASE, I2C4_SDA_MUX_BASE, I2C4_SDA_CFG_BASE, I2C4_SDA_INPUT_BASE},
};
for(i = 0; i < sizeof(addr_config) / sizeof(addr_config[0]); i++)
{
addr_config[i].i2c = (I2C_Type *)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c), 0x1000);
addr_config[i].i2c_scl_mux_base = (size_t)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c_scl_mux_base), 0x1000);
addr_config[i].i2c_scl_config_base = (size_t)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c_scl_config_base), 0x1000);
addr_config[i].i2c_scl_input_base = (size_t)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c_scl_input_base), 0x1000);
addr_config[i].i2c_sda_mux_base = (size_t)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c_sda_mux_base), 0x1000);
addr_config[i].i2c_sda_config_base = (size_t)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c_sda_config_base), 0x1000);
addr_config[i].i2c_sda_input_base = (size_t)rt_hw_kernel_phys_to_virt((void*)(addr_config[i].i2c_sda_input_base), 0x1000);
}
int ap3216c_test()
{
struct rt_sensor_config cfg;
cfg.intf.dev_name = "i2c1";
cfg.mode = RT_SENSOR_MODE_POLLING;
rt_hw_ap3216c_init("ap", &cfg);
return RT_EOK;
}
INIT_DEVICE_EXPORT(ap3216c_test);
int main(int argc, char **argv)
{
struct rt_device_pin_mode pin_mode;
struct rt_device_pin_status pin_status;
int server_ch;
int shmid;
struct rt_channel_msg msg_text;
char *str;
printf("RiceChen rt-smart first app\n");
/* create the IPC channel for 'server' */
server_ch = rt_channel_open("server", O_CREAT);
if (server_ch == -1) {
printf("Error: rt_channel_open: fail to create the IPC channel for server!\n");
return -1;
}
printf("\nserver: wait on the IPC channel: %d\n", server_ch);
pin_dev = rt_device_find("pin");
if(pin_dev == RT_NULL)
{
printf("not find pin device\n");
return RT_ERROR;
}
rt_device_open(pin_dev, RT_DEVICE_OFLAG_RDWR);
pin_mode.pin = LED_PIN;
pin_mode.mode = 0; //OUTPUT
rt_device_control(pin_dev, 0, (void *)&pin_mode);
pin_status.pin = LED_PIN;
while(1)
{
rt_channel_recv(server_ch, &msg_text); //接收通道信息
shmid = (int)msg_text.u.d;
if (shmid < 0 || !(str = (char *)lwp_shmat(shmid, NULL)))
{
msg_text.u.d = (void *)-1;
printf("server: receive an invalid shared-memory page.\n");
rt_channel_reply(server_ch, &msg_text); /* send back -1 */
continue;
}
if(strcmp(str, "warning") == 0) //判断是否接收到"warning"信息
{
printf("light warning.\n");
pin_status.status = 1;
rt_device_write(pin_dev, 0, (void *)&pin_status, sizeof(pin_status));
rt_thread_mdelay(200);
pin_status.status = 0;
rt_device_write(pin_dev, 0, (void *)&pin_status, sizeof(pin_status));
rt_thread_mdelay(200);
pin_status.status = 1;
rt_device_write(pin_dev, 0, (void *)&pin_status, sizeof(pin_status));
rt_thread_mdelay(200);
pin_status.status = 0;
rt_device_write(pin_dev, 0, (void *)&pin_status, sizeof(pin_status));
rt_thread_mdelay(200);
}
lwp_shmdt(str);
msg_text.type = RT_CHANNEL_RAW;
msg_text.u.d = (void *)1;
rt_channel_reply(server_ch, &msg_text);
}
return 0;
}
int main(int argc, char **argv)
{
rt_device_t ap3216c_dev;
struct rt_sensor_data sensor_data;
int res;
int server_ch;
char warning_msg[256] = { 0 };
size_t len = 0;
/* channel messages to send and return back */
struct rt_channel_msg ch_msg, ch_msg_ret;
printf("RiceChen rt-smart second app\n");
/* open the IPC channel created by 'pong' */
server_ch = rt_channel_open("server", 0);
if (server_ch == -1)
{
printf("Error: rt_channel_open: could not find the \'server\' channel!\n");
return -1;
}
ap3216c_dev = rt_device_find(SENSOR_NAME);
if (ap3216c_dev == RT_NULL)
{
rt_kprintf("Can't find device:%s", SENSOR_NAME);
return -1;
}
if (rt_device_open(ap3216c_dev, RT_DEVICE_FLAG_RDWR) != RT_EOK)
{
rt_kprintf("open device failed!");
return -1;
}
rt_device_control(ap3216c_dev, RT_SENSOR_CTRL_SET_ODR, (void *)100);
while(1)
{
res = rt_device_read(ap3216c_dev, 0, &sensor_data, 1); //读取传感器数值
if (res != 1)
{
rt_kprintf("read data failed!size is %d\n", res);
}
else
{
rt_kprintf("light:%5d lux, timestamp:%5d\n", sensor_data.light, sensor_data.timestamp);
}
if(sensor_data.light > 50) //判断阈值
{
ch_msg.type = RT_CHANNEL_RAW;
snprintf(warning_msg, 255, "%s", "warning");
len = strlen(warning_msg) + 1;
warning_msg[len] = '\0';
int shmid = prepare_data(warning_msg, len);
if (shmid < 0)
{
printf("clent: fail to prepare the clent message.\n");
continue;
}
ch_msg.u.d = (void *)shmid;
rt_channel_send_recv(server_ch, &ch_msg, &ch_msg_ret); //发送警报信息
lwp_shmrm(shmid);
}
rt_thread_mdelay(2000);
}
rt_device_close(ap3216c_dev);
rt_channel_close(server_ch);
return 0;
}
http://mpvideo.qpic.cn/0bf2tqacoaaax4ajtip5hfpvbhgde6oaajya.f10003.mp4?dis_k=eb7e60fac969d1a4d82f1977fe3b1233&dis_t=1652175640&vid=wxv_1692360264878194688&format_id=10003&support_redirect=0&mmversion=false
本文分享自 Rice 嵌入式开发技术分享 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!