How fast can an output pin be toggled?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
dhylands
Posts: 3482
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How fast can an output pin be toggled?

Post by dhylands » Tue May 27, 2014 5:32 am

fma wrote:Is it a DMA limitation, or only on the current implementation?
Well, with the DMA solution you're DMA'ing pulse widths. So this is one pulse width per bit being sent to the LED. With DMA you essentially need to take your 24-bit value for each LED and expand it into a 24-byte array. I'm pretty sure that the extra 42 bytes is for the 50 usec reset pulse.

You should be able to setup a circular buffer with the DMA, perhaps having 2 x 24 byte buffers (so 2 buffers of 30 usec each) or some multiple thereof, and you fill one buffer, and while the DMA is sending it to the timer, you populate the next buffer. This is essentially how audio playback works.

mpymike
Posts: 10
Joined: Mon May 26, 2014 5:48 am

Re: How fast can an output pin be toggled?

Post by mpymike » Tue May 27, 2014 2:51 pm

I think I see what you are suggesting.
I maybe missing something but I don't see what the advantage is setting
up the DMA to pump out the data like that.
Its not like you've offloaded the cpu so it could do something else while the
transfer is happening.

Implementing a bitbang function in assembler has got to be more straight forward.
That is how the Arduino Adafruit_NeoPixel library does it.

-mike

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: How fast can an output pin be toggled?

Post by blmorris » Tue May 27, 2014 4:01 pm

Its not like you've offloaded the cpu so it could do something else while the
transfer is happening.
I might be obtuse here - it would be neither the first nor the last time - but isn't that exactly the point of DMA? Basically you load up the buffer with the data that you want to transfer and then the CPU can do something else while it gets sent out; for example, you can compute the values for the next pixel and load them into a second buffer while data in the first is being transferred out, and go back to refill the first while the second is transferring data. Ping-Pong buffer, if I remember correctly. I wouldn't know exactly how to write the code, but it seems like Micro Python on an STM32F405 should be able to do this and still have plenty of processing power left over for other stuff as well.

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

Re: How fast can an output pin be toggled?

Post by dhylands » Tue May 27, 2014 5:22 pm

mpymike wrote:I think I see what you are suggesting.
I maybe missing something but I don't see what the advantage is setting
up the DMA to pump out the data like that.
Its not like you've offloaded the cpu so it could do something else while the
transfer is happening.

Implementing a bitbang function in assembler has got to be more straight forward.
That is how the Arduino Adafruit_NeoPixel library does it.

-mike
The Arduino does it in assembler because you have no choice there. The processor is only running at 16 MHz, and generating the pulses at the required widths requires hand-coded and tuned assembly.

The disadvantage of the bit-banged assembler approach, is that you have to disable interrupts in order for it to work.

If you can fill the buffers and have enough spare CPU cycles then you can leave interrupts enabled while the DMA is occuring.

It may be the case, that disabling interrupts for isn't a problem, it really depends on what else you're doing on the system and how long you need to disable interrupts for. If you're talking about disabling interrupts for less than a millisecond, then that's probably fine. If its more than a millisecond, then you'll need to decide how important the millisecond timer is to the rest of your code.

User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Re: How fast can an output pin be toggled?

Post by Markus Gritsch » Wed May 28, 2014 6:37 am

dhylands wrote:The Arduino does it in assembler because you have no choice there. The processor is only running at 16 MHz, and generating the pulses at the required widths requires hand-coded and tuned assembly.
The timing requirements for the WS2812 LEDs seem to be not as strict as the datasheet says. Here is an extensive investigation, with relaxed Arduino code in C:

http://wp.josh.com/2014/05/13/ws2812-ne ... know-them/

Cheers,
Markus

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: How fast can an output pin be toggled?

Post by fma » Wed May 28, 2014 7:07 am

Looks interesting.

I was wondering: is it possible to use a timer which will call a function, and in the function, change the timer frequency according to the led datas we need to send? The callback first toggles the output bit, then reads the new duration (from leds data we want to send), changes the timer, and exits...

Does it make sens?
Frédéric

User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Re: How fast can an output pin be toggled?

Post by Markus Gritsch » Thu Jun 05, 2014 1:29 pm

Espruino has an example which uses these kind of LEDs:
http://www.espruino.com/Individually+Addressable+LEDs

They do it by using SPI @ 3200000 baud and sending 0b0001 and 0b0011 bit patterns.

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: How fast can an output pin be toggled?

Post by fma » Thu Jun 05, 2014 1:41 pm

Nice! But we lack the SPI.send4bit() method...
Frédéric

User avatar
Markus Gritsch
Posts: 41
Joined: Fri May 16, 2014 9:04 pm

Re: How fast can an output pin be toggled?

Post by Markus Gritsch » Thu Jun 05, 2014 1:46 pm

The example wasn't meant to be used verbatimely -- just to show that using SPI at this baud rate with the given bit patterns can do the job.

fma
Posts: 163
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: How fast can an output pin be toggled?

Post by fma » Thu Jun 05, 2014 1:51 pm

In fact, all it does is reducing the calling frequency to the send() method (by a factor 4). I don't know if it would be enough from micropython...

Nobody with STM32 C programming skills to write a dedicated driver for these leds? ;)
Frédéric

Post Reply