Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Adapting the fastgpio library


I am working on a project to make electronic toys to hand out for Halloween. I already have a PCB made for it, and it uses an ATTiny841 MCU & APA102 leds. I am using the Pololu APA102 library (https://github.com/pololu/apa102-arduino) and it works great. I need it to be fast as I am using other functions including touch sensing and sound output. Fastgpio doesn’t compile for the ATTiny841 (only ATMega328), but I see on an Uno that a test sketch runs faster and compiles smaller when using fastgpio.

I would like to adapt the fastgpio library to use with the ATTiny841. I am not a professional programmer, but I have made a few things with Arduino as a hobby and adapted some other libraries successfully.

Could you possibly tell me what I would have to do to adapt this library to the ATTINY841? It seems fairly abstract. At least tell me some kind of overview guidance for what would be needed to be done?


Hello. It looks like the ATTiny841 supports all the AVR assembly instructions we use in the FastGPIO library, so adding support for it should not be too hard. You would need to edit FastGPIO.h. Near the top of the library, around line 89, there is a chain of #if and #elif statements that detects what type of AVR you are compiling for and defines a table named pinStructs which tells the library what ports and bit numbers to use for each pin number that the user is allowed to specify. You would need to add a new #elif statement that detects the ATTiny841 and defines your own pinStructs table. The table does not need entries for all the pins on the AVR, but would at least need entries for the specific pins you are using.

Alternatively, might consider just editing the APA102 library. It has code that manipulates I/O pins by calling Arduino functions or FastGPIO functions. You would replace that code with code that directly manipulates the AVR I/O registers for the specific pins you are using in your application. For example, if the APA102 clock pin is connected to PA2, you would replace



PORTA |= (1 << 2);

After doing that, you could remove the template parameters (clockPin and dataPin) since they would not be used.

Please let me know if you have any further questions.


Thanks for the support. I was able to modify the library and it runs well. One thing I noticed is the pull ups are controlled differently with the 841 and might be different for other AVRs as well. Writing the port high or low has no effect on the pullup when the DDR is low. There is a PUE register that controls the pullups instead. Luckily I’m not using any pullups in the sketch yet.

Thanks for the help.

1 Like