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

etpu_fuel.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_fuel.c
00010 *
00011 * @author  Milan Brejl [r54529]
00012 *
00013 * @version 1.0
00014 *
00015 * @date    17-Mar-2014
00016 *
00017 * @brief   This file contains API for using the eTPU function
00018 *          Fuel Port Injection (FUEL).
00019 *
00020 ****************************************************************************//*!
00021 *
00022 * @mainpage
00023 *
00024 * The eTPU FUEL APIs @ref etpu_fuel.c/.h includes API functions for eTPU
00025 * function Fuel Port Injection.
00026 *
00027 * Each instance of the FUEL eTPU function controls a single FUEL channel.
00028 *
00029 * There is a FUEL parameter tdc_angle, relative to which all angles are
00030 * defined. Positive angles precede the tdc_angle, negative angles come after.
00031 *
00032 * The CPU can control the FUEL operation using FUEL config variables
00033 * angle_normal_end, angle_stop, angle_offset_recalc,
00034 * injection_time, compensation_time, injection_time_minimum and
00035 * off_time_minimum.
00036 *
00037 * The CPU can update the amount of injected fuel during the running injection
00038 * using the function @ref fs_etpu_fuel_update_injection_time() which not only
00039 * sets the injection_time value for the next engine cycles, but also updates the
00040 * current injection - shorts the pulse, extends the pulse or generates an
00041 * additional pulse.
00042 *
00043 * In order to
00044 * - immediatelly disable injection generation, set injection_time to 0
00045 * - disable the injection generation from the next cycle, but finish the running
00046 *   injection pulse, use generation_diable configuration flag.
00047 *
00048 * The CPU can monitor the FUEL operation using FUEL state variables
00049 * injection_time_applied, injection_start_angle and error.
00050 * The reported error flags are:
00051 *   @ref FS_ETPU_FUEL_ERROR_STOP_ANGLE_APPLIED - a fuel injection pulse has
00052 *     been stopped and shorted by the stop_angle. Hence, the commanded
00053 *     injection_time and the injection_time_applied may differ.
00054 *   @ref FS_ETPU_FUEL_ERROR_MINIMUM_INJ_TIME_APPLIED - a fuel injection pulse,
00055 *     the main or an additional one, is shorter than the injection_time_minimum
00056 *     and hence not generated, skipped. The commanded injection_time and
00057 *     the injection_time_applied may differ.
00058 *
00059 * Channel interrupt is generated once every engine cycle, on the angle_stop.
00060 *
00061 *******************************************************************************/
00062 /*******************************************************************************
00063 * Includes
00064 *******************************************************************************/
00065 #include "etpu_fuel.h"     /* private header file */
00066 #include "etpu_util.h"    /* utility routines for working with the eTPU */
00067 
00068 /*******************************************************************************
00069 * Global variables
00070 *******************************************************************************/
00071 extern uint32_t fs_etpu_data_ram_start;
00072 extern uint32_t fs_etpu_data_ram_ext;
00073 
00074 /*******************************************************************************
00075 * FUNCTION: fs_etpu_fuel_init
00076 ****************************************************************************//*!
00077 * @brief   This function initializes eTPU channels to run FUEL function.
00078 *
00079 * @note    The following actions are performed in order:
00080 *          -# Use user-defined CPBA or allocate new eTPU DATA RAM
00081 *          -# Write chan config registers and FM bits
00082 *          -# Write channel parameters
00083 *          -# Write HSR
00084 *          -# Set channel priority
00085 *
00086 * @param   *p_fuel_instance - This is a pointer to the instance structure
00087 *            @ref fuel_instance_t.
00088 * @param   *p_fuel_config - This is a pointer to the structure of configuration
00089 *            parameters @ref fuel_config_t.
00090 *
00091 * @return  Error codes that can be returned are:
00092 *          - @ref FS_ETPU_ERROR_MALLOC - eTPU DATA RAM memory allocation error
00093 *          - @ref FS_ETPU_ERROR_NONE - No error
00094 *
00095 * @warning This function does not configure the pins, only the eTPU channels.
00096 *******************************************************************************/
00097 uint32_t fs_etpu_fuel_init(
00098   struct fuel_instance_t   *p_fuel_instance,
00099   struct fuel_config_t     *p_fuel_config)
00100 {
00101   uint8_t  chan_num;
00102   uint8_t  priority;
00103   uint32_t *cpba;
00104 
00105   chan_num = p_fuel_instance->chan_num;
00106   priority = p_fuel_instance->priority;
00107   cpba     = p_fuel_instance->cpba;
00108 
00109   /* Use user-defined CPBA or allocate new eTPU DATA RAM for chan. parameters */
00110   if(cpba == 0)
00111   {
00112     cpba = fs_etpu_malloc(FS_ETPU_FUEL_NUM_PARMS);
00113     if(cpba == 0)
00114     {
00115       return(FS_ETPU_ERROR_MALLOC);
00116     }
00117     else
00118     {
00119       p_fuel_instance->cpba = cpba;
00120     }
00121   }
00122 
00123   /* Write chan config registers and FM bits */
00124   eTPU->CHAN[chan_num].CR.R =
00125        (FS_ETPU_FUEL_TABLE_SELECT << 24) +
00126        (FS_ETPU_FUEL_FUNCTION_NUMBER << 16) +
00127        (((uint32_t)cpba - fs_etpu_data_ram_start) >> 3);
00128   eTPU->CHAN[chan_num].SCR.R = (uint32_t)p_fuel_instance->polarity;
00129 
00130   /* Write channel parameters */
00131   /* 24-bit */
00132   *(cpba + ((FS_ETPU_FUEL_OFFSET_TDC_ANGLE                 - 1)>>2)) = p_fuel_instance->tdc_angle;
00133   *(cpba + ((FS_ETPU_FUEL_OFFSET_TDC_ANGLE_ACTUAL          - 1)>>2)) = 0;
00134   *(cpba + ((FS_ETPU_FUEL_OFFSET_ANGLE_NORMAL_END          - 1)>>2)) = p_fuel_config->angle_normal_end;
00135   *(cpba + ((FS_ETPU_FUEL_OFFSET_ANGLE_STOP                - 1)>>2)) = p_fuel_config->angle_stop;
00136   *(cpba + ((FS_ETPU_FUEL_OFFSET_ANGLE_OFFSET_RECALC       - 1)>>2)) = p_fuel_config->angle_offset_recalc;
00137   *(cpba + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME            - 1)>>2)) = p_fuel_config->injection_time;
00138   *(cpba + ((FS_ETPU_FUEL_OFFSET_COMPENSATION_TIME         - 1)>>2)) = p_fuel_config->compensation_time;
00139   *(cpba + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME_MINIMUM    - 1)>>2)) = p_fuel_config->injection_time_minimum;
00140   *(cpba + ((FS_ETPU_FUEL_OFFSET_OFF_TIME_MINIMUM          - 1)>>2)) = p_fuel_config->off_time_minimum;
00141   *(cpba + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME_APPLIED    - 1)>>2)) = 0;
00142   *(cpba + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME_APPLIED_CPU- 1)>>2)) = 0;
00143   *(cpba + ((FS_ETPU_FUEL_OFFSET_INJECTION_START_ANGLE     - 1)>>2)) = 0;
00144   *(cpba + ((FS_ETPU_FUEL_OFFSET_INJECTION_START_ANGLE_CPU - 1)>>2)) = 0;
00145   *(cpba + ((FS_ETPU_FUEL_OFFSET_PULSE_START_TIME          - 1)>>2)) = 0;
00146   *(cpba + ((FS_ETPU_FUEL_OFFSET_PULSE_END_TIME            - 1)>>2)) = 0;
00147   /* 8-bit */
00148   *((uint8_t*)cpba + FS_ETPU_FUEL_OFFSET_ERROR) = 0;
00149   *((uint8_t*)cpba + FS_ETPU_FUEL_OFFSET_GENERATION_DISABLE ) = p_fuel_config->generation_disable;
00150 
00151   /* Write HSR */
00152   eTPU->CHAN[chan_num].HSRR.R = FS_ETPU_FUEL_HSR_INIT;
00153 
00154   /* Set channel priority */
00155   fs_etpu_enable(chan_num, priority);
00156 
00157   return(FS_ETPU_ERROR_NONE);
00158 }
00159 
00160 /*******************************************************************************
00161 * FUNCTION: fs_etpu_fuel_config
00162 ****************************************************************************//*!
00163 * @brief   This function changes the FUEL configuration.
00164 *
00165 * @warning The new injection_time is applied from the next fuel injection.
00166 *          Use @ref fs_etpu_fuel_update_injection_time() in order to update
00167 *          the current injection.
00168 *
00169 * @note    The following actions are performed in order:
00170 *          -# Write configuration parameter values to eTPU DATA RAM
00171 *
00172 * @param   *p_fuel_instance - This is a pointer to the instance structure
00173 *            @ref inj_instance_t.
00174 * @param   *p_fuel_config - This is a pointer to the structure of configuration
00175 *            parameters @ref inj_config_t.
00176 *
00177 * @return  Error codes that can be returned are:
00178 *          - @ref FS_ETPU_ERROR_NONE - No error.
00179 *
00180 *******************************************************************************/
00181 uint32_t fs_etpu_fuel_config(
00182   struct fuel_instance_t *p_fuel_instance,
00183   struct fuel_config_t   *p_fuel_config)
00184 {
00185   uint32_t *cpba;
00186   uint32_t *cpbae;
00187 
00188   cpba  = p_fuel_instance->cpba;
00189   cpbae = cpba + (0x4000 >> 2); /* sign-extended memory area */
00190 
00191   /* Write channel parameters */
00192   /* 24-bit - use cpbae to prevent from overwriting bits 31:24 */
00193   *(cpbae + ((FS_ETPU_FUEL_OFFSET_ANGLE_NORMAL_END       - 1)>>2)) = (uint24_t)p_fuel_config->angle_normal_end;
00194   *(cpbae + ((FS_ETPU_FUEL_OFFSET_ANGLE_STOP             - 1)>>2)) = (uint24_t)p_fuel_config->angle_stop;
00195   *(cpbae + ((FS_ETPU_FUEL_OFFSET_ANGLE_OFFSET_RECALC    - 1)>>2)) = (uint24_t)p_fuel_config->angle_offset_recalc;
00196   *(cpbae + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME         - 1)>>2)) = p_fuel_config->injection_time;
00197   *(cpbae + ((FS_ETPU_FUEL_OFFSET_COMPENSATION_TIME      - 1)>>2)) = p_fuel_config->compensation_time;
00198   *(cpbae + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME_MINIMUM - 1)>>2)) = p_fuel_config->injection_time_minimum;
00199   *(cpbae + ((FS_ETPU_FUEL_OFFSET_OFF_TIME_MINIMUM       - 1)>>2)) = p_fuel_config->off_time_minimum;
00200   /* 8-bit */
00201   *((uint8_t*)cpba + FS_ETPU_FUEL_OFFSET_GENERATION_DISABLE) = p_fuel_config->generation_disable;
00202 
00203   return(FS_ETPU_ERROR_NONE);
00204 }
00205 
00206 /*******************************************************************************
00207 * FUNCTION: fs_etpu_fuel_update_injection_time
00208 ****************************************************************************//*!
00209 * @brief   This function updates the FUEL injection_time.
00210 *
00211 * @note    The following actions are performed in order:
00212 *          -# Check there is no pending HSR
00213 *          -# Write parameter value to eTPU DATA RAM
00214 *          -# Write HSR
00215 *
00216 * @param   *p_fuel_instance - This is a pointer to the instance structure
00217 *            @ref inj_instance_t.
00218 * @param   *p_fuel_config - This is a pointer to the structure of configuration
00219 *            parameters @ref inj_config_t.
00220 *
00221 * @return  Error codes that can be returned are:
00222 *          - @ref FS_ETPU_ERROR_NONE - No error.
00223 *          - @ref FS_ETPU_ERROR_TIMING - The mode was not set because there is
00224 *              a HSR pending on the eTPU channel.
00225 *              It can be caused by a call of another API function which writes
00226 *              the HSR to the same channel shortly before this function call.
00227 *              Try to repeat the function call several microseconds later.
00228 *
00229 *******************************************************************************/
00230 uint32_t fs_etpu_fuel_update_injection_time(
00231   struct fuel_instance_t *p_fuel_instance,
00232   struct fuel_config_t   *p_fuel_config)
00233 {
00234   uint32_t *cpba;
00235   uint32_t *cpbae;
00236 
00237   /* Check there is no pending HSR */
00238   if(eTPU->CHAN[p_fuel_instance->chan_num].HSRR.R != 0)
00239   {
00240     return(FS_ETPU_ERROR_TIMING);
00241   }
00242   else
00243   {
00244     cpba  = p_fuel_instance->cpba;
00245     cpbae = cpba + (0x4000 >> 2); /* sign-extended memory area */
00246 
00247     /* Write channel parameter */
00248     /* 24-bit - use cpbae to prevent from overwriting bits 31:24 */
00249     *(cpbae + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME - 1)>>2)) = p_fuel_config->injection_time;
00250 
00251     /* Write HSR to run UPDATE on eTPU */
00252     eTPU->CHAN[p_fuel_instance->chan_num].HSRR.R = FS_ETPU_FUEL_HSR_UPDATE;
00253 
00254     return(FS_ETPU_ERROR_NONE);
00255   }
00256 }
00257 
00258 /*******************************************************************************
00259 * FUNCTION: fs_etpu_fuel_get_states
00260 ****************************************************************************//*!
00261 * @brief   This function reads FUEL state variables, including error flags,
00262 *          and clears the eTPU error after reading.
00263 *
00264 * @note    The following actions are performed in order:
00265 *          -# Read state parameter values from eTPU DATA RAM
00266 *          -# Clear FUEL error
00267 *
00268 * @param   *p_fuel_instance - This is a pointer to the instance structure
00269 *            @ref p_fuel_instance.
00270 * @param   *p_fuel_states - This is a pointer to the return structure of states
00271 *            @ref p_fuel_states.
00272 *
00273 * @return  Error codes that can be returned are:
00274 *          - @ref FS_ETPU_ERROR_NONE - No error
00275 *
00276 *******************************************************************************/
00277 uint32_t fs_etpu_fuel_get_states(
00278   struct fuel_instance_t *p_fuel_instance,
00279   struct fuel_states_t   *p_fuel_states)
00280 {
00281   uint32_t *cpba;
00282   uint32_t *cpbae;
00283 
00284   cpba  = p_fuel_instance->cpba;
00285   cpbae = cpba + (0x4000 >> 2); /* sign-extended memory area */
00286 
00287   /* Read FUEL channel parameters */
00288   p_fuel_states->injection_time_applied = *(cpbae + ((FS_ETPU_FUEL_OFFSET_INJECTION_TIME_APPLIED_CPU - 1)>>2));
00289   p_fuel_states->injection_start_angle  = *(cpbae + ((FS_ETPU_FUEL_OFFSET_INJECTION_START_ANGLE_CPU  - 1)>>2));
00290   p_fuel_states->error                 |= *((uint8_t*)cpba + FS_ETPU_FUEL_OFFSET_ERROR);
00291   /* Clear FUEL error */
00292   *((uint8_t*)cpba + FS_ETPU_FUEL_OFFSET_ERROR) = 0;
00293 
00294   return(FS_ETPU_ERROR_NONE);
00295 }
00296 
00297 /*******************************************************************************
00298  *
00299  * Copyright:
00300  *  Freescale Semiconductor, INC. All Rights Reserved.
00301  *  You are hereby granted a copyright license to use, modify, and
00302  *  distribute the SOFTWARE so long as this entire notice is
00303  *  retained without alteration in any modified and/or redistributed
00304  *  versions, and that such modified versions are clearly identified
00305  *  as such. No licenses are granted by implication, estoppel or
00306  *  otherwise under any patents or trademarks of Freescale
00307  *  Semiconductor, Inc. This software is provided on an "AS IS"
00308  *  basis and without warranty.
00309  *
00310  *  To the maximum extent permitted by applicable law, Freescale
00311  *  Semiconductor DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
00312  *  INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
00313  *  PARTICULAR PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH
00314  *  REGARD TO THE SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
00315  *  AND ANY ACCOMPANYING WRITTEN MATERIALS.
00316  *
00317  *  To the maximum extent permitted by applicable law, IN NO EVENT
00318  *  SHALL Freescale Semiconductor BE LIABLE FOR ANY DAMAGES WHATSOEVER
00319  *  (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00320  *  BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER
00321  *  PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.
00322  *
00323  *  Freescale Semiconductor assumes no responsibility for the
00324  *  maintenance and support of this software
00325  ******************************************************************************/
00326 /*******************************************************************************
00327  *
00328  * REVISION HISTORY:
00329  *
00330  * FILE OWNER: Milan Brejl [r54529]
00331  *
00332  * Revision 1.0  2014/03/17  r54529
00333  * Minor comment and formating improvements.
00334  * Ready for eTPU Engine Control Library release 1.0.
00335  *
00336  * Revision 0.2  2013/09/05  r54529
00337  * Addition of generation_disable.
00338  *
00339  * Revision 0.1  2013/08/28  r54529
00340  * Initial version of file.
00341  *
00342  ******************************************************************************/