cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo STM32F4012RE with StdPeriph USART Issue

dman
Associate II
Posted on November 21, 2015 at 13:26

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
3 REPLIES 3
Posted on November 21, 2015 at 14:06

Which SPL are you using to build this with?

I used the package for the 401C Discovery as my starting point.

You'd want to make sure that the HSE_VALUE defined for the project, that it is 8 MHz, not 25 MHz. You also want to make sure the PLL settings reflect this, and the lower speed of the 401 parts.

I'm also not sure why you need to #include everything individually. If you use ST's project templates these will be brought in via the stm32f4xx_conf.h file. Check you have USE_STDPERIPH_DRIVER defined.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dman
Associate II
Posted on November 21, 2015 at 21:43

Hi,

The SPL is V1.6.1 - October 2015.

I've got these defines in my Keil project setup: USE_STDPERIPH_DRIVER,STM32F401xx,HSI_VALUE=80000000,HSE_VALUE=8000000

The only templates I am aware off are the ones generated by STMCubeMX and it automatically includes all HAL libraries. I've generated a demo project, removed all HAL libraries and included only the ones I've needed from the SPL. Probably not the best way to go about it, I agree.

Thanks

dman
Associate II
Posted on November 21, 2015 at 22:06

Got it working. I've set my defines to USE_STDPERIPH_DRIVER,STM32F401xx,HSE_VALUE=8000000,HSI_VALUE=16000000 and Xtal to 16.0

Seems to be working.

Thanks