simple port access

Discussion in 'C++' started by learnfpga@gmail.com, Aug 9, 2006.

  1. Guest

    I am trying to write a simple code for bit maniipulation and to start
    with wrote a little code to read the port A of a microcontroller
    MPC8xx. Its running on VxWorks. Anyways thats probably irrelevant. Can
    anyone please point out mistake in the code.?

    #include <stdio.h>
    #include <iostream.h>
    #include "arch/ppc/vxPpcLib.h"

    unsigned long ppc_base_addr = vxImmrGet(); //Get the base address
    #define PADAT *(volatile unsigned long *)(ppc_base_addr+0x956) //0x956
    is address of port A
    #define BIT(x) (1 << (x))

    unsigned long read()
    {

    unsigned long i = 0x80000000;
    PADAT = i;
    unsigned long temp = *(unsigned long *)(PADAT);
    return(temp);
    }
    Thanks for any suggestions.

    PS- I can probably ask the Q in VxWorks or Embedded group but I think
    its my C++ that a problem....
     
    , Aug 9, 2006
    #1
    1. Advertising

  2. wrote:
    > I am trying to write a simple code for bit maniipulation and to start
    > with wrote a little code to read the port A of a microcontroller
    > MPC8xx.


    That's beyond the scope of this newsgroup. Hardware manipulation and
    control is not part of C++ *language*. It's defined at the platform
    level, and as such cannot be expressed in terms of Standard C++ alone.

    > Its running on VxWorks. Anyways thats probably irrelevant.


    Platform is irrelevant to most questions that are on topic here. What
    your program does and why it doesn't do what it should (if that's so),
    it absolutely relevant to what platform it runs on.

    > Can
    > anyone please point out mistake in the code.?


    I'll tell you what I see as a mistake, but it doesn't mean you will fix
    your program by correcting it.

    >
    > #include <stdio.h>
    > #include <iostream.h>


    There is no header '<iostream.h>' in C++. The header you need is called
    '<iostream>' (notice no '.h').

    > #include "arch/ppc/vxPpcLib.h"


    That's a non-standard header, and I have no idea what it does or how
    it can affect the correctness of your program.

    >
    > unsigned long ppc_base_addr = vxImmrGet(); //Get the base address


    'vxImmrGet' is not a standard function. Use at your own risk.

    > #define PADAT *(volatile unsigned long *)(ppc_base_addr+0x956) //0x956
    > is address of port A
    > #define BIT(x) (1 << (x))
    >
    > unsigned long read()
    > {
    >
    > unsigned long i = 0x80000000;
    > PADAT = i;


    The line above hides what really happens. I'd probably define 'PADAT'
    *without* the leading dereference and then used

    *PADAT = i;

    here.

    > unsigned long temp = *(unsigned long *)(PADAT);


    This is probably the source of your trouble. 'PADAT' is a reference to
    the long that resides in your port. You're treating it as a pointer.
    Use my advice above, and drop the leading asterisk from the definition
    of PADAT macro.

    > return(temp);


    Parentheses are superfluous here.

    > }
    > Thanks for any suggestions.
    >
    > PS- I can probably ask the Q in VxWorks or Embedded group but I think
    > its my C++ that a problem....


    Next time you probably should.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 9, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > I am trying to write a simple code for bit maniipulation and to start
    > with wrote a little code to read the port A of a microcontroller
    > MPC8xx. Its running on VxWorks. Anyways thats probably irrelevant. Can
    > anyone please point out mistake in the code.?
    >
    > #include <stdio.h>
    > #include <iostream.h>
    > #include "arch/ppc/vxPpcLib.h"
    >
    > unsigned long ppc_base_addr = vxImmrGet(); //Get the base address
    > #define PADAT *(volatile unsigned long *)(ppc_base_addr+0x956)

    #define PAADDR (volatile unsigned long *)(ppc_base_addr+0x956)
    > #define BIT(x) (1 << (x))
    >
    > unsigned long read()
    > {
    >
    > unsigned long i = 0x80000000;
    > PADAT = i;
    > unsigned long temp = *PAADDR;
    > return(temp);
    > }
     
    , Aug 9, 2006
    #3
  4. wrote:
    > wrote:
    >> I am trying to write a simple code for bit maniipulation and to start
    >> with wrote a little code to read the port A of a microcontroller
    >> MPC8xx. Its running on VxWorks. Anyways thats probably irrelevant.
    >> Can anyone please point out mistake in the code.?
    >>
    >> #include <stdio.h>
    >> #include <iostream.h>
    >> #include "arch/ppc/vxPpcLib.h"
    >>
    >> unsigned long ppc_base_addr = vxImmrGet(); //Get the base address
    >> #define PADAT *(volatile unsigned long *)(ppc_base_addr+0x956)

    > #define PAADDR (volatile unsigned long *)(ppc_base_addr+0x956)
    >> #define BIT(x) (1 << (x))
    >>
    >> unsigned long read()
    >> {
    >>
    >> unsigned long i = 0x80000000;
    >> PADAT = i;


    Then probably

    *PADAT = i;

    >> unsigned long temp = *PAADDR;
    >> return(temp);
    >> }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 9, 2006
    #4
  5. David Harmon Guest

    On 9 Aug 2006 14:05:36 -0700 in comp.lang.c++, ""
    <> wrote,
    >Can anyone please point out mistake in the code.?


    What makes you think there is any mistake in the code? Is it
    supposed to do something?

    >#define PADAT *(volatile unsigned long *)(ppc_base_addr+0x956) //0x956
    >is address of port A


    I'm not real happy about seeing this macro here. Better C++ style
    would be inline functions for such access.

    And in this case, the macro is hiding your problem from you. Below
    you are using PADAT as if it was both the value you are assigning
    and elsewhere as if it was a pointer. You are getting confused
    about how many levels of indirection to apply.

    > PADAT = i;

    No *, not a pointer.

    > unsigned long temp = *(unsigned long *)(PADAT);

    Extra *, PADAT treated as a pointer.
     
    David Harmon, Aug 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John T. Goodman

    Overhead of 4-port over 2-port SRAM

    John T. Goodman, Jan 25, 2005, in forum: VHDL
    Replies:
    0
    Views:
    660
    John T. Goodman
    Jan 25, 2005
  2. Sean Wolfe
    Replies:
    1
    Views:
    2,314
    Joerg Jooss
    Apr 28, 2005
  3. b3ny
    Replies:
    11
    Views:
    959
    Babu Kalakrishnan
    Nov 20, 2004
  4. Gerald Klix
    Replies:
    0
    Views:
    1,327
    Gerald Klix
    Oct 26, 2005
  5. Pom
    Replies:
    2
    Views:
    1,797
    Bas-i
    Jan 31, 2007
Loading...

Share This Page