introduction to embedded systems - gbv

5
¥ Introduction to Embedded Systems Interfacing to the Freescale 9S12 Jonathan W. Valvano University of Texas at Austin ; # CENGAGE Learning" Australia • Canada • Mexico • Singapore • Spain • United Kingdom • United States

Upload: others

Post on 10-May-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Embedded Systems - GBV

¥

Introduction to Embedded Systems Interfacing to the Freescale 9S12

Jonathan W. Valvano University of Texas at Austin

; # C E N G A G E Learning"

Australia • Canada • Mexico • Singapore • Spain • United Kingdom • United States

Page 2: Introduction to Embedded Systems - GBV

Contents

1 Introduction to Embedded Microcomputer Systems 1

1.1 Basic Components of an Embedded System 2

1.2 Applications Involving Embedded Systems 5

1.3 Flowcharts and Structured Programming 6

1.4 Concurrent and Parallel Programming 10

1.5 Product Development Cycle 12 1.6 Successive Refinement 17 1.7 Quality Design 19

1.7.1 Quantitative Performance Measurements 19 1.7.2 Qualitative Performance Measurements 19 1.7.3 Attitude 20

1.8 Debugging Theory 21 1.9 Tutorial 1. Getting Started 23

1.10 Homework Assignments 24

2 Introduction to Assembly Language Programming 27

2.1 Binary and Hexadecimal Numbers 2.2 Addresses, Registers, and Accessing

Memory 29 2.3 Assembly Syntax 32

2.3.1 Assembly Language Instructions 32 2.3.2 Pseudo Operation Codes 33

2.4 Simplified 9S12 Machine Language Execution 33

2.5 Simple Addressing Modes 36 2.5.1 Inherent Addressing Mode 37 2.5.2 Immediate Addressing Mode 37 2.5.3 Direct Addressing Mode 38 2.5.4 Extended Addressing Mode 38 2.5.5 Indexed Addressing Mode 39 2.5.6 PC Relative Addressing Mode 39

2.6 The Assembly Language Development Process 40

2.7 Memory Transfer Operations 41 2.8 Subroutines 43 2.9 Input/Output 45

2.9.1 Direction Registers 45 2.9.2 Switch Interface 46 2.9.3 LED Interface 47

27

2.10 Tutorial 2. Running with TExaS 2.11 Homework Assignments 52 2.12 Laboratory Assignments 55

3 Representation and Manipulation of Information 57

51

3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8

3.9

3.10 3.11 3.12 3.13

3.14 3.15

Precision 57 Boolean Information 59 8-bit Numbers 60 16-bit Numbers 64 Extended Precision Numbers 66 Logical Operations 66 Shift Operations 76 Arithmetic Operations: Addition and Subtractions 78 Arithmetic Operations: Multiplication and Divide 92 Character Information 97 Conversions 99 Debugging Monitor Using a LED Tutorial 3. Arithmetic and Logical Operations 102 Homework Assignments 104 Laboratory Assignments 110

102

4 9S12 Architecture 111 4.1 Introduction 111

4.1.1 Big and Little Endian 111 4.1.2 Memory-Mapped I/O 112 4.1.3 *I/0-Mapped I/O 113 4.1.4 *Segmented or Partitioned Memory 113 4.1.5 Memory Bus Cycles 114 4.1.6 Processor Architecture 116 4.1.7 I/O Port Architecture 118

4.2 *Understanding Software Execution at the Bus Cycle Level 121

4.3 9S12 Architecture Details 127 4.3.1 9S12C32 Architecture 126 4.3.2 9S12DP512 Architecture 129 4.3.3 9S12E128 Architecture 132 4.3.4 Operating Modes 134 4.3.5 Phase-Lock-Loop (PLL) 134

4.4 The Stack 135

VIII

Page 3: Introduction to Embedded Systems - GBV

• Contents ix

4.5 16-Bit Timer 137 4.6 *Memory Allocation 140 4.7 Performance Debugging 142

4.7.1 Instrumentation 142 4.7.2 Measurement of Dynamic Efficiency 142

4.8 Tutorial 4. Building a Microcomputer and Executing Machine Code 144

4.9 Homework Assignments 147 4.10 Laboratory Assignments 148

> _ э Modular

Programming 152 5.1 Modular Design 153

5.1.1 Definition and Goals 153 5.1.2 Functions, Procedures, Methods, and

Subroutines 155 5.1.3 Dividing a Software Task

into Modules 156 5.1.4 How to Draw a Call-Graph 158 5.1.5 How to Draw a Data

Flow Graph .160 5.1.6 Тор-Down Versus

Bottom-Up Design 160 5.2 Making Decisions 161

5.2.1 Conditional Branch Instructions 161 5.2.2 Conditional i f - t h e n Statements 163 5.2.3 Conditional i f - t h e n - e l s e

Statements 166 5.2.4 While Loops 166 5.2.5 For Loops 167

5.3 *Macros 168 5.4 *Recursion 171 5.5 Writing Quality Software 174

5.5.1 Assembly Language Style Guidelines 174

5.5.2 Comments 177 5.5.3 Inappropriate I/O and Portability 179

5.6 *How Assemblers Work 179 5.7 Functional Debugging 180

5.7.1 Stabilization 180 5.7.2 Single Stepping 180 5.7.3 Breakpoints Without Filtering 181 5.7.4 Conditional Breakpoints 181 5.7.5 Instrumentation: Print Statements 181

5.8 Tutorial 5a. Editing and Assembling 181

5.9 Tutorial 5b. Microcomputer-Based Lock 182

5.10 Homework Problems 186 5.11 Laboratory Assignments 190

6 Pointers and Data Structures 192

6.1 Indexed Addressing Modes used in Implement Pointers 193 6.1.1 Indexed Addressing Mode 193 6.1.2 Auto Pre/Post Decrement/Increment

Indexed Addressing Mode 195 6.1.3 Accumulator Offset Indexed Addressing

Mode 196 6.1.4 Indexed Indirect Addressing Mode 196 6.1.5 Accumulator D Offset Indexed Indirect

Addressing Mode 196 6.1.6 Post-Byte Machine Coded for Indexed

Addressing 196 6.1.6 Load Effective Address Instructions 197 6.1.7 Call-by-Reference Parameter Passing 198

6.2 Arrays 199 6.3 Strings 203 6.4 *Matrices 204 6.5 Structures 209 6.6 *Tables 210 6.7 Trees 212 6.8 Finite-State Machines with Statically

Allocated Linked Structures 216 6.8.1 Abstraction 216 6.8.2 Moore Finite-State Machines 217 6.8.3 Mealy Finite-State Machines 221 6.8.4 Functional Abstraction Within

Finite-State Machines 223 6.9 *Dynamically Allocated

Data Structures 226 6.9.1 *Fixed Block Memory Manager 229 6.9.2 *Linked List FIFO 230

6.10 *9S12 Paged Memory 232 6.11 Functional Debugging 235

6.11.1 Instrumentation: Dump Into Array without Filtering 235

6.11.2 Instrumentation: Dump Into Array with Filtering 236

6.12 Tutorial 6. Software Abstraction 237 6.13 Homework Assignments 238 6.14 Laboratory Assignments 244

7 Local Variables and Parameter Passing 256

7.1 Local Versus Global 256 7.2 Stack Rules 259 7.3 Local Variables Allocated

on the Stack 261 7.4 Stack Frames 262

Page 4: Introduction to Embedded Systems - GBV

x • Contents

7.5 Parameter Passing Using Registers, Stack and Global Variables 265 7.5.1 Parameter Passing in С 265 7.5.2 Parameter Passing in

Assembly Language 267 7.5.3 С Compiler Implementation of Local

and Global Variables 270 7.6 Tutorial 7. Debugging Techniques 274 7.7 Homework Problems 276 7.8 Laboratory Assignments 280

8 Serial and Parallel Port Interfacing 284

8.1 General Introduction to Interfacing 284

8.2 Serial Communications Interface, SCI 286 8.2.1 RS232 Protocol 286 8.2.2 Transmitting in Asynchronous Mode 287 8.2.3 Receiving in Asynchronous Mode 288 8.2.4 9S12 SCI Details 290

8.3 Synchronous Peripheral Interface, SPI 294 8.3.1 SPI Fundamentals 294 8.3.2 SPI Details 296 8.3.3 9S12DP512 Module Routing Register 298 8.3.4 8-bit DAC Interface 299

8.4 Scanned Keyboards 301 8.5 Parallel Port LCD Interface with the

HD44780 Controller 303 8.6 Binary Actuators 306

8.6.1 Interface 306 8.6.2 Electromagnetic

and Solid-State Relays 309 8.6.3 Solenoids 311

8.7 *Pulse-Width Modulation 311 8.8 *Stepper Motors 316 8.9 Homework Problems 320

8.10 Laboratory Assignments 321

9 Interrupt Programming and Real-Time Systems 326

9.1 I/O Sychronization 326 9.2 Interrupt Concepts 330

9.2.1 Introduction 330 9.2.2 Essential Components of Interrupt

Processing 332 9.2.3 Sequence of Events 333 9.2.4 9S12 Interrupts 334 9.2.5 Polled versus Vectored Interrupts 337 9.2.6 Pseudo-Interrupt Vectors 337

9.3 Key Wakeup Interrupts 338 9.4 Periodic Interrupt Programming 342 9.5 Real-Time Interrupt (RTI) 343 9.6 Timer Overflow, Output Compare and

Input Capture 345 9.6.1 Timer Features and Timer Overflow 345 9.6.2 Output Compare Interrupts 347 9.6.3 Input Capture Interrupts 350

9.7 Pulse Accumulator 352 9.8 *Direct Memory Access 356 9.9 Hardware Debugging Tools 357

9.10 Profiling 358 9.10.1 Profiling using a Software Dump

to Study Execution Pattern 359 9.10.2 Profiling using an Output Port 360 9.10.3 *Thread Profile 361

9.11 Tutorial 9. Profiling 363 9.12 Homework Problems 365 9.13 Laboratory Assignments 367

1 0 Numerical Calculations 368

10.1 Fixed-Point Numbers 368 10.2 *Extended Precision Calculations 371

10.2.1 Addition and Subtraction 372 10.2.2 Shift Operations 374 10.2.3 Mathematical Instructions

on the 9S12 375 10.2.4 Multiplication and Division 377 10.2.5 Table Lookup and Interpolation 380

10.3 Expression Evaluation 381 10.4 *IEEE Floating-Point Numbers 383 10.5 Tutorial 10. Overflow

and Dropout 387 10.6 Homework Problems 388 10.7 Laboratory Problems 392

1 1 Analog I/O Interfacing 398

11.1 Approximating Continuous Signals in the Digital Domain 398

11.2 Digital to Analog Conversion 399 11.3 Music Generation 400 11.4 Analog to Digital Conversion 403

11.4.1 9S12 ADC Details 403 11.4.2 ADC Data Formats 406 11.4.3 ADC Resolution 407

11.5 *Multiple Access Circular Queues 408 11.6 Real-Time Data Acquisition 409 11.7 *Control Systems 413

Page 5: Introduction to Embedded Systems - GBV

• Contents xi

11.8 Tutorial 11. Analog Input Programming 416

11.9 Homework Problems 417 11.10 Laboratory Assignments 419

12

12.1 12.2

12.3

Communication Systems 433

Introduction 433 Reentrant Programming and Critical Sections 434 Interthread Communication and Synchronization 438 12.3.1 Mailbox 439

12.3.4 Producer Consumer Problem 440

12.3.4 FIFO Queue Implementation 444

12.3.4 Double Buffer 446

Serial Port Interface using Interrupt Synchronization 438 *Distributed Systems. 447 *Design and Implementation of a Controller Area Network (CAN) 449 12.6.1 The Fundamentals of CAN 451 12.6.2 Details of the 9S12 CAN 454 12.6.3 9S12 CAN Device Driver 468 * Inter-Integrated Circuit (l2C) Interface 460 12.7.1 The Fundamentals of I2C 460 12.7.2 I2C Synchronization 464 12.7.3 9S12I2C Details 465 12.7.4 9S12I2C Single Master Example 469

12.8 Wireless Communication 470 12.9 Tutorial 12. Performance

Debugging 470 12.10 Homework Problems 472 12.11 Laboratory Assignments 476

12.4

12.5 12.6

12.7

A1.4 Running and Modifiing Existing Assembly Language Programs 490

A1.5 TExaS Editor 491 A1.6 Assembly Language Syntax 492

Al.6.1 Overall Structure 492 Al.6.2 Label Field 492 Al.6.3 Operation Field 493 A 1.6.4 Operand Field 493 A 1.6.5 Expressions 494 Al.6.6 Comment Field 496 Al.6.7 Assembly Listing and Errors 497 Al.6.8 Assembler Pseudo-Ops 499 Al.6.9 S-19 Object Code 503

A1.7 TExaSViewBox 505 A1.8 Microcomputer Interfacing

in TExaS 506

Appendix 2 Running on an Evaluation Board 508

Appendix 3 Systems Engineering 511 A3.1 Design for Manufacturability 511 A3.2 Battery Power 512

Glossary of Terms 514

Solutions Manual 529 Checkpoint Solutions 529 Tutorial Solutions 542

Index 550

Appendix 1 Embedded System Development Using TExaS 480

A1.1 Introduction to TExaS 480 A1.2 Major Components of TExaS 483 A1.3 Embedded System Design Process 486