From 896f9db71fcd4283c984982cfa6d0730e35a8b12 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 20 Feb 2020 13:34:37 +0100 Subject: [PATCH] tests/periph_i2c: add tests for periph_i2c_reconfigure features Add a test to re-configure the I2C pins to GPIO functionality and use them as output. A small delay is added to allow for observing the change in power draw. --- tests/periph_i2c/Makefile | 2 ++ tests/periph_i2c/main.c | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/tests/periph_i2c/Makefile b/tests/periph_i2c/Makefile index 1aa079ca54..77e10cbeac 100644 --- a/tests/periph_i2c/Makefile +++ b/tests/periph_i2c/Makefile @@ -2,7 +2,9 @@ BOARD ?= samr21-xpro include ../Makefile.tests_common FEATURES_REQUIRED = periph_i2c +FEATURES_OPTIONAL = periph_i2c_reconfigure USEMODULE += shell +USEMODULE += xtimer include $(RIOTBASE)/Makefile.include diff --git a/tests/periph_i2c/main.c b/tests/periph_i2c/main.c index e6ab7e6110..0b5f4f87a5 100644 --- a/tests/periph_i2c/main.c +++ b/tests/periph_i2c/main.c @@ -24,9 +24,12 @@ #include #include "periph_conf.h" +#include "periph/gpio.h" #include "periph/i2c.h" #include "shell.h" +#include + #ifndef I2C_ACK #define I2C_ACK (0) #endif @@ -160,6 +163,47 @@ int cmd_i2c_release(int argc, char **argv) return 0; } +#ifdef MODULE_PERIPH_I2C_RECONFIGURE +int cmd_i2c_gpio(int argc, char **argv) +{ + int dev; + + dev = _check_param(argc, argv, 1, 1, "DEV"); + if (dev == ARG_ERROR) { + return 1; + } + + gpio_t sda_pin = i2c_pin_sda(dev); + gpio_t scl_pin = i2c_pin_scl(dev); + + printf("Command: i2c_deinit_pins(%i)\n", dev); + i2c_deinit_pins(dev); + + gpio_init(sda_pin, GPIO_OUT); + gpio_init(scl_pin, GPIO_OUT); + + xtimer_sleep(1); + + printf("Command: gpio_set()\n"); + gpio_set(sda_pin); + gpio_set(scl_pin); + + xtimer_sleep(1); + + printf("Command: gpio_clear()\n"); + gpio_clear(sda_pin); + gpio_clear(scl_pin); + + xtimer_sleep(1); + + printf("Command: i2c_init_pins(%i)\n", dev); + i2c_init_pins(dev); + + printf("Success: i2c_%i re-init\n", dev); + return 0; +} +#endif + int cmd_i2c_read_reg(int argc, char **argv) { int res; @@ -444,6 +488,9 @@ int cmd_i2c_get_id(int argc, char **argv) static const shell_command_t shell_commands[] = { { "i2c_acquire", "Get access to the I2C bus", cmd_i2c_acquire }, { "i2c_release", "Release to the I2C bus", cmd_i2c_release }, +#ifdef MODULE_PERIPH_I2C_RECONFIGURE + { "i2c_gpio", "Re-configures I2C pins to GPIO mode and back.", cmd_i2c_gpio }, +#endif { "i2c_read_reg", "Read byte from register", cmd_i2c_read_reg }, { "i2c_read_regs", "Read bytes from registers", cmd_i2c_read_regs }, { "i2c_read_byte", "Read byte from the I2C device", cmd_i2c_read_byte },