frt.c 14.1 KB
Newer Older
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
/******************************************************************************
 * $Revision: 423 $
 * $Date:: 2017-04-07 16:03:30 +0900#$
 *****************************************************************************/
/* __DISCLAIMER_START__                                                      */
/******************************************************************************
* (c)2017, Cypress Semiconductor Corporation
* or a subsidiary of Cypress Semiconductor Corporation. All rights
* reserved.
*
* This software, including source code, documentation and related
* materials ( "Software" ), is owned by Cypress Semiconductor
* Corporation or one of its subsidiaries ( "Cypress" ) and is protected by
* and subject to worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ( "EULA" ).
*
* If no EULA applies, Cypress hereby grants you a personal, nonexclusive,
* non-transferable license to copy, modify, and compile the
* Software source code solely for use in connection with Cypress' s
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING,
* BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE. Cypress reserves the right to make
* changes to the Software without notice. Cypress does not assume any
* liability arising out of the application or use of the Software or any
* product or circuit described in the Software. Cypress does not
* authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ( "High Risk Product" ). By
* including Cypress' s product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
******************************************************************************/
/* __DISCLAIMER_END__                                                        */
/*****************************************************************************
 ** \file frt.c
 **
 ** A detailed description is available at
 ** @link FrtGroup FRT Module description @endlink
 **
 ** History:
 ** - 2014-05-30  0.01  ST  First version
 *****************************************************************************/

/*****************************************************************************/
/* Include files                                                             */
/*****************************************************************************/
#include "pdl.h"
#include "interrupts.h"


#if defined( PDL_PERIPHERAL_FRT_ACTIVE )

/**
 *****************************************************************************
 ** \addtogroup FrtGroup
 *****************************************************************************/
//@{

/*****************************************************************************/
/* Local pre-processor symbols/macros ('#define')                            */
/*****************************************************************************/


/*****************************************************************************/
/* Global variable definitions (declared in header file with 'extern')       */
/*****************************************************************************/


/*****************************************************************************/
/* Local type definitions ('typedef')                                        */
/*****************************************************************************/


/*****************************************************************************/
/* Local variable definitions ('static')                                     */
/*****************************************************************************/


/*****************************************************************************/
/* Local function prototypes ('static')                                      */
/*****************************************************************************/
static void FrtIrqHandler( volatile stc_frtn_t* pstcFrt,
                           stc_frt_intern_data_t* pstcFrtInternData
                         ) ;


/*****************************************************************************/
/* Function implementation - global ('extern') and local ('static')          */
/*****************************************************************************/

#include "frt_generated.c"

/**
 *****************************************************************************
 ** \brief ISR callback for FRT.
 **
 ** This handler is called by the FRT ISR whenever a FRT triggers an interrupt.
 ** It in return calls the callback function that has been given
 ** during FRT initialization (see Frt_Init() and stc_frt_config_t) and clears
 ** the interrupt flags of the Free Running Timer.
 **
 ** \param [in] pstcFrt           FRT unit instance that caused the interrupt.
 ** \param [in] pstcFrtInternData Internal data associated with the FRT instance.
 *****************************************************************************/
static void FrtIrqHandler( volatile stc_frtn_t* pstcFrt,
                           stc_frt_intern_data_t* pstcFrtInternData
                         )
{
    /* Check if a compare clear/overflow interrupt is raised */
    if ( pstcFrt->unTCCS.stcField.u1ICLR == 1 )
    {
        /* Clear compare clear flag */
        PDL_WRITE_REG_SYNC(pstcFrt->unTCCSC.stcField.u1ICLRC, 1);

        /* Call callback */
        if ( pstcFrtInternData->pfnCompareClearCallback != NULL )
        {
            pstcFrtInternData->pfnCompareClearCallback() ;
        }
    } /* if ( pstcFrt->unTCCS.stcField.u1ICLR == 1 ) */

    /* Check if a zero detect interrupt is raised */
    if ( pstcFrt->unTCCS.stcField.u1IRQZF == 1 )
    {
        /* Clear compare clear flag */
        PDL_WRITE_REG_SYNC(pstcFrt->unTCCSC.stcField.u1IRQZFC, 1);

        /* Call callback */
        if ( pstcFrtInternData->pfnZeroDetectCallback != NULL )
        {
            pstcFrtInternData->pfnZeroDetectCallback() ;
        }
    } /* if ( pstcFrt->unTCCS.stcField.u1IRQZF == 1 ) */
} /* FrtIrqHandler */


/**
 *****************************************************************************
 ** \brief Initialize a Free Running Timer configuration
 **
 ** This function initializes the given FRT instance with the provided
 ** parameters.
 ** The parameters are checked for plausibility and the given callbacks are
 ** stored internally.
 **
 ** To enable the configuration and prevent side effects, the timer is stopped
 ** before the settings are applied. The interrupts are disabled and the
 ** interrupt mask counters are set to zero.
 **
 ** \param [in]  pstcFrt          FRT unit instance
 ** \param [in]  pstcConfig       FRT configuration
 **
 ** \retval Ok                    FRT configuration has been applied.
 ** \retval ErrorInvalidParameter If one of the following conditions are met:
 **             - pstcFrt == NULL
 **             - pstcConfig == NULL
 *****************************************************************************/
en_result_t Frt_Init( volatile stc_frtn_t* pstcFrt, const stc_frt_config_t* pstcConfig )
{
    /* Pointer to internal data structure */
    stc_frt_intern_data_t* pstcInternData ;

    /* Shadow data to avoid RMW and speed up HW access */
    un_frtn_tccs_t     unTCCS     = { 0 } ;
    un_frtn_teccs_t    unTECCS    = { 0 } ;

    /* Check parameters */
    if ( pstcFrt    == NULL ||
         pstcConfig == NULL
       )
    {
        return ErrorInvalidParameter ;
    }

    /* Stop FRT before configuration */
    pstcFrt->unTCCSS.stcField.u1STOPS = 1 ;
    PDL_WRITE_REG_SYNC(pstcFrt->unTCCSC.stcField.u1IRQZFC, 1);
    PDL_WRITE_REG_SYNC(pstcFrt->unTCCSC.stcField.u1ICLRC, 1);

    /* Clear timer */
    /* Since the timer is now stopped the FRTn_TCCS_SCLR bit will not work. */
    /* We have to write to the FRTn_TCDT register instead. */
    pstcFrt->unTCDT.u32Register = 0x00000000 ;

    /* Get pointer to internal data structure */
    pstcInternData = FrtGetInternDataPtr( pstcFrt ) ;
    /* Store callbacks and related parameters if needed */
    if ( pstcInternData != NULL )
    {
        pstcInternData->pfnCompareClearCallback    = \
                                           pstcConfig->pfnCompareClearCallback ;
        pstcInternData->pfnZeroDetectCallback      = \
                                             pstcConfig->pfnZeroDetectCallback ;
    }

    /* FRT configuration */

    /* Setup Compare Clear Buffer Enable */
    unTCCS.stcField.u1BFE   = \
                       (pstcConfig->bCompareClearBufferEnable == TRUE) ? 1 : 0 ;

    /* Setup Count Mode of FRT */
    unTCCS.stcField.u1MODE = \
                              (pstcConfig->bUpDownCountEnable == TRUE) ? 1 : 0 ;

    /* Setup interrupt mask selection */
    unTCCS.stcField.u3MSI   = pstcConfig->enInterruptMaskSelection ;

    /* Setup clock settings */
    unTCCS.stcField.u4CLK   = pstcConfig->enPrescaler ;

    /* Stop is the default mode */
    unTCCS.stcField.u1STOP = 1 ;

    /* Setup interrupt mask mode2 */
    unTECCS.stcField.u1MODE2 = \
                              (pstcConfig->bInterruptMaskMode2 == TRUE) ? 1 : 0 ;

    /* Set Compare clear interrupt mask selection */
    unTECCS.stcField.u3MSI   = pstcConfig->enCompareClearInterruptMaskSelection ;


    /* Now write shadow data to real HW */
    pstcFrt->unTECCS.u32Register    = unTECCS.u32Register ;
    pstcFrt->unTCCS.u32Register     = unTCCS.u32Register ;

    return Ok ;
} /* Frt_Init */


/**
 *****************************************************************************
 ** \brief Start a FRT.
 **
 ** This function starts the given FRT. If callbacks were set by Frt_Init(),
 ** the FRT interrupts are enabled and the internal ISR in turn calls the given callbacks.
 **
 ** The interrupts(and thus the callbacks) are not enabled even if callbacks were set by Frt_Init().
 **
 ** \param [in]  pstcFrt               FRT unit instance
 ** \param [in]  u16CompareClearValue  Compare clear value to count up to
 **
 ** \retval Ok                         FRT has been started.
 ** \retval ErrorInvalidParameter      If one of the following conditions are met:
 **                                 - pstcFrt == NULL
 **                                 - pstcConfig == NULL
 *****************************************************************************/
en_result_t Frt_Start( volatile stc_frtn_t* pstcFrt,
                       uint32_t u32CompareClearValue
                     )
{
    /* Pointer to internal data structure */
    stc_frt_intern_data_t* pstcInternData ;

    /* Check for NULL pointer */
    if ( pstcFrt == NULL )
    {
        return ErrorInvalidParameter ;
    }

    /* Enable interrupt if a callback has been given */
    pstcInternData = FrtGetInternDataPtr( pstcFrt ) ;

    if ( pstcInternData->pfnCompareClearCallback != NULL ||
         pstcInternData->pfnZeroDetectCallback   != NULL
       )
    {
        /* Enable zero detect interrupt */
        pstcFrt->unTCCSS.stcField.u1IRQZES = \
                       (pstcInternData->pfnZeroDetectCallback != NULL) ? 1 : 0 ;

        /* Enable compare clear interrupt */
        pstcFrt->unTCCSS.stcField.u1ICRES = \
                     (pstcInternData->pfnCompareClearCallback != NULL) ? 1 : 0 ;
    }

    /* Stop FRT (set stop bit) */
    pstcFrt->unTCCSS.stcField.u1STOPS = 1 ;

    /* Clear timer */
    pstcFrt->unTCDT.u32Register = 0x00000000;

    /* Set compare clear value */
    pstcFrt->unCPCLRB.u32Register = u32CompareClearValue ;

    /* Clear "zero detect" and "compare clear" flags */
    PDL_WRITE_REG_SYNC(pstcFrt->unTCCSC.stcField.u1IRQZFC, 1);
    PDL_WRITE_REG_SYNC(pstcFrt->unTCCSC.stcField.u1ICLRC, 1);

    /* Start counting (clear stop bit) */
    pstcFrt->unTCCSC.stcField.u1STOPC = 1 ;

    return Ok ;
} /* Frt_Start */


/**
 *****************************************************************************
 ** \brief Stop a running FRT.
 **
 ** This function stops the timer and disables the FRT interrupts for the specified
 ** FRT instance.
 **
 ** \param [in]  pstcFrt          FRT unit instance
 **
 ** \retval Ok                    FRT has been stopped
 ** \retval ErrorInvalidParameter If pstcFrt == NULL
 *****************************************************************************/
en_result_t Frt_Stop( volatile stc_frtn_t* pstcFrt )
{
    /* Check for NULL pointer */
    if ( pstcFrt == NULL )
    {
        return ErrorInvalidParameter;
    }

    /* Stop FRT (set stop bit) */
    pstcFrt->unTCCSS.stcField.u1STOPS = 1 ;

    /* Disable zero detect interrupt */
    PDL_SAFELY_DISABLE_INTERRUPT(pstcFrt->unTCCSC.stcField.u1IRQZEC, 1)

    /* Disable compare clear interrupt */
    PDL_SAFELY_DISABLE_INTERRUPT(pstcFrt->unTCCSC.stcField.u1ICLRC, 1)

    return Ok ;
} /* Frt_Stop */


/**
 *****************************************************************************
 ** \brief Get a Timer data.
 **
 ** This function get a Timer data value.
 **
 ** \param [in]   pstcFrt          FRT unit instance
 ** \param [out]  data             Timer data
 **
 ** \retval Ok                    Got a timer data.
 ** \retval ErrorInvalidParameter If pstcFrt == NULL.
 *****************************************************************************/
en_result_t Frt_GetCount( volatile stc_frtn_t* pstcFrt, uint32_t* data )
{
    /* Check for NULL pointer */
    if ( pstcFrt == NULL )
    {
        return ErrorInvalidParameter;
    }

    *data = pstcFrt->unTCDT.u32Register;

    return Ok ;
} /* Frt_GetCount */


//@}
#endif /* PDL_PERIPHERAL_FRT_ACTIVE */
/*****************************************************************************/
/* EOF (not truncated)                                                       */
/*****************************************************************************/