Friendly_Coding_in_Embedded_Systems
- 1. Friendly Coding in Embedded Systems
By Rangineni Balasubramanyam
©rangineni balasubramanyam
- 2. What is Friendly Coding?
• In embedded systems Friendly Coding means –
• A piece of the code which only affects the individual
pins as needed nothing but Masking.
• Friendly Software does not change the other bits in a
bit addressable register.
• Unfriendly software modifies more bits of a register
than it needs to.
• Unfriendly software generates the unreliable code,
where as friendly software generates the reliable
code.
©rangineni balasubramanyam
- 3. Example to SET the PIN
• The operation I am going to use is “OR ( | ) ” operation to SET the pin
• Bit wise OR ( | )
• The result in each position is 1 x y x | y
if only the first bit is 1or the 0 0 0
second bit is 1, or both bits are 1; 0 1 1
otherwise the result is 0. 1 0 1
1 1 1
©rangineni balasubramanyam
- 4. Example to SET the PIN
• Let’s consider you wanna glow LED connected to the pin 7
i.e., set pin 7
OR( | ) – wil do the
• GPIO_PORTF_DATA_R |= 0x80; // Friendly code
• GPIO_PORTF_DATA_R t x x x x x x
|
0x80 (mask) 1 0 0 0 0 0 0
=
1 x x x x x x
t – target bit
x – don’t care
Pin
7
6
5
-
-
R
©rangineni balasubramanyam
GND
- 5. Example to SET the PIN
• From above example you can observe that
only the target pin is changed independent of
its current state( either 0 or 1) to the specified
state ( ‘1’ ).
• The operator( | ) does not change the status of
the remaining pins.
• Friendly coding does not alter the status of the
other pins.
©rangineni balasubramanyam
- 6. Example to CLEAR the PIN
• The operation I am going to use is “AND ( & ) ” operation to CLEAR the
pin
• Bit wise AND ( & )
x y x & y
• The result in each position is 1 0 0 0
if the first bit is 1 and the second bit is 1; 0 1 0
otherwise, the result is 0 1 0 0
1 1 1
©rangineni balasubramanyam
- 7. Example to CLEAR the PIN
• Let’s consider you wanna OFF the LED connected to the pin 7
i.e., clear pin 7
• GPIO_PORTF_DATA_R &= ~0x80; // Friendly code
• GPIO_PORTF_DATA_R t x x x x x x
&
~0x80 0 1 1 1 1 1 1
=
GPIO_PORTF_DATA_R 0 x x x x x x
t – target bit
x – don’t care
Pin
7
6
5
-
-
R
©rangineni balasubramanyam
GND
- 8. Example to CLEAR the PIN
• From above example you can observe that only the
target pin is changed independent of its current state(
either 0 or 1) to the specified state ( ‘0’ ).
• The operator( & ) does not change the status of the
remaining pins.
• Friendly coding does not alter the status of the other
pins.
©rangineni balasubramanyam
- 9. Example to CLEAR the PIN
• From the above example we can use the following for clearing
0xEF = 0b 0111:1111
• Because the above is counter intuitive to think of mask being
0xEF, it is better to use
~0x80 = ~ 0b 1000:0000 = 0xEF = 0b0111:1111
• Here we know that we are going to manipulate bit 7, by simply
using the complimentary fashion we can achieve the expected
behavior.
©rangineni balasubramanyam
- 10. References
• “Friendly Code” name is taken from the course
- UT.6.01x Embedded Systems - Shape the
World
• Sincere Thanks to prof. Jonathan Valvano and
Ramesh Yerraballi.
©rangineni balasubramanyam