Created
March 7, 2021 20:33
-
-
Save elcritch/1c8279418fc62f5e941b41a5df41ab79 to your computer and use it in GitHub Desktop.
Nim Hard Port Configuration Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
Direction* = enum | |
input | |
output | |
PinConf* = object | |
dir: Direction | |
Port0* = object | |
pin: array[32, PinConf] | |
RefPort0* = ptr Port0 | |
Port1* = object | |
pin: array[32, PinConf] | |
RefPort1* = ptr Port1 | |
var | |
# a real microcontroller would need a bit of magic here | |
# to ensure the correct memory addresses were used | |
port0 = Port0() | |
port1 = Port1() | |
# normally just import these from the C headers as-is | |
# but this lets us run it | |
p0* : RefPort0 = addr port0 | |
p1* : RefPort1 = addr port1 | |
var rows = ( | |
( port: p1, pin: 0 ), | |
( port: p1, pin: 1 ), | |
( port: p1, pin: 2 ), | |
( port: p1, pin: 4 ), | |
) | |
var cols = ( | |
(port: p0, pin: 13 ), | |
(port: p1, pin: 15 ), | |
(port: p0, pin: 17 ), | |
(port: p0, pin: 20 ), | |
(port: p0, pin: 22 ), | |
(port: p0, pin: 24 ), | |
(port: p0, pin: 9 ), | |
(port: p0, pin: 10 ), | |
(port: p0, pin: 4 ), | |
(port: p0, pin: 26 ), | |
(port: p0, pin: 2 ) | |
) | |
proc initKeyboardGPIO() = | |
rows[0].port.pin[rows[0].pin].dir = output | |
for item in rows.fields: | |
item.port.pin[item.pin].dir = output | |
for item in cols.fields: | |
var port = item.port | |
port.pin[item.pin].dir = output | |
echo "\nPre" | |
echo "pre init: p0: " & $p0[] | |
echo "pre init: p1: " & $p1[] | |
echo "\nSetup" | |
initKeyboardGPIO() | |
echo "\nPost" | |
echo "post init: p0: " & $p0[] | |
echo "post init: p1: " & $p1[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile and run:
time nim c -r gpio_setup_example.nim