Download - How to Use HC08 Cross C Compiler
How to Use HC08 Cross C Compiler
Hiware C
“The C Programming Language” (1988)
• Developed by Dennis Ritchie in Bell Lab for Unix on PDP-11
• “The C Programming Language 2nd Ed” (1988)
Kernighan &Ritchie
• The ANSI C Standard– Defined the Syntax and Semantics Standard
– Defined the Standard C library
Hello World!
C and Unix
• C is originally from Unix (Linux gcc)
• Unix (Linux ) is written with C
What Unix (Linux) Does?
• Process management (Kernel, but not Real Time)
• Memory Management
• Files Management
• I/O Management
How is the I/O in Unix ?
• Everything is Unix is to be seen as a file
• Any I/O is seen as a file:
• If you want to use an I/O, you have to:– Mount the device (data flow or data block)
– Open the file ( Open for Read or Write)
• Memory buffer
– Access the file (R/W)
– Close the file
• Release the buffer
I/O in Cross C for Embedded System
• I/O is not the part of ANSI C
• I/O can be part of OS
• Write Hardware Independent Part of an application only
• Because normally no file sys in an Embedded System, I/Os can be treated as tasks
RAM & ROM are limited resource
need memory management & code/data optimization
memory map & system resources various for different MCUs
unique memory map header file & emulator personality file for every MCU
variables are stored in RAM, while codes & constant tables are stored in ROM
Cross C Compiler (1)
Cross C Compiler (2)
system resources(variables, I/Os & functional modules) should be initialized during startup
mostly include real time applications
executable codes are NOT relocatable
usually, functions cannot be re-entried
C-compiler Architecture
Compiler
Directives Parser
Assembler Code Generator
Assembler Code Optimizer
Relocatable Obj. Code Generator
Absolute Exe.Code Generator
Executable Code toS-19 Code Converter
Absolute ListingGenerator
Object Code Libraries(system & user)
DebuggingPre-processor
Linker UtilitiesDebugger
C-Level & ASMTrace
H/W & S/W Breakpoints
Bus State Analyzer
Memory & RegistersMonitor & Modify
Assembler(relocatable)
In-line Assembler Macro Definition Compile Directives
Linker Command file Integrated Environmentfor project edit, compile,link, debug, ...
Commands & macros Activities log file
Source Files for HC08 C Program
Startup File(Reset entry, variable & stack initialization, program exit, …)
Program Files(+ Libraries)
MCU Memory Map Header File
MCU Vector Table Header File
Macro Definition Header File
Function & Variable Declaration Header File
I/Os & Registers
RAM(Page 0)
RAM(non-page 0)
ROM(internal)
External RAM[optional]
Reset & InterruptVector Table(ROM)
stack
MCU Memory Map
Variables(@tiny).bsct seg (init. data).ubsct seg(non-init. data)
Variables(@near).data seg(init. data).bss seg(non-init. data)
Functions.text seg(code)
Constants.const seg(data)
Constants[Pointers].text seg(code)
Variables(@<addr>)
Variables(@near).data seg(init. data).bss seg(non-init. data)
Main Pgm. File
ISR Files
Typ. Memory Usage
Fcn Module Files
Section-to-Segment Mapping
.text executable code ROM void main();
.bsct initialized data(page 0) RAM(page 0)@tiny char table_ptr = 0;
.ubsct non-init. data(page 0) RAM(page 0)@tiny int I, j, k;
.data initialized data RAM @near char tb_cnt = 5;
.bss non-initialized data RAM @near float result;
.const constant ROM @near const char step_table[5]=
{ 0, 10, 20, 30, 40 };
Memory Segment
Program Section ExampleMemory Type
Usually, the above mapping is assumed by the compiler, and cannot be overrided.
General Cross C compiler
• C_start_up file:– Initial Stack Pointer ;(Macro)– Initial System – Call main()– Jump Exit() ;return to Monitor
• Simple than ANSI C– No Command line parameters,
– No input output redirection – No initial variable
HC08 Demo Guide
EDIT
Compiler
Link
Burn
Down Load
Debugging
Simulator
Assembler
Dis assembler
.C .h
.O
.abs
.S19
default.ini Working PathProject.env Windows ShellProject.prm Hardware Parameters for linkProject.map Memory Location for Debug
Start a Cross C
Start from simplest Program (1)
Function_a()
{
};
You got .o file, in which only one machine code of:
RTS ;
Start from simplest Program (2)
Main()
{
}
You got more code than other functions
Man()
{ printf (“\n Hello World!”)
} /* in most case, it does not work!*/
68HC08 C Compiler
How to insert ASM in C:
#define HALT {__asm CLRA; __asm SWI ! {A}, {A+HX+SR};}#define HALTX(x) {__asm LDA x; __asm SWI ! {A}, {A+HX+SR};}#define HALT_AND_QUIT HALTX(#32)#define EnableInterrupts {__asm CLI;}#define DisableInterrupts {__asm SEI;}
How to define hardwire address:In your .h file:#define Input_port_A (*(unsigned char *) 0x1800)In your .c program:Main(){Input_port_A = 5;}
/* test.c */
main(){
add(3,5);
}
int add(int a, int b){return(a+b);}
The Relationship between C and ASMParameters transferred through Stack
3: main()
4: {
5:
6: add(3,5);
7:
450003 LDHX #0x0003
89 PSHX 8B PSHH ;high byte of the
3
A605 LDA #0x055F CLRX ;high byte of the 5
CD0000 JSR add
A702 AIS #2
8: }
81 RTS
10: int add(int a, int b) 11: {
87 PSHA ;low byte of the b
89 PSHX ;high byte of the b 12: return(a+b);9EE606 LDA 6,SP ;low byte of the a
9EEB02 ADD 2,SP ;low byte of the b
87 PSHA ;low of the result
9F TXA ;hi of the b
9EE906 ADC 6,SP ;hi of the a
97 TAX ;result hi in X
86 PULA ;result low in A
A702 AIS #2 81 RTS
Decoding File: ‘ .o'
C and ASM
• Parameters Transfer between C and ASM
– Data transferred with Stack– Where is the first parameter?– Where is the returned value?– What is the order of of Parameters in the
stack?
Demo C Program
/* demo for the usage of a virtual Real Time Interrupt Timer */
#define TIMER_IC 0x80 #define TIMER_OC 0x40#define TIMER_TO 0x20
#define TIMER_CONTROL (*((char*)0x12))#define TIMER_STATUS (*((char*)0x13))#define TIMER_CNT_MSB (*((char*)0x14))#define TIMER_CNT_LSB (*((char*)0x15)) /** One may read the counter MSB first. Then LSB is stored in temporary place, and next read will reveal this value. **/#define TIMER_CMP_MSB (*((char*)0x16))#define TIMER_CMP_LSB (*((char*)0x17)) /** Compare value. Interrupt if ctr == cmp && TIMER_OC set **/
typedef void (*Vector)(void);
#define T_INT 16#define T_VECTOR (*((Vector*)(0xFFFE - (T_INT << 1))))/*#pragma DATA_SEG SHORT _DATA_ZEROPAGE*/int counter, ticks, time;
#pragma TRAP_PROC
void TimerInterrupt(void){ if (TIMER_STATUS & TIMER_OC) { /** also clears status reg **/ ticks++; }}
void TimerInit(void){ TIMER_CMP_MSB = 1; TIMER_CMP_LSB = 0; TIMER_CONTROL |= TIMER_OC; T_VECTOR = TimerInterrupt;}void main(void) { int tick0; TimerInit (); counter = ticks = time = 0; asm CLI; for (;;) { if (counter == 5000) { time = ticks - tick0; counter = 0; tick0 = ticks; } counter++; } }
LINK towers.absNAMES towers.o start08.o ansi.lib ENDSECTIONS Z_RAM = READ_WRITE 0x0080 TO 0x00FF; MY_RAM = READ_WRITE 0x1000 TO 0x7FFF; MY_ROM = READ_ONLY 0xF000 TO 0xFEFF;
PLACEMENT DEFAULT_ROM INTO MY_ROM; DEFAULT_RAM INTO MY_RAM; _DATA_ZEROPAGE INTO Z_RAM;END STACKSIZE 0x100VECTOR 0 _Startup
Link File Towers.prmLet Compiler knows your Hardware
Generate Motorola S Code
S00D0000746F776572732E616273EAS123F0009BCCF4190000F30E0000000011570001F01A0000F026F01EF02010000058000068S109F020FFFF00000000E8S105FFFEF419F0S123F028A7FECE1054898ACE1055650000931F9EEF028B869EE7019F8B8848594859898A63S123F04897D6100BC71055D6100AC71054654F8395E601FEA702818789A7FA9E6F049E6F2B………………………………….………………………………….………………………………….S123F3A8F014F7205395F687EE018AE6019EE706F69EE705AD509EEE02878AE6019EE7044AS123F3C8F69EE703AD4095F795F687EE018AF68795E60387EE048A86F7956C0326026C028AS123F3E86C0126017C6D0526026A046A05E60487EE058A65000092D095F687EE018AE60153S123F408FA26A2A70A819FAB029EE7048B86A90081C6F005A502260DC6F00CB7FEC6F00D0DS116F428B7FF55FE94CDF331CEF006898ACEF007FD20DEA8S105F0260000E4S903F0000C
MC68HC08 C编译器编程范例
(实现简单的端口输出功能)
步骤一 . 建立工程
步骤二 . 编写源文件
/************************************************ Demo files: The Simplest Program Of 08GP32 ************************************************/#include <hidef.h> //C编译器提供的头文件
Byte PORTA @0x0000; //部分寄存器标号定义Byte DDRA @0x0004;Byte CONFIG1 @0x001E;Byte CONFIG2 @0x001F;Byte PCTL @0x0036;Byte PBWC @0x0037;Byte PMSH @0x0038;Byte PMSL @0x0039;Byte PMRS @0x003A;#define b_PLLON 5#define b_AUTO 7#define b_BCS 4
#pragma CODE_SEG DEFAULT //标志代码段的开始
void init_gp32(void) //初始化 08GP32{ asm {
sei LDA #0x01 //CONFIG设置 STA CONFIG1 LDA #0x3D STA CONFIG2
CLR PCTL //PLL锁相环的设置 MOV #0x01,PCTL MOV #0x04,PMSH
MOV #0xB0,PMSL MOV #0xFF,PMRS BSET b_PLLON,PCTL BSET 3,PCTL BSET 2,PCTL BSET b_AUTO,PBWC BSET b_BCS,PCTL
lda #0x05 //允许 IRQ中断sta 0x1d
}}
void main(void) { int i,j;
init_gp32(); asm {
cli //允许中断}PORTA=0xFE; //初始化 PORTADDRA=255;
while (TRUE) {
for(i=0;i<200;i++) //延时for (j=0;j<100;j++);
PORTA=PORTA<<1; //移位,产生走马灯效果PORTA++;if (PORTA==0xFF)
PORTA=0xFE;}
}
void interrupt 2 Int_Event(void) //IRQ中断服务{
PORTA=0xFE; //PORTA复位asm {
lda #0x05 //清除中断标志sta 0x1d
}}
void dummy(void) //其他无用中断的服务程序{
asm {rti
}}
步骤三 . 编译 (COMPILE)
步骤四 . 链接 ( LINKER
)
## fibo.prm文件
LINK fibo.abs NAMES fibo.o start08.o ansi.lib END
SECTIONS MY_RAM = READ_WRITE 0x0040 TO 0x023F; MY_ROM = READ_ONLY 0x8000 TO 0xFFFF;PLACEMENT DEFAULT_ROM INTO MY_ROM; DEFAULT_RAM INTO MY_RAM;END
STACKSIZE 0x100VECTOR 0 _StartupVECTOR 1 dummyVECTOR 3 dummy…...
MAKE ( 第三、四步的综合 )
#### fibo.mak文件##
fibo.abs : fibo.prm fibo.o $(LINK) fibo.prm fibo.o : fibo.c $(COMP) $(FLAGS) fibo.c
步骤五 . 调试 ( DEBUG )
步骤七 . 写入芯片 ( BURNER )