NVIC_IntEnable hangs code - STM32F407

General discussion on mikroC PRO for ARM.
Post Reply
Author
Message
Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

NVIC_IntEnable hangs code - STM32F407

#1 Post by Sparky1039 » 31 Oct 2014 07:51

Weird problem.
In a short initialization function for setting up USART1 interrupt capability, when this command is called it hangs the code.

Code: Select all

NVIC_IntEnable(IVT_INT_USART1);
If I comment out this line the code continues.
I have no explanation why. Perhaps mE can enlighten me.

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: NVIC_IntEnable hangs code - STM32F407

#2 Post by marina.petrovic » 31 Oct 2014 10:48

Hi,

This routine should work without any problem (it simple enable proper interrupt vector).

For example, you can take a look at Bluetooth click example where UART interrupt is used
(for STM32F107VC, but it can be easily adjusted for STM32F407):
http://www.libstock.com/projects/view/2 ... ck-example

You can send me some simple project which demonstrate problem which you have so I can test it on my system?

Brest regards,
Marina

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: NVIC_IntEnable hangs code - STM32F407

#3 Post by Sparky1039 » 31 Oct 2014 23:57

Beats me why it hangs, but for some reason the code gets hung at the NVIC_ enable line. The odd thing too is the code gets hung before the "EnableInterrupts();" command but yet the interrupt actually does work.

For example: If I move those lines in the "displayUART_Text()" function between the "//===== " and place them inside the ISR at the bottom of the 'IF' conditional where I have LED test indicators, the display shows the received USART text just fine. This makes it really confusing because technically the EnableInterrupts() function never gets called which in theory NO interrupts should function. But yet it does...

How I determined the NVIC_ enable command is effecting the code flow is by placing an indicator LED at the top of the "displayUART_Text()" function that is set when it's called. As the code is written below, that LED never lights. But when I comment out NVIC_ enable line this LED comes on. Also as a secondary test I commented out the EnableInterrupts(); command to eliminate this as a possibility. The result is the same, the code never enters the "displayUART_Text()" function to light the LED.

STM32F407VG
EasyMx Pro v.7

Code: Select all

#include "TFT_objects.h"
#include "TFT_resources.h"

sbit UART_Int_LED at GPIOD_ODR.B8;
sbit UART_msgFlag_LED at GPIOD_ODR.B9;

void displayUART_Text (void);
void UART1_RX_Init (void);

unsigned char RX_msgArray [32] = {0x00};
unsigned char inBuffer = 0;
unsigned char RX_ByteCount = 0;
unsigned char RX_msgFlag = 0;

//==================================================================================================================
//==================================================================================================================
void main() 
{
  GPIO_Digital_Output(&GPIOD_ODR, _GPIO_PINMASK_ALL);        // Set PORTD as output
  UART_Int_LED = 0;
  UART_msgFlag_LED = 0;
  
    UART1_Init(115200);

    UART1_Write_Text("UART RX Int Test\r\n");
    
    Start_TP();
    
    TFT_Fill_Screen(CL_NAVY);
    TFT_Set_Font(&Comic_Sans_MS_Regular_24x28, CL_SILVER, FO_HORIZONTAL);

    TFT_Write_Text("UART RX Int TEST", 10, 60);
    delay_ms (1000);
    TFT_Fill_Screen(CL_NAVY);

    UART1_RX_Init();
    displayUART_Text();

  while (1);
  
}//!

/********************************************************************************************************
* Name: INT_UART1_RX
* Description: ISR for receiving one byte from the UART
* Arguments: none
* Returns: none
********************************************************************************************************/
void INT_UART1_RX() iv IVT_INT_USART1 ics ICS_AUTO
{
  if (USART1_SRbits.RXNE)
    {
     USART1_SRbits.RXNE = 0; // clear interrupt intflag
     inBuffer = USART1_DR; // read receive buffer

      if (inBuffer == 0x0D)
        {
          RX_msgArray[RX_ByteCount] = inBuffer;
          inBuffer = 0x00;
          RX_ByteCount++;
          RX_msgArray[RX_ByteCount] = 0x00; // add null termination to string array
          RX_msgFlag = 1;  // set flag indicating a complete message has been received

          GPIOD_ODR = RX_ByteCount; // <<< TEST - LED indicator for # received bytes >>>
          UART1_Write_Text(&RX_msgArray[0]); // echo received text string. << TEST >>
        }
      else
        {
          RX_msgArray[RX_ByteCount] = inBuffer;
          RX_ByteCount++;
          inBuffer = 0x00;
          RX_msgFlag = 0x00;
        }
    }
}//!

/*******************************************************************************
* Name: UART1_RX_Init
* Description: Function sets up UART1 for interrupt driven RX
* Arguments: Nothing
* Returns: Nothing
*******************************************************************************/
void UART1_RX_Init (void)
{
  UART1_Init(115200);
  USART1_CR1.RXNEIE = 1; // enable USART1 RX interrupt
  NVIC_IntEnable(IVT_INT_USART1);
  EnableInterrupts();
}//!

/********************************************************************************************************
* Name: displayUART_Text
* Description: display received USART text 
* Arguments: none
* Returns: none
********************************************************************************************************/
void displayUART_Text (void)
{
  RX_msgFlag = 0;
  RX_ByteCount = 0;
  UART_Int_LED = 1; // indicate this function has been entered << test LED >>
  
  while (1)
  {
    while (!RX_msgFlag);  // loop here waiting for a \r terminated message string to arrive

//=====
    TFT_Write_Text(&RX_msgArray[0], 10, 60);  // display received string for a few seconds
    delay_ms (3000);
    TFT_Fill_Screen(CL_NAVY); // clear the screen
    RX_msgFlag = 0; // reset flag and counter
    RX_ByteCount = 0;
//=====
  }
}//!

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: NVIC_IntEnable hangs code - STM32F407

#4 Post by yo2lio » 03 Nov 2014 09:55

Hi,

I have a question for Mikroe team:

Why need to use EnableInterrupts(); after NVIC_IntEnable(IVT_INT_USART1);

NVIC_IntEnable(IVT_INT_USART1); will enable the USART1 interrupt and will works without EnableInterrupts();

In my code, I have two interrupts enabled with NVIC_IntEnable and interrupts RUN without EnableInterrupts();
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: NVIC_IntEnable hangs code - STM32F407

#5 Post by marina.petrovic » 04 Nov 2014 15:01

Hi,

Indeed, like yo2lio it's not mandatory to use EnableInterrupts() routine in every situation.
EnableInterrupts() is compiler in-built routine which enables the processor interrupt and allows the processor to respond to interrupts.
This does not affect the set of interrupts enabled in the interrupt controller - it just gates the single interrupt from the controller
to the processor.

Did you tried simple UART interrupt example on your hardware (without TFT and other parts of the code),
just to make sure that interrupt is working properly?

You can search our forum, some of our users posted their projects which can help you.

Best regards,
Marina

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: NVIC_IntEnable hangs code - STM32F407

#6 Post by Sparky1039 » 04 Nov 2014 16:09

Marina,
it's not mandatory to use EnableInterrupts() routine in every situation.
Ok, which situations are they and why? The compiler documentation does not say anything about this, how does mE expect it's users to fully utilize the interrupt capability unless it's documented? Do you expect us to be mind readers?
This does not affect the set of interrupts enabled in the interrupt controller - it just gates the single interrupt from the controller
to the processor.
This answer is taken directly from the help file and is very confusing. Please explain in more detail what exactly does this mean.

But back to my problem of the code hanging... as requested I provided an example for you that exhibits the interrupt hang problem but you have not answered the original question this forum post was generated for. Have you tested it and what were the results?

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: NVIC_IntEnable hangs code - STM32F407

#7 Post by marina.petrovic » 05 Nov 2014 11:48

Hi,

Sorry if I didn't explained well in my previous answer.

In-built EnableInterrupts() routine basically enable "global" interrupts and NVIC_IntEnable() enable specific interrupt.
By default (after reset), "global" interrupts are enabled so you only need to enable specific interrupt which you want to use,
but, for example, if you have some larger project and in some moment you disable interrupt, you need to also call EnableInterrupts() routine to make sure that interrupt controller is enabled.

Because of that in some smaller project, we do not use EnableInterrupts() routine, but to be sure you can use it
(that shouldn't create an error).

I will pass to my colleagues in charge suggestion about adding some additional explanation in compiler Help file.

Best regards,
Marina

engr.talha72
Posts: 15
Joined: 05 Sep 2018 08:48

Re: NVIC_IntEnable hangs code - STM32F407

#8 Post by engr.talha72 » 11 May 2022 07:37

After all those years. I have encountered same problem. I am using 4 UART ports on STM32F407Vgt6 MCU.. UART 1 and 2 work fine with "NVIC_IntEnable" function. but as soon as in enable "NVIC_IntEnable" for UART 3 4 or 5. my code stops and doesnt operate. Disabling "NVIC_IntEnable" for UART 3 and 4 results in normal function. But is need interrupts for UART 3 and 4. kindly help.


void usart_init(void){


UART1_Init_Advanced(115200, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART1_PA9_10); ///TX: PA9, RX: PA10
USART1_CR1bits.RXNEIE = 1; // enable uart rx interrupt
NVIC_IntEnable(IVT_INT_USART1);


UART2_Init_Advanced(9600, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART2_PD56); ///TX: PD5, RX: PD6
USART2_CR1bits.RXNEIE = 1; // enable uart rx interrupt
NVIC_IntEnable(IVT_INT_USART2);


UART3_Init_Advanced(9600, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART3_PD89); ///TX: PD8, RX: PD9
USART3_CR1bits.RXNEIE = 1; // enable uart rx interrupt
NVIC_IntEnable( IVT_INT_USART3 );


UART4_Init_Advanced(9600, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_UART4_PC10_11); ///TX: PC10, RX: PC11
UART4_CR1bits.RXNEIE = 1; // enable uart rx interrupt
NVIC_IntEnable(IVT_INT_UART4);



}

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

Re: NVIC_IntEnable hangs code - STM32F407

#9 Post by Thomas.Pahl@t-online.de » 13 May 2022 06:18

show us your interrupt service routine. The problem is always there.

Post Reply

Return to “mikroC PRO for ARM General”