MicroPython on Altera NIOS 2

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Fri Jun 19, 2015 7:25 pm

Hello,

I am pretty happy with the Altera NIOS 2 port. So far things are working as expected.
What steps are needed to get the code to the micropython github repository, so others can try ?

On a side note, my application requires to load a python script from memory and run it.
I've successfully tested one line scripts, but now want to try more complex multi line ones,
such as this one:

Code: Select all

count = 0
while count < 5:
    print count
    count += 1
Is the following an example of the correct method ?

Thanks

-

Code: Select all

static char *stack_top;
static char heap[2048];

void do_str(const char *src) 
{
    mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
    if (lex == NULL) {
        printf("MemoryError: lexer could not allocate memory\n");
        return;
    }
    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        qstr source_name = lex->source_name;
        mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
        mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
        mp_call_function_0(module_fun);
        nlr_pop();
    } else {
        // uncaught exception
        mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
    }	
}

int main(int argc, char **argv) 
{	
    int stack_dummy;
    stack_top = (char*)&stack_dummy;

    #if MICROPY_ENABLE_GC
    gc_init(heap, heap + sizeof(heap));
    #endif
    mp_init();

    do_str("count = 0");
    do_str("while count < 5:");
    do_str("  print count");
    do_str("  count += 1");
	
    mp_deinit();

    return 0;
}

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

Re: MicroPython on Altera NIOS 2

Post by dhylands » Fri Jun 19, 2015 8:41 pm

Look in stmhal/main.c and search for boot.py and main.py to see examples.

usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Mon Jun 22, 2015 5:42 pm

Thanks,

Is there another form that takes a char* of the multi line python script itself
instead of a file name ?

I don't have files, just the script in memory (hard coded in a const char*
for now, but could also be received from a socket at some point)

I won't be able to use this form, I believe:
int pyexec_file(const char *filename);

Here is what I would like to do, though it does not work
(note, that instead of the do_str(), pyexec_event_repl_process_char() is used)

Code: Select all

int stack_dummy;
stack_top = (char*)&stack_dummy;

#if MICROPY_ENABLE_GC
gc_init(heap, heap + sizeof(heap));
#endif
mp_init();	

const char* src = "count = 0\n"
		  "while count < 5:\n"
    		  "  print count\n"
    		  "  count += 1\n";

char* pChar = src;
while(*pChar != '\0')
{
  pyexec_event_repl_process_char(*pChar);
  pChar++;
}
		
mp_deinit();

usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Mon Jun 22, 2015 9:58 pm

Found that in the do_str() function needed to replace MP_PARSE_SINGLE_INPUT with MP_PARSE_FILE_INPUT. This will parse the input string as though it were a file, instead of a single line.

blipton
Posts: 8
Joined: Mon Dec 15, 2014 3:11 am

Re: MicroPython on Altera NIOS 2

Post by blipton » Tue Jun 30, 2015 7:00 pm

Hi, are you using an altera development board to test? I have a DE1 (cyclone II) that I'd love to try this on! Would it be possible to post your code?

I normally use Nios Eclipse IDE (windows) to include and build, but it looks like you needed to get into the makefiles and cygwin.. is that because you are compiling under linux and/or using command-line compilation instead of the GUI?

usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Tue Jun 30, 2015 7:59 pm

Hello,

I will post code asap.

Yes, I am newer dev board, but it should work on that version as well.

I am also using Windows and the Nios Eclipse IDE with the Nios cygwin cmd shell.
I typically build in the Nios cygwin cmd shell, but you can also build in the IDE.

Regards

GezerGeek
Posts: 2
Joined: Wed Jul 29, 2015 8:57 pm

Re: MicroPython on Altera NIOS 2

Post by GezerGeek » Wed Jul 29, 2015 9:02 pm

Awsome! This is something I have been thinking about for quite a while. Congrats on actually getting it done. I can't wait to be able to try it out myself. I have done quit a bit of work with the NIOS CPU surrounded by custom FPGA logic.

GezerGeek
Posts: 2
Joined: Wed Jul 29, 2015 8:57 pm

Re: MicroPython on Altera NIOS 2

Post by GezerGeek » Thu Sep 10, 2015 3:59 am

I would like to try it out on my DE0-Nano board. Any chance you could post the project code?

Best, gg

usjcarkm
Posts: 17
Joined: Fri Jun 05, 2015 9:25 pm

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Tue Sep 15, 2015 8:28 pm

A pull request for the nios2 port code to go into the main micropython code base is here:
https://github.com/micropython/micropython/pull/1463

The full main micropython code base with the nios2 branch is here in the meantime:
https://github.com/bitmachinejc/micropy ... tree/nios2

Regards

blipton
Posts: 8
Joined: Mon Dec 15, 2014 3:11 am

Re: MicroPython on Altera NIOS 2

Post by blipton » Mon Sep 28, 2015 12:29 am

I'm having a bit of trouble setting up the project...

For example, which version of Quartus/Nios2 are you using? I understand starting with Quartus/Eclipse 14, you can select between NiosII Gen2 (economy and fast) and NiosII Classic (economy, fast, and standard). I'm using Quartus/Nios 11.1 standard, as the newer versions of the Altera tools no longer support the Cyclone II (DE-1).

Unzipping micropython-nios2-nios2 and executing './buildlib' from within the nios2 sub-folder ( Nios II 15 Command Shell ) I get an error: nios2-elf-ar: build/py/mpstate.o: No such file or directory

Image


I also tried creating a Hello World project, and copying the nios2 and py folders over. There seems to be alot of other folders, but figured I'd start small. Unfortunately, I couldn't get Eclipse to use the Makefile from within the Nios2 folder, so I moved that to the root and changed the include statements from : "include ../py/mkenv.mk" to "include py/mkenv.mk" Now it doesn't give any errors, but says:

make all
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
make: Nothing to be done for `all'.

I must admit, I'm treading way over my head when it comes to makefiles and bash script files. Any recommended steps on how to get started in creating/compiling the MicroPython project in the command line or via the Nios IDE?

Post Reply