cancel
Showing results for 
Search instead for 
Did you mean: 

Hard Fault error

tkjmail2
Associate II
Posted on March 13, 2010 at 22:53

Hard Fault error

15 REPLIES 15
domen23
Associate II
Posted on May 17, 2011 at 13:43

One of following two accesses is definitely not going to be aligned to short boundary, and ARM doesn't handle this. (Also, they look wrong even if it'd worked?)

tkjmail2
Associate II
Posted on May 17, 2011 at 13:43

Oh...?

But then how would you connect two chars (bytes) to a 16-bit usigned integer (u16)?

Thomas

Posted on May 17, 2011 at 13:43

The following methods should be safe, they operate one byte at a time.

unsigned short a;

unsigned char b[100];

memcpy(&a,&b[x+ 0],sizeof(unsigned short));

-OR-

a = ((unsigned short)b[x + 0] << 0) + ((unsigned short)b[x + 1] << 8);

You just can't use casts to arbitrarily change a pointer type and load it.

-Clive

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tkjmail2
Associate II
Posted on May 17, 2011 at 13:43

Why do you use memcpy? Wouldn't the ''a ='' be enough?

Thanks anyways.

Thomas

Posted on May 17, 2011 at 13:43

It was an either/or suggestion.

memcpy() works well for structures, and is often optimized to an intrinsic.

-Clive
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tkjmail2
Associate II
Posted on May 17, 2011 at 13:43

I can't make it work... Remember, I'm using pointers!

If I try this code, it doesn't get the correct value:

u16 color;

color = ((unsigned short)(&str[ 2 * temp]) << 0) + ((unsigned short)(&str[ 2 * temp+1]) << 8);

And if I use this code, it goes into the Hard Fault routine again:

u16 color;

memcpy(&color,&str[2 * temp],sizeof(unsigned short)); 

Best Regards

Thomas Jespersen

Posted on May 17, 2011 at 13:43

There seems to be a misunderstanding of how to use pointers, you don't need the '&' when you access the content of str[], only when you want the address (ie &str[])

color = ((unsigned short)(str[ 2 * temp]) << 0) + ((unsigned short)(str[ (2 * temp)+1]) << 8);

How are you calling the routine?

void foo(const unsigned char *str);

const unsigned char data[] = { 0x01, 0x02, 0x03, 0x04 };

foo(data); // Or foo(&data[0]);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 17, 2011 at 13:43

void DispPic(u16 x0, u16 y0, const unsigned char *str)

{

  typedef union

  {

    u16 U16;

    u8 U8[2];

  } ColorTypeDef;

 u32 temp;

 u16 i, j;

 u16 imageWidth;

 u16 imageHeight;

 ColorTypeDef color;

 color.U8[1] = str[0];

 color.U8[0] = str[1];

 imageWidth = color.U16;

 color.U8[1] = str[2];

 color.U8[0] = str[3];

 imageHeight = color.U16;

 temp = 4;

 for(j=0; j<imageHeight; j++)

 {

    Lcd_SetCursor(x0, y0);

    LCD_WriteRAM_Prepare();

    for(i=0; i<imageWidth; i++)

    { //Write all pixels to ram, and then update display (fast)

      color.U8[1] = str[temp++];

      color.U8[0] = str[temp++];

      LCD_WriteRAM(color.U16);

    }

    y0++;

  }

}

const unsigned char data[] = {

  0x00, 0x03, // 3 Wide

  0x00, 0x02, // 2 High

  0xAA, 0xAA,  0xBB, 0xBB,  0xCC, 0xCC,

  0xDD, 0xDD,  0xEE, 0xEE,  0xFF, 0xFF };

int main(int argc, char **argv)

{

  DispPic(10, 12, data);

  return(1);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tkjmail2
Associate II
Posted on May 17, 2011 at 13:43

After it has shown a couple of lines (I think it's 5 pixel lines) of the image, it goes into the Hard Fault routine.

OBS: The image is 56x56 pixels!

Thomas