AnsweredAssumed Answered

STM32F100 UART problem

Question asked by nirmal.sumanth on Sep 25, 2012
Latest reply on Sep 27, 2012 by Clive One

Hello,

I'm currently trying to implement a UART driver with interrupt handling for the STM32F100x6 on my Discovery board.
but I am unable to receive or send data and even the interrupts are not getting generated.
check my code and help me out..


void UART_ConfigIni()
{
 
 
 

  /*enable the USART1 peripheral clock */
RCC->APB2ENR = RCC->APB2ENR | RCC_APB2ENR_USART1EN;
/*enable the clock for GPIO port A */
RCC->APB2ENR = RCC->APB2ENR | RCC_APB2ENR_IOPAEN;

/* set the TX as push - pull mode with 50MHz and RX as pull - up mode */
GPIOA->CRH = GPIOA->CRH | GPIO_CRH_MODE9 ;    /* Alternate function output Push-pull*/
GPIOA->CRH = GPIOA->CRH | GPIO_CRH_CNF9_1 ;   /* Output mode, max speed 50 MHz.*/
GPIOA->CRH = GPIOA->CRH | GPIO_CRH_MODE10_1 ; /*Input with pull-up.*/

/* setting the USART_CR1 register*/
USART1->CR1 = USART1->CR1 | USART_CR1_RE;     /* reciever enable*/
USART1->CR1 = USART1->CR1 | USART_CR1_TE ;    /*Transmitter Enable*/
USART1->CR1 = USART1->CR1 | USART_CR1_RXNEIE; /*RXNE Interrupt Enable*/
USART1->CR1 = USART1->CR1 | USART_CR1_TCIE;   /*Transmission Complete Interrupt Enable */
USART1->CR1 = USART1->CR1 | USART_CR1_UE;     /*USART Enable */
USART1->CR1 = USART1->CR1 | USART_CR1_OVER8;  /*USART Oversmapling 8-bits */
USART1->CR1 = USART1->CR1 | USART_CR1_OVER8;
/* by deafult it takes 1 start bit and 8 data bits */

/* USART_CR2 is not set, by default we have 1 stop bit */

/* setting one sample bit method in USART_CR3*/

USART1->CR3 = USART1->CR3 | USART_CR3_ONEBIT ;  /*One Bit method*/
}


void   UART_BaudConfig (uint32 u32_baudUART_bps)
{
 real32 r64_uart_div, r64_uart_fraction;
  uint u32_mantissa,u32_fraction;
 
   r64_uart_div = (real32) 8000000 / ( u32_baudUART_bps * 16 );
   u32_mantissa = (int) (r64_uart_div);
  r64_uart_fraction =  r64_uart_div - (uint)(r64_uart_div);
   r64_uart_fraction =  r64_uart_fraction * 8;
   u32_fraction =  ceil (r64_uart_fraction);
   if ( u32_fraction >=16 )
   u32_mantissa++;
   
   u32_mantissa = u32_mantissa << 4;
   u32_fraction = u32_fraction & 0x07;
   
   USART1->BRR = u32_mantissa | u32_fraction;

}  



uint8 UART_TxData(const uint8* tx_data, uint16 u16_txlen)
{
  uint8 i, Tx_sucess;
 
 for (i=0;i<u16_txlen;i++)
  {
    Tx_sucess = UART_TxByte(*tx_data);
    tx_data++;
    if (Tx_sucess)
      {
        
      }
  }
   return 1;
 
}


uint8 UART_RxData(uint8* rx_data, uint16 u16_rxlen)
{
 extern bool b_rx_flag;
 uint8 rx_success;
 
 
 while(*rx_data != 10)
 {   
    if(b_rx_flag == 1)
    {
      rx_success = UART_RxByte (rx_data);
      b_rx_flag = 0;
    }
 }
 b_rx_flag = 0;
 return 1;
 
   
}



uint8 UART_TxByte(const uint8 u8_byteTx)
{
  extern bool b_tx_flag;
  USART1->DR = u8_byteTx;
  while (!b_tx_flag);
    b_tx_flag=0;
        
    return 1;
}

uint8  UART_RxByte (uint8* pu8_byteRx)
{
 *pu8_byteRx =  USART1->DR & 0x01ff;
 return 1;

}

void USART1_IRQHandler(void)
{  
  extern bool b_tx_flag;
  extern bool b_rx_flag;
   
  if ( USART1->SR & 0x0040)
    {
      b_tx_flag = 1;
    }
  else if (  USART1->SR & 0x0020)
    {
      b_rx_flag = 1;       
    }
}


in my main function I am checking by transmitting a single byte.


bool b_tx_flag=0;
bool b_rx_flag=0;

int main(void)
{

uint8 sucess,a=0x23;
UART_ConfigIni();
UART_BaudConfig (9600);
  while (1)
  {
     
     UART_TxEnbl (1);
    sucess = UART_TxByte ('a');
     if (sucess)
      {
         USART1->SR=0x0000;
        GPIOC->CRH = 0x3;
       GPIOC->ODR = 0xFFFF;
      }
     
  }
}

this is my code.
I am not getting any interrupt.

whats wrong in my code help me out.

Outcomes