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

📄 main.c

📁 汽车电子上现在最火热的总线之一lin2.0总线的单片机实现。
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************/
/*!

   \file main.c

   
   \brief Includes the main function.
	
   \version 1.0
   \author  Christian Bock (cbc) 
   \date 13-Jan-2005 (founded)
	
   Information included in this document is the exclusive property of IEE SA.
   Copying, use or disclosure of its contents, even in part, are not permitted
   without the prior written agreement of the proprietor.
	
*******************************************************************************/


/*******************************************************************************
		INCLUDES
		--------
*******************************************************************************/
#include "mc68hc908qb8.h"
#include "stdint.h"
#include "lin_api.h"
#include "ee_emulator.h"
#include "tim_task.h"
#include "ppd_calculation.h"
#include "iee_functions.h"
#include "diag_task.h"
#include "main_assembler.h"


/*******************************************************************************
		MACROS (#define ...)
		--------------------
*******************************************************************************/
#define MAX_TIME_SINCE_SYNCH_BREAK     3000
#define ERR_IG_CYCLE_SC_NOT_INCR       0x01
#define ERR_IG_CYCLE_OPC_NOT_INCR      0x02
#define ERR_IG_CYCLE_BUS_NOT_INCR      0x04
#define ERR_IG_CYCLE_INT_NOT_INCR      0x08
#define RES_ERR_IG_CYCLE_SC_NOT_INCR   0xFE
#define RES_ERR_IG_CYCLE_OPC_NOT_INCR  0xFD
#define RES_ERR_IG_CYCLE_BUS_NOT_INCR  0xFB
#define RES_ERR_IG_CYCLE_INT_NOT_INCR  0xF7



/*******************************************************************************
		MODULE GLOBAL VARIABLES
		-----------------------
*******************************************************************************/
#pragma DATA_SEG TWO_BYTE_VARIABLE 
/* here come the 2-Byte variables */
uint16_t main_gusTemp;

#pragma DATA_SEG ONE_BYTE_VARIABLE 
/* here come the 1-Byte variables */
uint8_t main_gucTemp;
uint8_t main_gucErrorIncIgnCycle;



/*******************************************************************************
		INTERNAL FUNCTION DECLARATION
		-----------------------------
*******************************************************************************/
void voInit_uC           ( void );
void voEnableCOPCounter  ( void );
void voDisableCOPCounter ( void );


/*******************************************************************************
		LOW LEVEL FUNCTION IMPLEMENTATION (HW related)
		---------------------------------------------
*******************************************************************************/


/*******************************************************************************
		FUNCTION IMPLEMENTATION
		-----------------------
*******************************************************************************/


/******************************************************************************/
/*! \fn void main( void ) 
 *  \brief main function
 *  \param ---
 *  \return ---
 */
void main( void ) 
{   
   
   /* variable initialization */
   main_gusTemp               = (uint16_t)0;
   main_gucTemp               = (uint8_t) 0;
  
   main_gucErrorIncIgnCycle   = (uint8_t) 0x0F; /* reset all incrementation flags */


      
   /* Initialization of the different modules: */
   voInit_uC();               /* Main Task - global uC settings */
   l_sys_init();              /* LIN bus interface */
   TIM_voInit();              /* Timer Task */
   /* tbd */                  /* Diagnostic */
   PPD_voInit();              /* Passenger detection */
   IEE_voInit();              /* QC Modus */
  

   DIAG_voInitCheckCodeArrayCRCBlockwise(); 


   /* after initialization, but before entering the endless loop: 
      update directly all LIN signal with the current values. */
   
   /* Operation hour counter */
   l_u16_wr_INFO_OPET_SEAT_OCCU_PS_LIN( EEE_ucReadOperationHourCounter() );

   /* Error counters init */               
   l_u8_wr_ERR_COU_INTL_SEAT_OCCU_PS_LIN( 
         EEE_ucReadErrorCounter( ERROR_COUNTER_INTERN_ADDRESS ) );
   l_u8_wr_ERR_COU_BUS_SEAT_OCCU_PS_LIN( 
         EEE_ucReadErrorCounter( ERROR_COUNTER_COM_ADDRESS )    );
   l_u8_wr_ERR_COU_OPC_SEAT_OCCU_PS_LIN( 
         EEE_ucReadErrorCounter( ERROR_COUNTER_OPEN_ADDRESS )   );
   l_u8_wr_ERR_COU_SC_SEAT_OCCU_PS_LIN( 
         EEE_ucReadErrorCounter( ERROR_COUNTER_SHORT_ADDRESS )  );  


   voEnableCOPCounter();
   EnableInterrupts;
   

   /* main loop - stay until the QC Mode will be entered */
   while( gucQCModeDisabled )
   {

      COPCTL = (uint8_t)0; /* reset COP counter */
            
      
      /* time to detect occupation? */
      if( (gucTimerFlag & (uint8_t)MASK_SET_MEAS_OCC_FLAG) > (uint8_t)0 )
      {
         /* reset the flag */
         gucTimerFlag &= MASK_RESET_MEAS_OCC_FLAG;

         /* initiate the occupation detection */
         PPD_voRunDetection();
         
         /* update signals */
         l_u8_wr_THRV_SEAT_OCCU_DR_LIN ( gucLinBusOccuState              );
         l_u8_wr_OPC_SEAT_OCCU_DR_LIN  ( gucLinBusErrorStateOpenCircuit  );
         l_u8_wr_SC_SEAT_OCCU_DR_LIN   ( gucLinBusErrorStateShortCircuit );
         
         
         COPCTL = (uint8_t)0; /* reset COP counter */         

         /* increment the short and open circuit error counter, if necessary */
         if( gucLinBusErrorStateOpenCircuit > (uint8_t)0 )
         {   
            
            /* only if it is the first time for this ignition cycle */
            if( ((uint8_t)ERR_IG_CYCLE_OPC_NOT_INCR & main_gucErrorIncIgnCycle) 
                  > (uint8_t)0 )
            {
               EEE_ucIncErrorCounter( ERROR_COUNTER_OPEN_ADDRESS );
               EnableInterrupts; /* flash write routine disables interrupts! */
               
               /* update signal */
               l_u8_wr_ERR_COU_OPC_SEAT_OCCU_PS_LIN( 
                     EEE_ucReadErrorCounter( ERROR_COUNTER_OPEN_ADDRESS ) );

               /* reset the flag to avoid one more 
                  incrementation in this ignition cycle */
               main_gucErrorIncIgnCycle &= RES_ERR_IG_CYCLE_OPC_NOT_INCR;
            }
         }


         COPCTL = (uint8_t)0; /* reset COP counter */         

         if( gucLinBusErrorStateShortCircuit > (uint8_t)0 )
         {   
            
            /* only if it is the first time for this ignition cycle */
            if( ( (uint8_t)ERR_IG_CYCLE_SC_NOT_INCR & main_gucErrorIncIgnCycle ) 
                  > (uint8_t)0 )
            {
               EEE_ucIncErrorCounter( ERROR_COUNTER_SHORT_ADDRESS );
               EnableInterrupts; /* flash write routine disables interrupts! */
               
               /* update signal */
               l_u8_wr_ERR_COU_SC_SEAT_OCCU_PS_LIN( 
                     EEE_ucReadErrorCounter( ERROR_COUNTER_SHORT_ADDRESS ) );  

               /* reset the flag to avoid one more 
                  incrementation in this ignition cycle */
               main_gucErrorIncIgnCycle &= RES_ERR_IG_CYCLE_SC_NOT_INCR;
            }
         }

      }
      
      
      COPCTL = (uint8_t)0; /* reset COP counter */

      /* time to run diagnostics? */
      if( (gucTimerFlag & (uint8_t)MASK_SET_MAT_DIAG_FLAG) > (uint8_t)0 )
      {
         /* reset the flag */
         gucTimerFlag &= (uint8_t)MASK_RESET_MAT_DIAG_FLAG;         

         /* run the calibration section CRC check */
         main_gucTemp = DIAG_ucCheckCalibrationArrayCRC();
         l_u8_wr_INTL_ERR_SEAT_OCCU_DR_LIN( main_gucTemp );

         
         if( main_gucTemp > (uint8_t)0 )
         {   
         
            /* only if it is the first time for this ignition cycle */
            if( ((uint8_t)ERR_IG_CYCLE_INT_NOT_INCR & main_gucErrorIncIgnCycle) 
                  > (uint8_t)0 )
            {
               EEE_ucIncErrorCounter( ERROR_COUNTER_INTERN_ADDRESS );
               EnableInterrupts; /* flash write routine disables interrupts! */
               
               /* update signal */
               l_u8_wr_ERR_COU_INTL_SEAT_OCCU_PS_LIN( 
                     EEE_ucReadErrorCounter( ERROR_COUNTER_INTERN_ADDRESS ) );

               /* reset the flag to avoid one more 
                  incrementation in this ignition cycle */
               main_gucErrorIncIgnCycle &= RES_ERR_IG_CYCLE_INT_NOT_INCR;
            }
            
         }
         
         COPCTL = (uint8_t)0; /* reset COP counter */

         /* calculate code array CRC - will be done page-wise */
         /* as long the return is 2 - comtinue */
         main_gucTemp = DIAG_ucCheckCodeArrayCRCBlockwise();
         
         COPCTL = (uint8_t)0; /* reset COP counter */         

         /* result not ok */
         if( main_gucTemp == (uint8_t)CRC_CALCULATION_COMPLETED_NOK )
         {
            
            /* set telegram error flag */
            l_u8_wr_INTL_ERR_SEAT_OCCU_DR_LIN( 1 );
            /* and init next calculation */
            DIAG_voInitCheckCodeArrayCRCBlockwise();
            
            /* only if it is the first time for this ignition cycle */
            if( ((uint8_t)ERR_IG_CYCLE_INT_NOT_INCR & main_gucErrorIncIgnCycle) 
                  > (uint8_t)0 )
            {
               EEE_ucIncErrorCounter( ERROR_COUNTER_INTERN_ADDRESS );
               EnableInterrupts; /* flash write routine disables interrupts! */
               
               /* update signal */
               l_u8_wr_ERR_COU_INTL_SEAT_OCCU_PS_LIN( 
                     EEE_ucReadErrorCounter( ERROR_COUNTER_INTERN_ADDRESS ) );

               /* reset the flag to avoid one more 
                  incrementation in this ignition cycle */
               main_gucErrorIncIgnCycle &= RES_ERR_IG_CYCLE_INT_NOT_INCR;
            }
            
         }

         COPCTL = (uint8_t)0; /* reset COP counter */         

         /* result ok */
         if( main_gucTemp == (uint8_t)CRC_CALCULATION_COMPLETED_OK )
         {
            
            /* clear telegram error flag */
            l_u8_wr_INTL_ERR_SEAT_OCCU_DR_LIN( (uint8_t)0 );
            /* and init next calculation */
            DIAG_voInitCheckCodeArrayCRCBlockwise();
            
         }
         
         COPCTL = (uint8_t)0; /* reset COP counter */         

         /* higher than 2 - not posible, but try it again */
         if( main_gucTemp > (uint8_t)CRC_CALCULATION_IN_PROGRESS )
         {
            
            /* set telegram error flag */
            l_u8_wr_INTL_ERR_SEAT_OCCU_DR_LIN( (uint8_t)1 );
            /* and init next calculation */
            DIAG_voInitCheckCodeArrayCRCBlockwise();
            
         }
              

         
         COPCTL = (uint8_t)0; /* reset COP counter */         

         /* Only to init the other signals in this frame. 
            The diagnostic is values are not yet supported. */
         l_u8_wr_NOD_SEAT_OCCU_DR_LIN( (uint8_t)0 );


         /* only if it is the first time for this ignition cycle */
         if( (ERR_IG_CYCLE_BUS_NOT_INCR & main_gucErrorIncIgnCycle) > (uint8_t)0 )
         {
            /* initiate diagnostics */
            if( l_ucGetComError() )
            {
               /* increment the counter */
               EEE_ucIncErrorCounter( ERROR_COUNTER_COM_ADDRESS );
               EnableInterrupts; /* flash write routine disables interrupts! */

               /* clear the flag */
               l_voClearComError();
               
               /* update the telegram signal */
               l_bool_wr_COMM_ERR_SEAT_OCCU_LIN( (uint8_t)1 );         

               /* reset the flag to avoid one more 
                  incrementation in this ignition cycle */

⌨️ 快捷键说明

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