Jump to content
43oh

slow digitalWrite/Read


Recommended Posts

Hi,

 

I don't know if it's a know problem but the digitalWrite and digitalRead functions are quite slow.

I'm using a nRF24L01+ in my project with a software SPI (so digitalWrite and digitalRead are used a lot) an and the whole function that fetch ADC values and send them to the radio takes 46.5ms using digitalWrite functions and only 15.9ms by accessing directly to the registers (like P1OUT |= BIT1 or P1IN |= BIT0)).

 

Is there any solution to speed up these functions?

Link to post
Share on other sites

Yes I can use it and I'm implementing it but software SPI is a very good example to show the slowness of the digitalWrite/Read functions. I made a little sketch to toggle a pin as fast as possible and I achieve a toggle rate of about 426kHz with digitalWrite and about 2.8MHz with direct access to registers.

Link to post
Share on other sites
Yes I can use it and I'm implementing it but software SPI is a very good example to show the slowness of the digitalWrite/Read functions. I made a little sketch to toggle a pin as fast as possible and I achieve a toggle rate of about 426kHz with digitalWrite and about 2.8MHz with direct access to registers.

 

I'm surprised you were able to toggle the pins that fast with direct register manipulation. That code would be interesting to see. The best code I've seen for toggling in a loop takes 10-12 cycles so that works out to be 16MHz/10 .. so more like 1.6MHz if you are lucky and no interrupts are running. With Energia in addition to the digitalRead/Write being slow, there may be Timer and Watchdog ISR handlers running in the background stealing other cycles.

 

-rick

Link to post
Share on other sites

I'm also surprised that you are able to toggle the ports that fast.

 

You can get even more speed than writing to the registers directly in C if you write in assembly language. Lower level will give you better performance and the opposite is also true. Energia is perfect for a lot of projects but if you need efficiency and performance you will still need to know C or assembly.

 

I just recently threw together a program to drain our evaporative cooler twice a day using a relay, an extra pump, CDs cell and 2231. Energia made it super easy to just throw together code to run about 45 seconds of drain pump at sunrise and sunset that CCS would have taken more time for. Obviously more advanced stuff can be done but I spent maybe 5 mins and had the code uploaded and done.

Link to post
Share on other sites

Yes of course Energia (as Arduino, Maple and maybe other stuff like this) is great for doing something quickly without the need of opening a datasheet but I was wondering if it's possible to enhance some parts of the library like digitalWrite, not for my application but for everybody. Even if 95% of people doesn't care of the toggling rate with Energia's embedded functions, it's still a good thing to improve. If you take a battery powered application that uses of the low power modes, it's very important to stay as short as possible in the run mode to save your battery.

Link to post
Share on other sites
... but I was wondering if it's possible to enhance some parts of the library like digitalWrite, not for my application but for everybody. ...

 

You might be interested in the discussion I started a while back about faster GPIO:

 

http://www.43oh.com/forum/viewtopic.php?f=5&t=2141

 

To be honest, I never really finished that approach as the topic didn't seem to garner much external interest.

 

I had thought about adding this to Energia, however for the general case it is probably better for Energia to have the same comparable speed as the Arduino digitalRead/Write functions. This gives existing code a better chance of working as the time taken by the functions will be similar. However, for a new project you might take an optimized approach.

 

The key to getting the most out of the msp430 is to take advantage of its peripherials. Bit banging should be the last option and using its hardware peripherals and sleeping while they are executing is the most power efficient way to go.

 

-rick

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...