STM32F105 USART2's DR register not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 4:48 AM
Hi all,
I'm trying to use the USART2 on the STM32F105CL to communicate with an hyper-terminal but unfortunatly, the hyper-terminal is not receiving anything. Even with a very simple code./* Includes ------------------------------------------------------------------*/#include <stdio.h>#include <string.h>#include ''stm32f10x.h''#include ''stm32f10x_gpio.h''#include ''stm32f10x_rcc.h''#include ''stm32f10x_usart.h''#include ''leds.h''/* Private typedef -----------------------------------------------------------*//* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*//* Private function prototypes -----------------------------------------------*//* Private functions ---------------------------------------------------------*/int main(void){ LED_Init(); /* ************************************************************/ USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* Enable USART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); //Configure USART2 pins: Rx and Tx ---------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 57600; 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); /* ********************************************/ while (1) { for(int i=0;i<100000;i++) { LED_On(); } for(int i=0;i<100000;i++) { LED_Off(); } while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET){}; USART_SendData(USART2,'a'); while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){}; } return 0;}Sombody has any clue ?Thanks,- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 5:39 AM
Get a scope, confirm if there is a signal, or not.
Make sure HSE_VALUE reflects the external crystal speed. Don't front test TC, you only need to test TXE to know if the holding buffer is empty or not. while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){}; USART_SendData(USART2,'a');Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:12 AM
hi, thanks for the answer !
The fact that the led is going on and off the right way doesn't mean that the HSE_Value reflects the external crystal speed?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:16 AM
And I can't test with a score, because after 2 turns in the while(1), i'm in an infinite loop waiting for USART_FLAG_TXE to be SET...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:31 AM
The HSE_VALUE is used to compute the correct baud rate, and play no role in flashing your LEDs
If the USART2 registers always contain zero, it would suggest the peripheral is not clocked properly. Suggest you don't use a debugger, or park a peripheral view over the USART. Beyond that you'd need to review the tool chain you are using, and the project construction. Review the templates provided in the standard peripheral library.Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:33 AM
I use a debugger. That's why I can say that the register DR is never full. Using breakpoint, when I'm at the line USART_SendData(...), the flag register change to 0 and DR stay at 0.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:37 AM
After Initialization of the USART, in USART2, i have SR = 192 and CR1 = 8204.
Don't know if it's help...- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:53 AM
Look the debugger is invasive. And you can't read the value you wrote in DR, it's a peripheral register, not a memory, the read/write go to different internal registers.
If you remove the breakpoints, and watchs, and just let the code run does the LED flash?Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 6:56 AM
Oh ok...
Hum, no, the led flashs only once, after i'm in a infinite loop in the ''while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){};''- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2014-07-18 7:58 AM
The code itself doesn't look problematic, I don't like the C++ syntax used, but probably not the issue here. The way you've used the #include's suggests you haven't used one of the project templates.
What tool chain are you using? What version of the library? What is the value of RESET?Up vote any posts that you find helpful, it shows what's working..
