jafrey wrote:I guess I'll have to read up on the eTPU from ST and Freescale. I'm actually interested in using it for the injector drivers and engine speed and offloading all that processing from the main CPU. (Like the big boys do in the automotive world).
Here is a quick example for and very simple 4-cyclinder engine (far from complete). I have not tried it with SPC5 studio yet, but will give it a shot when time permits.
This is a publically available function set for eTPU, there are many variations (most not public). This one covers 3-6 cylinder engines. The multi-spark, not sure why as it is far from common on induction type coils, especially coil near plug. As can be seen eTPU functions can be very robust and why anything having to do with an internal combustion engine (ICE), is a great fit for this MPU w/TPU.
Notes:
* TPU TCR2 must be set for angle.
* Angle in this function set is 0-71999 or 0-719.99 degrees (this is a 4-stroke).
* Time base is in uS (microseconds).
* Crank and cam inputs must be first 2 TPU input channels.
* Typical crank is 36 tooth, 35 teeth with 1 missing tooth as reference mark.
* Missing tooth is almost always before TDC (top dead center) for cylinder 1.
* Cam sensor to determine if it’s a compression or exhaust stroke.
* “Tooth Generator” is what it sounds like, a simulation output that goes to crank and cam inputs.
Code: Select all
/************************************************************************
* This file was generated by eTPU Graphical Configuration Tool
*
* DO NOT EDIT !!! DO NOT EDIT !!! DO NOT EDIT !!! DO NOT EDIT !!!
*
* DESCRIPTION:
* This file contains my_system_etpu_init and my_system_etpu_start
* functions and related items. The function initialize the eTPU
* global and channel settings, based on the user settings
* in the eTPU GCT, and runs the eTPU.
* A header file of the same name .h is also generated.
*
* Creation Date: 5/18/2014 1:00:54 AM
*
**************************************************************************/
/* general routines */
#include "etpu_util.h" /* Utility routines for working eTPU */
#include "mpc563m_vars.h" /* Processor dependent variables */
/* eTPU function set used */
#include "etpu/_etpu_set/etpu_set.h"
/* eTPU functions APIs */
#include "etpu/fuel/etpu_fuel.h" /* Fuel */
#include "etpu/spark/etpu_spark.h" /* Spark */
#include "etpu/toothgen/etpu_toothgen.h" /* ToothGen - Tooth Generator */
#include "etpu/engine_position/etpu_app_eng_pos.h" /* Engine Position (CAM and CRANK channels): main header file */
#include "etpu/ic/etpu_ic.h" /* IC */
/* our private header file (generated by eTPU GCT) */
#include "small_eng_etpu_gct.h"
/* External object declarations */
/* ... no external variables used */
/**************************************************************************
*
* Global Variables
*
**************************************************************************/
uint32_t *fs_free_param;
/**************************************************************************
*
* CPU and clock configuration:
*
* CPU = MPC5634M
* SYSCLK = 80 MHz
* Input_TCRCLK_A = OFF
* Input_TCRCLK_B = OFF
*
**************************************************************************/
uint32_t etpu_a_tcr1_freq = 40000000;
uint32_t etpu_a_tcr2_freq = 10000000;
uint32_t etpu_b_tcr1_freq = 0;
uint32_t etpu_b_tcr2_freq = 10000000;
/**************************************************************************
*
* etpu_config structure filled accodring to the user settings
*
**************************************************************************/
struct etpu_config_t my_etpu_config =
{
/* etpu_config.mcr */
FS_ETPU_VIS_OFF /* SCM not visible (VIS=0) */
| FS_ETPU_MISC_DISABLE /* SCM operation disabled (SCMMISEN=0) */,
/* etpu_config.misc */
FS_ETPU_MISC,
/* etpu_config.ecr_a */
FS_ETPU_ENTRY_TABLE_ADDR /* entry table base address = shifted FS_ETPU_ENTRY_TABLE */
| FS_ETPU_CHAN_FILTER_2SAMPLE /* channel filter mode = two-sample mode (CDFC=0) */
| FS_ETPU_ENGINE_DISABLE /* engine is stopped (MDIS=1) */
| FS_ETPU_FILTER_CLOCK_DIV2 /* channel filter clock = etpuclk div 2 (FPSCK=0) */,
/* etpu_config.tbcr_a */
FS_ETPU_ANGLE_MODE_ENABLE /* TCR2 works in angle mode (AM=1) */
| FS_ETPU_TCR2_PRESCALER(1) /* TCR2 prescaler = 1 (TCR2P='1-1') */
| FS_ETPU_TCR1_PRESCALER(1) /* TCR1 prescaler = 1 (TCR1P='1-1') */
| FS_ETPU_TCRCLK_MODE_INTEGRATION /* TCRCLK signal is filtered using integration mode (TCRCF=1x) */
| FS_ETPU_TCR2CTL_GATEDDIV8 /* TCR2 source = etpuclk div 8, gated by TCRCLK (TCR2CTL=0) */
| FS_ETPU_TCRCLK_INPUT_CHANCLOCK /* TCRCLK signal is filtered with filter clock = channel filter clock (TCRCF=x1) */
| FS_ETPU_TCR1CTL_DIV2 /* TCR1 source = etpuclk div 2 (TCR1CTL=2) */,
/* etpu_config.stacr_a */
FS_ETPU_TCR1_STAC_SERVER /* TCR1 resource operates as server (RSC1=1) */
| FS_ETPU_TCR2_STAC_SERVER /* TCR2 resource operates as server (RSC2=1) */
| FS_ETPU_TCR1_STAC_DISABLE /* TCR1 operation on STAC bus disabled (REN1=0) */
| FS_ETPU_TCR2_STAC_DISABLE /* TCR2 operation on STAC bus disabled (REN2=0) */
| FS_ETPU_TCR1_STAC_SRVSLOT(0) /* TCR1 resource server slot = 0 (SRV1=0) */
| FS_ETPU_TCR2_STAC_SRVSLOT(0) /* TCR2 resource server slot = 0 (SRV2=0) */,
/* etpu_config.ecr_b */
0,
/* etpu_config.tbcr_b */
0,
/* etpu_config.stacr_b */
0
};
/**************************************************************************
*
* FUNCTION : my_system_etpu_init
*
* PURPOSE : Initializes the eTPU settings and channel settings, based
* on the user settings in the eTPU GCT:
*
* 1. Initialize global setting using fs_etpu_init function
* and the my_etpu_config structure.
* 2. Initialize channel setting using channel function APIs
* or standard eTPU routines.
*
* RETURNS : If the initialization of all channel functions passed OK,
* the return value is 0. If the initialization of a channel
* failed, the returned value is a number of the channel that
* failed to initialized increased by 1.
*
**************************************************************************/
int32_t my_system_etpu_init()
{
int32_t err_code;
/*
* Initialization of eTPU global settings
*/
fs_etpu_init( my_etpu_config, (uint32_t *)etpu_code, sizeof(etpu_code),
(uint32_t *)etpu_globals, sizeof(etpu_globals));
/*
* Initialization of eTPU channel settings
*/
/*
* eTPU API Function initialization: 'Fuel'
*/
err_code = fs_etpu_fuel_init_4cylinders (FUEL0_FUEL_1, /* engine: A; channel: 2 */
FUEL0_FUEL_2, /* engine: A; channel: 3 */
FUEL0_FUEL_3, /* engine: A; channel: 4 */
FUEL0_FUEL_4, /* engine: A; channel: 5 */
0, /* cam_chan: 0 */
FS_ETPU_PRIORITY_LOW, /* priority: Low */
FS_ETPU_FUEL_PULSE_HIGH, /* polarity: Pulse active high */
0, /* cylinder_offset_angle_1: 0 */
0, /* cylinder_offset_angle_2: 0 */
0, /* cylinder_offset_angle_3: 0 */
0, /* cylinder_offset_angle_4: 0 */
0, /* drop_dead_angle: 0 */
0, /* injection_normal_end_angle: 0 */
0, /* recalculation_offset_angle: 0 */
0, /* injection_time_us_1: 0 */
0, /* injection_time_us_2: 0 */
0, /* injection_time_us_3: 0 */
0, /* injection_time_us_4: 0 */
0, /* compensation_time_us: 0 */
0, /* minimum_injection_time_us: 0 */
0); /* minimum_off_time_us: 0 */
if (err_code != 0)
return ((FUEL0_FUEL_1) + 1);
/*
* eTPU API Function initialization: 'Spark'
*/
err_code = fs_etpu_spark_init_4cylinders (SPARK0_SPARK_1, /* engine: A; channel: 6 */
SPARK0_SPARK_2, /* engine: A; channel: 7 */
SPARK0_SPARK_3, /* engine: A; channel: 8 */
SPARK0_SPARK_4, /* engine: A; channel: 9 */
0, /* cam_chan: 0 */
17999, /* cyl_offset_angle_1: 17999 */
35999, /* cyl_offset_angle_2: 35999 */
53999, /* cyl_offset_angle_3: 53999 */
71999, /* cyl_offset_angle_4: 71999 */
FS_ETPU_PRIORITY_MIDDLE, /* priority: Middle */
FS_ETPU_SPARK_ACTIVE_HIGH, /* polarity: FS_ETPU_SPARK_ACTIVE_HIGH */
3000, /* min_dwell_time: 3000 */
10000, /* max_dwell_time: 10000 */
0, /* multi_on_time: 0 */
0, /* multi_off_time: 0 */
0, /* multi_num_pulses: 0 */
0, /* recalc_offset_angle: 0 */
0, /* init_dwell_time_1: 0 */
0, /* init_dwell_time_2: 0 */
0, /* init_end_angle_1: 0 */
0); /* init_end_angle_2: 0 */
if (err_code != 0)
return ((SPARK0_SPARK_1) + 1);
/*
* eTPU API Function initialization: 'ToothGen - Tooth Generator'
*/
err_code = fs_etpu_toothgen_init (TOOTHGEN0_CRANK, /* engine: A; channel: 14 */
TOOTHGEN0_CAM, /* engine: A; channel: 15 */
FS_ETPU_PRIORITY_LOW, /* priority: Low */
35, /* crank_number_of_physical_teeth: 35 */
1, /* crank_number_of_missing_teeth: 1 */
0x000FFF, /* tooth_duty_cycle: 0x000FFF */
36, /* tooth_number: 36 */
2500, /* engine_speed_rpm: 2500 */
40000000, /* tcr1_timebase_freq: 40000000 */
3, /* cam_start: 3 */
4); /* cam_stop: 4 */
if (err_code != 0)
return ((TOOTHGEN0_CRANK) + 1);
/*
* eTPU API Function initialization: 'Engine Position (CAM and CRANK channels)'
*/
err_code = fs_etpu_app_eng_pos_init (APP_ENG_POS0_CAM, /* engine: A; channel: 0 */
FS_ETPU_CAM_PRIORITY_HIGH, /* cam_priority: High */
FS_ETPU_CAM_FM0_RISING_EDGE, /* cam_edge_polarity: FS_ETPU_CAM_FM0_RISING_EDGE */
0, /* cam_angle_window_start: 0 */
0, /* cam_angle_window_width: 0 */
APP_ENG_POS0_CRANK, /* engine: A; channel: 1 */
FS_ETPU_CRANK_PRIORITY_HIGH, /* crank_priority: High */
FS_ETPU_CRANK_FM0_RISING_EDGE, /* crank_edge_polarity: FS_ETPU_CRANK_FM0_RISING_EDGE */
0, /* crank_number_of_physical_teeth: 0 */
0, /* crank_number_of_missing_teeth: 0 */
0, /* crank_blank_tooth_count: 0 */
0, /* crank_tcr2_ticks_per_tooth: 0 */
0, /* crank_windowing_ratio_normal: 0 */
0, /* crank_windowing_ratio_after_gap: 0 */
0, /* crank_windowing_ratio_across_gap: 0 */
0, /* crank_windowing_ratio_timeout: 0 */
0, /* crank_gap_ratio: 0 */
0, /* crank_blank_time_ms: 0 */
0, /* crank_first_tooth_timeout_us: 0 */
0, /* crank_link_1: 0 */
0, /* crank_link_2: 0 */
0, /* crank_link_3: 0 */
0, /* crank_link_4: 0 */
0); /* tcr1_timebase_freq: 0 */
if (err_code != 0)
return ((APP_ENG_POS0_CAM) + 1);
/*
* eTPU API Function initialization: 'IC'
*/
err_code = fs_etpu_ic_init (IC0_CHANNEL, /* engine: A; channel: 10 */
FS_ETPU_PRIORITY_MIDDLE, /* priority: Middle */
ETPU_IC_MODE_CONT, /* mode: ETPU_IC_MODE_CONT */
FS_ETPU_TCR1, /* timebase: TCR1 */
ETPU_IC_RISING_EDGE, /* edge: ETPU_IC_RISING_EDGE */
4); /* max_count: 4 */
if (err_code != 0)
return ((IC0_CHANNEL) + 1);
return(0);
}
/**************************************************************************
*
* FUNCTION : my_system_etpu_start
*
* PURPOSE : Initializes the eTPU settings and channel settings, based
* on the user settings in the eTPU GCT:
*
* 1. Initialize
* - Interrupt Enable
* - DMA Enable
* - Output Disable
* options of each channel.
* 2. Run eTPU using Global Timebase Enable (GTBE) bit.
*
**************************************************************************/
void my_system_etpu_start()
{
/*
* Initialization of Interrupt Enable, DMA Enable
* and Output Disable channel options
*/
uint8_t i;
uint32_t odis_a;
uint32_t opol_a;
eTPU->CIER_A.R = ETPU_CIE_A;
eTPU->CDTRER_A.R = ETPU_DTRE_A;
odis_a = ETPU_ODIS_A;
opol_a = ETPU_OPOL_A;
for(i=0; i<32; i++) {
eTPU->CHAN[i].CR.B.ODIS = odis_a & 0x00000001;
eTPU->CHAN[i].CR.B.OPOL = opol_a & 0x00000001;
odis_a >>= 1;
opol_a >>= 1;
}
/*
* Synchronous start of all TCR time bases
*/
fs_timer_start();
}
/**************************************************************************
*
* Private GCT-generated data may follow this comment
*
**************************************************************************/
Code: Select all
/************************************************************************
* This file was generated by eTPU Graphical Configuration Tool
*
* DO NOT EDIT !!! DO NOT EDIT !!! DO NOT EDIT !!! DO NOT EDIT !!!
*
* DESCRIPTION:
* This file is generated together with the same name .c file.
* It contains function prototypes and useful defines.
*
* Creation Date: 5/18/2014 1:00:54 AM
*
**************************************************************************/
/**************************************************************************
* general macros
**************************************************************************/
#define FS_ETPU_TCR1_PRESCALER(x) (((x)-1) & 0xFF)
#define FS_ETPU_TCR2_PRESCALER(x) ((((x)-1) & 0x3F)<<16)
#define FS_ETPU_TCR1_STAC_SRVSLOT(x) (((x) & 0xF)<<16)
#define FS_ETPU_TCR2_STAC_SRVSLOT(x) ((x) & 0xF)
#define FS_ETPU_CHANNEL_FUNCTION(x) ((x) & 0x1F)
#define FS_ETPU_CHANNEL_PARAMS(x) ((x) & 0x7F)
#define FS_ETPU_FUNCTION_MODE(x) ((x) & 0x3)
#define FS_ETPU_ENTRY_TABLE_ADDR (((FS_ETPU_ENTRY_TABLE)>>11) & 0x1F)
#define FS_ETPU_FUNCTION_MODE_00 FS_ETPU_FUNCTION_MODE(0)
#define FS_ETPU_FUNCTION_MODE_01 FS_ETPU_FUNCTION_MODE(1)
#define FS_ETPU_FUNCTION_MODE_10 FS_ETPU_FUNCTION_MODE(2)
#define FS_ETPU_FUNCTION_MODE_11 FS_ETPU_FUNCTION_MODE(3)
#define FS_ETPU_HSR_0 (0)
#define FS_ETPU_HSR_1 (1)
#define FS_ETPU_HSR_2 (2)
#define FS_ETPU_HSR_3 (3)
#define FS_ETPU_HSR_4 (4)
#define FS_ETPU_HSR_5 (5)
#define FS_ETPU_HSR_6 (6)
#define FS_ETPU_HSR_7 (7)
#define ETPU_ENGINE_A_CHANNEL(x) (x)
#define ETPU_ENGINE_B_CHANNEL(x) ((x)+64)
#define ETPU_CHAN_NOT_USED (-1)
/**************************************************************************
* define functions to channels
**************************************************************************/
#define FUEL0_FUEL_1 ETPU_ENGINE_A_CHANNEL(2)
#define FUEL0_FUEL_2 ETPU_ENGINE_A_CHANNEL(3)
#define FUEL0_FUEL_3 ETPU_ENGINE_A_CHANNEL(4)
#define FUEL0_FUEL_4 ETPU_ENGINE_A_CHANNEL(5)
#define SPARK0_SPARK_1 ETPU_ENGINE_A_CHANNEL(6)
#define SPARK0_SPARK_2 ETPU_ENGINE_A_CHANNEL(7)
#define SPARK0_SPARK_3 ETPU_ENGINE_A_CHANNEL(8)
#define SPARK0_SPARK_4 ETPU_ENGINE_A_CHANNEL(9)
#define TOOTHGEN0_CRANK ETPU_ENGINE_A_CHANNEL(14)
#define TOOTHGEN0_CAM ETPU_ENGINE_A_CHANNEL(15)
#define APP_ENG_POS0_CAM ETPU_ENGINE_A_CHANNEL(0)
#define APP_ENG_POS0_CRANK ETPU_ENGINE_A_CHANNEL(1)
#define IC0_CHANNEL ETPU_ENGINE_A_CHANNEL(10)
/**************************************************************************
* Interrupt Enable, DMA Enable and Output Disable options
* filled accodring to the user settings
**************************************************************************/
#define ETPU_CIE_A 0x00000000
#define ETPU_DTRE_A 0x00000000
#define ETPU_ODIS_A 0x00003fff
#define ETPU_OPOL_A 0x00003ffc
/**************************************************************************
* function prototypes
**************************************************************************/
int32_t my_system_etpu_init ();
void my_system_etpu_start();