On 09/11/17 04:57, Szikra Istvan wrote: > Hi > > Thanks for the SP, I missed that. > And apparently Atmel Studio also cannot find it, and underlines it with > red error marker. > It does compile, and I have found it in avr/common.h, it's probably a > problem with __AVR_ARCH__ handling by AS... > I guess that's what I get for trusting the IDE:) > > I know that just reading SP is not enough, I do also use stack > watermarking. It's just an additional diagnostic information. > Note: SP can also be used to re-mark the unused stack to trace stack > usage over time... > (or mark stack on platforms without .init* sections.) > > Note #999: > "the problem is in your code." > This isn't actually my code. My code was written for ARM and looked > something like this: > unsigned int GetStackPointer() { > volatile register unsigned int sp asm("r13"); > return sp; > } > that someone ported to AVR, and now I'm fixing it... ;) >
That code is wrong for the ARM too. You should not fix a variable to an assembly register that is used specifically by the compiler. Depending on the ARM in question, you should probably use a CMSIS function like _get_PSP(). The standard definition for gcc and Cortex-M is: __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PSP(void) { register uint32_t result; __ASM volatile ("MRS %0, psp\n" : "=r" (result) ); return(result); } Just to read the r13 register, the code would be something like this (I haven't checked the details here) : static inline uint32_t GetStackPointer(void) { uint32_t sp; asm volatile("mov %0, r13" : "=r" (sp)); return sp; } And you can probably just use __builtin_frame_address() - that should, in theory, work on the AVR and the ARM (I have not tested it on either target). _______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-gcc-list