Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import digitalio
- import busio
- import board
- import binascii
- VERBOSE = True
- WAKEUP = 0x02
- STANDBY = 0x04
- RESET = 0x06
- START = 0x08
- STOP = 0x0A
- RDATAC = 0x10
- SDATAC = 0x11
- RDATA = 0x12
- ID = 0x00
- CONFIG1 = 0x01
- CONFIG2 = 0x02
- CONFIG3 = 0x03
- CH1SET = 0x05
- CH2SET = 0x06
- CH3SET = 0x07
- CH4SET = 0x08
- CH5SET = 0x09
- CH6SET = 0x0A
- CH7SET = 0x0B
- CH8SET = 0x0C
- def delay(ms: float):
- time.sleep(ms / 1000)
- def delay_microseconds(microseconds: float):
- time.sleep(microseconds / 1000000)
- class ADS1299:
- def __init__(
- self, drdy, rst, cs, freq, sck, mosi, miso
- ):
- delay(50)
- self.rst = digitalio.DigitalInOut(rst)
- self.rst.direction = digitalio.Direction.OUTPUT
- self.rst.value = False
- delay_microseconds(4) # Toggle reset pin
- self.rst.value = True
- delay_microseconds(20) # Recommended to wait 18 tCLK before using device (~8uS)
- # SPI setup
- self.cs = digitalio.DigitalInOut(cs)
- self.cs.direction = digitalio.Direction.OUTPUT
- # self.sck.value = False
- self.cs.value = True
- # Set as master and enable SPI
- self.spi = busio.SPI(sck, MISO=miso, MOSI=mosi) # SPI bus
- while not self.spi.try_lock():
- pass
- self.spi.configure(baudrate=9600, phase=1, polarity=0)
- # End of SPI setup
- # Initialize the data ready chip select and reset pins
- self.drdy = digitalio.DigitalInOut(drdy)
- self.drdy.direction = digitalio.Direction.INPUT
- self.cs.value = True
- self.rst.value = True
- def transfer(self, data: bytearray):
- # SPI communication methods
- # cli() and sei() in Arduino
- data_received = bytearray(1)
- self.spi.write_readinto(bytearray([data]), data_received)
- return data_received
- def wakeup(self):
- self.cs.value = False
- self.transfer(WAKEUP)
- self.cs.value = True
- delay_microseconds(
- 3
- ) # Must wait 4 tCLK cycles before sending another command (Datasheet, pg. 35)
- def standby(self):
- # Only allowed to send WAKEUP after sending STANDBY
- self.cs.value = False
- self.transfer(STANDBY)
- self.cs.value = True
- def reset(self):
- # Reset all the registers to default settings
- self.cs.value = False
- self.transfer(RESET)
- delay_microseconds(
- 12
- ) # Must wait 18 tCLK cycles to execute this command - page 35
- self.cs.value = True
- def start(self):
- # Start data conversion
- self.cs.value = False
- self.transfer(START)
- self.cs.value = True
- def stop(self):
- # Stop data conversion
- self.cs.value = False
- self.transfer(STOP)
- self.cs.value = True
- def rdatac(self):
- self.cs.value = False
- self.transfer(RDATAC)
- self.cs.value = True
- delay_microseconds(3)
- def sdatac(self):
- self.cs.value = False
- self.transfer(SDATAC)
- self.cs.value = True
- delay_microseconds(3)
- def rreg(self, address):
- # Reads ONE register at address
- opcode1 = address + 0x20 # RREG expects 001rrrrr where rrrrr = _address
- self.cs.value = False
- self.transfer(opcode1)
- self.transfer(0x00) # Size to read = # of registers - 1
- result = self.transfer(0x00)
- self.cs.value = True
- if VERBOSE:
- print(f"Reading {address}: {result}")
- return result
- def rregs():
- pass
- def wreg(self, address: bytes, value: bytes):
- # Write one register at address
- opcode1 = address + 0x40 # WREG expects 010rrrrr where rrrrr = _address
- self.cs.value = False
- self.transfer(opcode1)
- self.transfer(0x00)
- self.transfer(value)
- self.cs.value = True
- if VERBOSE:
- print(f"Write register {address}: {value}")
- def wregs(self, address: bytes, registers_minus_one: bytes):
- pass
- def rdata(self):
- # Read data
- pass
- def get_device_id(self):
- # Simple check
- data = self.rreg(ID)
- if VERBOSE:
- print("Device ID: ", data)
- return data
- if __name__ == "__main__":
- device = ADS1299(board.IO2, board.IO1, board.IO10, 0, board.IO12, board.IO13, board.IO11)
- device.get_device_id()
Advertisement
Add Comment
Please, Sign In to add comment