3 ADC10 client (currently only supports x2274 chip, on the GoodFET31
4 board, where pin 5 of JTAG header is sampled.)
6 Scott Livingston <slivingston@caltech.edu>
10 import matplotlib as mpl
11 import matplotlib.pyplot as plt
15 from GoodFET import GoodFET
18 # Some constants (not really immutable...)
27 def man_sample(count=1):
28 """Obtain a single, instantaneous ADC10 sample
29 of channel A5 (pin 5 of JTAG header on the GoodFET31 board).
31 This may be repeated count times (default is 1).
32 Result is returned as a list.
33 Failed samples are indicated by -1.
38 client.writecmd( ADC10APP, ADC10_INIT, 0 ) # Initialize ADC10 module
40 data = client.writecmd( ADC10APP, 0x83, 0 ) # Get a sample
42 samp = ord(data[0]) + (ord(data[1]) << 8)
49 def nsample(count=10, t_sample=3, clock_div=8):
50 """Obtain a sequence of count samples, at constant rate.
52 (Copied from firmware/include/adc.h)
54 t_sample is ``sample-and-hold time''; this is written directly to
55 ADC10SHTx field of ADC10CTL0 register; possible values in binary are
56 00 => 4 ADC10CLK ticks,
57 01 => 8 ADC10CLK ticks,
58 10 => 16 ADC10CLK ticks,
59 11 => 64 ADC10CLK ticks.
61 clock_div is the value by which to divide SMCLK (which is assumed to
62 be 16 MHz), then giving ADC10CLK. Possible values are 1..8
63 Cf. Fig. 20-1 (on page 585?) of the msp430x2xxx family manual.
65 To estimate the sampling rate, use
66 (SMCLK/clock_div)/(13+t_sample_ticks), where ``t_sample_ticks'' is the
67 number of ticks corresponding to t_sample (see above). E.g., 16 MHz
68 SMCLK (as on the GoodFET31, and others?), t_sample := 11 (binary) and
69 clock_div := 8 gives a sampling rate of approx. 25.97 kSps.
70 Similarly, e.g., 16 MHz, t_sample=00, clock_div=5 yields approx. 188 kSps.
73 Failure is indicated by -1 (within the list).
77 client.writecmd( ADC10APP, ADC10_INIT, 0 ) # Initialize ADC10 module
78 data = client.writecmd( ADC10APP, 0x84, 3, # ADC10_NSAMPLE
83 for k in range(client.count/2):
84 result.append( ord(data[k*2]) + (ord(data[k*2+1]) << 8) )
90 #if __name__ == "__main__":
91 if len(sys.argv) == 1:
92 print "Usage: %s verb [objects]\n" % sys.argv[0]
93 print "%s sample [N]" % sys.argv[0]
94 print "%s sequence [N]" % sys.argv[0]
96 Warning: only known to work for x2274 chip, on GoodFET31 board.
100 # Initialize and open connection to GoodFET
102 client.verbose = False # Dump activity to terminal
103 client.serInit( timeout=10 ) # UART comm timeout (in seconds)
106 # Handle each possible ADC10 verb in turn
108 if sys.argv[1] == "sample": #0x83
109 if len(sys.argv) == 2:
111 print "Requesting a single sample..."
113 count = int(sys.argv[2])
115 print "Error: number of requested samples must be at least 1."
117 print "Requesting %d samples..." % count
118 result = man_sample(count)
121 elif sys.argv[1] == "sequence":
122 if len(sys.argv) == 2:
124 print "Requesting max sampling sequence length..."
126 count = int(sys.argv[2])
128 print "Error: number of requested samples must be at least 1."
130 print "Requesting sampling sequence of length %d..." % count
131 #result = nsample(count, 0, 5)
134 result = nsample(count, 0, 5)
136 plt.plot(result,'.-')
141 #plt.savefig('figs/test-' + str(int(time.time())) + '.png')