cancel
Showing results for 
Search instead for 
Did you mean: 

reset on nucleo -f401RE with even very simple main loop

roseanne qiu
Associate II
Posted on May 24, 2018 at 22:20

Dear Sir/Madam:

I am using nucleo -f401RE with STM32 . I found the program keeps reset even with almost empty main loop

there is no way to do seftreset from the codes. 

only settings for hardware are system clock and some hall_init

any clue why it is reset?

here are the ref code:

int main(void)

{

//=================================================================

// Initialize everything

/*

STM32F4xx HAL library initialization:

- Configure the Flash prefetch, instruction and Data caches

- Configure the Systick to generate an interrupt each 1 msec

- Set NVIC Group Priority to 4

- Global MSP (MCU Support Package) initialization

*/

HAL_Init();

// My local init

Init();

static bool fcFlag = false;

// Set up variables related to state machine

int main_state = START; 

//=================================================================

// Now loop forever and cycle through states

while(1)

{

switch (main_state) {

case START:

if ( fcFlag == false )

{

STROBE(START) // send to gp PC8 pin to scope to check reset 

fcFlag = true;

}

break;

default:

}// switch

//===============================================

int Init(void) {

GPIO_InitTypeDef GPIO_InitStruct;

/*######## Configure the system clock to 84 MHz ############################*/

SystemClock_Config();

/*######## Turn on GPIO clocks ############################*/

__GPIOA_CLK_ENABLE();

__GPIOB_CLK_ENABLE();

__GPIOC_CLK_ENABLE();

// Now turn on pin PC8 used for debug.

GPIO_InitStruct.Pin = GPIO_PIN_8;

GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

return 0;

} // end of Init

//=======================================

//===============================================================================

// Private fcns.

/*-------------------------------------------------------------*/

/**

* @brief System Clock Configuration

* The system Clock is configured as follow :

* System Clock source = PLL (HSI)

* SYSCLK(Hz) = 84000000

* HCLK(Hz) = 84000000

* AHB Prescaler = 1

* APB1 Prescaler = 2

* APB2 Prescaler = 1

* HSI Frequency(Hz) = 16000000

* PLL_M = 16

* PLL_N = 336

* PLL_P = 4

* PLL_Q = 7

* VDD(V) = 3.3

* Main regulator output voltage = Scale2 mode

* Flash Latency(WS) = 2

* @param None

* @retval None

*/

static void SystemClock_Config(void)

{

RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_OscInitTypeDef RCC_OscInitStruct;

/* Enable Power Control clock */

__HAL_RCC_PWR_CLK_ENABLE();

/* The voltage scaling allows optimizing the power consumption when the device is

clocked below the maximum system frequency, to update the voltage scaling value

regarding system frequency refer to product datasheet. */

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

/* Enable HSI Oscillator and activate PLL with HSI as source */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 0x10;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLM = 16;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

RCC_OscInitStruct.PLL.PLLQ = 7;

if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2

clocks dividers */

RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)

{

Error_Handler();

}

}

//============================================================

/**

* @brief This function is used to initialize the HAL Library; it must be the first

* instruction to be executed in the main program (before to call any other

* HAL function), it performs the following:

* Configure the Flash prefetch, instruction and Data caches.

* Configures the SysTick to generate an interrupt each 1 millisecond,

* which is clocked by the HSI (at this stage, the clock is not yet

* configured and thus the system is running from the internal HSI at 16 MHz).

* Set NVIC Group Priority to 4.

* Calls the HAL_MspInit() callback function defined in user file

* 'stm32f4xx_hal_msp.c' to do the global low level hardware initialization

*

* @note SysTick is used as time base for the HAL_Delay() function, the application

* need to ensure that the SysTick time base is always set to 1 millisecond

* to have correct HAL operation.

* @retval HAL status

*/

HAL_StatusTypeDef HAL_Init(void)

{

/* Configure Flash prefetch, Instruction cache, Data cache */

#if (INSTRUCTION_CACHE_ENABLE != 0U)

__HAL_FLASH_INSTRUCTION_CACHE_ENABLE();

#endif /* INSTRUCTION_CACHE_ENABLE */

#if (DATA_CACHE_ENABLE != 0U)

__HAL_FLASH_DATA_CACHE_ENABLE();

#endif /* DATA_CACHE_ENABLE */

#if (PREFETCH_ENABLE != 0U)

__HAL_FLASH_PREFETCH_BUFFER_ENABLE();

#endif /* PREFETCH_ENABLE */

/* Set Interrupt Group Priority */

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

/* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */

HAL_InitTick(TICK_INT_PRIORITY);

/* Init the low level hardware */

HAL_MspInit();

/* Return function status */

return HAL_OK;

}
6 REPLIES 6
Artur IWANICKI
ST Employee
Posted on May 25, 2018 at 07:46

Hello,

Is your code generated by STM32CubeMX?

If it is done manually, could you please share complete main.c and sources where I can find HAL_Init() function?

What are the settings of the option bytes of your MCU? Could you please check whether HW option for watchdogs is not activated?

Thank you in advance,

Best Regards,

Artur

Posted on May 25, 2018 at 18:15

Dear Artur:

thanks for your reply. Main loop and Hal_Init have already attached above. Please ref my first post.

I do not use 

STM32CubeMX, but similar with it.

the watchdogs is disabled in my codes (please also ref my first post). 

I really need help for this. It block our all the debug and work.

thanks

roseanne

Artur IWANICKI
ST Employee
Posted on May 28, 2018 at 07:02

Dear Roseanne,

I will analyze your code.

In the meantime, could you please read the option bytes in your MCU using STLink and STLink Utitliy?

I would like to be sure that hardware option of internal watchdogs is not set (if it would be set watchdog will be activated automatically after reset).

Thank you in advance,

Best Regards,

Artur

Posted on May 29, 2018 at 18:58

Dear Artur:

thanks for your following up.

I am not sure your question? we bought the nucleo board, and have not update anything including no touching for jumper. We just use stm32 to download our simple codes. would you please reshape your question in detail?

thanks

roseanne

Artur IWANICKI
ST Employee
Posted on May 30, 2018 at 08:33

Dear Roseanne,

I have copied your code within my project and all is working correctly

:(

Would it be possible for you to pass me complete project, please?

I will have a look at startup file and initial configuration of the MCU (SystemInit() function especially which is called before the main).

Thank you in advance,

Best Regards,

Artur

roseanne qiu
Associate II
Posted on June 14, 2018 at 22:26

Dear Artur:

thanks for your help, and sorry to reply you so late.

at last I found the problem : when we use another PC with the same board and same wire. It does not reset. 

Now I have another problem I would need your help.

I use UART6 as our debug console port, when I set interrupt for both rx and tx , even I input char, no input interrupt happen. I do not know why , if I use non interrupt, it works fine.

1-------------->here our initialization for uart6

static void Configure_Uart6(void) {

// UART 6 talks to the debug

GPIO_InitTypeDef GPIO_InitStruct;

/* Put the USART peripheral in the Asynchronous mode (UART Mode) */

/* UART6 configured as follow:

- Word Length = 8 Bits

- Stop Bit = One Stop bit

- Parity = no parity

- BaudRate = 57600 baud

- Hardware flow control disabled (RTS and CTS signals)

- Tx pin = PC6

- Rx pin = PC7

*/

__GPIOA_CLK_ENABLE();

Uart6Handle.Instance = USART6;

Uart6Handle.Init.BaudRate = 57600; // 115200; // original 57600;

Uart6Handle.Init.WordLength = UART_WORDLENGTH_8B;

Uart6Handle.Init.StopBits = UART_STOPBITS_2;

Uart6Handle.Init.Parity = UART_PARITY_NONE;

Uart6Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;

Uart6Handle.Init.Mode = UART_MODE_TX_RX;

Uart6Handle.Init.OverSampling = UART_OVERSAMPLING_8;

/* Enable GPIO TX/RX clock */

USART6_TX_GPIO_CLK_ENABLE();

USART6_RX_GPIO_CLK_ENABLE();

/* Enable USARTx clock */

USART6_CLK_ENABLE();

#if 1

/* UART6 TX GPIO pin configuration */

GPIO_InitStruct.Pin = USART6_TX_PIN;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;

GPIO_InitStruct.Alternate = USART6_TX_AF;

HAL_GPIO_Init(USART6_TX_GPIO_PORT, &GPIO_InitStruct);

/* UART6 RX GPIO pin configuration */

GPIO_InitStruct.Pin = USART6_RX_PIN;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Must use this mode!

GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;

GPIO_InitStruct.Alternate = USART6_RX_AF;

HAL_GPIO_Init(USART6_RX_GPIO_PORT, &GPIO_InitStruct);

// Clear the crap out of the receive buffer

HAL_NVIC_ClearPendingIRQ(USART6_IRQn);

__HAL_UART_FLUSH_DRREGISTER(&Uart6Handle);

// Enable IT interrupts on this port

HAL_NVIC_SetPriority(USART6_IRQn, 2, 0);

HAL_NVIC_EnableIRQ(USART6_IRQn);

// Initialize

if(HAL_UART_Init(&Uart6Handle) != HAL_OK)

{

/* Initialization Error */

Error_Handler();

}

}

// register interrupt call back functions for three of UART:1,2,6

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

if (huart->Instance == USART1) {

HAL_UART1_RxCpltCallback(huart);

} else if (huart->Instance == USART2) {

HAL_UART2_RxCpltCallback(huart);

}

if (huart->Instance == USART6) {

HAL_UART6_RxCpltCallback(huart);

}

}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {

if (huart->Instance == USART1) {

HAL_UART1_TxCpltCallback(huart);

} else if (huart->Instance == USART2) {

HAL_UART2_TxCpltCallback(huart);

}

if (huart->Instance == USART6) {

HAL_UART6_TxCpltCallback(huart);

}

}

2. ---------> here is our none interrupt rx case, it work fine, but it block other

modules executions

// non interrupt retrieve

....

if ( HAL_UART_Receive(&Uart6Handle, &flRxChar, 1, 0xFFFF) == HAL_OK )

{

if ( flRxChar == 0x0d )// last one

{

parse_input_cmd(flRxBuf);

flRxSize = flRxCharPtr;

flRxCharPtr = 0;

flRxChar = 0;

flRxBuf[flRxSize++] = '\r';

flRxBuf[flRxSize++] = '\n';

flRxBuf[flRxSize] = '\0';

#if defined(RQIU_DEBUG_SIG)

dbPrintf(flState);

#elif defined(RQIU_DEBUG_RAW)

dbPrintf(flRxBuf);

#elif defined(RQIU_DEBUG_CHAR)

;// dbPrintf(flRxBuf);

#endif

flState = CONSOLE_PARSE;

}

else

{

flRxBuf[flRxCharPtr++] = (char) flRxChar;

flRxChar = 0;

}

3.------ here is interrupt enable, but it does not work when we enter any chars.

...

     HAL_UART_Receive_IT(&Uart6Handle, &flRxChar, 1U);// enable rx interrupt

....

if (flStatus & RX_DONE ||

conNewMsg == 1)

{

//rx char

conNewMsg = 0;

flStatus &= ~RX_DONE;

// process input buffer 

flRxBuf

}

// interrupt call back function:

void HAL_UART6_RxCpltCallback(UART_HandleTypeDef *UartHandle) {

flRxChar = (uint8_t)UartHandle->Instance->DR;

flRxBuf[flRxCharPtr++] = (char) flRxChar;

if (flRxChar == 0x0a ) //0X0d, original is 0x0a, here is for debug to see whether there is interrupt. carriage return

{

// Received final char in message. Set flag.

conNewMsg = 1;

flStatus |= RX_DONE;

flRxSize = flRxCharPtr;

flRxCharPtr = 0;

flRxChar = 0;

}

else

{

// Not done yet. Set up system to receive next char.

__HAL_UART_FLUSH_DRREGISTER(&Uart6Handle);

HAL_UART_Receive_IT(&Uart6Handle, &flRxChar, 1U);

}

return;

}

4.-----> would you please help us? if still not work, can I use block function  workaround(with expire timer?), but still I would like to know the reason. if we could use interrupt, it is more efficent for our 

system.

thanks for your time and help

roseanne