Using the C API?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API?

Post by dhylands » Wed Jul 13, 2016 5:39 pm

cduran wrote: Wouldn't I also have to add the new callbacks to qstrdefsport.h?
Since I wrote that example, qstr definition has been largely automated so that you no longer need to update qstrdefsport.h

I've gone ahead and rebased my example to the latest master. You can find the rebased example here:
https://github.com/dhylands/micropython ... 8997508622

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Using the C API?

Post by cduran » Wed Jul 13, 2016 8:37 pm

Just one more question, in your example, how would I pass an argument(s) from python to C? ie. c_sample.call_callback(X, Y, ... )

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API?

Post by dhylands » Wed Jul 13, 2016 10:32 pm

I've extended the example to have various numbers of arguments passed into the C functions.
https://github.com/dhylands/micropython ... 055abb5fef

Some sample output from running:

Code: Select all

>>> import c_sample
>>> c_sample.func_0()
c_sample_func_0
>>> c_sample.func_1('a')
c_sample_func_1: arg1 = a
>>> c_sample.func_2(47, 'b')
c_sample_func_2: arg1 = 47, arg2 = b
>>> c_sample.func_3('c', 'd', 'e')
c_sample_func_3: arg1 = c, arg2 = d, arg3 = e
>>> c_sample.func_var(11, 22, 33, 44, 55, 66)
c_sample_func_var: num_args = 6
  arg[0] = 11
  arg[1] = 22
  arg[2] = 33
  arg[3] = 44
  arg[4] = 55
  arg[5] = 66
>>> c_sample.func_var()
c_sample_func_var: num_args = 0
>>> c_sample.func_var('abc', 'def')
c_sample_func_var: num_args = 2
  arg[0] = abc
  arg[1] = def
>>> c_sample.func_int(123)
c_sample_func_int: num = 123
>>> c_sample.func_int('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert str to int
>>> c_sample.func_str('a')
c_sample_func_str: str = 'a'
>>> c_sample.func_str(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert 'int' object to str implicitly

balasubramanian.sb
Posts: 2
Joined: Thu Jan 05, 2017 10:53 am

Re: Using the C API?

Post by balasubramanian.sb » Fri Jan 27, 2017 7:01 am

Hi
Iam trying to import builtin micropython libraries in cc3200 launchxl can any one please help me how to import those libraries in the flash
and to get in Repl enviroinment

Iam trying as in http://processors.wiki.ti.com/index.php ... hon_CC3200

please reply

User avatar
AaronKelly
Posts: 7
Joined: Sun Jun 19, 2016 7:44 am

Re: Using the C API?

Post by AaronKelly » Fri Jan 27, 2017 11:12 am

@cduran

Nope, the only places you need to refrence it is in the makefile

Code: Select all

# source files
SRC_C = ...
and mpconfigport.h

Code: Select all

extern const struct _mp_obj_module_t mp_module_sample;
...
...
...
#define MICROPY_PORT_BUILTIN_MODULES \
	...
	{ MP_ROM_QSTR(MP_QSTR_sample), MP_ROM_PTR(&mp_module_sample) }, \
which automatically modifies qstrdefsport.h if I'm not mistaken, judging by the source code of it.

Code: Select all

29: // All the qstr definitions in this file are available as constants.
30: // That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx.
I tried with this source code, named modtest.c on the Unix port, (you may need a few dependencies if you are working on a micro controller)

Code: Select all

/*
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Aaron Kelly
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdio.h>
#include <stdint.h>

#include "py/runtime.h"
#include "py/obj.h"

STATIC mp_obj_t test_init(void) {
    return mp_obj_new_str("test", 4, false);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(test_init_obj, test_init);

STATIC const mp_rom_map_elem_t test_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_test) },

    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&test_init_obj) },
};

STATIC MP_DEFINE_CONST_DICT(test_module_globals, test_module_globals_table);

const mp_obj_module_t mp_module_test = {
    .base = { &mp_type_module },
    .globals = (mp_obj_dict_t*)&test_module_globals,
};
then I got this output

Code: Select all

> Import test
> test.init()
'test'
Hope this helps

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API?

Post by dhylands » Fri Apr 21, 2017 6:19 pm

I just rebased the c_sample2 example in my repository to the latest master.

You can find the branch here:
https://github.com/dhylands/micropython/tree/c-sample2

and the commit with the c_sample2 files on it here:
https://github.com/dhylands/micropython ... 57f5d7281b

For my own reference, the other conversation which I updated is this one:
viewtopic.php?f=16&t=2861

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Using the C API?

Post by cduran » Thu Feb 27, 2020 2:47 pm

I've been using this sample for a while but now I'm getting build errors and even when I fix the build errors I'm getting crashes. I recently updated to mp 1.12. Is there an updated sample for this? Thank you.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API?

Post by dhylands » Sat Feb 29, 2020 9:09 pm

There is also a c-sample3 branch, although its also fairly old.

I'm currently on vacation and don't have my development machine, so I can't verify that c-sample3 works or not.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using the C API?

Post by dhylands » Sun Mar 01, 2020 2:35 am

I created a new branch here: https://github.com/dhylands/micropython/tree/c-sample4 that works with the latest version of micropython (v1.12-210)

Post Reply