Search code examples
cstructargumentsembeddedmember

How to pass a struct member as an argument to a function? − C


I'm trying to pass a member TRISCbits.TRISC2 of a struct to a function.

microcontroller.h:

typedef union {
    struct {
        unsigned TRISC0                 :1;
        unsigned TRISC1                 :1;
        unsigned TRISC2                 :1;
    };
} TRISCbits_t;
extern volatile TRISCbits_t TRISCbits @ 0x014;

What I'd like to do:

user.c:

static void pwm5Init(volatile unsigned __bit* trisBit){
    *trisBit = 0;
}

I would like to pass the TRISC2 bit as an argument: pwm5Init(&TRISCbits.TRISC2). And I want my function pwm5Init to modify the real TRISC2 bit at address 0x14+3bits −not a local copy−

As you can't make a pointer point a bit, how would you do? What are the good practices?


Solution

  • I can not imagine a better example than linux kernel

    Here is the code fragment how to modify single bit

    unsigned tmp;
    // some code here
    tmp = pmu_raw_readl(EXYNOS_L2_OPTION(0));
    tmp &= ~EXYNOS_L2_USE_RETENTION;
    pmu_raw_writel(tmp, EXYNOS_L2_OPTION(0));
    

    The patter is read a word from memory, then modify the bit, then write a word. I believe memory is addressed on a byte level. On some architectures even on a word level. So modifying a single bit with "native" instruction is not possible.