Can anyone explain me the meaning of the following code block

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srikanthpadava
    New Member
    • Oct 2012
    • 9

    Can anyone explain me the meaning of the following code block

    Can anyone please help me in understanding the following code:
    UD1RX is referred to in the application code (foreg: RX = UD1RX) and i have found the following defines for the same, but
    couldnt quite understand why they mean.

    I understand it is being casted as a pointer to a pointer. But it is confusing. I appreciate if any one could explain it.

    Code:
    #define	UD1RX            CAST_UC(0xfffffa16UL)
    
    #ifdef __LANGUAGE_ASM__
       #define CAST_UC(x) x
    #else
       typedef volatile unsigned char*   pU08necDevFile;
    
       #define CAST_UC(x) ( *(pU08necDevFile)(x) )
    Last edited by zmbd; Nov 14 '12, 01:31 PM. Reason: [z(fixed code tag. Was "}" now "]")]
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I can't see what CAST_UC does but if the flag __LANGUAGE_ASM_ _ is defined, then CAST_UC will do something with the value in parentheses. If __LANGUAGE_ASM_ _ is not defined, then your x will be type cast to a volatile unsigned char* and then de-referenced to a volatile char and then pass to CAST_UC.

    You would think you could just cast the x to a volatile char and omit the typedef but that typedef may be used elsewhere in the programif __LANGUAGE_ASM_ _ if is not defined.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Your example
      Code:
      ...
      RX = UD1RX;
      expands to the following if __LANGUAGE_ASM_ _ is defined:
      Code:
      ...
      RX = 0xfffffa16UL;
      otherwise it expands to this (the typedef is not part of the expansion, I wrote it below to provide context):
      Code:
      typedef volatile unsigned char*   pU08necDevFile;
      ...
      RX = ( *(pU08necDevFile)(0xfffffa16UL) );
      .
      I suspect RX is itself a macro whose expansion depends on __LANGUAGE_ASM_ _.

      Comment

      • srikanthpadava
        New Member
        • Oct 2012
        • 9

        #4
        hi weaknessforcats and donbock,

        Thanks for your responses.
        I have understood to some extent. But i still couldnot understand the following: (I am quoting a few lines out of weaknessforcats 's response)
        "If __LANGUAGE_ASM_ _ is not defined, then your x will be type cast to a volatile unsigned char* and then de-referenced to a volatile char and then pass to CAST_UC. "

        i.e in my example if x=0xfffffa16UL, at first it is being typecast to volatile unsigned char* and then derefenced, in the sense that the value at this adress (oxfffffa16UL) is stored in RX. Have i understood it correctly?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          @srikanthpadava : Refer to line 3 of the third code block in my earlier reply post for the case when __LANGUAGE_ASM_ _ is undefined.
          Code:
          RX = ( *(pU08necDevFile)(0xfffffa16UL) );
          First, 0xfffffa16UL is cast to a pU08necDevFile (volatile unsigned char*). Then, the asterisk deferences the pointer. That is, the value of the unsigned char at location 0xfffffa16UL is stored in RX.

          Is RX a macro?

          Comment

          • srikanthpadava
            New Member
            • Oct 2012
            • 9

            #6
            hi donbock,

            thanks for the explanation.

            No RX is not a macro.
            RX is defined as static T_UINT8 RXTemp without any ifdef
            and takes directly the value of UD1RX which is i believe is a register

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              My guess would be that UD1RX is a register and that it may be stands for something like

              UART Device [b]1[b] Receive

              RX is a common abbreviation of receive(r) as TX is for transmit(er).

              If __LANGUAGE_ASM_ _ is not defined my guess is that this is reading the receive buffer of a UART device. For some reason if __LANGUAGE_ASM_ _ is defined it does not; a reason for this is that the software can build built on difference platforms, may be a target platform and a simulator or test platform. On the simulator platform there is no UART device so the coder wanted to ensure the program didn't read a random in invalid memory address and since he needs to return something he just returns the value of the address.

              Why they have used __LANGUAGE_ASM_ _ as the symbol to control this I do not know, I would use something that was more meaningful but it is possible that they just looked for a symbol that is defined by the compiler of the simulator build and is not defined by the compiler of the target build. Personally I would say that wasn't very good practice.


              BTW if you don't know a UART or USART device controls communication using a serial line such as an RS232 port. Modern USARTs normally support multiple different serial protocols RS232, I2C, SPI etc

              Comment

              • srikanthpadava
                New Member
                • Oct 2012
                • 9

                #8
                thanks for the response banfa.

                Yes UD1RX is the register of the device (UART) controlling the serial communication you were mentioning.

                Comment

                Working...