2014-05-05 05:22 PM
Hello everyone I need to use USART2 from my STM32F3 but I can't make it work. Here is my code. If someone can find my error please help me.
&sharpinclude ''stm32f30x.h'' /**************************************************************************************/ void RCC_Configuration(void){ /* Enable GPIO clock */ RCC->AHBENR |= RCC_AHBENR_GPIODEN; /* Enable USART clock */ RCC->APB1ENR |= RCC_APB1ENR_USART2EN;} /**************************************************************************************/ void GPIO_Configuration(void){ /* Connect PD5 to USART1_Tx */ GPIOD->MODER |= 2 << ( 5*2 ); // [ 9:8 ] MODER4[ 1:0 ] = 10b Alternate function on // PIN C4 //GPIOC->OTYPER |= 1 << ( 5*1 ); // [ 4 ] OT4 = 1 Output as open drain GPIOD->OSPEEDR |= 3 << ( 5*2 ); // [ 9:8 ] OSPEEDR4[ 1:0 ] = 11b 50 MHz High-speed on PIN C4 //GPIOC->PUPDR &= ~( 3 << ( 5*2 ) ); // [ 9:8 ] PUPDR4[ 1:0 ] = 0 No pull-up/down om PIN C4 GPIOD->AFR[ 0 ] |= 7 << ( 5*4 ); /* Connect PD6 to USART1_Rx */ GPIOD->MODER |= 2 << (6*2); // GPIO_Mode_AF GPIOD->AFR[0] |= 7 << (6*4); // AF7 } /**************************************************************************************/ void USART2_Configuration(void){ /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/ /* USART 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 */ USART2->BRR = 72000000/9600; USART2->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16 USART2->CR1 &= ~USART_CR1_M; // Word length = 8 bits USART2->CR1 &= ~USART_CR1_PCE; // No parity USART2->CR1 |= USART_CR1_TE; // Transmitter enable USART2->CR1 |= USART_CR1_RE; // Receiver enable USART2->CR1 |= USART_CR1_UE; // USART enable USART2->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0);} /**************************************************************************************/ void OutString(char *s){ while(*s) { while(!(USART2->ISR & USART_ISR_TXE)); // Wait for Empty USART2->TDR =(USART2, *s++); }} /**************************************************************************************/ int main(void){ RCC_Configuration(); GPIO_Configuration(); USART2_Configuration(); OutString(''This is an echo back test for USART1\r\n''); while(1) // Don't want to exit { uint16_t ch; while(!(USART2->ISR & USART_ISR_RXNE)); // Wait for Char ch = USART2->RDR; // Collect Char while(!(USART2->ISR & USART_ISR_TXE)); // Wait for Empty USART2->TDR=(USART2, ch); // Echo Char }} /**************************************************************************************/ &sharpifdef 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) { }}&sharpendif #usart-stm32f3Solved! Go to Solution.
2014-05-05 07:14 PM
Yes it is valid.
Ok, but what does that construct actually do? But If I try to change it to USART2 and PD5/PD6 does not work. If you can help me would be nice. Could it be that APB1 doesn't run at 72 MHz?// STM32 USART2 LOOP (Tx PD.5, Rx PD.6) STM32F3xx - sourcer32@gmail.com
#include ''stm32f30x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Connect PD5 to USART2_Tx */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7);
/* Connect PD6 to USART2_Rx */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7);
/* Configure USART Tx/Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | 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_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/**************************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
/* USART 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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
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++);
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
OutString(''This is an echo back test for USART2
'');
while(1) // Don't want to exit
{
uint16_t ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
ch = USART_ReceiveData(USART2); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, ch); // 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) */
/* Infinite loop */
while (1)
{
}
}
#endif
2014-05-05 05:34 PM
This is valid C?
USART2->TDR=(USART2, ch); // Echo Char USART2->TDR =(USART2, *s++); Sorry, really not interested in wading through register level code.2014-05-05 05:41 PM
Yes it is valid. The problem is that I have a code that works with PC4 and PC5 (USART1) but I need to change it to USART2 which uses PD5 and PD6. I use this code for PC4 and PC5 and this totally works.
#include ''stm32f30x.h'' /**************************************************************************************/ void RCC_Configuration(void){ /* Enable GPIO clock */ RCC->AHBENR |= RCC_AHBENR_GPIOCEN; /* Enable USART clock */ RCC->APB2ENR |= RCC_APB2ENR_USART1EN;} /**************************************************************************************/ void GPIO_Configuration(void){ /* Connect PC4 to USART1_Tx */ GPIOC->MODER |= 2 << ( 4*2 ); // [ 9:8 ] MODER4[ 1:0 ] = 10b Alternate function on // PIN C4 //GPIOC->OTYPER |= 1 << ( 4*1 ); // [ 4 ] OT4 = 1 Output as open drain GPIOC->OSPEEDR |= 3 << ( 4*2 ); // [ 9:8 ] OSPEEDR4[ 1:0 ] = 11b 50 MHz High-speed on PIN C4 //GPIOC->PUPDR &= ~( 3 << ( 4*2 ) ); // [ 9:8 ] PUPDR4[ 1:0 ] = 0 No pull-up/down om PIN C4 GPIOC->AFR[ 0 ] |= 7 << ( 4*4 ); /* Connect PC5 to USART1_Rx */ GPIOC->MODER |= 2 << (5*2); // GPIO_Mode_AF GPIOC->AFR[0] |= 7 << (5*4); // AF7 } /**************************************************************************************/ void USART1_Configuration(void){ /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/ /* USART 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 */ USART1->BRR = 72000000/9600; USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16 USART1->CR1 &= ~USART_CR1_M; // Word length = 8 bits USART1->CR1 &= ~USART_CR1_PCE; // No parity USART1->CR1 |= USART_CR1_TE; // Transmitter enable USART1->CR1 |= USART_CR1_RE; // Receiver enable USART1->CR1 |= USART_CR1_UE; // USART enable USART1->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0);} /**************************************************************************************/ void OutString(char *s){ while(*s) { while(!(USART1->ISR & USART_ISR_TXE)); // Wait for Empty USART1->TDR =(USART1, *s++); }} /**************************************************************************************/ int main(void){ RCC_Configuration(); GPIO_Configuration(); USART1_Configuration(); OutString(''This is an echo back test for USART1\r\n''); while(1) // Don't want to exit { uint16_t ch; while(!(USART1->ISR & USART_ISR_RXNE)); // Wait for Char ch = USART1->RDR; // Collect Char while(!(USART1->ISR & USART_ISR_TXE)); // Wait for Empty USART1->TDR=(USART1, ch); // 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\r\n'', file, line) */ /* Infinite loop */ while (1) { }}#endifBut If I try to change it to USART2 and PD5/PD6 does not work. If you can help me would be nice.2014-05-05 07:14 PM
Yes it is valid.
Ok, but what does that construct actually do? But If I try to change it to USART2 and PD5/PD6 does not work. If you can help me would be nice. Could it be that APB1 doesn't run at 72 MHz?// STM32 USART2 LOOP (Tx PD.5, Rx PD.6) STM32F3xx - sourcer32@gmail.com
#include ''stm32f30x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Connect PD5 to USART2_Tx */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7);
/* Connect PD6 to USART2_Rx */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7);
/* Configure USART Tx/Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | 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_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/**************************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
/* USART 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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
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++);
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
OutString(''This is an echo back test for USART2
'');
while(1) // Don't want to exit
{
uint16_t ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
ch = USART_ReceiveData(USART2); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, ch); // 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) */
/* Infinite loop */
while (1)
{
}
}
#endif
2014-05-05 07:22 PM
You are right Clive it was the frequency I changed it to the 36MHz that is needed to work and I did it. Thanks Very Much Man
2018-06-02 04:05 AM
I tried the exact same code for PA2->TX, PA3->RX but it doesn't seem to work. The device is stm32f303CB also to view the data I am using a USB to TTL converter and connection of the same is as follows:-
Converter STM32
RX
---------> TX
TX
---------> RX
GND
---------> GND
3.3V ---------> 3.3V
The change done by me is as follows:-
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
/* Connect PD6 to USART2_Rx */GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);I am new to arm based devices do let me know where I might be going wrong.
2018-06-03 10:52 AM
The message was stuck in moderation, an off-board conversation indicates this has been resolved.