Skip to content

Commit dffb431

Browse files
authored
Merge pull request #137 from sansarus/master
Add i2c bus autoselector (just Novatek hal support now)
2 parents b86e6a3 + 2efd800 commit dffb431

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/sensors.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "sensors.h"
2020
#include "tools.h"
2121

22+
2223
static read_register_t sensor_read_register;
2324
static write_register_t sensor_write_register;
2425

@@ -971,7 +972,7 @@ static int detect_possible_sensors(sensor_ctx_t *ctx, int fd,
971972

972973
static bool get_sensor_id_i2c(sensor_ctx_t *ctx) {
973974
bool detected = false;
974-
int fd = open_i2c_sensor_fd();
975+
int fd = open_i2c_sensor_fd(i2c_adapter_nr);
975976
if (fd == -1)
976977
return false;
977978

@@ -1045,10 +1046,12 @@ static bool get_sensor_id_spi(sensor_ctx_t *ctx) {
10451046
}
10461047

10471048
bool getsensorid(sensor_ctx_t *ctx) {
1049+
1050+
int current_i2c_adapter_nr;
10481051
if (!getchipname())
10491052
return NULL;
10501053
// there is no platform specific i2c/spi access layer
1051-
if (!open_i2c_sensor_fd)
1054+
if (!open_i2c_sensor_fd(i2c_adapter_nr))
10521055
return NULL;
10531056

10541057
// Use common settings as default
@@ -1064,8 +1067,44 @@ bool getsensorid(sensor_ctx_t *ctx) {
10641067
bool spi_detected = get_sensor_id_spi(ctx);
10651068
if (spi_detected) {
10661069
strcpy(ctx->control, "spi");
1070+
return spi_detected;
1071+
}
1072+
// "try sensor search at not standart i2c buses"
1073+
1074+
current_i2c_adapter_nr = i2c_adapter_nr;
1075+
1076+
for (int i = 0; i <= 5; i++) {
1077+
if (i == current_i2c_adapter_nr) {
1078+
continue; // Skip the current value
1079+
}
1080+
1081+
i2c_adapter_nr = i;
1082+
1083+
1084+
if (!open_i2c_sensor_fd(i2c_adapter_nr))
1085+
return NULL;
1086+
1087+
// Use common settings as default
1088+
ctx->data_width = 1;
1089+
ctx->reg_width = 2;
1090+
1091+
i2c_detected = get_sensor_id_i2c(ctx);
1092+
if (i2c_detected) {
1093+
strcpy(ctx->control, "i2c");
1094+
return true;
1095+
}
1096+
1097+
spi_detected = get_sensor_id_spi(ctx);
1098+
if (spi_detected) {
1099+
strcpy(ctx->control, "spi");
1100+
return spi_detected;
10671101
}
1068-
return spi_detected;
1102+
1103+
1104+
1105+
}
1106+
1107+
10691108
}
10701109

10711110
#ifndef STANDALONE_LIBRARY

0 commit comments

Comments
 (0)