Freescale Semiconductor Inc.
Main Page | Data Structures | File List | Data Fields | Globals

etpu_cam.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 *
00003 * Freescale Semiconductor Inc.
00004 * (c) Copyright 2004-2014 Freescale Semiconductor, Inc.
00005 * ALL RIGHTS RESERVED.
00006 *
00007 ****************************************************************************//*!
00008 *
00009 * @file    etpu_cam.c
00010 *
00011 * @author  Milan Brejl [r54529]
00012 *
00013 * @version 1.0
00014 *
00015 * @date    16-Mar-2014
00016 *
00017 * @brief   This file contains API for using the eTPU function
00018 *          Cam (CAM).
00019 *
00020 ****************************************************************************//*!
00021 *
00022 * @mainpage
00023 *
00024 * The eTPU CAM APIs @ref etpu_cam.c/.h includes API functions for eTPU
00025 * function CAM, typically used together with CRANK in an engine position system.
00026 *
00027 * The CAM eTPU function uses 1 eTPU channel to log input signal transitions.
00028 * More instances of CAM can be initialized to independently log several inputs.
00029 *
00030 * Features:
00031 * - Based on the selected mode, either raising, falling or both signal
00032 *   transitions are detected and logged.
00033 * - The log buffer size is configurable.
00034 * - The log can be reset automatically by a link from Crank eTPU function or
00035 *   manually by the CPU application. Reseting the log means setting the buffer
00036 *   index to the buffer start position so that the next transition overwrites
00037 *   the first value in the buffer.
00038 * - Number of transitions logged during the last engine cycle (between last
00039 *   two log resets) and the actual position in the log buffer are available
00040 *   to read.
00041 * - 2 error conditions are reported:
00042 *   - @ref FS_ETPU_CAM_ERROR_ZERO_TRANS – no input transition was logged during
00043 *     the last engine cycle (between last two leg resets).
00044 *     It might mean the cam signal is lost.
00045 *   - @ref FS_ETPU_CAM_ERROR_LOG_OVERFLOW – the log buffer size is not big
00046 *     enought to log all input transitions. The last transition was not logged.
00047 * - Channel interrupt is generated when an error condition is detected.
00048 *
00049 * A single item in the log buffer is a 32-bit word which includes:
00050 * - transition TCR2 angle in the lower 24 bits,
00051 * - transition polarity (0-falling, 1-rising) in upper 8 bits.
00052 *
00053 *******************************************************************************/
00054 /*******************************************************************************
00055 * Includes
00056 *******************************************************************************/
00057 #include "etpu_cam.h"     /* private header file */
00058 #include "etpu_util.h"    /* utility routines for working with the eTPU */
00059 
00060 /*******************************************************************************
00061 * Global variables
00062 *******************************************************************************/
00063 extern uint32_t fs_etpu_data_ram_start;
00064 extern uint32_t fs_etpu_data_ram_ext;
00065 
00066 /*******************************************************************************
00067 * FUNCTION: fs_etpu_cam_init
00068 ****************************************************************************//*!
00069 * @brief   This function initializes eTPU channels to run CAM function.
00070 *
00071 * @note    The following actions are performed in order:
00072 *          -# Use user-defined CPBA or allocate new eTPU DATA RAM
00073 *          -# Write chan config registers and FM bits
00074 *          -# Write channel parameters
00075 *          -# Write HSR
00076 *          -# Set channel priority
00077 *
00078 * @param   *p_cam_instance - This is a pointer to the instance structure
00079 *            @ref cam_instance_t.
00080 * @param   *p_cam_config - This is a pointer to the structure of configuration
00081 *            parameters @ref cam_config_t.
00082 *
00083 * @return  Error codes that can be returned are:
00084 *          - @ref FS_ETPU_ERROR_MALLOC - eTPU DATA RAM memory allocation error
00085 *          - @ref FS_ETPU_ERROR_NONE - No error
00086 *
00087 * @warning This function does not configure the pins, only the eTPU channels.
00088 *******************************************************************************/
00089 uint32_t fs_etpu_cam_init(
00090   struct cam_instance_t *p_cam_instance,
00091   struct cam_config_t   *p_cam_config)
00092 {
00093   uint8_t  chan_num;
00094   uint8_t  priority;
00095   uint8_t  log_size;
00096   uint32_t *cpba_log;
00097   uint32_t *cpba;
00098 
00099   chan_num = p_cam_instance->chan_num;
00100   priority = p_cam_instance->priority;
00101   log_size = p_cam_instance->log_size;
00102   cpba_log = p_cam_instance->cpba_log;
00103   cpba     = p_cam_instance->cpba;
00104 
00105   /* Use user-defined CPBA or allocate new eTPU DATA RAM */
00106   if(cpba == 0)
00107   {
00108     cpba = fs_etpu_malloc(FS_ETPU_CAM_NUM_PARMS);
00109     if(cpba == 0)
00110     {
00111       return(FS_ETPU_ERROR_MALLOC);
00112     }
00113     else
00114     {
00115       p_cam_instance->cpba = cpba;
00116     }
00117   }
00118 
00119   /* Use user-defined log or allocate new eTPU DATA RAM */
00120   if(cpba_log == 0)
00121   {
00122     cpba_log = fs_etpu_malloc(log_size<<2);
00123     if(cpba_log == 0)
00124     {
00125       return(FS_ETPU_ERROR_MALLOC);
00126     }
00127     else
00128     {
00129       p_cam_instance->cpba_log = cpba_log;
00130     }
00131   }
00132 
00133   /* Write chan config registers and FM bits */
00134   eTPU->CHAN[chan_num].CR.R =
00135        (FS_ETPU_CAM_TABLE_SELECT << 24) +
00136        (FS_ETPU_CAM_FUNCTION_NUMBER << 16) +
00137        (((uint32_t)cpba - fs_etpu_data_ram_start) >> 3);
00138   eTPU->CHAN[chan_num].SCR.R = (uint32_t)p_cam_config->mode;
00139 
00140   /* Write channel parameters */
00141   *(cpba + ((FS_ETPU_CAM_OFFSET_LOG_SIZE  - 1)>>2)) = log_size;
00142   *(cpba + ((FS_ETPU_CAM_OFFSET_LOG_IDX   - 1)>>2)) = 0;
00143   *(cpba + ((FS_ETPU_CAM_OFFSET_LOG_COUNT - 1)>>2)) = 0;
00144   *(cpba + ((FS_ETPU_CAM_OFFSET_LOG       - 1)>>2)) = (uint32_t)cpba_log - fs_etpu_data_ram_start;
00145   *((uint8_t*)cpba + FS_ETPU_CAM_OFFSET_ERROR     ) = FS_ETPU_CAM_ERROR_NO;
00146 
00147   /* Write HSR */
00148   eTPU->CHAN[chan_num].HSRR.R = FS_ETPU_CAM_HSR_INIT;
00149 
00150   /* Set channel priority */
00151   fs_etpu_enable(chan_num, priority);
00152 
00153   return(FS_ETPU_ERROR_NONE);
00154 }
00155 
00156 /*******************************************************************************
00157 * FUNCTION: fs_etpu_cam_config
00158 ****************************************************************************//*!
00159 * @brief   This function changes the CAM configuration.
00160 *
00161 * @note    The following actions are performed in order:
00162 *          -# Write FM bits
00163 *
00164 * @param   *p_cam_instance - This is a pointer to the instance structure
00165 *            @ref cam_instance_t.
00166 * @param   *p_cam_config - This is a pointer to the structure of configuration
00167 *            parameters @ref cam_config_t.
00168 *
00169 * @return  Error codes that can be returned are:
00170 *          - @ref FS_ETPU_ERROR_NONE - No error
00171 *
00172 *******************************************************************************/
00173 uint32_t fs_etpu_cam_config(
00174   struct cam_instance_t *p_cam_instance,
00175   struct cam_config_t   *p_cam_config)
00176 {
00177   /* Write FM bits */
00178   eTPU->CHAN[p_cam_instance->chan_num].SCR.R = (uint32_t)p_cam_config->mode;
00179 
00180   return(FS_ETPU_ERROR_NONE);
00181 }
00182 
00183 /*******************************************************************************
00184 * FUNCTION: fs_etpu_cam_get_states
00185 ****************************************************************************//*!
00186 * @brief   This function reads state parameter values of the CAM function.
00187 *
00188 * @note    The following actions are performed in order:
00189 *          -# Read output parameter values from eTPU DATA RAM
00190 *          -# Clear CAM error flags in eTPU DATA RAM
00191 *
00192 * @param   *p_cam_instance - This is a pointer to the instance structure
00193 *            @ref cam_instance_t.
00194 * @param   *p_cam_states - This is a pointer to the structure of states
00195 *            @ref cam_states_t.
00196 *
00197 * @return  Error codes that can be returned are:
00198 *          - @ref FS_ETPU_ERROR_NONE - No error
00199 *
00200 *******************************************************************************/
00201 uint32_t fs_etpu_cam_get_states(
00202   struct cam_instance_t *p_cam_instance,
00203   struct cam_states_t   *p_cam_states)
00204 {
00205   uint32_t *cpba;
00206 
00207   cpba = p_cam_instance->cpba;
00208 
00209   /* Read channel parameters */
00210   p_cam_states->log_count = *(cpba + ((FS_ETPU_CAM_OFFSET_LOG_COUNT - 1)>>2));
00211   p_cam_states->log_idx   = *(cpba + ((FS_ETPU_CAM_OFFSET_LOG_IDX   - 1)>>2));
00212   p_cam_states->error     = *((uint8_t*)cpba + FS_ETPU_CAM_OFFSET_ERROR);
00213 
00214   /* Clear CAM error flags */
00215   *((uint8_t*)cpba + FS_ETPU_CAM_OFFSET_ERROR) = 0;
00216 
00217   return(FS_ETPU_ERROR_NONE);
00218 }
00219 
00220 /*******************************************************************************
00221 * FUNCTION: fs_etpu_cam_copy_log
00222 ****************************************************************************//*!
00223 * @brief   This function copies the CAM log into another array in RAM.
00224 *
00225 * @note    The following actions are performed in order:
00226 *          -# Copy out data from eTPU DATA RAM.
00227 *
00228 * @param   *p_cam_instance - This is a pointer to the instance structure
00229 *            @ref cam_instance_t.
00230 * @param   *p_cam_log - This is a pointer where the Cam log will be copied to.
00231 *            The Cam log is an array of input transitions captured. Each item is
00232 *            a 32-bit word which includes:
00233 *            - transition polarity (0-falling, 1-rising) in upper 8 bits
00234 *            - transition TCR2 angle in lower 24 bits.
00235 *            The amount of data copied in bytes is 4 * p_cam_instance->log_size.
00236 *
00237 * @return  A pointer to a memory location just after the copied data.
00238 *
00239 *******************************************************************************/
00240 uint32_t *fs_etpu_cam_copy_log(
00241   struct cam_instance_t *p_cam_instance,
00242                uint32_t *p_cam_log)
00243 {
00244   uint32_t *dest;
00245   uint32_t *source;
00246   uint8_t  size;
00247 
00248   dest   = p_cam_log;
00249   source = p_cam_instance->cpba_log;
00250   size   = p_cam_instance->log_size;
00251 
00252   while(size--)
00253   {
00254     *dest++ = *source++;
00255   }
00256 
00257   return(dest);
00258 }
00259 
00260 /*******************************************************************************
00261 * FUNCTION: fs_etpu_cam_reset_log
00262 ****************************************************************************//*!
00263 * @brief   This function resets the index to the CAM log so that the next
00264 *          detected transition is logged to the first position.
00265 *          Before that, the internal log_count variable is set to the number
00266 *          of logged transitions before the reset.
00267 *          If this number is 0 the channel interrupt is raised together with
00268 *          setting the error FS_ETPU_CAM_ERROR_ZERO_TRANS.
00269 *
00270 * @note    The following actions are performed in order:
00271 *          -# Check HSR is 0
00272 *          -# Write HSR
00273 *
00274 * @param   *p_cam_instance - This is a pointer to the instance structure
00275 *            @ref cam_instance_t.
00276 *
00277 * @return  Error codes that can be returned are:
00278 *          - @ref FS_ETPU_ERROR_NONE - No error.
00279 *          - @ref FS_ETPU_ERROR_TIMING - The reset was not done because there is
00280 *              a HSR pending on the eTPU channel.
00281 *              It can be caused by a call of another API function which writes
00282 *              the HSR to the same channel shortly before this function call.
00283 *              Try to repeat the function call several microseconds later.
00284 *
00285 *******************************************************************************/
00286 uint32_t fs_etpu_cam_reset_log(
00287   struct cam_instance_t *p_cam_instance)
00288 {
00289   /* Check there is no pending HSR */
00290   if(eTPU->CHAN[p_cam_instance->chan_num].HSRR.R != 0)
00291   {
00292     return(FS_ETPU_ERROR_TIMING);
00293   }
00294   else
00295   {
00296     /* Write HSR to run RESET on eTPU */
00297     eTPU->CHAN[p_cam_instance->chan_num].HSRR.R = FS_ETPU_CAM_HSR_RESET;
00298 
00299     return(FS_ETPU_ERROR_NONE);
00300   }
00301 }
00302 
00303 
00304 /*******************************************************************************
00305  *
00306  * Copyright:
00307  *  Freescale Semiconductor, INC. All Rights Reserved.
00308  *  You are hereby granted a copyright license to use, modify, and
00309  *  distribute the SOFTWARE so long as this entire notice is
00310  *  retained without alteration in any modified and/or redistributed
00311  *  versions, and that such modified versions are clearly identified
00312  *  as such. No licenses are granted by implication, estoppel or
00313  *  otherwise under any patents or trademarks of Freescale
00314  *  Semiconductor, Inc. This software is provided on an "AS IS"
00315  *  basis and without warranty.
00316  *
00317  *  To the maximum extent permitted by applicable law, Freescale
00318  *  Semiconductor DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
00319  *  INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
00320  *  PARTICULAR PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH
00321  *  REGARD TO THE SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
00322  *  AND ANY ACCOMPANYING WRITTEN MATERIALS.
00323  *
00324  *  To the maximum extent permitted by applicable law, IN NO EVENT
00325  *  SHALL Freescale Semiconductor BE LIABLE FOR ANY DAMAGES WHATSOEVER
00326  *  (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00327  *  BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER
00328  *  PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.
00329  *
00330  *  Freescale Semiconductor assumes no responsibility for the
00331  *  maintenance and support of this software
00332  ******************************************************************************/
00333 /*******************************************************************************
00334  *
00335  * REVISION HISTORY:
00336  *
00337  * FILE OWNER: Milan Brejl [r54529]
00338  *
00339  * Revision 1.0  2014/03/16  r54529
00340  * Minor comment and formating improvements.
00341  * Ready for eTPU Engine Control Library release 1.0.
00342  *
00343  * Revision 0.2  2014/01/23  r54529
00344  * fs_etpu_cam_reset_log() added.
00345  *
00346  * Revision 0.1  2012/05/17  r54529
00347  * Initial version of file.
00348  ******************************************************************************/