0% found this document useful (0 votes)
7 views

UCSD Embedded Programming Concepts Letcture

Embedded

Uploaded by

lxl2818
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)
7 views

UCSD Embedded Programming Concepts Letcture

Embedded

Uploaded by

lxl2818
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
You are on page 1/ 19

Lesson 3:

Embedded
Programming
Concepts

1 © William Kim 2019 Introduction to Embedded Systems


Lecture Outline

• Embedded Programming Languages


• The Compile Process
• Data Types
• Registers
• Bit Manipulation

2 © William Kim 2019 Introduction to Embedded Systems


An Embedded Program…

• Optimally use resources


• Creates efficient program code
• Satisfies real-time constraints

3 © William Kim 2019 Introduction to Embedded Systems


Embedded Programming
Languages
• Some programming languages, in
order of popularity:
o C
o C++
o Java
o Python
o Ada
• This lecture will focus on Embedded
C
4 © William Kim 2019 Introduction to Embedded Systems
Why C?
• A “middle level” language
o Not quite Assembly and not quite C++
o Provides direct access to hardware without sacrificing the
benefits of higher level languages
• Compact, Fast and Efficient
o Simple without any of the added overhead of higher level
language features
o “Loose type checking” allows for easily and efficiently passing
around data
• Scalable for large projects
o Although some would prefer Object Oriented of C++
• Fairly portable as it is processor
independent
o The compiler is what maps the code to the processor

5 © William Kim 2019 Introduction to Embedded Systems


Why / Why Not C++?
• Contains all of the core features of C plus some
extras
• More secure than C
o Type checking
o String Literals, enumerated constants, etc
• Possibly more scaleable than C
o Object Oriented nature
o Overloaded functions and constructors
o Data Abstraction
• Not ideal for highly resource constrained
systems
o The added features have some overhead and reduce efficiency
o With modern technology this may not be as much of a concern
anymore

6 © William Kim 2019 Introduction to Embedded Systems


Dangers (Advantages) of C
• The C language assumes that the
programmer knows what they are doing
o Provides direct access to the hardware
• Memory
• Registers
• I/O
• Interrupts
o Allows pointers to be passed around and casted without type
checking
• Unmanaged and extremely efficient
o No exception handling
• Promotes safe coding practices to prevent exceptions from
occurring in the first place
o Multi-threading
• The programmer has to be very careful if developing a multi-
threaded application
7 © William Kim 2019 Introduction to Embedded Systems
The Embedded Compile
Process
• A compiler translates programs written in some
human-readable language into an equivalent set of
opcodes for a particular processor.
o The compiler is specific to the target processor
• A linker combines the compiled object files and
resolves all symbols
o The object files are incomplete by themselves
o Some of the internal variable and function references have not yet been
resolved.
• A locator assigns physical memory addresses to each
of the code and data sections within the relocatable
program
o Requires information about the memory on the target board as input
o Produces an output file that contains a binary memory image that can be
loaded into the target

8 © William Kim 2019 Introduction to Embedded Systems


The Embedded Compile
Process
• Each source file is
compiled or assembled
into an object file.
• The object files are linked
together to produce a
single object file, called
the relocatable program.
• Physical memory
addresses are assigned to
the relative offsets within
the relocatable program
(relocation).
• A binary executable is
created to be loaded onto
the target platform
9 © William Kim 2019 Introduction to Embedded Systems
Data Types
• The following Data Types are defined
in C:

10 © Instructor Name 2019 Name of Course Goes Here


Optimized Data Types
• The compilers job is to generate the most
efficient code possible
o For this reason, declaring something as simple as an int
can be 8, 16, 32, or 64 bits depending on the most efficient
size for the processor
o As a result, multiple compilers could use different sizes for
the same code
• In embedded programming, the compiler
“helping” can actually do the opposite
o In many circumstances the data size actually matters
o Bit manipulations, copying memory, etc…

11 © Instructor Name 2019 Name of Course Goes Here


Fixed Data Types
• To solve the Size Signed Unsigned
optimized data type 8 Bits int8_t uint8_t
problem it is 16 Bits int16_t uint16_t
recommended to 32 Bits int32_t uint32_t
define specific data 64 Bits int64_t uint64_t
types for specific
sizes on a processor
o C99 actually requires it to be
identified in the compiler
o Otherwise the user should
define it using typedefs of
char, short, long, long long (if
applicable)

12 © William Kim 2019 Introduction to Embedded Systems


Registers
• A register is used by the CPU to store
information on a temporary basis
o Store data to be processed
o Store address pointing to the data which is to be fetched
o Usually located within processors memory space or I/O
space
o Because of the location registers are generally very fast
• There are two main types of registers
o General Purpose Registers
o Special Purpose Registers

13 © Instructor Name 2019 Name of Course Goes Here


General Purpose
Registers
• Used to hold data values before and
after calculations
• These are used by the compiler for
loading and storing data and/or
addresses as well as performing
calculations

14 © William Kim 2019 Introduction to Embedded Systems


Special Purpose Registers
• Special Purpose Registers are designed
for a specific task.
• Contain the state of the program
• Includes the following:
o Instruction Register – holds instruction being executed
o Memory Data Register – holds data fetched from memory
o Memory Address Register – next address of memory to
fetch
o Program Counter – location of next instruction to execute
o Control/Status Register(s) – used to control various CPU
functionalities and check the status

15 © William Kim 2019 Introduction to Embedded Systems


Special Purpose Registers:
Memory Mapped I/O
• Device drivers communicate with
peripheral devices using device registers
o Sends commands / data to the device
o Retrieves status / data from the device
• Memory Mapped I/O maps device
registers to fixed addresses in memory
o This allows registers to be accessed in the same manner
on would access any location in memory
o Programs can use ordinary assignment operators to write /
read from the registers

16 © William Kim 2019 Introduction to Embedded Systems


Bit Manipulation
• Bit manipulation is used to manipulate
data and the contents of registers
o Particularly used for configuring and reading the status of
hardware
o Keep in mind that bit manipulation works on just that, bits
• So a uint8_t would execute on all 8 bits
• Supported bitwise operators
o AND (&)
o OR (|)
o NOT (~)
o XOR (^)
o Left Shift (<<)
o Right Shift (>>)

17 © Instructor Name 2019 Name of Course Goes Here


Bit Manipulation Examples
• Testing Bits A 0110 1001
o The AND operator is used to test if a bit is set B 1100 0011
• Setting Bits
o The OR operator is used to set a bit A&B 0100 0001
• Clearing Bits
o The AND and NOT operators are used to clear A|B 1110 1011
bits
• Toggling Bits A & ~B 0010 1000
o The XOR operator is use to toggle a bit
• Shifting Bits A^B 1010 1010
o The SHIFT operators are used to shift bits left
or right
• Bitmasks A >> 1 0011 0100
o Used to operate on bits in a field A >> 2 0001 1010
o For example – to only operate I the lowermost
2 bits of an 8 bit field are set A << 1 1101 0010
• #define LOWER_2_BITS 0x3 A << 2 1010 0100
• If ( ( val & LOWER_2_BITS ) ==
LOWER_2_BITS ) { do something }

18 © William Kim 2019 Introduction to Embedded Systems


Homework & Quiz #3
• Review the registers for the STM32
board and its peripherals
• Homework Assignment #3 will
consist of:
o STM32 Registers
o Bit Manipulation
• This weeks quiz will focus on:
o Data Types
o STM32 Registers
o Bit Manipulation

19 © William Kim 2019 Introduction to Embedded Systems

You might also like