EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Codewarrior / c problem?

Started by JesperAagaard December 7, 2008
I hope someone can tell me what is going wrong, im fairly new to both
codewarrior/c and the HCS08 family.

I'm making a LED driver on a HCS08GT32A.

I have the following method: 

BUFFER_LENGTH = 4
BUFFER_HEIGHT = 10

void display_string_still(const unsigned char *str0, const unsigned char
*str, const unsigned long int time)
{ 
  unsigned char disp_con[4][6] = {0};
  unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0};
  
     
  unsigned char *stillptr;
  unsigned char *tempptr;
  unsigned char i;
  unsigned char j;  
  unsigned char x;
  unsigned char y;
  
  unsigned long int time_cnt;
  unsigned int length_of_incomming_str;
  unsigned char *charptr;


  length_of_incomming_str = strlen(str);
  
    
    for(j = 0; j < length_of_incomming_str; j++)
    { 
      charptr = (unsigned char *)ascii_to_font(str[j]);
      for(i = 0; i < BUFFER_HEIGHT; i++)
      {                
        disp_buffer[j][i] = charptr[i];
      }
    }
    
    //fixes byte0 and byte1
    for(y = 0; y < BUFFER_HEIGHT; y++)
    {               
      disp_buffer[0][y] = disp_buffer[0][y] >> 1;       
    }

    for(y = 0; y < BUFFER_HEIGHT; y++)
    {         
      if(disp_buffer[1][y] & 0b10000000)   // MSB from byte1 becomes LSB
in byte0
      {  
        disp_buffer[0][y] = disp_buffer[0][y] | (1 <<0);
        disp_buffer[1][y] = disp_buffer[1][y] << 1;
      }
      else       
        disp_buffer[1][y] = disp_buffer[1][y] << 1;       
    }

    //fixes byte1 and byte2   
    for(x = 0; x <= 2; x++)
    {      
      for(y = 0; y < BUFFER_HEIGHT; y++)
      {         
        if(disp_buffer[2][y] & 0b10000000)   // MSB from byte2 becomes LSB
in byte1
        {
          disp_buffer[1][y] = disp_buffer[1][y] | (1 << 2-x);
          disp_buffer[2][y] = disp_buffer[2][y] << 1;
        }
        else         
          disp_buffer[2][y] = disp_buffer[2][y] << 1; 
      }
    }  

    display_string_rolling_left(str0);
    display_clear();
    
    for(time_cnt = 0; time_cnt < time; time_cnt++)
    {
      display_buffer_setup(disp_buffer);  
    }
    display_clear();
}

Look at the first line in the method, where i declare this array:  
unsigned char disp_con[4][6] = {0}; 

now the method doesnt work at all, but if i comment out the line it works
perfectly. Or if i change the allocation size to unsigned char
disp_con[4][5] = {0}; or lower it works...

I can't really seem to figure out what is going wrong, all I can do is
observe that, changing the array size or comment it out, makes my program
run perfectly. Have i missed something ? 

I really hope someone can tell me whats going wrong?

Thanks in advance.


On Sun, 07 Dec 2008 07:59:18 -0600, "JesperAagaard"
<megakogle@hotmail.com> wrote:

>I hope someone can tell me what is going wrong, im fairly new to both >codewarrior/c and the HCS08 family. > >I'm making a LED driver on a HCS08GT32A. > >I have the following method: > >BUFFER_LENGTH = 4 >BUFFER_HEIGHT = 10 > >void display_string_still(const unsigned char *str0, const unsigned char >*str, const unsigned long int time) >{ > unsigned char disp_con[4][6] = {0}; > unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0}; > > > unsigned char *stillptr; > unsigned char *tempptr; > unsigned char i; > unsigned char j; > unsigned char x; > unsigned char y; > > unsigned long int time_cnt; > unsigned int length_of_incomming_str; > unsigned char *charptr; > > > length_of_incomming_str = strlen(str); > > > for(j = 0; j < length_of_incomming_str; j++) > { > charptr = (unsigned char *)ascii_to_font(str[j]); > for(i = 0; i < BUFFER_HEIGHT; i++) > { > disp_buffer[j][i] = charptr[i]; > } > }
You have a potential buffer overrun here. j *must* be trapped if it's greater than or equal to BUFFER_LENGTH but length_of_incomming_str could be (is?) larger. Oh, and you're not using The One True Brace Style. ;-) -- Rich Webb Norfolk, VA
JesperAagaard wrote:

> I hope someone can tell me what is going wrong, im fairly new to both > codewarrior/c and the HCS08 family. > > I'm making a LED driver on a HCS08GT32A. > > I have the following method: > > BUFFER_LENGTH = 4 > BUFFER_HEIGHT = 10 > > void display_string_still(const unsigned char *str0, const unsigned char > *str, const unsigned long int time) > { > unsigned char disp_con[4][6] = {0}; > unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0};
The arrays are local in the function, so they are allocated on stack. How large has you configured the stack size? Try add a "static": static unsigned char disp_con[4][6] = {0}; Then it will be allocated somewhere in the RAM (but it is not local anymore to the function, but same as if you woul declare it as a global variable). But looks like the disp_con variable is not used anyway in the rest of your code, so you can delete this. -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
On Sun, 07 Dec 2008 07:59:18 -0600, "JesperAagaard"
<megakogle@hotmail.com> wrote:

>I hope someone can tell me what is going wrong, im fairly new to both >codewarrior/c and the HCS08 family.
It looks like you are using a total of 77 bytes of stack for local variables. I would be sure your stack can handle that much allocation.
> unsigned char disp_con[4][6] = {0}; > unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0};
Maybe I am not up on all my C concepts, but in your first two arrays, are you trying to zero them out using the "= {0};"? If those arrays were either declared global or static (which is preferred over global), they would be zeroed by the compiler once on startup, unless you specifically tell it not to do that. In your code, are you expecting the arrays to be automatically cleared every time the function is entered? I don't think it does that, if that is what you want. Lou
Mr. C wrote:
> On Sun, 07 Dec 2008 07:59:18 -0600, "JesperAagaard" > <megakogle@hotmail.com> wrote:
>> unsigned char disp_con[4][6] = {0}; >> unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0};
> In your code, are you expecting the arrays to be automatically > cleared every time the function is entered? I don't think it does > that, if that is what you want.
In that case you would be thinking incorrectly. The C compiler is definitely required to perform all explicit initialization of automatic variables every time their enclosing block begins executing. You may be mixing this up with a closely related case: it's not required to initialize variables that have no explicit initializer. I.e. a variable { int random_garbage; /* ... */ } will contain exactly that.
   For what it's worth, I've been using the Codewarrior/HC08 combo
recently. 
On Sat, 13 Dec 2008 01:15:24 +0100, Hans-Bernhard Br&#4294967295;ker
<HBBroeker@t-online.de> wrote:

>Mr. C wrote: >> On Sun, 07 Dec 2008 07:59:18 -0600, "JesperAagaard" >> <megakogle@hotmail.com> wrote: > >>> unsigned char disp_con[4][6] = {0}; >>> unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0}; > >> In your code, are you expecting the arrays to be automatically >> cleared every time the function is entered? I don't think it does >> that, if that is what you want. > >In that case you would be thinking incorrectly. The C compiler is >definitely required to perform all explicit initialization of automatic >variables every time their enclosing block begins executing.
> >You may be mixing this up with a closely related case: it's not required >to initialize variables that have no explicit initializer. I.e. a variable > >{ > int random_garbage; > /* ... */ >} > > >will contain exactly that.
It depends on the compiler setting. "Full ANSI C" mode in the Codewarrior HC08 compiler. You can set it to do minimal initialization before jumping to main(), only setting the stack pointer, and maybe a few other tiny things, but it won't zero global variables.
On Sat, 13 Dec 2008 01:36:32 -0500, Ben Bradley
<ben_nospam_bradley@frontiernet.net> wrote:

> For what it's worth, I've been using the Codewarrior/HC08 combo >recently.
I meant to type more there: There are forums at http://freescale.com that appear to be excellent for technical questions such as this.
>In that case you would be thinking incorrectly. The C compiler is >definitely required to perform all explicit initialization of automatic >variables every time their enclosing block begins executing.
Yes, you are correct. I use it so little that I had forgotten that. I almost always initialize local variables in the code, so I had forgotten initializing in that way. Thanks. Lou

The 2024 Embedded Online Conference