Understanding Fatal Error with respect to STM32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
lnsri22
Posts: 66
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Understanding Fatal Error with respect to STM32

Post by lnsri22 » Sat Jan 19, 2019 6:45 am

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!!
lnsri22 :)

Maksym Galemin
Posts: 10
Joined: Mon May 28, 2018 11:48 pm

Re: Understanding Fatal Error with respect to STM32

Post by Maksym Galemin » Tue Jan 22, 2019 12:14 am

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)?

lnsri22
Posts: 66
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding Fatal Error with respect to STM32

Post by lnsri22 » Tue Jan 22, 2019 4:50 am

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!!
lnsri22 :)

Maksym Galemin
Posts: 10
Joined: Mon May 28, 2018 11:48 pm

Re: Understanding Fatal Error with respect to STM32

Post by Maksym Galemin » Tue Jan 22, 2019 8:43 am

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).

lnsri22
Posts: 66
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding Fatal Error with respect to STM32

Post by lnsri22 » Tue Jan 22, 2019 9:18 am

Thanks again!!

Let me give a quick try to capture this.

I will update the result once this error occur!!
lnsri22 :)

lnsri22
Posts: 66
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding Fatal Error with respect to STM32

Post by lnsri22 » Sat Feb 09, 2019 11:46 am

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!!
lnsri22 :)

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

Re: Understanding Fatal Error with respect to STM32

Post by dhylands » Sat Feb 09, 2019 4:38 pm

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

lnsri22
Posts: 66
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding Fatal Error with respect to STM32

Post by lnsri22 » Sun Feb 10, 2019 7:04 am

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:
lnsri22 :)

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

Re: Understanding Fatal Error with respect to STM32

Post by dhylands » Sun Feb 10, 2019 4:26 pm

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.

lnsri22
Posts: 66
Joined: Fri Aug 17, 2018 12:16 pm
Location: India

Re: Understanding Fatal Error with respect to STM32

Post by lnsri22 » Sat Feb 16, 2019 4:55 am

Dave,

I am about to try this.

Will update you once I'm done with this

Thanks again!!
lnsri22 :)

Post Reply