Embedded Software in C for an ARM Cortex M

Jonathan W. Valvano and Ramesh Yerraballi (1/2015)

Chapter 1: Program Structure

A sample program introduces C
C is a free field language
Precedence of the operator determines the order of operation
Comments are used to document the software
Preprocessor directives are special operations that occur first
Global declarations provide modular building blocks
Declarations are the basic operations
Function declarations allow for one routine to call another
Compound statements are the more complex operations
Global variables are permanent and can be shared
Local variables are temporary and are private
Source files make it easier to maintain large projects

Chapter 2: Tokens

ASCII characters
Literals include numbers characters and strings
Keywords are predefined
Names are user defined
Punctuation marks

Chapter 3: Literals include numbers characters and strings

How are numbers represented on the computer
8-bit unsigned numbers
8-bit signed numbers
16-bit unsigned numbers
16-bit signed numbers
Big and little Endian
Boolean (true/false)
Decimal numbers
Hexadecimal numbers
Octal numbers
Escape sequences

Chapter 4: Variables

A static variable exists permanently
A static global can be accessed only from within the same file
A static local can be accessed only in the function
We specify volatile variables when using interrupts and I/O ports
Automatic variables are allocated on the stack
We can understand automatics by looking at the assembly code
A constant local can not be changed
External variables are defined elsewhere
The scope of a variable defines where it can be accessed
Variables declarations
8-bit variables are defined with char
Discussion of when to use static versus automatic variables
Initialization of variables and constants
We can understand initialization by looking at the assembly code

Chapter 5: Expressions

Precedence and associativity
Unary operators
Binary operators
Assignment operators
Expression type and explicit casting
Selection operator
Arithmetic overflow and underflow

Chapter 6: Flow of Control

Simple statements
Compound statements
if and if-else statements
switch statements
while statements
for statements
do statements
return statements
goto statements
Null statements

Chapter 7: Pointers

Definitions of address and pointer
Declarations of pointers define the type and allocate space in memory
How do we use pointers
Memory architecture of the TM4C123 and TM4C1294 ARM Cortex M4
Pointer math
Pointer comparisons
FIFO queue implemented with pointers
I/O port access

Chapter 8: Arrays and Strings

Array Subscripts
Array Declarations
Array References
Pointers and Array Names
Negative Subscripts
Address Arithmetic
String Functions defined in string.h
Fifo Queue Example

Chapter 9: Structures

Structure Declarations
Accessing elements of a structure
Initialization of structure data
Using pointers to access structures
Passing structures as parameters to functions
Example of a Linear Linked List
Example of a Huffman Code

Chapter 10: Functions

Function Declarations
Function Definitions
Function Calls
Parameter Passing
Making our C programs "look like" C++
Stack frame created Keil uVision
Finite State Machine using Function Pointers
Linked list interpreter

Chapter 11: Preprocessor Directives

Using #define to create macros
Using #ifdef to implement conditional compilation
Using #include to load other software modules
Assembler Directives

Appendix 1: C Declarations - A Short Primer


Chapter 0 The Preface

Zero is an appropriate place for a book on C to start. Zero has many special meanings to the C programmer. On the ARM Cortex M, zero is the address of the initial stack pointer that gets set on reset.  The compiler will initialize all global variables to zero on start-up. We use a zero to signify the end of a string. A pointer with a zero value is considered a null-pointer (doesn't point to anything). We use a zero value to signify the Boolean false, and true is any nonzero value. The array subscripts in C start with zero.

This document serves as an introduction to C programming on the Texas Instruments TM4C123 or TM4C1294 microcomputers. Its purpose is to provide a short introduction to C programming in the context of Embedded Systems. Initially when we use general (not Embedded System specific) C constructs. we will assume you have access to a free compiler (codepad.org is an excellent start). For Embedded System development where a microcontroller board is used an IDE like the Keil uVision from ARM (https://www.keil.com/demo/eval/armv4.htm) is an invaluable tool to edit, compile and debug your code in both simulated environments as well as actual boards. 

This document differs from classical C programming books in its emphasis on embedded systems. While reviewing the existing literature on C programming I was stuck by the high percentage of programming examples in these books that rely on the functions scanf and printf to perform input/output. While I/O is extremely important for embedded systems, rarely is serial I/O with scanf and printf an important aspect of an embedded system. This HTML document is clearly not comprehensive rather it serves as a short refresher for those C programmers whose skills are a little rusty. This document also assists the experienced programmer trained in another language like Pascal or C++, that now wishes to program in C for an embedded Cortex M system. If the reader in interested in a more classical approach to C programming I suggest:

A Book on C: Programming in C, by Kelley and Pohl, Addison-Wesley

Send comments and suggestions about this document to: valvano@mail.utexas.edu or ramesh@mail.utexas.edu 

The style and structure of this HTML document was derived from A Small C Compiler: Language, Usage, Theory, and Design, by James E. Hendrix.

Legal Statement

Creative Commons License
Embedded Software in C for an ARM Cortex M by Jonathan Valvano and Ramesh Yerraballi is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
Based on a work at http://users.ece.utexas.edu/~valvano/arm/outline1.htm.