CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Typedef conversion for ANSI compliance

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Typedef conversion for ANSI compliance
PostPosted: Mon Jan 28, 2008 1:05 pm     Reply with quote

Hi there. I'm having problems with typedef conversions. I'm busy trying to implement the fat lib at http://elm-chan.org/fsw/ff/00index_e.html. It's apparently ANSI C compatible and has a include file to handle type conversions for non ANSI compilers AKA CCS.

My v4.057 compiler does not like this file, on the first line it warns "Expecting an identifier" which I can understand as an INT already has a "meaning" to the compiler but how can I overcome this problem other than re-writing all the code. I looked in the help file on the topic of typedef but doesn't help much. I have however found the "#Type" directive but am not sure if this will do what I want it to. I have included my modified version of the code but it gives me a compiler warning "Expresion must evaluate to a constant" Am I on the right track or using functions for the wrong purpose? Secondly, is the "enum" function used correctly, as I don't see the need for the "bool" at the end, and is the function actually needed?

Thanx in advance

PS. Anybody had any luck with this FAT lib??

here is the original file:
Code:

#ifndef _INTEGER

/* These types are assumed as 16-bit or larger integer */
typedef signed int      INT;
typedef unsigned int   UINT;

/* These types are assumed as 8-bit integer */
typedef signed char      CHAR;
typedef unsigned char   UCHAR;
typedef unsigned char   BYTE;

/* These types are assumed as 16-bit integer */
typedef signed short   SHORT;
typedef unsigned short   USHORT;
typedef unsigned short   WORD;

/* These types are assumed as 32-bit integer */
typedef signed long      LONG;
typedef unsigned long   ULONG;
typedef unsigned long   DWORD;

/* Boolean type */
typedef enum { FALSE = 0, TRUE } BOOL;

#define _INTEGER
#endif



and here is my modified file:
Code:

#ifndef _INTEGER

/* These types are assumed as 16-bit or larger integer */
#TYPE   signed INT=16
typedef unsigned int   UINT;

/* These types are assumed as 8-bit integer */
#TYPE  signed char=8
typedef unsigned char   UCHAR;

/* These types are assumed as 16-bit integer */
#TYPE   signed SHORT=16
typedef unsigned short   USHORT;
typedef unsigned short   WORD;

/* These types are assumed as 32-bit integer */
#TYPE   signed LONG=32
typedef unsigned long   ULONG;
typedef unsigned long   DWORD;

/* Boolean type */
typedef enum { FALSE=0, TRUE };

#define _INTEGER
#endif
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Tue Jan 29, 2008 4:32 pm     Reply with quote

Have you tried removing the data type from the expressions? It is unnecessary for what you are trying to do.

You want TYPE to change the length of the default type. Since INT is normally 8 bits, by using #TYPE INT=16, you have made INT equal to 16 bits. You can also use #TYPE SIGNED to make all default uses of the word as signed. As a result, INT should now be 16 bit signed. Then you use typedef to change unsigned INT to UINT which should now be 16 bit unsigned.

Try:
Code:

#ifndef _INTEGER

/* Set the default variable type as signed */
#TYPE SIGNED

/* These types are assumed as 16-bit or larger integer */
#TYPE   INT=16
typedef unsigned INT   UINT;

/* These types are assumed as 8-bit integer */
#TYPE CHAR=8
typedef unsigned CHAR   UCHAR;

/* These types are assumed as 16-bit integer */
#TYPE   SHORT=16
typedef unsigned SHORT   USHORT;
typedef unsigned SHORT   WORD;

/* These types are assumed as 32-bit integer */
#TYPE   LONG=32
typedef unsigned LONG   ULONG;
typedef unsigned LONG   DWORD;

/* Boolean type */
typedef enum { FALSE=0, TRUE };

#define _INTEGER
#endif

_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Typedef not working
PostPosted: Tue Jan 29, 2008 11:58 pm     Reply with quote

Hi again. I tried the changes as suggested but the compiler gets stuck on the "#TYPE CHAR=8" line with message "Expresion must evaluate to a constant" and when i comment this line out, it gets stuck on the "typedef enum { FALSE=0, TRUE };" line meth message "Expecting and identifier". Any other suggestions???

Kind Regards
Guest








Re: Typedef not working
PostPosted: Wed Jan 30, 2008 12:27 am     Reply with quote

crystal_lattice wrote:
Hi again. I tried the changes as suggested but the compiler gets stuck on the "#TYPE CHAR=8" line with message "Expresion must evaluate to a constant" and when i comment this line out, it gets stuck on the "typedef enum { FALSE=0, TRUE };" line meth message "Expecting and identifier". Any other suggestions???

Kind Regards


It is expecting an identifier: the name for the typedef. The line in the original file was:

Code:

/* Boolean type */
typedef enum { FALSE = 0, TRUE } BOOL;


You left out the BOOL name in your modification.

For the other issue of not being able to define INT as being something else I believe is due to the fact the CCS compilers are by default case insensitive. So 'int' is exactly the same as 'INT' to the compiler. If you turn this feature off likely your original code will work as expected. However the downside is it may break some CCS library code which depends on case insensitivity.
dyeatman



Joined: 06 Sep 2003
Posts: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Jan 30, 2008 12:33 am     Reply with quote

You are trying to redefine the Char and Bool types that are already standard defaults (Pg 37 in the current manual).

Read the #Type definition section of the manual. It tells you the allowed standard types for the #Type pre-processor (pg 115 in the current manual)
ak6dn



Joined: 08 Jan 2006
Posts: 23
Location: Saratoga, CA USA

View user's profile Send private message

Re: Typedef not working
PostPosted: Wed Jan 30, 2008 12:34 am     Reply with quote

Anonymous wrote:
crystal_lattice wrote:
Hi again. I tried the changes as suggested but the compiler gets stuck on the "#TYPE CHAR=8" line with message "Expresion must evaluate to a constant" and when i comment this line out, it gets stuck on the "typedef enum { FALSE=0, TRUE };" line meth message "Expecting and identifier". Any other suggestions???

Kind Regards


It is expecting an identifier: the name for the typedef. The line in the original file was:

Code:

/* Boolean type */
typedef enum { FALSE = 0, TRUE } BOOL;


You left out the BOOL name in your modification.

For the other issue of not being able to define INT as being something else I believe is due to the fact the CCS compilers are by default case insensitive. So 'int' is exactly the same as 'INT' to the compiler. If you turn this feature off likely your original code will work as expected. However the downside is it may break some CCS library code which depends on case insensitivity.


You want to include the pragma:
Code:

#case

in your file to turn case sensitivity OFF. I forgot to re-enable autologin prior to my last post :-(
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Typedef update
PostPosted: Wed Jan 30, 2008 3:23 am     Reply with quote

Update: Inserting the #case at the top of the file causes other problems; compiler stops at "typedef unsigned INT UINT;" line with warning "Expecting a ; or,". Removing the #case causes my previous problems to come back!! I fixed the "typedef enum { FALSE=0, TRUE }Bool;" but still warning "Expecting identifier" Any other hints??

PS. If I'm not mistaken the compiler is case insensitive and using #case makes it case sensitive

Kind Regards
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Wed Jan 30, 2008 8:32 am     Reply with quote

Break it apart - fix one piece at a time - then put the working parts back together. I think you can get there Smile

Ken
ak6dn



Joined: 08 Jan 2006
Posts: 23
Location: Saratoga, CA USA

View user's profile Send private message

Re: Typedef update
PostPosted: Wed Jan 30, 2008 11:54 pm     Reply with quote

crystal_lattice wrote:
Update: Inserting the #case at the top of the file causes other problems; compiler stops at "typedef unsigned INT UINT;" line with warning "Expecting a ; or,". Removing the #case causes my previous problems to come back!! I fixed the "typedef enum { FALSE=0, TRUE }Bool;" but still warning "Expecting identifier" Any other hints??

PS. If I'm not mistaken the compiler is case insensitive and using #case makes it case sensitive

Kind Regards


Ok, so I modified your original code to this to compile with no errors (compiler 4.066):
Code:


#device PIC18F4550

#case

/* These types are assumed as 16-bit or larger integer */
typedef signed int      INT;
typedef unsigned int   UINT;

/* These types are assumed as 8-bit integer */
typedef signed char      CHAR;
typedef unsigned char   UCHAR;
typedef unsigned char   BYTE;

/* These types are assumed as 16-bit integer */
//typedef signed short   SHORT;
typedef unsigned short   USHORT;
typedef unsigned short   WORD;

/* These types are assumed as 32-bit integer */
typedef signed long      LONG;
typedef unsigned long   ULONG;
typedef unsigned long   DWORD;

/* Boolean type */
typedef enum { FALSE = 0, TRUE } BOOL;


I had to comment out the "typedef signed short SHORT;" line because it keeps throwing an error; I don't know why this would be.

Normally the CCS device files define BYTE, TRUE, and FALSE, so if you have a "#include <18F4550.h>" type of line in your file it will throw errors on these lines as well.
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

Re: Typedef update
PostPosted: Thu Jan 31, 2008 11:31 am     Reply with quote

ak6dn wrote:
crystal_lattice wrote:
Update: Inserting the #case at the top of the file causes other problems; compiler stops at "typedef unsigned INT UINT;" line with warning "Expecting a ; or,". Removing the #case causes my previous problems to come back!! I fixed the "typedef enum { FALSE=0, TRUE }Bool;" but still warning "Expecting identifier" Any other hints??

PS. If I'm not mistaken the compiler is case insensitive and using #case makes it case sensitive

Kind Regards


Ok, so I modified your original code to this to compile with no errors (compiler 4.066):
Code:


#device PIC18F4550

#case

/* These types are assumed as 16-bit or larger integer */
typedef signed int      INT;
typedef unsigned int   UINT;

/* These types are assumed as 8-bit integer */
typedef signed char      CHAR;
typedef unsigned char   UCHAR;
typedef unsigned char   BYTE;

/* These types are assumed as 16-bit integer */
//typedef signed short   SHORT;
typedef unsigned short   USHORT;
typedef unsigned short   WORD;

/* These types are assumed as 32-bit integer */
typedef signed long      LONG;
typedef unsigned long   ULONG;
typedef unsigned long   DWORD;

/* Boolean type */
typedef enum { FALSE = 0, TRUE } BOOL;


I had to comment out the "typedef signed short SHORT;" line because it keeps throwing an error; I don't know why this would be.

Normally the CCS device files define BYTE, TRUE, and FALSE, so if you have a "#include <18F4550.h>" type of line in your file it will throw errors on these lines as well.


Unfortunately, this will not work as it keeps the data types the same size. As a result, "typedef signed short SHORT" fails because short is normally 1 bit in size and can not be signed.

Try the following:
Code:
#ifndef _INTEGER

/* Boolean type */
typedef INT1 BOOL;

/* Set the default variable type as signed */
#TYPE SIGNED

/* These types are assumed as 8-bit integer */
typedef unsigned CHAR UCHAR;

/* These types are assumed as 16-bit integer */
#TYPE INT=16
typedef unsigned INT UINT;

/* These types are assumed as 16-bit integer */
#TYPE SHORT=16
typedef unsigned SHORT USHORT;
typedef unsigned SHORT WORD;

/* These types are assumed as 32-bit integer */
#TYPE LONG=32
typedef unsigned LONG ULONG;
typedef unsigned LONG DWORD;

#ifndef FALSE
#define FALSE   0
#endif

#ifndef TRUE
#define TRUE   1
#endif

#define _INTEGER

#endif

_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Typdef problems
PostPosted: Thu Jan 31, 2008 11:47 pm     Reply with quote

Hi guys, I kinda got the code working with the mods you guys did - thanks a lot! I'am running in to other problems now further down the line. Some of the function declarations are causing problems "FRESULT f_open (FIL*, const char*, BYTE);" is giving warning "Expecting identifier" I suspect it has something to do with a pointer pointing to a constant?? Here is the header of the function:
Code:


FRESULT f_open (
  FIL *fp,      /* Pointer to the blank file object */
  const char *path, /* Pointer to the file name */
  BYTE mode     /* Access mode and file open mode flags */
)
{


As always any help is appreciated.

PS. If anyone has used this code (FAT FS) please let me know if you experiencing same problems

Kind Regards
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 7:21 am     Reply with quote

I've had problems with "const char *" in a function def or prototype. The compiler should accept this - it just says your code can't modify char(s). Oh, well . . .

Ken
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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