Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] Trying to understand the Cube HAL library

Status
Not open for further replies.

Prototype21

Junior Member level 1
Joined
Mar 3, 2020
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hey Guys,
I am new to edaboard and ARM microcontroller. I happened to encounter the following two macro definition in HAL library in STMCubeMX.

First:
Code:
#define __HAL_DMA_GET_TC_FLAG_INDEX(__HANDLE__) \
(((uint32_t)((__HANDLE__)->Instance) == ((uint32_t)DMA1_Channel1))? DMA_FLAG_TC1 :\
 ((uint32_t)((__HANDLE__)->Instance) == ((uint32_t)DMA1_Channel2))? DMA_FLAG_TC2 :\
 ((uint32_t)((__HANDLE__)->Instance) == ((uint32_t)DMA1_Channel3))? DMA_FLAG_TC3 :\
 ((uint32_t)((__HANDLE__)->Instance) == ((uint32_t)DMA1_Channel4))? DMA_FLAG_TC4 :\
   DMA_FLAG_TC5)

Second:
Code:
__HAL_DMA_CLEAR_FLAG(__HANDLE__, __FLAG__) (DMA1->IFCR = (__FLAG__))

In the c file, it called like

Code:
__HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));

I am little confused here. How can a macro behave like a function. Is it possible to pass __HAL_DMA_GET_TC_FLAG_INDEX(hdma) as a parameter to __HAL_DMA_CLEAR_FLAG. Whats happening here?
 

A macro will always be expanded to the corresponding text. It can be written with parameters (as in the '__HANDLE__' part here) which means it will substitute whatever text is in the macro invocation as part of the expansion.
For example:
Code:
#define abc(d) d * 2
fred(abc(x));
will expand to
Code:
fred(x * 2);
From this you should be able to see the answer to your second question: you are not passing the macro as a parameter, you will be passing the textual expansion of the macro - in this case it is an ugly nested ternary statement that will be evaluated at runtime and the result will be the parameter that is passed to the function.
Susan
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top