C Language Compiler: Psoc Designer
C Language Compiler: Psoc Designer
Copyright Information Copyright 2000-2001 Cypress MicroSystems, Inc. All rights reserved. Copyright 1999-2000 ImageCraft Creations Inc. All rights reserved. Copyright 1981-1998 Microsoft Corp. as related to Windows All third-party products referenced herein are either trademarks or registered trademarks of their respective companies. The information contained herein is subject to change without notice.
Page 1 of 37
After generating your device configuration, click the Application Editor icon in the toolbar to access the C compiler and pre-configured source files. The source tree of project files appears in the left frame. The folders can be expanded to reveal the files. Double-click individual files to open and edit them in the main window. Click File >> New to add .c files to your project.
Click a hyperlink to reference key material: Notation Standards Accessing/Enabling the Compiler Compiler Files Basics Functions Processing Directives (#s) Librarian Status Window Messages
Bottom Line
10 seconds
The PSoC Designer C Compiler is an extra tool you can use to customize the functionality you desire into the M8C microprocessor. Times up Now get to work.
Page 2 of 37
Documentation Conventions
Following, are easily identifiable conventions used throughout the PSoC Designer suite of product documentation. Convention
Times New Roman Size 10-12
Usage Displays input: //--------------------------------------------------------------// Sample Code // Burn some cycles //--------------------------------------------------------------void main() { char cOuter, cInner; for(cOuter=0x20; cOuter>0; cOuter--) { for(cInner=0x7F; cInner>0; cInner--) { } } } Displays file locations: c:\Program Files\Cypress MicroSystems\PSoC Designer\tools Displays file names: projectname.rom Displays keyboard commands: [Enter] Displays menu paths: Edit >> Cut
Page 3 of 37
Notation Standards
Following, are notation standards used throughout the PSoC Designer suite of product documentation. Virtual Registers: Virtual registers _r0, _r1, _r2, _r3, _r4, _r5, _r6, _r7, _r8, _r9, _r10, _r11, _rX, _rY occupy 14 bytes of RAM. These locations are used for temporary data when using the C language compiler. Currently, these virtual memory register locations are allocated even if the source for the M8C in PSoC Designer is written only in assembly language.
Page 4 of 37
Table of Contents
Two Minute Overview ...................................................................... 2
Quick-start summary for advanced users who are ready to dive in.
Section 1. Introduction.................................................................... 7
Describes purpose of this guide and overviews section and product information.
1.1. Purpose..................................................................................................... 7 1.2. Section Overview ...................................................................................... 7 1.3. Product Updates ....................................................................................... 8 1.4. Support ..................................................................................................... 8
2.1. Enabling the Compiler............................................................................... 9 2.2. Accessing the Compiler ............................................................................ 9 2.3. Menu Options.......................................................................................... 10
4.1. Types ...................................................................................................... 12 4.2. Operators ................................................................................................ 13 4.3. Expressions ............................................................................................ 14 4.4. Statements.............................................................................................. 14 4.5. Pointers................................................................................................... 15 4.6. Re-entrancy ............................................................................................ 15 4.7. Processing Directives (#s)...................................................................... 15
Page 5 of 37
6.1. 6.2. 5.3. 6.4. 6.5. 6.6. 6.7. 6.8. 6.9. 6.10. 6.11. 6.12. 6.13. 6.14.
Accessing M8C Features ................................................................. 18 Addressing Absolute Memory Locations .......................................... 18 Assembly Interface and Calling Conventions................................... 18 Bit Twiddling..................................................................................... 19 Inline Assembly................................................................................ 20 Interrupts.......................................................................................... 20 IO Registers...................................................................................... 20 Long Jump/Call................................................................................. 20 Memory Areas.................................................................................. 21 Program and Data Memory Usage .................................................. 21 Program Memory as Related to Constant Data ............................... 22 Stack Architecture and Frame Layout.............................................. 23 Strings.............................................................................................. 23 Virtual Registers............................................................................... 24
Section 7. Linker............................................................................ 25
Describes C Compiler linker functionality.
7.1.
Linker Operations.................................................................................
8.1.
Librarian ........................................................................................... 26
Page 6 of 37
Section 1. Introduction
1.1. Purpose
The purpose of the PSoC Designer: C Language Compiler User Guide is reference for using a C language compiler within the parameters of PSoC Designer. The PSoC Designer C Compiler compiles each .c source file to an M8C assembly file. The PSoC Designer Assembler then translates each assembly file (either those produced by the compiler or those that have been added) into a relocatable object file, .o. After all the files have been translated into object files, the builder/linker combines them together to form an executable file. This .rom file is then downloaded to the emulator where it is debugged to perfect M8C design functionality. For comprehensive details on system use and assembly language, see: PSoC Designer: Integrated Development Environment User Guide PSoC Designer: Assembly Language User Guide
Together, these three user guides complete the PSoC Designer documentation suite.
Page 7 of 37
Section 5. Functions
Lists C compiler functions that are compatible within PSoC Designer parameters. Discusses C compiler linker options deployed within PSoC Designer. Discusses C compiler library functions used within PSoC Designer. Overviews C compiler command line features that can be used strictly within the constraints of PSoC Designer. Directs users to available reference resources.
Section 6. Linker
Section 7. Librarian
1.4. Support
Support for the C Language Compiler is bundled into a PSoC Designer Support Contract. For details, see the PSoC Designer: Integrated Development Environment User Guide.
Page 8 of 37
To access the Application Editor subsystem, click the Application Editor icon. This icon can be found in the subsystem toolbar.
Subsystem Toolbar
Such features include adding and modifying .c project files, both of which are described ahead in brief, and in the PSoC Designer: Integrated Development Environment User Guide in detail.
Page 9 of 37
Following, is a description of the menu options available for use with the compiler:
Icon Option Compile/Assemble Menu Build >> Compile/Assemble Build >> Build Build New File Open File Indent Outdent Comment Uncomment Toggle Bookmark File >> New File >> Open Shortcut [Ctrl] [F7] Feature Compiles/assembles the most prominent open, active file (.c or .asm) Builds entire project and links applicable files Adds a new file to the project Opens an existing file in the project Indents specified text Outdents specified text Comments selected text Uncomments selected text Toggles the bookmark: Sets/removes user-defined bookmarks used to navigate source files Clears all user-defined bookmarks Goes to next bookmark Goes to previous bookmark Find specified text Replace specified text Repeats last replace Set options for editor Undo last action Redo last action
Clear Bookmark Next Bookmark Previous Bookmark Find Text Replace Text Repeat Replace Set Editor Options Undo Redo
Page 10 of 37
Page 11 of 37
4.1. Types
PSoC Designer C Compiler supports the following standard data types: All types support the signed and unsigned type modifiers.
Type char Int Bytes 1 2 Description A single byte of memory that defines characters Used to define integer numbers Range unsigned 0255 signed -128127 unsigned 065535 signed -3276832767 unsigned 065535 signed -3276832767 unsigned 04294967295 signed -21474836482147483647 1.175e-383.40e+38 1.175e-383.40e+38 065535
2 4 4 4 2
Standard type specifying 2-byte integers Standard type specifying the largest integer entity Single precision floating point number in IEEE format Single precision floating point number in IEEE format Used to define a list of aliases that represent integers.
All floating-point operations are supported in the PSoC Designer C Compiler. floats and doubles are in IEEE standard 32-bit format with 7-bit exponent and 23bit mantissa.
Page 12 of 37
4.2. Operators
Following is a list of the most common operators supported within the PSoC Designer C Compiler. Operators with a higher precedence are applied first. Operators of the same precedence are applied right to left. Use parenthesis where appropriate to prevent ambiguity.
P. 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 4 4 5 5 6 6 6 6 7 7 8 9 10 11 12 13 14 14 14 14 14 14 14 14 14 14 14 15 Op. ++ -[] () . -> sizeof ++ -& * + ~ !
(declaration)
* / % + << >> < <= > >= == != & ^ | && || ?: = *= /= %= += -= <<= >>= &= ^= |= ,
Function Postincrement Postdecrement Subscript Function Call Select Member Point at Member Sizeof Preincrement Predecrement Address of Indirection Plus Minus Bitwise NOT Logical NOT Type Cast Multiplication Division Modulus Addition Subtraction Left Shift Right Shift Less Less or Equal Greater Greater or Equal Equals Not Equals Bitwise AND Bitwise Exclusive OR Bitwise Inclusive OR Logical AND Logical OR Conditional Assignment Multiply Assign Divide Assign Remainder Assign Add Assign Subtract Assign Left Shift Assign Right Shift Assign Bitwise AND Assign Bitwise Exclusive OR Assign Bitwise Inclusive OR Assign Comma
Group
Description
Unary
++ a -- a &a *a +a -a ~a !a a*b a/b a%b a+b a-b a << b a >> b a<b a <= b a>b a >= b a == b a != b a&b a^b a|b a && b a || b c?a:b a=b a *= b a /= b a %= b a += b a -= b a <<= b a >>= b a &= b a ^= b a |= b a,b
1s complement of a (declaration)a a multiplied by b a divided by b Remainder of a divided by b a plus b a minus b Value of a shifted b bits left Value of a shifted b bits right a less than b a less than or equal to b a greater than b a greater than or equal to b
Page 13 of 37
4.3. Expressions
PSoC Designer supports standard C language expressions.
4.4. Statements
PSoC Designer compiler supports the following standard statements: if else: Decides on an action based on if being true. switch: Compares a single variable to several possible constants. If the variable matches one of the constants, a jump is made. while: Repeats (iterative loop) a statement until the expression proves false. do: Same as while, only the test runs after execution of statement, not before. for: Executes a controlled loop. goto: Transfers execution to a label. continue: Used in a loop to skip the rest of the statement. break: Used with a switch or in a loop to terminate the switch or loop. return: Terminates the current function. struct: Used to group common variables together. typedef: Declares a type.
Page 14 of 37
4.5. Pointers
A pointer is a variable that contains an address that points to data. It can point to any data type (i.e., int, float, char, etc.). A generic (or unknown) pointer type is declared as void and can be freely cast between other pointer types. Function pointers are also supported. Due to the nature of the Harvard architecture of the M8C, a data pointer may point to data located in either data or program memory. To discern which data is to be accessed, the const qualifier is used to signify that a data item is located in program memory. See Program Memory as Related to Constant Data in section 6. Pointers require 2 bytes of memory storage to account for the size of both the data and program memory.
4.6. Re-entrancy
Currently, there are no pure re-entrant library functions. It is possible, however, to create a re-entrant condition that will compile and build successfully. Due to the constraints that a small stack presents, re-entrant code is not recommended.
Page 15 of 37
Section 5. Functions
In this section you can reference compiler functions supported within PSoC Designer. PSoC Designer C Compiler functions use arguments and always return a value. All C programs must have a function called main(). Each function must be self-contained in that you may not define a function within another function or extend the definition or a function across more than one file. It is important to note that the compiler generates inline code whenever possible. However, for some C constructs, the compiler generates calls to low level routines. These routines are prefixed with two underscores and should not be called directly by the user.
strcpy strcmp
char *strcpy(char *s1, char *s2) char *cstrcpy(char *s1, const char *s2) int strcmp(char *s1, char *s2) int cstrcmp(char *s1, const char *s2)
string.h string.h
strlen
size_t strlen(char *s) size_t cstrlen(const char *s) char *strcat(char *s1, const char *s2) char *cstrcat(char *s1, const char *s2)
string.h
strcat
string.h
You can also view these functions at a command prompt window by typing: :\Program Files\Cypress MicroSystems\PSoC Designer\tools> ilibw t libcm8c.a
Page 16 of 37
Int,
First char in A and second in X MSB in X and LSB in A MSB in A and LSB in X First argument passed in A. Successive arguments are pointed to by X, where X is set up as a pointer to the remaining arguments. Typically, these arguments are stored on the stack X is set up as a pointer that points to the contiguous block of memory that stores the arguments. Typically, the arguments are stored on the stack. Same as above
Arguments that are pushed on the stack are pushed from right to left. The reference of returned structures reside in the A and X registers. If passed by value, a structure is always passed through the stack, and not in registers. Passing a structure by reference (i.e., passing the address of a structure) is the same as passing the address of any data item, that is, a pointer (which is 2 bytes). The following table reflects the set of #pragma fastcall conventions used for return value register assignments:
Return Type char int long pointer Return Register A X, A __r0..__r3 A, X Comment
Page 17 of 37
Page 18 of 37
This operator is useful for checking if certain bits are set. For example: if ((PORTA & 0x81) == 0) // check bit 7 and bit 1
Note that the parenthesis is needed around the expression of an & operator because it has lower precedence than the == operator. This is a source of many programming bugs in compiler programs. See Section 4. Complier Basics for the table of supported operators and precedence. a ^ b bitwise exclusive or This operator is useful for complementing a bit. For example, in the following case, bit 7 is flipped: PORTA ^= 0x80; ~a bitwise complement // flip bit 7
This operator performs a ones-complement on the expression. It is especially useful when combined with the bitwise and operator to turn off certain bits. For example: PORTA &= ~0x80; // turn off bit 7
Page 19 of 37
6.6. IO Registers
IO registers are specified using the following #pragma:
#pragma ioport LED:0x04; char LED;. LED = 1; // ioport is at I/O space 0x04 LED must be declared in global scope
6.7. Interrupts
All interrupt-level functions must be written in assembly language. Interrupt C functions are not supported.
Page 20 of 37
6.9.1. Data Memory data: This is the data area containing global and static variables, and strings. The initial values of the global variables are stored in the "idata" area and copied to the data area at startup time. bss: This is the data area containing "uninitialized" C global variables. Per ANSI C definition, these variables will get initialized to zero at startup time.
The job of the linker is to collect areas of the same types from all the input object files and concatenate them together in the output file. For further information, see Section 7. Linker.
Page 21 of 37
6.10.2. Data Memory The Data Memory is used for storing variables and the stack frames. In general, they do not appear in the output file but are used when the program is running. A program uses data memory as follows: [high memory] [stack frames] [global variables] [virtual registers] [low memory] It is up to you, the programmer, to ensure that the stack does not leak into the variable section. Otherwise, unexpected results will occur.
Page 22 of 37
Note that the C compiler does not require const data to be put in the read-only memory, and in a conventional architecture, this would not matter except for access rights. So, this use of the const qualifier is unconventional, but within the allowable parameters of the compiler. However, this does introduce conflicts with some of the standard C function definitions. For example, the standard prototype for strcpy is strcpy(char *dst, const char *src), with the const qualifier of the second argument signifying that the function does not modify the argument. However, under the M8C, the const qualifier would indicate that the second argument points to the program memory. For example, variables defined outside of a function body or variables that have the static storage class, have file storage class. If you declare local variables with the const qualifier, they will not be put into FLASH and undefined behaviors may result.
6.13. Strings
The compiler allocates all literal strings into program memory. Effectively, the type for a literal string is const char * and you must ensure that function parameters take the appropriate argument type.
Page 23 of 37
Page 24 of 37
Section 7. Linker
In this section you will learn how the linker operates within PSoC Designer.
Page 25 of 37
Section 8. Librarian
In this section you will learn the librarian functions of PSoC Designer.
8.1. Librarian
A library is a collection of object files in a special form that the linker understands. When your program references a librarys component object file directly or indirectly, the linker pulls out the library code and links it to your program. The library that contains supported C functions is located in the PSoC Designer working directory of c:\Program Files\Cypress MicroSystems\PSoC Designer\tools\libcm8c.a. There are times when you need to modify or create libraries. A command line tool called ilibw.exe is provided for this purpose. Note that a library file must have the .a extension. For further reference, see Section 7. Linker. 8.1.1. Compiling a File into a Library Module Each library module is simply an object file. Therefore, to create a library module, you need to compile a source file into an object file. To do this, open the file in the IDE and invoke the File >> Compile File To Object command. 8.1.2. Listing the Contents of a Library On a command prompt window, change the directory to where the library is, and give the command ilibw -t <library>. For example: ilibw -t libcm8c.a 8.1.3. Adding or Replacing a Module 1. Compile the source file into an object module. 2. Copy the library into the work directory. 3. Use the command ilibw -a <library> <module> to add or replace a module. ilibw creates the library file if it does not exist, so to create a new library, just give ilibw a new library file name. 8.1.4. Deleting a Module The command switch -d deletes a module from the library. For example, the following deletes crtm8c.o from the libcm8c.a library: ilibw -d libcm8c.a crtm8c.o ; delete
Page 26 of 37
9.2. Driver
The compiler driver handles all the details previously mentioned. It takes the list of files and compiles them into an executable file (which is the default) or to some intermediate stage (e.g., into object files). It is the compiler driver that invokes the compiler, assembler, and linker as needed. The compiler driver examines each input file and acts on it based on its extension and the command-line arguments given. .c files are C compiler source files and .asm files are assembly source files, respectively. The design philosophy for the IDE is to make it as easy to use as possible. The command line compiler, though, is extremely flexible. You control its behavior by passing command-line arguments to it. If you want to interface the compiler with PSoC Designer, note the following: Error messages referring to the source files begin with "!E file(line):.." To bypass the command line length limit on Windows 95/98/NT, you may put command-line arguments in a file, and pass it to the compiler as @file or @-file. If you pass it as @-file, the compiler will delete file after it is run.
Page 27 of 37
Page 28 of 37
Page 29 of 37
Page 30 of 37
(Preprocessor cont.)
Unknown preprocessor control line Unterminated #if/#ifdef/#ifndef Unterminated string or char const
C Compiler
Error/Warning expecting <character> literal too long IO port <name> cannot be redeclared as local variable IO port <name> cannot be redeclared as parameter IO port variable <name> cannot have initializer <n> is a preprocessing number but an invalid %s constant <n> is an illegal array size <n> is an illegal bit-field size <type> is an illegal bit-field type <type> is an illegal field type sizeof applied to a bit field addressable object required asm string too long assignment to const identifier assignment to const location cannot initialize undefined case label must be a constant integer expression cast from <type> to <type> is illegal in constant expressions cast from <type> to <type> is illegal conflicting argument declarations for function <name> declared parameter <name> is missing duplicate case label <n> duplicate declaration for <name> previously declared at <line> duplicate field name <name> in <structure> empty declaration expecting an enumerator identifier expecting an identifier extra default label extraneous identifier <id> extraneous old-style parameter list extraneous return value
Page 31 of 37
(C Compiler cont.)
field name expected field name missing found <id> expected a function ill-formed hexadecimal escape sequence illegal break statement illegal case label illegal character <c> illegal continue statement illegal default label illegal expression illegal formal parameter types illegal initialization for <id> illegal initialization for parameter <id> illegal initialization of extern <name> illegal return type <type> illegal statement termination illegal type <type> in switch expression illegal type array of <name> illegal use of incomplete type illegal use of type name <name> Initializer must be constant insufficient number of arguments to <function> integer expression must be constant Interrupt handler <name> cannot have arguments invalid field declarations invalid floating constant invalid hexadecimal constant invalid initialization type; found <type> expected <type> invalid octal constant invalid operand of unary &; <id> is declared register invalid storage class <storage class> for <id> invalid type argument <type> to sizeof invalid type specification invalid use of typedef left operand of -> has incompatible type left operand of . has incompatible type lvalue required missing <c> missing tag missing array size missing identifier missing label in goto missing name for parameter to function <name> missing parameter type missing string constant in asm missing { in initialization of <name> Operand of unary <operator> has illegal type operands of <operator> have illegal types <type> and <type> Overflow in value for enumeration constant
Page 32 of 37
(C Compiler cont.)
redeclaration of <name> previously declared at <line> redeclaration of <name> redefinition of <name> previously defined at <line> redefinition of label <name> previously defined at <line> size of <type> exceeds <n> bytes size of array of <type> exceeds <n> bytes syntax error; found too many arguments to <function> too many errors too many initializers too many variable references in asm string type error in argument <name> to <function>; <type> is illegal type error in argument <name> to <function>; found <type> expected <type> type error Unclosed comment undeclared identifier <name> undefined label undefined size for <name> undefined size for field <name> undefined size for parameter <name> undefined static <name> Unknown #pragma Unknown size for type <type> unrecognized declaration unrecognized statement
Page 33 of 37
Assembler
Error/Warning [ addressing mode must end with ] ) expected .if/.else/.endif mismatched <character> expected EOF encountered before end of macro definition No preceding global symbol absolute expression expected badly formed argument, ( without a matching ) branch out of range cannot add two relocatable items cannot perform subtract relocation cannot subtract two relocatable items cannot use .org in relocatable area character expected comma expected equ statement must have a label identifier expected, but got character <c> illegal addressing mode illegal operand input expected label must start with an alphabet, . or _ letter expected but got <c> macro <name> already entered macro definition cannot be nested maximum <#> macro arguments exceeded missing macro argument number multiple definitions <name> no such mnemonic <name> relocation error target too far for instruction too many include files too many nested .if undefined mnemonic <word> undefined symbol unknown operator unmatched .else unmatched .endif
Page 34 of 37
Linker
Error/Warning Address <address> already contains a value cant find address for symbol <symbol> cant open file <file> cant open temporary file <file> cannot open library file <file> cannot write to <file> definition of builtin symbol <symbol> ignored ill-formed line <%s> in the listing file multiple define <name> no space left in section <area> redefinition of symbol <symbol> undefined symbol <name> unknown output format <format>
Page 35 of 37
Index
Accessing M8C Features .......................... 18 Accessing the Compiler............................... 9 Addressing Absolute Memory Locations... 18 Appendix A ................................................ 30 Assembly Interface and Calling Conventions .............................................................. 18 Bit Twiddling .............................................. 19 Character Type Functions ......................... 16 Compilation Process ................................. 27 Compiler Arguments.................................. 28 Documentation Conventions ....................... 3 Driver ......................................................... 27 Enabling the Compiler ................................. 9 Expressions ............................................... 14 Inline Assembly ......................................... 20 Interfacing C and Assembly ...................... 17 IO Registers .............................................. 20 Librarian..................................................... 26 Library Descriptions................................... 11 Library Functions....................................... 16 Linker Operations ...................................... 25 Long Jump/Call ......................................... 20 Memory Areas ........................................... 21 Menu Options ............................................ 10 Notation Standards...................................... 4 Operators .................................................. 13 Pointers ..................................................... 15 Processing Directives (#s) ........................15 Product Upgrades........................................8 Program and Data Memory Usage............21 Program Memory as Related to Constant Data.......................................................22 Purpose .......................................................7 Re-entrancy ...............................................15 Section 1. Introduction..............................7 Section 2. Accessing the Compiler .........9 Section 3. Compiler Files........................11 Section 4. Compiler Basics ....................12 Section 5. Compiler Functions...............16 Section 6. Additional Considerations....18 Section 7. Linker......................................25 Section 8. Librarian .................................26 Section 9. Command Line Compiler Overview ..............................................27 Section Overview.........................................7 Stack Architecture and Frame Layout .......23 Startup File ................................................11 Statements ................................................14 Strings........................................................23 Support ........................................................8 Two Minute Overview ..................................2 Types .........................................................12 Virtual Registers ........................................24
Page 36 of 37