⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dp_user.c

📁 PROFIBUS SLAVE PROGRAMS PRO FIBUS SLAVE PROGRAMS
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************  Filename: dp_user.c  ****************************/
/* ========================================================================= */
/*                                                                           */
/* 0000  000   000  00000 0  000  0   0 0 0000                               */
/* 0   0 0  0 0   0 0     0 0   0 0   0 0 0   0                              */
/* 0   0 0  0 0   0 0     0 0     0   0 0 0   0      Einsteinstra遝 6        */
/* 0000  000  0   0 000   0 0     00000 0 0000       91074 Herzogenaurach    */
/* 0     00   0   0 0     0 0     0   0 0 0                                  */
/* 0     0 0  0   0 0     0 0   0 0   0 0 0          Tel: ++49-9132-744-200  */
/* 0     0  0  000  0     0  000  0   0 0 0    GmbH  Fax: ++49-9132-744-204  */
/*                                                                           */
/* ========================================================================= */
/*                                                                           */
/* Function:      Demo for PROFICHIP Extension Board AT89C5132               */
/*                This example simulates a profibus device                   */
/*                with 2 Byte Input and 2 Byte Output.                       */
/*                                                                           */
/* ------------------------------------------------------------------------- */
/*                                                                           */
/* Hardware requirements: ProfiChip Evaluation Board AT89C5132  (PA006101)   */
/*                        ProfiChip Evaluation Board VPC3+/C    (PA006103)   */
/*                                                                           */
/* ------------------------------------------------------------------------- */
/* memory:  0000H ... 7FFFH: RAM                                             */
/*          8000H ... 8FFFH: VPC3+                                           */
/*          9000H ... 9FFFH: Reserved                                        */
/*          A000H ... AFFFH: FPGA                                            */
/*          B000H ... BFFFH: RTC                                             */
/*          C000H ... CFFFH: LCD                                             */
/*          D000H ... DFFFH: I/O Port 0                                      */
/*          E000H ... EFFFH: I/O Port 1                                      */
/*          F000H ... FFFFH: I/O Port 2                                      */
/*                                                                           */
/* ------------------------------------------------------------------------- */
/*                                                                           */
/* Technical support:       P. Fredehorst                                    */
/*                          Tel. : ++49-9132/744-214                         */
/*                          Fax. :              -204                         */
/*                          eMail: pfredehorst@profichip.com                 */
/*                                                                           */
/*****************************************************************************/


/*****************************************************************************/
/* contents:

  - function prototypes
  - data structures
  - internal functions

*/
/*****************************************************************************/
/* include hierarchy */

#include "..\..\dp_inc\platform.h"
#include "..\..\dp_inc\dp_inc.h"

/*---------------------------------------------------------------------------*/
/* defines, structures                                                       */
/*---------------------------------------------------------------------------*/
// -- defines for user state
#define USER_STATE_CLEAR            ((UBYTE)0x00)
#define USER_STATE_RUN              ((UBYTE)0x01)
// -- defines for diagnostics
#define USER_TYPE_DPV0              ((UBYTE)0xFA)
#define USER_TYPE_PRM_OK            ((UBYTE)0xFB)
#define USER_TYPE_PRM_NOK           ((UBYTE)0xFC)
#define USER_TYPE_CFG_OK            ((UBYTE)0xFD)
#define USER_TYPE_CFG_NOK           ((UBYTE)0xFE)
#define USER_TYPE_APPL_RDY          ((UBYTE)0xFF)

typedef struct
{
    UBYTE                       state;
    UBYTE                       user_diag_active;
    UWORD                       old_diag;
    UBYTE                       user_diag[DIAG_BUFSIZE];
    UWORD                       event;
    UBYTE                       application_ready;
    CFG_STRUCT                  real_cfg;
    UBYTE                       input[2];
    UBYTE                       output[2];
}USER_STRUC;

/*---------------------------------------------------------------------------*/
/* global user data definitions                                              */
/*---------------------------------------------------------------------------*/
VPC3_STRUC_ERRCB                vpc3_errcb; // error structure
USER_STRUC                      user;       // user system structure

/*---------------------------------------------------------------------------*/
/* defines, structures and variables for our demo application                */
/*---------------------------------------------------------------------------*/

ROMCONST__ UBYTE NAME[8] = { 0x45, 0x41, 0x53, 0x59, 0x34, 0x37, 0x31, 0x31 }; //EASY4711

//default configuration data for startup
#define UserCfgDataLength ((UBYTE)0x01)
ROMCONST__ UBYTE DefCfg[1] = { 0x31 };

/*---------------------------------------------------------------------------*/
/* function prototypes                                                       */
/*---------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*/
/* function: read_input_data                                                */
/*--------------------------------------------------------------------------*/
void read_input_data( void )
{
VPC3_UNSIGNED8_PTR  input_buf_ptr;  // pointer to input buffer

    // write DIN data to VPC3
    if(input_buf_ptr = vpc3_get_dinbufptr ())            // get pointer to DIN data
    {
        //todo:
        //handle here data (slave --> master)
        memcpy( input_buf_ptr, &user.input[0], dp_sys.inp_data_len );

        // the user makes a new Din-Buffer available in the state N
        dp_sys.vpc3_con = VPC3_INPUT_UPDATE();
    }//if(input_buf_ptr = vpc3_get_dinbufptr ())
}//void read_input_data( void )

/*---------------------------------------------------------------------------*/
/* function: user_alarm ( is also called from alarm state machine !!!! )     */
/*---------------------------------------------------------------------------*/
UBYTE user_alarm( UBYTE alarm_type, UBYTE check_diag_flag )
{
VPC3_UNION_DIAG_PTR tmp_diag;
UBYTE               ret_value;
UBYTE               ext_diag;
UBYTE               len_diag;
UBYTE               error;
UWORD               diag;

    ret_value = 0x00;

    diag = (UWORD)alarm_type;

    //don't send diagnostic twice!
    if(    ( diag  != user.old_diag         )
        && ( FALSE == user.user_diag_active )
      )
    {
        memset( &user.user_diag[0], 0x00, sizeof( user.user_diag ) );
        tmp_diag.byte_ptr = user.user_diag;

        ext_diag = 0x00;
        len_diag = 0x00;

        switch( alarm_type )
        {
            case USER_TYPE_CFG_OK:
            {
                ext_diag = STAT_DIAG_SET;
                break;
            }//case USER_TYPE_CFG_OK:

            case USER_TYPE_APPL_RDY:
            case USER_TYPE_PRM_NOK:
            case USER_TYPE_PRM_OK:
            default:
            {
                ext_diag = 0x00;
                len_diag = 0x00;
                break;
            }//default:
        }//switch(user[vpc3_channel].diag_byte)

        user.user_diag_active = TRUE;

        error = set_diagnosis( tmp_diag, len_diag, ext_diag, check_diag_flag  );

        if( error == DP_OK )
        {
            user.old_diag = diag;
            if( alarm_type >= USER_TYPE_PRM_OK )
            {
                user.user_diag_active = FALSE;
            }//if( alarm_type >= USER_TYPE_PRM_OK )
            ret_value = DP_OK;
        }//if( error == DP_OK )
        else
        {
            user.user_diag_active = FALSE;

            #ifdef RS232_SERIO
                print_string("DIAG_DP_Error: ");
                print_hexbyte(error);
                print_hexbyte(alarm_type);
            #endif//#ifdef RS232_SERIO

            ret_value = error;
        }//else of if( error == DP_OK )
    }//if(     ( diag  != user.old_diag         ) ...

    return ret_value;
}//UBYTE user_alarm( UBYTE alarm_type, UBYTE check_diag_flag )

/*--------------------------------------------------------------------------*/
/* function: application_ready                                              */
/*--------------------------------------------------------------------------*/
void application_ready( void )
{
    //todo
    //make here your own initialization
    user.input[0] = *READ_PORT1;
    user.input[1] = *READ_PORT2;

    //read input data
    read_input_data();
    //reset Diag.Stat
    user_alarm( USER_TYPE_APPL_RDY, FALSE );
    user.application_ready = TRUE;
}//void application_ready( void )

/*---------------------------------------------------------------------------*/
/* function: user_main                                                       */
/*---------------------------------------------------------------------------*/
void user_main( void )
{
VPC3_UNSIGNED8_PTR          output_buf_ptr; // pointer to output buffer
DP_ERROR_CODE               error;
UBYTE                       outp_state;     // state of output data

    /*-----------------------------------------------------------------------*/
    /* init user data                                                        */
    /*-----------------------------------------------------------------------*/
    memset( &user, 0, sizeof(user) );

    /* TRUE  deactivates diagnosis handling ! */
    /* FALSE activates   diagnosis handling ! */
    user.user_diag_active  = FALSE;
    user.application_ready = FALSE;
    user.state = USER_STATE_CLEAR;

    /*-----------------------------------------------------------------------*/
    /* initialize VPC3                                                       */
    /*-----------------------------------------------------------------------*/

    //todo:
    //insert your real configuration data here
    user.real_cfg.length = UserCfgDataLength; // length of configuration data
    memcpy( &user.real_cfg.cfg_data[0], &DefCfg[0], user.real_cfg.length );

    #ifdef EvaBoard_AT89C5132
        error = vpc3_initialization( (*READ_PORT0 & 0x7F), user.real_cfg );     // address of slave; PORT0
    #else
        error = vpc3_initialization( DP_ADDR, user.real_cfg );                  // address of slave
    #endif//#ifdef EvaBoard_AT89C5132

    if( DP_OK == error )
    {
        // set microcontroller interrupt to falling edge
        SET_FALLING_EDGE__;
        // enable microcontroller interrupt
        ENABLE_VPC3_INT__;

        //todo: before startup the VPC3+, make here your own initialzations

        START_VPC3();
    }
    else
    {
        vpc3_errcb.error_code = error;
        fatal_error( _DP_USER, __LINE__, &vpc3_errcb );
    }//if( DP_OK == error )

    /*-----------------------------------------------------------------------*/
    /* endless demo operation loop                                           */
    /*-----------------------------------------------------------------------*/
    while( 1 )
    {
        /*-------------------------------------------------------------------*/
        /* trigger watchdogs                                                 */
        /*-------------------------------------------------------------------*/
        // toggle user watchdog
        VPC3_RESET_USER_WD();   // toggle user watchdog

        #ifdef RS232_SERIO
            if(rec_counter > 0)
            {
                DISABLE_ALL_INTERRUPT_BIT__;

                print_serial_inputs();

                ENABLE_ALL_INTERRUPT_BIT__;
            }
        #endif//#ifdef RS232_SERIO


        /*-------------------------------------------------------------------*/
        /* internal state machine                                            */
        /*-------------------------------------------------------------------*/
        if( user.state == USER_STATE_CLEAR )
        {
            // clear data
            memset( &user.input[0] , 0, 2 );
            memset( &user.output[0], 0, 2 );

            #ifdef EvaBoard_AT89C5132
                *WRITE_PORT1 = 0x00;
                *WRITE_PORT2 = 0x00;
            #endif//#ifdef EvaBoard_AT89C5132

            user.state = USER_STATE_RUN;
        }//if( user.state == USER_STATE_CLEAR )

        /*-------------------------------------------------------------------*/
        /* dpv1 statemachines                                                */
        /*-------------------------------------------------------------------*/
        vpc3_dpv1_statemachine();

        /*-------------------------------------------------------------------*/
        /* VPC3+ DP-state                                                    */
        /*-------------------------------------------------------------------*/
        switch(VPC3_GET_DP_STATE())
        {
            case WAIT_PRM:
            {
                // set LED's
                CLR_LED_YLW__;
                SET_LED_RED__;

                //BugFix 502
                if( ( vpc3_get_master() != 0xFF ) && (( p_Vpc3->isreg.rd.status_L == 0x85 ) || ( p_Vpc3->isreg.rd.status_L == 0x81 )) )
                {
                    GO_OFFLINE_VPC3();
                    do
                    {
                        //wait, for offline
                    }while( VPC3_GET_OFF_PASS() );

                    START_VPC3();
                }

                if( user.event & VPC3_EV_DX_OUT )
                {
                    user.event &= ~VPC3_EV_DX_OUT;       // clear event
                }

                break;
            }//case WAIT_PRM:

            case WAIT_CFG:
            case DP_ERROR:
            {
                // set LED's
                CLR_LED_YLW__;
                SET_LED_RED__;

                if( user.event & VPC3_EV_DX_OUT )
                {
                    user.event &= ~VPC3_EV_DX_OUT;       // clear event
                }
                break;
            }

⌨️ 快捷键说明

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