2014-08-07 16:51:52 +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
|
2014-08-14 18:32:33 +02:00
|
|
|
* @brief Application for testing low-level SPI driver implementations
|
|
|
|
*
|
|
|
|
* This implementation covers both, master and slave configurations.
|
2014-08-07 16:51:52 +02:00
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-08-14 18:32:33 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-08-07 16:51:52 +02:00
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
#include "board.h"
|
2014-08-07 16:51:52 +02:00
|
|
|
#include "shell.h"
|
|
|
|
#include "periph/spi.h"
|
|
|
|
#include "periph/gpio.h"
|
|
|
|
|
|
|
|
#define SHELL_BUFSIZE (128U)
|
|
|
|
|
|
|
|
enum {
|
|
|
|
READ = 0,
|
|
|
|
WRITE,
|
|
|
|
INIT
|
|
|
|
} rw;
|
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
static int spi_dev = -1;
|
|
|
|
static int spi_cs = -1;
|
|
|
|
static int spi_mode = -1;
|
|
|
|
static int spi_speed = -1;
|
|
|
|
static int spi_master = -1; /* 0 for slave, 1 for master, -1 for not initialized */
|
|
|
|
|
|
|
|
static char buffer[256]; /* temporary buffer */
|
2014-10-17 10:09:25 +02:00
|
|
|
static char rx_buffer[256]; /* global receive buffer */
|
|
|
|
static int rx_counter = 0;
|
2014-08-14 18:32:33 +02:00
|
|
|
|
2014-08-07 16:51:52 +02:00
|
|
|
static volatile int state;
|
|
|
|
static char* mem = "Hello Master! abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
int parse_spi_dev(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* reset default values */
|
|
|
|
spi_dev = SPI_0;
|
|
|
|
spi_mode = SPI_CONF_FIRST_RISING;
|
|
|
|
spi_speed = SPI_SPEED_1MHZ;
|
2014-08-07 16:51:52 +02:00
|
|
|
|
2015-01-16 11:15:42 +01:00
|
|
|
if (argc < 3 || argc > 5) {
|
2014-08-14 18:32:33 +02:00
|
|
|
printf("usage: %s DEV CS [MODE [SPEED]]\n", argv[0]);
|
|
|
|
puts(" DEV is the SPI device to use:");
|
|
|
|
for (int i = 0; i < SPI_NUMOF; i++) {
|
|
|
|
printf(" %i - SPI_%i\n", i, i);
|
|
|
|
}
|
|
|
|
puts(" CS is the GPIO used for the chip-select signal:");
|
|
|
|
for (int i = 0; i < GPIO_NUMOF; i++) {
|
|
|
|
printf(" %i - GPIO_%i\n", i, i);
|
|
|
|
}
|
|
|
|
puts(" MODE must be one of the following options (* marks default value):");
|
|
|
|
puts(" *0 - POL:0, PHASE:0 - ON FIRST RISING EDGE");
|
|
|
|
puts(" 1 - POL:0, PHASE:1 - ON SECOND RISING EDGE");
|
|
|
|
puts(" 2 - POL:1, PHASE:0 - ON FIRST FALLING EDGE");
|
|
|
|
puts(" 3 - POL:1, PHASE:1 - on second falling edge");
|
|
|
|
puts(" SPEED must be one of the following options (only used in master mode):");
|
|
|
|
puts(" 0 - 100 KHz");
|
|
|
|
puts(" 1 - 400 KHz");
|
|
|
|
puts(" *2 - 1 MHz");
|
|
|
|
puts(" 3 - 5 MHz");
|
|
|
|
puts(" 4 - 10 MHz\n");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
spi_dev = atoi(argv[1]);
|
|
|
|
if (spi_dev < 0 || spi_dev >= SPI_NUMOF) {
|
|
|
|
puts("error: invalid DEV value given");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
spi_cs = atoi(argv[2]);
|
|
|
|
if (spi_dev < 0 || spi_dev >= GPIO_NUMOF) {
|
|
|
|
puts("error: invalid CS value given");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (argc >= 4) {
|
2015-01-16 11:15:42 +01:00
|
|
|
spi_mode = argv[3][0] - '0';
|
2014-08-14 18:32:33 +02:00
|
|
|
if (spi_mode < 0 || spi_mode > 3) {
|
|
|
|
puts("error: invalid MODE value given");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc >= 5) {
|
2015-01-16 11:15:42 +01:00
|
|
|
spi_speed = argv[4][0] - '0';
|
2014-08-14 18:32:33 +02:00
|
|
|
if (spi_speed < 0 || spi_speed > 4) {
|
|
|
|
puts("error: invalid SPEED value given");
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_bytes(char* title, char* chars, int length)
|
|
|
|
{
|
|
|
|
printf("%4s", title);
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
printf(" %2i ", i);
|
|
|
|
}
|
|
|
|
printf("\n ");
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
printf(" 0x%02x", (int)chars[i]);
|
|
|
|
}
|
|
|
|
printf("\n ");
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (chars[i] < ' ' || chars[i] > '~') {
|
|
|
|
printf(" ?? ");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(" %c ", chars[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
}
|
|
|
|
|
2014-08-15 13:57:37 +02:00
|
|
|
void slave_on_cs(void *arg)
|
2014-08-07 16:51:52 +02:00
|
|
|
{
|
2014-08-15 13:57:37 +02:00
|
|
|
(void)arg;
|
|
|
|
|
2014-09-25 13:59:12 +02:00
|
|
|
LED_RED_ON;
|
2014-08-14 18:32:33 +02:00
|
|
|
spi_transmission_begin(spi_dev, 'F');
|
2014-08-07 16:51:52 +02:00
|
|
|
state = 0;
|
|
|
|
rw = INIT;
|
2014-09-25 13:59:12 +02:00
|
|
|
LED_RED_OFF;
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
char slave_on_data(char data)
|
2014-08-07 16:51:52 +02:00
|
|
|
{
|
2014-10-17 10:09:25 +02:00
|
|
|
rx_buffer[rx_counter] = data;
|
|
|
|
rx_counter++;
|
|
|
|
if (rx_counter >= 256) {
|
|
|
|
rx_counter = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-07 16:51:52 +02:00
|
|
|
switch (rw) {
|
|
|
|
case READ:
|
|
|
|
return mem[state++];
|
|
|
|
case WRITE:
|
|
|
|
mem[state++] = data;
|
|
|
|
return 'o';
|
|
|
|
case INIT:
|
|
|
|
if (data == ' ') {
|
|
|
|
rw = READ;
|
|
|
|
return mem[state++];
|
|
|
|
} else if (data & 0x80) {
|
|
|
|
rw = WRITE;
|
|
|
|
state = (data & 0x7f);
|
|
|
|
return 'W';
|
|
|
|
} else {
|
|
|
|
rw = READ;
|
|
|
|
state = data;
|
|
|
|
return mem[state++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'e';
|
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_init_master(int argc, char **argv)
|
2014-08-07 16:51:52 +02:00
|
|
|
{
|
2014-08-14 18:32:33 +02:00
|
|
|
int res;
|
|
|
|
spi_master = -1;
|
|
|
|
if (parse_spi_dev(argc, argv) < 0) {
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
2015-01-16 16:05:04 +01:00
|
|
|
spi_acquire(spi_dev);
|
2014-08-14 18:32:33 +02:00
|
|
|
res = spi_init_master(spi_dev, spi_mode, spi_speed);
|
2015-01-16 16:05:04 +01:00
|
|
|
spi_release(spi_dev);
|
2014-08-14 18:32:33 +02:00
|
|
|
if (res < 0) {
|
|
|
|
printf("spi_init_master: error initializing SPI_%i device (code %i)\n", spi_dev, res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
|
|
|
res = gpio_init_out(spi_cs, GPIO_PULLUP);
|
|
|
|
if (res < 0){
|
|
|
|
printf("gpio_init_out: error initializing GPIO_%i as CS line (code %i)\n", spi_cs, res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
|
|
|
gpio_set(spi_cs);
|
|
|
|
spi_master = 1;
|
|
|
|
printf("SPI_%i successfully initialized as master, cs: GPIO_%i, mode: %i, speed: %i\n",
|
|
|
|
spi_dev, spi_cs, spi_mode, spi_speed);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_init_slave(int argc, char **argv)
|
2014-08-07 16:51:52 +02:00
|
|
|
{
|
2014-08-14 18:32:33 +02:00
|
|
|
int res;
|
|
|
|
spi_master = -1;
|
|
|
|
if (parse_spi_dev(argc, argv) < 0) {
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
2015-01-16 16:05:04 +01:00
|
|
|
spi_acquire(spi_dev);
|
2014-08-14 18:32:33 +02:00
|
|
|
res = spi_init_slave(spi_dev, spi_mode, slave_on_data);
|
2015-01-16 16:05:04 +01:00
|
|
|
spi_release(spi_dev);
|
2014-08-14 18:32:33 +02:00
|
|
|
if (res < 0) {
|
|
|
|
printf("spi_init_slave: error initializing SPI_%i device (code: %i)\n", spi_dev, res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
2014-08-15 13:57:37 +02:00
|
|
|
res = gpio_init_int(spi_cs, GPIO_NOPULL, GPIO_FALLING, slave_on_cs, 0);
|
2014-08-14 18:32:33 +02:00
|
|
|
if (res < 0){
|
|
|
|
printf("gpio_init_int: error initializing GPIO_%i as CS line (code %i)\n", spi_cs, res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
|
|
|
spi_master = 0;
|
|
|
|
printf("SPI_%i successfully initialized as slave, cs: GPIO_%i, mode: %i\n",
|
|
|
|
spi_dev, spi_cs, spi_mode);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_transfer(int argc, char **argv)
|
2014-08-07 16:51:52 +02:00
|
|
|
{
|
|
|
|
int res;
|
2014-08-14 18:32:33 +02:00
|
|
|
char *hello = "Hello";
|
2014-08-07 16:51:52 +02:00
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
if (spi_master != 1) {
|
|
|
|
puts("error: node is not initialized as master, please do so first");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
2014-08-07 16:51:52 +02:00
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
puts("No data to transfer given, will transfer 'Hello' to device");
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-08-14 18:32:33 +02:00
|
|
|
hello = argv[1];
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
/* do the actual data transfer */
|
2015-01-16 16:05:04 +01:00
|
|
|
spi_acquire(spi_dev);
|
2014-08-14 18:32:33 +02:00
|
|
|
gpio_clear(spi_cs);
|
|
|
|
res = spi_transfer_bytes(spi_dev, hello, buffer, strlen(hello));
|
|
|
|
gpio_set(spi_cs);
|
2015-01-16 16:05:04 +01:00
|
|
|
spi_release(spi_dev);
|
2014-08-14 18:32:33 +02:00
|
|
|
|
|
|
|
/* look at the results */
|
|
|
|
if (res < 0) {
|
|
|
|
printf("error: unable to transfer data to slave (code: %i)\n", res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-08-14 18:32:33 +02:00
|
|
|
printf("Transfered %i bytes:\n", res);
|
|
|
|
print_bytes("MOSI", hello, res);
|
|
|
|
print_bytes("MISO", buffer, res);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-08-07 16:51:52 +02:00
|
|
|
}
|
2014-08-14 18:32:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
int cmd_print(int argc, char **argv)
|
2014-10-17 10:09:25 +02:00
|
|
|
{
|
|
|
|
if (spi_master != 0) {
|
|
|
|
puts("error: node is not initialized as slave");
|
2015-03-20 08:51:45 +01:00
|
|
|
return 1;
|
2014-10-17 10:09:25 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Received %i bytes:\n", rx_counter);
|
|
|
|
print_bytes("MOSI", rx_buffer, rx_counter);
|
|
|
|
}
|
|
|
|
rx_counter = 0;
|
|
|
|
memset(&rx_buffer, 0, 256);
|
2015-03-20 08:51:45 +01:00
|
|
|
return 0;
|
2014-10-17 10:09:25 +02:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:32:33 +02:00
|
|
|
int shell_getchar(void)
|
|
|
|
{
|
|
|
|
return (int)getchar();
|
|
|
|
}
|
|
|
|
|
|
|
|
void shell_putchar(int c)
|
|
|
|
{
|
|
|
|
putchar((char)c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const shell_command_t shell_commands[] = {
|
|
|
|
{ "init_master", "Initialize node as SPI master", cmd_init_master },
|
|
|
|
{ "init_slave", "Initialize node as SPI slave", cmd_init_slave },
|
|
|
|
{ "send", "Transfer string to slave (only in master mode)", cmd_transfer },
|
2014-10-17 10:09:25 +02:00
|
|
|
{ "print_rx", "Print the received string (only in slave mode)", cmd_print },
|
2014-08-14 18:32:33 +02:00
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
shell_t shell;
|
|
|
|
|
|
|
|
puts("\nRIOT low-level SPI driver test");
|
|
|
|
puts("This application enables you to test a platforms SPI driver implementation.");
|
|
|
|
puts("Enter 'help' to get started\n");
|
2014-08-07 16:51:52 +02:00
|
|
|
|
|
|
|
/* run the shell */
|
|
|
|
shell_init(&shell, shell_commands, SHELL_BUFSIZE, shell_getchar, shell_putchar);
|
|
|
|
shell_run(&shell);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|