00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef HWTIMER
00027 #define HWTIMER
00028
00029 #include "hardware.h"
00030 #include "pinatport.h"
00031 #include "rwmem.h"
00032 #include "prescalermux.h"
00033 #include "timerirq.h"
00034 #include "traceval.h"
00035 #include "icapturesrc.h"
00036
00038
00041 class BasicTimerUnit: public Hardware, public TraceValueRegister {
00042
00043 private:
00044 int cs;
00045 TraceValue* counterTrace;
00046 bool captureInputState;
00047 int icapNCcounter;
00048 bool icapNCstate;
00049
00050 protected:
00052 enum WGMtype {
00053 WGM_NORMAL = 0,
00054 WGM_PCPWM_8BIT,
00055 WGM_PCPWM_9BIT,
00056 WGM_PCPWM_10BIT,
00057 WGM_CTC_OCRA,
00058 WGM_FASTPWM_8BIT,
00059 WGM_FASTPWM_9BIT,
00060 WGM_FASTPWM_10BIT,
00061 WGM_PFCPWM_ICR,
00062 WGM_PFCPWM_OCRA,
00063 WGM_PCPWM_ICR,
00064 WGM_PCPWM_OCRA,
00065 WGM_CTC_ICR,
00066 WGM_RESERVED,
00067 WGM_FASTPWM_ICR,
00068 WGM_FASTPWM_OCRA,
00069 WGM_tablesize
00070 };
00072 enum COMtype {
00073 COM_NOOP = 0,
00074 COM_TOGGLE,
00075 COM_CLEAR,
00076 COM_SET
00077 };
00079 enum CEtype {
00080 EVT_TOP_REACHED = 0,
00081 EVT_MAX_REACHED,
00082 EVT_BOTTOM_REACHED,
00083 EVT_COMPARE_1,
00084 EVT_COMPARE_2,
00085 EVT_COMPARE_3,
00086 };
00088 enum OCRIDXtype {
00089 OCRIDX_A = 0,
00090 OCRIDX_B,
00091 OCRIDX_C,
00092 OCRIDX_maxUnits
00093 };
00094 typedef void (BasicTimerUnit::*wgmfunc_t)(CEtype);
00095
00096 AvrDevice *core;
00097 PrescalerMultiplexer* premx;
00098 IRQLine* timerOverflow;
00099 IRQLine* timerCapture;
00100
00101 unsigned long vtcnt;
00102 unsigned long vlast_tcnt;
00103
00104
00105
00106 int updown_counting;
00107 bool count_down;
00108 unsigned long limit_bottom;
00109 unsigned long limit_top;
00110 unsigned long limit_max;
00111
00112 unsigned long icapRegister;
00113 ICaptureSource* icapSource;
00114 bool icapRisingEdge;
00115 bool icapNoiseCanceler;
00116
00117 WGMtype wgm;
00118 wgmfunc_t wgmfunc[WGM_tablesize];
00119 unsigned long compare[OCRIDX_maxUnits];
00120 unsigned long compare_dbl[OCRIDX_maxUnits];
00121 bool compareEnable[OCRIDX_maxUnits];
00122 COMtype com[OCRIDX_maxUnits];
00123 IRQLine* timerCompare[OCRIDX_maxUnits];
00124 PinAtPort* compare_output[OCRIDX_maxUnits];
00125 bool compare_output_state[OCRIDX_maxUnits];
00126
00128 void CountTimer(void);
00130 virtual void InputCapture(void);
00132
00136 void HandleEvent(CEtype event) { (this->*wgmfunc[wgm])(event); }
00138 void SetClockMode(int _cs);
00140 void SetCounter(unsigned long val);
00142 void SetCompareOutputMode(int idx, COMtype mode);
00144 void SetCompareOutput(int idx);
00146 void SetPWMCompareOutput(int idx, bool topOrDown);
00147
00149 bool WGMisPWM(void) { return wgm != WGM_NORMAL && wgm != WGM_CTC_OCRA && wgm != WGM_CTC_ICR; }
00151 bool WGMuseICR(void) { return wgm == WGM_FASTPWM_ICR || wgm == WGM_CTC_ICR || wgm == WGM_PCPWM_ICR || wgm == WGM_PFCPWM_ICR; }
00153 void WGMFunc_noop(CEtype event) {}
00155 void WGMfunc_normal(CEtype event);
00157 void WGMfunc_ctc(CEtype event);
00159 void WGMfunc_fastpwm(CEtype event);
00161 void WGMfunc_pcpwm(CEtype event);
00163 void WGMfunc_pfcpwm(CEtype event);
00164
00165 public:
00167 BasicTimerUnit(AvrDevice *core,
00168 PrescalerMultiplexer *p,
00169 int unit,
00170 IRQLine* tov,
00171 IRQLine* tcap,
00172 ICaptureSource* icapsrc,
00173 int countersize = 8);
00174 ~BasicTimerUnit();
00176 void Reset();
00177
00179 virtual unsigned int CpuCycle();
00180 };
00181
00183 class HWTimer8: public BasicTimerUnit {
00184
00185 protected:
00187 void ChangeWGM(WGMtype mode);
00188
00190 void SetCompareRegister(int idx, unsigned char val);
00192 unsigned char GetCompareRegister(int idx);
00193
00195 void Set_TCNT(unsigned char val) { SetCounter(val); }
00197 unsigned char Get_TCNT() { return vtcnt & 0xff; }
00198
00200 void Set_OCRA(unsigned char val) { SetCompareRegister(0, val); }
00202 unsigned char Get_OCRA() { return GetCompareRegister(0); }
00203
00205 void Set_OCRB(unsigned char val) { SetCompareRegister(1, val); }
00207 unsigned char Get_OCRB() { return GetCompareRegister(1); }
00208
00209 public:
00210 IOReg<HWTimer8> tcnt_reg;
00211 IOReg<HWTimer8> ocra_reg;
00212 IOReg<HWTimer8> ocrb_reg;
00213
00214 HWTimer8(AvrDevice *core,
00215 PrescalerMultiplexer *p,
00216 int unit,
00217 IRQLine* tov,
00218 IRQLine* tcompA,
00219 PinAtPort* outA,
00220 IRQLine* tcompB,
00221 PinAtPort* outB);
00223 void Reset();
00224 };
00225
00227 class HWTimer16: public BasicTimerUnit {
00228
00229 protected:
00231 unsigned char accessTempRegister;
00232
00234 void SetCompareRegister(int idx, bool high, unsigned char val);
00236 unsigned char GetCompareRegister(int idx, bool high);
00238 void SetComplexRegister(bool is_icr, bool high, unsigned char val);
00240 unsigned char GetComplexRegister(bool is_icr, bool high);
00241
00243 void ChangeWGM(WGMtype mode);
00244
00246 void Set_TCNTH(unsigned char val) { SetComplexRegister(false, true, val); }
00248 unsigned char Get_TCNTH() { return GetComplexRegister(false, true); }
00250 void Set_TCNTL(unsigned char val) { SetComplexRegister(false, false, val); }
00252 unsigned char Get_TCNTL() { return GetComplexRegister(false, false); }
00253
00255 void Set_OCRAH(unsigned char val) { SetCompareRegister(0, true, val); }
00257 unsigned char Get_OCRAH() { return GetCompareRegister(0, true); }
00259 void Set_OCRAL(unsigned char val) { SetCompareRegister(0, false, val); }
00261 unsigned char Get_OCRAL() { return GetCompareRegister(0, false); }
00262
00264 void Set_OCRBH(unsigned char val) { SetCompareRegister(1, true, val); }
00266 unsigned char Get_OCRBH() { return GetCompareRegister(1, true); }
00268 void Set_OCRBL(unsigned char val) { SetCompareRegister(1, false, val); }
00270 unsigned char Get_OCRBL() { return GetCompareRegister(1, false); }
00271
00273 void Set_OCRCH(unsigned char val) { SetCompareRegister(2, true, val); }
00275 unsigned char Get_OCRCH() { return GetCompareRegister(2, true); }
00277 void Set_OCRCL(unsigned char val) { SetCompareRegister(2, false, val); }
00279 unsigned char Get_OCRCL() { return GetCompareRegister(2, false); }
00280
00282 void Set_ICRH(unsigned char val) { SetComplexRegister(true, true, val); }
00284 unsigned char Get_ICRH() { return GetComplexRegister(true, true); }
00286 void Set_ICRL(unsigned char val) { SetComplexRegister(true, false, val); }
00288 unsigned char Get_ICRL() { return GetComplexRegister(true, false); }
00289
00290 public:
00291 IOReg<HWTimer16> tcnt_h_reg;
00292 IOReg<HWTimer16> tcnt_l_reg;
00293 IOReg<HWTimer16> ocra_h_reg;
00294 IOReg<HWTimer16> ocra_l_reg;
00295 IOReg<HWTimer16> ocrb_h_reg;
00296 IOReg<HWTimer16> ocrb_l_reg;
00297 IOReg<HWTimer16> ocrc_h_reg;
00298 IOReg<HWTimer16> ocrc_l_reg;
00299 IOReg<HWTimer16> icr_h_reg;
00300 IOReg<HWTimer16> icr_l_reg;
00301
00302 HWTimer16(AvrDevice *core,
00303 PrescalerMultiplexer *p,
00304 int unit,
00305 IRQLine* tov,
00306 IRQLine* tcompA,
00307 PinAtPort* outA,
00308 IRQLine* tcompB,
00309 PinAtPort* outB,
00310 IRQLine* tcompC,
00311 PinAtPort* outC,
00312 IRQLine* ticap,
00313 ICaptureSource* icapsrc);
00315 void Reset(void);
00316 };
00317
00319
00328 class HWTimer8_0C: public HWTimer8 {
00329
00330 protected:
00331 unsigned char tccr_val;
00332
00334 void Set_TCCR(unsigned char val);
00336 unsigned char Get_TCCR() { return tccr_val; }
00337
00338 public:
00339 IOReg<HWTimer8_0C> tccr_reg;
00340
00341 HWTimer8_0C(AvrDevice *core,
00342 PrescalerMultiplexer *p,
00343 int unit,
00344 IRQLine* tov);
00346 void Reset(void);
00347 };
00348
00350
00358 class HWTimer8_1C: public HWTimer8 {
00359
00360 protected:
00361 unsigned char tccr_val;
00362
00364 void Set_TCCR(unsigned char val);
00366 unsigned char Get_TCCR() { return tccr_val; }
00367
00368 public:
00369 IOReg<HWTimer8_1C> tccr_reg;
00370
00371 HWTimer8_1C(AvrDevice *core,
00372 PrescalerMultiplexer *p,
00373 int unit,
00374 IRQLine* tov,
00375 IRQLine* tcompA,
00376 PinAtPort* outA);
00378 void Reset(void);
00379 };
00380
00382
00397 class HWTimer8_2C: public HWTimer8 {
00398
00399 private:
00400 int wgm_raw;
00401
00403 void Set_WGM(int val);
00404
00405 protected:
00406 unsigned char tccra_val;
00407 unsigned char tccrb_val;
00408
00410 void Set_TCCRA(unsigned char val);
00412 unsigned char Get_TCCRA() { return tccra_val; }
00413
00415 void Set_TCCRB(unsigned char val);
00417 unsigned char Get_TCCRB() { return tccrb_val; }
00418
00419 public:
00420 IOReg<HWTimer8_2C> tccra_reg;
00421 IOReg<HWTimer8_2C> tccrb_reg;
00422
00423 HWTimer8_2C(AvrDevice *core,
00424 PrescalerMultiplexer *p,
00425 int unit,
00426 IRQLine* tov,
00427 IRQLine* tcompA,
00428 PinAtPort* outA,
00429 IRQLine* tcompB,
00430 PinAtPort* outB);
00432 void Reset(void);
00433 };
00434
00436
00451 class HWTimer16_1C: public HWTimer16 {
00452
00453 private:
00454 int wgm_raw;
00455
00457 void Set_WGM(int val);
00458
00459 protected:
00460 unsigned char tccra_val;
00461 unsigned char tccrb_val;
00462
00464 void Set_TCCRA(unsigned char val);
00466 unsigned char Get_TCCRA() { return tccra_val; }
00467
00469 void Set_TCCRB(unsigned char val);
00471 unsigned char Get_TCCRB() { return tccrb_val; }
00472
00473 public:
00474 IOReg<HWTimer16_1C> tccra_reg;
00475 IOReg<HWTimer16_1C> tccrb_reg;
00476
00477 HWTimer16_1C(AvrDevice *core,
00478 PrescalerMultiplexer *p,
00479 int unit,
00480 IRQLine* tov,
00481 IRQLine* tcompA,
00482 PinAtPort* outA,
00483 IRQLine* ticap,
00484 ICaptureSource* icapsrc);
00486 void Reset(void);
00487 };
00488
00490
00509 class HWTimer16_2C2: public HWTimer16 {
00510
00511 private:
00512 int wgm_raw;
00513 bool at8515_mode;
00514
00516 void Set_WGM(int val);
00517
00518 protected:
00519 unsigned char tccra_val;
00520 unsigned char tccrb_val;
00521
00523 void Set_TCCRA(unsigned char val);
00525 unsigned char Get_TCCRA() { return tccra_val; }
00526
00528 void Set_TCCRB(unsigned char val);
00530 unsigned char Get_TCCRB() { return tccrb_val; }
00531
00532 public:
00533 IOReg<HWTimer16_2C2> tccra_reg;
00534 IOReg<HWTimer16_2C2> tccrb_reg;
00535
00536 HWTimer16_2C2(AvrDevice *core,
00537 PrescalerMultiplexer *p,
00538 int unit,
00539 IRQLine* tov,
00540 IRQLine* tcompA,
00541 PinAtPort* outA,
00542 IRQLine* tcompB,
00543 PinAtPort* outB,
00544 IRQLine* ticap,
00545 ICaptureSource* icapsrc,
00546 bool is_at8515);
00548 void Reset(void);
00549 };
00550
00552
00574 class HWTimer16_2C3: public HWTimer16 {
00575
00576 protected:
00577 unsigned char tccra_val;
00578 unsigned char tccrb_val;
00579
00581 void Set_TCCRA(unsigned char val);
00583 unsigned char Get_TCCRA() { return tccra_val; }
00584
00586 void Set_TCCRB(unsigned char val);
00588 unsigned char Get_TCCRB() { return tccrb_val; }
00589
00591 void Set_TCCRC(unsigned char val);
00593 unsigned char Get_TCCRC() { return 0; }
00594
00595 public:
00596 IOReg<HWTimer16_2C3> tccra_reg;
00597 IOReg<HWTimer16_2C3> tccrb_reg;
00598 IOReg<HWTimer16_2C3> tccrc_reg;
00599
00600 HWTimer16_2C3(AvrDevice *core,
00601 PrescalerMultiplexer *p,
00602 int unit,
00603 IRQLine* tov,
00604 IRQLine* tcompA,
00605 PinAtPort* outA,
00606 IRQLine* tcompB,
00607 PinAtPort* outB,
00608 IRQLine* ticap,
00609 ICaptureSource* icapsrc);
00611 void Reset(void);
00612 };
00613
00615
00637 class HWTimer16_3C: public HWTimer16 {
00638
00639 protected:
00640 unsigned char tccra_val;
00641 unsigned char tccrb_val;
00642
00644 void Set_TCCRA(unsigned char val);
00646 unsigned char Get_TCCRA() { return tccra_val; }
00647
00649 void Set_TCCRB(unsigned char val);
00651 unsigned char Get_TCCRB() { return tccrb_val; }
00652
00654 void Set_TCCRC(unsigned char val);
00656 unsigned char Get_TCCRC() { return 0; }
00657
00658 public:
00659 IOReg<HWTimer16_3C> tccra_reg;
00660 IOReg<HWTimer16_3C> tccrb_reg;
00661 IOReg<HWTimer16_3C> tccrc_reg;
00662
00663 HWTimer16_3C(AvrDevice *core,
00664 PrescalerMultiplexer *p,
00665 int unit,
00666 IRQLine* tov,
00667 IRQLine* tcompA,
00668 PinAtPort* outA,
00669 IRQLine* tcompB,
00670 PinAtPort* outB,
00671 IRQLine* tcompC,
00672 PinAtPort* outC,
00673 IRQLine* ticap,
00674 ICaptureSource* icapsrc);
00676 void Reset(void);
00677 };
00678
00679 #endif