how to use hc08 cross c compiler

39
How to Use HC08 Cross C Compiler Hiware C

Upload: clem

Post on 13-Jan-2016

72 views

Category:

Documents


1 download

DESCRIPTION

How to Use HC08 Cross C Compiler. Hiware C. Developed by Dennis Ritchie in Bell Lab for Unix on PDP-11 “The C Programming Language 2 nd Ed” (1988) Kernighan &Ritchie The ANSI C Standard Defined the Syntax and Semantics Standard Defined the Standard C library. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: How to Use HC08 Cross C Compiler

How to Use HC08 Cross C Compiler

Hiware C

Page 2: How to Use HC08 Cross C Compiler

“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

Page 3: How to Use HC08 Cross C Compiler

Hello World!

Page 4: How to Use HC08 Cross C Compiler

C and Unix

• C is originally from Unix (Linux gcc)

• Unix (Linux ) is written with C

Page 5: How to Use HC08 Cross C Compiler

What Unix (Linux) Does?

• Process management (Kernel, but not Real Time)

• Memory Management

• Files Management

• I/O Management

Page 6: How to Use HC08 Cross C Compiler

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

Page 7: How to Use HC08 Cross C Compiler

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

Page 8: How to Use HC08 Cross C Compiler

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)

Page 9: How to Use HC08 Cross C Compiler

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

Page 10: How to Use HC08 Cross C Compiler

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

Page 11: How to Use HC08 Cross C Compiler

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

Page 12: How to Use HC08 Cross C Compiler

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.

Page 13: How to Use HC08 Cross C Compiler

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

Page 14: How to Use HC08 Cross C Compiler

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

Page 15: How to Use HC08 Cross C Compiler

Start a Cross C

Start from simplest Program (1)

Function_a()

{

};

You got .o file, in which only one machine code of:

RTS ;

Page 16: How to Use HC08 Cross C Compiler

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!*/

Page 17: How to Use HC08 Cross C Compiler

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;}

Page 18: How to Use HC08 Cross C Compiler

/* test.c */

main(){

add(3,5);

}

int add(int a, int b){return(a+b);}

The Relationship between C and ASMParameters transferred through Stack

Page 19: How to Use HC08 Cross C Compiler

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'

Page 20: How to Use HC08 Cross C Compiler

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?

Page 21: How to Use HC08 Cross C Compiler

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 **/

Page 22: How to Use HC08 Cross C Compiler

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++; }}

Page 23: How to Use HC08 Cross C Compiler

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++; } }

Page 24: How to Use HC08 Cross C Compiler

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

Page 25: How to Use HC08 Cross C Compiler

Generate Motorola S Code

S00D0000746F776572732E616273EAS123F0009BCCF4190000F30E0000000011570001F01A0000F026F01EF02010000058000068S109F020FFFF00000000E8S105FFFEF419F0S123F028A7FECE1054898ACE1055650000931F9EEF028B869EE7019F8B8848594859898A63S123F04897D6100BC71055D6100AC71054654F8395E601FEA702818789A7FA9E6F049E6F2B………………………………….………………………………….………………………………….S123F3A8F014F7205395F687EE018AE6019EE706F69EE705AD509EEE02878AE6019EE7044AS123F3C8F69EE703AD4095F795F687EE018AF68795E60387EE048A86F7956C0326026C028AS123F3E86C0126017C6D0526026A046A05E60487EE058A65000092D095F687EE018AE60153S123F408FA26A2A70A819FAB029EE7048B86A90081C6F005A502260DC6F00CB7FEC6F00D0DS116F428B7FF55FE94CDF331CEF006898ACEF007FD20DEA8S105F0260000E4S903F0000C

Page 26: How to Use HC08 Cross C Compiler

MC68HC08 C编译器编程范例

(实现简单的端口输出功能)

Page 27: How to Use HC08 Cross C Compiler

步骤一 . 建立工程

Page 28: How to Use HC08 Cross C Compiler

步骤二 . 编写源文件

Page 29: How to Use HC08 Cross C Compiler

/************************************************ 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 //标志代码段的开始

Page 30: How to Use HC08 Cross C Compiler

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

}}

Page 31: How to Use HC08 Cross C Compiler

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;}

}

Page 32: How to Use HC08 Cross C Compiler

void interrupt 2 Int_Event(void) //IRQ中断服务{

PORTA=0xFE; //PORTA复位asm {

lda #0x05 //清除中断标志sta 0x1d

}}

void dummy(void) //其他无用中断的服务程序{

asm {rti

}}

Page 33: How to Use HC08 Cross C Compiler

步骤三 . 编译 (COMPILE)

Page 34: How to Use HC08 Cross C Compiler

步骤四 . 链接 ( LINKER

)

Page 35: How to Use HC08 Cross C Compiler

## 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…...

Page 36: How to Use HC08 Cross C Compiler

MAKE ( 第三、四步的综合 )

Page 37: How to Use HC08 Cross C Compiler

#### fibo.mak文件##

fibo.abs : fibo.prm fibo.o $(LINK) fibo.prm fibo.o : fibo.c $(COMP) $(FLAGS) fibo.c

Page 38: How to Use HC08 Cross C Compiler

步骤五 . 调试 ( DEBUG )

Page 39: How to Use HC08 Cross C Compiler

步骤七 . 写入芯片 ( BURNER )