2020-03-30 07:20 PM
I'm new to STM32 and trying to bring up the STM32L053-DISCO project in STM32CubeIDE. It mostly comiles fine, but has an issue with the HAL defines, such as below:
#define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
__IO uint32_t tmpreg; \
SET_BIT(RCC->IOPENR, RCC_IOPENR_GPIOAEN);\
/* Delay after an RCC peripheral clock enabling */ \
tmpreg = READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOAEN);\
UNUSED(tmpreg); \
} while(0)
I really don't see why it is being flagged as an error. I'm sure this must have been seen by someone else...
Best regards,
Stephen
2020-03-30 07:57 PM
It says *before* do, so before the use of the macro.
2020-03-30 08:08 PM
One function call upp from this is:
#define __GPIOA_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE
and before that, this:
#define KEY_BUTTON_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
and before that, this:
#define BUTTONx_GPIO_CLK_ENABLE(__INDEX__) (KEY_BUTTON_GPIO_CLK_ENABLE())
and above that, this:
BUTTONx_GPIO_CLK_ENABLE(Button);
The HAL code is littered with this usage, and it is from a STM sourced project, admittedly written some ago. It must be something obvious but I just don't see what it is.
2020-03-30 08:15 PM
Perhaps you can show the code where the compiler is complaining. The code at and a few lines before __HAL_RCC_GPIOA_CLK_ENABLE is used.
2020-03-31 09:12 AM
This is where the original function call is from (BUTTONx_GPIO_CLK_ENABLE(Button). It is from the BSP files for the STM32L053-DISCO eval board.
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable the BUTTON Clock */
BUTTONx_GPIO_CLK_ENABLE(Button);
if (ButtonMode == BUTTON_MODE_GPIO)
{
/* Configure Button pin as input */
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
}
if (ButtonMode == BUTTON_MODE_EXTI)
{
/* Configure Button pin as input with External interrupt */
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 3, 0);
HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
}
}
2020-03-31 09:17 AM
The problem is
#define BUTTONx_GPIO_CLK_ENABLE(__INDEX__) (KEY_BUTTON_GPIO_CLK_ENABLE())
When this expands, it puts the code you want to execute within parentheses, which leads to invalid syntax. Just replace that line with the appropriate macro, e.g.
__HAL_RCC_GPIOA_CLK_ENABLE();
2020-03-31 09:22 AM
So why can't the original code work? It is example code from ST. I would expect the example code to be able to cleanly compile without issue. Is it possibly because a different compiler may have been used? There aren't any compiler dependencies listed in the original code...
2020-03-31 09:31 AM
You're probably using different HAL includes than when the code was written. My guess is that at the time, __HAL_RCC_GPIOA_CLK_ENABLE was written as something that could be evaluated, e.g. REG->CR |= VALUE, instead of its current do/while loop.
You haven't really explained where you got the code, so one can only guess.
Here's one BSP_LED_Init example online that I found which does not use the syntax you're using, and which would compile correctly.
int32_t BSP_LED_Init (Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
/* Enable the GPIO_LED Clock */
switch (Led)
{
case LED1:
LED1_GPIO_CLK_ENABLE ();
break;
(…)
case LEDN:
LEDN_GPIO_CLK_ENABLE ();
break;
default:
break;
}
/* configure the GPIO_LED pin */
gpio_init_structure.Pin = LED_PIN [Led];
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init (LED_PORT [Led], &gpio_init_structure);
HAL_GPIO_WritePin (LED_PORT [Led], LED_PIN [Led], GPIO_PIN_RESET);
return BSP_ERROR_NONE;
}
Edit: I misread the code. This probably wouldn't compile either.
2020-03-31 09:53 AM
Thanks for your help.
The code was downloaded from here:
2020-03-31 01:13 PM
Also, just build the project from STM32CubeMX for the STM32L0538-DISCO and it will try to use the same macros above.