My feedback/question on HAL source code - tracing HW registers accesses
Hi all,
I wonder if that's something that has been overlooked during a HAL code review (and could be improved in next releases)? Or can you suggest another way of tracing ALL HW registers writes in STM32 for educational/debugging purposes?
For embedded SW professionals, when writing own code, it's sometimes useful to do some kind of reverse-engineering for available standard SW modules or libraries. That can be a valuable help when their own code is not working as expected.
I wondered if there was an easy way to trace all HW registers accesses e.g. during the system startup and initialization. What I did (in a nutshell) was I instrumented SET_BIT(), CLEAR_BIT(), WRITE_REG(), CLEAR_REG(), MODIFY_REG() macros that are found in stm32l4xx.h (in case of my board) so that the macros were writing registers addresses and values in a global array. Just to illustrate the idea:
#define WRITE_REG(REG, VAL) ((REG) = (VAL)); \
if (g_logOn) { \
g_logTable[g_index].op = (uint32_t) 'W'; \
g_logTable[g_index].reg = &(REG); \
g_logTable[g_index++].val = (VAL); \
}
Then I added a simple loop in main.c to dump the contents of the global array to a terminal console. It worked and showed me a sequence of MMIO writes during clock, GPIO, UART etc. initialization in my sample project.
The problem is that the HAL source code is not consequent with using the mentioned macros for registers accesses. For example when you look into the UART driver source, you will see an extensive use of *_REG() macros, either directly or indirectly, BUT in some cases registers are written with a regular C language expression, e.g.
huart->Instance->BRR = value;
Of course it means that my simple instrumentation is not catching all register write operations...
(RR)
