Skip to content

Instantly share code, notes, and snippets.

@whatnick
Created August 7, 2017 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatnick/d9a6692fef67a158fcb2b8ce1a85b0f7 to your computer and use it in GitHub Desktop.
Save whatnick/d9a6692fef67a158fcb2b8ce1a85b0f7 to your computer and use it in GitHub Desktop.
atm90e26_esp32.py
import machine
import time
SysStatus = 0x01 #System Status
sck = machine.Pin(5,machine.Pin.OUT)
mosi = machine.Pin(18,machine.Pin.OUT)
miso = machine.Pin(19,machine.Pin.IN)
cs1 = machine.Pin(15,machine.Pin.OUT)
cs2 = machine.Pin(33,machine.Pin.OUT)
spi = machine.SPI(1,baudrate=200000,bits=8,polarity=1,phase=1,firstbit=machine.SPI.MSB,sck=sck,mosi=mosi,miso=miso)
while True:
cs1.value(0)
spi.write(bytearray([0x81]))
time.sleep_us(4)
val = spi.read(2)
print(val)
cs1.value(1)
time.sleep_ms(100)
while True:
val = commatm90(True,SysStatus,0x0000,cs2)
print(val)
time.sleep_ms(100)
def commatm90(bool rw,int address,int val,machine.Pin cs):
#switch MSB and LSB of value
buf = bytearray(2)
#use struct ?
buf[0] = val>>8
buf[1] = val<<8
#Set read write flag
address|=RW<<7
address=address.to_bytes(1,'big')
cs.value(0)
time.sleep_us(10)
spi.write(bytearray([address]))
''' Must wait 4 us for data to become valid '''
time.sleep_us(4)
#Read data
#Do for each byte in transfer
if(RW):
buf = spi.read(2)
else:
spi.write(buf) # write all the bytes
cs.value(1)
output=(buf[1]>>8)|(buf[0]<<8) #reverse MSB and LSB
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment