Renesas 78K0R GPIO driver

Thread Starter

robertin75

Joined Oct 18, 2012
5
Hello:

I'm deveoping a simple IO driver for the Renesas 78K0R family of microcontrollers.

For now I have 2 functions:

bool Get_Port (unsigned char port_id, unsigned char pin_mask)

void Set_port (unsigned char port_id, unsigned char pin_mask, bool value).

For example in the Set_port function I have a switch case which checks for all the ports that belong to this micro, something like:

#define PORT1 0
#define PORT2 1

switch(port_id):

case PORT1:
value == TRUE? (P1|=pin_mask:p1 &= ~pin_mask);
case PORT2:
value == TRUE? (P2|=pin_mask:p2 &= ~pin_mask);
.
.
.
.

As you can see the logic is very simple. If for example I want to turn on a LED I just pass a value of 1. If I want to turn it off I pass a value of 0.

Is there any way to make the port number as a parameter so I don't have to use a lot of switch..case statements?

Otherwise if I have 10 different ports I will have to create 10 different switch..case statements.

Thanks and help is greatly appreciated
 

Thread Starter

robertin75

Joined Oct 18, 2012
5
Use a (shift with a and/or) mask to set or clear the bits.

http://en.wikipedia.org/wiki/Mask_(computing)

Use the bitwise OR operator (|) to set a bit.
number |= 1 << x;

Use the bitwise AND operator (&) to clear a bit.
number &= ~(1 << x);
That's something similar of what I'm doing if you look at my first post.

pin_mask already has the proper masks so no need to add the extra 1 when doing the shifting.

e.g. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 etc.

P1 and P2 are microcontroller specific IO registers for the Renesas 78K0R defined in a header file.

My main question is how can I code a function that would take as a parameter a specific register?

If there are 50 ports I will have to code 50 switch..case statements which makes the code not very portable and practical.

The type of those registers in the header file is not a simple data type like usigned char but some weird types that start with ___.

Thanks
 
Last edited:

nsaspook

Joined Aug 27, 2009
13,265
That's what I'm doing if you look at my first post.

P1 and P2 are microcontroller specific IO registers for the Renesas 78K0R defined in a header file.

My main question is how can I code a function that would take as a parameter a specific register?

The type of those registers in the header file is not a simple data type like usigned char but some weird types that start with ___.

Thanks
If it's a memory-mapped register you can use pointers with the address of the register as a parameter.

http://www.embedded.com/electronics-blogs/programming-pointers/4025002/Mapping-memory
http://www.embedded.com/electronics...nters/4208573/Accessing-memory-mapped-classes
 

nsaspook

Joined Aug 27, 2009
13,265
I already tried that but it does not work.

The LED's won't turn on.

Hopefully that article will shed some light.

Thanks and will report back if I still have problems.
That should work if you are passing the correct type pointer and using a deref to access the actual data at that pointer address. Make sure any pointers are "volatile" as your compiler might optimize the code to nothing otherwise.
 
Last edited:
Top