Square Wave Generator

A square wave is a periodic waveform that alternates between two discrete values.

Here's an example square wave that is generated using a simple Python function.


In [1]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = np.arange(0, 100)

def square(x):
    return (x % 50) < 25

plt.plot(x, square(x))


Out[1]:
[<matplotlib.lines.Line2D at 0x10f1b67b8>]

To implement our square wave in magma, we start by importing the IceStick module from loam. We instance the IceStick and turn on the Clock and J3[0] (configured as an output).


In [ ]:
import magma as m
m.set_mantle_target("ice40")
from loam.boards.icestick import IceStick

icestick = IceStick()
icestick.Clock.on()
icestick.J3[0].output().on()

Create a magma circuit that will produce a digital square wave on the J3[0] GPIO pin.


In [ ]:
import magma as m
import mantle
main = icestick.main()

# TODO: Define main

In [ ]:
m.compile('build/square', main)

In [ ]:
%%bash
cd build
cat square.pcf
yosys -q -p 'synth_ice40 -top main -blif square.blif' square.v
arachne-pnr -q -d 1k -o square.txt -p square.pcf square.blif 
icepack square.txt square.bin
iceprog square.bin

Wire up GPIO pin 62 to a logic analyzer to verify that our circuit produces the correct square waveform.