]> sigrok.org Git - sigrok-dumps.git/blame - spi/nrf24l01/gen-testfiles.py
Add nrf24l01 unit test dump.
[sigrok-dumps.git] / spi / nrf24l01 / gen-testfiles.py
CommitLineData
aeada8ac
JS
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, write to the Free Software
19## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20##
21
22## This script generates test cases for the nrf24l01 protocol decoder.
23
24import csv
25import subprocess
26import tempfile
27
28class SPI:
29 def __init__(self, filename):
30 self._filename = filename
31 self._data = []
32
33 def CSlow(self):
34 self._data.append([1, 0, 0, 0])
35 self._data.append([0, 0, 0, 0])
36
37 def CShigh(self):
38 self._data.append([0, 0, 0, 0])
39 self._data.append([1, 0, 0, 0])
40
41 def add(self, mosi, miso):
42 for _ in range(8):
43 mo = mosi & 0x80
44 mi = miso & 0x80
45 mosi <<= 1
46 miso <<= 1
47 self._data.append([0, 0, mo, mi])
48 self._data.append([0, 1, mo, mi])
49
50 self._data.append([0, 0, 0, 0])
51
52 def write(self):
53 with tempfile.NamedTemporaryFile() as tf:
54 with open(tf.name, 'w') as tff:
55 w = csv.writer(tff)
56 w.writerow(['CS', 'CLK', 'MOSI', 'MISO'])
57 w.writerows(self._data)
58
59 fn = '{}.sr'.format(self._filename)
60 I = 'csv:header=true:samplerate=1k'
61 subprocess.check_call(['sigrok-cli', '-I', I, '-i', tf.name, '-o', fn])
62
63spi = SPI('nrf24l01-test-activate')
64spi.CSlow()
65spi.add(0x50, 0x00) # ACTIVATE
66spi.add(0x73, 0x00) # correct payload
67spi.CShigh()
68spi.CSlow()
69spi.add(0x50, 0x00) # ACTIVATE
70spi.add(0x74, 0x00) # wrong payload
71spi.CShigh()
72spi.write()
73
74spi = SPI('nrf24l01-test-excess-bytes')
75spi.CSlow()
76spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
77spi.add(0x00, 0x00)
78spi.CShigh()
79spi.CSlow()
80spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
81spi.add(0x00, 0x00)
82spi.add(0x00, 0x00) # excess
83spi.CShigh()
84spi.CSlow()
85spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
86spi.add(0x00, 0x00)
87spi.add(0x00, 0x00) # excess
88spi.CShigh()
89spi.CSlow()
90spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
91spi.add(0x00, 0x00)
92spi.add(0x00, 0x00) # excess
93spi.add(0x00, 0x00) # excess
94spi.CShigh()
95spi.CSlow()
96spi.add(0x2a, 0x00) # W_REGISTER, reg = RX_ADDR_P0
97spi.add(0x00, 0x00)
98spi.add(0x00, 0x00)
99spi.add(0x00, 0x00)
100spi.add(0x00, 0x00)
101spi.add(0x00, 0x00)
102spi.add(0x00, 0x00) # excess
103spi.CShigh()
104spi.CSlow()
105spi.add(0x2c, 0x00) # W_REGISTER, reg = RX_ADDR_P2
106spi.add(0x00, 0x00)
107spi.add(0x00, 0x00) # excess
108spi.CShigh()
109spi.CSlow()
110spi.add(0xa0, 0x00) # W_ACK_PAYLOAD, pipe = 0
111for i in range(33):
112 spi.add(i, 0x00) # write 33 bytes, command only expects 32
113spi.CShigh()
114spi.write()
115
116spi = SPI('nrf24l01-test-missing-bytes')
117spi.CSlow()
118spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
119spi.CShigh()
120spi.CSlow()
121spi.add(0xb0, 0x00) # W_TX_PAYLOAD_NOACK
122spi.CShigh()
123spi.write()
124
125spi = SPI('nrf24l01-test-no-command')
126spi.CSlow()
127spi.CShigh()
128spi.CSlow()
129spi.CShigh()
130spi.CSlow()
131spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
132spi.add(0x00, 0x00)
133spi.CShigh()
134spi.CSlow()
135spi.CShigh()
136spi.CSlow()
137spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
138spi.add(0x00, 0x00)
139spi.CShigh()
140spi.write()
141
142spi = SPI('nrf24l01-test-unknown-register')
143spi.CSlow()
144spi.add(0x1f, 0x00) # R_REGISTER, reg = 0x1f
145spi.add(0x00, 0x00)
146spi.CShigh()
147spi.write()
148
149spi = SPI('nrf24l01-test-unknown-command')
150spi.CSlow()
151spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
152spi.add(0x00, 0x00)
153spi.CShigh()
154spi.CSlow()
155spi.add(0xf0, 0x00) # wrong command
156spi.add(0x00, 0x00)
157spi.CShigh()
158spi.CSlow()
159spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
160spi.add(0x00, 0x00)
161spi.CShigh()
162spi.write()
163
164spi = SPI('nrf24l01-test-misc')
165spi.CSlow()
166spi.add(0xe3, 0x00) # REUSE_TX_PL
167spi.CShigh()
168spi.CSlow()
169spi.add(0x60, 0x00) # R_RX_PL_WID
170spi.add(0x00, 0x09)
171spi.CShigh()
172spi.CSlow()
173spi.add(0x60, 0x00) # R_RX_PL_WID
174spi.CShigh()
175spi.CSlow()
176spi.add(0x60, 0x00) # R_RX_PL_WID
177spi.add(0x00, 0x09)
178spi.add(0x00, 0x09) # excess
179spi.CShigh()
180spi.CSlow()
181spi.add(0xa9, 0x00) # W_ACK_PAYLOAD, pipe = 1
182for i in range(5):
183 spi.add(i, 0x00)
184for c in 'abcdef':
185 spi.add(ord(c), 0x00)
186for i in range(5):
187 spi.add(i, 0x00)
188spi.CShigh()
189spi.write()
a7744064
JS
190
191spi = SPI('nrf24l01-test-incomplete-cmd')
192spi.add(0xff, 0xff) # some bytes from a command
193spi.add(0xff, 0xff) # that was captured incompletely
194spi.CShigh()
195spi.CSlow()
196spi.add(0xe1, 0x00) # FLUSH_TX
197spi.CShigh()
198spi.write()