MHVLib  20111011
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_Timer8.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Make, Hack, Void Inc
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *  * Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *  * Neither the name of the Make, Hack, Void nor the
00013  *    names of its contributors may be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 
00029 #ifndef MHV_TIMER8_H_
00030 #define MHV_TIMER8_H_
00031 
00032 #include <inttypes.h>
00033 #include <avr/interrupt.h>
00034 #include <avr/io.h>
00035 #include <stdio.h>
00036 #include <MHV_io.h>
00037 
00038 enum mhv_timer_mode {
00039         MHV_TIMER_ONE_SHOT,
00040         MHV_TIMER_REPETITIVE,
00041         MHV_TIMER_8_PWM_PHASE_CORRECT_VAR_FREQ,
00042         MHV_TIMER_8_PWM_PHASE_CORRECT_2_OUTPUT,
00043         MHV_TIMER_8_PWM_FAST_VAR_FREQ,
00044         MHV_TIMER_8_PWM_FAST_2_OUTPUT,
00045         MHV_TIMER_16_PWM_FAST,
00046         MHV_TIMER_16_PWM_PHASE_CORRECT,
00047         MHV_TIMER_16_PWM_PHASE_FREQ_CORRECT
00048 };
00049 typedef enum mhv_timer_mode MHV_TIMER_MODE;
00050 
00051 enum mhv_timer_type {
00052         MHV_TIMER_TYPE_5_PRESCALERS,
00053         MHV_TIMER_TYPE_7_PRESCALERS
00054 };
00055 typedef enum mhv_timer_type MHV_TIMER_TYPE;
00056 
00057 enum mhv_timer_prescaler {
00058         MHV_TIMER_PRESCALER_DISABLED   = 0,
00059         MHV_TIMER_PRESCALER_5_1        = 1,
00060         MHV_TIMER_PRESCALER_5_8        = 2,
00061         MHV_TIMER_PRESCALER_5_64       = 3,
00062         MHV_TIMER_PRESCALER_5_256      = 4,
00063         MHV_TIMER_PRESCALER_5_1024     = 5,
00064         MHV_TIMER_PRESCALER_5_EXT_RISE = 6,
00065         MHV_TIMER_PRESCALER_5_EXT_FALL = 7,
00066         MHV_TIMER_PRESCALER_7_1        = 1,
00067         MHV_TIMER_PRESCALER_7_8        = 2,
00068         MHV_TIMER_PRESCALER_7_32       = 3,
00069         MHV_TIMER_PRESCALER_7_64       = 4,
00070         MHV_TIMER_PRESCALER_7_128      = 5,
00071         MHV_TIMER_PRESCALER_7_256      = 6,
00072         MHV_TIMER_PRESCALER_7_1024     = 7,
00073 };
00074 typedef enum mhv_timer_prescaler MHV_TIMER_PRESCALER;
00075 
00076 enum mhv_timer_connect_type {
00077         MHV_TIMER_CONNECT_DISCONNECTED = 0,
00078         MHV_TIMER_CONNECT_TOGGLE = 1,
00079         MHV_TIMER_CONNECT_CLEAR = 2,
00080         MHV_TIMER_CONNECT_SET = 3
00081 };
00082 typedef enum mhv_timer_connect_type MHV_TIMER_CONNECT_TYPE;
00083 
00084 #define MHV_TIMER_ASSIGN_1INTERRUPT(mhvTimer, mhvTimerVectors) \
00085         _MHV_TIMER_ASSIGN_1INTERRUPT(mhvTimer, mhvTimerVectors)
00086 #define _MHV_TIMER_ASSIGN_1INTERRUPT(mhvTimer, mhvTimerVect1, mhvTimerVect2, mhvTimerVect3) \
00087 ISR(mhvTimerVect1) { \
00088         mhvTimer.trigger1(); \
00089 }
00090 
00091 #define MHV_TIMER_ASSIGN_2INTERRUPTS(mhvTimer, mhvTimerVectors) \
00092         _MHV_TIMER_ASSIGN_2INTERRUPTS(mhvTimer, mhvTimerVectors)
00093 #define _MHV_TIMER_ASSIGN_2INTERRUPTS(mhvTimer, mhvTimerVect1, mhvTimerVect2, mhvTImerVect3) \
00094 ISR(mhvTimerVect1) { \
00095         mhvTimer.trigger1(); \
00096 } \
00097 ISR(mhvTimerVect2) { \
00098         mhvTimer.trigger2(); \
00099 }
00100 
00101 class MHV_Timer8 {
00102 protected:
00103         volatile uint8_t        *_controlRegA;
00104         volatile uint8_t        *_controlRegB;
00105         volatile uint8_t        *_outputCompare1;
00106         volatile uint8_t        *_outputCompare2;
00107         volatile uint8_t        *_counter;
00108         volatile uint8_t        *_interrupt;
00109         uint8_t                         _interruptEnableA;
00110         MHV_TIMER_PRESCALER     _prescaler;
00111         MHV_TIMER_MODE          _mode;
00112         MHV_TIMER_TYPE          _type;
00113         uint8_t                         _counterSize;
00114         bool                            _haveTime2;
00115         void (*_triggerFunction1)(void *data);
00116         void *_triggerData1;
00117         void (*_triggerFunction2)(void *data);
00118         void *_triggerData2;
00119 
00120         uint8_t calculatePrescaler(uint32_t time, MHV_TIMER_PRESCALER *prescaler, uint16_t *factor);
00121         void calculateTop(uint32_t *time, uint16_t factor);
00122         void setGenerationMode();
00123         MHV_Timer8();
00124         void _setPrescaler(MHV_TIMER_PRESCALER prescaler);
00125 
00126 public:
00127         MHV_Timer8(MHV_TIMER_TYPE type, volatile uint8_t *controlRegA, volatile uint8_t *controlRegB,
00128                         volatile uint8_t *overflowReg1, volatile uint8_t *overflowReg2, volatile uint8_t *counter,
00129                         volatile uint8_t *interrupt, uint8_t interruptEnableA);
00130         bool setPeriods(uint32_t usec1, uint32_t usec2);
00131         uint8_t current();
00132         void setPeriods(MHV_TIMER_PRESCALER prescaler, uint8_t time1, uint8_t time2);
00133         MHV_TIMER_PRESCALER getPrescaler();
00134         uint16_t getPrescalerMultiplier();
00135         void setPrescaler(MHV_TIMER_PRESCALER prescaler);
00136 
00137         uint8_t getTop();
00138         void setTop(uint8_t value);
00139         void setOutput(uint8_t channel, uint8_t value);
00140         void setOutput1(uint8_t value);
00141         void setOutput2(uint8_t value);
00142         uint8_t getOutput(uint8_t channel);
00143         uint8_t getOutput1();
00144         uint8_t getOutput2();
00145         void connectOutput1(MHV_TIMER_CONNECT_TYPE type);
00146         void connectOutput2(MHV_TIMER_CONNECT_TYPE type);
00147         void enable();
00148         void disable();
00149         bool enabled();
00150         void trigger1();
00151         void trigger2();
00152         void setTriggers(void (*triggerFunction1)(void *triggerData), void *triggerData1,
00153                         void (*triggerFunction2)(void *triggerData), void *triggerData2);
00154         void setMode(MHV_TIMER_MODE mode);
00155 };
00156 
00157 #endif /* MHV_TIMER8_H_ */