C and accessing registers on the Rasberri Pi

Discuss all aspects of programming here. From 8-bit through to modern architectures.
Post Reply
TimoHartong
Posts: 11
Joined: Fri Dec 30, 2016 10:02 pm
Contact:

C and accessing registers on the Rasberri Pi

Post by TimoHartong » Sat Mar 24, 2018 12:33 pm

The last few weeks I'm programming to get the second UART ( miniUART ) working on the Rasberri Pi for RISCOS. I do this in C instead of assembler. Getting data from a peripheral in C worked quite Ok but writing was a problem. Reading through the riscopen forum and in the source tree ( in code we trust :D ) I read you have to insert a Data Memory Barrier. I could have written a macro which does this. But would there be a more elegant method ? Yes there is use the keyword 'volatile' for the variable used to access the peripheral. This solved the problem for me.

Relevant links:
https://www.riscosopen.org/forum/forums/11/topics/3191
https://barrgroup.com/Embedded-Systems/ ... le-Keyword
http://infocenter.arm.com/help/index.js ... a3750.html

User avatar
hoglet
Posts: 7444
Joined: Sat Oct 13, 2012 6:21 pm
Location: Bristol
Contact:

Re: C and accessing registers on the Rasberri Pi

Post by hoglet » Sat Mar 24, 2018 12:46 pm

TimoHartong wrote: This solved the problem for me.
Was it using the memory barrier or using volatile that fixed the problem?

volatile is definitely necessary, otherwise the compiler can optimize out critical parts of the code relating to I/O accesses.

In theory the memory barrier is needed when switching from accessing one peripheral to another. Otherwise I think reads can possibly get re-ordered. But in practice you can often get away without it.

I've also never seen a good description of what constitutes the boundaries of "one peripheral" wrt. the BCM2835.

Dave

TimoHartong
Posts: 11
Joined: Fri Dec 30, 2016 10:02 pm
Contact:

Re: C and accessing registers on the Rasberri Pi

Post by TimoHartong » Sat Mar 24, 2018 1:14 pm

Sorry using the keyword volatile fixed it for me I didn't need the barrier. It is code inside a module so I'm already in SVC mode which makes life much easier using OS_EnterOS in C has some problems I read ;-)

Post Reply