cancel
Showing results for 
Search instead for 
Did you mean: 

Using SysTick causes reset?

SBona.2
Associate II

UPDATE: The issue has been solved. The nBFB2 option bit had to be enabled.

Hello, I am currently programming and STM32L152RE on a Nucleo development board. I am using SysTick as a .5s timer that ran a status LED. Everything was working up until recently when I noticed the LED stopped working, and the processor seemed to be restarting. Stepping through with a debugger showed it was able to run all the setup code correctly, but when I let it run to get to the SysTick_Handler(), it would instead go to the reset handler. I also verified it is caused by SysTick as the code does not crash when I do not use it. I also checked the RCC_CR register after the reset to determine the cause, and only the PORRSTF and PINRSTF bits are set, which didn't help me. Here is the code:

int main(void)
{
	clk_init();
	statusLedInit();
        // Turn status LED on
	GPIOA->ODR |= GPIO_ODR_ODR_5;
	usartInit();
	SysTick_Config(SYS_TCK_RELOAD_VAL);
	int val = 0;
	printStr("Hello World!\n");
	while (1)
	{
		val += 1;
	}
}
 
void clk_init()
{
	// Turn the HSI on
	RCC->CR |= RCC_CR_HSION;
	// Wait for it to be stabalized
	while ((RCC->CR & RCC_CR_HSIRDY) == 0)
		;
	// Set HSI to the sys clock
	RCC->CFGR = (RCC->CFGR & ~(RCC_CFGR_SW_Msk)) | 0x1;
	// If it was succesful, clock should now be HSI
	while ((RCC->CFGR & RCC_CFGR_SWS_Msk) == 0x1)
		;
	// Turn the MSI off
	RCC->CR &= ~RCC_CR_MSION;
}

I can include usartInit() and statusLedInit() if needed, but given it crashes regardless if they are there they don't seem relevant. I've also tried erasing the chip and reprogramming, but that did not work either. Thanks for any help.

1 ACCEPTED SOLUTION

Accepted Solutions

> It is still going to the bootloader

it's probably not sure to your code, but hardware (BOOT0 pin) or option bits settings. Look into AN2606.

JW

View solution in original post

10 REPLIES 10
berendi
Principal

What is in SysTick_Handler() ?

If you set a breakpoint on SysTick_Handler(), is it reached, or crashes before that?

Where does the stack pointer point to?

Compare the value of the SysTick vector at flash address 0x0800003C with the actual address of SysTick_Handler in the map file.

Here is what's inside systick handler:

void SysTick_Handler()
{
	counterVal += 1;
	GPIOA->ODR ^= GPIO_ODR_ODR_5;
}

If I set a break point on it, it does not reach it.

The stack pointer is at the top of my stack on the reset.

Here is the map file, and it appears the handler is at 0x0800028c:

0693W000000VkRoQAK.png

Could this be the issue?

> The stack pointer is at the top of my stack on the reset.

What's its value?

> the handler is at 0x0800028c:

Okay, and how does the vector table (starting at 0x0800'0000) look like in disasm?

JW

Actually I must've misread the stack pointer, it is nowhere near the top of the stack. It's at 0x20000f38, and the top of stack is 0x20004ffc. Which is odd considering all I'm doing before the systick exception is called is incrementing a value and printing text. I also noticed the LR is all F's, which is odd. I'm not sure of a circumstance that would happen. Here's a screenshot:

0693W000000VkuvQAC.png

The vector table looks like this (note the address of SysTick_Handler is different because I changed some other code a bit:0693W000000Vkv0QAC.png

The vector table has 0x0800028d, and the address of SysTick is 0x0800028c. 0x28d seems reasonable considering I'm compiling with thumb, which does 16-bit instructions if I remember correctly.

PC=0x1FF00348 means that it is executing the ROM bootloader, not your program. This explains the odd SP value, according to AN2606 the ROM uses the first 4K of the internal SRAM, i.e. 0x20000000 to 0x20000FFF. The question is of course why did it enter the ROM bootloader?

> the top of stack is 0x20004ffc

The MCU has 80k of RAM, 0x20000000 to 0x20013FFF. Are you using some custom memory layout?

All fault handlers except Reset and Systick are empty in the vector table, why?

Lots of unusual stuff. What kind of toolchain is that? Where does your startup code and vector table come from?

I am using a custom memory layout. My top of stack is 0x20004ffc, then the rest of RAM is the data and bss section. The actual startup script I wrote myself, but the vector table portion was copied from one of the startup scripts that was included in the STM32CubeL1 software package. Here's that vector table:

vtable:
	.word _estack
  	.word reset_handler
  	.word NMI_Handler
  	.word HardFault_Handler
  	.word MemManage_Handler
  	.word BusFault_Handler
  	.word UsageFault_Handler
  	.word 0
  	.word 0
  	.word 0
  	.word 0
  	.word SVC_Handler
  	.word DebugMon_Handler
  	.word 0
  	.word PendSV_Handler
  	.word SysTick_Handler
  	.word WWDG_IRQHandler
  	.word PVD_IRQHandler
  	.word TAMPER_STAMP_IRQHandler
  	.word RTC_WKUP_IRQHandler
  	.word FLASH_IRQHandler
  	.word RCC_IRQHandler
  	.word EXTI0_IRQHandler
  	.word EXTI1_IRQHandler
  	.word EXTI2_IRQHandler
  	.word EXTI3_IRQHandler
  	.word EXTI4_IRQHandler
  	.word DMA1_Channel1_IRQHandler
  	.word DMA1_Channel2_IRQHandler
  	.word DMA1_Channel3_IRQHandler
  	.word DMA1_Channel4_IRQHandler
  	.word DMA1_Channel5_IRQHandler
  	.word DMA1_Channel6_IRQHandler
  	.word DMA1_Channel7_IRQHandler
  	.word ADC1_IRQHandler
  	.word USB_HP_IRQHandler
  	.word USB_LP_IRQHandler
  	.word DAC_IRQHandler
  	.word COMP_IRQHandler
  	.word EXTI9_5_IRQHandler
  	.word LCD_IRQHandler  
  	.word TIM9_IRQHandler
  	.word TIM10_IRQHandler
  	.word TIM11_IRQHandler
  	.word TIM2_IRQHandler
  	.word TIM3_IRQHandler
  	.word TIM4_IRQHandler
  	.word I2C1_EV_IRQHandler
  	.word I2C1_ER_IRQHandler
  	.word I2C2_EV_IRQHandler
  	.word I2C2_ER_IRQHandler
  	.word SPI1_IRQHandler
  	.word SPI2_IRQHandler
  	.word USART1_IRQHandler
  	.word USART2_IRQHandler
  	.word USART3_IRQHandler
  	.word EXTI15_10_IRQHandler
  	.word RTC_Alarm_IRQHandler
  	.word USB_FS_WKUP_IRQHandler
  	.word TIM6_IRQHandler
  	.word TIM7_IRQHandler
  	.word 0
  	.word TIM5_IRQHandler
  	.word SPI3_IRQHandler
  	.word UART4_IRQHandler
  	.word UART5_IRQHandler
  	.word DMA2_Channel1_IRQHandler
  	.word DMA2_Channel2_IRQHandler
  	.word DMA2_Channel3_IRQHandler
  	.word DMA2_Channel4_IRQHandler
  	.word DMA2_Channel5_IRQHandler
  	.word 0
  	.word COMP_ACQ_IRQHandler
  	.word 0
  	.word 0
  	.word 0
  	.word 0
  	.word 0
	
	// Dummy exception handlers
	.weak NMI_Handler     
	                B       .
	.weak HardFault_Handler
	                B       .
	.weak MemManage_Handler
	                 B       .
	.weak BusFault_Handler
	                 B       .
	.weak UsageFault_Handler
	                 B       .
	.weak SVC_Handler     
	                 B       .
	.weak DebugMon_Handler
	                 B       .
	.weak PendSV_Handler  
	                 B       .
	.weak SysTick_Handler 
	                B       .
	
	/* Defining all the external interupt handlers as weak
	 * This allows us to override them but simply having a function
	 * for each handler, while still maintaining a default handler
	 */
 
  	.weak WWDG_IRQHandler
  	.thumb_set WWDG_IRQHandler,default_interrupt_handler
 
  	.weak PVD_IRQHandler
  	.thumb_set PVD_IRQHandler,default_interrupt_handler
 
  	.weak TAMPER_STAMP_IRQHandler
  	.thumb_set TAMPER_STAMP_IRQHandler,default_interrupt_handler
 
  	.weak RTC_WKUP_IRQHandler
  	.thumb_set RTC_WKUP_IRQHandler,default_interrupt_handler
 
  	.weak FLASH_IRQHandler
  	.thumb_set FLASH_IRQHandler,default_interrupt_handler
 
  	.weak RCC_IRQHandler
  	.thumb_set RCC_IRQHandler,default_interrupt_handler
 
  	.weak EXTI0_IRQHandler
  	.thumb_set EXTI0_IRQHandler,default_interrupt_handler
 
  	.weak EXTI1_IRQHandler
  	.thumb_set EXTI1_IRQHandler,default_interrupt_handler
 
  	.weak EXTI2_IRQHandler
  	.thumb_set EXTI2_IRQHandler,default_interrupt_handler
 
  	.weak EXTI3_IRQHandler
  	.thumb_set EXTI3_IRQHandler,default_interrupt_handler
 
  	.weak EXTI4_IRQHandler
  	.thumb_set EXTI4_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel1_IRQHandler
  	.thumb_set DMA1_Channel1_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel2_IRQHandler
  	.thumb_set DMA1_Channel2_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel3_IRQHandler
  	.thumb_set DMA1_Channel3_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel4_IRQHandler
  	.thumb_set DMA1_Channel4_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel5_IRQHandler
  	.thumb_set DMA1_Channel5_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel6_IRQHandler
  	.thumb_set DMA1_Channel6_IRQHandler,default_interrupt_handler
 
  	.weak DMA1_Channel7_IRQHandler
  	.thumb_set DMA1_Channel7_IRQHandler,default_interrupt_handler
 
  	.weak ADC1_IRQHandler
  	.thumb_set ADC1_IRQHandler,default_interrupt_handler
 
  	.weak USB_HP_IRQHandler
  	.thumb_set USB_HP_IRQHandler,default_interrupt_handler
 
  	.weak USB_LP_IRQHandler
  	.thumb_set USB_LP_IRQHandler,default_interrupt_handler
 
  	.weak DAC_IRQHandler
  	.thumb_set DAC_IRQHandler,default_interrupt_handler
 
  	.weak COMP_IRQHandler
  	.thumb_set COMP_IRQHandler,default_interrupt_handler
 
  	.weak EXTI9_5_IRQHandler
  	.thumb_set EXTI9_5_IRQHandler,default_interrupt_handler
 
  	.weak LCD_IRQHandler
  	.thumb_set LCD_IRQHandler,default_interrupt_handler  
 
  	.weak TIM9_IRQHandler
  	.thumb_set TIM9_IRQHandler,default_interrupt_handler
 
  	.weak TIM10_IRQHandler
  	.thumb_set TIM10_IRQHandler,default_interrupt_handler
 
  	.weak TIM11_IRQHandler
  	.thumb_set TIM11_IRQHandler,default_interrupt_handler
 
  	.weak TIM2_IRQHandler
  	.thumb_set TIM2_IRQHandler,default_interrupt_handler
 
  	.weak TIM3_IRQHandler
  	.thumb_set TIM3_IRQHandler,default_interrupt_handler
 
  	.weak TIM4_IRQHandler
  	.thumb_set TIM4_IRQHandler,default_interrupt_handler
 
  	.weak I2C1_EV_IRQHandler
  	.thumb_set I2C1_EV_IRQHandler,default_interrupt_handler
 
  	.weak I2C1_ER_IRQHandler
  	.thumb_set I2C1_ER_IRQHandler,default_interrupt_handler
 
  	.weak I2C2_EV_IRQHandler
  	.thumb_set I2C2_EV_IRQHandler,default_interrupt_handler
 
  	.weak I2C2_ER_IRQHandler
  	.thumb_set I2C2_ER_IRQHandler,default_interrupt_handler
 
  	.weak SPI1_IRQHandler
  	.thumb_set SPI1_IRQHandler,default_interrupt_handler
 
  	.weak SPI2_IRQHandler
  	.thumb_set SPI2_IRQHandler,default_interrupt_handler
 
  	.weak USART1_IRQHandler
  	.thumb_set USART1_IRQHandler,default_interrupt_handler
 
  	.weak USART2_IRQHandler
  	.thumb_set USART2_IRQHandler,default_interrupt_handler
 
  	.weak USART3_IRQHandler
  	.thumb_set USART3_IRQHandler,default_interrupt_handler
 
  	.weak EXTI15_10_IRQHandler
  	.thumb_set EXTI15_10_IRQHandler,default_interrupt_handler
 
  	.weak RTC_Alarm_IRQHandler
  	.thumb_set RTC_Alarm_IRQHandler,default_interrupt_handler
 
  	.weak USB_FS_WKUP_IRQHandler
  	.thumb_set USB_FS_WKUP_IRQHandler,default_interrupt_handler
 
  	.weak TIM6_IRQHandler
  	.thumb_set TIM6_IRQHandler,default_interrupt_handler
 
  	.weak TIM7_IRQHandler
  	.thumb_set TIM7_IRQHandler,default_interrupt_handler
 
  	.weak TIM5_IRQHandler
  	.thumb_set TIM5_IRQHandler,default_interrupt_handler
  
  	.weak SPI3_IRQHandler
  	.thumb_set SPI3_IRQHandler,default_interrupt_handler
 
  	.weak UART4_IRQHandler
  	.thumb_set UART4_IRQHandler,default_interrupt_handler
 
  	.weak UART5_IRQHandler
  	.thumb_set UART5_IRQHandler,default_interrupt_handler
  
  	.weak DMA2_Channel1_IRQHandler
  	.thumb_set DMA2_Channel1_IRQHandler,default_interrupt_handler
 
  	.weak DMA2_Channel2_IRQHandler
  	.thumb_set DMA2_Channel2_IRQHandler,default_interrupt_handler
 
  	.weak DMA2_Channel3_IRQHandler
  	.thumb_set DMA2_Channel3_IRQHandler,default_interrupt_handler
 
  	.weak DMA2_Channel4_IRQHandler
  	.thumb_set DMA2_Channel4_IRQHandler,default_interrupt_handler
 
  	.weak DMA2_Channel5_IRQHandler
  	.thumb_set DMA2_Channel5_IRQHandler,default_interrupt_handler
 
  	.weak COMP_ACQ_IRQHandler
  	.thumb_set COMP_ACQ_IRQHandler,default_interrupt_handler

So I'm not sure why they're empty on the chip. And for toolchain I'm using using arm-none-eabi-gcc and a custom makefile.

.weak NMI_Handler     
	                B       .

I think this does not actually define a symbol for NMI_Handler, that's why the vectors are empty. Try it this way for all handlers:

.weak NMI_Handler     
	NMI_Handler:
	                B       .

It would not solve the problem, but at least we'd see which fault is triggered.

Can you create a minimal example that exhibits the problem and upload the whole project?

Doing that populated the vector table, but it's not getting caught in any of those. It is still going to the bootloader. I attached a minimal example. Here's a link: https://drive.google.com/file/d/1pXD28OMzdRnm0EvA2I5Q_WGOHI2bIDoo/view?usp=sharing

> It is still going to the bootloader

it's probably not sure to your code, but hardware (BOOT0 pin) or option bits settings. Look into AN2606.

JW