SPR access

David Brown david@westcontrol.com
Fri Jul 8 16:07:00 GMT 2016


(You have something wrong with your emails - the subject for your post
had a copy of most of the body of the email.  I have changed it to
something smaller.)

You can't use a function argument for the number of the SPR, because the
assembler requires the SPR at assembly time to generate the full mtspr
instruction.  My solution was to use a macro for this, rather than a
function (even a static inline function won't work):

#define readSpr(sp) ({ uint32_t res; \
        asm volatile (" mfspr %[res], %[spr] " : \
		[res] "=r" (res) : \
		[spr] "i" (sp) ); \
        res; })

#define writeSpr(sp, va) \
        asm volatile (" mtspr %[spr], %[val] " : \
		: \
		[spr] "i" (sp), [val] "r" (va) )



On 08/07/16 16:16, Tran Tu Truong wrote:
> Dear Mr/Ms;
> I am Truong, working at Vietnam human engineering in Vietnam.
> Now, I am using D32 Design Studio for programming MPC5777C micro
> controller (GNU C Compiler 4.9). I've already made a sample inline asm
> code in .C file ase bellow:
> void set_spr ( uint32_t spr_num, uint32_t val )
> {
> asm volalatile ("mtspr %0, %1"
> :
> :"r"(spr_num), "r"(val)
> );
> }
> This code does not work correct (It should work but not work). I found
> out the constraints operands [:"r"(spr_num),] is not correct. I thinks
> it better if spr_num is be referred as a constant not a register ( ex:
> mtspr 120, val ). But I don't know how to revise.
> I also referred on
> https://gcc.gnu.org/onlinedocs/gcc/Constraints.html#Constraints
> about PowerPC but I cannot understand clearly.So, could you please
> help me to explain about that?
> If you have the instruction about "constraints for asm operands" for
> MPC5777C micro controller, please provide me.
> Thank you.
> --Tran Tu Truong--

More information about the Gcc-help mailing list