View previous topic :: View next topic |
Author |
Message |
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
Typedef conversion for ANSI compliance |
Posted: Mon Jan 28, 2008 1:05 pm |
|
|
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
|
|
Posted: Tue Jan 29, 2008 4:32 pm |
|
|
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
|
Typedef not working |
Posted: Tue Jan 29, 2008 11:58 pm |
|
|
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 |
Posted: Wed Jan 30, 2008 12:27 am |
|
|
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: 1934 Location: Norman, OK
|
|
Posted: Wed Jan 30, 2008 12:33 am |
|
|
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
|
Re: Typedef not working |
Posted: Wed Jan 30, 2008 12:34 am |
|
|
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:
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
|
Typedef update |
Posted: Wed Jan 30, 2008 3:23 am |
|
|
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
|
|
Posted: Wed Jan 30, 2008 8:32 am |
|
|
Break it apart - fix one piece at a time - then put the working parts back together. I think you can get there
Ken |
|
|
ak6dn
Joined: 08 Jan 2006 Posts: 23 Location: Saratoga, CA USA
|
Re: Typedef update |
Posted: Wed Jan 30, 2008 11:54 pm |
|
|
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
|
Re: Typedef update |
Posted: Thu Jan 31, 2008 11:31 am |
|
|
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
|
Typdef problems |
Posted: Thu Jan 31, 2008 11:47 pm |
|
|
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
|
|
Posted: Fri Feb 01, 2008 7:21 am |
|
|
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 |
|
|
|