Skip to content

Instantly share code, notes, and snippets.

@cyber-murmel
Created February 13, 2020 11:41
Show Gist options
  • Save cyber-murmel/8ab4e2ad45c683089bd5089692aee7df to your computer and use it in GitHub Desktop.
Save cyber-murmel/8ab4e2ad45c683089bd5089692aee7df to your computer and use it in GitHub Desktop.
ice40web.py
# -*- coding: utf-8 -*-
# Copyright 2020 by https://badge.team/
# This file is released under the "CC0 1.0 Universal" license.
# https://creativecommons.org/publicdomain/zero/1.0/legalcode
from machine import Pin, SPI
import wifi, urequests, sys
# Pins
pin_reset = Pin(16, Pin.OUT)
pin_done = Pin(21)
pin_miso = Pin(5)
pin_mosi = Pin(17, Pin.OUT)
pin_sck = Pin(19, Pin.OUT)
pin_ss = Pin(18, Pin.OUT)
# SPI bus
spi = SPI(1, baudrate=1000000, sck=pin_sck, mosi=pin_mosi, miso=pin_miso)
def program(data):
# Reset the ICE40 into SPI slave download mode
pin_reset.value(0) # FPGA into RESET
pin_ss.value(0) # Hold down SS pin
pin_reset.value(1) # Take FPGA out of RESET
print("Writing...")
position = 0
length = len(data)
while True:
if position >= length:
break
spi.write(data[position:position+128])
position += 128
print("Sending dummy bytes...")
spi.write(bytes([0]*100))
print("Done")
while True:
print("Connecting to WiFi...")
wifi.connect()
wifi.wait()
print("Downloading...")
r = urequests.get("http://10.42.1.2/example.bin")
program(r.content)
print("Waiting for keypress...")
sys.stdin.read(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment