ODROID-C5: analog: fix for ODROID-C5

The iio device file, The read speed of "in_voltage0_input" is very slow. This will adversely affect the stepper motor operation and result in a "Stepper too far in past" error.

Read the value of Voltage0_raw and calculate the ADC voltage directly in the klipper.

Change-Id: Iedb1b5f7fbfedc60d42fc1a28eaa7e49879d6404
This commit is contained in:
2025-06-05 17:08:15 +09:00
parent 88ad6d1934
commit 4a66eefe5d
2 changed files with 42 additions and 8 deletions

View File

@@ -14,30 +14,54 @@
#include "sched.h" // sched_shutdown #include "sched.h" // sched_shutdown
// ODROID-C5 // ODROID-C5
DECL_CONSTANT("ADC_MAX", 1800); // Assume 12bit adc DECL_CONSTANT("ADC_MAX", 1800);
#define ANALOG_START (1<<12) #define ANALOG_START (1<<12)
DECL_ENUMERATION_RANGE("pin", "analog0", ANALOG_START, 8); DECL_ENUMERATION_RANGE("pin", "analog0", ANALOG_START, 8);
// ODROID-C5 // ODROID-C5
#define IIO_PATH "/sys/bus/iio/devices/iio:device0/in_voltage%d_input" #define IIO_PATH "/sys/bus/iio/devices/iio:device0/in_voltage0_raw"
#define SCALE_PATH "/sys/bus/iio/devices/iio:device0/in_voltage_scale"
#define OFFSET_PATH "/sys/bus/iio/devices/iio:device0/in_voltage_offset"
struct gpio_adc struct gpio_adc
gpio_adc_setup(uint32_t pin) gpio_adc_setup(uint32_t pin)
{ {
char fname[256]; struct gpio_adc g;
snprintf(fname, sizeof(fname), IIO_PATH, pin-ANALOG_START); int fd;
char buf[64];
int ret;
int fd = open(fname, O_RDONLY|O_CLOEXEC); fd = open(SCALE_PATH, O_RDONLY);
ret = read(fd, buf, sizeof(buf)-1);
if (ret <= 0) {
goto fail;
}
buf[ret] = '\0';
g.scale = atof(buf) * 1000;
close(fd);
fd = open(OFFSET_PATH, O_RDONLY);
ret = read(fd, buf, sizeof(buf)-1);
if (ret <= 0) {
goto fail;
}
buf[ret] = '\0';
g.offset = atoi(buf);
close(fd);
fd = open(IIO_PATH, O_RDONLY|O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
report_errno("analog open", fd); report_errno("analog open", fd);
goto fail; goto fail;
} }
int ret = set_non_blocking(fd); ret = set_non_blocking(fd);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
return (struct gpio_adc){ .fd = fd };
g.fd = fd;
return g;
fail: fail:
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
@@ -54,6 +78,7 @@ uint16_t
gpio_adc_read(struct gpio_adc g) gpio_adc_read(struct gpio_adc g)
{ {
char buf[64]; char buf[64];
int voltage;
int ret = pread(g.fd, buf, sizeof(buf)-1, 0); int ret = pread(g.fd, buf, sizeof(buf)-1, 0);
if (ret <= 0) { if (ret <= 0) {
report_errno("analog read", ret); report_errno("analog read", ret);
@@ -61,7 +86,14 @@ gpio_adc_read(struct gpio_adc g)
return 0; return 0;
} }
buf[ret] = '\0'; buf[ret] = '\0';
return atoi(buf);
voltage = atoi(buf) + g.offset;
voltage = voltage > 0 ? voltage : 0;
voltage = voltage * g.scale;
voltage = voltage < 1800000 ? voltage : 1800000;
voltage /= 1000;
return voltage;
} }
void void

View File

@@ -20,6 +20,8 @@ void gpio_in_reset(struct gpio_in g, int8_t pull_up);
uint8_t gpio_in_read(struct gpio_in g); uint8_t gpio_in_read(struct gpio_in g);
struct gpio_adc { struct gpio_adc {
int scale;
int offset;
int fd; int fd;
}; };
struct gpio_adc gpio_adc_setup(uint32_t pin); struct gpio_adc gpio_adc_setup(uint32_t pin);