Skip to content

Commit

Permalink
tests/periph_i2c: add tests for periph_i2c_reconfigure features
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benpicco committed Mar 12, 2020
1 parent 52e9d5c commit 4ea4094
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/periph_i2c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 47 additions & 0 deletions tests/periph_i2c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
#include <errno.h>

#include "periph_conf.h"
#include "periph/gpio.h"
#include "periph/i2c.h"
#include "shell.h"

#include <xtimer.h>

#ifndef I2C_ACK
#define I2C_ACK (0)
#endif
Expand Down Expand Up @@ -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(%i)\n", dev);
i2c_deinit(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(%i)\n", dev);
i2c_init(dev);

printf("Success: i2c_%i re-init\n", dev);
return 0;
}
#endif

int cmd_i2c_read_reg(int argc, char **argv)
{
int res;
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit 4ea4094

Please sign in to comment.