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

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
wwsheldons
Posts: 31
Joined: Wed Dec 02, 2015 1:47 pm

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

Post by wwsheldons » Thu May 26, 2016 6:58 am

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

PappaPeppar
Posts: 30
Joined: Thu Dec 18, 2014 10:38 pm

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

Post by PappaPeppar » Fri May 27, 2016 11:19 am

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.

wwsheldons
Posts: 31
Joined: Wed Dec 02, 2015 1:47 pm

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

Post by wwsheldons » Mon Jun 06, 2016 2:28 am

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

folke
Posts: 8
Joined: Thu Jun 04, 2015 3:26 pm

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

Post by folke » Mon Jun 06, 2016 5:54 am

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!

PappaPeppar
Posts: 30
Joined: Thu Dec 18, 2014 10:38 pm

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

Post by PappaPeppar » Thu Jun 09, 2016 8:32 pm

folke is right, the soft irq feature does only exist in my repository https://github.com/HenrikSolver/micropy ... e/softirq2 so far.

wwsheldons
Posts: 31
Joined: Wed Dec 02, 2015 1:47 pm

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

Post by wwsheldons » Fri Jun 10, 2016 8:14 am

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

wwsheldons
Posts: 31
Joined: Wed Dec 02, 2015 1:47 pm

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

Post by wwsheldons » Fri Jun 10, 2016 8:50 am

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?

PappaPeppar
Posts: 30
Joined: Thu Dec 18, 2014 10:38 pm

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

Post by PappaPeppar » Fri Jun 10, 2016 2:30 pm

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.

wwsheldons
Posts: 31
Joined: Wed Dec 02, 2015 1:47 pm

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

Post by wwsheldons » Sat Jun 11, 2016 1:11 am

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?

PappaPeppar
Posts: 30
Joined: Thu Dec 18, 2014 10:38 pm

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

Post by PappaPeppar » Sat Jun 11, 2016 5:26 am

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.

Post Reply