• AVR Freaks

Hot!Nested Struct/Union

Author
LittleRain
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2018/11/01 21:08:39
  • Location: 0
  • Status: offline
2019/12/06 13:56:33 (permalink)
0

Nested Struct/Union

I'm just creating a library for an I2S Amplifier, but coming from a C# background, I'm having trouble with certain things in C, specifically nested structs.
 
The amp is mono, so if you want stereo or more, you would use more than one in your design. So because of that I want to use structs to hold the config, I2C address and what ever else is needed to run the specific IC, but I don't know how to do nested structs properly. I've been contemplating making it in C++ as a class instead, but since most MCU's use C most of the time I'm trying to avoid that.
 
Anyways, I am getting this error. My top struct is in TFA9879.h, at the bottom of this code.
 
main.c:66:9: error: 'amp' has an incomplete type
Tfa_Configure(amp, 0, 0);
 
Here is the problematic code.
 
(TFA9879.h)
 
(More struct Registers above, following the below format)

 typedef union {
        unsigned short reg;
        struct{
            unsigned OTPOK : 1; //overtemperature protection: {0:active 1:inactive}
            unsigned OCPOKB : 1; //overcurrent protection on pin OUTB {0:active 1:inactive}
            unsigned OCPOKA : 1; //overcurrent protection on pin OUTA {0:active 1:inactive}
            unsigned UFP1 : 1; //underfrequency protection serial1 {LRCK1 is (0:inline or lower 1:higher) than selected interface format}
            unsigned OFP1 : 1; //overfrequency protection serial1 {LRCK1 is (0:inline or lower 1:higher) than selected interface format}
            unsigned IBP1 : 1; //invalid bit clock protection serial1 {ratio between SCK1 and LRCK1 is (0:valid 1:invalid) for the selected interface format}
            unsigned UFP2 : 1; //underfrequency protection serial2 {LRCK2 is (0:inline or lower 1:higher) than selected interface format}
            unsigned OFP2 : 1; //overfrequency protection serial2 {LRCK2 is (0:inline or lower 1:higher) than selected interface format}
            unsigned IBP2 : 1; //invalid bit clock protection serial2 {ratio between SCK2 and LRCK2 is (0:valid 1:invalid) for the selected interface format}
            unsigned AMP : 2; //Amplifier mode status {00:off 01:statup 10:statup 11:functional}
            unsigned reserved1 : 2;
            unsigned PORA : 1; //analog 1V8 regulator status {0:Off or output voltage too low 1:Available and correct}
            unsigned PS : 1; //Power stage status {0:Class-D amp power stage floating 1:Class-D amp power stage switching;PWM signals on pins OUTA and OUTB}
            unsigned reserved2 : 1;
        };
    }Misc_Status_reg;
    
    typedef struct{
        Device_Control_reg device_control_reg;
        Serial_Interface_reg serial_reg1;
        Serial_Interface_reg serial_reg2;
        PCM_IOM2_Format_reg format_reg1;
        PCM_IOM2_Format_reg format_reg2;
        Equalizer_reg equalizerA_reg;
        Equalizer_reg equalizerB_reg;
        Equalizer_reg equalizerC_reg;
        Equalizer_reg equalizerD_reg;
        Equalizer_reg equalizerE_reg;
        Bypass_reg bypass_reg;
        DynamicRangeCompressor_reg drc_reg;
        Bass_Treble_reg basstreb_reg;
        HighPass_Filter_reg highpass_reg;
        Volume_reg volume_reg;
        DeEmphasis_Soft_Hard_Power_Limiter_reg emphasis_reg;
        Misc_Status_reg misc_reg;
    }TFA9879_Config_t;

    typedef union {
        unsigned char adr;
        struct{
#if !I2C_LIB_SHIFTS_ADDRESS //If your I2C library takes a 7bit address and shifts it for you, we don't want to include this bit
            unsigned rw : 1; //Request is Read or Write?
#endif
            unsigned a1 : 1; //Adsel1 pin
            unsigned a2 : 1; //Adsel2 pin
            unsigned cadr : 5; //Constant address (0b11011[a1][a2]|[rw])
        };
    }TFA9879_Address_t;

    typedef struct{
        TFA9879_Config_t config;
        TFA9879_Address_t address;
    }TFA9879_t;

 
 
(main.c)
 
struct TFA9879_t amp;

int main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    while (1)
    {
        
        uint8_t *pData;
        Tfa_Configure(amp, 0, 0);
        
        if(Tfa_Init(amp, pData) == 1){
            
        }
        
        // Add your application code
        //FatFsDemo_Tasks();
        
    }
    return 1;
}

#1

3 Replies Related Threads

    _fragment_
    Super Member
    • Total Posts : 408
    • Reward points : 0
    • Joined: 2008/10/08 09:17:55
    • Location: Idaho
    • Status: offline
    Re: Nested Struct/Union 2019/12/06 14:39:28 (permalink)
    0
    Sanity check: you have a #include for TFA9879.h in main.c, and
    Tfa_Configure() takes a TFA9879_t and not a TFA9879_t pointer?
     
    Dave
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11521
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Nested Struct/Union 2019/12/06 14:40:45 (permalink)
    4 (1)
    If you've declared with "typedef struct", you don't use "struct" again in the definition, just do
     
    TFA9879_t amp;
    #3
    LittleRain
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2018/11/01 21:08:39
    • Location: 0
    • Status: offline
    Re: Nested Struct/Union 2019/12/06 15:18:31 (permalink)
    0
    _fragment_
    Sanity check: you have a #include for TFA9879.h in main.c, and
    Tfa_Configure() takes a TFA9879_t and not a TFA9879_t pointer?
     
    Dave


    Yes I do have include, and yes it takes a pointer, which seemed to be the problem...
    Adding the & fixed it, I swear I tried that already...
     
    Thanks
     
    jtemples
    If you've declared with "typedef struct", you don't use "struct" again in the definition, just do
     
    TFA9879_t amp;



    I think I'm losing it, I just went to change it, and the struct was gone.
     
     
     
    #4
    Jump to:
    © 2020 APG vNext Commercial Version 4.5