cancel
Showing results for 
Search instead for 
Did you mean: 

uart4 interfacing of stm32f4discoveryboard

arunabha1989
Associate
Posted on September 22, 2014 at 12:49

Hi everyone,

I am trying hard to make my uart4 code to working.But I am not getting anything on my hyperterminal.I am using keiluvision4.I have uploadeed th epiece of my code below.Please have a look and suggest me the chanegs required.

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

 

  /* Enable USART6 clock */

  RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

     

  /* Configure port as pushpull, 50MHz and No pull up & down  */

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

         

  /* Configure PC6 as alternate function  */

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10|GPIO_Pin_11;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

 

  /* Configure PC7 as alternate function  */

  //GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

 // GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;

 // GPIO_Init(GPIOA, &GPIO_InitStructure);

     

  /* Connect PC6 to USART6_Tx*/

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);

 

  /* Connect PC7 to USART6_Rx*/

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);

     

  /* USART6 configured as follow:

        - BaudRate = 115200 baud  

        - Word Length = 8 Bits

        - One Stop Bit

        - No parity

        - Hardware flow control disabled (RTS and CTS signals)

        - Receive and transmit enabled

  */

  USART_InitStructure.USART_BaudRate = 9600;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_No;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

   

 

  /* USART configuration */

  USART_Init(UART4, &USART_InitStructure);

     

  /* Enable USART */

  USART_Cmd(UART4, ENABLE);

while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);

//while( !(UART4->SR & 0x00000040) );

  USART_SendData(UART4, 'a');

thanks

5 REPLIES 5
Posted on September 22, 2014 at 12:59

>   /* Enable USART6 clock */

>   RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

     

So, is it APB1 or APB2?

JW

arunabha1989
Associate
Posted on September 22, 2014 at 13:11

Thanks for ur reply.i have changed this line to RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);Now it is not hanging at flag checking instruction.But I am still not getting anything on my terminal.

Thanks

megahercas6
Senior
Posted on September 22, 2014 at 15:46

void init_USART1(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStruct; 
USART_InitTypeDef USART_InitStruct; 
NVIC_InitTypeDef NVIC_InitStructure; 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; 
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; 
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_Init(GPIOC, &GPIO_InitStruct); 
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); 
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
USART_InitStruct.USART_BaudRate = baudrate; 
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1; 
USART_InitStruct.USART_Parity = USART_Parity_No; 
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; 
USART_Init(UART4, &USART_InitStruct); 
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); 
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(UART4, ENABLE);
}
void UART4_IRQnHandler(void)
{
char data = 0;
while(USART_GetITStatus(UART4, USART_IT_RXNE)==RESET);
data=USART_ReceiveData(UART4);
while (USART_GetITStatus(UART4, USART_IT_TXE) != RESET);
USART_SendData(UART4, (uint8_t) data);
}

Posted on September 22, 2014 at 16:44

Also understand that the STM32 generates/uses CMOS levels, not RS232 levels, so you'll have to suitably interface to the PC.

Also the DISCO boards use a 8 MHz clock source, and the EVAL boards use a 25 MHz one, you need to make sure your PLL is set up for the right speed, and the define HSE_VALUE correctly reflects the clock source speed.

If you need to debug further use a scope to measure the bit timing and levels, and that you have the RX/TX sense correct.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
megahercas6
Senior
Posted on September 23, 2014 at 08:36

Changing HSE value must be done not only in system_stm32f4xx.c, but MUST be changed in stm32f4xx.h

#if !defined  (HSE_VALUE)

  #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */