Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
18.4 unions shall not be used
#1
Hello

I use unions in the following example. And I want to know, if in such a situation the use of unions is compliant or not compliant to MISRA-C:2004. Or if somebody has a better idea for a good implementation.


Code:
Example_Ports.h /* eg. a processor library of a processor manufacturer */
-----------FILE START----------------
...
...
...
#define PORTA        (*((volatile uint8_t *)(2000)))
/* So with \"PORTA\" we get access to data content of PORTA (adress 2000), it is a unsigned char, 8 bits. The file and this line is given by the processor manufacturer */
...
...
-----------FILE END----------------





/*Example_Test.c /* the example c file*/
-----------FILE START----------------
...
...
...

typedef union {
  uint8_t cb ;
  struct bits {
    uint8_t
    b0 : 1,
    b1 : 1,
    b2 : 1,
    b3 : 1,
    b4 : 1,
    b5 : 1,
    b6 : 1,
    b7 : 1 ;
} b ;
}example_bitfield;


#define B7       b.b7
#define B6       b.b6
#define B5       b.b5
#define B4       b.b4
#define B3       b.b3
#define B2       b.b2
#define B1       b.b1
#define B0       b.b0


    #define bitf_porta  (*((volatile example_bitfield*) &PORTA))
    /* Typcast to easy and safty access to each bit, see next line */


    #define porta_light bitf_porta.B5
    /* for a good identifier for the bit. which is for example for a light */


int main(void)
{
    uint8_t x;
    x = 0;

/* Now we can easy use the bits of the ports of the processor manufacturer. eg: */

        porta_light = 1;
        x = porta_light;

}

A. What do you think of this little example. Good user, or bad work?

B. Compliant to rule 18.4, a situation in which unions could be used? Or not. And then, how we could do it in a MISRA-C:2004 way?

thanks a lot

Best regards,
Manni

BTW: Big thanks for your help in my last threads. My MISRA Guide proceed.
<t>best regards,<br/>
Manni</t>
#2
MISRA-C meeting 22-8-2006

The use of unions always requires a deviation.

See discussion under 18.4 for further guidance and examples.

The decision to accept a deviation is for the organisation writing the software.

MISRA cannot endose any specific deviation method.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 1 Guest(s)