From 28d2da2f6c11ab7f2220dc27ef07bbf6bb5e3c8e Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 22 Dec 2023 14:08:54 +0000 Subject: [PATCH 01/31] Change arch from "riscv" to "risc-v" --- config.nims | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.nims b/config.nims index 0d6c4c74cc..a124f3f670 100644 --- a/config.nims +++ b/config.nims @@ -65,11 +65,13 @@ proc read_config(cfg: string): DotConfig = continue case keyval[0] of "ARCH": + echo "keyval=" & keyval #### + echo "keyval[1]=" & keyval[1] #### let arch = keyval[1].strip(chars = {'"'}) case arch of "arm", "arm64": result.arch = arch - of "riscv": + of "risc-v": result.arch = "riscv32" of "sim": if defined(amd64): From 05f966795992f4872419dcc252593751ca801582 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 22 Dec 2023 14:09:22 +0000 Subject: [PATCH 02/31] Skip include --- examples/hello_nim/hello_nim_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/hello_nim/hello_nim_main.c b/examples/hello_nim/hello_nim_main.c index 9cad2fd924..5cfc2615db 100644 --- a/examples/hello_nim/hello_nim_main.c +++ b/examples/hello_nim/hello_nim_main.c @@ -23,7 +23,7 @@ ****************************************************************************/ #include -#include "hello_nim_async.h" +////#include "hello_nim_async.h" /**************************************************************************** * Public Functions @@ -33,7 +33,7 @@ * hello_main ****************************************************************************/ -int main(int argc, FAR char *argv[]) +int main(int argc, char *argv[]) { NimMain(); hello_nim(); From 866d2a310cd212a230d66d872f315c2fca91d00d Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 22 Dec 2023 14:09:39 +0000 Subject: [PATCH 03/31] Ignore .nimcache --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 6bdd5c4644..088690722e 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ Make.dep .dirlinks .vscode .DS_Store +.nimcache + From d2864ab653687c1bcbb428419827526e976b5483 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 22 Dec 2023 14:37:57 +0000 Subject: [PATCH 04/31] Change arch from riscv32 to riscv64 --- config.nims | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.nims b/config.nims index a124f3f670..d24d0df221 100644 --- a/config.nims +++ b/config.nims @@ -72,7 +72,7 @@ proc read_config(cfg: string): DotConfig = of "arm", "arm64": result.arch = arch of "risc-v": - result.arch = "riscv32" + result.arch = "riscv64" of "sim": if defined(amd64): result.arch = "amd64" From 5316172dda801fb504a35d74be63c650ec8d3902 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 22 Dec 2023 14:41:15 +0000 Subject: [PATCH 05/31] Change riscv64-unknown-elf-gcc to riscv-none-elf-gcc --- config.nims | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.nims b/config.nims index d24d0df221..4ea2da00b7 100644 --- a/config.nims +++ b/config.nims @@ -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" #### +switch "riscv64.nuttx.gcc.exe", "riscv-none-elf-gcc" #### switch "amd64.nuttx.gcc.exe", "x86_64-linux-gnu-gcc" switch "nimcache", ".nimcache" From 9740477d6f9a71449820013f3d9961ec64dfd0a2 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 03:25:45 +0000 Subject: [PATCH 06/31] Fix include --- examples/hello_nim/hello_nim_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_nim/hello_nim_main.c b/examples/hello_nim/hello_nim_main.c index 5cfc2615db..191ef95202 100644 --- a/examples/hello_nim/hello_nim_main.c +++ b/examples/hello_nim/hello_nim_main.c @@ -23,7 +23,7 @@ ****************************************************************************/ #include -////#include "hello_nim_async.h" +#include "../.nimcache/hello_nim_async.h" /**************************************************************************** * Public Functions From 0d5a3f51094922063cf5294559248f78560bb076 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 03:28:19 +0000 Subject: [PATCH 07/31] Fix include --- examples/hello_nim/Makefile | 2 +- examples/hello_nim/hello_nim_main.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/hello_nim/Makefile b/examples/hello_nim/Makefile index 52d306d965..351fe19186 100644 --- a/examples/hello_nim/Makefile +++ b/examples/hello_nim/Makefile @@ -30,7 +30,7 @@ MODULE = $(CONFIG_EXAMPLES_HELLO_NIM) # Hello, World! Example NIMPATH = $(shell choosenim show path) -CFLAGS += -I $(NIMPATH)/lib -I ./.nimcache +CFLAGS += -I $(NIMPATH)/lib -I ../../.nimcache CSRCS += $(wildcard .nimcache/*.c) MAINSRC = hello_nim_main.c diff --git a/examples/hello_nim/hello_nim_main.c b/examples/hello_nim/hello_nim_main.c index 191ef95202..18f1151d8f 100644 --- a/examples/hello_nim/hello_nim_main.c +++ b/examples/hello_nim/hello_nim_main.c @@ -23,7 +23,7 @@ ****************************************************************************/ #include -#include "../.nimcache/hello_nim_async.h" +#include "hello_nim_async.h" /**************************************************************************** * Public Functions From 0b8dcb3d579af9142f7b88f724d281e930be7431 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 03:36:47 +0000 Subject: [PATCH 08/31] Add log --- config.nims | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.nims b/config.nims index 4ea2da00b7..f1c81c2d25 100644 --- a/config.nims +++ b/config.nims @@ -59,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 @@ -93,6 +94,7 @@ proc read_config(cfg: string): DotConfig = 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" From dcb79fb805706c5746d3be05c2696af8c735b54e Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 03:45:30 +0000 Subject: [PATCH 09/31] Test OK on Linux --- examples/hello_nim/hello_nim_async.nim | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 580283c17b..ebac46152c 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -16,3 +16,16 @@ proc launch() {.async.} = proc hello_nim() {.exportc, cdecl.} = waitFor launch() GC_runOrc() + +# 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 From 03476b91e7c0c1e1aa9326c892f01c2c010afc82 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 03:52:10 +0000 Subject: [PATCH 10/31] Why is Nim generating JSON instead of compiling? --- config.nims | 7 +++++++ examples/hello_nim/hello_nim_async.nim | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/config.nims b/config.nims index f1c81c2d25..31458d8383 100644 --- a/config.nims +++ b/config.nims @@ -68,6 +68,7 @@ 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 = {'"'}) @@ -75,6 +76,8 @@ proc read_config(cfg: string): DotConfig = of "arm", "arm64": result.arch = arch of "risc-v": + #### TODO: Check for riscv32 or riscv3 + #### CONFIG_ARCH_RV32=y or CONFIG_ARCH_RV64=y result.arch = "riscv64" of "sim": if defined(amd64): @@ -83,12 +86,16 @@ proc read_config(cfg: string): DotConfig = 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 diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index ebac46152c..972a88c2d7 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -17,6 +17,12 @@ proc hello_nim() {.exportc, cdecl.} = waitFor launch() GC_runOrc() +# Compile on NuttX: +# export TOPDIR=/workspaces/bookworm/nuttx +# cd /workspaces/bookworm/apps/examples/hello_nim +# nim c --header hello_nim_async.nim + +# Test in Linux: # hello_nim() #### # Output: # $ ./hello_nim_async From 0a01c2fdcea1961d875ebfd3f461cdb7f61c6ada Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 06:19:42 +0000 Subject: [PATCH 11/31] Pointer size mismatch between Nim and C/C++ backend --- config.nims | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config.nims b/config.nims index 31458d8383..6d4e70cf1e 100644 --- a/config.nims +++ b/config.nims @@ -27,8 +27,8 @@ 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", "riscv-none-elf-gcc" #### -switch "riscv64.nuttx.gcc.exe", "riscv-none-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" @@ -38,7 +38,7 @@ switch "d", "nimAllocPagesViaMalloc" switch "d", "noSignalHandler" switch "threads", "off" switch "noMain", "on" -switch "compileOnly", "on" +####switch "compileOnly", "on" switch "noLinking", "on" # TODO: need OpenSSL-mbedTLS wrapper library. #switch "d", "ssl" From 5ba4de512f520b1b6c401df95a7bfcb31a07b719 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 06:38:57 +0000 Subject: [PATCH 12/31] /home/vscode/.choosenim/toolchains/nim-#devel/lib/std/monotimes.nim:110:63: error: 'CLOCK_MONOTONIC' undeclared --- config.nims | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config.nims b/config.nims index 6d4e70cf1e..043cde7fa2 100644 --- a/config.nims +++ b/config.nims @@ -79,6 +79,9 @@ proc read_config(cfg: string): DotConfig = #### 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" From c714a317e531aa8ab2de7b9a8e4c4b0f89f66626 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 07:04:21 +0000 Subject: [PATCH 13/31] Compile Only to JSON --- config.nims | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config.nims b/config.nims index 043cde7fa2..ce0d8afb46 100644 --- a/config.nims +++ b/config.nims @@ -38,7 +38,7 @@ switch "d", "nimAllocPagesViaMalloc" switch "d", "noSignalHandler" switch "threads", "off" switch "noMain", "on" -####switch "compileOnly", "on" +switch "compileOnly", "on" switch "noLinking", "on" # TODO: need OpenSSL-mbedTLS wrapper library. #switch "d", "ssl" @@ -79,9 +79,9 @@ proc read_config(cfg: string): DotConfig = #### 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 + # 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" From e6fc2559ec2381ef2232bacadcda072913f6495d Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 07:05:55 +0000 Subject: [PATCH 14/31] Fix nimcache location --- examples/hello_nim/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_nim/Makefile b/examples/hello_nim/Makefile index 351fe19186..67e9bf9c97 100644 --- a/examples/hello_nim/Makefile +++ b/examples/hello_nim/Makefile @@ -31,7 +31,7 @@ MODULE = $(CONFIG_EXAMPLES_HELLO_NIM) NIMPATH = $(shell choosenim show path) CFLAGS += -I $(NIMPATH)/lib -I ../../.nimcache -CSRCS += $(wildcard .nimcache/*.c) +CSRCS += $(wildcard ../../.nimcache/*.c) MAINSRC = hello_nim_main.c From e96653ec9be424a75676cff5c2e53ad3d988ec4f Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 07:19:01 +0000 Subject: [PATCH 15/31] riscv_exception: EXCEPTION: Load access fault. MCAUSE: 0000000000000005, EPC: 000000008000e19c, MTVAL: c0ef000555638482 --- examples/hello_nim/hello_nim_main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/hello_nim/hello_nim_main.c b/examples/hello_nim/hello_nim_main.c index 18f1151d8f..ba20f55102 100644 --- a/examples/hello_nim/hello_nim_main.c +++ b/examples/hello_nim/hello_nim_main.c @@ -39,3 +39,6 @@ int main(int argc, char *argv[]) hello_nim(); return 0; } + +////TODO +void riscv_netinitialize(void) {} ////TODO From 4b3fbbecb54a26d36e6067e0be9cca240ff41145 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sat, 23 Dec 2023 07:30:27 +0000 Subject: [PATCH 16/31] Hello Nim works OK yay! --- examples/hello_nim/hello_nim_async.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 972a88c2d7..4bdcd36e31 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -14,7 +14,8 @@ proc launch() {.async.} = await task(3) proc hello_nim() {.exportc, cdecl.} = - waitFor launch() + #### waitFor launch() + echo "Hello Nim!" #### GC_runOrc() # Compile on NuttX: From e7b83709a4fa430db2a7857da814ed86908f07be Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 24 Dec 2023 12:39:01 +0000 Subject: [PATCH 17/31] Clean up --- examples/hello_nim/hello_nim_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_nim/hello_nim_main.c b/examples/hello_nim/hello_nim_main.c index ba20f55102..df1c910156 100644 --- a/examples/hello_nim/hello_nim_main.c +++ b/examples/hello_nim/hello_nim_main.c @@ -40,5 +40,5 @@ int main(int argc, char *argv[]) return 0; } -////TODO +////TODO: Needed by nuttx/arch/risc-v/src/common/riscv_initialize.c void riscv_netinitialize(void) {} ////TODO From f7fd2d93b355935341658612189de595b34d95eb Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 06:37:05 +0000 Subject: [PATCH 18/31] Blink LED in Hello --- examples/hello/hello_main.c | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/examples/hello/hello_main.c b/examples/hello/hello_main.c index 2cd6cb400a..06b5dd5fb9 100644 --- a/examples/hello/hello_main.c +++ b/examples/hello/hello_main.c @@ -23,7 +23,11 @@ ****************************************************************************/ #include +#include #include +#include + +#include /**************************************************************************** * Public Functions @@ -36,5 +40,43 @@ 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 %s: %d\n", + CONFIG_EXAMPLES_LEDS_DEVPATH, 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 on 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; + } + return 0; } From 303a06131987ff5d0fec5242ea47e96d8bc0564d Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 09:25:43 +0000 Subject: [PATCH 19/31] Test ioctl --- examples/hello_nim/hello_nim_async.nim | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 4bdcd36e31..63c06c656e 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -13,9 +13,22 @@ proc launch() {.async.} = await sleepAsync(200) await task(3) +## Import ioctl() from C +proc c_ioctl(fd: cint, request: cint): cint {. + importc: "ioctl", header: "", varargs.} + proc hello_nim() {.exportc, cdecl.} = - #### waitFor launch() - echo "Hello Nim!" #### + + ## Print something + echo "Hello Nim!" + + ## Test ioctl + var ret = c_ioctl(0, 0) + echo "ret=" + echo ret + + ## Finish + ## waitFor launch() GC_runOrc() # Compile on NuttX: From 609b2efe9d89334ddc833ffb48daf01dc6536cdb Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 09:34:02 +0000 Subject: [PATCH 20/31] Test ioctl --- examples/hello_nim/hello_nim_async.nim | 35 ++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 63c06c656e..a5f025ac12 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,22 +1,11 @@ -import std/asyncdispatch import std/strformat -proc task(id: int): Future[void] {.async.} = - for loop in 0..2: - echo &"Hello from task {id}! loops: {loop}" - if loop < 2: - await sleepAsync(1000) - -proc launch() {.async.} = - for id in 1..2: - asyncCheck task(id) - await sleepAsync(200) - await task(3) - ## Import ioctl() from C +## Based on /home/vscode/.choosenim/toolchains/nim-#devel/lib/std/syncio.nim proc c_ioctl(fd: cint, request: cint): cint {. importc: "ioctl", header: "", varargs.} +## Main Function in Nim proc hello_nim() {.exportc, cdecl.} = ## Print something @@ -24,18 +13,32 @@ proc hello_nim() {.exportc, cdecl.} = ## Test ioctl var ret = c_ioctl(0, 0) - echo "ret=" - echo ret + echo &"ret={ret}" ## Finish ## waitFor launch() GC_runOrc() -# Compile on NuttX: +## Compile on NuttX: # export TOPDIR=/workspaces/bookworm/nuttx # cd /workspaces/bookworm/apps/examples/hello_nim # nim c --header hello_nim_async.nim +## Previously: +import std/asyncdispatch + +proc task(id: int): Future[void] {.async.} = + for loop in 0..2: + echo &"Hello from task {id}! loops: {loop}" + if loop < 2: + await sleepAsync(1000) + +proc launch() {.async.} = + for id in 1..2: + asyncCheck task(id) + await sleepAsync(200) + await task(3) + # Test in Linux: # hello_nim() #### # Output: From 69123734dd1bdd8360b098dddc39279af689278b Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 09:34:27 +0000 Subject: [PATCH 21/31] Test ioctl --- examples/hello_nim/hello_nim_async.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index a5f025ac12..6ce1d60608 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,4 +1,4 @@ -import std/strformat +import std/strformat ## String Formatting ## Import ioctl() from C ## Based on /home/vscode/.choosenim/toolchains/nim-#devel/lib/std/syncio.nim From 36bd4aa0a0f78cca6cd92b293c6d0f02db966dfc Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 13:17:27 +0000 Subject: [PATCH 22/31] Test LED in Nim --- examples/hello/hello_main.c | 7 ++-- examples/hello_nim/hello_nim_async.nim | 44 +++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/examples/hello/hello_main.c b/examples/hello/hello_main.c index 06b5dd5fb9..cc49fe81d9 100644 --- a/examples/hello/hello_main.c +++ b/examples/hello/hello_main.c @@ -47,8 +47,8 @@ int main(int argc, FAR char *argv[]) if (fd < 0) { int errcode = errno; - printf("ERROR: Failed to open %s: %d\n", - CONFIG_EXAMPLES_LEDS_DEVPATH, errcode); + printf("ERROR: Failed to open /dev/userleds: %d\n", + errcode); return EXIT_FAILURE; } @@ -78,5 +78,8 @@ int main(int argc, FAR char *argv[]) return EXIT_FAILURE; } + // Close the LED Driver + close(fd); + return 0; } diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 6ce1d60608..b2d1e810c6 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,9 +1,46 @@ import std/strformat ## String Formatting -## Import ioctl() from C +## Import Standard Functions from C. ## Based on /home/vscode/.choosenim/toolchains/nim-#devel/lib/std/syncio.nim +proc c_open(filename: cstring, mode: cint): cint {. + importc: "open", nodecl.} +proc c_close(fd: cint): cint {. + importc: "close", nodecl.} proc c_ioctl(fd: cint, request: cint): cint {. importc: "ioctl", header: "", varargs.} +proc c_usleep(usec: cuint): cint {. + importc: "usleep", nodecl.} +var O_WRONLY {.importc: "O_WRONLY", header: "".}: cint +var ULEDIOC_SETALL {.importc: "ULEDIOC_SETALL", header: "".}: cint + +proc blink_led() = + ## Open the LED driver + echo "Opening /dev/userleds" + var fd = c_open("/dev/userleds", O_WRONLY) + if fd < 0: + echo "Failed to open /dev/userleds" + return + + ## 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 + + ## Sleep a while + echo "Waiting..." + discard c_usleep(500 * 1000) + + ## Turn on LED + echo "Set LED 0 to 0" + ret = c_ioctl(fd, ULEDIOC_SETALL, 0) + if ret < 0: + echo "ioctl(ULEDIOC_SETALL) failed" + return + + ## Close the LED Driver + discard c_close(fd) ## Main Function in Nim proc hello_nim() {.exportc, cdecl.} = @@ -11,9 +48,8 @@ proc hello_nim() {.exportc, cdecl.} = ## Print something echo "Hello Nim!" - ## Test ioctl - var ret = c_ioctl(0, 0) - echo &"ret={ret}" + ## Blink the LED + blink_led() ## Finish ## waitFor launch() From 597bc576b3a85c04639bcab59ca2d8c427b45213 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 13:19:18 +0000 Subject: [PATCH 23/31] Runs OK yay! --- examples/hello_nim/hello_nim_async.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index b2d1e810c6..e87973228a 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -30,7 +30,7 @@ proc blink_led() = ## Sleep a while echo "Waiting..." - discard c_usleep(500 * 1000) + discard c_usleep(1000_000) ## Turn on LED echo "Set LED 0 to 0" From e58c9e728be2dd42b1feb147ec094b5895c50f5e Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 13:36:47 +0000 Subject: [PATCH 24/31] Runs OK yay! --- examples/hello_nim/hello_nim_async.nim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index e87973228a..f415e888ab 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -5,15 +5,17 @@ import std/strformat ## String Formatting proc c_open(filename: cstring, mode: cint): cint {. importc: "open", nodecl.} proc c_close(fd: cint): cint {. - importc: "close", nodecl.} + importc: "close", nodecl, discardable.} proc c_ioctl(fd: cint, request: cint): cint {. - importc: "ioctl", header: "", varargs.} + importc: "ioctl", nodecl, varargs.} proc c_usleep(usec: cuint): cint {. - importc: "usleep", nodecl.} + importc: "usleep", nodecl, discardable.} var O_WRONLY {.importc: "O_WRONLY", header: "".}: cint var ULEDIOC_SETALL {.importc: "ULEDIOC_SETALL", header: "".}: cint +## Blink the LED proc blink_led() = + ## Open the LED driver echo "Opening /dev/userleds" var fd = c_open("/dev/userleds", O_WRONLY) @@ -28,9 +30,9 @@ proc blink_led() = echo "ioctl(ULEDIOC_SETALL) failed" return - ## Sleep a while + ## Wait a second (literally) echo "Waiting..." - discard c_usleep(1000_000) + c_usleep(1000_000) ## Turn on LED echo "Set LED 0 to 0" @@ -40,7 +42,7 @@ proc blink_led() = return ## Close the LED Driver - discard c_close(fd) + c_close(fd) ## Main Function in Nim proc hello_nim() {.exportc, cdecl.} = From 8c5447fa9055e45f0b20d8d7ba00b1a3ff80c6ac Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 13:44:23 +0000 Subject: [PATCH 25/31] Clean up --- examples/hello_nim/hello_nim_async.nim | 40 ++++++++++++++++++++------ 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index f415e888ab..0ff85ed953 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,17 +1,23 @@ import std/strformat ## String Formatting -## Import Standard Functions from C. +## Import NuttX Functions and Macros from C. ## Based on /home/vscode/.choosenim/toolchains/nim-#devel/lib/std/syncio.nim proc c_open(filename: cstring, mode: cint): cint {. - importc: "open", nodecl.} + importc: "open", header: "", + nodecl.} proc c_close(fd: cint): cint {. - importc: "close", nodecl, discardable.} + importc: "close", header: "", + nodecl, discardable.} proc c_ioctl(fd: cint, request: cint): cint {. - importc: "ioctl", nodecl, varargs.} + importc: "ioctl", header: "", + nodecl, varargs.} proc c_usleep(usec: cuint): cint {. - importc: "usleep", nodecl, discardable.} -var O_WRONLY {.importc: "O_WRONLY", header: "".}: cint -var ULEDIOC_SETALL {.importc: "ULEDIOC_SETALL", header: "".}: cint + importc: "usleep", header: "", + nodecl, discardable.} +var O_WRONLY {. + importc: "O_WRONLY", header: "".}: cint +var ULEDIOC_SETALL {. + importc: "ULEDIOC_SETALL", header: "".}: cint ## Blink the LED proc blink_led() = @@ -54,14 +60,30 @@ proc hello_nim() {.exportc, cdecl.} = blink_led() ## Finish - ## waitFor launch() + ## Previously: waitFor launch() GC_runOrc() -## Compile on NuttX: +## 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: import std/asyncdispatch From 6d845fcd4688e1fb5f891996452c774d760b654e Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Mon, 25 Dec 2023 14:13:53 +0000 Subject: [PATCH 26/31] Clean up --- examples/hello/hello_main.c | 4 ++-- examples/hello_nim/hello_nim_async.nim | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/hello/hello_main.c b/examples/hello/hello_main.c index cc49fe81d9..c3247020b4 100644 --- a/examples/hello/hello_main.c +++ b/examples/hello/hello_main.c @@ -41,7 +41,7 @@ int main(int argc, FAR char *argv[]) { printf("Hello, World!!\n"); - // Open the LED driver + // Open the LED Driver printf("Opening /dev/userleds\n"); int fd = open("/dev/userleds", O_WRONLY); if (fd < 0) @@ -67,7 +67,7 @@ int main(int argc, FAR char *argv[]) puts("Waiting..."); usleep(500 * 1000L); - // Turn on LED + // Turn off LED puts("Set LED 0 to 0"); ret = ioctl(fd, ULEDIOC_SETALL, 0); if (ret < 0) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 0ff85ed953..b1798b87ca 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -22,7 +22,7 @@ var ULEDIOC_SETALL {. ## Blink the LED proc blink_led() = - ## Open the LED driver + ## Open the LED Driver echo "Opening /dev/userleds" var fd = c_open("/dev/userleds", O_WRONLY) if fd < 0: @@ -40,7 +40,7 @@ proc blink_led() = echo "Waiting..." c_usleep(1000_000) - ## Turn on LED + ## Turn off LED echo "Set LED 0 to 0" ret = c_ioctl(fd, ULEDIOC_SETALL, 0) if ret < 0: From f94c0a584b54d78a68ea51f6301d677d0576633a Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Tue, 26 Dec 2023 03:16:03 +0000 Subject: [PATCH 27/31] Blink Loop --- examples/hello_nim/hello_nim_async.nim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index b1798b87ca..80c524d786 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -24,7 +24,7 @@ proc blink_led() = ## Open the LED Driver echo "Opening /dev/userleds" - var fd = c_open("/dev/userleds", O_WRONLY) + let fd = c_open("/dev/userleds", O_WRONLY) if fd < 0: echo "Failed to open /dev/userleds" return @@ -47,6 +47,10 @@ proc blink_led() = echo "ioctl(ULEDIOC_SETALL) failed" return + ## Wait again + echo "Waiting..." + c_usleep(1000_000) + ## Close the LED Driver c_close(fd) @@ -56,8 +60,9 @@ proc hello_nim() {.exportc, cdecl.} = ## Print something echo "Hello Nim!" - ## Blink the LED - blink_led() + ## Blink the LED 20 times + for loop in 0..19: + blink_led() ## Finish ## Previously: waitFor launch() From 1970acb5d8dad3d41b78f90f1e0f4c6461d93c2f Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Tue, 26 Dec 2023 10:49:13 +0000 Subject: [PATCH 28/31] Clean up --- examples/hello_nim/hello_nim_async.nim | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 80c524d786..2add47ef28 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -29,6 +29,9 @@ proc blink_led() = 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) @@ -51,12 +54,12 @@ proc blink_led() = echo "Waiting..." c_usleep(1000_000) - ## Close the LED Driver - c_close(fd) - ## Main Function in Nim proc hello_nim() {.exportc, cdecl.} = + ## On Return: Force the Garbage Collection + defer: GC_runOrc() + ## Print something echo "Hello Nim!" @@ -64,10 +67,6 @@ proc hello_nim() {.exportc, cdecl.} = for loop in 0..19: blink_led() - ## Finish - ## Previously: waitFor launch() - GC_runOrc() - ## To test the compilation for NuttX: # export TOPDIR=/workspaces/bookworm/nuttx # cd /workspaces/bookworm/apps/examples/hello_nim From 8a1a6de8b13dbbabf609876fd3605e9fbe88ab2d Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Tue, 26 Dec 2023 11:41:41 +0000 Subject: [PATCH 29/31] Clean up --- examples/hello_nim/hello_nim_async.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index 2add47ef28..b9d9937a60 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,7 +1,7 @@ import std/strformat ## String Formatting ## Import NuttX Functions and Macros from C. -## Based on /home/vscode/.choosenim/toolchains/nim-#devel/lib/std/syncio.nim +## 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: "", nodecl.} From 1c8af3517d6b7ce7dcf71c562cc3e5d7b634b54a Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Wed, 27 Dec 2023 23:20:26 +0000 Subject: [PATCH 30/31] Clean up --- examples/hello_nim/hello_nim_async.nim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index b9d9937a60..fd140b4393 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,5 +1,3 @@ -import std/strformat ## String Formatting - ## 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 {. @@ -88,8 +86,9 @@ proc hello_nim() {.exportc, cdecl.} = # board_userled_all: led=1, val=0 # board_userled_all: led=2, val=0 -## Previously: +## Previously: Test for Async Tasks import std/asyncdispatch +import std/strformat proc task(id: int): Future[void] {.async.} = for loop in 0..2: From aa554ea462afeedacae601d20841e49bedc84dc4 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 31 Dec 2023 12:02:35 +0000 Subject: [PATCH 31/31] Remove nodecl --- examples/hello_nim/hello_nim_async.nim | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/hello_nim/hello_nim_async.nim b/examples/hello_nim/hello_nim_async.nim index fd140b4393..4091203bcd 100644 --- a/examples/hello_nim/hello_nim_async.nim +++ b/examples/hello_nim/hello_nim_async.nim @@ -1,17 +1,16 @@ ## 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: "", - nodecl.} + importc: "open", header: "".} proc c_close(fd: cint): cint {. importc: "close", header: "", - nodecl, discardable.} + discardable.} proc c_ioctl(fd: cint, request: cint): cint {. importc: "ioctl", header: "", - nodecl, varargs.} + varargs.} proc c_usleep(usec: cuint): cint {. importc: "usleep", header: "", - nodecl, discardable.} + discardable.} var O_WRONLY {. importc: "O_WRONLY", header: "".}: cint var ULEDIOC_SETALL {.