-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathad-experiments.py
124 lines (95 loc) · 2.89 KB
/
ad-experiments.py
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
# -*- coding: utf-8 -*-
import time
import struct
import pigpio as io
from ADS1256_definitions import *
# Change this to the local DNS name of your Pi (often raspberrypi.local, if you have changed it) or
# make it blank to connect to localhost.
PI_HOST = 'klabs.local'
SPI_CHANNEL = 1
SPI_FLAGS = 0b0000000000000011100001
SPI_FREQUENCY = 976563
# The RPI GPIOs used: All of these are optional and must be set to None if not
# used. See datasheet.
# CS_PIN = None
#CS_PIN = 15
# pigpio uses different pin numbers!
CS_PIN = 22
# This pin makes no sense. It should not have been 11!
#DRDY_PIN = 11
# It should have been 7 per the soft mapping, but 17 per the schematic
#DRDY_PIN = 7
DRDY_PIN = 17
#RESET_PIN = 12
RESET_PIN = 18
#PDWN_PIN = 13
PDWN_PIN = 27
DRDY_TIMEOUT = 2
DRDY_DELAY = 0.000001
CLKIN_FREQUENCY = 7680000
# Analog reference voltage at VREF pin
v_ref = 3.3
def usleep(x):
time.sleep(x / 1000000.0)
def wait_DRDY():
start = time.time()
elapsed = time.time() - start
drdy_level = pi.read(DRDY_PIN)
print('DRDY is ', drdy_level, 'on initial read, we\'re good to go!')
while (drdy_level == 1) and (elapsed < DRDY_TIMEOUT):
elapsed = time.time() - start
drdy_level = pi.read(DRDY_PIN)
print('DRDY is ', drdy_level)
# Delay in order to avoid busy wait and reduce CPU load.
time.sleep(DRDY_DELAY)
if elapsed >= DRDY_TIMEOUT:
print("\nWarning: Timeout while polling configured DRDY pin!\n")
def chip_select(value):
csval = 0 if value else 1
print('Chip select on pin', CS_PIN, 'is now ', csval)
pi.write(CS_PIN, csval)
def send_byte(register, value):
pi.spi_write(spi_iface, bytes(value))
def reset():
print('Issuing reset()')
chip_select(True)
usleep(2)
pi.spi_write(spi_iface, bytes(CMD_RESET))
wait_DRDY()
chip_select(False)
def read_reg(register):
print('Reading register ', register)
chip_select(True)
payload = [CMD_RREG | register, 0x0]
print(bytes(payload))
pi.spi_write(spi_iface, payload)
usleep(2)
(b,d) = pi.spi_read(spi_iface, 1)
if b < 0:
raise IOError("Can't read shit")
chip_select(False)
return d
pi = io.pi(PI_HOST)
if not pi.connected:
raise IOError("Could not connect to hardware via pigpio library")
print('We are connected to klabs.local')
pi.set_mode(DRDY_PIN, io.INPUT)
print('=DRDY set to input')
pi.set_mode(CS_PIN, io.OUTPUT)
pi.write(CS_PIN, io.HIGH)
print('=CS set to output: ', io.HIGH)
pi.set_mode(RESET_PIN, io.OUTPUT)
pi.write(RESET_PIN, io.HIGH)
print('=RESET set to output: ', io.HIGH)
pi.set_mode(PDWN_PIN, io.OUTPUT)
pi.write(PDWN_PIN, io.HIGH)
print('=PDWN_PIN set to output: ', io.HIGH)
spi_iface = pi.spi_open(SPI_CHANNEL, SPI_FREQUENCY, SPI_FLAGS)
# 30ms settling
time.sleep(0.03)
print('Waiting on the DRDY pin...')
wait_DRDY()
reset()
time.sleep(0.03)
print(read_reg(0x4))
pi.stop()