cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f429-disco and beginners question

mail239955
Associate II
Posted on June 20, 2014 at 15:26

Hi,

as a beginner I am starting freshly with stm32 programming on a stm32f429-disco board using eclipse with the gnu-arm-plugin and the gnu-arm-eabi toolchain in linux (toolchain is working - the simple LED-blinky example works). Problem: When I modify the source-code of the blinky example (condensing all settings into the ''main.c'' for better understanding) and trying to blink a LED, the ''while(1)'' loop seems to run only once and is not repeated - Why ? I also have this problem with other examples trying to read/write GPIOs. Is it a problem of the eclipse-toolchain or a programming issue ? Here the modified blinky-code:

//
// This file is part of the GNU ARM Eclipse distribution.
// Copyright (c) 2014 Liviu Ionescu.
//
// ----------------------------------------------------------------------------
#include <
stdio.h
>
#include ''diag/Trace.h''
//#include ''Timer.h''
#include ''cmsis_device.h''
#include ''cortexm/ExceptionHandlers.h''
//#include ''BlinkLed.h''
#include ''stm32f4xx.h''
#include ''stm32f4xx_hal.h''
// ----------------------------------------------------------------------------
//
// STM32F4 led blink sample (trace via NONE).
//
// In debug configurations, demonstrate how to print a greeting message
// on the trace device. In release configurations the message is
// simply discarded.
//
// To demonstrate POSIX retargetting, reroute the STDOUT and STDERR to the
// trace device and display messages on both of them.
//
// Then demonstrates how to blink a led with 1Hz, using a
// continuous loop and SysTick delays.
//
// On DEBUG, the uptime in seconds is also displayed on the trace device.
//
// Trace support is enabled by adding the TRACE macro definition.
// By default the trace messages are forwarded to the NONE output,
// but can be rerouted to any device or completely suppressed, by
// changing the definitions required in system/src/diag/trace_impl.c
// (currently OS_USE_TRACE_ITM, OS_USE_TRACE_SEMIHOSTING_DEBUG/_STDOUT).
//
// ----- Timing definitions -------------------------------------------------
#define BLINK_PORT_NUMBER (6)
#define BLINK_PIN_NUMBER (13)
#define BLINK_ACTIVE_LOW (0)
#define BLINK_GPIOx(_N) ((GPIO_TypeDef *)(GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE)*(_N)))
#define BLINK_PIN_MASK(_N) (1 << (_N))
#define BLINK_RCC_MASKx(_N) (RCC_AHB1ENR_GPIOAEN << (_N))
// Keep the LED on for 2/3 of a second.
#define TIMER_FREQUENCY_HZ (1000u)
#define BLINK_ON_TICKS (TIMER_FREQUENCY_HZ * 2 / 3)
#define BLINK_OFF_TICKS (TIMER_FREQUENCY_HZ - BLINK_ON_TICKS)
typedef uint32_t timer_ticks_t;
volatile timer_ticks_t timer_delayCount;
// ----- main() ---------------------------------------------------------------
// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored ''-Wunused-parameter''
#pragma GCC diagnostic ignored ''-Wmissing-declarations''
#pragma GCC diagnostic ignored ''-Wreturn-type''
int
main(int argc, char* argv[])
{
// By customising __initialize_args() it is possible to pass arguments,
// for example when running tests with semihosting you can pass various
// options to the test.
// trace_dump_args(argc, argv);
// Send a greeting to the trace device (skipped on Release).
//trace_puts(''Hello ARM World!'');
// The standard output and the standard error should be forwarded to
// the trace device. For this to work, a redirection in _write.c is
// required.
//puts(''Standard output message.'');
//fprintf(stderr, ''Standard error message.\n'');
// At this stage the system clock should have already been configured
// at high speed.
//trace_printf(''System clock: %uHz\n'', SystemCoreClock);
//timer_start();
SysTick_Config(SystemCoreClock / TIMER_FREQUENCY_HZ);
//blink_led_init();
// Enable GPIO Peripheral clock
RCC->AHB1ENR |= BLINK_RCC_MASKx(BLINK_PORT_NUMBER);
GPIO_InitTypeDef GPIO_InitStructure;
// Configure pin in output push/pull mode
GPIO_InitStructure.Pin = BLINK_PIN_MASK(BLINK_PIN_NUMBER);
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_PULLUP;
HAL_GPIO_Init(BLINK_GPIOx(BLINK_PORT_NUMBER), &GPIO_InitStructure);
//uint32_t seconds = 0;
// Infinite loop
while (1)
{
//blink_led_on();
HAL_GPIO_WritePin(BLINK_GPIOx(BLINK_PORT_NUMBER),
BLINK_PIN_MASK(BLINK_PIN_NUMBER), GPIO_PIN_SET);
//timer_sleep(BLINK_ON_TICKS);
timer_delayCount = 1000u;
while (timer_delayCount != 0u)
;
//blink_led_off();
HAL_GPIO_WritePin(BLINK_GPIOx(BLINK_PORT_NUMBER),
BLINK_PIN_MASK(BLINK_PIN_NUMBER), GPIO_PIN_RESET);
//timer_sleep(BLINK_OFF_TICKS);
timer_delayCount = 1000u;
while (BLINK_OFF_TICKS != 0u)
;
//++seconds;
// Count seconds on the trace device.
//trace_printf(''Second %u\n'', seconds);
}
// Infinite loop, never return.
}
// ----- SysTick_Handler() ----------------------------------------------------
void
SysTick_Handler (void)
{
if (timer_delayCount != 0u)
{
--timer_delayCount;
}
}
// ----------------------------------------------------------------------------
#pragma GCC diagnostic pop
// ----------------------------------------------------------------------------

5 REPLIES 5
Andrew Neil
Evangelist III
Posted on June 20, 2014 at 21:37

''the 'while(1)' loop seems to run only once and is not repeated''

Why do you think that?

Where does it go after ''exiting'' the infinite loop?

Did you copy & paste that code direct from your IDE into the forum?

No

  manual re-typing?

hmolesworth
Associate
Posted on June 21, 2014 at 00:34

Have a look at this:

while (BLINK_OFF_TICKS != 0u)
;

... it's probably not what you intended to write ...
mail239955
Associate II
Posted on June 22, 2014 at 13:38

Thanks !

Of course this was the bug, I checked the wrong value ! It should be:

while (timer_delayCount != 0u)
;

mail239955
Associate II
Posted on June 22, 2014 at 23:09

Now, I am struggling with the simple interrupt example with button on PA0.

When the button is pushed the green LED shall be turned ON - once and for all. But nothing happens when button is pushed - even in the Debug mode the Callback-function for the EXTI0 is never entered. Seems like the interrupt is not detected when pushing the button - WHY ? Is the interrupt for PA0 initialized correctly ? Here is the code:

//
// This file is part of the GNU ARM Eclipse distribution.
// Copyright (c) 2014 Liviu Ionescu.
//
// ----------------------------------------------------------------------------
#include <
stdio.h
>
#include ''diag/Trace.h''
#include ''cmsis_device.h''
#include ''cortexm/ExceptionHandlers.h''
#include ''stm32f4xx.h''
#include ''stm32f4xx_hal.h''
// ----- main() ---------------------------------------------------------------
// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored ''-Wunused-parameter''
#pragma GCC diagnostic ignored ''-Wmissing-declarations''
#pragma GCC diagnostic ignored ''-Wreturn-type''
int main(int argc, char* argv[])
{
/*** GPIO INITIALIZATION ***/
GPIO_InitTypeDef GPIO_InitStructure;
// Initialize green LEDs on PG13 and PG14
// Enable GPIO Peripheral clock on Port G for LEDs
__GPIOG_CLK_ENABLE();
// Configure pin in output push/pull mode
GPIO_InitStructure.Pin = GPIO_PIN_13;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
// Configure pin in output push/pull mode
GPIO_InitStructure.Pin = GPIO_PIN_14;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
// Initialize Pushbutton on PA0 as interrupt driven
__GPIOA_CLK_ENABLE();
__SYSCFG_CLK_ENABLE();
//Configure Button pin as input on PA0
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
//GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Enable and set EXTI Line0 Interrupt to the lowest priority */
HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
/*** While loop ***
*/
//initialize LED state
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_13, GPIO_PIN_RESET); // green OFF
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14, GPIO_PIN_RESET); // red OFF
// Infinite loop
while (1)
{
}
// Infinite loop, never return.
}
// ----- External Interrupt () ------------------------------------------------
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_0)
{
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_13, GPIO_PIN_SET); // green ON
}
}
#pragma GCC diagnostic pop
// ----------------------------------------------------------------------------

mail239955
Associate II
Posted on June 23, 2014 at 18:11

Hi,

I started a new discussion for the above question.

This thread can be marked as closed !

Thanks for the support and help!