summaryrefslogtreecommitdiff
path: root/loragw_spi_stress/src/loragw_spi_stress.c
blob: 2c9f2e2bbb4c00babad469e7ebd2888b84648e85 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    ©2013 Semtech-Cycleo

Description:
	SPI stress test

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


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

/* fix an issue between POSIX and C99 */
#if __STDC_VERSION__ >= 199901L
	#define _XOPEN_SOURCE 600
#else
	#define _XOPEN_SOURCE 500
#endif

#include <stdint.h>		/* C99 types */
#include <stdbool.h>	/* bool type */
#include <stdio.h>		/* printf fprintf sprintf fopen fputs */

#include <signal.h>		/* sigaction */
#include <unistd.h>		/* getopt access */
#include <stdlib.h>		/* rand */

#include "loragw_reg.h"

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

#define ARRAY_SIZE(a)	(sizeof(a) / sizeof((a)[0]))
#define MSG(args...)	fprintf(stderr, args) /* message that is destined to the user */

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

#define		VERS				103
#define		READS_WHEN_ERROR	16 /* number of times a read is repeated if there is a read error */
#define		BUFF_SIZE			1024

/* -------------------------------------------------------------------------- */
/* --- PRIVATE VARIABLES (GLOBAL) ------------------------------------------- */

/* signal handling variables */
struct sigaction sigact; /* SIGQUIT&SIGINT&SIGTERM signal handling */
static int exit_sig = 0; /* 1 -> application terminates cleanly (shut down hardware, close open files, etc) */
static int quit_sig = 0; /* 1 -> application terminates without shutting down the hardware */

/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DECLARATION ---------------------------------------- */

static void sig_handler(int sigio);

void usage (void);

/* -------------------------------------------------------------------------- */
/* --- PRIVATE FUNCTIONS DEFINITION ----------------------------------------- */

static void sig_handler(int sigio) {
	if (sigio == SIGQUIT) {
		quit_sig = 1;;
	} else if ((sigio == SIGINT) || (sigio == SIGTERM)) {
		exit_sig = 1;
	}
}

/* describe command line options */
void usage(void) {
	MSG( "Available options:\n");
	MSG( " -h print this help\n");
	MSG( " -t <int> specify which test you want to run (1-4)\n");
}

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

int main(int argc, char **argv)
{
	int i;
	int xi = 0;
	
	/* application option */
	int test_number = 1;
	int cycle_number = 0;
	int repeats_per_cycle = 1000;
	bool error = false;
	
	/* in/out variables */
	int32_t test_value;
	int32_t read_value;
	int32_t rb1, rb2, rb3; /* interstitial readbacks, to flush buffers if needed */
	
	/* data buffer */
	int32_t test_addr;
	uint8_t test_buff[BUFF_SIZE];
	uint8_t read_buff[BUFF_SIZE];
	
	/* parse command line options */
	while ((i = getopt (argc, argv, "ht:")) != -1) {
		switch (i) {
			case 'h':
				usage();
				return EXIT_FAILURE;
				break;
			
			case 't':
				i = sscanf(optarg, "%i", &xi);
				if ((i != 1) || (xi < 1) || (xi > 4)) {
					MSG("ERROR: invalid test number\n");
					return EXIT_FAILURE;
				} else {
					test_number = xi;
				}
				break;
			
			default:
				MSG("ERROR: argument parsing use -h option for help\n");
				usage();
				return EXIT_FAILURE;
		}
	}
	MSG("INFO: Starting Lora concentrator SPI stress-test number %i\n", test_number);
	
	/* configure signal handling */
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = 0;
	sigact.sa_handler = sig_handler;
	sigaction(SIGQUIT, &sigact, NULL);
	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);
	
	/* start SPI link */
	i = lgw_connect();
	if (i != LGW_REG_SUCCESS) {
		MSG("ERROR: lgw_connect() did not return SUCCESS");
		return EXIT_FAILURE;
	}
	
	if (test_number == 1) {
		/* single 8b register R/W stress test */
		while ((quit_sig != 1) && (exit_sig != 1)) {
			printf("Cycle %i > ", cycle_number);
			for (i=0; i<repeats_per_cycle; ++i) {
				test_value = (rand() % 256);
				lgw_reg_w(LGW_IMPLICIT_PAYLOAD_LENGHT, test_value);
				lgw_reg_r(LGW_IMPLICIT_PAYLOAD_LENGHT, &read_value);
				if (read_value != test_value) {
					error = true;
					break;
				}
			}
			if (error) {
				printf("error during the %ith iteration: write 0x%02X, read 0x%02X\n", i+1, test_value, read_value);
				printf("Repeat read of target register:");
				for (i=0; i<READS_WHEN_ERROR; ++i) {
					lgw_reg_r(LGW_IMPLICIT_PAYLOAD_LENGHT, &read_value);
					printf(" 0x%02X", read_value);
				}
				printf("\n");
				return EXIT_FAILURE;
			} else {
				printf("did %i R/W on an 8 bits reg with no error\n", repeats_per_cycle);
				++cycle_number;
			}
		}
	} else if (test_number == 2) {
		/* single 8b register R/W with interstitial VERSION check stress test */
		while ((quit_sig != 1) && (exit_sig != 1)) {
			printf("Cycle %i > ", cycle_number);
			for (i=0; i<repeats_per_cycle; ++i) {
				test_value = (rand() % 256);
				lgw_reg_r(LGW_VERSION, &rb1);
				lgw_reg_w(LGW_IMPLICIT_PAYLOAD_LENGHT, test_value);
				lgw_reg_r(LGW_VERSION, &rb2);
				lgw_reg_r(LGW_IMPLICIT_PAYLOAD_LENGHT, &read_value);
				lgw_reg_r(LGW_VERSION, &rb3);
				if ((rb1 != VERS) || (rb2 != VERS) || (rb3 != VERS) || (read_value != test_value)) {
					error = true;
					break;
				}
			}
			if (error) {
				printf("error during the %ith iteration: write %02X, read %02X, version (%i, %i, %i)\n", i+1, test_value, read_value, rb1, rb2, rb3);
				printf("Repeat read of target register:");
				for (i=0; i<READS_WHEN_ERROR; ++i) {
					lgw_reg_r(LGW_IMPLICIT_PAYLOAD_LENGHT, &read_value);
					printf(" 0x%02X", read_value);
				}
				printf("\n");
				return EXIT_FAILURE;
			} else {
				printf("did %i R/W on an 8 bits reg with no error\n", repeats_per_cycle);
				++cycle_number;
			}
		}
	} else if (test_number == 3) {
		/* 32b register R/W stress test */
		while ((quit_sig != 1) && (exit_sig != 1)) {
			printf("Cycle %i > ", cycle_number);
			for (i=0; i<repeats_per_cycle; ++i) {
				test_value = (rand() & 0x0000FFFF);
				test_value += (int32_t)(rand() & 0x0000FFFF) << 16;
				lgw_reg_w(LGW_FSK_REF_PATTERN_LSB, test_value);
				lgw_reg_r(LGW_FSK_REF_PATTERN_LSB, &read_value);
				if (read_value != test_value) {
					error = true;
					break;
				}
			}
			if (error) {
				printf("error during the %ith iteration: write 0x%08X, read 0x%08X\n", i+1, test_value, read_value);
				printf("Repeat read of target register:");
				for (i=0; i<READS_WHEN_ERROR; ++i) {
					lgw_reg_r(LGW_FSK_REF_PATTERN_LSB, &read_value);
					printf(" 0x%08X", read_value);
				}
				printf("\n");
				return EXIT_FAILURE;
			} else {
				printf("did %i R/W on a 32 bits reg with no error\n", repeats_per_cycle);
				++cycle_number;
			}
		}
	} else if (test_number == 4) {
		/* databuffer R/W stress test */
		while ((quit_sig != 1) && (exit_sig != 1)) {
			for (i=0; i<BUFF_SIZE; ++i) {
				test_buff[i] = rand() & 0xFF;
			}
			printf("Cycle %i > ", cycle_number);
			test_addr = rand() & 0xFFFF;
			lgw_reg_w(LGW_RX_DATA_BUF_ADDR, test_addr); /* write at random offset in memory */
			lgw_reg_wb(LGW_RX_DATA_BUF_DATA, test_buff, BUFF_SIZE);
			lgw_reg_w(LGW_RX_DATA_BUF_ADDR, test_addr); /* go back to start of segment */
			lgw_reg_rb(LGW_RX_DATA_BUF_DATA, read_buff, BUFF_SIZE);
			for (i=0; ((i<BUFF_SIZE) && (test_buff[i] == read_buff[i])); ++i);
			if (i != BUFF_SIZE) {
				printf("error during the buffer comparison\n");
				printf("Written values:\n");
				for (i=0; i<BUFF_SIZE; ++i) {
					printf(" %02X ", test_buff[i]);
					if (i%16 == 15) printf("\n");
				}
				printf("\n");
				printf("Read values:\n");
				for (i=0; i<BUFF_SIZE; ++i) {
					printf(" %02X ", read_buff[i]);
					if (i%16 == 15) printf("\n");
				}
				printf("\n");
				lgw_reg_w(LGW_RX_DATA_BUF_ADDR, test_addr); /* go back to start of segment */
				lgw_reg_rb(LGW_RX_DATA_BUF_DATA, read_buff, BUFF_SIZE);
				printf("Re-read values:\n");
				for (i=0; i<BUFF_SIZE; ++i) {
					printf(" %02X ", read_buff[i]);
					if (i%16 == 15) printf("\n");
				}
				printf("\n");
				return EXIT_FAILURE;
			} else {
				printf("did a %i-byte R/W on a data buffer with no error\n", BUFF_SIZE);
				++cycle_number;
			}
		}
	} else {
		MSG("ERROR: invalid test number");
		usage();
	}
	
	/* close SPI link */
	i = lgw_disconnect();
	if (i != LGW_REG_SUCCESS) {
		MSG("ERROR: lgw_disconnect() did not return SUCCESS");
		return EXIT_FAILURE;
	}
	
	MSG("INFO: Exiting Lora concentrator SPI stress-test program\n");
	return EXIT_SUCCESS;
}

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