2014-10-09 10:40:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* 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 tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Test application for the low-level I2C peripheral driver
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "periph/i2c.h"
|
|
|
|
#include "shell.h"
|
|
|
|
|
|
|
|
#define BUFSIZE (128U)
|
|
|
|
|
|
|
|
static int i2c_dev = -1;
|
|
|
|
|
2018-03-29 13:13:10 +02:00
|
|
|
static i2c_speed_t map_speed[] = {
|
|
|
|
I2C_SPEED_LOW,
|
|
|
|
I2C_SPEED_NORMAL,
|
|
|
|
I2C_SPEED_FAST,
|
|
|
|
I2C_SPEED_FAST_PLUS,
|
|
|
|
I2C_SPEED_HIGH
|
|
|
|
};
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_init_master(int argc, char **argv)
|
2014-10-09 10:40:31 +02:00
|
|
|
{
|
|
|
|
int dev, speed, res;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
puts("Error: Init: Invalid number of arguments!");
|
|
|
|
printf("Usage:\n%s: [DEVICE] [SPEED]\n", argv[0]);
|
|
|
|
puts(" with DEVICE:");
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
for (unsigned i = 0; i < I2C_NUMOF; i++) {
|
|
|
|
printf(" %u -> I2C_%u\n", i, i);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
puts(" SPEED:");
|
|
|
|
puts(" 0 -> SPEED_LOW (10kbit/s)");
|
|
|
|
puts(" 1 -> SPEED_NORMAL (100kbit/s)");
|
|
|
|
puts(" 2 -> SPEED_FAST (400kbit/s)");
|
|
|
|
puts(" 3 -> SPEED_FAST_PLUS (1Mbit/s)");
|
|
|
|
puts(" 4 -> SPEED_HIGH (3.4Mbit/s)\n");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dev = atoi(argv[1]);
|
|
|
|
speed = atoi(argv[2]);
|
|
|
|
|
2018-03-29 13:13:10 +02:00
|
|
|
if (speed < 0 || speed > 4) {
|
|
|
|
puts("Error: Speed value not in range (0 - 4)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = i2c_init_master(dev, map_speed[speed]);
|
2014-10-09 10:40:31 +02:00
|
|
|
if (res == -1) {
|
|
|
|
puts("Error: Init: Given device not available");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else if (res == -2) {
|
|
|
|
puts("Error: Init: Unsupported speed value");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("I2C_%i successfully initialized as master!\n", dev);
|
|
|
|
i2c_dev = dev;
|
|
|
|
}
|
2015-03-20 08:51:45 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_write(int argc, char **argv)
|
2014-10-09 10:40:31 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uint8_t addr;
|
|
|
|
int length = argc - 2;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t data[BUFSIZE];
|
2014-10-09 10:40:31 +02:00
|
|
|
|
|
|
|
if (i2c_dev < 0) {
|
|
|
|
puts("Error: no I2C device was initialized");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
if (argc < 3) {
|
|
|
|
puts("Error: not enough arguments given");
|
|
|
|
printf("Usage:\n%s: ADDR BYTE0 [BYTE1 [BYTE_n [...]]]\n", argv[0]);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
addr = atoi(argv[1]);
|
2014-10-09 10:40:31 +02:00
|
|
|
for (int i = 0; i < length; i++) {
|
2016-09-30 23:01:46 +02:00
|
|
|
data[i] = atoi(argv[i + 2]);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 1) {
|
2016-09-30 23:01:46 +02:00
|
|
|
printf("i2c_write_byte(I2C_%i, 0x%02x, 0x%02x)\n", i2c_dev, addr, data[0]);
|
2014-10-09 10:40:31 +02:00
|
|
|
res = i2c_write_byte(i2c_dev, addr, data[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("i2c_write_bytes(I2C_%i, 0x%02x, [", i2c_dev, addr);
|
|
|
|
for (int i = 0; i < length; i++) {
|
2016-09-30 23:01:46 +02:00
|
|
|
printf(", 0x%02x", data[i]);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
puts("])");
|
|
|
|
res = i2c_write_bytes(i2c_dev, addr, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
puts("Error: no bytes were written");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("I2C_%i: successfully wrote %i bytes to the bus\n", i2c_dev, res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_write_reg(int argc, char **argv)
|
2014-10-09 10:40:31 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uint8_t addr, reg;
|
|
|
|
int length = argc - 3;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t data[BUFSIZE];
|
2014-10-09 10:40:31 +02:00
|
|
|
|
|
|
|
if (i2c_dev < 0) {
|
|
|
|
puts("Error: no I2C device initialized");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
if (length < 1) {
|
|
|
|
puts("Error: not enough arguments given");
|
|
|
|
printf("Usage:\n%s ADDR REG BYTE0 [BYTE1 ...]\n", argv[0]);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
addr = atoi(argv[1]);
|
|
|
|
reg = atoi(argv[2]);
|
2014-10-09 10:40:31 +02:00
|
|
|
for (int i = 0; i < length; i++) {
|
2016-09-30 23:01:46 +02:00
|
|
|
data[i] = atoi(argv[i + 3]);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 1) {
|
|
|
|
printf("i2c_write_reg(I2C_%i, 0x%02x, 0x%02x, 0x%02x)\n",
|
2016-09-30 23:01:46 +02:00
|
|
|
i2c_dev, addr, reg, data[0]);
|
2014-10-09 10:40:31 +02:00
|
|
|
res = i2c_write_reg(i2c_dev, addr, reg, data[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("i2c_write_regs(I2C_%i, 0x%02x, 0x%02x, [", i2c_dev, addr, reg);
|
|
|
|
for (int i = 0; i < length; i++) {
|
2016-09-30 23:01:46 +02:00
|
|
|
printf("0x%02x, ", data[i]);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
puts("])");
|
|
|
|
res = i2c_write_regs(i2c_dev, addr, reg, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 1) {
|
|
|
|
puts("Error: no bytes were written");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("I2C_%i: successfully wrote %i bytes to register 0x%02x\n", i2c_dev, res, reg);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_read(int argc, char **argv)
|
2014-10-09 10:40:31 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uint8_t addr;
|
|
|
|
int length;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t data[BUFSIZE];
|
2014-10-09 10:40:31 +02:00
|
|
|
|
|
|
|
if (i2c_dev < 0) {
|
|
|
|
puts("Error: no I2C device initialized");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
if (argc < 3) {
|
|
|
|
puts("Error: not enough arguments given");
|
2017-09-06 21:21:25 +02:00
|
|
|
printf("Usage:\n%s ADDR LENGTH\n", argv[0]);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
addr = atoi(argv[1]);
|
2014-10-09 10:40:31 +02:00
|
|
|
length = atoi(argv[2]);
|
|
|
|
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
if (length < 1 || length > (int)BUFSIZE) {
|
2014-10-09 10:40:31 +02:00
|
|
|
puts("Error: invalid LENGTH parameter given\n");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else if (length == 1) {
|
2017-09-06 21:21:25 +02:00
|
|
|
printf("i2c_read_byte(I2C_%i, 0x%02x)\n", i2c_dev, addr);
|
2014-10-09 10:40:31 +02:00
|
|
|
res = i2c_read_byte(i2c_dev, addr, data);
|
|
|
|
}
|
|
|
|
else {
|
2017-09-06 21:21:25 +02:00
|
|
|
printf("i2c_read_bytes(I2C_%i, 0x%02x, %i)\n", i2c_dev, addr, length);
|
2014-10-09 10:40:31 +02:00
|
|
|
res = i2c_read_bytes(i2c_dev, addr, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 1) {
|
|
|
|
puts("Error: no bytes were read");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("I2C_%i: successfully read %i bytes:\n [", i2c_dev, res);
|
|
|
|
for (int i = 0; i < res; i++) {
|
2016-09-30 23:01:46 +02:00
|
|
|
printf("0x%02x, ", data[i]);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
2017-09-06 21:21:25 +02:00
|
|
|
puts("]");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_read_reg(int argc, char **argv)
|
2014-10-09 10:40:31 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uint8_t addr, reg;
|
|
|
|
int length;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t data[BUFSIZE];
|
2014-10-09 10:40:31 +02:00
|
|
|
|
|
|
|
if (i2c_dev < 0) {
|
|
|
|
puts("Error: no I2C device initialized");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
if (argc < 4) {
|
|
|
|
puts("Error: not enough arguments given");
|
2017-09-06 21:21:25 +02:00
|
|
|
printf("Usage:\n%s ADDR REG LENGTH\n", argv[0]);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:01:46 +02:00
|
|
|
addr = atoi(argv[1]);
|
|
|
|
reg = atoi(argv[2]);
|
2014-10-09 10:40:31 +02:00
|
|
|
length = atoi(argv[3]);
|
|
|
|
|
make: fix sign-compare errors
cpu, nrf5x_common: fix sign-compare in periph/flashpage
drivers, periph_common: fix sign-compare in flashpage
cpu, sam0_common: fix sign-compare error in periph/gpio
cpu, cc2538: fix sign-compare in periph/timer
cpu, sam3: fix sign-compare in periph/gpio
cpu, stm32_common: fix sign-compare in periph/pwm
cpu, stm32_common: fix sign-compare in periph/timer
cpu, stm32_common: fix sign-compare in periph/flashpage
cpu, nrf5x_common: fix sign-compare in radio/nrfmin
cpu, samd21: fix sign-compare in periph/pwm
cpu, ezr32wg: fix sign-compare in periph/gpio
cpu, ezr32wg: fix sign-compare in periph/timer
drivers, ethos: fix sign-compare
sys, net: fix sign-compare
cpu, atmega_common: fix sign-compare error
cpu, msp430fxyz: fix sign-compare in periph/gpio
boards, msb-430-common: fix sign-compare in board_init
driver, cc2420: fix sign-compared
sys/net: fix sign-compare in gnrc_tftp
driver, pcd8544: fix sign-compare
driver, pn532: fix sign-compare
driver, sdcard_spi: fix sign-compare
tests: fix sign_compare
sys/net, lwmac: fix sign_compare
pkg, lwip: fix sign-compare
boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error
tests, sock_ip: fix sign compare
tests, msg_avail: fix sign compare
tests, sock_udp: fix sign compare
boards: fix sign-compare for calliope and microbit matrix
2017-10-31 11:57:40 +01:00
|
|
|
if (length < 1 || length > (int)BUFSIZE) {
|
2014-10-09 10:40:31 +02:00
|
|
|
puts("Error: invalid LENGTH parameter given");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else if (length == 1) {
|
2017-09-06 21:21:25 +02:00
|
|
|
printf("i2c_read_reg(I2C_%i, 0x%02x, 0x%02x)\n", i2c_dev, addr, reg);
|
2014-10-09 10:40:31 +02:00
|
|
|
res = i2c_read_reg(i2c_dev, addr, reg, data);
|
|
|
|
}
|
|
|
|
else {
|
2017-09-06 21:21:25 +02:00
|
|
|
printf("i2c_read_regs(I2C_%i, 0x%02x, 0x%02x, %i)\n", i2c_dev, addr, reg, length);
|
2014-10-09 10:40:31 +02:00
|
|
|
res = i2c_read_regs(i2c_dev, addr, reg, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 1) {
|
|
|
|
puts("Error: no bytes were read");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("I2C_%i: successfully read %i bytes from reg 0x%02x:\n [", i2c_dev, res, reg);
|
|
|
|
for (int i = 0; i < res; i++) {
|
2016-09-30 23:01:46 +02:00
|
|
|
printf("0x%02x, ", data[i]);
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
2017-09-06 21:21:25 +02:00
|
|
|
puts("]");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-10-09 10:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static const shell_command_t shell_commands[] = {
|
|
|
|
{ "init_master", "Initialize I2C as master", cmd_init_master },
|
|
|
|
{ "w", "write bytes to given address", cmd_write },
|
|
|
|
{ "wr", "write to register ", cmd_write_reg },
|
|
|
|
{ "r", "read bytes from given address", cmd_read },
|
|
|
|
{ "rr", "read bytes from register", cmd_read_reg },
|
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
puts("Test for the low-level I2C driver");
|
|
|
|
|
|
|
|
/* define own shell commands */
|
2015-07-14 18:42:46 +02:00
|
|
|
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
|
|
|
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
|
2014-10-09 10:40:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|