From ec27ef4e3077f54320fca6a9c9a1489c41048fe5 Mon Sep 17 00:00:00 2001 From: Jean THOMAS Date: Tue, 24 Feb 2026 12:13:15 +0100 Subject: [PATCH] examples: add ina260 Add example application for INA260 current/voltage/power sensor driver. Signed-off-by: Jean THOMAS --- examples/ina260/CMakeLists.txt | 33 +++++++++++++ examples/ina260/Kconfig | 36 +++++++++++++++ examples/ina260/Make.defs | 25 ++++++++++ examples/ina260/Makefile | 36 +++++++++++++++ examples/ina260/ina260_main.c | 84 ++++++++++++++++++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 examples/ina260/CMakeLists.txt create mode 100644 examples/ina260/Kconfig create mode 100644 examples/ina260/Make.defs create mode 100644 examples/ina260/Makefile create mode 100644 examples/ina260/ina260_main.c diff --git a/examples/ina260/CMakeLists.txt b/examples/ina260/CMakeLists.txt new file mode 100644 index 00000000000..a2fe3547ff2 --- /dev/null +++ b/examples/ina260/CMakeLists.txt @@ -0,0 +1,33 @@ +# ############################################################################## +# apps/examples/ina260/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_EXAMPLES_INA260) + nuttx_add_application( + NAME + ina260 + SRCS + ina260_main.c + STACKSIZE + ${CONFIG_EXAMPLES_INA260_STACKSIZE} + PRIORITY + ${CONFIG_EXAMPLES_INA260_PRIORITY}) +endif() diff --git a/examples/ina260/Kconfig b/examples/ina260/Kconfig new file mode 100644 index 00000000000..d578d534a99 --- /dev/null +++ b/examples/ina260/Kconfig @@ -0,0 +1,36 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_INA260 + tristate "INA260 example" + default n + depends on SENSORS_INA260 + ---help--- + Enable the INA260 example + +if EXAMPLES_INA260 + +config EXAMPLES_INA260_PROGNAME + string "Program name" + default "ina260" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config INA260_DEVICE_PATH + string "INA260 device name" + default "/dev/ina260" + ---help--- + This is the device path for the INA260 sensor. + +config EXAMPLES_INA260_PRIORITY + int "INA260 task priority" + default 100 + +config EXAMPLES_INA260_STACKSIZE + int "INA260 stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/examples/ina260/Make.defs b/examples/ina260/Make.defs new file mode 100644 index 00000000000..c34e9f42910 --- /dev/null +++ b/examples/ina260/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/examples/ina260/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_INA260),) +CONFIGURED_APPS += $(APPDIR)/examples/ina260 +endif diff --git a/examples/ina260/Makefile b/examples/ina260/Makefile new file mode 100644 index 00000000000..3d9459587a1 --- /dev/null +++ b/examples/ina260/Makefile @@ -0,0 +1,36 @@ +############################################################################ +# apps/examples/ina260/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# INA260 built-in application info + +PROGNAME = $(CONFIG_EXAMPLES_INA260_PROGNAME) +PRIORITY = $(CONFIG_EXAMPLES_INA260_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_INA260_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_INA260) + +# INA260 Example + +MAINSRC = ina260_main.c + +include $(APPDIR)/Application.mk diff --git a/examples/ina260/ina260_main.c b/examples/ina260/ina260_main.c new file mode 100644 index 00000000000..75dc9dc1687 --- /dev/null +++ b/examples/ina260/ina260_main.c @@ -0,0 +1,84 @@ +/**************************************************************************** + * apps/examples/ina260/ina260_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_INA260_DEVICE_PATH +# define CONFIG_INA260_DEVICE_PATH "/dev/ina260" +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * ina260_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + struct ina260_s sample; + int fd; + int ret; + + fd = open(CONFIG_INA260_DEVICE_PATH, O_RDWR); + if (fd < 0) + { + int errcode = errno; + printf("ERROR: Failed to open %s: %d\n", + CONFIG_INA260_DEVICE_PATH, errcode); + return EXIT_FAILURE; + } + + while (1) + { + ret = read(fd, &sample, sizeof(sample)); + if (ret != sizeof(sample)) + { + break; + } + + printf("U=%6" PRIu32 "mV I=%6" PRIi32 "mA P=%6" PRIu32 "mW\n", + sample.voltage, sample.current, sample.power); + + usleep(500000); + } + + close(fd); + return 0; +}