[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] How to port "char flash *" used with IAR to avr-gcc?
From: |
Ned Konz |
Subject: |
Re: [avr-gcc-list] How to port "char flash *" used with IAR to avr-gcc? |
Date: |
Fri, 29 Oct 2004 08:14:34 -0700 |
User-agent: |
KMail/1.7 |
On Wednesday 20 October 2004 6:43 am, Günter Dannoritzer wrote:
> I have some code that was developed under IAR. Now I want to port it to
> avr-gcc and have some trouble getting a print function to work.
>
> This is the function, used under IAR:
>
> void PrintStr( char flash *str )
> {
> unsigned char count=0;
>
> while ((str[count] != '\0') && (count != 64)) {
> UartTransmitByte(str[count++]);
> }
> }
>
>
> It prints out the given string to the serial port.
>
> To compile it with avr-gcc I changed function parameter type to:
>
> void PrintStr( char * str)
>
> leaving the remaining code in the function body the same.
>
> It does not print out any text over the serial port.
>
> I searched for some information of the "flash" type and in the Atmel
> application note #35 it explains that this is a pointer to a flash
> location.
>
> The way I changed that now is, that I made it a pointer to a RAM location.
>
> From the explanation in the Atmel application note it seems like that
> after reset the whole code gets copied from the flash to the RAM. So if
> I have a PrintStr("Hello World") function call, the "Hello World" string
> should get copied to the RAM as well or am I missing here something?
But why would you want to waste RAM on a constant string?
> Reading through the libc documentation concerning reading data from the
> flash, I guess the other way to do this with avr-gcc would be to use the
> PGM_P and pgm_read_byte function to leave the string in the flash. Would
> that be the equivalent to the IAR flash pointer?
>
> Can anybody give me a hint how to fix this?
Try something like this:
/*
* compile with:
*
avr-gcc -O3 -Wall -Wl,-Map,test.map -g -mtiny-stack -mint8 -mmcu=atmega16 -o
test.elf test3.c
avr-objdump -h -S test.elf | tee test.lst
*/
#include <avr/pgmspace.h>
void
UartTransmitByte(char c)
{
/* do something */
}
void
PrintStr(PGM_P str)
{
PGM_P end = str + 64;
char c;
while ((c = pgm_read_byte_near(*str++) != '\0') && str < end)
UartTransmitByte(c);
}
static const char PROGMEM constantString[] = "this is a constant string in
ROM\r\n";
void
main(void)
{
PrintStr(PSTR("this is an unnamed const string\r\n"));
PrintStr(constantString);
}
--
Ned Konz
http://bike-nomad.com