Hello,
I am trying to pass a pointer to an array like so:
let task1name: cstring = "task 1"
proc task1() =
while true:
var l {.volatile.} = 50000.uint16
while l > 0.uint16:
l.dec
P4OUT[] = P4OUT[] and not BIT7.uint8
var task1Stack: array[256, uint8]
let task1Info = TaskInfo(name: task1name,
task: task1,
taskStackPtr: cast[pointer](task1Stack))
The cast to a pointer isn't working. What is the proper way to pass this pointer (or even create the array)?
Your task1Stack is a value type, not a ref or ptr. Nim arrays are generally value types, living on the stack if defined inside of a proc, or living in a global data area (bss segment) if defined outside of a proc.
Code like this may work:
var a: array[2, int]
var p: pointer = addr(a)
var p2: pointer = cast[pointer](a.addr)
let task1name: cstring = "task 1"
proc task1() =
while true:
var l {.volatile.} = 50000.uint16
while l > 0.uint16:
l.dec
P4OUT[] = P4OUT[] and not BIT7.uint8
var task1Stack: array[256, uint8]
let task1Info = TaskInfo(name: task1name,
task: task1,
taskStackPtr: task1Stack.addr)
Now I get the compilation error Error: system module needs 'genericReset'.
I am trying to write this for --cpu:msp430 or --cpu:avr with --os:standalone.
Try to eliminate parts of your code until that error disappears, it should show you what causes it.
Btw bumping a thread after 4 hours is a bit too much :)
The following code works, but I still don't understand what was wrong with the first code sample I gave? Could someone please help a friendly idiot... I learned to program in 8-bit assembly first and then moved to C. I still don't grok intuitively anything higher level. I understand C because I get how it maps to assembly. I try to understand Nim by how it maps to C, but apparently it's not working for me! :)
I thought that I was basically copy-pastaing examples for object construction from the manual before.
proc task1() =
while true:
var l {.volatile.} = 50000.uint16
while l > 0.uint16:
l.dec
P4OUT[] = P4OUT[] and not BIT7.uint8
var
task1Stack: array[256, uint8]
task1Info:TaskInfo
task1Info.name = "task 1"
task1Info.task = task1
task1Info.taskStackPtr = task1Stack.addr
You are trying to use tasks on 8 bit microcontroller without OS?
That is interesting, I never tried that with C or assembly on these devices.
The error message "system module needs 'genericReset'" is not too helpful for me, but Araq may know what it means :-)
What you may try is to put all your code that is currently not included in procs in a main() proc. Maybe your "let task1Info = TaskInfo()" would work inside of a proc.
Try changing
TaskHandle* = (proc())
to
TaskHandle* = (proc() {.nimcall.})
proc() is a closure and closures are GC' ed and takes two machine words(pointer to proc and environment)
Turns out the issue is that the initialization syntax in the first example I gave uses the garbage collected heap. I'm not sure why it is done that way for an object type. My application doesn't have a heap.
I haven't found a way around that. My intention was a const struct definition. I haven't found a way to do that yet. The defined struct must end up in the .rodata section of an elf file, ie. be prepended by const modifier.
Turns out the issue is that the initialization syntax in the first example I gave uses the garbage collected heap. I'm not sure why it is done that way for an object type. My application doesn't have a heap.
Are you sure? Your type appears to be an object, not a ref object so this shouldn't happen. Furthermore, aren't you disabling the GC via --gc:none, for a embedded applications you should probably do that.
Thank you for the response. Seriously thank you.
I did disable it, and that's when I get the strange error. Then I was trying to change different things to zero in on the problem. When I get rid of the GC:none flag, I get the error that always comes up when trying to use the heap with an int size of 16 bits. That makes me think it is allocating.
I'm not at computer now, so I can't get the other message, but you can replicate with os:standalone and CPU:msp430 and compile only. Instantiate any object type with the above syntax.