2012-10-26 01:57 AM
Hi Guys,
Been trying to get the USART to work for a couple of days now and is time to get some help... This is my code:GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f4xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f4xx.c file
*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
/* GPIO Pins configuration for Alternative Function */
/* for TX Pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* for RX Pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
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(GPIOD, &GPIO_InitStructure);
/* 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 = 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);
/* Output a message on Hyperterminal using printf function */
//printf(''\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r'');
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
USART_Cmd(USART2,ENABLE);
USART_SendData(USART2, (uint8_t) 0x31);
while (1)
{
}
}
I tried pretty much all the USARTs on the board and I can not send anything.
I looped back the TX & RX pins on my USB to Serial converter and that works fine.
The processor seems to be cycling fine thorough the code when on Debug.
I am complety at a loss why the program will not work, Any assistance would be appreciated.
Best Regards
Luis
2012-10-26 04:02 AM
I looped back the TX & RX pins on my USB to Serial converter and that works fine.
What is the question or point if it works fine (I assume that means that you can read what you sent)? Where it does not work? In initialization code you are missing NVIC_InitStructure and you forgot to post interrupt handlers as that is probably where something useful is happening (while(1) is not all that interesting)...
2012-10-26 05:28 AM
The code doesn't look too bad, I'd probably spin sending characters, I've posted lots of examples for the STM32F4-Discovery board
Be aware however that the STM32 does not output RS232 level serial signals.2012-10-26 06:11 AM
Hi Clive1,
My USB to serial converter is working at the correct levels, it is design to interface directly to an uC, . it's an FTDI MM232R. The thing actually started working now, but the worrying thing is that I didn't know why it didn't work and now I don't know why is working... need to leave it until Monday I think. Thanks Best Regards Luis2012-10-26 01:53 PM
That looks fine.
// STM32 USART2 (Tx PD.5, Rx PD.6) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); // USART2_TX
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); // USART2_RX
}
/**************************************************************************************/
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);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
while(1)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, 0x49); // Send 'I'
}
while(1); // Don't want to exit
}
2012-10-29 01:14 AM
Hi Guys,
The Usart is now working fine and I am just tiding up the code for it and I have a question about the GPIO PIN setup. After reading about it on the reference manual it looks that all I need to do to get, in my case the Pins configured for USART is:/* for TX Pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* for RX Pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOD, &GPIO_InitStructure);
But in other examples they also they added , as I did initially:
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
I have now removed it from my code and the Usart is working perfectly fine.
Is this last part of the setup of the pins required? Is it the case that as soon as you select the alternative function the rest will be ignored completely and it is not required at all.
Thanks
Best Regards
Luis
2012-10-29 07:03 AM
The AF connection can select the input/output (Hi, Lo, Z) state of the pin.
You need to configure the driver (OD or PP), slew rate (MHz) and pull resistors. GPIO_Init will use the random data in the structure if you don't provide it, better to be explicit so you aren't surprised later especially when using local/auto variables on the stack.2013-04-12 09:22 AM
hi moreira.luis.001
i uses MM232R with stm32f4 discovery in a project ..there are 16 pins in MM232R .. Could you please send me the wiring of these different pins ...Thank you.2013-04-12 09:32 AM
2013-04-12 12:51 PM
thank you...
Without connecting Vcc(pin 2)???