I was trying USART on STM32F103RC,baud 115200, sysclk 72MHz, 1 stop bit, no parity and i connected my stm with usb to serial to look my output on terminal. At first i did polling of RX and TX and send command from terminal to stm like "led on" to turn on led and "led off" to turn off led and it worked perfectly with the response i have send from stm. rx and tx worked fine
Next what i did was i configured rx as interrupt and tx as polling.when i did that my rx is working fine but my Tx stopped working.
My code looks like this
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);
}
}
}
USART INITIALIZATION
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 != '\0')
{
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
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);
}
}
With this code led is turned on and off perfectly but Nothing is transmitted on terminal. I guess it's something with IRQ as IRQ is for USART1 and not mentioned anything about either it's for RX or TX.
But i want to poll for Tx and Take interrupt for Rx and just not getting the correct procedure to do that. Any suggestion would be helpful.
Thanks in advance.
P.S I can provide full source code if required.