forked from FourierSignal/Hello-World
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_C_questions
More file actions
108 lines (77 loc) · 4.16 KB
/
Copy pathcommon_C_questions
File metadata and controls
108 lines (77 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Volatile Qualifier:
########################
In practice, only three types of variables could change:
1. Memory-mapped peripheral registers
2. Global variables modified by an interrupt service routine
3. Global variables accessed by multiple tasks within a multi-threaded application
Register access case:
----------------------
uint8_t * pReg = (uint8_t *) 0x1234;
while (*pReg == 0)
{
// Wait for register to become non-zero
}
if compiler optimization is on,
the compiler will generate assembly language that looks something like this:
mov ptr, #0x1234
mov a, @ptr
loop:
bz loop
This is because compiler has no way of knowing that pReg is a pheripheral-regiser
it optimizes while loop as infinite loop --treating pReg as normal pointer.
uint8_t volatile * pReg = (uint8_t *) 0x8000;
while (*pReg == 0)
{
// Wait for register to become non-zero
}
===>
mov ptr, #0x1234
loop:
mov a, @ptr
bz loop
ISR case :
-------------------
############################################################
Mutex vs semaphore :
can binary semaphore be used as a mutex?? - no . why??
https://blog.feabhas.com/2009/09/mutex-vs-semaphores-%E2%80%93-part-1-semaphores/
https://blog.feabhas.com/2009/09/mutex-vs-semaphores-%E2%80%93-part-2-the-mutex/
https://blog.feabhas.com/2009/10/mutex-vs-semaphores-%E2%80%93-part-3-final-part-mutual-exclusion-problems/
##################################################################################
INLINE function vs MACRO vs Regular Function call:
MACROs --> preprocessing stage, no compilation error checks.
INLINE fn--> compilation stage , avoids function call.
why not MACRO but INLINE prefered??
---
disadvantages of macros:
a) There is no type checking
b) Difficult to debug as they cause simple replacement.
c) Macro don’t have namespace, so a macro in one section of code can affect other section.
d) Macros can cause side effects as shown below:
#define CUBE(b) b*b*b printf("%d", CUBE(1+2)); ==> 1+2*1+2*1+2 = 7
inline int cube(int a) { return a*a*a; } ---> compiler can choose to substitute or not
printf("%d", cube(1+2));===> 27
################################################################################################
const volatile -- can they be used together? yes
const volatile or volatile const ?? --- both
const(never change) and volatile(even changing) :
Typical use scenarios for both the keywords:
either pointers to memory-mapped hardware registers or variables located in shared memory areas.
int volatile g_shared_with_isr; ---> flag shared b/w ISR and task
uint16_t const max_temp_in_c = 1000; ---> the variable is located in non-volatile mem like ROM/flash
Any attempt the code makes to write to a const variable will result in a compile-time error
uint8_t volatile * p_led_reg = (uint8_t *) 0x80000; ---> Hw register at a known physical address
uint8_t const * p_latch_reg = 0x10000000; ---> to mark Hw-reg as readonly
any attempt to write to that physical memory address will result in a compile-time error
---------------------------------------------------------------------------------------------------------
uint8_t volatile * const p_led_reg = (uint8_t *) 0x80000; ---> constant pointer to a volatile hardware register.
any attempt to modify pointer leads to error and optimisation avoided while dealing with value of register.
---------------------------------------------------------------------------------------
TO enforce --software doesn't try to overwrite the memory location,
uint8_t const volatile comm_buffer[BUFFER_SIZE]; --> Read-only shared-memory buffer
----------------------------------------------------------------------------------------------------
TO enforce --software doesn't try to overwrite the memory location,
you also need to be sure that each and every requested read actually occurs.
By declaring your variable is a (constant) pointer TO a constant and volatile memory location
uint8_t const volatile * const p_latch_reg = (uint8_t *) 0x10000000; ---> to mark Hw-reg as readonly
----------------------------------------------------------------------------------------------------