why MCU reset after execute the code?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

why MCU reset after execute the code?

492 Views
allenhuang1
Contributor I

Dear Sir,

I have simulated the below code in IAR.

And it worked!

But I run this code in real KE02.

this code will cause KE02 reset.

could you help find out root cause?

I have changed the macro as below. this macro worked normally.

#define Set16Int(p,d)        *(p)=d&0xff; *(p+1)=d>>8

====================================================

#define Get16Int(p) *((uint16*)(p))
#define Set16Int(p,d) *((uint16*)(p)) = d

uint16 u16gT=0;

uint8 u8gRxBuff[16];

u8gRxBuff[0]=0x12;

u8gRxBuff[1]=0x34;

u16gT = Get16Int(u8gRxBuff);

u16gT=0x1234;

Set16Int(u8gRxBuff,u16gT);

 

0 Kudos
3 Replies

395 Views
mjbcswitzerland
Specialist V

Allen

The KE02 has a Cortex-m0+ core and this can only access aligned words.

Your code may or may not work, depending on the alignment of uint8 u8gRxBuff[16];

Take a look in the map file and you will presumably find that this is aligned on an odd address, which will cause

u16gT = Get16Int(u8gRxBuff);

to hard fault.

You either need to align you arrays (eg. use uint16 u8gRxBuff[16/sizeof(unsigned short)]; to force it) instead or else use macros which are core independent: eg. #define Get16Int(x) ((*(uint8*)x << 8) | (*(uint8*)((uint8 *)(x + 1))))

The IAR simulation may have been OK if you luckily had an even address, or possibly the IAR simulator is not checking correctly (test by passing u8gRxBuff + 1 instead to see whether it flags an illegal access).

Regards

Mark

http://www.utasker.com/kinetis.html
http://www.utasker.com/kinetis/FRDM-KE02Z.html
http://www.utasker.com/kinetis/FRDM-KE02Z40M.html

0 Kudos

395 Views
allenhuang1
Contributor I

Dear Mark,

Thanks for your reply.

If this is aligned problem, do you have any method can force the array is aligned.

0 Kudos

395 Views
mjbcswitzerland
Specialist V

Allen

I gave this possibility in the previous post (although it may not be respected by all compilers):

uint16 u8gRxBuff[16/sizeof(unsigned short)];

Others are

uint8 u8gRxBuff[16 + 1];
uint8 *ptru8gRxBuff = u8gRxBuff;
if (((unsigned long)ptru8gRxBuff  & 0x1) != 0) { // if not short word aligned
    ptru8gRxBuff ++; // skip the first byte so that it is aligned

}

// Use ptru8gRxBuff  for all operations (it is aligned memory)

uint8 *ptru8gRxBuff = malloc(16); // assuming a version of malloc() that always returns long word aligned memory

Regards

Mark

0 Kudos