Baremetal C Programming for Embedded Systems

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
Operating Systems Lecture 10 Issues in Paging and Virtual Memory Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing.
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
Embedded ‘C’.  It is a ‘mid-level’, with ‘high-level’ features (such as support for functions and modules), and ‘low-level’ features (such as good access.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Purpose  This training course describes how to configure the the C/C++ compiler options.
Memory Management 3 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
Ethernet Driver Changes for NET+OS V5.1. Design Changes Resides in bsp\devices\ethernet directory. Source code broken into more C files. Native driver.
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
Chapter 4 Memory Management Virtual Memory.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
6-1 Infineon 167 Interrupts The C167CS provides 56 separate interrupt sources that may be assigned to 16 priority levels. The C167CS uses a vectored interrupt.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Presented by: © 2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.
Presented by: © 2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 4, 2015 Jacob Beningo, CSDP Class 5: Robust.
ARM Embedded Programming Lecture 4 Accessing Memory Mapped Registers.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Embedded Real-Time Systems
Lecture 9 Scheduling. Scheduling Policies Preemptive Priority Scheduling.
Input/Output (I/O) Important OS function – control I/O
C++ Lesson 1.
Embedded System Design Techniques™:
Processes and threads.
Embedded Software Development with Python and the Raspberry Pi
CS501 Advanced Computer Architecture
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Embedded Software Development with Python and the Raspberry Pi
COMBINED PAGING AND SEGMENTATION
Computer Architecture
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Review: Two Programming Paradigms
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C Short Overview Lembit Jürimägi.
C Basics.
Introduction to C Programming Language
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Instructor: Ioannis A. Vetsikas
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
An Introduction to Embedded Software Architecture and Design
Embedded Software Development with Python and the Raspberry Pi
Code Generation.
An Introduction to Embedded Software Architecture and Design
Writing Portable and Robust Firmware in C
Writing Portable and Robust Firmware in C
February 26, 2015 Jacob Beningo, CSDP
Baremetal C Programming for Embedded Systems
Govt. Polytechnic,Dhangar
Dr. Bhargavi Dept of CS CHRIST
Virtual Memory Hardware
Lecture 3: Main Memory.
Classes and Objects.
Effective and Efficient memory Protection Using Dynamic Tainting
Architectural Support for OS
Baremetal C Programming for Embedded Systems
Operating Systems : Overview
Chapter 8: Memory Management strategies
An Introduction to Embedded Software Architecture and Design
Operating Systems : Overview
An Introduction to Embedded Software Architecture and Design
Architectural Support for OS
An Introduction to Embedded Software Architecture and Design
Embedded System Design Techniques™:
February 24, 2015 Jacob Beningo, CSDP
C Language B. DHIVYA 17PCA140 II MCA.
COMP755 Advanced Operating Systems
The Three Attributes of an Identifier
CSc 453 Interpreters & Interpretation
Embedded System Development Lecture 12 4/4/2007
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Baremetal C Programming for Embedded Systems Class 1: C Concepts for Embedded Systems February 23, 2015 Jacob Beningo, CSDP

Jacob Beningo Newsletters P.O. Box 400 Embedded Bytes Linden, Michigan 48451 www.beningo.com Newsletters Embedded Bytes http://bit.ly/1BAHYXm Training MicroPython Bootloaders Low Power Design Real-time Software C/C++ Embedded : jacob@beningo.com : 810-844-1522 : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant 2

Course Overview C Concepts for Embedded Systems Baremetal Scheduling Techniques Driver Design Techniques Design Patterns for Firmware Writing Portable Code

Session Overview Review of C Programming Understanding Scope Static and Volatile Pointer Review Pointers vs Arrays Enum vs Preprocessing Interrupts

Program Scope Global Program Scope Every variable is exposed Anyone or anything can purposely or accidentally access any other variable Poor structure Achieved by using single source file Achieved through heavy use of extern keyword Simple to implement Nightmare to debug

Controlling Program Scope Static is a storage class specifier that can be applied to any data type char, short, int, etc tells the compiler to make the variable or function limited in scope while allowing it to persist throughout the life of the program used to encapsulate or hide variables from the rest of the program to prevent inadvertent access

Using Volatile Definition of volatile “An object whose type is qualified with volatile may be modified by other processes or events. The volatile keyword instructs the compiler to reread the object’s value each time it is used, even if the program itself has not changed it since the previous access.” - C in a nutshell page 157 How can a variable change outside of the program? A second thread or processor could have modified the memory space The variable could be an access into hardware! The variable is modified by an interrupt service routine

Using Volatile Example hardware usage When accessing a hardware register the volatile keyword should be used! The reason? Because the compiler may try to optimize register code assuming it won’t change! uint16_t * Reg_Ptr = (uint16_t*)0x1000; while(*Reg_Ptr != 0); // Loop while register is not 0 uint16_t volatile * Reg_Ptr = (uint16_t volatile *)0x1000;

Reserved Words to Avoid auto break continue extern goto inline register restrict

A Review of Pointers Pointer Operators Examples * is used to dereference a pointer & is used to get the address of a variable Examples A NULL pointer int * NullPtr = 0; Variable pointer int * VarPtr = &Var1; Setting the value of Var1 *VarPtr = 0x22;

A Review of Pointers Examining the Memory Map int Var1 = 0x000E; int * PtrInt = &Var1;

A Review of Pointers Pointers and Arrays Passing an Array int Array[] = {14, 15, 16}; Var = Array[0]; // Access the value 14 Passing an Array Void Function(int *); Function(Array); Function(&Array[1]); Pointer Behavior int * IntPtr = array; Var = IntPtr[1]; // Access the value 14

A Review of Pointers Pointers and Structures struct Configtype Config; Configtype * ConfigPtr = &Config; Accessing a Pointer to a struct Config.size = 4; (*ConfigPtr).Size = 4 ConfigPtr->Size = 4 Summary *, ->, [] operators are used to dereference / get at the data stored by the & get address operator

Pointer Arrays An interesting way to map hardware Advantages Groups similar registers into a common array Breaks the system up into “channels” that can be accessed through an index in the array Simplifies initialization and access code Can use a configuration table to initialize the peripheral! Disadvantage Can appear more complicated than direct accessing a register Uses slightly more flash to store the pointers

Enum vs Preprocessing #define SYSTEM_STATE_1 1 typedef enum { SystemState1 = 1, SystemState2 = 2, SystemState3 = 3, SystemState4 = 4 }SystemStateType

Memory Allocation Statically allocated memory Dynamically allocated memory uint8_t Buffer[] = {1,2,3,4,5}; uint8_t * Buffer = malloc(5); for(int i = 1; i < 6; i++) { Buffer[i] = I; } free(Buffer);

Interrupts vs Polling Polling is easy Interrupts are “hard” Processor continually checks status bit to see if the device is ready (i.e. data has arrived, button has been pressed, etc) Generally inefficient (time is wasted to check the status) Usually checked far too often Not great solution for battery operated device Interrupts are “hard” Processor only interrupted when needed Very efficient Handled on the spot after a context switch Great solution for battery operated device

Additional Resources Download Course Material for Updated C Doxygen Templates (Feb 2015) Example drivers source code Microcontroller API Standard EDN Embedded Basics Articles Embedded Bytes Newsletter From www.beningo.com under - Blog and Articles > Software Techniques > CEC Baremetal C Programming

Jacob Beningo Newsletters P.O. Box 400 Embedded Bytes Linden, Michigan 48451 www.beningo.com Newsletters Embedded Bytes http://bit.ly/1BAHYXm Training MicroPython Bootloaders Low Power Design Real-time Software C/C++ Embedded : jacob@beningo.com : 810-844-1522 : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant 19