2015-11-11 11:08 AM
Hi,
I don't know why this code doesn't work. I have following simple code to check USART and the code is running on STM32F072 discovery board. But when I check PA2 pin with scope, it shows nothing. I also connect the board to PC serial port and check with hyperterminal and no luck. *******************************************static void USART_Config(void);
static void GPIO_Config(void); static void RCC_Config(void);/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program. * @param None * @retval None */ int main(void) { /* RCC configuration */ RCC_Config(); /* GPIO configuration */ GPIO_Config();/* USART configuration */
USART_Config();while(1)
{ while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, 10);}
}/**
* @brief Configure the USART1 Device * @param None * @retval None */ static void USART_Config(void) { USART_InitTypeDef USART_InitStructure; /* USART1 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - Two Stop Bit - Odd 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); }/**
* @brief Configure the GPIO Device * @param None * @retval None */ static void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;/* Connect PXx to USART2_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);/* Connect PXx to USART2_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); /* Configure USART2 Tx/Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); }/**
* @brief Configure the RCC * @param None * @retval None */ static void RCC_Config(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //enable GPIOA clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //enable USART2 clock}
#rtfm2015-11-11 12:25 PM
Try this, at 115200 8N1
static void USART_Config(void);
static void GPIO_Config(void);
static void RCC_Config(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/* RCC configuration */
RCC_Config();
/* GPIO configuration */
GPIO_Config();
/* USART configuration */
USART_Config();
while(1)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, 65);
}
}
/**
* @brief Configure the USART1 Device
* @param None
* @retval None
*/
static void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- Two Stop Bit
- Odd 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);
}
/**
* @brief Configure the GPIO Device
* @param None
* @retval None
*/
static void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART2 Tx/Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect PXx to USART2_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
/* Connect PXx to USART2_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
}
/**
* @brief Configure the RCC
* @param None
* @retval None
*/
static void RCC_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //enable GPIOA clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //enable USART2 clock
}
Make sure there are no solder-bridges to be made.
2015-11-11 01:19 PM
Thanks for the quick response.
I have tried your code, but it still doesn't work. The board is on default configuration, no modification has been made.2015-11-12 12:14 PM
Today, I try the code on another new STM32F072 board. Again it doesn't work.
I am totally lost here, can anyone help on this?2015-11-12 01:59 PM
What debugger are you using? And have you stepped through the code in the debugger? This code looks reasonable enough.
Make sure to use a USB-to-CMOS Serial to connect to the signals as they are not RS232 compatible levels, for that you'd need a MAX3232 or something similar.2015-11-12 03:23 PM
USART_SendData(USART2, 65);
2015-11-12 03:40 PM
TX:
Transmit data Output. When the transmitter is disabled, the output pin returns to its I/O port configuration. When the transmitter is enabled and nothing is to be transmitted, the TX pin is at high level. In single-wire and smartcard modes, this I/O is used to transmit and receive the data.This is stated from STM32F072 reference manual. It seems to me that PA2 pin should stay at high level if USART2 is enabled, but I don't see that. ???
2015-11-12 03:56 PM
And you're using the line
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
2015-11-12 04:08 PM
Yes, the code sets it to NO_PULL. When I set it to PULLUP, I still see PA2 at low level.
2015-11-12 05:41 PM
You need to read the manual and follow the directions to check the solder bridges. SB27 and SB28 need to be made for PA2 and PA3 to get to the pins.
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00099401.pdf