AnsweredAssumed Answered

Nucleo STM32F4012RE with StdPeriph USART Issue

Question asked by narsutis.dainius on Nov 21, 2015
Latest reply on Nov 21, 2015 by narsutis.dainius
Hi,

I've been trying to get USART working on my Nucleo board with the StdPeriph Libraries. (I haven't moved to HAL yet). Anyway, I have followed clive'1 post from [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=1777]here  but it does not seem to work on my Nucleo and I cannot figure out why.
Here is what I've got:

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_usart.h"
#include "stdio.h"


#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
     
     
/**
 * @brief  Wait till USART finishes transmission
 */
#define USART_WAIT(USARTx)                  do { while (!((USARTx)->SR & USART_FLAG_TXE)); } while (0)

static uint8_t buf[64];


/* Private function prototypes ----------------------------------*/
void Delay(__IO uint32_t nCount);
void GPIO_Configuration(void);
void Initialise_I2C(void);
void RCC_Configuration(void);

void RCC_Configuration(void)
{
  /* --------------------------- System Clocks Configuration -----------------*/
  
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
 
/**************************************************************************************/
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;


  /* Connect USART pins to AF */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
 
  /*-------------------------- GPIO Configuration ----------------------------*/
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // PA.2 USART2_TX, PA.3 USART2_RX
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
     GPIO_Init(GPIOA, &GPIO_InitStructure);


}
 
/**************************************************************************************/
 
void USART2_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
 
  /* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 9600 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_Init(USART2, &USART_InitStructure);
 
  USART_Cmd(USART2, ENABLE);
}


void OutString(char *str)
{
     
  /* Go through entire string */
     while (*str) {

          /* Wait to be ready, buffer empty */
          USART_WAIT(USART2);
          /* Send data */
          USART2->DR = (uint16_t)(*str++ & 0x01FF);
          
          /* Wait to be ready, buffer empty */
          USART_WAIT(USART2);
     }
}


/**
     * @brief     Main program
     * @param     None
     * @retval None
     */
int main(void)
{
          RCC_Configuration();
      
          GPIO_Configuration();
      
          USART2_Configuration();

          OutString("Hello World!");
          
     
     while (1)
     {          
          Delay(0x000FF);
     }
}

I can connect to COM port with no problem, but the output I am seeing is garbage: "ððððððððððððððððððððððð".

I've tried the example UART project that comes with STM32Cube and it works on the same USART2. 

I've also had my oscilloscope hooked to PA_2 and PA_3 but could not see anything. Not sure if its my scope not picking anything or some settings on it are wrong.

Any advice would be appreciated.

Thanks

Outcomes