This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

about structs

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?

Parents
  • 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.

Reply
  • 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.

Children