Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nim #3

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
28d2da2
Change arch from "riscv" to "risc-v"
lupyuen Dec 22, 2023
05f9667
Skip include
lupyuen Dec 22, 2023
866d2a3
Ignore .nimcache
lupyuen Dec 22, 2023
d2864ab
Change arch from riscv32 to riscv64
lupyuen Dec 22, 2023
5316172
Change riscv64-unknown-elf-gcc to riscv-none-elf-gcc
lupyuen Dec 22, 2023
9740477
Fix include
lupyuen Dec 23, 2023
0d5a3f5
Fix include
lupyuen Dec 23, 2023
0b8dcb3
Add log
lupyuen Dec 23, 2023
dcb79fb
Test OK on Linux
lupyuen Dec 23, 2023
03476b9
Why is Nim generating JSON instead of compiling?
lupyuen Dec 23, 2023
0a01c2f
Pointer size mismatch between Nim and C/C++ backend
lupyuen Dec 23, 2023
5ba4de5
/home/vscode/.choosenim/toolchains/nim-#devel/lib/std/monotimes.nim:1…
lupyuen Dec 23, 2023
c714a31
Compile Only to JSON
lupyuen Dec 23, 2023
e6fc255
Fix nimcache location
lupyuen Dec 23, 2023
e96653e
riscv_exception: EXCEPTION: Load access fault. MCAUSE: 00000000000000…
lupyuen Dec 23, 2023
4b3fbbe
Hello Nim works OK yay!
lupyuen Dec 23, 2023
e7b8370
Clean up
lupyuen Dec 24, 2023
f7fd2d9
Blink LED in Hello
lupyuen Dec 25, 2023
303a061
Test ioctl
lupyuen Dec 25, 2023
609b2ef
Test ioctl
lupyuen Dec 25, 2023
6912373
Test ioctl
lupyuen Dec 25, 2023
36bd4aa
Test LED in Nim
lupyuen Dec 25, 2023
597bc57
Runs OK yay!
lupyuen Dec 25, 2023
e58c9e7
Runs OK yay!
lupyuen Dec 25, 2023
8c5447f
Clean up
lupyuen Dec 25, 2023
6d845fc
Clean up
lupyuen Dec 25, 2023
f94c0a5
Blink Loop
lupyuen Dec 26, 2023
1970acb
Clean up
lupyuen Dec 26, 2023
8a1a6de
Clean up
lupyuen Dec 26, 2023
1c8af35
Clean up
lupyuen Dec 27, 2023
aa554ea
Remove nodecl
lupyuen Dec 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ Make.dep
.dirlinks
.vscode
.DS_Store
.nimcache

22 changes: 19 additions & 3 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ switch "mm", "orc"

switch "arm.nuttx.gcc.exe", "arm-none-eabi-gcc"
switch "arm64.nuttx.gcc.exe", "aarch64-none-elf-gcc"
switch "riscv32.nuttx.gcc.exe", "riscv64-unknown-elf-gcc"
####switch "riscv32.nuttx.gcc.exe", "riscv64-unknown-elf-gcc"
switch "riscv32.nuttx.gcc.exe", "riscv-none-elf-gcc" #### TODO
switch "riscv64.nuttx.gcc.exe", "riscv-none-elf-gcc" #### TODO
switch "amd64.nuttx.gcc.exe", "x86_64-linux-gnu-gcc"

switch "nimcache", ".nimcache"
Expand Down Expand Up @@ -57,6 +59,7 @@ proc killoBytes(bytes: int): int =
result = (bytes / 1024).int

proc read_config(cfg: string): DotConfig =
echo "read_config: " & cfg ####
for line in cfg.readFile.splitLines:
if not line.startsWith("CONFIG_"):
continue
Expand All @@ -65,30 +68,43 @@ proc read_config(cfg: string): DotConfig =
continue
case keyval[0]
of "ARCH":
echo "line=" & line ####
echo "keyval=" & keyval ####
echo "keyval[1]=" & keyval[1] ####
let arch = keyval[1].strip(chars = {'"'})
case arch
of "arm", "arm64":
result.arch = arch
of "riscv":
result.arch = "riscv32"
of "risc-v":
#### TODO: Check for riscv32 or riscv3
#### CONFIG_ARCH_RV32=y or CONFIG_ARCH_RV64=y
result.arch = "riscv64"
# switch("passC", "-mcmodel=medany") #### TODO
# switch("passC", "-march=rv64imafdc_zicsr_zifencei") #### TODO
# switch("passC", "-mabi=lp64d") #### TODO
of "sim":
if defined(amd64):
result.arch = "amd64"
elif defined(aarch64):
result.arch = "arm64"
result.isSim = true
of "DEBUG_NOOPT":
echo "line=" & line ####
result.opt = oNone
of "DEBUG_FULLOPT":
echo "line=" & line ####
result.opt = oSize
of "DEBUG_SYMBOLS":
echo "line=" & line ####
result.debugSymbols = true
of "RAM_SIZE":
echo "line=" & line ####
result.ramSize = keyval[1].parseInt
echo "* arch: " & result.arch
echo "* opt: " & $result.opt
echo "* debug: " & $result.debugSymbols
echo "* ramSize: " & $result.ramSize
echo "* isSim: " & $result.isSim

func bool2onoff(b: bool): string =
result = if b: "on" else: "off"
Expand Down
45 changes: 45 additions & 0 deletions examples/hello/hello_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
****************************************************************************/

#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>

#include <nuttx/leds/userled.h>

/****************************************************************************
* Public Functions
Expand All @@ -36,5 +40,46 @@
int main(int argc, FAR char *argv[])
{
printf("Hello, World!!\n");

// Open the LED Driver
printf("Opening /dev/userleds\n");
int fd = open("/dev/userleds", O_WRONLY);
if (fd < 0)
{
int errcode = errno;
printf("ERROR: Failed to open /dev/userleds: %d\n",
errcode);
return EXIT_FAILURE;
}

// Turn on LED
puts("Set LED 0 to 1");
int ret = ioctl(fd, ULEDIOC_SETALL, 1);
if (ret < 0)
{
int errcode = errno;
printf("ERROR: ioctl(ULEDIOC_SUPPORTED) failed: %d\n",
errcode);
return EXIT_FAILURE;
}

// Sleep a while
puts("Waiting...");
usleep(500 * 1000L);

// Turn off LED
puts("Set LED 0 to 0");
ret = ioctl(fd, ULEDIOC_SETALL, 0);
if (ret < 0)
{
int errcode = errno;
printf("ERROR: ioctl(ULEDIOC_SUPPORTED) failed: %d\n",
errcode);
return EXIT_FAILURE;
}

// Close the LED Driver
close(fd);

return 0;
}
4 changes: 2 additions & 2 deletions examples/hello_nim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ MODULE = $(CONFIG_EXAMPLES_HELLO_NIM)
# Hello, World! Example

NIMPATH = $(shell choosenim show path)
CFLAGS += -I $(NIMPATH)/lib -I ./.nimcache
CSRCS += $(wildcard .nimcache/*.c)
CFLAGS += -I $(NIMPATH)/lib -I ../../.nimcache
CSRCS += $(wildcard ../../.nimcache/*.c)

MAINSRC = hello_nim_main.c

Expand Down
104 changes: 101 additions & 3 deletions examples/hello_nim/hello_nim_async.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,91 @@
## Import NuttX Functions and Macros from C.
## Based on https://github.com/nim-lang/Nim/blob/devel/lib/std/syncio.nim
proc c_open(filename: cstring, mode: cint): cint {.
importc: "open", header: "<fcntl.h>".}
proc c_close(fd: cint): cint {.
importc: "close", header: "<fcntl.h>",
discardable.}
proc c_ioctl(fd: cint, request: cint): cint {.
importc: "ioctl", header: "<sys/ioctl.h>",
varargs.}
proc c_usleep(usec: cuint): cint {.
importc: "usleep", header: "<unistd.h>",
discardable.}
var O_WRONLY {.
importc: "O_WRONLY", header: "<fcntl.h>".}: cint
var ULEDIOC_SETALL {.
importc: "ULEDIOC_SETALL", header: "<nuttx/leds/userled.h>".}: cint

## Blink the LED
proc blink_led() =

## Open the LED Driver
echo "Opening /dev/userleds"
let fd = c_open("/dev/userleds", O_WRONLY)
if fd < 0:
echo "Failed to open /dev/userleds"
return

## On Return: Close the LED Driver
defer: c_close(fd)

## Turn on LED
echo "Set LED 0 to 1"
var ret = c_ioctl(fd, ULEDIOC_SETALL, 1)
if ret < 0:
echo "ioctl(ULEDIOC_SETALL) failed"
return

## Wait a second (literally)
echo "Waiting..."
c_usleep(1000_000)

## Turn off LED
echo "Set LED 0 to 0"
ret = c_ioctl(fd, ULEDIOC_SETALL, 0)
if ret < 0:
echo "ioctl(ULEDIOC_SETALL) failed"
return

## Wait again
echo "Waiting..."
c_usleep(1000_000)

## Main Function in Nim
proc hello_nim() {.exportc, cdecl.} =

## On Return: Force the Garbage Collection
defer: GC_runOrc()

## Print something
echo "Hello Nim!"

## Blink the LED 20 times
for loop in 0..19:
blink_led()

## To test the compilation for NuttX:
# export TOPDIR=/workspaces/bookworm/nuttx
# cd /workspaces/bookworm/apps/examples/hello_nim
# nim c --header hello_nim_async.nim

## Output Log:
# nsh> hello_nim
# Hello Nim!
# Opening /dev/userleds
# Set LED 0 to 1
# board_userled_all: ledset=0x1
# board_userled_all: led=0, val=1
# board_userled_all: led=1, val=0
# board_userled_all: led=2, val=0
# Waiting...
# Set LED 0 to 0
# board_userled_all: ledset=0x0
# board_userled_all: led=0, val=0
# board_userled_all: led=1, val=0
# board_userled_all: led=2, val=0

## Previously: Test for Async Tasks
import std/asyncdispatch
import std/strformat

Expand All @@ -13,6 +101,16 @@ proc launch() {.async.} =
await sleepAsync(200)
await task(3)

proc hello_nim() {.exportc, cdecl.} =
waitFor launch()
GC_runOrc()
# Test in Linux:
# hello_nim() ####
# Output:
# $ ./hello_nim_async
# Hello from task 1! loops: 0
# Hello from task 2! loops: 0
# Hello from task 3! loops: 0
# Hello from task 1! loops: 1
# Hello from task 2! loops: 1
# Hello from task 3! loops: 1
# Hello from task 1! loops: 2
# Hello from task 2! loops: 2
# Hello from task 3! loops: 2
5 changes: 4 additions & 1 deletion examples/hello_nim/hello_nim_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
* hello_main
****************************************************************************/

int main(int argc, FAR char *argv[])
int main(int argc, char *argv[])
{
NimMain();
hello_nim();
return 0;
}

////TODO: Needed by nuttx/arch/risc-v/src/common/riscv_initialize.c
void riscv_netinitialize(void) {} ////TODO
Loading