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.
User avatar
dhylands
Posts: 3227
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: MicroPython on Altera NIOS 2

Post by dhylands » Fri Jun 12, 2015 7:36 pm

So when you added nlrx86.S back in, the log shows it was executing gcc and not nios2-elf-gcc

Shouldn't it have been using the cross compiler?

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

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Mon Jun 15, 2015 2:06 pm

I had forgot to set the CROSS switch the last time around.
CROSS = 1

It is now compiling, but not linking, with the stock py.mk file that has the lines:
nlrx86.o \
nlrx64.o \

I have a preliminary nios2_mphal.c in place as well.

Note, that this file (mpconfigport.h) has the same content as the one found in the minimal folder, apart from these additions:

#define MICROPY_NLR_SETJMP (1)
#define BYTES_PER_WORD (4)
#define MP_ENDIANNESS_LITTLE (1)

-

Here is the output currently, showing the errors:

Code: Select all

.
.
CC main.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/main.o main.c
CC uart_core.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/uart_core.o uart_core.c
CC uart_extra.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/uart_extra.o uart_extra.c
CC nios2_mphal.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/nios2_mphal.o nios2_mphal.c
nios2_mphal.c:38: warning: 'sighandler' defined but not used
CC ../stmhal/printf.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/stmhal/printf.o ../stmhal/printf.c
CC ../stmhal/pyexec.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/stmhal/pyexec.o ../stmhal/pyexec.c
../stmhal/pyexec.c: In function 'parse_compile_execute':
../stmhal/pyexec.c:71: warning: implicit declaration of function 'mp_hal_set_interrupt_char'
CC ../lib/libc/string0.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/lib/libc/string0.o ../lib/libc/string0.c
CC ../lib/mp-readline/readline.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -nostdlib -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/lib/mp-readline/readline.o ../lib/mp-readline
/readline.c
LINK build/firmware.elf
nios2-elf-ld -nostdlib -T stm32f405.ld -Map=build/firmware.elf.map --cref -o build/firmware.elf build/py/mpstate.o build/py/nlrx86.o build/py/nlrx64.o build/py/nlrthumb.o build/py/nlrxtensa.o build/py
/nlrsetjmp.o build/py/malloc.o build/py/gc.o build/py/qstr.o build/py/vstr.o build/py/mpprint.o build/py/unicode.o build/py/mpz.o build/py/lexer.o build/py/lexerstr.o build/py/lexerunix.o build/py/par
se.o build/py/scope.o build/py/compile.o build/py/emitcommon.o build/py/emitcpy.o build/py/emitbc.o build/py/asmx64.o build/py/emitnx64.o build/py/asmx86.o build/py/emitnx86.o build/py/asmthumb.o buil
d/py/emitnthumb.o build/py/emitinlinethumb.o build/py/asmarm.o build/py/emitnarm.o build/py/formatfloat.o build/py/parsenumbase.o build/py/parsenum.o build/py/emitglue.o build/py/runtime.o build/py/na
tiveglue.o build/py/stackctrl.o build/py/argcheck.o build/py/warning.o build/py/map.o build/py/obj.o build/py/objarray.o build/py/objattrtuple.o build/py/objbool.o build/py/objboundmeth.o build/py/obj
cell.o build/py/objclosure.o build/py/objcomplex.o build/py/objdict.o build/py/objenumerate.o build/py/objexcept.o build/py/objfilter.o build/py/objfloat.o build/py/objfun.o build/py/objgenerator.o bu
ild/py/objgetitemiter.o build/py/objint.o build/py/objint_longlong.o build/py/objint_mpz.o build/py/objlist.o build/py/objmap.o build/py/objmodule.o build/py/objobject.o build/py/objproperty.o build/p
y/objnone.o build/py/objnamedtuple.o build/py/objrange.o build/py/objreversed.o build/py/objset.o build/py/objsingleton.o build/py/objslice.o build/py/objstr.o build/py/objstrunicode.o build/py/objstr
ingio.o build/py/objtuple.o build/py/objtype.o build/py/objzip.o build/py/opmethods.o build/py/sequence.o build/py/stream.o build/py/binary.o build/py/builtinimport.o build/py/builtinevex.o build/py/m
odarray.o build/py/modbuiltins.o build/py/modcollections.o build/py/modgc.o build/py/modio.o build/py/modmath.o build/py/modcmath.o build/py/modmicropython.o build/py/modstruct.o build/py/modsys.o bui
ld/py/vm.o build/py/bc.o build/py/showbc.o build/py/repl.o build/py/smallint.o build/py/frozenmod.o build/py/../extmod/moductypes.o build/py/../extmod/modujson.o build/py/../extmod/modure.o build/py/.
./extmod/moduzlib.o build/py/../extmod/moduheapq.o build/py/../extmod/moduhashlib.o build/py/../extmod/modubinascii.o build/py/../extmod/modmachine.o build/main.o build/uart_core.o build/uart_extra.o
build/nios2_mphal.o build/stmhal/printf.o build/stmhal/pyexec.o build/lib/libc/string0.o build/lib/mp-readline/readline.o
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn':
nios2_mphal.c:(.text+0x4): multiple definition of `mp_hal_stdout_tx_strn'
build/uart_core.o:uart_core.c:(.text+0x8): first defined here
build/nios2_mphal.o: In function `mp_hal_stdout_tx_str':
nios2_mphal.c:(.text+0x14): multiple definition of `mp_hal_stdout_tx_str'
build/uart_extra.o:uart_extra.c:(.text+0x0): first defined here
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn_cooked':
nios2_mphal.c:(.text+0x40): multiple definition of `mp_hal_stdout_tx_strn_cooked'
build/uart_extra.o:uart_extra.c:(.text+0x2c): first defined here
build/nios2_mphal.o: In function `mp_hal_stdin_rx_chr':
nios2_mphal.c:(.text+0x44): multiple definition of `mp_hal_stdin_rx_chr'
build/uart_core.o:uart_core.c:(.text+0x0): first defined here
build/py/nlrsetjmp.o: In function `nlr_setjmp_jump':
nlrsetjmp.c:(.text+0x28): undefined reference to `longjmp'
build/py/gc.o: In function `gc_init':
gc.c:(.text+0x724): undefined reference to `__udivsi3'
build/py/mpprint.o: In function `mp_print_int':
mpprint.c:(.text+0x208): undefined reference to `__umodsi3'
mpprint.c:(.text+0x23c): undefined reference to `__udivsi3'
build/py/lexer.o: In function `mp_lexer_next_token_into':
lexer.c:(.text+0x9b0): warning: global pointer relative relocation at address 0x100009b8 when _gp not defined

make: *** [build/firmware.elf] Error 1

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

Re: MicroPython on Altera NIOS 2

Post by dhylands » Mon Jun 15, 2015 2:53 pm

I see the following undefined references:

longjmp
__udivsi3
__umodsi3

longjmp comes from the C runtime library and __udivsi3 and __umodsi3 are helper routines for performing arithmetic which isn't supported natively by the CPU (and normally come from libgcc.a)

__udivsi3 does unsigned full word division
__umodsi3 provides the remainder of unsigned full-word division

The stmhal variant of micropython is built with the -nostdlib option so that we don't pull in the C runtime library. It has a side effect of also not pulling in libgcc. For your CPU, you should probably remove the -nostdlib option.

I also noticed that you're trying to use the stm32f405.ld linker script file. This will probably not work, unless the memory map of your CPU is identical to the memory map of the stm32F405.

I also see some duplicate symbols for mp_hal_stdout_xxx which normally would come from mphal.c and you seem to be pulling mphal.c and have defined versions in uart_core.c

The warning from lexer.c probably relates to the memory map being incorrect for your CPU, but I'm not 100% sure.

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

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Tue Jun 16, 2015 2:28 pm

Thanks. Yes that resolved some of the errors.

Here below is the current output with the remaining linker errors.
It appears to still not be able to load libgcc.a based on your explanation.
I do see the file on my machine however. I may need to consult the altera help groups as well:
C:\altera\12.1sp1\nios2eds\bin\gnu\H-i686-mingw32\lib\gcc\nios2-elf\4.1.2\libgcc.a

Code: Select all

$ make V=1
python ../py/makeversionhdr.py build/genhdr/mpversion.h
mkdir -p build/
mkdir -p build/lib/libc/
mkdir -p build/lib/mp-readline/
mkdir -p build/py/
mkdir -p build/py/../extmod/
mkdir -p build/stmhal/
LINK build/firmware.elf
nios2-elf-ld -Map=build/firmware.elf.map --cref -o build/firmware.elf build/py/mpstate.o build/py/nlrx86.o build/py/nlrx64.o build/py/nlrthumb.o build/py/nlrxtensa.o build/py/nlrsetjmp.o build/py/mall
oc.o build/py/gc.o build/py/qstr.o build/py/vstr.o build/py/mpprint.o build/py/unicode.o build/py/mpz.o build/py/lexer.o build/py/lexerstr.o build/py/lexerunix.o build/py/parse.o build/py/scope.o buil
d/py/compile.o build/py/emitcommon.o build/py/emitcpy.o build/py/emitbc.o build/py/asmx64.o build/py/emitnx64.o build/py/asmx86.o build/py/emitnx86.o build/py/asmthumb.o build/py/emitnthumb.o build/py
/emitinlinethumb.o build/py/asmarm.o build/py/emitnarm.o build/py/formatfloat.o build/py/parsenumbase.o build/py/parsenum.o build/py/emitglue.o build/py/runtime.o build/py/nativeglue.o build/py/stackc
trl.o build/py/argcheck.o build/py/warning.o build/py/map.o build/py/obj.o build/py/objarray.o build/py/objattrtuple.o build/py/objbool.o build/py/objboundmeth.o build/py/objcell.o build/py/objclosure
.o build/py/objcomplex.o build/py/objdict.o build/py/objenumerate.o build/py/objexcept.o build/py/objfilter.o build/py/objfloat.o build/py/objfun.o build/py/objgenerator.o build/py/objgetitemiter.o bu
ild/py/objint.o build/py/objint_longlong.o build/py/objint_mpz.o build/py/objlist.o build/py/objmap.o build/py/objmodule.o build/py/objobject.o build/py/objproperty.o build/py/objnone.o build/py/objna
medtuple.o build/py/objrange.o build/py/objreversed.o build/py/objset.o build/py/objsingleton.o build/py/objslice.o build/py/objstr.o build/py/objstrunicode.o build/py/objstringio.o build/py/objtuple.
o build/py/objtype.o build/py/objzip.o build/py/opmethods.o build/py/sequence.o build/py/stream.o build/py/binary.o build/py/builtinimport.o build/py/builtinevex.o build/py/modarray.o build/py/modbuil
tins.o build/py/modcollections.o build/py/modgc.o build/py/modio.o build/py/modmath.o build/py/modcmath.o build/py/modmicropython.o build/py/modstruct.o build/py/modsys.o build/py/vm.o build/py/bc.o b
uild/py/showbc.o build/py/repl.o build/py/smallint.o build/py/frozenmod.o build/py/../extmod/moductypes.o build/py/../extmod/modujson.o build/py/../extmod/modure.o build/py/../extmod/moduzlib.o build/
py/../extmod/moduheapq.o build/py/../extmod/moduhashlib.o build/py/../extmod/modubinascii.o build/py/../extmod/modmachine.o build/main.o build/uart_core.o build/uart_extra.o build/nios2_mphal.o build/
stmhal/printf.o build/stmhal/pyexec.o build/lib/libc/string0.o build/lib/mp-readline/readline.o
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn':
nios2_mphal.c:(.text+0x4): multiple definition of `mp_hal_stdout_tx_strn'
build/uart_core.o:uart_core.c:(.text+0x8): first defined here
build/nios2_mphal.o: In function `mp_hal_stdout_tx_str':
nios2_mphal.c:(.text+0x14): multiple definition of `mp_hal_stdout_tx_str'
build/uart_extra.o:uart_extra.c:(.text+0x0): first defined here
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn_cooked':
nios2_mphal.c:(.text+0x40): multiple definition of `mp_hal_stdout_tx_strn_cooked'
build/uart_extra.o:uart_extra.c:(.text+0x2c): first defined here
build/nios2_mphal.o: In function `mp_hal_stdin_rx_chr':
nios2_mphal.c:(.text+0x44): multiple definition of `mp_hal_stdin_rx_chr'
build/uart_core.o:uart_core.c:(.text+0x0): first defined here
build/py/nlrsetjmp.o: In function `nlr_setjmp_jump':
nlrsetjmp.c:(.text+0x28): undefined reference to `longjmp'
build/py/gc.o: In function `gc_init':
gc.c:(.text+0x724): undefined reference to `__udivsi3'
build/py/mpprint.o: In function `mp_print_int':
mpprint.c:(.text+0x208): undefined reference to `__umodsi3'
mpprint.c:(.text+0x23c): undefined reference to `__udivsi3'
build/py/runtime.o: In function `mp_iternext':
runtime.c:(.text+0x764): undefined reference to `setjmp'
build/py/runtime.o: In function `mp_parse_compile_execute':
runtime.c:(.text+0x1488): undefined reference to `setjmp'
build/py/map.o: In function `mp_map_lookup':
map.c:(.text+0x310): undefined reference to `__umodsi3'
map.c:(.text+0x3bc): undefined reference to `__umodsi3'
map.c:(.text+0x3ec): undefined reference to `__umodsi3'
build/py/objgetitemiter.o: In function `it_iternext':
objgetitemiter.c:(.text+0x74): undefined reference to `setjmp'
build/py/objint.o: In function `mp_obj_int_formatted':
objint.c:(.text+0x37c): undefined reference to `__udivsi3'
objint.c:(.text+0x39c): undefined reference to `__udivsi3'
objint.c:(.text+0x410): undefined reference to `__modsi3'
objint.c:(.text+0x420): undefined reference to `__divsi3'
build/py/objrange.o: In function `range_len':
objrange.c:(.text+0x74): undefined reference to `__divsi3'
build/py/modbuiltins.o:(.rodata+0x3fc): undefined reference to `mp_builtin_open_obj'
build/py/vm.o: In function `mp_execute_bytecode':
vm.c:(.text+0x80): undefined reference to `setjmp'
build/py/smallint.o: In function `mp_small_int_mul_overflow':
smallint.c:(.text+0x2c): undefined reference to `__divsi3'
smallint.c:(.text+0x4c): undefined reference to `__divsi3'
smallint.c:(.text+0x6c): undefined reference to `__divsi3'
build/py/smallint.o: In function `mp_small_int_modulo':
smallint.c:(.text+0x98): undefined reference to `__modsi3'
build/py/smallint.o: In function `mp_small_int_floor_divide':
smallint.c:(.text+0xec): undefined reference to `__divsi3'
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn':
nios2_mphal.c:(.text+0x10): undefined reference to `write'
build/nios2_mphal.o: In function `mp_hal_stdin_rx_chr':
nios2_mphal.c:(.text+0x58): undefined reference to `read'
build/stmhal/pyexec.o: In function `parse_compile_execute':
pyexec.c:(.text+0x60): undefined reference to `setjmp'
make: *** [build/firmware.elf] Error 1
Also, here is the current contents of my Makefile

Code: Select all

include ../py/mkenv.mk

CROSS = 1

# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h

# include py core make definitions
include ../py/py.mk

ifeq ($(CROSS), 1)
CROSS_COMPILE = nios2-elf-
endif

INC =  -I.
INC += -I..
INC += -I../lib/mp-readline
INC += -I../stmhal
INC += -I$(BUILD)

ifeq ($(CROSS), 1)
CFLAGS = $(INC) -Wall -ansi -std=gnu99 -fsingle-precision-constant $(COPT)
else
CFLAGS = $(INC) -Wall -ansi -std=gnu99 $(COPT)
endif

#Debugging/Optimization
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -ggdb
else
CFLAGS += -Os -DNDEBUG
endif

ifeq ($(CROSS), 1)
LDFLAGS = -Map=$@.map --cref
else
LD = gcc
LDFLAGS = -Wl,-Map=$@.map,--cref
endif
LIBS =

SRC_C = \
	main.c \
	uart_core.c \
	uart_extra.c \
	nios2_mphal.c \
	stmhal/printf.c \
	stmhal/pyexec.c \
	lib/libc/string0.c \
	lib/mp-readline/readline.c \

SRC_S = \
	gchelper.s \

OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))

all: $(BUILD)/firmware.elf

$(BUILD)/firmware.elf: $(OBJ)
	$(ECHO) "LINK $@"
	$(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
	$(Q)$(SIZE) $@

# Run emulation build on a POSIX system with suitable terminal settings
run:
	stty raw opost -echo
	build/firmware.elf
	@echo Resetting terminal...
# This sleep is useful to spot segfaults
	sleep 1
	reset

test: $(BUILD)/firmware.elf
	$(Q)/bin/echo -e "print('hello world!', list(x+1 for x in range(10)), end='eol\\\\n')\\r\\n\\x04" | $(BUILD)/firmware.elf | tail -n2 | grep "^hello world! \\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10\\]eol"

include ../py/mkrules.mk

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

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Tue Jun 16, 2015 3:08 pm

I was able to resolve some of the linker errors by setting the LIBS
item in the makefile to the path to the libgcc.a:

Code: Select all

LIBS = C:/altera/12.1sp1/nios2eds/bin/gnu/H-i686-mingw32/lib/gcc/nios2-elf/4.1.2/libgcc.a
Here is the current compiler output. A few linker errors remain:

Code: Select all

$ make V=1
python ../py/makeversionhdr.py build/genhdr/mpversion.h
mkdir -p build/
mkdir -p build/lib/libc/
mkdir -p build/lib/mp-readline/
mkdir -p build/py/
mkdir -p build/py/../extmod/
mkdir -p build/stmhal/
LINK build/firmware.elf
nios2-elf-ld -Map=build/firmware.elf.map --cref -o build/firmware.elf build/py/mpstate.o build/py/nlrx86.o build/py/nlrx64.o build/py/nlrthumb.o build/py/nlrxtensa.o build/py/nlrsetjmp.o build/py/mall
oc.o build/py/gc.o build/py/qstr.o build/py/vstr.o build/py/mpprint.o build/py/unicode.o build/py/mpz.o build/py/lexer.o build/py/lexerstr.o build/py/lexerunix.o build/py/parse.o build/py/scope.o buil
d/py/compile.o build/py/emitcommon.o build/py/emitcpy.o build/py/emitbc.o build/py/asmx64.o build/py/emitnx64.o build/py/asmx86.o build/py/emitnx86.o build/py/asmthumb.o build/py/emitnthumb.o build/py
/emitinlinethumb.o build/py/asmarm.o build/py/emitnarm.o build/py/formatfloat.o build/py/parsenumbase.o build/py/parsenum.o build/py/emitglue.o build/py/runtime.o build/py/nativeglue.o build/py/stackc
trl.o build/py/argcheck.o build/py/warning.o build/py/map.o build/py/obj.o build/py/objarray.o build/py/objattrtuple.o build/py/objbool.o build/py/objboundmeth.o build/py/objcell.o build/py/objclosure
.o build/py/objcomplex.o build/py/objdict.o build/py/objenumerate.o build/py/objexcept.o build/py/objfilter.o build/py/objfloat.o build/py/objfun.o build/py/objgenerator.o build/py/objgetitemiter.o bu
ild/py/objint.o build/py/objint_longlong.o build/py/objint_mpz.o build/py/objlist.o build/py/objmap.o build/py/objmodule.o build/py/objobject.o build/py/objproperty.o build/py/objnone.o build/py/objna
medtuple.o build/py/objrange.o build/py/objreversed.o build/py/objset.o build/py/objsingleton.o build/py/objslice.o build/py/objstr.o build/py/objstrunicode.o build/py/objstringio.o build/py/objtuple.
o build/py/objtype.o build/py/objzip.o build/py/opmethods.o build/py/sequence.o build/py/stream.o build/py/binary.o build/py/builtinimport.o build/py/builtinevex.o build/py/modarray.o build/py/modbuil
tins.o build/py/modcollections.o build/py/modgc.o build/py/modio.o build/py/modmath.o build/py/modcmath.o build/py/modmicropython.o build/py/modstruct.o build/py/modsys.o build/py/vm.o build/py/bc.o b
uild/py/showbc.o build/py/repl.o build/py/smallint.o build/py/frozenmod.o build/py/../extmod/moductypes.o build/py/../extmod/modujson.o build/py/../extmod/modure.o build/py/../extmod/moduzlib.o build/
py/../extmod/moduheapq.o build/py/../extmod/moduhashlib.o build/py/../extmod/modubinascii.o build/py/../extmod/modmachine.o build/main.o build/uart_core.o build/uart_extra.o build/nios2_mphal.o build/
stmhal/printf.o build/stmhal/pyexec.o build/lib/libc/string0.o build/lib/mp-readline/readline.o C:/altera/12.1sp1/nios2eds/bin/gnu/H-i686-mingw32/lib/gcc/nios2-elf/4.1.2/libgcc.a
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn':
nios2_mphal.c:(.text+0x4): multiple definition of `mp_hal_stdout_tx_strn'
build/uart_core.o:uart_core.c:(.text+0x8): first defined here
build/nios2_mphal.o: In function `mp_hal_stdout_tx_str':
nios2_mphal.c:(.text+0x14): multiple definition of `mp_hal_stdout_tx_str'
build/uart_extra.o:uart_extra.c:(.text+0x0): first defined here
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn_cooked':
nios2_mphal.c:(.text+0x40): multiple definition of `mp_hal_stdout_tx_strn_cooked'
build/uart_extra.o:uart_extra.c:(.text+0x2c): first defined here
build/nios2_mphal.o: In function `mp_hal_stdin_rx_chr':
nios2_mphal.c:(.text+0x44): multiple definition of `mp_hal_stdin_rx_chr'
build/uart_core.o:uart_core.c:(.text+0x0): first defined here
build/py/nlrsetjmp.o: In function `nlr_setjmp_jump':
nlrsetjmp.c:(.text+0x28): undefined reference to `longjmp'
build/py/runtime.o: In function `mp_iternext':
runtime.c:(.text+0x764): undefined reference to `setjmp'
build/py/runtime.o: In function `mp_parse_compile_execute':
runtime.c:(.text+0x1488): undefined reference to `setjmp'
build/py/objgetitemiter.o: In function `it_iternext':
objgetitemiter.c:(.text+0x74): undefined reference to `setjmp'
build/py/modbuiltins.o:(.rodata+0x3fc): undefined reference to `mp_builtin_open_obj'
build/py/vm.o: In function `mp_execute_bytecode':
vm.c:(.text+0x80): undefined reference to `setjmp'
build/nios2_mphal.o: In function `mp_hal_stdout_tx_strn':
nios2_mphal.c:(.text+0x10): undefined reference to `write'
build/nios2_mphal.o: In function `mp_hal_stdin_rx_chr':
nios2_mphal.c:(.text+0x58): undefined reference to `read'
build/stmhal/pyexec.o: In function `parse_compile_execute':
pyexec.c:(.text+0x60): undefined reference to `setjmp'
make: *** [build/firmware.elf] Error 1

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

Re: MicroPython on Altera NIOS 2

Post by dhylands » Tue Jun 16, 2015 4:37 pm

usjcarkm wrote:I was able to resolve some of the linker errors by setting the LIBS
item in the makefile to the path to the libgcc.a:

Code: Select all

LIBS = C:/altera/12.1sp1/nios2eds/bin/gnu/H-i686-mingw32/lib/gcc/nios2-elf/4.1.2/libgcc.a
I think that you need to link using gcc rather than using ld directly. When you link using gcc, then gcc will automatically add references to the c runtime library and libgcc. You can see this is you do the following example:

Code: Select all

gcc -c hello-world.c
gcc -v hello-world.o
The tail end of the output will look like this:

Code: Select all

 /usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. hello-world.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
collect2 is a variation of ld. You can also see that gcc adds -lc and -lgcc

You'll need to modify some of the other arguments when using gcc rather than using ld. Instead of -Map=build/firmware.elf.map --cref , you'll need to use -Wl,-Map=build/firmware.elf.map,--cref (-Wl tells gcc to pass arguments to the linker).

When using gcc to do the link, you shouldn't need to specify libgcc.

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

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Tue Jun 16, 2015 6:40 pm

Thanks. Yes that did it !

I now I have an fully build elf file that appears to run on the device.

My main.c has the same codebase as the one in the minimal folder,
apart from allowing this line to additionally run.
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')");

I now need to perform some more testing to verify it is functioning
(we have a serial port, so stdio should be active),
and additionally modify the makefile to also produce a micropython.a (library).

Code: Select all

$ make V=1
python ../py/makeversionhdr.py build/genhdr/mpversion.h
mkdir -p build/
mkdir -p build/lib/libc/
mkdir -p build/lib/mp-readline/
mkdir -p build/py/
mkdir -p build/py/../extmod/
mkdir -p build/stmhal/
CC main.c
nios2-elf-gcc -I. -I.. -I../lib/mp-readline -I../stmhal -Ibuild -Wall -ansi -std=gnu99 -fsingle-precision-constant  -Os -DNDEBUG -c -MD -o build/main.o main.c
LINK build/firmware.elf
nios2-elf-gcc -Wl,-Map=build/firmware.elf.map,--cref -o build/firmware.elf build/py/mpstate.o build/py/nlrx86.o build/py/nlrx64.o build/py/nlrthumb.o build/py/nlrxtensa.o build/py/nlrsetjmp.o build/py
/malloc.o build/py/gc.o build/py/qstr.o build/py/vstr.o build/py/mpprint.o build/py/unicode.o build/py/mpz.o build/py/lexer.o build/py/lexerstr.o build/py/lexerunix.o build/py/parse.o build/py/scope.o
 build/py/compile.o build/py/emitcommon.o build/py/emitcpy.o build/py/emitbc.o build/py/asmx64.o build/py/emitnx64.o build/py/asmx86.o build/py/emitnx86.o build/py/asmthumb.o build/py/emitnthumb.o bui
ld/py/emitinlinethumb.o build/py/asmarm.o build/py/emitnarm.o build/py/formatfloat.o build/py/parsenumbase.o build/py/parsenum.o build/py/emitglue.o build/py/runtime.o build/py/nativeglue.o build/py/s
tackctrl.o build/py/argcheck.o build/py/warning.o build/py/map.o build/py/obj.o build/py/objarray.o build/py/objattrtuple.o build/py/objbool.o build/py/objboundmeth.o build/py/objcell.o build/py/objcl
osure.o build/py/objcomplex.o build/py/objdict.o build/py/objenumerate.o build/py/objexcept.o build/py/objfilter.o build/py/objfloat.o build/py/objfun.o build/py/objgenerator.o build/py/objgetitemiter
.o build/py/objint.o build/py/objint_longlong.o build/py/objint_mpz.o build/py/objlist.o build/py/objmap.o build/py/objmodule.o build/py/objobject.o build/py/objproperty.o build/py/objnone.o build/py/
objnamedtuple.o build/py/objrange.o build/py/objreversed.o build/py/objset.o build/py/objsingleton.o build/py/objslice.o build/py/objstr.o build/py/objstrunicode.o build/py/objstringio.o build/py/objt
uple.o build/py/objtype.o build/py/objzip.o build/py/opmethods.o build/py/sequence.o build/py/stream.o build/py/binary.o build/py/builtinimport.o build/py/builtinevex.o build/py/modarray.o build/py/mo
dbuiltins.o build/py/modcollections.o build/py/modgc.o build/py/modio.o build/py/modmath.o build/py/modcmath.o build/py/modmicropython.o build/py/modstruct.o build/py/modsys.o build/py/vm.o build/py/b
c.o build/py/showbc.o build/py/repl.o build/py/smallint.o build/py/frozenmod.o build/py/../extmod/moductypes.o build/py/../extmod/modujson.o build/py/../extmod/modure.o build/py/../extmod/moduzlib.o b
uild/py/../extmod/moduheapq.o build/py/../extmod/moduhashlib.o build/py/../extmod/modubinascii.o build/py/../extmod/modmachine.o build/main.o build/nios2_mphal.o build/stmhal/printf.o build/stmhal/pye
xec.o build/lib/libc/string0.o build/lib/mp-readline/readline.o
nios2-elf-size build/firmware.elf
   text    data     bss     dec     hex filename
 135740    2360     464  138564   21d44 build/firmware.elf

Jonty
Posts: 2
Joined: Wed Jun 17, 2015 2:06 pm

Re: MicroPython on Altera NIOS 2

Post by Jonty » Wed Jun 17, 2015 2:31 pm

Awesome! Will it work on my Altera DE1/Nios II board?

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

Re: MicroPython on Altera NIOS 2

Post by usjcarkm » Wed Jun 17, 2015 3:35 pm

Yes it should. Any Altera NIOS 2 platform should be fine. I am using the Cyclone 3 and 5.

Once I prove a few things out, perhaps the code can be uploaded to the shared repository.

Jonty
Posts: 2
Joined: Wed Jun 17, 2015 2:06 pm

Re: MicroPython on Altera NIOS 2

Post by Jonty » Wed Jun 17, 2015 6:35 pm

Much appreciated! I look forward to trying it in due course.

Post Reply