Page 1 of 1

Understanding Fatal Error with respect to STM32

Posted: Sat Jan 19, 2019 6:45 am
by lnsri22
Hello Everyone!!

I have got running micropython in my custom hw based on stm32.(As of now using pyb module completely)

I have implemented uasyncio to run some coros so as to get data from different sensors at different time frames, amongst sending data to server.

What happens is that the controller runs for few hours and after some time it encounters FATAL ERROR with most of the time the issue being memory allocation. What am I missing ?

Thanks in advance!!

Re: Understanding Fatal Error with respect to STM32

Posted: Tue Jan 22, 2019 12:14 am
by Maksym Galemin
Any particular fatal errors like MemManage or HardFault?
Have you tried to set a breakpoint @ __fatal_error() function in ports/stm32/main.c (or in your HW STM32 port)?

Re: Understanding Fatal Error with respect to STM32

Posted: Tue Jan 22, 2019 4:50 am
by lnsri22
Hi Maksym,

Thanks for reply there!!

I'm not sure about the particularity of the error.

But all I could see is that the LED (on-board) toggle in a specific pattern(I have made the custom hardware have only one LED for indicating CPU heart-beat( which is actually RED Led in pyboard I mean the hardware mapping)

Ever since this error is encountered by the hardware, my perception is that the pattern might be similar to that of this
https://github.com/micropython/micropyt ... main.c#L88

I will put a breakpoint as you said and observe the behaviour. Anyhow I'm not sure about when this would happen. It happens in a random manner. (At times in a couple of days, and sometimes may be in 6 or 7 hours)

Thanks!!

Re: Understanding Fatal Error with respect to STM32

Posted: Tue Jan 22, 2019 8:43 am
by Maksym Galemin
I would start with capturing your debug UART port output defined @ mp_hal_stdout_tx_strn() and looking for "\nFATAL ERROR:\n" strings in order to find out the actual error (as a parameter to __fatal_error() function).

Re: Understanding Fatal Error with respect to STM32

Posted: Tue Jan 22, 2019 9:18 am
by lnsri22
Thanks again!!

Let me give a quick try to capture this.

I will update the result once this error occur!!

Re: Understanding Fatal Error with respect to STM32

Posted: Sat Feb 09, 2019 11:46 am
by lnsri22
Hi Maksym!!

Thought of giving it a try and finally got the output

It is

Code: Select all

MemoryError: memory allocation failed, allocating 31655 bytes
MicroPython 8dc4982-dirty on 2019-02-09; MySTM with STM32F405VG
Type "help()" for more information.
This happens at random time intervals after restart.

What am I missing?

Thanks in advance!!

Re: Understanding Fatal Error with respect to STM32

Posted: Sat Feb 09, 2019 4:38 pm
by dhylands
I'd guess that you have an ISR which is trying to allocate a buffer.

Any code which uses ISRs should allocate an emergency exception buffer as described here:
http://docs.micropython.org/en/latest/r ... ion-buffer

then you should get some more detail (i.e. file and line number) about the exception

Re: Understanding Fatal Error with respect to STM32

Posted: Sun Feb 10, 2019 7:04 am
by lnsri22
Thanks Dave for the quick reply!! :)

Let me look into it. But Dave, How do i get the info on file and line number ?? :roll:

Re: Understanding Fatal Error with respect to STM32

Posted: Sun Feb 10, 2019 4:26 pm
by dhylands
I'm assuming that you've got some code running from main.py and this code is what's causing the failure. Are there any other messages before this? Allocating 31655 is a rather large buffer and needs to done properlly to ensure success. For a buffer that large, you want to allocate it as early as possible in your program and you don't want to free it. You want to reuse it. The chances of allocating another buffer that large a second time after your program is running for a while is extremely slim due to heap fragmentation.

I think your error might be something slightly different than what I was proposing in my previous message. This is the example I tried:

Code: Select all

import pyb
#import micropython
#micropython.alloc_emergency_exception_buf(100)

def heartbeat_cb(tim):
    global tick
    if tick <= 3:
        led.toggle()
        x = []
    tick = (tick + 1) % 10

tick = 0
led = pyb.LED(4) # 4 = Blue
tim = pyb.Timer(4)
tim.init(freq=10)
tim.callback(heartbeat_cb)
If I run that then I get this error:

Code: Select all

>>> import memfail
>>> uncaught exception in Timer(4) interrupt handler
MemoryError: 
If I then uncomment the 2nd and 3rd lines and re-run, I get this error instead:

Code: Select all

>>> import memfail
>>> uncaught exception in Timer(4) interrupt handler
Traceback (most recent call last):
  File "memfail.py", line 9, in heartbeat_cb
MemoryError: memory allocation failed, heap is locked
which has the line number and filename information. Your error would seem to indicate actually running out of memory.

Re: Understanding Fatal Error with respect to STM32

Posted: Sat Feb 16, 2019 4:55 am
by lnsri22
Dave,

I am about to try this.

Will update you once I'm done with this

Thanks again!!