]> sigrok.org Git - sigrok-dumps.git/blame - spi/nrf24l01/gen-testfiles.py
avr_isp: add dump for ATmega328/P
[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
7c43d7c6 18## along with this program; if not, see <http://www.gnu.org/licenses/>.
aeada8ac
JS
19##
20
21## This script generates test cases for the nrf24l01 protocol decoder.
22
23import csv
24import subprocess
25import tempfile
26
27class 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):
8e11d5a7
JS
42 mo = (mosi & 0x80) >> 7
43 mi = (miso & 0x80) >> 7
aeada8ac
JS
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)
8e11d5a7 59 I = 'csv:header=true:samplerate=1000'
aeada8ac
JS
60 subprocess.check_call(['sigrok-cli', '-I', I, '-i', tf.name, '-o', fn])
61
62spi = SPI('nrf24l01-test-activate')
63spi.CSlow()
64spi.add(0x50, 0x00) # ACTIVATE
65spi.add(0x73, 0x00) # correct payload
66spi.CShigh()
67spi.CSlow()
68spi.add(0x50, 0x00) # ACTIVATE
69spi.add(0x74, 0x00) # wrong payload
70spi.CShigh()
71spi.write()
72
73spi = SPI('nrf24l01-test-excess-bytes')
74spi.CSlow()
75spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
76spi.add(0x00, 0x00)
77spi.CShigh()
78spi.CSlow()
79spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
80spi.add(0x00, 0x00)
81spi.add(0x00, 0x00) # excess
82spi.CShigh()
83spi.CSlow()
84spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
85spi.add(0x00, 0x00)
86spi.add(0x00, 0x00) # excess
87spi.CShigh()
88spi.CSlow()
89spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
90spi.add(0x00, 0x00)
91spi.add(0x00, 0x00) # excess
92spi.add(0x00, 0x00) # excess
93spi.CShigh()
94spi.CSlow()
95spi.add(0x2a, 0x00) # W_REGISTER, reg = RX_ADDR_P0
96spi.add(0x00, 0x00)
97spi.add(0x00, 0x00)
98spi.add(0x00, 0x00)
99spi.add(0x00, 0x00)
100spi.add(0x00, 0x00)
101spi.add(0x00, 0x00) # excess
102spi.CShigh()
103spi.CSlow()
104spi.add(0x2c, 0x00) # W_REGISTER, reg = RX_ADDR_P2
105spi.add(0x00, 0x00)
106spi.add(0x00, 0x00) # excess
107spi.CShigh()
108spi.CSlow()
109spi.add(0xa0, 0x00) # W_ACK_PAYLOAD, pipe = 0
110for i in range(33):
111 spi.add(i, 0x00) # write 33 bytes, command only expects 32
112spi.CShigh()
113spi.write()
114
115spi = SPI('nrf24l01-test-missing-bytes')
116spi.CSlow()
117spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
118spi.CShigh()
119spi.CSlow()
120spi.add(0xb0, 0x00) # W_TX_PAYLOAD_NOACK
121spi.CShigh()
122spi.write()
123
124spi = SPI('nrf24l01-test-no-command')
125spi.CSlow()
126spi.CShigh()
127spi.CSlow()
128spi.CShigh()
129spi.CSlow()
130spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
131spi.add(0x00, 0x00)
132spi.CShigh()
133spi.CSlow()
134spi.CShigh()
135spi.CSlow()
136spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
137spi.add(0x00, 0x00)
138spi.CShigh()
139spi.write()
140
141spi = SPI('nrf24l01-test-unknown-register')
142spi.CSlow()
143spi.add(0x1f, 0x00) # R_REGISTER, reg = 0x1f
144spi.add(0x00, 0x00)
145spi.CShigh()
146spi.write()
147
148spi = SPI('nrf24l01-test-unknown-command')
149spi.CSlow()
150spi.add(0x00, 0x00) # R_REGISTER, reg = CONFIG
151spi.add(0x00, 0x00)
152spi.CShigh()
153spi.CSlow()
154spi.add(0xf0, 0x00) # wrong command
155spi.add(0x00, 0x00)
156spi.CShigh()
157spi.CSlow()
158spi.add(0x20, 0x00) # W_REGISTER, reg = CONFIG
159spi.add(0x00, 0x00)
160spi.CShigh()
161spi.write()
162
163spi = SPI('nrf24l01-test-misc')
164spi.CSlow()
165spi.add(0xe3, 0x00) # REUSE_TX_PL
166spi.CShigh()
167spi.CSlow()
168spi.add(0x60, 0x00) # R_RX_PL_WID
169spi.add(0x00, 0x09)
170spi.CShigh()
171spi.CSlow()
172spi.add(0x60, 0x00) # R_RX_PL_WID
173spi.CShigh()
174spi.CSlow()
175spi.add(0x60, 0x00) # R_RX_PL_WID
176spi.add(0x00, 0x09)
177spi.add(0x00, 0x09) # excess
178spi.CShigh()
179spi.CSlow()
180spi.add(0xa9, 0x00) # W_ACK_PAYLOAD, pipe = 1
181for i in range(5):
182 spi.add(i, 0x00)
183for c in 'abcdef':
184 spi.add(ord(c), 0x00)
185for i in range(5):
186 spi.add(i, 0x00)
187spi.CShigh()
188spi.write()
a7744064
JS
189
190spi = SPI('nrf24l01-test-incomplete-cmd')
191spi.add(0xff, 0xff) # some bytes from a command
192spi.add(0xff, 0xff) # that was captured incompletely
193spi.CShigh()
194spi.CSlow()
195spi.add(0xe1, 0x00) # FLUSH_TX
196spi.CShigh()
197spi.write()