SlideShare a Scribd company logo
1 of 20
© 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

What's hot (20)

Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Timers
TimersTimers
Timers
 
Toolchain
ToolchainToolchain
Toolchain
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Architecture Porting
Architecture PortingArchitecture Porting
Architecture Porting
 
Real Time Systems
Real Time SystemsReal Time Systems
Real Time Systems
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Embedded Storage Management
Embedded Storage ManagementEmbedded Storage Management
Embedded Storage Management
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Understanding the BBB
Understanding the BBBUnderstanding the BBB
Understanding the BBB
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 

Viewers also liked (13)

gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
References
ReferencesReferences
References
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Network Drivers
Network DriversNetwork Drivers
Network Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
File Systems
File SystemsFile Systems
File Systems
 

Similar to Embedded C

리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3Sangho Park
 
IBM Systems Technical Symposium Melbourne, 2015
IBM Systems Technical Symposium Melbourne, 2015IBM Systems Technical Symposium Melbourne, 2015
IBM Systems Technical Symposium Melbourne, 2015Filipe Miranda
 
Node.js primer for ITE students
Node.js primer for ITE studentsNode.js primer for ITE students
Node.js primer for ITE studentsQuhan Arunasalam
 
f37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptf37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptssuserf06014
 
f37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptf37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptVhhvf
 
Vasiliy Litvinov - Python Profiling
Vasiliy Litvinov - Python ProfilingVasiliy Litvinov - Python Profiling
Vasiliy Litvinov - Python ProfilingSergey Arkhipov
 
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersDefcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersAlexandre Moneger
 
07 processor basics
07 processor basics07 processor basics
07 processor basicsMurali M
 
O365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin Timmermann
O365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin TimmermannO365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin Timmermann
O365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin TimmermannNCCOMMS
 
What's new in AVR 12.0 and VS 2013
What's new in AVR 12.0 and VS 2013What's new in AVR 12.0 and VS 2013
What's new in AVR 12.0 and VS 2013Roger Pence
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Windows Developer
 
Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...
Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...
Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...Intel® Software
 
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28Amazon Web Services
 
Meeting rooms are talking! are you listening?
Meeting rooms are talking! are you listening?Meeting rooms are talking! are you listening?
Meeting rooms are talking! are you listening?Cisco DevNet
 

Similar to Embedded C (20)

리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3
 
IBM Systems Technical Symposium Melbourne, 2015
IBM Systems Technical Symposium Melbourne, 2015IBM Systems Technical Symposium Melbourne, 2015
IBM Systems Technical Symposium Melbourne, 2015
 
Rsockets ofa12
Rsockets ofa12Rsockets ofa12
Rsockets ofa12
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Node.js primer for ITE students
Node.js primer for ITE studentsNode.js primer for ITE students
Node.js primer for ITE students
 
f37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptf37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.ppt
 
f37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.pptf37-book-intarch-pres-pt2.ppt
f37-book-intarch-pres-pt2.ppt
 
03 workshop
 03 workshop 03 workshop
03 workshop
 
Vasiliy Litvinov - Python Profiling
Vasiliy Litvinov - Python ProfilingVasiliy Litvinov - Python Profiling
Vasiliy Litvinov - Python Profiling
 
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbersDefcon 22 - Stitching numbers - generating rop payloads from in memory numbers
Defcon 22 - Stitching numbers - generating rop payloads from in memory numbers
 
07 processor basics
07 processor basics07 processor basics
07 processor basics
 
O365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin Timmermann
O365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin TimmermannO365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin Timmermann
O365Con18 - Using ARM Templates to Deploy Solutions on Azure - Kevin Timmermann
 
What's new in AVR 12.0 and VS 2013
What's new in AVR 12.0 and VS 2013What's new in AVR 12.0 and VS 2013
What's new in AVR 12.0 and VS 2013
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
 
Node.js Workshop
Node.js WorkshopNode.js Workshop
Node.js Workshop
 
JavaMicroBenchmarkpptm
JavaMicroBenchmarkpptmJavaMicroBenchmarkpptm
JavaMicroBenchmarkpptm
 
Es2015 training material-syedawase
Es2015 training material-syedawaseEs2015 training material-syedawase
Es2015 training material-syedawase
 
Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...
Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...
Simple Single Instruction Multiple Data (SIMD) with the Intel® Implicit SPMD ...
 
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
 
Meeting rooms are talking! are you listening?
Meeting rooms are talking! are you listening?Meeting rooms are talking! are you listening?
Meeting rooms are talking! are you listening?
 

More from Anil Kumar Pugalia (19)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Processes
ProcessesProcesses
Processes
 
System Calls
System CallsSystem Calls
System Calls
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
System Calls
System CallsSystem Calls
System Calls
 
Threads
ThreadsThreads
Threads
 
Processes
ProcessesProcesses
Processes
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 

Recently uploaded

Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 

Recently uploaded (20)

Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 

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?