Skip to main content
cai.bai
Associate II
Sep 12, 2018 7:31 AM UTC
Question

hard fault after configure systick

i use NUCLEO - F767ZI and test a simple code as follow

int sum(int a,int b){
 
 return a+b;
}
 
int main(void){
 
 int dly;
 
 __enable_irq();
 SysTick_Config(100000);
 NVIC_EnableIRQ(SysTick_IRQn);
 
 dly = sum(1,3);
 return 0;
}

i debug it and when call sum(1,3), it will goto hard fault.

if comment ​NVIC_EnableIRQ(SysTick_IRQn); call and it work fine.

can someone help me ?

This topic has been closed for replies.

14 replies

waclawek.jan
Super User
Sep 12, 2018 6:02 PM UTC

> The NVIC_EnableIRQ doesn't work for SysTick!!!!

Very well spotted, Tomas! - essentially the same thing Clive said but yes, it's imperative. And the comment at NVIC_EnableIRQ() says that, too.

----- [following is not reply to the original post]

Out of curiosity I had a look at NVIC_EnableIRQ() in the CMSIS headers I'm using for ages (namely  

* @file    core_cm4.h

 * @brief   CMSIS Cortex-M4 Core Peripheral Access Layer Header File

 * @version V2.10

:(

static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
{
/* NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); enable interrupt */
 NVIC->ISER[(uint32_t)((int32_t)IRQn) >> 5] = (uint32_t)(1 << ((uint32_t)((int32_t)IRQn) & (uint32_t)0x1F)); /* enable interrupt */
}

Wow. IRQn_Type is an enum and signedness of that is implementation-defined; however, I don't quite get what was the benefit of the double cast (clearly it won't "heal" the improper use with SysTick and other "negative-numbered" exceptions). (In newer CMSIS the commented-out line is omitted.)

I'm apparently not alone: https://github.com/ARM-software/CMSIS_5/issues/248

JW

Tesla DeLorean
Guru
Sep 12, 2018 7:27 PM UTC

I was perhaps a little too subtle there, herding cats, should have pulled the cattle prod....

It abends because the negative pointer math is super bad.

0690X000006C1BMQA0.jpg

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Sep 12, 2018 10:36 PM UTC

In more updated CubeMX versions, it says "Value cannot be negative" in comment.

https://github.com/pavel-a/stm32f4_libs/blob/c4560650ddaea6ee33de171027d801a31109565a/STM32Cube_FW_F4/Drivers/CMSIS/Include/core_cm7.h#L1832

GCC compatible compiler even could assert this in compile time ( if (__builtin_constant_p(IRQn) ) _Static_assert (IRQn > 0) ... )

-- pa

waclawek.jan
Super User
Sep 12, 2018 10:49 PM UTC

> GCC compatible compiler even could assert

Good idea, but 1. it should be told to ARM as this is in CMSIS; 2. _Static_assert is C11, so I guess this would work only if gcc is run with some of the C11-related -std switch.

JW