Skip to content

Commit

Permalink
safer use of GPIO during POST
Browse files Browse the repository at this point in the history
  • Loading branch information
psychogenic committed Sep 12, 2024
1 parent d085b83 commit d48fd31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
14 changes: 6 additions & 8 deletions src/ttboard/boot/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ttboard.pins.upython import Pin
import ttboard.util.time as time
from ttboard.boot.first import FirstBoot
from ttboard.pins.gpio_map import GPIOMap
import ttboard.pins.gpio_map as gp
from ttboard.demoboard import DemoBoard
from ttboard.mode import RPMode

Expand All @@ -37,7 +37,7 @@ def read_all_pins(self) -> dict:
to read value.
'''
pin_states = dict()
for name, io in GPIOMap.all().items():
for name, io in gp.GPIOMap.all().items():
p = Pin(io, Pin.IN)
pin_states[name] = p()

Expand All @@ -54,14 +54,14 @@ def read_pin(cls, pin:str) -> int:
@return: the value read
'''

p = GPIOMap.get_raw_pin(pin, Pin.IN)
p = gp.GPIOMap.get_raw_pin(pin, Pin.IN)
if p is None:
raise KeyError(f'No pin named {pin} found')
return p()

@classmethod
def write_pin(cls, pin:str, value:int):
p = GPIOMap.get_raw_pin(pin, Pin.OUT)
p = gp.GPIOMap.get_raw_pin(pin, Pin.OUT)
if p is None:
raise KeyError(f'No pin named {pin} found')
p(value)
Expand All @@ -70,17 +70,15 @@ def write_pin(cls, pin:str, value:int):

@classmethod
def dotest_buttons_held(cls):
mux_selectpin = GPIOMap.get_raw_pin('hk_csb', Pin.OUT)
mux_selectpin = gp.GPIOMap.get_raw_pin('hk_csb', Pin.OUT)
if mux_selectpin is not None:
mux_selectpin(1) # have mux, make sure it's is pointed right way
if cls.read_pin(GPIOMap.project_clock()) and not cls.read_pin(GPIOMap.project_reset()):
if cls.read_pin(gp.GPIOMap.project_clock()) and not cls.read_pin(gp.GPIOMap.project_reset()):
log.info('POST "do test" buttons held')
return True

return False

# could also check

@classmethod
def first_boot(cls):
return FirstBoot.is_first_boot()
Expand Down
6 changes: 6 additions & 0 deletions src/ttboard/pins/pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ def output_pins(self):

@property
def output_byte(self):

if platform.IsRP2040:
return platform.read_output_byte()
return self._read_byte(self.outputs)

@output_byte.setter
def output_byte(self, val:int):

if platform.IsRP2040:
platform.write_output_byte(val)
else:
Expand All @@ -227,6 +229,7 @@ def input_pins(self):

@property
def input_byte(self):

if platform.IsRP2040:
return platform.read_input_byte()

Expand All @@ -235,6 +238,7 @@ def input_byte(self):

@input_byte.setter
def input_byte(self, val:int):

if platform.IsRP2040:
platform.write_input_byte(val)
else:
Expand All @@ -261,6 +265,8 @@ def bidir_pins(self):

@property
def bidir_byte(self):


if platform.IsRP2040:
return platform.read_bidir_byte()

Expand Down
27 changes: 21 additions & 6 deletions src/ttboard/util/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
@author: Pat Deegan
@copyright: Copyright (C) 2024 Pat Deegan, https://psychogenic.com
'''


RP2040SystemClockDefaultHz = 125000000

IsRP2040 = False
Expand All @@ -14,8 +16,6 @@
pass




if IsRP2040:
'''
low-level machine related methods.
Expand All @@ -34,7 +34,8 @@
into an output.
NOTE: [muxing] there is a MUX on some pins of the low
output nibble, that could in theory interfere with this.
output nibble on TT04/05 demoboards, that could in theory
interfere with this.
If you use the tt.shuttle to select/enable projects, it
ensures that the mux is set to let these pins out as you'd
expect. If you're down in the weeds and want to make sure
Expand Down Expand Up @@ -65,6 +66,7 @@ def write_input_byte(val):
# low nibble starts at 9 | high nibble at 17 (-4 'cause high nibble)
val = ((val & 0xF) << 9) | ((val & 0xF0) << 17-4)
# xor with current GPIO values, and mask to keep only input bits
# 0x1E1E00 == 0b111100001111000000000 so GPIO 9-12 and 17-20
val = (machine.mem32[0xd0000010] ^ val) & 0x1E1E00
# val is now be all the input bits that have CHANGED:
# writing to 0xd000001c will flip any GPIO where a 1 is found
Expand Down Expand Up @@ -99,10 +101,8 @@ def read_bidir_byte():
def write_output_byte(val):
# low level machine stuff
# move the value bits to GPIO spots
# for bidir, all uio bits are in a line starting
# at GPIO 21

val = ((val & 0xF) << 5) | ((val & 0xF0) << 13-4)
# xor with current GPIO values, and mask to keep only input bits
val = (machine.mem32[0xd0000010] ^ val) & 0x1E1E0
# val is now be all the bits that have CHANGED:
# writing to 0xd000001c will flip any GPIO where a 1 is found,
Expand All @@ -111,6 +111,21 @@ def write_output_byte(val):

@micropython.native
def read_output_byte():

# sample code to deal with differences between
# PCBs, not actually required as we didn't move anything
# after all!
# global PCBVERSION_TT06
# all_io = machine.mem32[0xd0000004]
#if PCBVERSION_TT06 is None:
# import ttboard.boot.demoboard_detect as dbdet
# PCBVERSION_TT06 = True if dbdet.DemoboardDetect.PCB == dbdet.DemoboardVersion.TT06 else False

#if PCBVERSION_TT06:
# # gpio output bits are
# # 0x1e1e0 == 0b11110000111100000 so GPIO5-8 and GPIO 13-17
# val = ((all_io & (0xf << 13)) >> (13 - 4)) | ((all_io & (0xf << 5)) >> 5)
#else:
# just read the high and low nibbles from GPIO and combine into a byte
return ( (machine.mem32[0xd0000004] & (0xf << 13)) >> (13-4)) | ((machine.mem32[0xd0000004] & (0xf << 5)) >> 5)

Expand Down

0 comments on commit d48fd31

Please sign in to comment.