0

I've been messing around with the Cambridge baking pi tutorials (Basic OS development with little demos for the raspberry pi). Only i've been writing the code in C instead. I've got my development environment setup and i'm able to run my code successfully with GCC optimization turned off but not on.

The problem occurs with my code that uses memory mapped IO (if i compile everything else except the following file with optimizations on, it works). Which i initially thought to be that my pointers were not declared as volatile so the compiler was optimizing the actual writes to memory away and using registers instead. But even when i declare them as volatile, the problem persists.

Here's how i do a write to memory:

#define UART0_CR ((volatile uint32_t *) (UART0_BASE + 0x30))
...
*UART_CR = 0;

The pointers are of a volatile type so i can't see why GCC would decide not to do the actual write. Is there anything else i need to be on the lookout here for? Am i misunderstanding the use of volatile?

Complete working file (with optimization off anyway):

#include <stdint.h>
#include <uart.h>

#define GPIO_BASE   0x20200000
#define GPPUD       ((volatile uint32_t *) (GPIO_BASE + 0x94))
#define GPPUDCLK0   ((volatile uint32_t *) (GPIO_BASE + 0x98))

#define UART0_BASE      0x20201000
#define UART0_DR        ((volatile uint32_t *) (UART0_BASE + 0x00))
#define UART0_FR        ((volatile uint32_t *) (UART0_BASE + 0x18))
#define UART0_IBRD      ((volatile uint32_t *) (UART0_BASE + 0x24))
#define UART0_FBRD      ((volatile uint32_t *) (UART0_BASE + 0x28))
#define UART0_LCRH      ((volatile uint32_t *) (UART0_BASE + 0x2C))
#define UART0_CR        ((volatile uint32_t *) (UART0_BASE + 0x30))
#define UART0_IMSC      ((volatile uint32_t *) (UART0_BASE + 0x38))
#define UART0_ICR       ((volatile uint32_t *) (UART0_BASE + 0x44))

static void delay(int32_t count) {
    asm volatile("__delay%=: subs %[count], %[count], #1; bne __delay%=\n"
                :
                : [count]"r"(count) 
                : "cc"
    );
}

void uart_init() {    
    *UART0_CR = 0; // Disable UART0.    
    *GPPUD = 0;     // Disable pull up/down for all GPIO pins & delay for 150 cycles.
    delay(150);   
    *GPPUDCLK0 = (1 << 14) | (1 << 15); // Disable pull up/down for pin 14,15 & delay for 150 cycles.
    delay(150);   
    *GPPUDCLK0 = 0; // Write 0 to GPPUDCLK0 to make it take effect.    
    *UART0_ICR = 0x7FF; // Clear pending interrupts.
    *UART0_IBRD = 1; //Set rate
    *UART0_FBRD = 40;     
    *UART0_LCRH = (1 << 4) | (1 << 5) | (1 << 6); // Enable FIFO & 8 bit data transmissio (1 stop bit, no parity).    
    *UART0_IMSC = (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10); // Mask all interrupts.   
    *UART0_CR = (1 << 0) | (1 << 8) | (1 << 9);  // Enable UART0, receive & transfer part of UART.  
}

void uart_putc(uint8_t byte) {    
    while (1) { // wait for UART to become ready to transmit    
        if (!(*UART0_FR & (1 << 5))) break;
    }   
    *UART0_DR = byte; // Transmit
}

void uart_puts(const char *str) {
    while (*str) {
        uart_putc(*str++);
    }
}

EDIT:

Looked up how to view the assembly, very useful, thankyou. If i take first 2 writes out of uart i init (up to the first delay call) i get:

Unoptimized:

uart_init:
    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 1, uses_anonymous_args = 0
    stmfd   sp!, {fp, lr}
    add fp, sp, #4
    ldr r3, .L3
    mov r2, #0
    str r2, [r3, #0]
    ldr r3, .L3+4
    mov r2, #0
    str r2, [r3, #0]

Optimized:

uart_init:
    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 0, uses_anonymous_args = 0
    @ link register save eliminated.
    ldr r3, .L2
    ldr r2, .L2+4
    mov r1, #0
    str r1, [r3, #48]
    str r1, [r2, #148]

The only difference seems to be that for the unoptimized, it doesn't add an offset to the .L2 and .L2+4 labels, which the the optimized one does. Unless the pointers at those labels have been prepared to already have the offsets calculated on them.

I have a forked qemu (qemu-rpi) modded for raspberry pi support, so i'm going to try checking what values are loaded in the r3 and r2 registers to see if they are the correct pointers before the store happens and then i'm going check whether the putc is looping on the transmit using breakpoints. Not very adept with my environment yet so this might take me a while!

5
  • What evidence do you have that the actual write is not done? Did you look at the assembly dump?
    – ouah
    Jun 24, 2013 at 17:25
  • Code looks fine at first blush. What happens when it is optimized? Does uart_putc() loop forever waiting for bit 5?
    – torek
    Jun 24, 2013 at 17:26
  • I abstract my Mem/IO reads and writes for this and other reasons, github.com/dwelch67/raspberrypi. You can play games until you are blue in the face with compiler specific attributes and still end up with an optimizer problem at some point. The hardware I/O in my case is in asm, but is basically in a separate file, not part of the optimization.
    – old_timer
    Jun 24, 2013 at 18:03
  • Yes, i think i might put my mmio writes into a separate assemble function. The problem is now fixed but the hardware IO in C seems far too fragile and i don't have the compiler knowledge to deal with these problems properly. Thanks guys.
    – Callum
    Jun 24, 2013 at 18:18
  • "Hardware I/O in C" isn't fragile. You just have to be mindful of the fact you're "at the metal". And with that in mind, you can't use abstraction or otherwise try to remove yourself by layering. C is very convenient for this, otherwise you need ASM. Remember that C was originally designed as a portable assembler.
    – user2513931
    Jun 25, 2013 at 23:50

2 Answers 2

1

Using an optimizer along with code that talks to hardware can result in bizarre behavior.

Try using __attribute__((noinline)) in front of functions that talk to your hardware. I'd also recommend putting all code that talks with hardware into its own file and turning off optimization. The optimizer can re-order or in-line these items.

6
  • Okay this fixes the problem. I tried using that attribute and found that it's only required on the inline assembly delay function. Which i assume breaks the delay and causing the serial device to recieve commands too quickly. Why is this? Is it possible the optimizer is inlining the delay code into the methods forgetting it's volatile and optimizing the delay away?
    – Callum
    Jun 24, 2013 at 18:13
  • Thankyou so much. I think i'm going to put my mmio writes into a assembly function i'll call from C to avoid these kinds of problems in the future though!
    – Callum
    Jun 24, 2013 at 18:20
  • I don't remember specifically all the things the optimizer can do to your code. But it's pretty remarkable with its re-ordering, removal and inlining choices.
    – user2513931
    Jun 24, 2013 at 18:22
  • Curious. The volatile in the asm should keep this from being moved or combined even if the function is inlined. I wonder, though: does this particular gcc variant support %= for unique-ifying labels? If not, inline expansion of the delay would cause some serious problems. (Are local-labels available? ie, 0: and ..., 0b to branch back to the nearest label 0.)
    – torek
    Jun 24, 2013 at 18:32
  • My experience is that volatile only tells the compiler that the item can't be optimized out if there's no obvious reference to it. To make unique labels with in-line, prepend the function's name to all labels. ASM labels are globally visible.
    – user2513931
    Jun 24, 2013 at 18:36
-1

I'm trying to accomplish the same thing but instead of defining, I'm just assigning like this:

volatile const int *Register = (volatile const int *)0xFFFEF000;

Then you can send values directly to the registers by name:

Register = 0xFFFFFFFF;
1
  • I think volatile int *const Register will work better. You never want to change the address of a hardware register, but its contents might well need changing… Sep 11, 2020 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.