cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-F103RB and USART2

lemac
Associate II
Posted on July 02, 2014 at 13:25

Hello

I was buy recently a Nucleo-F103RB I downloaded the libraries from ST stsw-stm32143 I am trying modify the code of ''IO_Toggle'' for send data to the PC thougt USART 2 and the USB connexion. But I don't know because don't arrive nothing to PC. I put a Oscilloscope in TX PIN and there are no data in this pin. I was looking for examples code and every body make code similar to me. Can someone help me? Thanks I show you the main.c code

/* Includes ------------------------------------------------------------------*/
#include ''main.h''
#define REGION
/** @addtogroup IO_Toggle
* @{
*/ 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t TimingDelay;
uint8_t __IO BlinkSpeed = 0;
/* Private function prototypes -----------------------------------------------*/
RCC_ClocksTypeDef RCC_Clocks;
/* Private functions ---------------------------------------------------------*/
int main(void)
{
/** USART2 GPIO Configuration 
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
/****************************************************************************/
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART2_InitStruct;
/****************************************************************************/
/*Enable or disable APB2 peripheral clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/*Configure GPIO pin : PA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/****************************************************************************/
/* USART_InitStruct members default value */
USART2_InitStruct.USART_BaudRate = 9600;
USART2_InitStruct.USART_WordLength = USART_WordLength_8b;
USART2_InitStruct.USART_StopBits = USART_StopBits_1;
USART2_InitStruct.USART_Parity = USART_Parity_No ;
USART2_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART2_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
USART_DeInit(USART2);
USART_Init(USART2, &USART2_InitStruct);
//USART_StructInit(USART2_InitStruct); 
//USART_ClockInitTypeDef *USART2_ClockInitStruct;
//USART_ClockStructInit (USART2_ClockInitStruct);
//USART_Init(USART2, USART2_InitStruct); 
//USART_ClockInit(USART2, USART2_ClockInitStruct);
USART_Cmd(USART2, ENABLE); 
/****************************************************************************/

/*!< At this stage the microcontroller clock setting is already configured, 
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_md.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/ 
/* SysTick end of count event each 1ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
/* Initialize LED2 */
STM_EVAL_LEDInit(LED2);
/* Initialize User_Button on STM32NUCLEO */
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); 
/* Initiate Blink Speed variable */ 
BlinkSpeed = 0;
/****************************************************************************/
/* Infinite loop */
while (1)
{
/* Test on blink speed */
if(BlinkSpeed == 0)
{
/*LED2 Toggle each 50ms*/
STM_EVAL_LEDToggle(LED2);
Delay(50); 
} 
else if(BlinkSpeed == 1)
{
STM_EVAL_LEDToggle(LED2);
/*LED2 Toggle each 200ms */
Delay(200); 
}
USART_SendData(USART2,0xAA55);
}
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in 1 ms.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{ 
TimingDelay--;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{ 
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Regards
3 REPLIES 3
Posted on July 02, 2014 at 13:36

SendData can only send bytes, up to 9-bits if required. You must also wait for TXE status to assert before stuffing new data into the USART.

The 401, but perhaps helpful

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=174]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FUSART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=174
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Chris1
Senior III
Posted on July 02, 2014 at 15:34

It looks like you are missing a clock enable; USART2 APB1 peripheral clock must be enabled to use that peripheral.

lemac
Associate II
Posted on July 02, 2014 at 17:56

Hello

Thanks clive1. I have to modify something for work. Now it work. It don't work properly but i have a way to work. Now i have to tuning this. If somebody need this I put the modified code

#include ''main.h''
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART2_Configuration(void);
void OutString(char *s);
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // PA.2 USART2_TX, PA.3 USART2_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
}
/**************************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx 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 = 115200;
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 *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
f
GPIO_Configuration();
USART2_Configuration();
OutString(''Welcome to Nucleo F401RE

'');
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART2); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, Data); // Echo Char
}
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
while (1)
{}
}
#endif
/**************************************************************************************/

Regards and thanks