0

I was going through a programming manual for one of the microcontrollers I came across and it had the preprocessor definition as follows:

#define SCICTL1A (volatile unsigned int *)0x7051

and a statement in the source file as follows:

*SCICTL1A = 0X0003;

My question is, what is the pointer variable here and what is it pointing to, (I have never come across pointer definitions in preprocessor directives before since I am a beginner to C programming) and what does the assignment statement do?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52

3 Answers3

3

There is no variables here. The macro expands as text in place, so the 2nd excerpt becomes

*(volatile unsigned int *)0x7051 = 0X0003;

It casts the unsigned integer 0x7051 into a pointer to volatile unsigned integer, then references this in assignment. Essentially it stores 0x0003 into the unsigned integer-wide piece of memory that starts from address 0x7051 (or, however the integer-to-pointer conversion happens to work on your target platform)

volatile is required so that the compiler does not just optimize the assignment out - it must be strictly evaluated and considered a side effect (see as-if rule).

As for the actual reason why this is done - it is probably some memory-mapped device, check the microcontroller datasheets for more information.

  • `volatile` is required when you want to tell the compiler to not optimize out memory access, in this case to a register in a memory-mapped peripheral. This memory will update without the compiler knowing it. Normally it would predict when it needs to access memory and avoid unnecessary memory access to speed up the code (optimization). In this case, those predictions would almost always be wrong and your code would not behave how you would expect it. – bigwillydos Sep 11 '18 at 18:02
  • @bigwillydos: That's not correct. `volatile` does not enforce memory accesses. – too honest for this site Sep 11 '18 at 19:34
0

There is no variable there. Only the pointer.

the *SCICTL1A = 0X0003; is replaced by the preprocessor by the:

*(volatile unsigned int *)0x7051 = 0x0003;

You just write the location with the address of the 0x07051. That does it mean depends on your implementation

0___________
  • 60,014
  • 4
  • 34
  • 74
0

I'm assuming you're using a TMS320F2803x Piccolo microcontroller: http://www.ti.com/lit/ds/sprs584l/sprs584l.pdf

According to this document, address 0x7051 is Control Register 1 for the Serial Communications Interface (SCI) Module.

According to this document, https://www.swarthmore.edu/NatSci/echeeve1/Ref/embedRes/DL/28069TechRefManual_spruh18d.pdf, you're able to do the following with this register:

SCICTL1 controls the receiver/transmitter enable, TXWAKE and SLEEP functions, and the SCI software reset.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46