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

📄 sch51.lst

📁 PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont This code is copyright (c) 200
💻 LST
📖 第 1 页 / 共 5 页
字号:
           =2 
           =2 #define RETURN_NORMAL (bit) 0
           =2 #define RETURN_ERROR (bit) 1
           =2 
           =2 
           =2 //------------------------------------------------------------------
           =2 // Interrupts
           =2 // - see Chapter 13.  
           =2 //------------------------------------------------------------------
           =2 
           =2 // Generic 8051/52 timer interrupts (used in most schedulers)
           =2 #define INTERRUPT_Timer_0_Overflow 1
           =2 #define INTERRUPT_Timer_1_Overflow 3
           =2 #define INTERRUPT_Timer_2_Overflow 5
           =2 
           =2 // Additional interrupts (used in shared-clock schedulers)
           =2 #define INTERRUPT_EXTERNAL_0 0
           =2 #define INTERRUPT_EXTERNAL_1 2
           =2 #define INTERRUPT_UART_Rx_Tx 4
           =2 #define INTERRUPT_CAN_c515c 17
C51 COMPILER V6.10  SCH51                                                                  04/18/2001 16:48:04 PAGE 10  

           =2 
           =2 //------------------------------------------------------------------
           =2 // Error codes 
           =2 // - see Chapter 14. 
           =2 //------------------------------------------------------------------
           =2 
           =2 #define ERROR_SCH_TOO_MANY_TASKS (1)
           =2 #define ERROR_SCH_CANNOT_DELETE_TASK (2)
           =2 
           =2 #define ERROR_SCH_WAITING_FOR_SLAVE_TO_ACK (3)
           =2 #define ERROR_SCH_WAITING_FOR_START_COMMAND_FROM_MASTER (3)
           =2 
           =2 #define ERROR_SCH_ONE_OR_MORE_SLAVES_DID_NOT_START (4)
           =2 #define ERROR_SCH_LOST_SLAVE (5)
           =2 
           =2 #define ERROR_SCH_CAN_BUS_ERROR (6)
           =2 
           =2 #define ERROR_I2C_WRITE_BYTE (10)
           =2 #define ERROR_I2C_READ_BYTE (11)
           =2 #define ERROR_I2C_WRITE_BYTE_AT24C64 (12)
           =2 #define ERROR_I2C_READ_BYTE_AT24C64 (13)
           =2 #define ERROR_I2C_DS1621 (14)
           =2 
           =2 #define ERROR_USART_TI (21)
           =2 #define ERROR_USART_WRITE_CHAR (22)
           =2 
           =2 #define ERROR_SPI_EXCHANGE_BYTES_TIMEOUT (31)
           =2 #define ERROR_SPI_X25_TIMEOUT (32)
           =2 #define ERROR_SPI_MAX1110_TIMEOUT (33)
           =2 
           =2 #define ERROR_ADC_MAX150_TIMEOUT (44)
           =2 
           =2 #endif
 115      =2  
 116      =2  /*------------------------------------------------------------------*-
 117      =2    ---- END OF FILE -------------------------------------------------
 118      =2  -*------------------------------------------------------------------*/
  28      =1  
  29      =1  // ------ Public data type declarations ----------------------------
  30      =1  
  31      =1  // Store in DATA area, if possible, for rapid access  
  32      =1  // Total memory per task is 7 bytes
  33      =1  typedef data struct 
  34      =1     {
  35      =1     // Pointer to the task (must be a 'void (void)' function)
  36      =1     void (code * pTask)(void);  
  37      =1  
  38      =1     // Delay (ticks) until the function will (next) be run
  39      =1     // - see SCH_Add_Task() for further details
  40      =1     tWord Delay;       
  41      =1  
  42      =1     // Interval (ticks) between subsequent runs.
  43      =1     // - see SCH_Add_Task() for further details
  44      =1     tWord Period;       
  45      =1  
  46      =1     // Incremented (by scheduler) when task is due to execute
  47      =1     tByte RunMe;       
  48      =1     } sTask; 
  49      =1  
  50      =1  // ------ Public function prototypes -------------------------------
  51      =1  
  52      =1  // Core scheduler functions
C51 COMPILER V6.10  SCH51                                                                  04/18/2001 16:48:04 PAGE 11  

  53      =1  void  SCH_Dispatch_Tasks(void);
  54      =1  tByte SCH_Add_Task(void (code*) (void), const tWord, const tWord);  
  55      =1  bit   SCH_Delete_Task(const tByte);
  56      =1  void  SCH_Report_Status(void);
  57      =1  
  58      =1  // ------ Public constants -----------------------------------------
  59      =1  
  60      =1  // The maximum number of tasks required at any one time
  61      =1  // during the execution of the program
  62      =1  //
  63      =1  // MUST BE ADJUSTED FOR EACH NEW PROJECT
  64      =1  #define SCH_MAX_TASKS   (2)   
  65      =1   
  66      =1  #endif
  67      =1                                
  68      =1  /*------------------------------------------------------------------*-
  69      =1    ---- END OF FILE -------------------------------------------------
  70      =1  -*------------------------------------------------------------------*/
  71      =1  
  36          
  37          // ------ Public variable definitions ------------------------------
  38          
  39          // The array of tasks
  40          sTask SCH_tasks_G[SCH_MAX_TASKS];
  41          
  42          // Used to display the error code
  43          // See Main.H for details of error codes
  44          // See Port.H for details of the error port
  45          tByte Error_code_G = 0;
  46          
  47          // ------ Private function prototypes ------------------------------
  48          
  49          static void SCH_Go_To_Sleep(void);
  50          
  51          // ------ Private variables ----------------------------------------
  52          
  53          // Keeps track of time since last error was recorded (see below)
  54          static tWord Error_tick_count_G;
  55          
  56          // The code of the last error (reset after ~1 minute)
  57          static tByte Last_error_code_G;
  58          
  59          
  60          /*------------------------------------------------------------------*-
  61          
  62            SCH_Dispatch_Tasks()
  63          
  64            This is the 'dispatcher' function.  When a task (function)
  65            is due to run, SCH_Dispatch_Tasks() will run it.
  66            This function must be called (repeatedly) from the main loop.
  67          
  68          -*------------------------------------------------------------------*/
  69          void SCH_Dispatch_Tasks(void) 
  70             {
  71   1         tByte Index;
  72   1      
  73   1         // Dispatches (runs) the next task (if one is ready)
  74   1         for (Index = 0; Index < SCH_MAX_TASKS; Index++)
  75   1            {
  76   2            if (SCH_tasks_G[Index].RunMe > 0) 
  77   2               {
  78   3               (*SCH_tasks_G[Index].pTask)();  // Run the task
C51 COMPILER V6.10  SCH51                                                                  04/18/2001 16:48:04 PAGE 12  

  79   3      
  80   3               SCH_tasks_G[Index].RunMe -= 1;   // Reset / reduce RunMe flag
  81   3      
  82   3               // Periodic tasks will automatically run again
  83   3               // - if this is a 'one shot' task, remove it from the array
  84   3               if (SCH_tasks_G[Index].Period == 0)
  85   3                  {
  86   4                  SCH_Delete_Task(Index);
  87   4                  }
  88   3               }
  89   2            }
  90   1      
  91   1         // Report system status
  92   1         SCH_Report_Status();  
  93   1      
  94   1         // The scheduler enters idle mode at this point 
  95   1         SCH_Go_To_Sleep();          
  96   1         }
  97          
  98          /*------------------------------------------------------------------*-
  99          
 100            SCH_Add_Task()
 101          
 102            Causes a task (function) to be executed at regular intervals 
 103            or after a user-defined delay
 104          
 105            Fn_P   - The name of the function which is to be scheduled.
 106                     NOTE: All scheduled functions must be 'void, void' -
 107                     that is, they must take no parameters, and have 
 108                     a void return type. 
 109                             
 110            DELAY  - The interval (TICKS) before the task is first executed
 111          
 112            PERIOD - If 'PERIOD' is 0, the function is only called once,
 113                     at the time determined by 'DELAY'.  If PERIOD is non-zero,
 114                     then the function is called repeatedly at an interval
 115                     determined by the value of PERIOD (see below for examples
 116                     which should help clarify this).
 117          
 118          
 119            RETURN VALUE:  
 120          
 121            Returns the position in the task array at which the task has been 
 122            added.  If the return value is SCH_MAX_TASKS then the task could 
 123            not be added to the array (there was insufficient space).  If the
 124            return value is < SCH_MAX_TASKS, then the task was added 
 125            successfully.  
 126          
 127            Note: this return value may be required, if a task is
 128            to be subsequently deleted - see SCH_Delete_Task().
 129          
 130            EXAMPLES:
 131          
 132            Task_ID = SCH_Add_Task(Do_X,1000,0);
 133            Causes the function Do_X() to be executed once after 1000 sch ticks.            
 134          
 135            Task_ID = SCH_Add_Task(Do_X,0,1000);
 136            Causes the function Do_X() to be executed regularly, every 1000 sch ticks.            
 137          
 138            Task_ID = SCH_Add_Task(Do_X,300,1000);
 139            Causes the function Do_X() to be executed regularly, every 1000 ticks.
 140            Task will be first executed at T = 300 ticks, then 1300, 2300, etc.            
C51 COMPILER V6.10  SCH51                                                                  04/18/2001 16:48:04 PAGE 13  

 141           
 142          -*------------------------------------------------------------------*/
 143          tByte SCH_Add_Task(void (code * pFunction)(), 
 144                             const tWord DELAY, 
 145                             const tWord PERIOD)    
 146             {
 147   1         tByte Index = 0;
 148   1         
 149   1         // First find a gap in the array (if there is one)
 150   1         while ((SCH_tasks_G[Index].pTask != 0) && (Index < SCH_MAX_TASKS))
 151   1            {
 152   2            Index++;
 153   2            } 
 154   1         
 155   1         // Have we reached the end of the list?   
 156   1         if (Index == SCH_MAX_TASKS)
 157   1            {
 158   2            // Task list is full
 159   2            //
 160   2            // Set the global error variable
 161   2            Error_code_G = ERROR_SCH_TOO_MANY_TASKS;
 162   2      
 163   2            // Also return an error code
 164   2            return SCH_MAX_TASKS;  
 165   2            }
 166   1            
 167   1         // If we're here, there is a space in the task array
 168   1         SCH_tasks_G[Index].pTask  = pFunction;
 169   1           
 170   1         SCH_tasks_G[Index].Delay  = DELAY;
 171   1         SCH_tasks_G[Index].Period = PERIOD;
 172   1      
 173   1         SCH_tasks_G[Index].RunMe  = 0;
 174   1      
 175   1         return Index; // return position of task (to allow later deletion)
 176   1         }
 177          
 178          /*------------------------------------------------------------------*-
 179          
 180            SCH_Delete_Task()
 181          
 182            Removes a task from the scheduler.  Note that this does
 183            *not* delete the associated function from memory: 
 184            it simply means that it is no longer called by the scheduler. 
 185           
 186            TASK_INDEX - The task index.  Provided by SCH_Add_Task(). 
 187          
 188            RETURN VALUE:  RETURN_ERROR or RETURN_NORMAL
 189          
 190          -*------------------------------------------------------------------*/
 191          bit SCH_Delete_Task(const tByte TASK_INDEX) 
 192             {
 193   1         bit Return_code;
 194   1      
 195   1         if (SCH_tasks_G[TASK_INDEX].pTask == 0)
 196   1            {
 197   2            // No task at this location...
 198   2            //
 199   2            // Set the global error variable
 200   2            Error_code_G = ERROR_SCH_CANNOT_DELETE_TASK;
 201   2      
 202   2            // ...also return an error code
C51 COMPILER V6.10  SCH51                                                                  04/18/2001 16:48:04 PAGE 14  

⌨️ 快捷键说明

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