Renesas 78K0R GPIO driver

Discussion in 'Embedded Systems and Microcontrollers' started by robertin75, Oct 18, 2012.

  1. robertin75

    Thread Starter New Member

    Oct 18, 2012
    5
    0
    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
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,913
    2,187
    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);
     
  3. robertin75

    Thread Starter New Member

    Oct 18, 2012
    5
    0
    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: Oct 18, 2012
  4. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,913
    2,187
    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
     
  5. robertin75

    Thread Starter New Member

    Oct 18, 2012
    5
    0
  6. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,913
    2,187
    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: Oct 18, 2012
Loading...