I was recently compiling some Arduino code with NewSoftSerial and got hit by infamous “Error: register r24, r26, r28 or r30 required” error. NewSoftSerial is a great library and those using older avr-gcc compiler enjoy it alot. Problem is the newer gcc doesn’t like one nasty error in the code and here is how to fix it …
The problem arises in method NewSoftSerial::tunedDelay written in assembly language. If you don’t have much experience with asm this method looks somewhat cryptic. It basically consists of single call “asm volatile” that gets one to four parameters separated by semicolomn. The first parameter is the assembly code itself. It can include multiple references to the other three parts in the form of %id. The second and third parameters are the input and output params for the asm code. Let’s skip the fourth part as it is not used in this faulty tunedDelay method.
Let’s take a look at the second parameter of the asm call:
: "+r" (delay), "+a" (tmp)
The first magical “+r” (delay) means that all %0 (i.e. parameter #0) in the asm code will be replaced with the “delay” variable from the C code. “+” means it is read-write parameter. “r” is a “constraint class” – the cause of NewSoftSerial inability to compile. The problem is that asm code is about to make certain operations with the delay and those operations require only particular registers to be used. The set of registers allowed for an operation is defined by the constraint class. If you look at the asm code you will see operation “sbiw %0, 0×01″. According to the documentation this operation requires first argument of constraint class “w” while the NewSoftSerial code specifies class “r”. It looks like the old compilers were not smart enough to figure out this problem, while newer avr-gccs are more careful.
So, in order to fix the code and make it compile with the new compilers you need to replace the arguments spec to the following:
: "+w" (delay), "+a" (tmp)
After this little patch NewSoftSerial becomes compilable with the new compilers.
This post is published in Hardware, Linux, Robotics, Software.