Guest User

BCI firmware

a guest
Feb 29th, 2024
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. import time
  2. import digitalio
  3. import busio
  4. import board
  5. import binascii
  6.  
  7. VERBOSE = True
  8.  
  9. WAKEUP = 0x02
  10. STANDBY = 0x04
  11. RESET = 0x06
  12. START = 0x08
  13. STOP = 0x0A
  14. RDATAC = 0x10
  15. SDATAC = 0x11
  16. RDATA = 0x12
  17.  
  18. ID = 0x00
  19. CONFIG1 = 0x01
  20. CONFIG2 = 0x02
  21. CONFIG3 = 0x03
  22. CH1SET = 0x05
  23. CH2SET = 0x06
  24. CH3SET = 0x07
  25. CH4SET = 0x08
  26. CH5SET = 0x09
  27. CH6SET = 0x0A
  28. CH7SET = 0x0B
  29. CH8SET = 0x0C
  30.  
  31.  
  32. def delay(ms: float):
  33.     time.sleep(ms / 1000)
  34.  
  35.  
  36. def delay_microseconds(microseconds: float):
  37.     time.sleep(microseconds / 1000000)
  38.  
  39.  
  40. class ADS1299:
  41.     def __init__(
  42.         self, drdy, rst, cs, freq, sck, mosi, miso
  43.     ):
  44.         delay(50)
  45.  
  46.         self.rst = digitalio.DigitalInOut(rst)
  47.         self.rst.direction = digitalio.Direction.OUTPUT
  48.         self.rst.value = False
  49.         delay_microseconds(4)  # Toggle reset pin
  50.         self.rst.value = True
  51.         delay_microseconds(20)  # Recommended to wait 18 tCLK before using device (~8uS)
  52.  
  53.         # SPI setup
  54.         self.cs = digitalio.DigitalInOut(cs)
  55.         self.cs.direction = digitalio.Direction.OUTPUT
  56.  
  57.         # self.sck.value = False
  58.         self.cs.value = True
  59.  
  60.         # Set as master and enable SPI
  61.         self.spi = busio.SPI(sck, MISO=miso, MOSI=mosi)  # SPI bus
  62.         while not self.spi.try_lock():
  63.             pass
  64.         self.spi.configure(baudrate=9600, phase=1, polarity=0)
  65.  
  66.         # End of SPI setup
  67.  
  68.         # Initialize the data ready chip select and reset pins
  69.         self.drdy = digitalio.DigitalInOut(drdy)
  70.         self.drdy.direction = digitalio.Direction.INPUT
  71.  
  72.         self.cs.value = True
  73.         self.rst.value = True
  74.  
  75.     def transfer(self, data: bytearray):
  76.         # SPI communication methods
  77.         # cli() and sei() in Arduino
  78.         data_received = bytearray(1)  
  79.         self.spi.write_readinto(bytearray([data]), data_received)
  80.         return data_received
  81.  
  82.     def wakeup(self):
  83.         self.cs.value = False
  84.         self.transfer(WAKEUP)
  85.         self.cs.value = True
  86.         delay_microseconds(
  87.             3
  88.         )  # Must wait 4 tCLK cycles before sending another command (Datasheet, pg. 35)
  89.  
  90.     def standby(self):
  91.         # Only allowed to send WAKEUP after sending STANDBY
  92.         self.cs.value = False
  93.         self.transfer(STANDBY)
  94.         self.cs.value = True
  95.  
  96.     def reset(self):
  97.         # Reset all the registers to default settings
  98.         self.cs.value = False
  99.         self.transfer(RESET)
  100.         delay_microseconds(
  101.             12
  102.         )  # Must wait 18 tCLK cycles to execute this command - page 35
  103.         self.cs.value = True
  104.  
  105.     def start(self):
  106.         # Start data conversion
  107.         self.cs.value = False
  108.         self.transfer(START)
  109.         self.cs.value = True
  110.  
  111.     def stop(self):
  112.         # Stop data conversion
  113.         self.cs.value = False
  114.         self.transfer(STOP)
  115.         self.cs.value = True
  116.  
  117.     def rdatac(self):
  118.         self.cs.value = False
  119.         self.transfer(RDATAC)
  120.         self.cs.value = True
  121.         delay_microseconds(3)
  122.  
  123.     def sdatac(self):
  124.         self.cs.value = False
  125.         self.transfer(SDATAC)
  126.         self.cs.value = True
  127.         delay_microseconds(3)
  128.  
  129.     def rreg(self, address):
  130.         # Reads ONE register at address
  131.         opcode1 = address + 0x20  # RREG expects 001rrrrr where rrrrr = _address
  132.         self.cs.value = False
  133.         self.transfer(opcode1)
  134.         self.transfer(0x00)  # Size to read = # of registers - 1
  135.         result = self.transfer(0x00)
  136.         self.cs.value = True
  137.         if VERBOSE:
  138.             print(f"Reading {address}: {result}")
  139.         return result
  140.  
  141.     def rregs():
  142.         pass
  143.  
  144.     def wreg(self, address: bytes, value: bytes):
  145.         # Write one register at address
  146.         opcode1 = address + 0x40  # WREG expects 010rrrrr where rrrrr = _address
  147.         self.cs.value = False
  148.         self.transfer(opcode1)
  149.         self.transfer(0x00)
  150.         self.transfer(value)
  151.         self.cs.value = True
  152.         if VERBOSE:
  153.             print(f"Write register {address}: {value}")
  154.  
  155.     def wregs(self, address: bytes, registers_minus_one: bytes):
  156.         pass
  157.  
  158.     def rdata(self):
  159.         # Read data
  160.         pass
  161.  
  162.     def get_device_id(self):
  163.         # Simple check
  164.         data = self.rreg(ID)
  165.         if VERBOSE:
  166.             print("Device ID: ", data)
  167.         return data
  168.  
  169.  
  170. if __name__ == "__main__":
  171.     device = ADS1299(board.IO2, board.IO1, board.IO10, 0, board.IO12, board.IO13, board.IO11)
  172.     device.get_device_id()
Advertisement
Add Comment
Please, Sign In to add comment