summaryrefslogtreecommitdiff
path: root/libloragw/tst/test_loragw_reg.c
blob: 6248a1b9f1003678b6bdba4b7c326d607ed2a556 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
  (C)2013 Semtech-Cycleo

Description:
    Minimum test program for the loragw_spi 'library'

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


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

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

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

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

#define BURST_TEST_LENGTH    8192

/* describe command line options */
void usage(void) {
    printf("Library version information: %s\n", lgw_version_info());
    printf( "Available options:\n");
    printf( " -h print this help\n");
    printf(" --path <string> path of SPIDEV e.g. /dev/spidev0.0\n");
}

int main(int argc, char **argv)
{
    int32_t read_value, test_value;
    uint16_t lfsr;
    uint8_t burst_buffout[BURST_TEST_LENGTH];
    uint8_t burst_buffin[BURST_TEST_LENGTH];
    int i;

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

    /* parse command line options */
    while ((i = getopt_long (argc, argv, "h", long_options, &option_index)) != -1) {
        switch (i) {
            case 'h':
                usage();
                return -1;
                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 -1;
                    }
                    else {
                        lgw_spi_set_path(arg_s);
                    }
                }
                else {
                    printf("ERROR: argument parsing options. Use -h to print help\n");
                    return -1;
                }
                break;
            default:
                printf("ERROR: argument parsing\n");
                usage();
                return -1;
        }
    }


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

    int result = lgw_connect(false, 129E3);
    if (result == -1) {
        printf("ERROR: Failed to connect to the board\n");
        return -1;
    }
    /* 2 SPI transactions:
    -> 0x80 0x00        <- 0x00 0x00        forcing page 0
    -> 0x01 0x00        <- 0x00 0x64        checking version
    */

    /* --- READ TEST --- */

    lgw_reg_w(LGW_SOFT_RESET, 1);
    lgw_reg_check(stdout);

    /* --- READ/WRITE COHERENCY TEST --- */

    /* 8b unsigned */
    test_value = 197; /* 11000101b */
    lgw_reg_w(LGW_IMPLICIT_PAYLOAD_LENGHT, test_value);
    lgw_reg_r(LGW_IMPLICIT_PAYLOAD_LENGHT, &read_value);
    printf("IMPLICIT_PAYLOAD_LENGHT = %d (should be %d)\n", read_value, test_value);

    /* 8b signed */
    /* NO SUCH REG AVAILABLE */
    // /* RADIO_SELECT is normally unsigned, modify it manually in loragw_reg.c */
    // test_value = -59; /* 11000101b */
    // lgw_reg_w(LGW_RADIO_SELECT, test_value);
    // lgw_reg_r(LGW_RADIO_SELECT, &read_value);
    // printf("RADIO_SELECT = %d (should be %d)\n", read_value, test_value);

    /* less than 8b, with offset, unsigned */
    test_value = 11; /* 1011b */
    lgw_reg_w(LGW_FRAME_SYNCH_PEAK2_POS, test_value);
    lgw_reg_r(LGW_FRAME_SYNCH_PEAK2_POS, &read_value);
    printf("FRAME_SYNCH_PEAK2_POS = %d (should be %d)\n", read_value, test_value);

    /* less than 8b, with offset, signed */
    /* NO SUCH REG AVAILABLE */
    // /* MBWSSF_FRAME_SYNCH_PEAK2_POS is normally unsigned, modify it manually in loragw_reg.c */
    // test_value = -5; /* 1011b */
    // lgw_reg_w(LGW_MBWSSF_FRAME_SYNCH_PEAK2_POS, test_value);
    // lgw_reg_r(LGW_MBWSSF_FRAME_SYNCH_PEAK2_POS, &read_value);
    // printf("MBWSSF_FRAME_SYNCH_PEAK2_POS = %d (should be %d)\n", read_value, test_value);

    /* 16b unsigned */
    test_value = 49253; /* 11000000 01100101b */
    lgw_reg_w(LGW_PREAMBLE_SYMB1_NB, test_value);
    lgw_reg_r(LGW_PREAMBLE_SYMB1_NB, &read_value);
    printf("PREAMBLE_SYMB1_NB = %d (should be %d)\n", read_value, test_value);

    /* 16b signed */
    /* NO SUCH REG AVAILABLE */
    // /* CAPTURE_PERIOD is normally unsigned, modify it manually in loragw_reg.c */
    // test_value = -16283; /* 11000000 01100101b */
    // lgw_reg_w(LGW_CAPTURE_PERIOD, test_value);
    // lgw_reg_r(LGW_CAPTURE_PERIOD, &read_value);
    // printf("CAPTURE_PERIOD = %d (should be %d)\n", read_value, test_value);

    /* between 8b and 16b, unsigned */
    test_value = 3173; /* 1100 01100101b */
    lgw_reg_w(LGW_ADJUST_MODEM_START_OFFSET_SF12_RDX4, test_value);
    lgw_reg_r(LGW_ADJUST_MODEM_START_OFFSET_SF12_RDX4, &read_value);
    printf("ADJUST_MODEM_START_OFFSET_SF12_RDX4 = %d (should be %d)\n", read_value, test_value);

    /* between 8b and 16b, signed */
    test_value = -1947; /* 11000 01100101b */
    lgw_reg_w(LGW_IF_FREQ_1, test_value);
    lgw_reg_r(LGW_IF_FREQ_1, &read_value);
    printf("IF_FREQ_1 = %d (should be %d)\n", read_value, test_value);

    /* --- BURST WRITE AND READ TEST --- */

    /* initialize data for SPI test */
    lfsr = 0xFFFF;
    for(i=0; i<BURST_TEST_LENGTH; ++i) {
        burst_buffout[i] = (uint8_t)(lfsr ^ (lfsr >> 4));
        /* printf("%05d # 0x%04x 0x%02x\n", i, lfsr, burst_buffout[i]); */
        lfsr = (lfsr & 1) ? ((lfsr >> 1) ^ 0x8679) : (lfsr >> 1);
    }

    lgw_reg_wb(LGW_TX_DATA_BUF_DATA, burst_buffout, 256);
    lgw_reg_rb(LGW_RX_DATA_BUF_DATA, burst_buffin, 256);

    /* impossible to check in software,
    RX_DATA_BUF_DATA is read-only,
    TX_DATA_BUF_DATA is write only,
    use a logic analyser */

    /* --- END OF TEST --- */

    lgw_disconnect();
    /* no SPI transaction */

    printf("End of test for loragw_reg.c\n");
    return 0;
}

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