|
|
View previous topic :: View next topic |
Author |
Message |
future
Joined: 14 May 2004 Posts: 330
|
Post that code snippet that doesn't work here! v4.107 |
Posted: Thu Jun 10, 2010 8:48 pm |
|
|
Hello everybody,
I was thinking about how to improve this compiler. Actually I like it very much and don't want to use lots of pragmas and linker scripts.
Following the idea, I think we could share with CCS team all test codes we have that does not work as it is suppose to do.
To start it, I will post one that compiles without warnings and output is incorrect.
Code: | #include <18f4680.h>
#include <stdio.h>
#include <string.h>
#use delay(clock=40000000, RESTART_WDT)
#use rs232(baud=9600, parity=n, xmit=PIN_C6, rcv=PIN_C7, bits=8)
rom char aaa[] = "test";
char a;
void main(void){
a = strlen(aaa);
printf("%u", a);
while(1);
} |
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
Re: Post that code snippet that doesn't work here! v4.107 |
Posted: Fri Jun 11, 2010 1:06 am |
|
|
future wrote: | Hello everybody,
I was thinking about how to improve this compiler. Actually I like it very much and don't want to use lots of pragmas and linker scripts.
Following the idea, I think we could share with CCS team all test codes we have that does not work as it is suppose to do.
To start it, I will post one that compiles without warnings and output is incorrect.
Code: | #include <18f4680.h>
#include <stdio.h>
#include <string.h>
#use delay(clock=40000000, RESTART_WDT)
#use rs232(baud=9600, parity=n, xmit=PIN_C6, rcv=PIN_C7, bits=8)
rom char aaa[] = "test";
char a;
void main(void){
a = strlen(aaa);
printf("%u", a);
while(1);
} |
|
What were you expecting it to print?
What happens if you change aaa to this... rom char aaa[] = "test\0"; _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Fri Jun 11, 2010 7:24 am |
|
|
It doesn't work because the strlen() function expects a pointer to the address of the first member of your string. You declare a string in ROM which the CCS compiler doesn't like because now you have a pointer to ROM. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Fri Jun 11, 2010 8:42 am |
|
|
This is what the option 'PASS_STRINGS=IN_RAM' is all about.
Code: |
#include <18F4680.h>
#device PASS_STRINGS=IN_RAM
#ID checksum
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=40000000, RESTART_WDT)
#use rs232(baud=9600, parity=n, xmit=PIN_C6, rcv=PIN_C7, bits=8, ERRORS)
#include <stdio.h>
#include <string.h>
const char aaa[] = "test";
char a;
void main(void){
a = strlen(aaa);
printf("%u", a);
while(1);
}
|
The problem is that strlen, is a RAM string function. You have to critically remember that in the PIC, there are two completely separate memory spaces. Now 'ROM', allows some functions to be used on a constant string stored in ROM, but not all. Two choices exist:
1) Write your own overload for strlen, to work with a ROM string pointer.
2) Use versions of the constant string, copied temporarily into RAM.
The second is what the 'PASS_STRINGS' option does.
Works fine.
Best Wishes |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Fri Jun 11, 2010 11:17 am |
|
|
Quote: | The problem is that strlen, is a RAM string function |
I agree. But taht is not the case here... the compiler does not say anything about it. Also there aren´t two versions of strlen in the built-in library and the compiler is now suppose to do pointers to rom.
Compile the same code on PICC18 and the output is correct.
The idea of the thread is to have a resource to see what DON´T work. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|