SlideShare a Scribd company logo
© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Fun with 'Embedded' C
2© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
What is in Embedded?
De-jargonified Pointers
Hardware Programming
Compiler Optimizations
Register Programming Techniques
Playful Bit Operations
3© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What is in Embedded?
Typically for a cross architecture
Needs cross compilation
Needing architecture specific options like -mcpu
No-frills Programming
Typically no library code usage
No init setup code
Have a specific / custom memory map
Needs specific code placement
Programming a Bare Metal
No code support framework like stack, ...
No execution support framework like loader
4© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Pointers: De-jargonification
through 7 rules
5© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #0: Foundation
Memory Locations & its Address
CPU
RAM
DB
Integer i; Pointer p;
i = 6; p = 6;
i
p
AB
0
4
8
12
16
20
24
28
32
36
6
6
6© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #1: Pointer as an Integer
“Pointer is an Integer”
Exceptions:
May not be of same size
Rule #2
7© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #2: Pointer not an Integer
Variable Address
Referencing (&)
De-referencing (*)
8© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #3: Pointer Type
Why do we need types attached to pointers?
Only for 'dereferencing'
“Pointer of type t = t Pointer = (t *)”
It is a variable
Which contains an address
Which when dereferenced returns a variable of type t
Starting from that address
Defining a Pointer, indirectly
9© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #4: Pointer Value
“Pointer pointing to a Variable = Pointer contains
the Address of the Variable”
“Pointing means Containing Address”
10© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #5: NULL Pointer
Need for Pointing to 'Nothing'
Evolution of NULL, typically 0
“Pointer value of NULL = Null Addr = Null Pointer
= Pointing to Nothing”
11© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Array Interpretations
Original Big Variable
Consisting of Smaller Variables
Of Same Type
Placed consecutively
Constant Pointer to the 1st Small Variable
In the Big Variable
12© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #6: Array vs Pointer
arr + i = &arr[i]
Value(arr + i) = Value(arr) + i * sizeof(*arr)
“Value(p + i) = Value(p) + i * sizeof(*p)”
Corollaries:
p + i = &p[i]
*(p + i) = p[i]
sizeof(void) = 1
13© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Rule #7: Allocation Types
“Static Allocation vs Dynamic Allocation”
Named vs Unnamed Allocation
Managed by Compiler vs User
Done internally by Compiler vs Using malloc/free
Dynamic corresponding of a 1-D Static Array
Can be treated same once allocated
Except their sizes
14© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
2-D Arrays
Each Dimension could be
Static, or
Dynamic
Various Forms for 2-D Arrays (2x2 = 4)
Both Static (Rectangular) - arr[r][c]
First Static, Second Dynamic - *arr[r]
First Dynamic, Second Static - (*arr)[c]
Both Dynamic - **arr
2-D Arrays using a Single Level Pointer
15© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hardware Programming
16© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Compiler Optimizations
Using -O0, -O1, -O2, -O3, -Os, -Ofast, -Og
May eliminate seemingly redundant code
But important from embedded C perspective
Examples
Seemingly meaningless reads/writes
NOP loop for delay
Functions not called from C code
Ways to avoid
Use -O0 or no optimization
Use volatile for hardware mapped variables
Use __attribute__((optimize("O0"))) for specific functions
Use asmlinkage for functions called from assembly
17© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Register Programming Techniques
Direct using the (Bus) Address
Indirect through some Direct Register
Multiplexed using some Config Registers / Bits
Example: UART Registers, ...
Clear On Set
Example: Status Registers, ...
Protected Access using Lock / Unlock Registers
Example: MAC Id Registers, ...
18© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Bit Operations
Using the C operators &, |, ^, ~, <<, >>
Assignment equivalents of those
Clearing using &, ~
Setting using |
Toggling using ^
Shifting, Multiplication using <<
Shifting, Division using >>
19© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
Specifics of Embedded C
Architecture Specifics, Linker Scripts, Bare Metal
Pointers Simplified
7 Rules, Arrays
Hardware Programming
Compiler Optimizations
Register Programming Techniques
Playful Bit Operations
20© 2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

PDF
Kernel Timing Management
PDF
PDF
Interrupts
PDF
Introduction to Linux Drivers
PDF
Embedded Software Design
Kernel Timing Management
Interrupts
Introduction to Linux Drivers
Embedded Software Design

What's hot (20)

PDF
Linux Kernel Overview
PDF
Block Drivers
PDF
Kernel Debugging & Profiling
PDF
PDF
Kernel Debugging & Profiling
PDF
Architecture Porting
PDF
Real Time Systems
PDF
Linux Network Management
PDF
BeagleBone Black Bootloaders
PDF
Embedded Storage Management
PDF
Introduction to Linux
PDF
Understanding the BBB
PDF
Kernel Programming
PDF
Mobile Hacking using Linux Drivers
PDF
BeagleBoard-xM Booting Process
PDF
Synchronization
PDF
Shell Scripting
Linux Kernel Overview
Block Drivers
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Architecture Porting
Real Time Systems
Linux Network Management
BeagleBone Black Bootloaders
Embedded Storage Management
Introduction to Linux
Understanding the BBB
Kernel Programming
Mobile Hacking using Linux Drivers
BeagleBoard-xM Booting Process
Synchronization
Shell Scripting
Ad

Viewers also liked (13)

PDF
gcc and friends
PDF
References
PDF
File System Modules
PDF
PCI Drivers
PDF
Network Drivers
PDF
USB Drivers
PDF
BeagleBone Black Bootloaders
PDF
Linux Porting
PDF
BeagleBoard-xM Bootloaders
PDF
Character Drivers
PDF
File Systems
gcc and friends
References
File System Modules
PCI Drivers
Network Drivers
USB Drivers
BeagleBone Black Bootloaders
Linux Porting
BeagleBoard-xM Bootloaders
Character Drivers
File Systems
Ad

Similar to Embedded C (20)

PDF
C Programming - Refresher - Part III
PPTX
C programming - Pointer and DMA
PPTX
Assembly Language Tutorials for Windows - 03 Assembly Language Programming
PDF
Interview questions_mod.pdf
PDF
Interview questions
PDF
Interview questions
PPT
Al2ed chapter6
PDF
07 -pointers_and_memory_alloc
PPT
Pointers definition syntax structure use
PDF
Lecture Presentation 9.pdf fpga soc using c
PDF
Develop Embedded Software Module-Session 3
PPT
Chapter09-10 Pointers and operations .PPT
PPT
Chapter09-10.PPT
PDF
Embedded C Programming Module 3 Presentation
PPT
8871077.ppt
PPTX
Ponters
PPTX
C programming for embedded system applications.pptx
PDF
Microcontroladores: Programación en C para microcontroladores con AVR Butterf...
PPT
Al2ed chapter4
PDF
C Pointers
C Programming - Refresher - Part III
C programming - Pointer and DMA
Assembly Language Tutorials for Windows - 03 Assembly Language Programming
Interview questions_mod.pdf
Interview questions
Interview questions
Al2ed chapter6
07 -pointers_and_memory_alloc
Pointers definition syntax structure use
Lecture Presentation 9.pdf fpga soc using c
Develop Embedded Software Module-Session 3
Chapter09-10 Pointers and operations .PPT
Chapter09-10.PPT
Embedded C Programming Module 3 Presentation
8871077.ppt
Ponters
C programming for embedded system applications.pptx
Microcontroladores: Programación en C para microcontroladores con AVR Butterf...
Al2ed chapter4
C Pointers

More from Anil Kumar Pugalia (19)

PDF
File System Modules
PDF
PDF
System Calls
PDF
Playing with R L C Circuits
PDF
Audio Drivers
PDF
Video Drivers
PDF
Functional Programming with LISP
PDF
Power of vi
PDF
"make" system
PDF
Hardware Design for Software Hackers
PDF
RPM Building
PDF
Linux User Space Debugging & Profiling
PDF
System Calls
PDF
PDF
PDF
Linux Memory Management
PDF
Linux File System
PDF
Inter Process Communication
PDF
Introduction to Linux
File System Modules
System Calls
Playing with R L C Circuits
Audio Drivers
Video Drivers
Functional Programming with LISP
Power of vi
"make" system
Hardware Design for Software Hackers
RPM Building
Linux User Space Debugging & Profiling
System Calls
Linux Memory Management
Linux File System
Inter Process Communication
Introduction to Linux

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Approach and Philosophy of On baking technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Modernizing your data center with Dell and AMD
Review of recent advances in non-invasive hemoglobin estimation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Encapsulation_ Review paper, used for researhc scholars
Approach and Philosophy of On baking technology
Advanced methodologies resolving dimensionality complications for autism neur...
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Monthly Chronicles - July 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Teaching material agriculture food technology

Embedded C

  • 1. © 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Fun with 'Embedded' C
  • 2. 2© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? What is in Embedded? De-jargonified Pointers Hardware Programming Compiler Optimizations Register Programming Techniques Playful Bit Operations
  • 3. 3© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What is in Embedded? Typically for a cross architecture Needs cross compilation Needing architecture specific options like -mcpu No-frills Programming Typically no library code usage No init setup code Have a specific / custom memory map Needs specific code placement Programming a Bare Metal No code support framework like stack, ... No execution support framework like loader
  • 4. 4© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Pointers: De-jargonification through 7 rules
  • 5. 5© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #0: Foundation Memory Locations & its Address CPU RAM DB Integer i; Pointer p; i = 6; p = 6; i p AB 0 4 8 12 16 20 24 28 32 36 6 6
  • 6. 6© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #1: Pointer as an Integer “Pointer is an Integer” Exceptions: May not be of same size Rule #2
  • 7. 7© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #2: Pointer not an Integer Variable Address Referencing (&) De-referencing (*)
  • 8. 8© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #3: Pointer Type Why do we need types attached to pointers? Only for 'dereferencing' “Pointer of type t = t Pointer = (t *)” It is a variable Which contains an address Which when dereferenced returns a variable of type t Starting from that address Defining a Pointer, indirectly
  • 9. 9© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #4: Pointer Value “Pointer pointing to a Variable = Pointer contains the Address of the Variable” “Pointing means Containing Address”
  • 10. 10© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #5: NULL Pointer Need for Pointing to 'Nothing' Evolution of NULL, typically 0 “Pointer value of NULL = Null Addr = Null Pointer = Pointing to Nothing”
  • 11. 11© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Array Interpretations Original Big Variable Consisting of Smaller Variables Of Same Type Placed consecutively Constant Pointer to the 1st Small Variable In the Big Variable
  • 12. 12© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #6: Array vs Pointer arr + i = &arr[i] Value(arr + i) = Value(arr) + i * sizeof(*arr) “Value(p + i) = Value(p) + i * sizeof(*p)” Corollaries: p + i = &p[i] *(p + i) = p[i] sizeof(void) = 1
  • 13. 13© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Rule #7: Allocation Types “Static Allocation vs Dynamic Allocation” Named vs Unnamed Allocation Managed by Compiler vs User Done internally by Compiler vs Using malloc/free Dynamic corresponding of a 1-D Static Array Can be treated same once allocated Except their sizes
  • 14. 14© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. 2-D Arrays Each Dimension could be Static, or Dynamic Various Forms for 2-D Arrays (2x2 = 4) Both Static (Rectangular) - arr[r][c] First Static, Second Dynamic - *arr[r] First Dynamic, Second Static - (*arr)[c] Both Dynamic - **arr 2-D Arrays using a Single Level Pointer
  • 15. 15© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hardware Programming
  • 16. 16© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Compiler Optimizations Using -O0, -O1, -O2, -O3, -Os, -Ofast, -Og May eliminate seemingly redundant code But important from embedded C perspective Examples Seemingly meaningless reads/writes NOP loop for delay Functions not called from C code Ways to avoid Use -O0 or no optimization Use volatile for hardware mapped variables Use __attribute__((optimize("O0"))) for specific functions Use asmlinkage for functions called from assembly
  • 17. 17© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Register Programming Techniques Direct using the (Bus) Address Indirect through some Direct Register Multiplexed using some Config Registers / Bits Example: UART Registers, ... Clear On Set Example: Status Registers, ... Protected Access using Lock / Unlock Registers Example: MAC Id Registers, ...
  • 18. 18© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Bit Operations Using the C operators &, |, ^, ~, <<, >> Assignment equivalents of those Clearing using &, ~ Setting using | Toggling using ^ Shifting, Multiplication using << Shifting, Division using >>
  • 19. 19© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? Specifics of Embedded C Architecture Specifics, Linker Scripts, Bare Metal Pointers Simplified 7 Rules, Arrays Hardware Programming Compiler Optimizations Register Programming Techniques Playful Bit Operations
  • 20. 20© 2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?