Hi, I am new to C and Keil, However I wish to know more about the c programming in keil especially developing programs for STM32. My question is about USART1. I see that in file named stm32f10x_map.h definition for USART is given like:
typedef struct { vu16 SR; u16 RESERVED0; vu16 DR; u16 RESERVED1; vu16 BRR; u16 RESERVED2; vu16 CR1; u16 RESERVED3; vu16 CR2; u16 RESERVED4; vu16 CR3; u16 RESERVED5; vu16 GTPR; u16 RESERVED6; vu16 hamed; } USART_TypeDef;
and also USART1 is defined as a pointer to this struct like:
#ifdef _USART1 EXT USART_TypeDef *USART1; #endif /*_USART1 */
but later I see in file USART.c a command like:
USART1->SR &= ~USART_FLAG_RXNE
My question is that where does SR defined for the compiler? as I see it is only defined as a vu16 type in USART_TypeDef. Does compiler differ between SR and DR or BRR ,etc.? How?
I'm not really sure I understand your question.
The struct has a number of fields, and the pointer will be initialized to the address of the special function registers controlling the UART.
So using the pointer and write to a field in the struct will result in a write to the memory address that corresponds to that register in the UART device.
vu16 is just a nice way of telling that the struct member - which maps on top of a UART register - is a 16-bit unsigned, and that it is volatile so the compiler must generate code that always performs memory accesses instead of caching any value in processor registers.
So the definition of SR? It's just defined in the struct as basically how many bytes from the start of the struct. And the pointer is set to the start of the memory range that contains the UART control registers. So this means that the SR field of the struct gets mapped on top of the SR register in the UART device.
I have not yet find the answer to my Q. My question is that what is the difference between SR and DR according to out definition in the codes i mentioned, so how may the compiler refer to status register when we do USART1->SR ? is SR a predefined name for compiler? otherwise there should be no difference between SR and DR and other registers declared in the struct USART_TypeDef. please advise me how to access bits of SR register. I have seen declaration like USART1_SR_RXNE in some codes.
I did give the answer to that question - but that also requires that you know what a struct is in the C or C++ language.
DR and SR are two different members of the struct. Which means that they have different offset within the struct. Which means they will overlap different memory when the struct is placed "on top of" a block of memory.
So writing to the DR field will result in a write to one address in the memory, while writing to the SR field will result in a write to another address in the memory. And after having properly aligned the struct on top of the UART control registers, a write to the DR field of the struct will correspond to a write to the UART control register "DR".
You access bits in a register of the UART controller by accessing bits in a member of the struct. And you access bits in a member of the struct the same way that you access bits in any int-type variable in C or C++. How you do that? That's part of a "beginners guide to C" programming book.
Your questions really are very basic after you have learned the concepts of a struct, of a pointer and of bit operations. How to learn these concepts isn't part of the Keil forum and isn't specific to any ARM or 8051 or PPC or x86 processor but something you should solve by selecting a suitable book introducing you to the C programming language. A number of such books can even be downloaded for free. And it isn't really meaningful to play with bits in processor registers until you understand the basic language constructs.
Thank you very much.