简体   繁体   中英

Embedded ARM/Raspberry Pi Assembly in C

I am porting a library from Arduino to Raspbian. There is a section of embedded assembly that I cannot quite figure out and I am hoping someone can help or perhaps point me to a different resource. Helpfully, the code is well commented, so fingers crossed. Many thanks in advance.

The Arduino code is:

// The macro below uses 3 instructions per pin to generate the byte to transfer with SPI
// Retreive duty cycle setting from memory (ldd, 2 clockcycles)
// Compare with the counter (cp, 1 clockcycle) --> result is stored in carry
// Use the rotate over carry right to shift the compare result into the byte. (1 clockcycle).
#define add_one_pin_to_byte(sendbyte, counter, ledPtr) \
{ \
    unsigned char pwmval=*ledPtr; \
    asm volatile ("cp %0, %1" : /* No outputs */ : "r" (counter), "r" (pwmval): ); \
    asm volatile ("ror %0" : "+r" (sendbyte) : "r" (sendbyte) : );  \
}

unsigned char sendbyte;
add_one_pin_to_byte(sendbyte, counter, --ledPtr);

I believe that on ARM/Raspberry Pi "compare" is "CMP" rather than "CP", but the problem is with the "ROR" statement. I get an error message at build that says:

/tmp/ccZN1jmK.s: Assembler messages:
/tmp/ccZN1jmK.s:138: Error: bad arguments to instruction -- `ror r3'

I understand that "ROR" is Rotate Over Right, and I expect the compiler put the "r3" in, and that "r3" is probably the fourth register (third register if you use zeroth I suppose).

While this code is designed to be very tight, allowing a low power processor to do a lot of work, I would be OK converting it to C and eliminating the assembly if necessary.

I am porting a library from Arduino to Raspbian.

This isn't going to work.

The library you're trying to port is designed very specifically for the hardware of the AVR microcontroller used on the Arduino. It depends on the availability of memory-mapped port I/O and uses specific hardware timers.

The Raspberry Pi is a completely different system. Many of the features that this library depends upon are unavailable on the BCM283x processor used on the Pi. Moreover, the BCM283x runs at a much higher clock speed, making most of the optimizations used by this library unnecessary (and even inappropriate).

I'm not sure what you're trying to accomplish here exactly, but you will need to approach this at a higher level. Trying to perform a line-by-line "translation" of this code isn't going to yield useful results.

My problem was that on ARM/Raspberry Pi, the ROR instruction wants two parameters, destination register and register to be rotated. On the Arduino, the processor only required a single parameter. The statement that will build is:

asm volatile ("ROR %0, %1" : "=r" (sendbyte) : "r" (sendbyte) : );

However, I have decided to use something close to @Jester's C code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM