TX Polling no funciona al configurar la interrupción RX stm32f103rc

1

Estaba probando USART en STM32F103RC, baud 115200, sysclk 72MHz, 1 bit de parada, sin paridad y conecté mi stm con usb a serial para ver mi salida en el terminal. Al principio hice el sondeo de RX y TX y envié el comando desde la terminal a stm como "led on" para encender el led y "led off" para apagar el led y funcionó perfectamente con la respuesta que he enviado desde stm. rx y tx funcionaron bien

A continuación, lo que hice fue configurar rx como interrupt y tx as polling. Cuando hice eso, mi rx funciona bien pero mi Tx dejó de funcionar.

Mi código se ve así

USART1 IRQ

 /** @breif: USART1 IRQ
     * @param: None
     * @retVal: None
   */
 void USART1_IRQHandler(void)
 {
     if(USART1->SR & USART_SR_RXNE)
     {
            rx_data[datapos] = USART1->DR;
            datapos++;
            if (datapos > 255 )
            {
                datapos = 0;
                Clear_Buffer(rx_data);
            }
   }  
 }

INICIALIZACIÓN DE USART

 void USART_Init()
 {
     /*Enable Clock for USART port and USART1 */
     RCC->APB2ENR |=  RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN;
     /*Rx,Tx setup GPIOA->CRH = 0x000004B0*/
     GPIOA->CRH |= GPIO_CRH_MODE9;          // Tx Mode- 11: Output mode, max speed 50 MHz.
     GPIOA->CRH |= GPIO_CRH_CNF9_1;         // Tx CNF - 10: Alternate function output Push-pull
     GPIOA->CRH &= ~(GPIO_CRH_MODE10);  // Rx Mode- 00: Input mode (reset state)
     GPIOA->CRH |= GPIO_CRH_CNF10_0;    // Rx CNF - 01: Floating input (reset state)
     /* Baud rate set 72MHz/115200 */
     USART1->BRR = SYSCLK/BAUD;
     /*Enable Rx, Enable Tx, Enable USART*/
     USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
     USART1->CR1 |= USART_CR1_RXNEIE;  //enable rx interrupt
 }

USART TRANSMIT

 /** @breif: Transmit a String
     * @param: the string to be transmitted
     * @retVal: None
   */
 void Transmit_String(char *str)
 {
     while(*str != '
 int main(void)
 {
     Led_Init();
     USART_Init();
     NVIC_Init();
     datapos = 0;
     while(1)
     {
         if(strstr(rx_data,"led on\r"))
         {
                RESET_GPIO_BIT_PORTD(2);                        //Make Led High
                Transmit_String("\nLED is ON\r\n");
              datapos = 0;
              Clear_Buffer(rx_data);
         }
         else if(strstr(rx_data,"led off\r"))
         {
                SET_GPIO_BIT_PORTD(2);                      //Make Led Low
                Transmit_String("\nLED is OFF\r\n");
              datapos = 0;
              Clear_Buffer(rx_data);
         }
         Delay(1000);
     }
 }
') { Transmit_Char(*str); str++; } } /** @breif: Transmit a character * @param: the character to be transmitted * @retVal: None */ void Transmit_Char(char data) { while (!(USART1->SR & USART_SR_TXE)); USART1->DR = data; }

MAIN

 /** @breif: USART1 IRQ
     * @param: None
     * @retVal: None
   */
 void USART1_IRQHandler(void)
 {
     if(USART1->SR & USART_SR_RXNE)
     {
            rx_data[datapos] = USART1->DR;
            datapos++;
            if (datapos > 255 )
            {
                datapos = 0;
                Clear_Buffer(rx_data);
            }
   }  
 }

Con este código, el led se enciende y apaga perfectamente, pero no se transmite nada en el terminal. Supongo que es algo con IRQ, ya que IRQ es para USART1 y no se menciona nada sobre RX o TX.

Pero quiero sondear para Tx y Take interrupt para Rx y simplemente no obtener el procedimiento correcto para hacerlo. Cualquier sugerencia sería útil.

Gracias de antemano.

P.S Puedo proporcionar el código fuente completo si es necesario.

    
pregunta Devjeet Mandal

1 respuesta

0

Esta es la respuesta correcta. Gracias a todos

/* Project: USART(Interrupt RX)
 * Author : Devjeet Mandal   
 * Date   : 15/2/2018       11:48
 * 
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <string.h>

/* Private define ------------------------------------------------------------*/
#define SYSCLK   72000000
#define BAUD            115200

#define LED_PORT_EN()           ( RCC->APB2ENR |= RCC_APB2ENR_IOPDEN )
#define LED_PORT                    GPIOD

#define LED_MODE_BIT1           8
#define LED_MODE_BIT2           9
#define LED_CNF_BIT1            10
#define LED_CNF_BIT2            11

/* Private macro -------------------------------------------------------------*/
#define CNF_SET_PORTD(BIT1,BIT2)            (   LED_PORT->CRL &= ~((1<<BIT1) | (1<<BIT2)) )                 //General purpose output push-pull
#define MODE_SET_PORTD(BIT1,BIT2)           ( LED_PORT->CRL |=  (1<<BIT1) | (1<<BIT2) )                     //Output mode, max speed 50 MHz.

#define SET_GPIO_BIT_PORTD(BIT)             ( LED_PORT->BSRR =  (1 << BIT) )                                            //For setting the Bit   
#define RESET_GPIO_BIT_PORTD(BIT)           ( LED_PORT->BSRR =  ( (1 << BIT) << 16 )    )                       //For Resseting Bit

/* Private function prototypes -----------------------------------------------*/
 void Delay(int ms);
 void Led_Init(void);
 void Blink_Led(int ms);
 void Transmit_Char(char data);
 void Transmit_String(char *str);
 void USART_Init(void);
 void NVIC_Init(void);
 void USART1_IRQHandler(void);
 void Clear_Buffer(char *str);

 /* Private variables ---------------------------------------------------------*/
 __IO uint16_t datapos;
 char rx_data[255];


 int main(void)
 {
     Led_Init();
     USART_Init();
     NVIC_Init();
     datapos = 0;
     while(1)
     {
         if(strstr(rx_data,"led on\r"))
         {
                RESET_GPIO_BIT_PORTD(2);                        //Make Led High
                Transmit_String("\nLED is ON\r\n");
              datapos = 0;
              Clear_Buffer(rx_data);
         }
         else if(strstr(rx_data,"led off\r"))
         {
                SET_GPIO_BIT_PORTD(2);                      //Make Led Low
                Transmit_String("\nLED is OFF\r\n");
              datapos = 0;
              Clear_Buffer(rx_data);
         }
         Delay(1000);
     }
 }

 /** @breif: Clear Buffer
     * @param: Buffer to be cleared
     * @retVal: None
   */
 void Clear_Buffer(char *str)
 {
     while (*str != '
/* Project: USART(Interrupt RX)
 * Author : Devjeet Mandal   
 * Date   : 15/2/2018       11:48
 * 
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <string.h>

/* Private define ------------------------------------------------------------*/
#define SYSCLK   72000000
#define BAUD            115200

#define LED_PORT_EN()           ( RCC->APB2ENR |= RCC_APB2ENR_IOPDEN )
#define LED_PORT                    GPIOD

#define LED_MODE_BIT1           8
#define LED_MODE_BIT2           9
#define LED_CNF_BIT1            10
#define LED_CNF_BIT2            11

/* Private macro -------------------------------------------------------------*/
#define CNF_SET_PORTD(BIT1,BIT2)            (   LED_PORT->CRL &= ~((1<<BIT1) | (1<<BIT2)) )                 //General purpose output push-pull
#define MODE_SET_PORTD(BIT1,BIT2)           ( LED_PORT->CRL |=  (1<<BIT1) | (1<<BIT2) )                     //Output mode, max speed 50 MHz.

#define SET_GPIO_BIT_PORTD(BIT)             ( LED_PORT->BSRR =  (1 << BIT) )                                            //For setting the Bit   
#define RESET_GPIO_BIT_PORTD(BIT)           ( LED_PORT->BSRR =  ( (1 << BIT) << 16 )    )                       //For Resseting Bit

/* Private function prototypes -----------------------------------------------*/
 void Delay(int ms);
 void Led_Init(void);
 void Blink_Led(int ms);
 void Transmit_Char(char data);
 void Transmit_String(char *str);
 void USART_Init(void);
 void NVIC_Init(void);
 void USART1_IRQHandler(void);
 void Clear_Buffer(char *str);

 /* Private variables ---------------------------------------------------------*/
 __IO uint16_t datapos;
 char rx_data[255];


 int main(void)
 {
     Led_Init();
     USART_Init();
     NVIC_Init();
     datapos = 0;
     while(1)
     {
         if(strstr(rx_data,"led on\r"))
         {
                RESET_GPIO_BIT_PORTD(2);                        //Make Led High
                Transmit_String("\nLED is ON\r\n");
              datapos = 0;
              Clear_Buffer(rx_data);
         }
         else if(strstr(rx_data,"led off\r"))
         {
                SET_GPIO_BIT_PORTD(2);                      //Make Led Low
                Transmit_String("\nLED is OFF\r\n");
              datapos = 0;
              Clear_Buffer(rx_data);
         }
         Delay(1000);
     }
 }

 /** @breif: Clear Buffer
     * @param: Buffer to be cleared
     * @retVal: None
   */
 void Clear_Buffer(char *str)
 {
     while (*str != '%pre%')
     {
         *str = 0;
         str++;
     }
 }


 /** @breif: USART1 IRQ
     * @param: None
     * @retVal: None
   */
 void USART1_IRQHandler(void)
 {
     if(USART1->SR & USART_SR_RXNE)
     {
            rx_data[datapos] = USART1->DR;
            datapos++;
            if (datapos > 255 )
            {
                datapos = 0;
                Clear_Buffer(rx_data);
            }
   }  
 }

 /** @breif: Initialize setting for NVIC
     * @param: None
     * @retVal: None
   */
 void NVIC_Init()
 {
     NVIC_EnableIRQ(USART1_IRQn);
     NVIC_SetPriority(USART1_IRQn, 0);
 }

 /** @breif: Initialize setting for Usart
     * @param: None
     * @retVal: None
   */

 void USART_Init()
 {
     /*Enable Clock for USART port and USART1 */
     RCC->APB2ENR |=  RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN;
     /*Rx,Tx setup GPIOA->CRH = 0x000004B0*/
     GPIOA->CRH |= GPIO_CRH_MODE9;          // Tx Mode- 11: Output mode, max speed 50 MHz.
     GPIOA->CRH |= GPIO_CRH_CNF9_1;         // Tx CNF - 10: Alternate function output Push-pull
     GPIOA->CRH &= ~(GPIO_CRH_CNF9_0);

     GPIOA->CRH &= ~(GPIO_CRH_MODE10);  // Rx Mode- 00: Input mode (reset state)
     GPIOA->CRH |= GPIO_CRH_CNF10_0;    // Rx CNF - 01: Floating input (reset state)*/
     /* Baud rate set 72MHz/115200 */
     USART1->BRR = SYSCLK/BAUD;
     /*Enable Rx, Enable Tx, Enable USART*/
     USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
     USART1->CR1 |= USART_CR1_RXNEIE;  //enable rx interrupt
 }


  /** @breif: Transmit a String
     * @param: the string to be transmitted
     * @retVal: None
   */
 void Transmit_String(char *str)
 {
     while(*str != '%pre%')
     {
         Transmit_Char(*str);
         str++;
     }
 }

 /** @breif: Transmit a character
     * @param: the character to be transmitted
     * @retVal: None
   */

void Transmit_Char(char data)
{
    while (!(USART1->SR & USART_SR_TXE));
    USART1->DR = data;
}


/** @breif: For wait and doing nothing i.e for delay
     * @param: delaya time
     * @retVal: None
   */


    void Delay(int ms)
    {
        int i,j; 
        for (i = 0; i < ms; ++i) {
            for (j = 0; j < 1000; ++j);
            }
    }


 /** @breif: Initalize GPIO For Led
     * @param: None
     * @retVal: None
   */


    void Led_Init()
    {
        LED_PORT_EN();                                                              //Enable RCC for Led Port
        CNF_SET_PORTD(LED_CNF_BIT1,LED_CNF_BIT2);           //SET CNF       General purpose output push-pull
        MODE_SET_PORTD(LED_MODE_BIT1,LED_MODE_BIT2);    //SET MODE  Output mode, max speed 50 MHz. 
    }


 /** @breif: Blink Led Placed in PORT D Pin 2
     * @param: Delay for each state(ON/OFF)
     * @retVal: None
   */


    void Blink_Led(int ms)
    {
        RESET_GPIO_BIT_PORTD(2);                        //Make Led High
        Delay(ms);                                                  //wait
        SET_GPIO_BIT_PORTD(2);                          //Make Led Low
        Delay(ms);                                                  //wait
    }
') { *str = 0; str++; } } /** @breif: USART1 IRQ * @param: None * @retVal: None */ void USART1_IRQHandler(void) { if(USART1->SR & USART_SR_RXNE) { rx_data[datapos] = USART1->DR; datapos++; if (datapos > 255 ) { datapos = 0; Clear_Buffer(rx_data); } } } /** @breif: Initialize setting for NVIC * @param: None * @retVal: None */ void NVIC_Init() { NVIC_EnableIRQ(USART1_IRQn); NVIC_SetPriority(USART1_IRQn, 0); } /** @breif: Initialize setting for Usart * @param: None * @retVal: None */ void USART_Init() { /*Enable Clock for USART port and USART1 */ RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN; /*Rx,Tx setup GPIOA->CRH = 0x000004B0*/ GPIOA->CRH |= GPIO_CRH_MODE9; // Tx Mode- 11: Output mode, max speed 50 MHz. GPIOA->CRH |= GPIO_CRH_CNF9_1; // Tx CNF - 10: Alternate function output Push-pull GPIOA->CRH &= ~(GPIO_CRH_CNF9_0); GPIOA->CRH &= ~(GPIO_CRH_MODE10); // Rx Mode- 00: Input mode (reset state) GPIOA->CRH |= GPIO_CRH_CNF10_0; // Rx CNF - 01: Floating input (reset state)*/ /* Baud rate set 72MHz/115200 */ USART1->BRR = SYSCLK/BAUD; /*Enable Rx, Enable Tx, Enable USART*/ USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE; USART1->CR1 |= USART_CR1_RXNEIE; //enable rx interrupt } /** @breif: Transmit a String * @param: the string to be transmitted * @retVal: None */ void Transmit_String(char *str) { while(*str != '%pre%') { Transmit_Char(*str); str++; } } /** @breif: Transmit a character * @param: the character to be transmitted * @retVal: None */ void Transmit_Char(char data) { while (!(USART1->SR & USART_SR_TXE)); USART1->DR = data; } /** @breif: For wait and doing nothing i.e for delay * @param: delaya time * @retVal: None */ void Delay(int ms) { int i,j; for (i = 0; i < ms; ++i) { for (j = 0; j < 1000; ++j); } } /** @breif: Initalize GPIO For Led * @param: None * @retVal: None */ void Led_Init() { LED_PORT_EN(); //Enable RCC for Led Port CNF_SET_PORTD(LED_CNF_BIT1,LED_CNF_BIT2); //SET CNF General purpose output push-pull MODE_SET_PORTD(LED_MODE_BIT1,LED_MODE_BIT2); //SET MODE Output mode, max speed 50 MHz. } /** @breif: Blink Led Placed in PORT D Pin 2 * @param: Delay for each state(ON/OFF) * @retVal: None */ void Blink_Led(int ms) { RESET_GPIO_BIT_PORTD(2); //Make Led High Delay(ms); //wait SET_GPIO_BIT_PORTD(2); //Make Led Low Delay(ms); //wait }
    
respondido por el Devjeet Mandal

Lea otras preguntas en las etiquetas