summaryrefslogtreecommitdiff
path: root/libloragw/tst/test_loragw_reg.c
blob: fb047fbee399842d88b14e9e1010175303ad13ab (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
/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    ©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 "loragw_reg.h"

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

#define BURST_TEST_LENGTH	8192

int main()
{
	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;
	
	printf("Beginning of test for loragw_reg.c\n");
	
	lgw_connect();
	/* 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 ------------------------------------------------------------------ */