初始化GPIO时,函数调用时出现C-STM32f4错误

时间:2016-10-28 06:44:47

标签: c compiler-errors embedded stm32f4discovery

我已经为STM32F4编写了一个初始化GPIO的程序,但在我尝试构建此代码之后出于某种原因:

加入ST&#39的标题:

#include "stm32f4_discovery.h"

定义GPIO的起始地址:

#define GPIOA ((struct GPIO *) 0x40020000)
#define GPIOB ((struct GPIO *) 0x40020400)
#define GPIOC ((struct GPIO *) 0x40020800)
#define GPIOD ((struct GPIO *) 0x40020C00)
#define GPIOE ((struct GPIO *) 0x40021000)
#define GPIOF ((struct GPIO *) 0x40021400)
#define GPIOG ((struct GPIO *) 0x40021800)
#define GPIOH ((struct GPIO *) 0x40021C00)
#define GPIOI ((struct GPIO *) 0x40022000)

重置和时钟控制:

#define RCC   ((uint32_t *) 0x40023830)

#define IN              uint8_t 0
#define OUT             uint8_t 1
#define NO_PULL         uint8_t 0
#define PULL_UP         uint8_t 1
#define PULL_DOWN       uint8_t 2
#define PUSH_PULL       uint8_t 0
#define OPEN_DRAIN      uint8_t 1
#define S2MHz           uint8_t 0
#define S25MHz          uint8_t 1
#define S50MHz          uint8_t 2
#define S100MHz         uint8_t 3

基本GPIO结构:

struct GPIO {
    uint32_t MODER;
    uint32_t TYPER;
    uint32_t OSPEEDR;
    uint32_t PUPDR;
    uint32_t IDR;
    uint32_t ODR;
    uint16_t BSSR_SET;
    uint16_t BSSR_RESET;
};

void Init_GPIO(struct GPIO *GPIO_Type, uint32_t GPIO_Mode, uint8_t in_out, uint8_t pull, uint8_t push_pull, uint8_t freq) {
    // Set MODER:

    if (in_out) {
        GPIO_Type->MODER |= (1 << GPIO_Mode);
        GPIO_Type->MODER &= ~(2 << GPIO_Mode);
    }
    else {
        GPIO_Type->MODER &= ~(3 << GPIO_Mode);
    }

    // Set PUPDR:

    if (!pull) {
        GPIO_Type->PUPDR &= ~(3 << GPIO_Mode);
    }
    else if (pull == 1) {
        GPIO_Type->PUPDR |= (1 << GPIO_Mode);
        GPIO_Type->PUPDR &= ~(2 << GPIO_Mode);
    }
    else if (pull == 2) {
        GPIO_Type->PUPDR |= (2 << GPIO_Mode);
        GPIO_Type->PUPDR &= ~(1 << GPIO_Mode);
    }

    // Set TYPER:

    if (push_pull) {
        GPIO_Type->TYPER &= ~(1 << GPIO_Mode);
    }
    else {
        GPIO_Type->TYPER |= (1 << GPIO_Mode);
    }

    // Set OSPEEDR:

    if (!freq) {
        GPIO_Type->OSPEEDR &= ~(3 << GPIO_Mode);
    }
    else if (freq == 1) {
        GPIO_Type->OSPEEDR |= (1 << GPIO_Mode);
        GPIO_Type->OSPEEDR &= (2 << GPIO_Mode);
    }
    else if (freq == 2) {
        GPIO_Type->OSPEEDR |= (2 << GPIO_Mode);
        GPIO_Type->OSPEEDR &= ~(1 << GPIO_Mode);
    }
    else {
        GPIO_Type->OSPEEDR &= (3 << GPIO_Mode);
    }
}

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    Init_GPIO(GPIOD,  12, OUT, NO_PULL, PUSH_PULL, S2MHz);
    Init_GPIO(GPIOA, 0, IN, NO_PULL, PUSH_PULL, S2MHz);

    while (1) {

    }
}

我收到以下错误,这些错误引用了Init_GPIO fucntion call:

Error[Pe254]: type name is not allowed C:\Users\..\main.c 93 
Error[Pe165]: too few arguments in function call C:\Users\..\main.c 93
Error[Pe018]: expected a ")" C:\Users\..\main.c 93 
Error[Pe254]: type name is not allowed C:\Users\..\main.c 94
Error[Pe165]: too few arguments in function call C:\Users\..\main.c 94
Error[Pe018]: expected a ")" C:\Users\..\main.c 94
Error while running C/C++ Compiler 

1 个答案:

答案 0 :(得分:2)

您的宏INOUT等定义不正确,并且在调用Init_GPIO()时扩展时没有语法意义。

变化:

#define IN              uint8_t 0

#define IN              ((uint8_t)0)
例如,

,类似地为其他类似定义宏。

每当你在包含pre = processor宏的行上遇到编译器错误时,你必须考虑宏扩展后该行的样子,因为这就是编译器&#34;看到&#34;。

或者,使用ST标准外设库,其中GPIO初始化以及与您定义的类似的符号,类型和宏已经正确定义。可以找到使用标准外设库访问GPIO的示例here。作者将其称为CMSIS库,但严格来说它只是 CMSIS兼容而是STM32特定的。其他示例包含在库本身中。该库可能包含在您的工具链中(似乎是IAR),但可以从ST here下载 - 您可能会忽略有关STM32Cube取代的库的内容,除非您想要这样的手持式代码臃肿,并不想将您的代码移植到非STM32平台。