summaryrefslogtreecommitdiff
path: root/libloragw/tst/test_loragw_spi.c
blob: 4d06eaa1359f78481b91f43f8119b8736b5cced6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
  (C)2013 Semtech-Cycleo

Description:
    Minimum test program for the loragw_spi 'library'
    Use logic analyser to check the results.

License: Revised BSD License, see LICENSE.TXT file include in the project
Maintainer: Sylvain Miermont
*/


/* -------------------------------------------------------------------------- */
/* --- DEPENDANCIES --------------------------------------------------------- */

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <getopt.h>     /* getopt_long */

#include "loragw_hal.h"
#include "loragw_spi.h"

/* -------------------------------------------------------------------------- */
/* --- PRIVATE MACROS ------------------------------------------------------- */

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

/* -------------------------------------------------------------------------- */
/* --- PRIVATE CONSTANTS ---------------------------------------------------- */

#define BURST_TEST_SIZE 2500 /* >> LGW_BURST_CHUNK */
#define TIMING_REPEAT   1    /* repeat transactions multiple times for timing characterisation */

/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */

int main(int argc, char **argv)
{
    int i;
    void *spi_target = NULL;
    uint8_t data = 0;
    uint8_t dataout[BURST_TEST_SIZE];
    uint8_t datain[BURST_TEST_SIZE];
    uint8_t spi_mux_mode = LGW_SPI_MUX_MODE0;

    /* Parameter parsing */
    int option_index = 0;
    static struct option long_options[] = {
        {"path", 1, 0, 0},
        {0, 0, 0, 0}
    };
    char arg_s[64];

    while ((i = getopt_long (argc, argv, "h", long_options, &option_index)) != -1) {
        switch (i) {
            case 'h':
                printf("~~~ Library version string~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                printf(" %s\n", lgw_version_info());
                printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                printf(" --path  <string> Path of SPIDEV e.g. /dev/spidev0.0\n");
                printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                return 0;
                break;

            case 0:
                if (strcmp(long_options[option_index].name,"path") == 0) {
                    i = sscanf(optarg, "%s", arg_s);
                    if ((i != 1) || (strncmp(arg_s, "/dev/", 5 ) != 0)) {
                        printf("ERROR: argument parsing of --path argument. Use -h to print help\n");
                        return LGW_SPI_ERROR;
                    }
                    else {
                        lgw_spi_set_path(arg_s);
                    }
                }
                else {
                    printf("ERROR: argument parsing options. Use -h to print help\n");
                    return LGW_SPI_ERROR;
                }
                break;

        default:
            printf("ERROR: argument parsing options. Use -h to print help\n");
            return LGW_SPI_ERROR;
        }
    }

    for (i = 0; i < BURST_TEST_SIZE; ++i) {
        dataout[i] = 0x30 + (i % 10); /* ASCCI code for 0 -> 9 */
        datain[i] = 0x23; /* garbage data, to be overwritten by received data */
    }

    printf("Beginning of test for loragw_spi.c\n");

    int spi_stat = lgw_spi_open(&spi_target);

    if (spi_stat != LGW_SPI_SUCCESS) {
        printf("ERROR: Unable to connect to concentrator\n");
        return LGW_SPI_ERROR;
    }

    /* normal R/W test */
    for (i = 0; i < TIMING_REPEAT; ++i) {
        spi_stat = lgw_spi_w(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0xAA, 0x96);
        if (spi_stat != LGW_SPI_SUCCESS) {
            printf("ERROR: Failed write normal R/W test\n");
            return LGW_SPI_ERROR;
        }
    }
    for (i = 0; i < TIMING_REPEAT; ++i) {
        spi_stat = lgw_spi_r(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, &data);
        if (spi_stat != LGW_SPI_SUCCESS) {
            printf("ERROR: Failed read normal R/W test\n");
            return LGW_SPI_ERROR;
        }
    }

    /* burst R/W test, small bursts << LGW_BURST_CHUNK */
    for (i = 0; i < TIMING_REPEAT; ++i) {
        spi_stat = lgw_spi_wb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, dataout, 16);
        if (spi_stat != LGW_SPI_SUCCESS) {
            printf("ERROR: Failed write small burst R/W test\n");
            return LGW_SPI_ERROR;
        }
    }
    for (i = 0; i < TIMING_REPEAT; ++i) {
        spi_stat = lgw_spi_rb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, datain, 16);
        if (spi_stat != LGW_SPI_SUCCESS) {
            printf("ERROR: Failed read small burst R/W test\n");
            return LGW_SPI_ERROR;
        }
    }

    /* burst R/W test, large bursts >> LGW_BURST_CHUNK */
    for (i = 0; i < TIMING_REPEAT; ++i) {
        spi_stat = lgw_spi_wb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x5A, dataout, ARRAY_SIZE(dataout));
        if (spi_stat != LGW_SPI_SUCCESS) {
            printf("ERROR: Failed write large burst R/W test\n");
            return LGW_SPI_ERROR;
        }
    }
    for (i = 0; i < TIMING_REPEAT; ++i) {
        spi_stat = lgw_spi_rb(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x5A, datain, ARRAY_SIZE(datain));
        if (spi_stat != LGW_SPI_SUCCESS) {
            printf("ERROR: Failed read large burst R/W test\n");
            return LGW_SPI_ERROR;
        }
    }

    /* last read (blocking), just to be sure no to quit before the FTDI buffer is flushed */
    spi_stat = lgw_spi_r(spi_target, spi_mux_mode, LGW_SPI_MUX_TARGET_SX1301, 0x55, &data);
    if (spi_stat != LGW_SPI_SUCCESS) {
        printf("ERROR: Failed to read last block\n");
        return LGW_SPI_ERROR;
    }
    lgw_spi_close(spi_target);

    printf("data received (simple read): %d\n",data);
    printf("End of test for loragw_spi.c\n");

    return 0;
}

/* --- EOF ------------------------------------------------------------------ */