MAX30102 多通道 I2C 通讯与初始化问题分析

本文记录在多通道 I2C 通讯过程中使用 MAX30102 传感器时遇到的问题及解决方案,适合用于多通道血氧模块或多传感器项目参考。


==1. 通道选择问题:==

通过 write(sensor, &channel, 1) 来进行通道的选取和切换,但仅限于存在设备的通道可以正常扫描和初始化传感器。

问题原因:

由于 write() 具有阻塞作用,当扫描到一个无设备的通道时会卡死,导致无法继续后续通道扫描

解决思路:

借助 Linux 系统自带的 i2ctools 工具进行通道扫描,使用如下命令:

i2cset -y 4 0x70 0x01
i2cdetect -y 4

代码分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void MAX30102::scanf_channel()
{
for (int i = 0; i < 8; i++)
{
char command[128];
snprintf(command, sizeof(command), "i2cset -y 4 0x%02x 0x00 0x%02x", TCA9548A_ADDR, 1 << i);

if (system(command) != 0)
{
continue;
}

char detect_command[128];
snprintf(detect_command, sizeof(detect_command), "i2cdetect -y 4 | grep -q '57'");

if (system(detect_command) == 0)
{
enable_channels[count_channel++] = i;
}
else
{
std::cerr << "No device found at address 0x57 on channel " << std::endl;
continue;
}
}

for (int i = 0; i < count_channel; i++)
{
std::cout << "channel : " << enable_channels[i] << std::endl;
}
}

==2. 关于 I2C 总线设置问题:==

2.1 通信频率与打开限制

  • I2C 总线最大频率为 400kHz
  • I2C 设备 不允许频繁重复打开
  • 否则会报错或导致设备失效(总线错误)。

2.2 通道切换与设备切换失败问题

问题:
切换了通道但设备没有跟随切换,原因是 write() 函数阻塞,导致 I2C 指向未切换成功的设备节点。

解决方法:
先统一打开一次 I2C 总线,在实际访问设备时,动态返回配置后的设备句柄,并在使用后手动关闭以避免资源冲突。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int MAX30102::init_i2c(const char *device, int addr)
{
int temp_fd = open(device, O_RDWR);
if (temp_fd == -1)
{
perror("Failed to open I2C device");
return -1;
}
if (ioctl(temp_fd, I2C_SLAVE, addr) < 0)
{
perror("Failed to set I2C address");
close(temp_fd);
return -1;
}

return temp_fd;
}

MAX30102 多通道 I2C 通讯与初始化问题分析

https://garyaacm.github.io/2024/11/18/Linux嵌入式开发-max30102_tca9548/

作者

Gary

发布于

2024-11-18

更新于

2025-05-19

许可协议

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...