Page 1 of 2

Can I use the can.recv(0) in the rxcallback function?

Posted: Thu May 26, 2016 6:58 am
by wwsheldons
when I use the can.recv(0) in the rxcallback function, it will point that ' uncaught exception in CAN(2) rx interrupt handler MemoryError:' . why?

Code: Select all

    def rec0(self,bus, reason):
        print('rec0')
        if reason == 0:
            print('pending')
            #print(bus)
            #print(self.can)
            #help(bus)
            tmp = bus.recv(self.fifo)
            print(tmp[3])
    def send_dat(self,dat):
        host_can_id = 0x7ff
        for i in range(len(dat)):
            self.can.send(dat[i], host_can_id)
        return 1
Please use the code formatting tool when posting messages! - platforma

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Fri May 27, 2016 11:19 am
by PappaPeppar
The reason is that you are allocating memory in the callback.

Code: Select all

tmp = bus.recv(self.fifo)
The callback is executing in interrupt mode, and in that mode memory it is not allowed to allocate memory. There has been some discussions about how to solve this. One way is to add soft interrupts like in this PR https://github.com/micropython/micropython/pull/2081. There is a also a initiative to allow memory allocations when in interrupt mode (in the treading branch), so I don't know if the soft interrupts will be merged.

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Mon Jun 06, 2016 2:28 am
by wwsheldons
PappaPeppar wrote:The reason is that you are allocating memory in the callback.

Code: Select all

tmp = bus.recv(self.fifo)
The callback is executing in interrupt mode, and in that mode memory it is not allowed to allocate memory. There has been some discussions about how to solve this. One way is to add soft interrupts like in this PR https://github.com/micropython/micropython/pull/2081. There is a also a initiative to allow memory allocations when in interrupt mode (in the treading branch), so I don't know if the soft interrupts will be merged.

My micropython has no modelue "machine.setsoftirq" ,why?
the version is 1.7 pybV3

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Mon Jun 06, 2016 5:54 am
by folke
Hi,

If you haven't done so already, I suggest you study http://docs.micropython.org/en/latest/p ... rules.html to see if that can help you get things working.

I think soft irq will come, but work on it is not finished yet.

Cheers!

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Thu Jun 09, 2016 8:32 pm
by PappaPeppar
folke is right, the soft irq feature does only exist in my repository https://github.com/HenrikSolver/micropy ... e/softirq2 so far.

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Fri Jun 10, 2016 8:14 am
by wwsheldons
PappaPeppar wrote:folke is right, the soft irq feature does only exist in my repository https://github.com/HenrikSolver/micropy ... e/softirq2 so far.

thanks very much

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Fri Jun 10, 2016 8:50 am
by wwsheldons
PappaPeppar wrote:folke is right, the soft irq feature does only exist in my repository https://github.com/HenrikSolver/micropy ... e/softirq2 so far.

I rebuild the source from micropython-softirq2 by using "make BOARD=PYBV3", then the modoule of machine has no the attribute 'setsoftirq'. why?

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Fri Jun 10, 2016 2:30 pm
by PappaPeppar
Add the line

Code: Select all

#define MICROPY_PY_SOFTIRQ          (1)
To boards/PYBV3/mpconfigboard.h

I only have V10 boards, so thats the only one I have tried it on.

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Sat Jun 11, 2016 1:11 am
by wwsheldons
PappaPeppar wrote:Add the line

Code: Select all

#define MICROPY_PY_SOFTIRQ          (1)
To boards/PYBV3/mpconfigboard.h

I only have V10 boards, so thats the only one I have tried it on.

thank you , it works well . Could I use the function setsoftirq on the Timer callback?

Re: Can I use the can.recv(0) in the rxcallback function?

Posted: Sat Jun 11, 2016 5:26 am
by PappaPeppar
Yes it should be possible to use it in all interrupt functions. The intention is that all callbacks will have the option to be soft. Next in queue is extint. If you find any bugs or comes up with any possible improvments while using this, please report them here or even better in the PR at github.