Timer callback problem

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
stmfresser
Posts: 22
Joined: Sat Jun 20, 2015 11:48 am

Timer callback problem

Post by stmfresser » Mon Sep 12, 2016 6:10 pm

Hello,

i'm trying to toggle the leds every 1 second. i get the this error message

Code: Select all

uncaught exception in Timer(2) interrupt handler
TypeError: function takes 1 positional arguments but 2 were given

Code: Select all

import pyb
import micropython

micropython.alloc_emergency_exception_buf(200)

class A:
	def __init__(self):
		self.leds = [pyb.LED(i) for i in range(1,5)]
		tim = pyb.Timer(2, freq=1)
		tim.callback(self.ledToggle)

	def ledToggle(self):
		for led in self.leds:
			led.toggle()



if __name__ == '__main__':
	a = A()

does anyone know how to solve it?

thanks in advance

User avatar
deshipu
Posts: 1385
Joined: Thu May 28, 2015 5:54 pm

Re: Timer callback problem

Post by deshipu » Mon Sep 12, 2016 6:36 pm

I think the ledToggle callback needs to take a parameter.

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

Re: Timer callback problem

Post by dhylands » Mon Sep 12, 2016 6:38 pm

Yep - see an example here (that also uses classes):
https://github.com/dhylands/upy-example ... eat_irq.py

stmfresser
Posts: 22
Joined: Sat Jun 20, 2015 11:48 am

Re: Timer callback problem

Post by stmfresser » Mon Sep 12, 2016 7:01 pm

dhylands wrote:Yep - see an example here (that also uses classes):
https://github.com/dhylands/upy-example ... eat_irq.py
thanks.

now i get a another error message

Code: Select all

MemoryError: memory allocation failed, heap is locked 
micropython runs on a STM32F407 Discovery Board

Code: Select all

MicroPython v1.8.3-26-ga56a683 on 2016-08-16; F4DISC with STM32F407             

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

Re: Timer callback problem

Post by dhylands » Mon Sep 12, 2016 10:35 pm

You should read this: http://docs.micropython.org/en/latest/p ... =interrupt

You're not allowed to allocate memory inside an interrupt handler (and timer callbacks are interrupt handlers).
The for loop is trying to allocate an iterator object. If you rewrite it like this:

Code: Select all

import pyb
import micropython

micropython.alloc_emergency_exception_buf(200)

class A:
   def __init__(self):
      self.leds = [pyb.LED(i) for i in range(1,5)]
      tim = pyb.Timer(2, freq=1)
      tim.callback(self.ledToggle)

   def ledToggle(self, tim):
      for i in range(len(self.leds)):
         self.leds[i].toggle()

A()

stmfresser
Posts: 22
Joined: Sat Jun 20, 2015 11:48 am

Re: Timer callback problem

Post by stmfresser » Mon Sep 12, 2016 11:56 pm

dhylands wrote:You should read this: http://docs.micropython.org/en/latest/p ... =interrupt

You're not allowed to allocate memory inside an interrupt handler (and timer callbacks are interrupt handlers).
The for loop is trying to allocate an iterator object. If you rewrite it like this:

Code: Select all

import pyb
import micropython

micropython.alloc_emergency_exception_buf(200)

class A:
   def __init__(self):
      self.leds = [pyb.LED(i) for i in range(1,5)]
      tim = pyb.Timer(2, freq=1)
      tim.callback(self.ledToggle)

   def ledToggle(self, tim):
      for i in range(len(self.leds)):
         self.leds[i].toggle()

A()
Thank you!

Post Reply