cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USART DR unavailable

dima
Associate II
Posted on March 14, 2013 at 09:35

Hey, guys!

I have one stupid error, show me the right way, please. I'm actually working with STM32L-Discovery and trying firstly to work out the USART. I've done all necessary configurations for the simpliest transmitter mode, but my RS232/USB receiver don't want to see any sygnal. I've ckecked everything and catch the bug by means of DEBUGGING (using Keil). I have seen that function USART_SendData (including address USART->DR) do nothing with DR register!! It is 0x0000 all the time.

int main (void) {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // GPIOA Clock Enable, AHB Bus, AHBENR Register
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // GPIOB Clock Enable, AHB Bus, AHBENR Register
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); // GPIOC Clock Enable, AHB Bus, AHBENR Register
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); // GPIOD Clock Enable, AHB Bus, AHBENR Register
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); // USART3 Clock Enable, APB1 Bus, APB1ENR Register
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); // I2C1 Clock Enable, APB1 Bus, APB1ENR Register
RCC_APB1PeriphClockCmd(RCC_APB1Periph_LCD, ENABLE); // LCD Clock Enable, APB1 Bus, APB1ENR Register
PWR_RTCAccessCmd(ENABLE);
RCC_LSICmd(ENABLE);
Delay(100);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
RCC_RTCCLKCmd(ENABLE);
/* PC10 for USART3 TX PC11 for USART3 RX */ 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
GPIO_Init( GPIOC, &GPIO_InitStructure); 
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10,GPIO_AF_USART3); 
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11,GPIO_AF_USART3); 
/* USART3_InitStructure configuration */
USART3_InitStructure.USART_BaudRate = 9600;
USART3_InitStructure.USART_WordLength = USART_WordLength_8b;
USART3_InitStructure.USART_StopBits = USART_StopBits_1;
USART3_InitStructure.USART_Parity = USART_Parity_No ;
USART3_InitStructure.USART_Mode = USART_Mode_Tx; 
USART3_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
USART_DeInit(USART3);
USART_Init(USART3, &USART3_InitStructure);
USART_Cmd(USART3, ENABLE);
while (1) 
{ // Loop forever
USART_SendData(USART3, 0x00FF);
Delay(500);
};
}

#stm32-usart-data-register
6 REPLIES 6
Posted on March 14, 2013 at 10:05

> I've ckecked everything and catch the bug by means of DEBUGGING (using Keil). I have

> seen that function USART_SendData (including address USART->DR) do nothing with

> DR register!! It is 0x0000 all the time.

Actually, you can't ''see'' the ''result'' of writing to the USART_DR register, as reading it back it does not return the value written to it, rather, it returns the Rx value. Refer to the manual:

The Data register performs a double function (read and write) since it is composed of two registers, one for transmission (TDR) and one for reception (RDR)

JW

dima
Associate II
Posted on March 14, 2013 at 10:17

You're right, but i mean the DEGUB mode register monitoring and after operation USART_DataSend(USART3, Data) i must see in DR window data to outcome. Or maybe DEBUG operation with such double register is unpredictibile?

Posted on March 14, 2013 at 11:16

Don't be obsessed by how the debug stuff does or does not work. Your problem is not there.

JW

Posted on March 14, 2013 at 12:32

The internal state of the peripheral is not totally visible, neither you nor the debugger can see inside. Reading DR will always show the Received data. The data you write will be staged in the transmit register, before being moved to the outbound shift register.

Want to see what's getting transmitted, get a scope, or attach a terminal.

You should only send data when TXE signals. Spin on USART->SR & TXE

When enabling RTC clocks, etc, you should wait of ready status, not insert arbitrary delays. Such delays will bite you when there are silicon/component variations.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
John F.
Senior
Posted on March 14, 2013 at 17:49

Also, when using the debugger to view registers I think I have seen times where a register that clears status flags with a read operation (read/clear by read (rc_r)) has cleared them on a debug read potentially leaving the program code waiting on a flag that's never set.

dima
Associate II
Posted on March 17, 2013 at 19:10

Thank you, everybody. Code is OK, the problem was in wiring. 

Conclusion: DEBUGger really doesn't see USART->DR register.