r_sys_jcua.c 18.5 KB
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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
/*
****************************************************************************
PROJECT : JCUA driver
FILE    : $Id: r_sys_jcua.c 14698 2017-09-13 10:31:22Z shinya.tomari $
============================================================================

DESCRIPTION
JCUA support functions for d1x
============================================================================
                            C O P Y R I G H T
============================================================================
                           Copyright (c) 2013 - 2015
                                  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 customers premises listed in the signed license
agreement.

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

/***************************************************************************
  Title: JCUA Driver support functions

  Implementation of the JCUA driver support functions for the D1x device.
*/


/***************************************************************************
  Section: Includes
*/
#include    "r_typedefs.h"
#include    "r_jcua_api.h"
#include    "r_jcua_sys.h"
#include    "r_config_jcua.h"
#include    "r_dev_api.h"
#include    "r_tick_api.h"
#include    "r_ostm_api.h"

/***************************************************************************
  Section: Local Defines
*/
/***************************************************************************
  Constant: LOC_DEV_INT_ENABLE

  The offset value of the interrupt enable value.
*/
#define LOC_DEV_INT_ENABLE       (0x01u)

/***************************************************************************
  Constant: LOC_DEV_INT_DISABLE

  The offset value of the interrupt disable value.
*/
#define LOC_DEV_INT_DISABLE      (0x00u)


/***************************************************************************
  Constant: LOC_JCSWRST

  The address of the JCSWRST register (JCUA Software reset register)
*/
#define LOC_JCSWRST     (0xFFC06010u)

/***************************************************************************
  Constant: LOC_JCUA_JCUA0RES

  A constant for bit mask value and offset value of JCUA0RES(JCSWRST.JCUA0RES)

  LOC_JCUA_JCUA0RES_MASK  - Mask value.
  LOC_JCUA_JCUA0RES_SHIFT - Offset value.
*/
#define LOC_JCUA_JCUA0RES_MASK           (0x00000100u)
#define LOC_JCUA_JCUA0RES_SHIFT          (8)

/***************************************************************************
  Constant: LOC_JCUA_RESET_WAIT_TIME_US

  Wait time before JCSWRST.
*/
#define LOC_JCUA_RESET_WAIT_TIME_US       30u


/***************************************************************************
  Constant: LOC_JCUA_BUS_RESET_TIMEOUT_MS

  The timeout [in milliseconds] for JCUA bus reset.
  (JCUA Software reset via JCSWRST register)
*/
#define LOC_JCUA_BUS_RESET_TIMEOUT_MS     2u


/***************************************************************************
  Constant: 

  LOC_USE_OSTM               - Timer select. (0: use TICK0(OSTM0)  1: use OSTM1)

  In case of LOC_USE_OSTM == 1, OSTM driver detect any decode timeout (header or data)
  and OSTM driver notify timeout error to JCUA driver calling R_JCUA_IsrTimeOut callback function.

  loc_OstmInit()
       |
  loc_OstmStart()
       |
       |                        <----- timeout occur!
       +---------------------+  <----- OSTM interrupt!
                             |                  
                       loc_OstmCallback()
                          call  R_JCUA_IsrTimeOut() -> nortify timeout to JCUA driver
                             |                    
       +---------------------+
       |

*/

#define LOC_USE_OSTM                     (1)


/***************************************************************************
  Constant: 

  LOC_OSTM_UNIT              - Use OSTM unit number.
  LOC_JCUA_HEADER_TIMEOUT_US - Timeout value of Header part. (in usec)
                               If timeout is not needed, please define as 0.
  LOC_JCUA_DATA_TIMEOUT_US   - Timeout value of Image Data part. (in usec)
                               If timeout is not needed, please define as 0.

*/
#define LOC_OSTM_UNIT                    (1)
#define LOC_JCUA_HEADER_TIMEOUT_US       (4000u)      /* 4 msec */
#define LOC_JCUA_DATA_TIMEOUT_US         (0)          /* disable */


/***************************************************************************
  Section: Local Values
*/

#if (LOC_USE_OSTM == 1)
static uint32_t loc_TimerReached    = R_FALSE;
static uint32_t loc_PauseCouter     = 0;
static uint32_t loc_TiemOutCallback = R_FALSE;
#endif

/***************************************************************************
  Section: local Functions
*/

#if (LOC_USE_OSTM == 1)
/***************************************************************************
  Function: loc_OstmStop

  Description:
  This function stops OSTM driver.

  Parameter:
  Counter - Pointer to store the remain counter to reach timeout. 

  Return value:
  See <r_jcua_Error_t>.
*/
static r_jcua_Error_t loc_OstmStop(uint32_t* const Counter);
static r_jcua_Error_t loc_OstmStop(uint32_t* const Counter)
{
    r_jcua_Error_t     error = R_JCUA_ERR_OK;
    r_ostm_Error_t     ostm_err;
    
    ostm_err = R_OSTM_Stop(LOC_OSTM_UNIT);
    if (ostm_err == R_OSTM_ERR_OK)
    {
        if (Counter != R_NULL)
        {
            *Counter = R_OSTM_GetCounterValue(LOC_OSTM_UNIT);
        }
    }
    else
    {
        error = R_JCUA_ERR_TIMER_CTRL;
    }
    return error;
}

/***************************************************************************
  Function: loc_OstmCallback

  Description:
  This function is called when the setting time has arrived.
  It will be called from ISR.

  Parameter:
  None.

  Return value:
  None.
*/
static void loc_OstmCallback(void);
static void loc_OstmCallback(void)
{
    loc_TimerReached = R_TRUE;
    loc_OstmStop(R_NULL);
    
    if (loc_TiemOutCallback == R_TRUE)
    {
        /* Emergency Stop */
        R_JCUA_WRITE32(LOC_JCSWRST, LOC_JCUA_JCUA0RES_MASK);
        
        R_JCUA_IsrTimeOut(0);
        
        loc_TiemOutCallback = R_FALSE;
    }
}

/***************************************************************************
  Function: loc_OstmInit

  Description:
  This function initializes OSTM driver and set a timeout callback function to OSTM driver.

  Parameter:
  None.

  Return value:
  See <r_jcua_Error_t>.
*/
static r_jcua_Error_t loc_OstmInit(void);
static r_jcua_Error_t loc_OstmInit(void)
{
    r_jcua_Error_t     error = R_JCUA_ERR_OK;
    r_ostm_Error_t     ostm_err;
    r_ostm_Parameter_t ostm_prm;
    
    /* Init OS timer */
    ostm_prm.Mode   = R_OSTM_INTERVAL; 
    ostm_prm.CmpVal = 0;
    ostm_prm.IntCtl = R_OSTM_START_INT_DISABLE;
    
    ostm_err = R_OSTM_Init(LOC_OSTM_UNIT, &ostm_prm);
    if (ostm_err == R_OSTM_ERR_OK)
    {
        /* Set callback */
        ostm_err = R_OSTM_SetIsrCallback(LOC_OSTM_UNIT, R_OSTM_INT_TINT, &loc_OstmCallback);
        if (ostm_err == R_OSTM_ERR_OK)
        {
            ostm_err = R_OSTM_EnableInt(LOC_OSTM_UNIT, R_OSTM_INT_TINT);
        }
    }
    if (ostm_err != R_OSTM_ERR_OK)
    {
        error = R_JCUA_ERR_TIMER_CTRL;
    }
    return error;
}

/***************************************************************************
  Function: loc_OstmDeInit

  Description:
  This function deinitializes OSTM driver.

  Parameter:
  None.

  Return value:
  See <r_jcua_Error_t>.
*/
static r_jcua_Error_t loc_OstmDeInit(void);
static r_jcua_Error_t loc_OstmDeInit(void)
{
    r_jcua_Error_t     error = R_JCUA_ERR_OK;
    r_ostm_Error_t     ostm_err;
    
    ostm_err = R_OSTM_DisableInt(LOC_OSTM_UNIT, R_OSTM_INT_TINT);
    if (ostm_err == R_OSTM_ERR_OK)
    {
        /* Deinit OS timer */
        ostm_err = R_OSTM_DeInit(LOC_OSTM_UNIT);
    }
    if (ostm_err != R_OSTM_ERR_OK)
    {
        error = R_JCUA_ERR_TIMER_CTRL;
    }
    return error;
}

/***************************************************************************
  Function: loc_OstmStart

  Description:
  This function starts OSTM driver.
  If specified time or specified count is passed, <loc_OstmCallback> will called.

  Parameter:
  Usec  - Micro second time. If Usec != 0, Usec is used.
  Count - Count by OSTM clock frequency. If Usec == 0, Count is used.

  Return value:
  See <r_jcua_Error_t>.
*/
static r_jcua_Error_t loc_OstmStart(const uint32_t Usec, const uint32_t Count);
static r_jcua_Error_t loc_OstmStart(const uint32_t Usec, const uint32_t Count)
{
    r_jcua_Error_t     error = R_JCUA_ERR_OK;
    r_ostm_Error_t     ostm_err;
    uint32_t           cmp_val;
    uint32_t           ostm_clk;
    uint32_t           ostm_mhz;
    
    /* Get clock */
    ostm_clk = R_OSTM_GetClock(LOC_OSTM_UNIT);
    ostm_mhz = ostm_clk / 1000 / 1000; /* Hz->MHz */
    if (ostm_mhz != 0)
    {
        /* Set Compare value */
        if (Usec != 0)
        {
            cmp_val = ostm_mhz * Usec;
        }
        else if (Count != 0)
        {
            cmp_val = Count;
        }
        else
        {
            cmp_val = 1;
        }
        ostm_err = R_OSTM_SetCmp(LOC_OSTM_UNIT, cmp_val);
        if (ostm_err == R_OSTM_ERR_OK)
        {
            /* Timer start */
            loc_TimerReached = R_FALSE;
            ostm_err = R_OSTM_Start(LOC_OSTM_UNIT);
        }
        if (ostm_err != R_OSTM_ERR_OK)
        {
            error = R_JCUA_ERR_TIMER_CTRL;
        }
    }
    else
    {
        error = R_JCUA_ERR_TIMER_CTRL;
    }
    return error;
}
#endif /* LOC_USE_OSTM */


/***************************************************************************
  Section: JCUA Driver Support Functions
*/


/***************************************************************************
  Group: JCUA Basic interface functions
*/

/***************************************************************************
  Function: R_JCUA_Sys_Init

  see: <r_jcua_sys.h>
*/

r_jcua_Error_t R_JCUA_Sys_Init(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;
    uint32_t       val;

    if (Unit == 0)
    {
        /* Initialize Unit 0 of JCUA macro. */

        /* Bus Reset for JCUA (with timeout) */
        /* D1x OPC Rev5.01 item #12 "JCUA reset operation"
           -> JCCMD.BRST must not be used. (JCU macro internal reset not sufficent)
           -> JCSWRST.JCUA0RES must be used instead. (JCU macro external reset)
              Refer to D1x UM section 44.6 "Software Reset Processing".
                Use the following software reset procedure to reset the JCUA unit:
                0. Wait for 30us.
                1. Set JCSWRST.JCUA0RES bit to 1, to generate the software reset condition.
                2. Read JCSWRST.JCUA0RES bit until bit is set to 1, to wait for software reset is active.
                3. Set JCSWRST.JCUA0RES bit to 0, to release software reset condition.
                By this sequennce all JCUA registers with exception
                of the JCSWRST register are initialized by a software reset.
        */
#if (LOC_USE_OSTM == 0)
        uint32_t Timeout;
        Timeout = (R_TICK_GetTimeUS(0) + LOC_JCUA_RESET_WAIT_TIME_US);
        do {
            ;
        } while (Timeout > R_TICK_GetTimeUS(0));

        R_JCUA_WRITE32(LOC_JCSWRST, LOC_JCUA_JCUA0RES_MASK);
        val = 0;
        Timeout = (R_TICK_GetTimeMS(0) + LOC_JCUA_BUS_RESET_TIMEOUT_MS);
        while (0 == (val & LOC_JCUA_JCUA0RES_MASK)) {
            if (Timeout <= R_TICK_GetTimeMS(0))
            {
                /* timeout occured */
                return R_JCUA_ERR_BUS_TIMEOUT;
            }

            val = R_JCUA_READ32(LOC_JCSWRST);
        }
        R_JCUA_WRITE32(LOC_JCSWRST, 0u);
        
        /* Enable interrupt. */
        R_JCUA_Sys_InterruptEnable(0);

#else
        /* Initialize OS timer unit */
        error = loc_OstmInit();
        if (error == R_JCUA_ERR_OK)
        {
            loc_TiemOutCallback = R_FALSE;
            error = loc_OstmStart(LOC_JCUA_RESET_WAIT_TIME_US, 0);
            if (error == R_JCUA_ERR_OK)
            {
                /* 0. Wait for 30us. */
                while (loc_TimerReached == R_FALSE){};
                
                /* 1. Set JCSWRST.JCUA0RES bit to 1 */
                R_JCUA_WRITE32(LOC_JCSWRST, LOC_JCUA_JCUA0RES_MASK);
                
                error = loc_OstmStart(LOC_JCUA_BUS_RESET_TIMEOUT_MS * 1000, 0);
                if (error == R_JCUA_ERR_OK)
                {
                    /* 2. Read JCSWRST.JCUA0RES bit until bit is set to 1 */
                    val = 0;
                    while (0 == (val & LOC_JCUA_JCUA0RES_MASK)) {
                        if (loc_TimerReached == R_TRUE)
                        {
                            /* timeout occured */
                            loc_OstmStop(R_NULL);
                            return R_JCUA_ERR_BUS_TIMEOUT;
                        }

                        val = R_JCUA_READ32(LOC_JCSWRST);
                    }
                    error = loc_OstmStop(R_NULL);
                    if (error == R_JCUA_ERR_OK)
                    {
                        /* 3. Set JCSWRST.JCUA0RES bit to 0 */
                        R_JCUA_WRITE32(LOC_JCSWRST, 0u);
                        
                        /* Enable interrupt. */
                        R_JCUA_Sys_InterruptEnable(0);
                    }
                }
            }
        }
#endif
    }
    else
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    }
    return error;
}
/***************************************************************************
  Function: R_JCUA_Sys_DeInit

  see: <r_jcua_sys.h>
*/

r_jcua_Error_t R_JCUA_Sys_DeInit(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;

    if (Unit == 0)
    {
        /* De-Initialize Unit 0 of JCUA macro. */
        /* Disable interrupt. */
        R_JCUA_Sys_InterruptDisable(0);
#if (LOC_USE_OSTM == 1)
        error = loc_OstmDeInit();
#endif
    }
    else
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    }
    return error;
}


/***************************************************************************
  Function: R_JCUA_Sys_InterruptEnable

  see: <r_jcua_sys.h>
*/

void R_JCUA_Sys_InterruptEnable(const uint32_t Unit)
{
    if (Unit == 0)
    {
        R_DEV_IntEnable(R_DEV_INT_JCU0EDI, LOC_DEV_INT_ENABLE);
        R_DEV_IntEnable(R_DEV_INT_JCU0DTI, LOC_DEV_INT_ENABLE);
    } /* end if */
}


/***************************************************************************
  Function: R_JCUA_Sys_InterruptDisable

  see: <r_jcua_sys.h>
*/

void R_JCUA_Sys_InterruptDisable(const uint32_t Unit)
{
    if (Unit == 0)
    {
        R_DEV_IntEnable(R_DEV_INT_JCU0DTI, LOC_DEV_INT_DISABLE);
        R_DEV_IntEnable(R_DEV_INT_JCU0EDI, LOC_DEV_INT_DISABLE);
    } /* end if */
}

/***************************************************************************
  Function: R_JCUA_Sys_BaseAddrGet

  see: <r_jcua_sys.h>
*/
uint32_t R_JCUA_Sys_BaseAddrGet(const uint32_t Unit)
{
    uint32_t base_addr = 0;

    if (Unit == 0)
    {
        base_addr = R_JCUA_BASE_ADD;
    }
    return base_addr;
}

/***************************************************************************
  Function: R_JCUA_Sys_TimerStart

  see: <r_jcua_sys.h>
*/

r_jcua_Error_t R_JCUA_Sys_TimerStart(const uint32_t Unit, const uint32_t IsHeader)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;
    
    if (Unit == 0)
    {
#if (LOC_USE_OSTM == 1)
        if (IsHeader == R_TRUE)
        {
#if (LOC_JCUA_HEADER_TIMEOUT_US != 0)
            loc_TiemOutCallback = R_TRUE;
            error = loc_OstmStart(LOC_JCUA_HEADER_TIMEOUT_US, 0);
#endif
        }
        else
        {
#if (LOC_JCUA_DATA_TIMEOUT_US != 0)
            loc_TiemOutCallback = R_TRUE;
            error = loc_OstmStart(LOC_JCUA_DATA_TIMEOUT_US, 0);
#endif
        }
#endif /* LOC_USE_OSTM */
    }
    else
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    }
    
    return error;
}

/***************************************************************************
  Function: R_JCUA_Sys_TimerPause

  see: <r_jcua_sys.h>
*/
r_jcua_Error_t R_JCUA_Sys_TimerPause(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;
    
    if (Unit == 0)
    {
#if (LOC_USE_OSTM == 1)
        error = loc_OstmStop(&loc_PauseCouter);
#endif
    }
    else
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    }
    
    return error;
}

/***************************************************************************
  Function: R_JCUA_Sys_TimerResume

  see: <r_jcua_sys.h>
*/
r_jcua_Error_t R_JCUA_Sys_TimerResume(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;
    
    if (Unit == 0)
    {
#if (LOC_USE_OSTM == 1)
        if (loc_TiemOutCallback == R_TRUE)
        {
            error = loc_OstmStart(0, loc_PauseCouter);
        }
#endif
    }
    else
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    }
    
    return error;
}

/***************************************************************************
  Function: R_JCUA_Sys_TimerStop

  see: <r_jcua_sys.h>
*/
r_jcua_Error_t R_JCUA_Sys_TimerStop(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;
    
    if (Unit == 0)
    {
#if (LOC_USE_OSTM == 1)
        loc_TiemOutCallback = R_FALSE;
        error = loc_OstmStop(R_NULL);
#endif
    }
    else
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    }
    
    return error;
}

/***************************************************************************
  Group: JCUA OS interface
*/


/***************************************************************************
  Function: R_JCUA_Sys_Lock

  see: <r_jcua_sys.h>
*/

r_jcua_Error_t R_JCUA_Sys_Lock(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;

    /* Check parameters. */
    if (Unit >= R_JCUA_MACRO_NUM)
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    } /* end if */

    return error;
}


/***************************************************************************
  Function: R_JCUA_Sys_Unlock

  see: <r_jcua_sys.h>
*/

r_jcua_Error_t R_JCUA_Sys_Unlock(const uint32_t Unit)
{
    r_jcua_Error_t error = R_JCUA_ERR_OK;

    /* Check parameters. */
    if (Unit >= R_JCUA_MACRO_NUM)
    {
        error = R_JCUA_ERR_RANGE_UNIT;
    } /* end if */
    return error;
}