ARM M4 HARD FAULT C / ASM LANGUAGE MIXED.

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.  I'm really experimented at AVR programming in C / ASM and even mixed functions together.  But I'm new to ARM programming.  I'm using Atmel Studio 7.  I'm trying to make a simple call to a function written in ASM and I can't figure out what the problem can be...   I have two functions:  The function are simple, named test1 and test2, the two functions are the same, a NOP and a BX LR.    The code cause an HARD FAULT at the call of the second function even if the two fiunctions are the same.

 

Let me explain it:

 

This is the C code:

(The chip I use is ATSAMD51G19A)


#include "sam.h"
extern void test1();
extern void test2();

int main(void)
{
    /* Initialize the SAM system */
    SystemInit();  //This is the call in the librairy, no problem with this one

	test1();
	test2();

    while (1)
    {
    }
}

 

This is the Assembly code (Assembler1.S)

 


.thumb_func
.syntax unified

.global test1
.global test2

test1:
	nop
    bx  lr

test2:
	nop
	bx	lr

as you can see, this is really simple code.

 

When It call test1(), it execute normally and return to the C program in debug mode, no problem.  even in Disassembly, all is fine.

When It call test2(), it go to the NOP and the next step after executing the NOP it go directly to the interrupt DUMMY_HANDLER of the C function in the library made for that.

 

This is really strange.  If I modify the source code to call test1() many times, it will call the function many times without any problem and return between each.  But if I change for test2() first, it also crash even if the code is the same.  The only thing that change in the code is the memory location where it's compiled.

 

I also tried to make some ASM more complex code like a delay with a loop, and it works fine and return after completed.  Only the 1st declared ASM function at the top of the file works fine, and any other function cause a hard fault.  If I displace any ASM function that was not working at the top of the file, the fonction work and the other that worked doesn't.

 

I'm prety sure I'm doing a noobs mistake here and someone have the answer to my problem.  If you need more information, let me know.

Thanks in advance!

Cedric

 

This topic has a solution.
Last Edited: Fri. Jan 25, 2019 - 02:31 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok after a lot of search, I found the solution myself.  You must put the Pre compiler command ".thumb_func" at the beginning of each function.  If not, the compiler switch to ARM IT mode after the BX LR.  This is why it worked only in the first function.

Cedric