tpu_tsm_ex1.c

来自「mpc564 时钟中断 时钟中断」· C语言 代码 · 共 262 行

C
262
字号
/**************************************************************************/
/* FILE NAME: tpu_tsm_ex1.c                   COPYRIGHT (c) MOTOROLA 2002 */
/* VERSION: 1.0                                   All Rights Reserved     */
/*                                                                        */
/* DESCRIPTION: This sample program shows a simple example of a program   */
/* that initializes the TSM function and updates a desired position       */
/* varible from the CPU. The position in the current position moves most  */
/* efficiently to the new desired position. The table of byte variables   */
/* establishes the rate of speed. Advancing through the next byte step in */
/* the table will accelerate the speed of the motor. (ie. Table Stepper   */
/* Motor--TSM).                                                           */
/* The example uses the TPU A on a master channel and up to three         */
/* consecutive channels. The master channel for this example is 0. With   */
/* channels 1, 2, and 3 as the table parameter channels.                  */
/* The program is targeted for the MPC555 but should work on any MPC500   */
/* device with a TPU. For other devices the setup routines will also need */
/* to be changed.                                                         */
/*========================================================================*/
/* HISTORY           ORIGINAL AUTHOR: Jeff Loeliger                       */
/* REV      AUTHOR      DATE       DESCRIPTION OF CHANGE                  */
/* ---   -----------  ---------    ---------------------                  */
/* 1.0   J. Loeliger  03/Aug/02    Initial version of function.           */
/* 1.1   G. Jackson   12/Aug/02    Convert to TSM example.                */
/**************************************************************************/

#include "mpc565.h"     /* Define all of the MPC555 registers, this needs to */
                        /* changed if other MPC500 devices are used.         */
#include "mpc500.c"     /* Configuration routines for MPC555 EVB, will need */
                        /* to be changed if other hardware is used.         */
#include "mpc500_util.h"    /* Utility routines for using MPC500 devices    */
#include "tpu_tsm.h"    /* TPU TSM functions */

/* Prototypes for functions of example program */
void tpu_tsm_int_config(struct TPU3_tag *tpu, UINT8 channel, UINT8 level);
void Ext_Isr(void);
UINT32 tpu_tsm_sip_int_lvl(int int_level);

/* Initialization parameters -- redefine these parameters for a      */
/*                              second parallel TSM initialization.  */
UINT8 mas_channel = 13;  /* master channel designation, changes once here. */
UINT8 int_level = 2;     /* Set global interrupt level here.               */
UINT8 num_channel = 4;   /* Number of channels.                            */
UINT8 table_size = 24;   /* Number of bytes/2 in the parameter table.      */
INT16 start_position = 0x0010; /* Initial value for desired_ & current_position */
UINT16 table_siz_index = 0x1C00; /* Initial value for table size(1C) and index(00) */
UINT8  flag = 0;           /* Interrupt test flag.                        */

     struct TPU3_tag *tpua = &TPU_A;   /* pointer for TPU routines */

void main ()
{
     int x;  /* Just an integer to hold a value  */
  UINT16 dp_val; /* Value of desired position for program control. */
  UINT16 cp_val; /* Value of current position for program control. */

/* An example of a parameter table is shown below. Change this table */
/* to meet your requirements. Duplicate this table with another      */
/* name for a second TSM function (third, etc.), if different.       */

static UINT16 table[] = {0xF7FF, 0xE7F0, 0xD7E0, 0xC7D0,
                         0xB7C0, 0xA7B0, 0x97A0, 0x8790,
                         0x7780, 0x6770, 0x5760, 0x4750,
                         0x3740, 0x2730, 0x1720, 0x0F10,
                         0x0E0F, 0x0C0D, 0x0A0B, 0x0809,
                         0x0607, 0x0405, 0x0203, 0x0001};

/* Hardware Setup -- machine settings (watchdog, timers, speed, etc.) */
     setup_mpc500(40);       /*Setup device and programm PLL to 40MHz*/	


    /* Initialize Table Stepper Motor function with:         */
    /*    -Input signal on TPU A,                            */
    /*    -master_channel = mas_channel                      */
    /*    -Schedule as high priority in the TPU              */
    /*    -Inital desired position of 0x0010,                */
    /*    -Inital current position of 0x0010,                */
    /*    -Table Size of 0x18 (d24), Table Index of 0x00,    */
    /*    -Slew Period of 0x2000 (shift to 0x4000),  S = 0,  */
    /*    -Start Period of 0x6800 (shift to 0xD000), A = 1,  */
    /*    -Pin Sequence of 0xE0E0.                           */ 
    /*    -Number of channels = 4.                           */
    /*    -*table -- start address of parameter table.       */
    /*    -load_table_size -- number of table bytes to load. */

 tpu_tsm_init(tpua, mas_channel, TPU_PRIORITY_HIGH, start_position, 
              table_siz_index, 0x4000, 0xD001, 0xE0E0, num_channel, 
              table, table_size);

/* Enable interrupts; set the interrupt level in the TPU CIER   */
     tpu_tsm_int_config(tpua, mas_channel, int_level);

/**************************************************/
/*    Generate a new desired position.            */
/**************************************************/ 

/* Designate which tpu (A), the master channel = mas_channel,        */
/*      and the new desired location value (0x3000)                  */
/* NOTE: The Master Channel cannot change from the init designation! */
    dp_val = 0x3000;

      tpu_tsm_mov(tpua, mas_channel, dp_val); 

/**************************************************/
/* Make changes after an interrupt.               */
/**************************************************/

   while (!flag ) { /* Interrupt routine will set this flag */
/* Do other productive work while waiting for move to complete */ 
       x=8;
    };

/* Interrupt has occured -- continue:  */
/*  Below are examples of control logic used in a TSM function. */
/*  Adapt this to your specific needs and controls.             */
     dp_val = tpu_tsm_rd_dp(tpua, mas_channel);
     cp_val = tpu_tsm_rd_cp(tpua, mas_channel);
     if(dp_val = cp_val) { /* poll to see if current position has         */
                           /* reached desired position. Set new position. */
/* When return from interrupt, set a new desired location */
        if(cp_val > 2000) {
           dp_val -= 0x1000;
           tpu_tsm_mov(tpua, mas_channel, dp_val);
          }
        else {
               dp_val += 0x0500;
               tpu_tsm_mov(tpua, mas_channel, dp_val);
             }
       }


   while(1){
      /* Hold at end of program */
       x=4;
     };
}          /* End of main */

/*******************************************************************************
FUNCTION      : Ext_Isr
PURPOSE       : Interrupt call at interrupt request.
INPUTS NOTES  : This function has 0 parameters:
                 *tpu - This is a pointer to the TPU3 module to use. It is of
                         type TPU3_tag which is defined in m_tpu3.h
                 level - The interrupt level (0 to 31).
GENERAL NOTES : This routine is org'ed at 0x500. Only level 2 is shown as an
                 example here.
*******************************************************************************/
/* Below is a Diab Data centric routine. Other compilers */
/*  may require a different structure for an external    */
/*  interrupt service request.                           */

void Ext_Isr()                            
{ 
  UINT16 mas_chan_cisr;                
       mas_chan_cisr = tpu_tsm_mas_chan_cier(mas_channel);
 /* Check that the level of interrupt matches the TPU, TSM .*/
       flag = (tpu_tsm_int_chk(tpua, mas_chan_cisr));  
  /* Force the clearance of the CISR register */
       tpu_tsm_cisr_clr(tpua, mas_chan_cisr);
  
}                                                       

/*******************************************************************************
FUNCTION      : tpu_tsm_int_config
PURPOSE       : Interrupt activities when TSM interrupt request occurs.
INPUTS NOTES  : This function has 2 parameters:
                 *tpu - This is a pointer to the TPU3 module to use. It is of
                         type TPU3_tag which is defined in m_tpu3.h
                 channel - This is the number of the master channel
                 level - The interrupt level (0 to 7).
GENERAL NOTES : Channel must be a TSM master channel. Level must be in the 
                 range of 0 to 31. The activities listed here are for purposes
                 of example and to use all of the functions once.
*******************************************************************************/

void tpu_tsm_int_config(struct TPU3_tag *tpu, UINT8 channel, UINT8 level) {
     /* Shut down CPU MSR interrupts */
   UINT16 cisr_chan;
   UINT32 sipend_int_level;

    asm (" mtspr EID, r0 ");  /*  MSR[EE] = 0; MSR[RI] = 1 */
  
    cisr_chan = tpu_tsm_mas_chan_cier(channel);
    sipend_int_level = tpu_tsm_sip_int_lvl(level);
    /* Clear current interrupt requests              */
  /* Force the clearance of the CISR register */
    tpu_tsm_cisr_clr(tpua, cisr_chan);

    /* Configure the TPU Interrupt Configuration Register (TICR)  */
    /*  CIRL=2, ILBS=0 for a level 2 interrupt.                   */    
    tpu_tsm_int_lev( tpu, level);

    /* Set interupt enable to master channel CIER = master channel */
    tpu_interrupt_enable( tpu, channel);

    /* Set the USIU mask to accept a level 2 interrupt */
    USIU.SIMASK.R = sipend_int_level;

  /* Force the clearance of the CISR register */
    tpu_tsm_cisr_clr(tpua, cisr_chan);

/* Set the CPU MSR to enable interrupts. EIE activates MSR[EE], MSR[RI] */
   asm(" mtspr EIE, r3");                /* NOTE: r3 is a moot register */
   };

/*******************************************************************************
FUNCTION      : tpu_tsm_sip_int_lvl
PURPOSE       : Convert interrupt level integer into USIU.SIPEND UINT32 value
INPUTS NOTES  : This function has 1 parameter:
                 level - The interrupt level (0 to 7) as encoded through the
                         32-bit SIPEND register.
GENERAL NOTES : master_chan integer input is returned as a UINT16 value
                 for the TPU CIER and CISR registers.
*******************************************************************************/

UINT32 tpu_tsm_sip_int_lvl(int int_level) {
    /* Convert integer input interrupt level  */
    /* to the 32-bit SIPEND encoding.         */

UINT32 sip_level = 0x00000000;
   if(int_level == 0)  { sip_level = 0x40000000; }
   if(int_level == 1)  { sip_level = 0x10000000; }
   if(int_level == 2)  { sip_level = 0x04000000; }
   if(int_level == 3)  { sip_level = 0x01000000; }
   if(int_level == 4)  { sip_level = 0x00400000; }
   if(int_level == 5)  { sip_level = 0x00100000; }
   if(int_level == 6)  { sip_level = 0x00040000; }
   if(int_level == 7)  { sip_level = 0x00010000; }

   return sip_level;
}



/*********************************************************************
 *
 * Copyright:
 *	MOTOROLA, INC. All Rights Reserved.  
 *  You are hereby granted a copyright license to use, modify, and
 *  distribute the SOFTWARE so long as this entire notice is
 *  retained without alteration in any modified and/or redistributed
 *  versions, and that such modified versions are clearly identified
 *  as such. No licenses are granted by implication, estoppel or
 *  otherwise under any patents or trademarks of Motorola, Inc. This 
 *  software is provided on an "AS IS" basis and without warranty.
 *
 *  To the maximum extent permitted by applicable law, MOTOROLA 
 *  DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, INCLUDING 
 *  IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
 *  PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH REGARD TO THE 
 *  SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) AND ANY 
 *  ACCOMPANYING WRITTEN MATERIALS.
 * 
 *  To the maximum extent permitted by applicable law, IN NO EVENT
 *  SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING 
 *  WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS 
 *  INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY
 *  LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.   
 * 
 *  Motorola assumes no responsibility for the maintenance and support
 *  of this software
 ********************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?