0% found this document useful (0 votes)
3 views16 pages

L5 - Embedded System Programming

The document discusses embedded system programming using the HCS12 microcontroller, focusing on cross development environments, tools, and low-level programming in C. It covers topics such as the history of the C programming language, cross development processes, and the use of the XCC12 compiler and its libraries. Additionally, it explains interrupt handling, assembly code integration, and function call conventions within the XCC12 environment.

Uploaded by

yenealem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

L5 - Embedded System Programming

The document discusses embedded system programming using the HCS12 microcontroller, focusing on cross development environments, tools, and low-level programming in C. It covers topics such as the history of the C programming language, cross development processes, and the use of the XCC12 compiler and its libraries. Additionally, it explains interrupt handling, assembly code integration, and function call conventions within the XCC12 environment.

Uploaded by

yenealem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Roger Johansson

Embedded system programming: HCS12

„ Cross developments environments and tools


„ XCC12 cross C compiler in particular
„ Low level programming in ’C’

Embedded system programming: HCS12 1

Roger Johansson

Low level programming in C

„ Short history
„ Cross development
„ Software libraries
„ Embedded assembly code
„ Interrupts

Embedded system programming: HCS12 2


Roger Johansson

The program language ’C’


”The philosophy of BCPL is not one of the
tyrant who thinks he knows best and lays
down the law on what is and what is not
allowed; rather, BCPL acts more as a servant
offering his services to the best of his ability
without complaint, even when confronted
with apparent nonsense. The programmer
Brian
is always assumed to know what he is Martin
doing and is not hemmed in by petty Kernighan
restrictions.” Richards

’ BCPL’ Basic Combined Programming Language – Dennis


(Martin Richards) 1966 Ritchie
’B’ - (Johnson/Kernighan) 1973
’C’ – (Kernighan/Ritchie) 1978
’ANSI C’ – 1983, first standardisation
’C++’ – (Stroustrup) 1986
’ISO’ – 1995, 1999

Embedded system programming: HCS12 3

Roger Johansson

Cross development

„ Development for one type of computer


(target computer) with another type of
computer (host computer)
„ Tools:
– Cross assembler
– Cross compiler
–…

Embedded system programming: HCS12 4


Roger Johansson

Cross development environment

HOST
HOST TARGET
TARGET
RS-232

Cross monitor/
Development debugger
tools

Requires a resident software debugger in the target

Embedded system programming: HCS12 5

Roger Johansson

Cross development environment

TARGET
TARGET
HOST
HOST
BDM
BDM adapter
adapter BDM
RS-232
Cross
Development
tools

Requires extra hardware (BDM-adapter)

Embedded system programming: HCS12 6


Roger Johansson

Cross development tools

Host
Hostsystem
system
•Cross
•Crosscompilers
compilers(C/C++/Java/Ada/Fortran...)
(C/C++/Java/Ada/Fortran...)
•Cross assemblers
•Cross assemblers
•Linker
•Linker
•Terminal
•Terminalemulation,
emulation,download
download
•Simulators
•Simulators
•Debug
•Debugadapters
adaptersand
andsoftware
software

Embedded system programming: HCS12 7

Roger Johansson

The compiler
Assembly source code

CC mc68000
mc68000
mc68020
mc68020
C++
C++
ASL
ASL mc68hc11
Ada
Ada mc68hc11
mc68hc12
mc68hc12

Java
Java I80386
I80386
Source code Pentium
PentiumIII
III

Embedded system programming: HCS12 8


Roger Johansson

The assembler

mc68hc12
mc68hc12 Assembler
Assembler o12
o12
Assembly source code

Object code with symbolic


debug information

Embedded system programming: HCS12 9

Roger Johansson

Linking
Motorola
MotorolaS-S-
o12
o12 format
format
Binary code for download to
target
o12
o12 Linker
Linker

Library ”script-file”
”script-file”
Library o12
module(s)
module(s) Instructions
Instructionsfor for o12
linking
linking

Library
Library
module
module

Embedded system programming: HCS12 10


Roger Johansson

Software development with XCC12

Embedded system programming: HCS12 11

Roger Johansson

XCC12 ’Project Manager’

’Project’
All source code files belonging to an application.
Result is an executable program

’Workspace’
A practical way of grouping related
projects.

Embedded system programming: HCS12 12


Roger Johansson

XCC12 ’Application’

Any application requires a startup procedure. The


procedure can often be standardised, i.e. several
applications use the same startup.

Embedded system programming: HCS12 13

Roger Johansson

XCC12 Compiler libraries


Performs standard operations wich cannot be handled by a single instruction
in CPU12. For example addition of 32-bit numbers.

* long int la,lb,lc;


* lc = la + lb;
ldd 2+_lb
ldx _lb
pshd
pshx
ldd 2+_la
ldx _la
pshd
pshx
jsr add32 ’add32’ a function in
leas 8,sp the precompiled
std 2+_lc library
stx _lc

Embedded system programming: HCS12 14


Roger Johansson

XCC12 Standard libraries

Three different
libraries with
common
functions

Embedded system programming: HCS12 15

Roger Johansson

CC12 ’segment’
The compiler distinguishes the generated code by placing it in one of four
possible segments.
text – this is the segment for machine instructions (executable code)
data – this is the segment for initialised variables. These variables have user
defined values when the program starts but they may be changed by the
program.
cdata - this segment is also for initialised variables. These variables have user
defined values when the program starts and they can not be changed by the
program.
bss – this segment provides space for variables which don’t have initialised
values

Embedded system programming: HCS12 16


Roger Johansson

CC12 ’segment’ - EXAMPLE


segment bss
define _var
int var; _var:
rmb 2
.stab sym G:var:_var:(typ11)
const int novar = 1;
segment cdata
.stab sym G:novar:_novar:(typ11)
int init_var = 2; define _novar
_novar:
fdb $1
segment data
.stab sym
G:init_var:_init_var:(typ11)
define _init_var
_init_var:
fdb $2

Embedded system programming: HCS12 17

Roger Johansson

XCC12 linker instructions

//
// [Link]
// script for QLD
// for XCC12 applications in RWM
//
...

Embedded system programming: HCS12 18


Roger Johansson

XCC12 ’[Link]’
// OPTIONS SECTION
-M // generate listfile <basename>.map

// define program entry for debugger


entry( __start )

group ( c , const_group )
{
abs
}
group( r , test_group)
{
startupseg,
text,
cdata,
data,
bss
}
group( r, interrupt_vectors )
{
vectors
}
layout
{
0x1000,0x3C80 <= test_group,
0x3F80,0x3FFF <= interrupt_vectors
}

Embedded system programming: HCS12 19

Roger Johansson

XCC12 Embedded assembly code


Operations, which cannot be accomplished with ’C’-syntax, requires
”embedded assembly code”:

/*
/* EXAMPLE
EXAMPLE */
*/
void
void main(
main( void
void ))
{{
__asm(
__asm( "" andcc
andcc #$F0");
#$F0"); /*
/* clear
clear condition
condition flags
flags */
*/
}}

NOTE: This is not a part of the ’C’-language and may be compiler dependent.

Embedded system programming: HCS12 20


Roger Johansson

XCC12 Embedded assembly code


Variables and parameters can be referenced by name in
embedded assembly code.

void
void callfunc(
callfunc( int
int aa
aa ,, int
int ab
ab )) void
void callfunc(
callfunc( int
int aa
aa ,, int
int ab
ab ))
{{ {{
aa
aa == 1; __asm("
1; __asm(" movw
movw #1,%a",
#1,%a", aa);
aa);
ab
ab == 2;
2; __asm(" movw #2,%a", ab);
__asm(" movw #2,%a", ab);
}} }}

Embedded system programming: HCS12 21

Roger Johansson

Address (hex) Function


Remapping interrupt FFFE RESET, Startvector
ROM
handlers FFFC Clock Monitor Fail, JMP [3FFE]
FFFA COP Watchdog Timeout, JMP [3FFC]

FFF8 Illegal Op Code, JMP [3FFA]


Main program FFF6 SWI, JMP [3FF8]
FFF4 XIRQ, JMP [3FF4]
IRQHandler FFF2 IRQ, JMP [3FF2]
FF8C ... JMP [3Fxx]
FFF0

IRQ

Service
interrupt Address (hex) Funktion
3FFE Not used
RWM
3FFC ClockFailHandler
3FFA COPFailHandler

3FF8 IllOpHandler

RTI 3FF6 SWIHandler


3FF4 XIRQHandler
3FF2 IRQHandler
3F8C ...
3FF0

Embedded system programming: HCS12 22


Roger Johansson

Specification of interrupt handler

__interrupt
__interrupt void
void name(
name( void
void );
);

Keyword ”__interrupt” is the first word in the


specification.
An interrupt handler can neither have parameters
nor return values.

Embedded system programming: HCS12 23

Roger Johansson

Interrupt vector table in XCC12


#pragma
#pragma DATA
DATA vectors
vectors
/* 3F80 - 3FFF
/* 3F80 - 3FFF */*/
__interrupt
__interrupt void(*irqvecs[])()
void(*irqvecs[])() == script file for the application
{{
.....
.....
PWMEShutdownHandler, .....
.....
PWMEShutdownHandler,
PortPIntHandler,
PortPIntHandler,
TimerCh1Handler, group(
group( r,
r, interrupt_vectors
interrupt_vectors ))
TimerCh1Handler, {{
TimerCh0Handler,
TimerCh0Handler,
RTIHandler, vectors
vectors
RTIHandler, }}
IRQHandler,
IRQHandler,
XIRQHandler, .....
.....
XIRQHandler, layout
SWIHandler,
SWIHandler, layout
IllopHandler, {{
IllopHandler, ....,
COPFailHandler,
COPFailHandler, ....,
ClockFailHandler, 0x3F80,0x3FFF
0x3F80,0x3FFF <=
<= interrupt_vectors
interrupt_vectors
ClockFailHandler, }}
ResetHandler
ResetHandler
};
};

Embedded system programming: HCS12 24


Roger Johansson

Interrupt vector table, static initialisation


__interrupt
__interrupt void
void IRQHandler(
IRQHandler( void
void ))
{{
/*
declarations
/* Interrupt
Interrupt service
service routine
routine */
*/
}}

#pragma
#pragma DATA
DATA vectors
vectors
__interrupt
__interrupt void
void *irqvecs
*irqvecs == IRQHandler;
IRQHandler;

group(
group( r,
r, interrupt_vectors
interrupt_vectors ))
script file {{
vectors
vectors
}}
layout
layout
{{
....,
....,
0x3FF2,0x3FF4
0x3FF2,0x3FF4 <=
<= interrupt_vectors
interrupt_vectors
}}

Embedded system programming: HCS12 25

Roger Johansson

Interrupt vector table, run time initialisation


__interrupt
__interrupt void
void IRQHandler(
IRQHandler( void
void ))
{{
/*
/* Interrupt
Interrupt service
service routine
routine */
*/
}}

#pragma
#pragma DATA
DATA vectors
vectors
__interrupt
__interrupt void
void *irqvecs;
*irqvecs;
#pragma
#pragma DATA
DATA data
data
void
void main(void)
main(void)
{{
irqvecs
irqvecs == IRQHandler;
IRQHandler;
...
...
}}

Embedded system programming: HCS12 26


Roger Johansson

Run time initialisation no vector table


__interrupt
__interrupt void
void IRQHandler(
IRQHandler( void
void ))
{{
/*
/* Interrupt
Interrupt service
service routine
routine */
*/
}}

void
void main(void)
main(void)
{{
*(unsigned
*(unsigned short
short *)
*) 0x3FF2
0x3FF2 == (unsigned
(unsigned short)
short) IRQHandler;
IRQHandler;
...
...
}}

alternatively...

#define
#define SET_IRQ_VECTOR(x,y)
SET_IRQ_VECTOR(x,y) *(unsigned
*(unsigned short
short *)
*) yy == (unsigned
(unsigned short)
short) xx
void
void main(void)
main(void)
{{
SET_IRQ_VECTOR(
SET_IRQ_VECTOR( IRQHandler
IRQHandler ,, 0x3FF2
0x3FF2 )) ;;
...
...
}}

Embedded system programming: HCS12 27

Roger Johansson

Function call conventions in XCC12


EXAMPLE:
EXAMPLE: Function
Function call
call
int
int a,b;
a,b;
main()
main()
{{
callfunc(
callfunc( a,b
a,b );
);
}}
-----------------------------------------------------------
-----------------------------------------------------------
The
The following
following assembly
assembly code
code is
is generated
generated by
by the
the compiler:
compiler:
...
...
** 0009
0009 || callfunc(
callfunc( a,b
a,b );
);
ldd _b
ldd _b
pshd
pshd
ldd
ldd _a
_a
pshd
pshd
jsr
jsr _callfunc
_callfunc

Embedded system programming: HCS12 28


Roger Johansson

Function conventions in XCC12


The
The following
following assembly
assembly codecode is
is
callfunc(
callfunc( aa aa ,, ab
ab )) generated by the compiler:
{{ generated by the compiler:
...
...
aa
aa == 1;
1; ** 0007
ab 0007 |callfunc(
|callfunc( aa aa ,, ab
ab ))
ab == 2;
2; segment text
segment text
}} define
define _callfunc
_callfunc
_callfunc:
_callfunc:
pshy
pshy
tfr
tfr sp,y
sp,y
old Y msb ** 0008 |{
0008 |{
Y and SP ** 0009
old Y lsb 0009 || aa
aa == 1;
1;
movw #1,4,y
movw #1,4,y
PC msb ** 0010
0010 || ab
ab == 2;
2;
PC lsb movw #2,6,y
movw #2,6,y
** 0011
0011 |}
|}
4 ab msb puly
puly
ab lsb rts
rts
6 aa msb decreasing
aa lsb address

Embedded system programming: HCS12 29

Roger Johansson

Background Debug Mode –BDM adapter

„ Single wire electrical interface


„ Used for debugging and programming the FLASH memory
in HCS12
Embedded system programming: HCS12 30
Roger Johansson

BDM adapter (”pod”)

Embedded system programming: HCS12 31

Roger Johansson

Summary

we
we have
have got
got aa brief
brief introduction
introduction to
to

„ Low level programming in ’C’


„ Cross developments environments and
tools in general
„ XCC12 cross C compiler in particular

which
which finishes
finishes today’s
today’s lecture
lecture ...
...

Embedded system programming: HCS12 32

You might also like