Search code examples
cstructarmuart

Expected expression before 'struct' - Error


I'm using the CrossStudio IDE to make a simple function prototype that initializes the UART baudrate and other parameters for the STM32F30x ARM processor. The goal of the function prototype is just to print the baudrate from initialization peripheral (stm32f30x.c), thus I expected '9600'. Instead the error "expected expression before 'USART_Init'" returned. There are 3 files in total:

1) config_uart.c -> Contains the function prototype
2) stm32f30x_usart.c -> Contains Initializing function, which I call from config_uart.c
3) stm32f30x_usart.h -> Contains a prototype for struct definition

Config_uart.c Body

void Comm_Initial (void) {
    typedef struct USART_Init USART_Init;
    void USART_StructInit(USART_InitStruct);
    printf("%d\n", USART_Init->USART_BaudRate);
}

from stm32f30x_usart.c Body

void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
{
    /* USART_InitStruct members default value */
    USART_InitStruct->USART_BaudRate = 9600;
    USART_InitStruct->USART_WordLength = USART_WordLength_8b;
    USART_InitStruct->USART_StopBits = USART_StopBits_1;
    USART_InitStruct->USART_Parity = USART_Parity_No ;
    USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
}

from stm32f30x_usart.h Body

typedef struct
{
    uint32_t USART_BaudRate;
    uint32_t USART_WordLength;
    uint32_t USART_StopBits;
    uint32_t USART_Parity;
    uint32_t USART_Mode;
    uint32_t USART_HardwareFlowControl;
} USART_InitTypeDef;

I'm unsure where the error is as I've tried multiple variations in an attempt to identify the problem, this is the closest I've gotten. What am I not understanding? Any help is appreciated.


Solution

  • void Comm_Initial (void) {
        typedef struct USART_Init USART_Init;
    

    I don't see a type struct USART_Init defined anywhere. The typedef is still permitted, but it refers to an incomplete type.

    You do have a type named USART_InitTypeDef that has a member named USART_BaudRate.

        void USART_StructInit(USART_InitStruct);
        printf("%d\n", USART_Init->USART_BaudRate);
    }
    

    USART_Init is a type name. The -> operator requires an expression of pointer-to-struct or pointer-to-union type as its left operand.

    If you replace USART_Init with some expression of type USART_InitTypeDef*, then you should be able to evaluate the expression.

    Note that %d requires an argument of type int, not uint32_t. There are several ways to print a uint32_t value; the simplest is:

    printf("%lu\n", (unsigned long)some_pointer->USART_BaudRate);