amount of timer instances

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
spoetnic
Posts: 6
Joined: Sun Dec 08, 2019 12:08 pm

amount of timer instances

Post by spoetnic » Sun Feb 09, 2020 3:09 pm

Hello,

I'm trying to understand the internal functioning of the micro:bit a little better.
Therefor I made a program which uses an amount of timers to blink the LEDS at different interval rates.
I read on the internet, the chip has a number of hardware timers.
My program runs well up to the delay of 600. If I add more objects (from pause 650 and up), the program isn't executed at all anymore after uploading.
Can someone explain why it isn't executed anymore above this amount?
Are there 12 timers available on this chip, or is there more to it?

Thanks, Marc


basic.forever(function () {
basic.pause(50)
led.plot(0, 0)
basic.pause(50)
led.unplot(0, 0)
})
basic.forever(function () {
basic.pause(100)
led.plot(1, 0)
basic.pause(100)
led.unplot(1, 0)
})
basic.forever(function () {
basic.pause(150)
led.plot(2, 0)
basic.pause(150)
led.unplot(2, 0)
})
basic.forever(function () {
basic.pause(200)
led.plot(3, 0)
basic.pause(200)
led.unplot(3, 0)
})
basic.forever(function () {
basic.pause(250)
led.plot(4, 0)
basic.pause(250)
led.unplot(4, 0)
})
basic.forever(function () {
basic.pause(300)
led.plot(4, 1)
basic.pause(300)
led.unplot(4, 1)
})
basic.forever(function () {
basic.pause(350)
led.plot(4, 2)
basic.pause(350)
led.unplot(4, 2)
})
basic.forever(function () {
basic.pause(400)
led.plot(4, 3)
basic.pause(400)
led.unplot(4, 3)
})
basic.forever(function () {
basic.pause(450)
led.plot(4, 4)
basic.pause(450)
led.unplot(4, 4)
})
basic.forever(function () {
basic.pause(500)
led.plot(3, 4)
basic.pause(500)
led.unplot(3, 4)
})
basic.forever(function () {
basic.pause(550)
led.plot(2, 4)
basic.pause(550)
led.unplot(2, 4)
})
basic.forever(function () {
basic.pause(600)
led.plot(2, 4)
basic.pause(600)
led.unplot(2, 4)
})
basic.forever(function () {
basic.pause(650)
led.plot(1, 4)
basic.pause(650)
led.unplot(1, 4)
})
basic.forever(function () {
basic.pause(700)
led.plot(0, 4)
basic.pause(700)
led.unplot(0, 4)
})
basic.forever(function () {
basic.pause(750)
led.plot(0, 3)
basic.pause(750)
led.unplot(0, 3)
})
basic.forever(function () {
basic.pause(800)
led.plot(0, 2)
basic.pause(800)
led.unplot(0, 2)
})
basic.forever(function () {
basic.pause(850)
led.plot(0, 1)
basic.pause(850)
led.unplot(0, 1)
})
basic.forever(function () {
basic.pause(900)
led.plot(1, 1)
basic.pause(900)
led.unplot(1, 1)
})
basic.forever(function () {
basic.pause(950)
led.plot(2, 1)
basic.pause(950)
led.unplot(2, 1)
})
basic.forever(function () {
basic.pause(1000)
led.plot(3, 1)
basic.pause(1000)
led.unplot(3, 1)
})
basic.forever(function () {
basic.pause(1500)
led.plot(3, 2)
basic.pause(1500)
led.unplot(3, 2)
})
basic.forever(function () {
basic.pause(2000)
led.plot(3, 3)
basic.pause(2000)
led.unplot(3, 3)
})
basic.forever(function () {
basic.pause(2500)
led.plot(2, 3)
basic.pause(2500)
led.unplot(2, 3)
})
basic.forever(function () {
basic.pause(3000)
led.plot(1, 3)
basic.pause(3000)
led.unplot(1, 3)
})
basic.forever(function () {
basic.pause(3500)
led.plot(1, 2)
basic.pause(3500)
led.unplot(1, 2)
})
basic.forever(function () {
basic.pause(4000)
led.plot(2, 2)
basic.pause(4000)
led.unplot(2, 2)
})

User avatar
jimmo
Posts: 1139
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: amount of timer instances

Post by jimmo » Wed Feb 12, 2020 3:29 am

spoetnic wrote:
Sun Feb 09, 2020 3:09 pm
I'm trying to understand the internal functioning of the micro:bit a little better.
Hi Marc,

It doesn't look like you're using MicroPython -- the code snippet you posted is JavaScript.

You can use MicroPython on the micro:bit though. I recommend downloading https://codewith.mu/ which has everything you need including an editor and a way to flash code.

From a hardware perspective though, there aren't 12 timers, rather they tend to all run off a single timer (e.g. SysTick).

spoetnic
Posts: 6
Joined: Sun Dec 08, 2019 12:08 pm

Re: amount of timer instances

Post by spoetnic » Wed Feb 12, 2020 12:56 pm

Hello, thanks for your reply!
Indeed this code was made in the online micro:bit app.
Mu also looks nice for micro:bit.

From a hardware perspective there are 12 timers on the micro:bits processor right?
But again I like to know why it stops working with a certain amount of timing instances.
Obviously there is no warning given.

What is the maximum amount of instances that can perform seperate timing tasks?

User avatar
jimmo
Posts: 1139
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: amount of timer instances

Post by jimmo » Wed Feb 12, 2020 9:35 pm

spoetnic wrote:
Wed Feb 12, 2020 12:56 pm
From a hardware perspective there are 12 timers on the micro:bits processor right?
It's likely that whatever high-level language will multiplex many virtual timers on top of the small number of hardware ones (such as SysTick and the other SoC ones. Foe example the NRF51822 has 2 x 16 bit, 1 x 24bit, 2 x 24bit, 1x RTC). (Note these timers may have multiple channels so it's possible they do really use entirely the hardware timers, but that seems more complicated than it needs to be).
spoetnic wrote:
Wed Feb 12, 2020 12:56 pm
What is the maximum amount of instances that can perform seperate timing tasks?
Unfortunately I don't know why 12 is the limit for the JavaScript language you're using. (But this forum is for MicroPython -- happy to help with micro:bit stuff, but generally we only know how to help with MicroPython on the micro:bit) My guess is that they have a static list of slots or something, and it's been hardcoded to 12.

If you want to work around this (and you'd do the same in MicroPython) is to implement the same idea with a single timer. i.e. if i wanted 50ms, 75ms and 125ms timers, I'd make a single 25 ms timer and then call the individual handlers every 2, 3 and 5 times.

spoetnic
Posts: 6
Joined: Sun Dec 08, 2019 12:08 pm

Re: amount of timer instances

Post by spoetnic » Thu Feb 13, 2020 7:44 am

Thanks Jimmo, for your reply.
Actually I was using the micro:bit in order to get started with micropyton. I already have a pyboard and am trying to find a one-in-all IDE with debugger and uploader. So far I am trying with MS CODE, but can't upload code yet.

About the pyboard, based on an STM32F405 it has twelve general-purpose 16-bit timers.
Programming in a higher level language on a micro makes me wonder how it would deal with many timer instances with non-multiply-able timings?

In this case, my question is how far can we go in here? Also if you have a good IDE you recommend.

Thanks,

User avatar
jimmo
Posts: 1139
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: amount of timer instances

Post by jimmo » Thu Feb 13, 2020 8:11 am

spoetnic wrote:
Thu Feb 13, 2020 7:44 am
Actually I was using the micro:bit in order to get started with micropyton.
It doesn't sound like you're actually using MicroPython though? Take a look at https://codewith.mu/ as an all-in-one IDE for micro:bit development.

Something else to note is that the MicroPython variant that runs on the micro:bit is very cut down (even compared to pyboard). So there are no timers. Instead you do something like:

Code: Select all

from microbit import *
while True:
  if running_time() % 12 == 0:
    # this happens every 12 ms
  if running_time() % 37 == 0:
    # this happens every 37 ms
  .. etc
spoetnic wrote:
Thu Feb 13, 2020 7:44 am
I already have a pyboard and am trying to find a one-in-all IDE with debugger and uploader.
There is no debugger for MicroPython (to me a debugger is something like pdb or gdb, i.e. line by line debugging).

For micro:bit, I highly recommend https://codewith.mu/

For everything else, Thonny and uPyCraft seem to be the most common options.

But the whole point is that you don't need an IDE -- on the pyboard you can just copy the files to the USB drive. And use whatever serial console you're most comfortable with (putty, miniterm, picoterm, screen, etc).
Also worth investigating is pyboard.py (see the MicroPython docs) and rshell.

I've heard there's a plugin for vs.code (I assume that's what you mean by MS CODE?).
spoetnic wrote:
Thu Feb 13, 2020 7:44 am
About the pyboard, based on an STM32F405 it has twelve general-purpose 16-bit timers.
You can access these individually using pyb.Timer and pyb.TimerChannel

If you need more timers then you need to find out a common interval and share the timers as described (there's always a common interval).

spoetnic
Posts: 6
Joined: Sun Dec 08, 2019 12:08 pm

Re: amount of timer instances

Post by spoetnic » Thu Feb 13, 2020 8:49 am

Ok thanks. No I was using microbit to learn some about python.


Now I'm trying out micropython with the pyboard and am using VS CODE as an all-in-one IDE.
I've got some of the required plugins but can't run the code on the board.
Assuming VS code will also upload the code when pressing RUN???
Now its complaining about: No module named 'pyb'
Do you know why?


Here is the text from the console:

PS C:\Users\Marc Frim> & "C:/Users/Marc Friedheim/AppData/Local/Programs/Python/Python38-32/python.exe" "c:/Users/Marc Frim/.vscode/extensions/rt-thread.rt-thread-micropython-1.0.7/microExamples/examples/03.board/5.pyboard/blink.py"
Traceback (most recent call last):
File "c:/Users/Marc Frim/.vscode/extensions/rt-thread.rt-thread-micropython-1.0.7/microExamples/examples/03.board/5.pyboard/blink.py", line 2, in <module>
import pyb
ModuleNotFoundError: No module named 'pyb'
PS C:\Users\Marc Frim>

User avatar
jimmo
Posts: 1139
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: amount of timer instances

Post by jimmo » Sat Feb 15, 2020 12:06 pm

spoetnic wrote:
Thu Feb 13, 2020 8:49 am
Assuming VS code will also upload the code when pressing RUN???
Now its complaining about: No module named 'pyb'
Do you know why?
Based on the error below, it sounds like it's running your code with regular Python (i.e. CPython) and not on the actual device.

Post Reply