Pregunta

I develop on a map I'm working on STM32 and USART interrupts. After configuring the USART1 and make Enable receive interrupt. The problem that the interruption of reception have no detected????

¿Fue útil?

Solución

Such a question is difficult to answer without knowing which specific processor you are using, which board you are using, and/or which compiler you are using. But in an attempt to be helpful, here's my code.

Here's my GPIO and NVIC initialization code using Sourcery CodeBench Lite with an STM32F4 processor mounted on a custom board.

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);

// Enable the USART RX Interrupt 
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

Of course your settings will vary depending on your processor, board and interrupt priority.

Here's my interrupt handler code. In my development environment, this handler is declared in my startup assembly file as a weak reference to Default_Handler...

Default_Handler:
b .  

/* ... */

.word     USART3_IRQHandler

/* ... */

.weak      USART3_IRQHandler      
.thumb_set USART3_IRQHandler,Default_Handler

... so as long as I provide a new declaration and implementation of this interrupt handler, the weak reference will be replaced. Here's what my code looks like.

//Interrupt handler declaration
void USART3_IRQHandler();

If you are using C++ you will need to declare it as follows:

//Interrupt handler declaration in C/C++
#ifdef __cplusplus
 extern "C" {
#endif
void USART3_IRQHandler();
#ifdef __cplusplus
 }
#endif

And here's the interrupt handler implemenation.

//Interrupt handler implementation
void USART3_IRQHandler()
{
    //handle interrupt
}

Otros consejos

Here's short and simple code to configure STM32 USART (USART3) and Interrupt Handler.

Configure and Init

    void Init_USART3()
   {
     // PB.11 = RX    Floating Input
     // PB.10 = TX    Alternate function output Push-pull 50 MHz
     RCC->APB2ENR = RCC->APB2ENR | (RCC_APB2ENR_IOPBEN);
     RCC->APB1ENR |= RCC_APB1ENR_USART3EN;      // enable clock for  USART3.

     GPIOB->CRH = GPIOB->CRH & 0xFFFF00FF;
     GPIOB->CRH = GPIOB->CRH | 0x00004B00;

     USART3->BRR  =72000000/9600;                    // set baudrate. 
     USART3->CR1  |= (USART_CR1_RE | USART_CR1_TE);  // RX, TX enable. 
     USART3->CR1  |= USART_CR1_UE;                   // USART3 enable.  
     USART3->CR1  |= USART_CR1_RXNEIE;    // UART3 Receive Interrupt Enable.
     // Enable interrupt fromUSART1(NVIC level) 
     NVIC_EnableIRQ(USART3_IRQn);   
   }

Handle Receive Interrupt

void USART3_IRQHandler()
{
  if(USART3->SR & USART_SR_RXNE)
  {
    // Do Something
  }
}

Make sure you don't call HAL_UART_Transmit() on the same usart that you try interrupt. It is because this function calls UART_WaitOnFlagUntilTimeout() which disables the interrupt. The trace printf() that user7404301 mentioned above most likely calls it.

SAMPLE CODING TO HANDLE RECEIVE INTERRUPT

//USART1 Interrupt Handler
    void USART1_IRQHandler(void)
    {
      if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//enter interrupt when STM32 receice data.
      {
         USART_ClearITPendingBit(USART1, USART_IT_RXNE);
         USART_Temp_Data = (unsigned char) USART_ReceiveData(USART1); //receive a char
      }
    }

I had the same question with Eclipse(GCC)before and finally I found out the problem.The problem is not at the code but the "trace_printf", if you are using this API to print any details while running, the "trace_printf" will break the uart and your receive interrupt will never ocurrs.So, have a try, not use it and set breakpoints to see what you have recveived.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top