simple port access

L

learnfpga

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....
 
V

Victor Bazarov

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
' said:
#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
 
G

genetic.odium

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)
 
D

David Harmon

On 9 Aug 2006 14:05:36 -0700 in comp.lang.c++, "(e-mail address removed)"
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top