Home > database >  How to suppress 'value computed but not used' in C?
How to suppress 'value computed but not used' in C?

Time:04-03

Regarding the message 'value computed is not used', I find all sorts of explanations how to fix the corresponding code but I can't find any hint how to suppress this warning.

I have this perfectly fine code

HSEM->RLR[2] == (HSEM_CM4_CORE_ID | HSEM_PROC_ID | (1UL << 31));

This is the correct way to lock a semaphore in the STM32H745 as the semaphore is locked via a read. Since I know this is valid code, how do I suppress the warning for this particular line of code?

I am using gcc in the CubeIDE environment.

Edited to add:

Here's the code that I used to verify that the lock works:

#define HSEM_CM4_CORE_ID (1 << 8)  // CPU2 is CM4
#define HSEM_PROC_ID (0 << 0)
volatile uint32_t x;
x = HSEM->R[23] & (1 << 31);                                     // Read semaphore 23.
HSEM->RLR[23] == (HSEM_CM4_CORE_ID | HSEM_PROC_ID | (1 << 31));  // Lock semaphore 23.
x = HSEM->R[23] & (1 << 31);                                     // Read semaphore 23.
HSEM->R[23] = HSEM_CM4_CORE_ID | HSEM_PROC_ID;                   // Release semaphore 23.
x = HSEM->R[23] & (1 << 31);                                     // Read semaphore 23.

When I step through this code line by line with the debugger, the first time x is set to 0, the second time it is set to (1 << 31) and the third time it is back to 0.

CodePudding user response:

This is my understanding of the procedure to use the STM32H745 hardware semaphores, but I could be wrong. The document should provide coding examples instead of schematics.

When you read the semaphore, the value read will depend if the semaphore was free or not, but if it was free, the reading process will lock it and must unlock it when done:

while (HSEM->RLR[2] != HSEM_CM4_CORE_ID | 0x0000 | (1U << 31)) {
    /* semaphore was locked by another core/process, wait a bit */
    nanosleep(1000);
}
/* semaphore was locked, we can proceed */
[...]
/* unlock the semaphore */
HSEM->RLR[2] = (HSEM_CM4_CORE_ID | HSEM_PROC_ID);

If you are certain the semaphore is free and just mean to lock it, just read the register:

/* lock the semaphore */
(void)HSEM->RLR[2];

HSEM is #defined in the STM stm32h723xx.h header as a pointer to a memory mapped HSEM_TypeDef structure where RLR is defined as an __IO qualified array of 32 uint32_t. __IO is defined in core_cm7.h as volatile. Hence the compiler will generate code to read the value whether the value is used or not and the (void) cast means do not complain about this value not being used, effectively silencing the compiler warning.

CodePudding user response:

So for completeness sake referring to my example above in the initial question, the correct code with the suppressed warning looks like this:

#define HSEM_CM4_CORE_ID (1 << 8)  // CPU2 is CM4
#define HSEM_PROC_ID (0 << 0)
volatile uint32_t x;
x = HSEM->R[2] & (1 << 31);                     // Read semaphore 2.
(void) HSEM->RLR[2].                            // Lock semaphore 2.
x = HSEM->R[2] & (1 << 31);                     // Read semaphore 2.
HSEM->R[2] = HSEM_CM4_CORE_ID | HSEM_PROC_ID;   // Release semaphore 22.
x = HSEM->R[2] & (1 << 31);
  • Related