# SFBTicker.h

Go to the documentation of this file.

    00001 /*                                             -*- mode:C++; fill-column:100 -*-
    00002   SFBTicker.h - A basic 'where do the usec go?' profiling system
    00003   Copyright (C) 2009 The Regents of the University of New Mexico.  All rights reserved.
    00004
    00005   This library is free software; you can redistribute it and/or
    00006   modify it under the terms of the GNU Lesser General Public
    00007   License as published by the Free Software Foundation; either
    00008   version 2.1 of the License, or (at your option) any later version.
    00009
    00010   This library is distributed in the hope that it will be useful,
    00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013   Lesser General Public License for more details.
    00014
    00015   You should have received a copy of the GNU General Public License
    00016   along with this library; if not, write to the Free Software
    00017   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00018   USA
    00019 */
    00020
    00028 #ifndef SFBTICKER_H_
    00029 #define SFBTICKER_H_
    00030
    00031 #include "SFBTypes.h"
    00032 #include "SFBHWPins.h"
    00033 #include "SFBHWMisc.h"          // For suspendInterrupts
    00034
    00040 enum SFBTickerCategories {
    00041   TICKER_BACKGROUND,
    00042
    00043   TICKER_SERIAL_ISR,
    00044
    00045   TICKER_OTHER_ISR,
    00046
    00047   TICKER_PACKET_HANDLER,
    00048
    00049   TICKER_ALARM_HANDLER,
    00050
    00051   TICKER_SKETCH0,
    00052
    00053   TICKER_SKETCH1,
    00054
    00055   TICKER_SKETCH2,
    00056
    00057   MAX_TICKERS
    00058 };
    00059
    00079 class SFBTicker {
    00080  public:
    00081
    00099   inline void pushTime(u32 code) {
    00100     u32 var;
    00101     suspendInterrupts(var);
    00102     pushTimeISR(code);
    00103     restoreInterrupts(var);
    00104   }
    00105
    00116   inline void popTime() {
    00117     u32 var;
    00118     suspendInterrupts(var);
    00119     popTimeISR();
    00120     restoreInterrupts(var);
    00121   }
    00122
    00128   inline void pushTimeISR(u32 code) {
    00129     u32 was = top();
    00130     push(code);
    00131     switchCode(was,top());
    00132   }
    00133
    00139   inline void popTimeISR() {
    00140     u32 was = top();
    00141     pop();
    00142     switchCode(was,top());
    00143   }
    00144
    00155   u32 getUsec(u32 category) ;
    00156
    00161   u32 getTotalUsec() { return micros()-startUsec; }
    00162
    00177   void reset(u32 intoState = TICKER_BACKGROUND) ;
    00178
    00179 private:
    00180   u32 totalUsec[MAX_TICKERS];
    00181   u32 enterUsec[MAX_TICKERS];
    00182   u32 startUsec;
    00183   u32 stack;
    00184   enum {
    00185     BITS = 3,
    00186     MASK = (1<<BITS)-1
    00187   };
    00188   inline u32 crop(u32 code) { return code&MASK; }
    00189   inline void push(u32 code) { stack = (stack<<BITS)|crop(code); }
    00190   inline void pop() { stack >>= BITS; }
    00191   inline u32 top() { return crop(stack); }
    00192   inline void switchCode(u32 from, u32 to) {
    00193     u32 now = micros();
    00194     totalUsec[from] += now - enterUsec[from];
    00195     enterUsec[to] = now;
    00196   }
    00197 };
    00198
    00203 extern SFBTicker Ticker;
    00204
    00226 class SFBProfile {
    00227 public:
    00228
    00239   u32 getMsec(u32 tickerCode) ;
    00240
    00258   u32 getPPM(u32 tickerCode) ;
    00259
    00264   u32 getTotalMsec() ;
    00265
    00269   void begin() ;
    00270
    00275   void end() ;
    00276
    00277 private:
    00278   friend void updateProfile(u32 when, void * arg);
    00279   void update(u32 when);
    00280   u32 totalMsec[MAX_TICKERS];
    00281   u32 totalTime;
    00282   u8 alarmNumber;
    00283 };
    00284
    00289 extern SFBProfile Profile;
    00290 #endif /* SFBTICKER_H_ */