Make the flash sleep function toggleable

This commit is contained in:
Matthew Mets 2023-02-23 14:47:44 +00:00 committed by Michael Cardell Widerkrantz
parent c81b186c10
commit 04be27f6a7
No known key found for this signature in database
GPG key ID: D3DB3DDF57E704E5

View file

@ -456,6 +456,63 @@ def bitstream2nvcm(bitstream):
return rows
def sleep_flash():
# Disable board power
flasher.gpio_put(tp1_pins['5v_en'], False)
flasher.gpio_set_direction(tp1_pins['5v_en'], True)
# Pull CRST low to prevent FPGA from starting
flasher.gpio_set_direction(tp1_pins['crst'], True)
flasher.gpio_put(tp1_pins['crst'], False)
# Enable board power
flasher.gpio_put(tp1_pins['5v_en'], True)
# Configure pins for talking to flash
flasher.gpio_set_direction(tp1_pins['ss'], True)
flasher.gpio_set_direction(tp1_pins['mosi'], False)
flasher.gpio_set_direction(tp1_pins['sck'], True)
flasher.gpio_set_direction(tp1_pins['miso'], True)
flasher.gpio_put(tp1_pins['ss'], False)
flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0xAB])
flasher.gpio_put(tp1_pins['ss'], True)
# Confirm we can talk to flash
flasher.gpio_put(tp1_pins['ss'], False)
data = flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0x9f, 0,0])
flasher.gpio_put(tp1_pins['ss'], True)
print('flash ID while awake:', ' '.join(['{:02x}'.format(b) for b in data]))
assert(data == bytes([0xff, 0xef, 0x40]))
# Test that the flash will ignore a sleep command that doesn't start on the first byte
flasher.gpio_put(tp1_pins['ss'], False)
flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0, 0xb9])
flasher.gpio_put(tp1_pins['ss'], True)
# Confirm we can talk to flash
flasher.gpio_put(tp1_pins['ss'], False)
data = flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0x9f, 0,0])
flasher.gpio_put(tp1_pins['ss'], True)
print('flash ID while awake:', ' '.join(['{:02x}'.format(b) for b in data]))
assert(data == bytes([0xff, 0xef, 0x40]))
# put the flash to sleep
flasher.gpio_put(tp1_pins['ss'], False)
flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0xb9])
flasher.gpio_put(tp1_pins['ss'], True)
# Confirm flash is asleep
flasher.gpio_put(tp1_pins['ss'], False)
data = flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0x9f, 0,0])
flasher.gpio_put(tp1_pins['ss'], True)
print('flash ID while asleep:', ' '.join(['{:02x}'.format(b) for b in data]))
assert(data == bytes([0xff, 0xff, 0xff]))
if __name__ == "__main__":
import argparse
@ -472,6 +529,11 @@ if __name__ == "__main__":
action='store_true',
help='Show debug information and serial read/writes')
parser.add_argument('-f', '--sleep_flash',
dest='sleep_flash',
action='store_true',
help='Put an attached SPI flash chip in deep sleep before programming FPGA')
parser.add_argument('-b', '--boot',
dest='do_boot',
action='store_true',
@ -552,62 +614,8 @@ if __name__ == "__main__":
# for pin in tp1_pins:
# flasher.gpio_set_direction(tp1_pins[pin], False)
# Disable board power
flasher.gpio_put(tp1_pins['5v_en'], False)
flasher.gpio_set_direction(tp1_pins['5v_en'], True)
# Pull CRST low to prevent FPGA from starting
flasher.gpio_set_direction(tp1_pins['crst'], True)
flasher.gpio_put(tp1_pins['crst'], False)
sleep(1)
# Enable board power
flasher.gpio_put(tp1_pins['5v_en'], True)
# Configure pins for talking to flash
flasher.gpio_set_direction(tp1_pins['ss'], True)
flasher.gpio_set_direction(tp1_pins['mosi'], False)
flasher.gpio_set_direction(tp1_pins['sck'], True)
flasher.gpio_set_direction(tp1_pins['miso'], True)
flasher.gpio_put(tp1_pins['ss'], False)
flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0xAB])
flasher.gpio_put(tp1_pins['ss'], True)
# Confirm we can talk to flash
flasher.gpio_put(tp1_pins['ss'], False)
data = flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0x9f, 0,0])
flasher.gpio_put(tp1_pins['ss'], True)
print('flash ID while awake:', ' '.join(['{:02x}'.format(b) for b in data]))
assert(data == bytes([0xff, 0xef, 0x40]))
# Test that the flash will ignore a sleep command that doesn't start on the first byte
flasher.gpio_put(tp1_pins['ss'], False)
flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0, 0xb9])
flasher.gpio_put(tp1_pins['ss'], True)
# Confirm we can talk to flash
flasher.gpio_put(tp1_pins['ss'], False)
data = flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0x9f, 0,0])
flasher.gpio_put(tp1_pins['ss'], True)
print('flash ID while awake:', ' '.join(['{:02x}'.format(b) for b in data]))
assert(data == bytes([0xff, 0xef, 0x40]))
# put the flash to sleep
flasher.gpio_put(tp1_pins['ss'], False)
flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0xb9])
flasher.gpio_put(tp1_pins['ss'], True)
# Confirm flash is asleep
flasher.gpio_put(tp1_pins['ss'], False)
data = flasher.spi_bitbang(sck_pin=tp1_pins['sck'], mosi_pin=tp1_pins['miso'], miso_pin=tp1_pins['mosi'], buf=[0x9f, 0,0])
flasher.gpio_put(tp1_pins['ss'], True)
print('flash ID while asleep:', ' '.join(['{:02x}'.format(b) for b in data]))
assert(data == bytes([0xff, 0xff, 0xff]))
if args.sleep_flash:
sleep_flash()
# Configure pins for talking to ice40
flasher.gpio_set_direction(tp1_pins['ss'], True)
@ -617,17 +625,6 @@ if __name__ == "__main__":
# # Turn on ICE40 in CRAM boot mode
# init()
# nvcm_enable()
# nvcm_info()
# release power down: 0xAB
# sleep: 0xB9
## Request the JEDEC ID from the SPI device
#jedec_id = dev.exchange([0x9f], 3)
enable(1, 0) # enable and reset high
sleep(0.2)
enable(1, 1) # enable low, reset high