r_dev_pin.c 16 KB
Newer Older
时昊's avatar
时昊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
/*
****************************************************************************
PROJECT : device driver for V-Lib
FILE    : $Id: r_dev_pin.c 5139 2015-05-27 13:21:40Z tobyas.hennig $
============================================================================
DESCRIPTION
r7f701412 interrupt service routines
============================================================================
                            C O P Y R I G H T
============================================================================
                       Copyright (c) 2013 - 2014
                                  by
                       Renesas Electronics (Europe) GmbH.
                           Arcadiastrasse 10
                          D-40472 Duesseldorf
                               Germany
                          All rights reserved.
============================================================================
Purpose: only for testing, not for mass production

DISCLAIMER

LICENSEE has read, understood and accepted the terms and conditions defined in
the license agreement, especially the usage rights. In any case, it is
LICENSEE's responsibility to make sure that any user of the software complies
with the terms and conditions of the signed license agreement.

SAMPLE CODE is not part of the licensed software, as such it must not be used in
mass-production applications. It can only be used for evaluation and
demonstration purposes at customer's premises listed in the signed license
agreement.

****************************************************************************
*/

/*******************************************************************************
  Title: upd703670 port-pin init  functions


*/

/*******************************************************************************
  Section: Includes
*/

#include "r_typedefs.h"
#include "r_dev_api.h"
#include "r_config_pin.h"

/*******************************************************************************
  Section: Local Constants
*/

/*******************************************************************************
  PORTC Register Offsets
*/

/************************************************************************************************

Port registers
  <see r_dev_PinRegs_t> in the file "r_dev_pin_types.h".

*/

/*******************************************************************************
Array : R_GPIO_Offset_Table

The R_GPIO_Offset_Table array specifies  Register offset in a Macro
*/

static const uint32_t loc_RegOffset [ R_DEV_PIN_REG_LAST ] = {
    /*R_DEV_PIN_REG_PCR  */ 0x2000u,

    /*R_DEV_PIN_REG_P    */ 0x0000u,
    /*R_DEV_PIN_REG_PSR  */ 0x0004u,
    /*R_DEV_PIN_REG_PPR  */ 0x000Cu,
    /*R_DEV_PIN_REG_PM   */ 0x0010u,
    /*R_DEV_PIN_REG_PMC  */ 0x0014u,
    /*R_DEV_PIN_REG_PFC  */ 0x0018u,
    /*R_DEV_PIN_REG_PFCE */ 0x001Cu,
    /*R_DEV_PIN_REG_PNOT */ 0x0008u,
    /*R_DEV_PIN_REG_PMSR */ 0x0020u,
    /*R_DEV_PIN_REG_PMCSR*/ 0x0024u,

    /*  OS  */
    /*R_DEV_PIN_REG_PIBC */ (R_DEV_PIN_BASEO_OFFSET + 0x4000u),
    /*R_DEV_PIN_REG_PBDC */ (R_DEV_PIN_BASEO_OFFSET + 0x4004u),
    /*R_DEV_PIN_REG_PIPC */ (R_DEV_PIN_BASEO_OFFSET + 0x4008u),
    /*R_DEV_PIN_REG_PU   */ (R_DEV_PIN_BASEO_OFFSET + 0x400Cu),
    /*R_DEV_PIN_REG_PD   */ (R_DEV_PIN_BASEO_OFFSET + 0x4010u),
    /*R_DEV_PIN_REG_PODC */ (R_DEV_PIN_BASEO_OFFSET + 0x4014u),
    /*R_DEV_PIN_REG_PDSC */ (R_DEV_PIN_BASEO_OFFSET + 0x4018u),
    /*R_DEV_PIN_REG_PIS  */ (R_DEV_PIN_BASEO_OFFSET + 0x401Cu),
    /*R_DEV_PIN_REG_PISA */ (R_DEV_PIN_BASEO_OFFSET + 0x4024u),
    /*R_DEV_PIN_REG_PPCMD*/ (R_DEV_PIN_BASEO_OFFSET + 0x402Cu),
    /*R_DEV_PIN_REG_PPROTS*/ (R_DEV_PIN_BASEO_OFFSET + 0x4034u),
};

static const r_dev_PortInputCapability_t loc_PortCapability [] =
    /*  Port    InputType11,        InputType10,        InputType01,        InputType00 */
    {
        {0u, R_DEV_PIN_TTL, R_DEV_PIN_TTL, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {1u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {2u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {3u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {16u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {17u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {21u, R_DEV_PIN_TTL_MLB, R_DEV_PIN_TTL_MLB, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1},
        {42u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {43u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {46u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},
        {47u, R_DEV_PIN_CMOS1, R_DEV_PIN_CMOS1, R_DEV_PIN_SCHMITT4, R_DEV_PIN_SCHMITT1},

        /* delimiter - do not remove */
        {0xFFu, R_DEV_PIN_NONE, R_DEV_PIN_NONE, R_DEV_PIN_NONE, R_DEV_PIN_NONE},
};

/*******************************************************************************
  Section: Local types
*/

/*******************************************************************************
  Section: Local variables
*/
static uint8_t loc_PinEnable = 1;

/*******************************************************************************
  Section: Global functions
*/
/*******************************************************************************
  Function: R_DEV_PinOutStateGet

  See: r_dev_api.h for details

 */
void R_DEV_PinOutStateSet(uint8_t State)
{
    loc_PinEnable = State;
}
/*******************************************************************************
  Function: R_DEV_PinOutStateGet

  See: r_dev_api.h for details

*/
uint8_t R_DEV_PinOutStateGet(void)
{
    return loc_PinEnable;
}

/*******************************************************************************
  Function: R_DEV_PinConfig

  See: r_dev_api.h for details

*/

uint8_t R_DEV_PinConfig(const r_dev_PinConfig_t *PinCfg)
{
    uint32_t addr                = 0u;
    uint32_t prot_addr           = 0u;
    uint32_t reg_val             = 0u;
    uint8_t  ret_val             = 1u;
    uint16_t pin_position        = 1u;
    uint8_t  port_position       = 0u;
    uint8_t  portHasPisaRegister = 0u;
    uint8_t  portHasPisRegister  = 0u;
    uint8_t  PisaRegBitValue     = 0u;
    uint8_t  PisRegBitValue      = 0u;

    if ( 0 == loc_PinEnable )
    {
        return ret_val;
    }
    pin_position = ( uint16_t )(pin_position << PinCfg->Pin);
    /* optional check if already configured */
    /* read PMC, if 1 -> already set to other alternative mode, abort */
    /* read PM, if not 1 -> already set to output, abort (this is in case of pure
     * GPIO) */

    /* Input or output  */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PM, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( R_DEV_PIN_OUT == PinCfg->Dir )
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    else /* Set direction to input also in case of direct control */
    {
        reg_val |= pin_position;
    }
    R_DEV_WRITE_REG(16, addr, reg_val);

    /* direct control */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PIPC, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( R_DEV_PIN_DIRECT == PinCfg->Dir )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    R_DEV_WRITE_REG(16, addr, reg_val);

    /* activate pull-up / pull-down resistors */
    /*  set pull-up */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PU, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( R_DEV_PIN_PULLUP == PinCfg->Pull )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    R_DEV_WRITE_REG(16, addr, reg_val);
    /*  set pull-down */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PD, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( R_DEV_PIN_PULLDOWN == PinCfg->Pull )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    R_DEV_WRITE_REG(16, addr, reg_val);

    /* feedback input (bidirectional mode) */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PBDC, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( 1 == PinCfg->Feedback )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    R_DEV_WRITE_REG(16, addr, reg_val);

    /*Set mode to GPIO or alternative function */
    switch ( PinCfg->Func )
    {
        case 0: /* GPIO */
            /*Set PFC and PFCE bits to 0 */
            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFC, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val &= ( uint16_t )(~pin_position);
            R_DEV_WRITE_REG(16, addr, reg_val);

            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFCE, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val &= ( uint16_t )(~pin_position);
            R_DEV_WRITE_REG(16, addr, reg_val);
            break;
        case 1: /* Alternative function 1 */
            /*Set PFC and PFCE bits to 0 */
            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFC, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val &= ( uint16_t )(~pin_position);
            R_DEV_WRITE_REG(16, addr, reg_val);

            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFCE, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val &= ( uint16_t )(~pin_position);
            R_DEV_WRITE_REG(16, addr, reg_val);
            break;
        case 2: /* Alternative function 2 */
            /*Set PFC bit to 1  and PFCE bit to 0 */
            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFC, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val |= pin_position;
            R_DEV_WRITE_REG(16, addr, reg_val);

            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFCE, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val &= ( uint16_t )(~pin_position);
            R_DEV_WRITE_REG(16, addr, reg_val);
            break;
        case 3: /* Alternative function 3 */
            /*Set PFC bit to 0  and PFCE bit to 1 */
            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFC, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val &= ( uint16_t )(~pin_position);
            R_DEV_WRITE_REG(16, addr, reg_val);

            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFCE, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val |= pin_position;
            R_DEV_WRITE_REG(16, addr, reg_val);
            break;
        case 4: /* Alternative function 4 */
            /*Set PFC and PFCE bits to 1 */
            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFC, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val |= pin_position;
            R_DEV_WRITE_REG(16, addr, reg_val);

            addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PFCE, PinCfg->Port);
            reg_val = R_DEV_READ_REG(16, addr);
            reg_val |= pin_position;
            R_DEV_WRITE_REG(16, addr, reg_val);
            break;
        default:
            R_DEV_ERROR(0u, R_DEV_ERR_NG, "We do not have what you want!");
            break;
    }

    /* Some electrical charactersitics */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PDSC, PinCfg->Port);
    reg_val = R_DEV_READ_REG(32, addr);
    if ( 0 != PinCfg->HiSpeed )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    prot_addr = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PPCMD, PinCfg->Port);
    R_DEV_WRITE_PROT_REG(prot_addr, addr, reg_val);

    /* Activate pin */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PMC, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( 0 != PinCfg->Func )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    R_DEV_WRITE_REG(16, addr, reg_val);

    /* Set input characteristic */
    port_position       = 0u;
    portHasPisaRegister = 1u;
    portHasPisRegister  = 1u;
    PisaRegBitValue     = 0u;
    PisRegBitValue      = 0u;
    do
    {
        if ( loc_PortCapability [ port_position ].Port == PinCfg->Port )
        {
            if ( loc_PortCapability [ port_position ].InputType00 == PinCfg->InputType )
            {
                PisaRegBitValue = 0;
                PisRegBitValue  = 0;
            }
            if ( loc_PortCapability [ port_position ].InputType01 == PinCfg->InputType )
            {
                PisaRegBitValue = 0;
                PisRegBitValue  = 1;
                if ( loc_PortCapability [ port_position ].InputType01 == loc_PortCapability [ port_position ].InputType00 )
                {
                    portHasPisRegister = 0;
                }
            }
            if ( loc_PortCapability [ port_position ].InputType10 == PinCfg->InputType )
            {
                PisaRegBitValue = 1;
                PisRegBitValue  = 0;
            }
            if ( loc_PortCapability [ port_position ].InputType11 == PinCfg->InputType )
            {
                PisaRegBitValue = 1;
                PisRegBitValue  = 1;
                if ( loc_PortCapability [ port_position ].InputType11 == loc_PortCapability [ port_position ].InputType10 )
                {
                    portHasPisRegister = 0;
                }
            }

            break;
        }
        port_position += 1;
    } while ( 0xFFu != loc_PortCapability [ port_position ].Port );

    if ( 0 != portHasPisaRegister )
    {
        addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PIS, PinCfg->Port);
        reg_val = R_DEV_READ_REG(16, addr);
        if ( 0 != PisaRegBitValue )
        {
            reg_val |= pin_position;
        }
        else
        {
            reg_val &= ( uint16_t )(~pin_position);
        }
        R_DEV_WRITE_REG(16, addr, reg_val);
    }

    if ( 0 != portHasPisRegister )
    {
        addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PIS, PinCfg->Port);
        reg_val = R_DEV_READ_REG(16, addr);
        if ( 0 != PisRegBitValue )
        {
            reg_val |= pin_position;
        }
        else
        {
            reg_val &= ( uint16_t )(~pin_position);
        }
        R_DEV_WRITE_REG(16, addr, reg_val);
    }

    /* Activate input buffer in case of GP input  */
    addr    = R_DEV_PinGetRegAddr(R_DEV_PIN_REG_PIBC, PinCfg->Port);
    reg_val = R_DEV_READ_REG(16, addr);
    if ( (0 == PinCfg->Func) && (R_DEV_PIN_IN == PinCfg->Dir) )
    {
        reg_val |= pin_position;
    }
    else
    {
        reg_val &= ( uint16_t )(~pin_position);
    }
    R_DEV_WRITE_REG(16, addr, reg_val);

    return ret_val;
}

/*******************************************************************************
  Function: R_DEV_PinInit

  See: r_dev_api.h for details
*/
void R_DEV_PinInit(const r_dev_PinConfig_t *PinCfg)
{
    uint16_t i = 0u;
    /* Here we can glbally disable any pion configuration */
    if ( 0 == loc_PinEnable )
    {
        return;
    }
    /* assuming we got a config table, we go through it until
    we see the delimiter */
    while ( R_DEV_PIN_LAST != PinCfg [ i ].Pin )
    {
        R_DEV_PinConfig(&(PinCfg [ i ]));
        i++;
    }
}

/*******************************************************************************
  Function: R_DEV_PinGetRegAddr

  See: r_dev_api.h for details
*/
uint32_t R_DEV_PinGetRegAddr(r_dev_PinRegs_t Reg, uint32_t Port)
{
    return R_DEV_PinGetRegAddrExt(Reg, Port, 0u);
}

/*******************************************************************************
  Function: R_DEV_PinGetRegAddrExt

  See: r_dev_api.h for details
*/
uint32_t R_DEV_PinGetRegAddrExt(r_dev_PinRegs_t Reg, uint32_t Port, uint32_t Pin)
{
    uint32_t addr = R_DEV_PIN_BASE + (R_DEV_PORT_OFFSET * Port);

    if ( Reg < R_DEV_PIN_REG_LAST )
    {
        if ( Port == 0 )
        {
            /* add offset for port n = 0 */
            addr += R_DEV_PIN_BASEOU_0_OFFSET;
        }
        if ( R_DEV_PIN_REG_PCR == Reg )
        {
            /* only for PCRn register, that has pin based control */
            addr += (R_DEV_PIN_OFFSET * Pin);
        }
        addr += loc_RegOffset [ Reg ];
    }
    else
    {
    }
    return addr;
}