]> sigrok.org Git - sigrok-dumps.git/blob - spi/nrf24l01/gen-testfiles.py
ad5626: Fix pin names as per datasheet.
[sigrok-dumps.git] / spi / nrf24l01 / gen-testfiles.py
1 #!/usr/bin/env python3
2 ##
3 ## This file is part of the sigrok-dumps project.
4 ##
5 ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
6 ##
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ## GNU General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
19 ##
20
21 ## This script generates test cases for the nrf24l01 protocol decoder.
22
23 import csv
24 import subprocess
25 import tempfile
26
27 class SPI:
28     def __init__(self, filename):
29         self._filename = filename
30         self._data = []
31
32     def CSlow(self):
33         self._data.append([1, 0, 0, 0])
34         self._data.append([0, 0, 0, 0])
35
36     def CShigh(self):
37         self._data.append([0, 0, 0, 0])
38         self._data.append([1, 0, 0, 0])
39
40     def add(self, mosi, miso):
41         for _ in range(8):
42             mo = (mosi & 0x80) >> 7
43             mi = (miso & 0x80) >> 7
44             mosi <<= 1
45             miso <<= 1
46             self._data.append([0, 0, mo, mi])
47             self._data.append([0, 1, mo, mi])
48
49         self._data.append([0, 0, 0, 0])
50
51     def write(self):
52         with tempfile.NamedTemporaryFile() as tf:
53             with open(tf.name, 'w') as tff:
54                 w = csv.writer(tff)
55                 w.writerow(['CS', 'CLK', 'MOSI', 'MISO'])
56                 w.writerows(self._data)
57
58             fn = '{}.sr'.format(self._filename)
59             I = 'csv:header=true:samplerate=1000'
60             subprocess.check_call(['sigrok-cli', '-I', I, '-i', tf.name, '-o', fn])
61
62 spi = SPI('nrf24l01-test-activate')
63 spi.CSlow()
64 spi.add(0x50, 0x00) # ACTIVATE
65 spi.add(0x73, 0x00) # correct payload
66 spi.CShigh()
67 spi.CSlow()
68 spi.add(0x50, 0x00) # ACTIVATE
69 spi.add(0x74, 0x00) # wrong payload
70 spi.CShigh()
71 spi.write()
72
73 spi = SPI('nrf24l01-test-excess-bytes')
74 spi.CSlow()
75 spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
76 spi.add(0x00, 0x00)
77 spi.CShigh()
78 spi.CSlow()
79 spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
80 spi.add(0x00, 0x00)
81 spi.add(0x00, 0x00) # excess
82 spi.CShigh()
83 spi.CSlow()
84 spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
85 spi.add(0x00, 0x00)
86 spi.add(0x00, 0x00) # excess
87 spi.CShigh()
88 spi.CSlow()
89 spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
90 spi.add(0x00, 0x00)
91 spi.add(0x00, 0x00) # excess
92 spi.add(0x00, 0x00) # excess
93 spi.CShigh()
94 spi.CSlow()
95 spi.add(0x2a, 0x00) # W_REGISTER, reg = RX_ADDR_P0
96 spi.add(0x00, 0x00)
97 spi.add(0x00, 0x00)
98 spi.add(0x00, 0x00)
99 spi.add(0x00, 0x00)
100 spi.add(0x00, 0x00)
101 spi.add(0x00, 0x00) # excess
102 spi.CShigh()
103 spi.CSlow()
104 spi.add(0x2c, 0x00) # W_REGISTER, reg = RX_ADDR_P2
105 spi.add(0x00, 0x00)
106 spi.add(0x00, 0x00) # excess
107 spi.CShigh()
108 spi.CSlow()
109 spi.add(0xa0, 0x00) # W_ACK_PAYLOAD, pipe = 0
110 for i in range(33):
111     spi.add(i, 0x00) # write 33 bytes, command only expects 32
112 spi.CShigh()
113 spi.write()
114
115 spi = SPI('nrf24l01-test-missing-bytes')
116 spi.CSlow()
117 spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
118 spi.CShigh()
119 spi.CSlow()
120 spi.add(0xb0, 0x00) # W_TX_PAYLOAD_NOACK
121 spi.CShigh()
122 spi.write()
123
124 spi = SPI('nrf24l01-test-no-command')
125 spi.CSlow()
126 spi.CShigh()
127 spi.CSlow()
128 spi.CShigh()
129 spi.CSlow()
130 spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
131 spi.add(0x00, 0x00)
132 spi.CShigh()
133 spi.CSlow()
134 spi.CShigh()
135 spi.CSlow()
136 spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
137 spi.add(0x00, 0x00)
138 spi.CShigh()
139 spi.write()
140
141 spi = SPI('nrf24l01-test-unknown-register')
142 spi.CSlow()
143 spi.add(0x1f, 0x00) # R_REGISTER, reg = 0x1f
144 spi.add(0x00, 0x00)
145 spi.CShigh()
146 spi.write()
147
148 spi = SPI('nrf24l01-test-unknown-command')
149 spi.CSlow()
150 spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
151 spi.add(0x00, 0x00)
152 spi.CShigh()
153 spi.CSlow()
154 spi.add(0xf0, 0x00) # wrong command
155 spi.add(0x00, 0x00)
156 spi.CShigh()
157 spi.CSlow()
158 spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
159 spi.add(0x00, 0x00)
160 spi.CShigh()
161 spi.write()
162
163 spi = SPI('nrf24l01-test-misc')
164 spi.CSlow()
165 spi.add(0xe3, 0x00) # REUSE_TX_PL
166 spi.CShigh()
167 spi.CSlow()
168 spi.add(0x60, 0x00) # R_RX_PL_WID
169 spi.add(0x00, 0x09)
170 spi.CShigh()
171 spi.CSlow()
172 spi.add(0x60, 0x00) # R_RX_PL_WID
173 spi.CShigh()
174 spi.CSlow()
175 spi.add(0x60, 0x00) # R_RX_PL_WID
176 spi.add(0x00, 0x09)
177 spi.add(0x00, 0x09) # excess
178 spi.CShigh()
179 spi.CSlow()
180 spi.add(0xa9, 0x00) # W_ACK_PAYLOAD, pipe = 1
181 for i in range(5):
182     spi.add(i, 0x00)
183 for c in 'abcdef':
184     spi.add(ord(c), 0x00)
185 for i in range(5):
186     spi.add(i, 0x00)
187 spi.CShigh()
188 spi.write()
189
190 spi = SPI('nrf24l01-test-incomplete-cmd')
191 spi.add(0xff, 0xff) # some bytes from a command
192 spi.add(0xff, 0xff) # that was captured incompletely
193 spi.CShigh()
194 spi.CSlow()
195 spi.add(0xe1, 0x00) # FLUSH_TX
196 spi.CShigh()
197 spi.write()