_NATIVE_ Function for ADC and DAC

199 views
Skip to first unread message

kittipon meesompop

unread,
Dec 18, 2013, 12:39:16 AM12/18/13
to python-o...@googlegroups.com
Hello everybody,

i am tring to analyse the example _NATIVE_ programs. I understand a little bit how it works. But i am totally not sure how to write such a program myself for ADC and DAC.
Can someone explain it to me please ? or any suggestions where can i find documents about the topic.

best regards

Dean Hall

unread,
Dec 18, 2013, 8:09:33 AM12/18/13
to python-o...@googlegroups.com
To write a native function, declare a python function with the expected arguments and the body has the "pass" statement.
Inside this function's doc string, put the magic sequence "__NATIVE__" and follow that with C code.
Notice that two underscore are prepended and two are appended. Four underscores total.
The __NATIVE__ lets the build system know that it should process this function in a special way
and it will take care of everything for you.

Inside the C code, use special macros to access the arguments:
NATIVE_GET_NUM_ARGS() // "returns " a C int
NATIVE_GET_LOCAL(n) // where n is a C int. Returns the Python object at argument index n.
Remember arguments are on a stack so the numbering may be reverse of what you think.

Then do whatever stuff in C you need to do.

The most important thing is that if you do more than one allocation from the VM's heap,
you'll need to protect those items because they aren't connected to the VM's roots
and the later allocations may reclaim the earlier allocations.
Use heap_gcPushTempRoot() and heap_gcPopTempRoot() for this protection.
Follow example in src/lib/sys.py sys.heap().

Finally, use NATIVE_SET_TOS() to put a python object on the top of the stack.
This wil serve as the native function's return value for Python.
Then return PM_RET_OK if all is well or another value of type PmReturn_t to signal an exception.

regards,

!!Dean
> --
> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google Group.
> Site: http://groups.google.com/group/python-on-a-chip
>
> ---
> You received this message because you are subscribed to the Google Groups "python-on-a-chip" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python-on-a-ch...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

kittipon meesompop

unread,
Dec 18, 2013, 8:41:52 AM12/18/13
to python-o...@googlegroups.com
thank a lot for the answer :)

i wrote a native function to read adc like this:

def _analogRead(port, pin):

"""__NATIVE__

.....

....

pPmObj_t pa = ADC_GetConversionValue(ADC1);

NATIVE_SET_TOS(pa);

return retval;

"""

pass


and call it with: test = gpio.analogRead(PA1) and print test

i got "none" as output. i dont know wheter i have done something wrong.

best regards


Dean Hall

unread,
Dec 18, 2013, 8:51:47 AM12/18/13
to python-o...@googlegroups.com
I presume ADC_GetConversionValue() returns a C integer.
You need to create a python integer with this value: int_new(...)
and put the python integer on the top of stack.

!!Dean
Message has been deleted

kittipon meesompop

unread,
Dec 18, 2013, 11:45:48 AM12/18/13
to python-o...@googlegroups.com
thanks for your advice!!
i got the next thing what i do not understand. Does pymite run c-function only once or c-functions can be called in a python-loop?

i try to read adc-value in a while loop but i got only same value. The value doesnt change, although i change the voltage at the input.

best regards

Dean Hall

unread,
Dec 19, 2013, 11:59:03 PM12/19/13
to python-o...@googlegroups.com
If programmed correctly (no memory leaks added), you should be able to call a native function as often as you like, including within a while loop.
I can't explain what is going on with your experiment.

I suggest you comment away your ADC value for the moment.
For testing, insert a known value in your native function to be sure you are able to return a known value from native to Python.
For example:

def getAdc1():
"""__NATIVE__
<snip>
// u16_adc_value = ADC_GetConversionValue(ADC1);
int_new(0x5A, &pa);
NATIVE_SET_TOS(pa);
return PM_RET_OK;
"""
pass

Now call your native function and verify it always returns 0x5A, even in a while loop.

!!Dean

kittipon meesompop

unread,
Dec 20, 2013, 1:21:55 AM12/20/13
to python-o...@googlegroups.com

i also got the value of adc in endless loop but i got only the same value although i change the voltage at input.
 i am not sure whether i call the function and fetch the value correctly. Are there any examples with calling and fetching value?
best regard and merry christmas :)
Reply all
Reply to author
Forward
0 new messages