1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/shell/cmds/i2c_scan.c
Marian Buschsieweke c06335b71b
sys/shell: make cmds submodules
Previously `shell_commands` was a "catch-all" module that included
shell commands for each and every used module that has a shell
companion. Instead, the new `shell_cmds` module is now used to provide
shell commands as individually selectable submodules, e.g.
`cmd_gnrc_icmpv6_echo` now provides the ICMPv6 echo command (a.k.a.
ping).

To still have a "catch all" module to pull in shell commands of modules
already used, `shell_cmds_default` was introduced. `shell_commands`
depends now on `shell_cmds_default` for backward compatibility, but
has been deprecated. New apps should use `shell_cmds_default`
instead.

For a handful of shell commands individual selection was already
possible. Those modules now depend on the corresponding `cmd_%` module
and they have been deprecated.
2022-09-16 13:15:45 +02:00

113 lines
2.7 KiB
C

/*
* Copyright (C) 2019 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_shell_commands
* @{
*
* @file
* @brief An I2C bus scanner
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
* @}
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "board.h"
#include "periph/i2c.h"
#include "shell.h"
static int get_dev(i2c_t *dev, int argc, char **argv)
{
if (argc == 2) {
int idx = atoi(argv[1]);
if ((idx < 0) || (idx >= (int)I2C_NUMOF)) {
printf("I2C device with number \"%s\" not found\n", argv[1]);
}
else {
*dev = I2C_DEV(idx);
return 0;
}
}
printf("Usage: %s [DEVICE_NUMBER]\n", argv[0]);
return -1;
}
static inline int is_addr_reserved(uint16_t addr)
{
if ((addr < 0x0e) || (addr > 0x77))
return 1;
return 0;
}
static int _i2c_scan(int argc, char **argv)
{
i2c_t dev;
if (get_dev(&dev, argc, argv)) {
return -1;
}
printf("Scanning I2C device %s...\n", argv[1]);
i2c_acquire(dev);
puts(
"addr not ack'ed = \"-\", addr ack'ed = \"X\", addr reserved = \"R\", error = \"E\"\n"
" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
);
for (char i = 0; i < 8; i++) {
char row[] = { '0', 'x', '0' + i, '0', '\0' };
fputs(row, stdout);
uint16_t addr = i;
addr <<= 4;
for (unsigned j = 0; j < 16; j++) {
char str[] = { ' ', '-', '\0' };
if (is_addr_reserved(addr)) {
str[1] = 'R';
}
else {
char dummy[1];
int retval;
while (-EAGAIN == (retval = i2c_read_byte(dev, addr, dummy, 0))) {
/* retry until bus arbitration succeeds */
}
switch (retval) {
case 0:
/* success: Device did respond */
str[1] = 'X';
break;
case -ENXIO:
/* No ACK --> no device */
break;
default:
/* Some unexpected error */
str[1] = 'E';
break;
}
}
fputs(str, stdout);
addr++;
}
puts("");
}
i2c_release(dev);
return 0;
}
SHELL_COMMAND(i2c_scan, "Performs an I2C bus scan", _i2c_scan);