[PIC32]Experiment on PIC32MX170F256B, gc.collect() crashes.

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.
Post Reply
QuanLin
Posts: 9
Joined: Tue Oct 01, 2019 1:40 am
Location: Melbourne AU

[PIC32]Experiment on PIC32MX170F256B, gc.collect() crashes.

Post by QuanLin » Thu Oct 31, 2019 2:05 am

Hi,

I'm not sure if PIC32 port is still of any interest. I did a quick experiment anyway.
The micro is PIC32MX170F256B in DIP package for easy soldering.
I made a simple hand soldered board with only 1 LED as peripheral and UART1 as REPL.

The firmware development environment is:
MPLAB X IDE v5.10
MCC v3.85.1 (it is a MPLAP X plugin for easily generating configuration code)
XC32 v2.30


I finally made it work after some work. The mpconfigport.h and main.c code is very similar to the one from pic16bit port.
Here's a brief test result with REPL:

######################
MicroPython v1.11 on 2019-10-31; EXPERIMENT with PIC32MX170F256B
>>> 256*256
65536
>>> 100//3
33
>>> a=1;b=2
>>> c=a+b
>>> c
3
>>> list(5 * x + y for x in range(10) for y in [4, 2, 1])
[4, 2, 1, 9, 7, 6, 14, 12, 11, 19, 17, 16, 24, 22, 21, 29, 27, 26, 34, 32, 31, 39, 37, 36, 44, 42, 41, 49, 47, 46]
>>> import pyb
>>> led=pyb.LED(1)
>>> led.off() #Here the LED turned off.
>>> led.on() #Here the LED turned on.
>>> import gc
>>> dir(gc)
['__name__', 'collect', 'disable', 'enable', 'isenabled', 'mem_alloc', 'mem_free', 'threshold']
>>> gc.mem_free()
13216
######################

It looks good so far. But as the test goes, when python memory is used up, the firmware crashes and REPL does not respond anymore.
I suspect this problem is related to gc. So I did this straight away:

######################
MicroPython v1.11 on 2019-10-31; EXPERIMENT with PIC32MX170F256B
>>> import gc
>>> gc.mem_free()
15808
>>> gc.collect()
######################

And it crashed straight away.

And then I found the gc.collect() implementation code in main.c:
######################
void gc_collect(void) {
// TODO possibly need to trace registers
void *dummy;
gc_collect_start();
// Node: stack is ascending
gc_collect_root(&dummy, ((mp_uint_t)&dummy - (mp_uint_t)MP_STATE_THREAD(stack_top)) / sizeof(mp_uint_t));
gc_collect_end();
}
######################

As the name "dummy" suggests, this function is not fully implemented yet.
Other ports show some asm implementation on get_sp() which is quite specific for the port.

My questions:
1, Can any one shed some light on how to implement get_sp() generally?
2, Dose the note "stack is ascending" still apply to PIC32? (This code is copied from pic16bit port)
3, Another "dummy" named variable found in main.c, what should I do with stack_dummy?:
######################
// init MicroPython runtime
int stack_dummy;
MP_STATE_THREAD(stack_top) = (char*)&stack_dummy;
gc_init(heap, heap + sizeof(heap));
mp_init();
mp_hal_init();
######################

Many thanks!!

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: [PIC32]Experiment on PIC32MX170F256B, gc.collect() crashes.

Post by stijn » Thu Oct 31, 2019 11:52 am

As the name "dummy" suggests, this function is not fully implemented yet.
Can't answer your other questions, but I think this is not what 'dummy' means here: the function is implemented, it's just a variable which is at the top of the stack (well, or bottom, depending on how you look at it) when this function is called and could have any name.

uCTRL
Posts: 47
Joined: Fri Oct 12, 2018 11:50 pm

Re: [PIC32]Experiment on PIC32MX170F256B, gc.collect() crashes.

Post by uCTRL » Thu Oct 31, 2019 9:47 pm

I'm not sure if PIC32 port is still of any interest
I have been using PIC32MX and would be interested to see how it performs under micropython.

Are you interested in making your port public and published on Github?

QuanLin
Posts: 9
Joined: Tue Oct 01, 2019 1:40 am
Location: Melbourne AU

Re: [PIC32]Experiment on PIC32MX170F256B, gc.collect() crashes.

Post by QuanLin » Thu Oct 31, 2019 11:35 pm

Hi @stijn,

Thank you for the explanation.
I've figured it out. Now gc.collect() works fine.

To answer these questions to myself or others who are interested:
1, To read out stack pointer, must use some asm code, but can be very simple.
2, PIC32 stack is descending.
3, stack_dummy in main.c does not matter. Just leave it as it is.

QuanLin
Posts: 9
Joined: Tue Oct 01, 2019 1:40 am
Location: Melbourne AU

Re: [PIC32]Experiment on PIC32MX170F256B, gc.collect() crashes.

Post by QuanLin » Thu Oct 31, 2019 11:41 pm

uCTRL wrote:
Thu Oct 31, 2019 9:47 pm
I'm not sure if PIC32 port is still of any interest
I have been using PIC32MX and would be interested to see how it performs under micropython.

Are you interested in making your port public and published on Github?
Hi uCTRL,

I will try to upload it to github and do a pull request for pic32 port.
There is still something to be cleaned first. And micropython port doesn't use MPLAB X, so some work is needed to make it compile that way.

Post Reply