Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_tick.h
Go to the documentation of this file.
1 
33 
105 #ifndef _NZDEFAULT_TICK_H_
106 #define _NZDEFAULT_TICK_H_
107 
109 // "Interrupts" - documentation module
111 
117 //The amount of uticks per micro second, default is 2
118 #if defined(__PIC24FJ256GB206__) || defined(__PIC24FJ256GB208__) || defined(__PIC24FJ256GB210__) || defined(__PIC24FJ128GB106__) || defined(__PIC24FJ128GB108__) || defined(__PIC24FJ128GB110__)
119 #ifndef NZ_UTICKS_PER_MS
120 #define NZ_UTICKS_PER_MS 2000
121 #endif
122 #elif defined(__PIC32MX__)
123 //Default is 80MHz crystal with 64 timer prescaller = 1250 ticks per milli second for timer
124 #ifndef NZ_UTICKS_PER_MS
125 #define NZ_UTICKS_PER_MS 1250
126 #endif
127 #endif
128 
129 
130 #ifndef __INLINE_FUNCTION__
131 #define __INLINE_FUNCTION__ extern inline __attribute__((always_inline))
132 #endif
133 
135 #if defined(__C30__)
136 
137 #ifndef nzINT_PRIORITY_TICK
138 #define nzINT_PRIORITY_TICK ( 5 ) //Define the interrupt priority level, 1-7 (7 is highest)
139 #endif
140 
141 #ifndef nzINT_PRIORITY_UTICK
142 #define nzINT_PRIORITY_UTICK ( 6 ) //Define the interrupt priority level, 1-7 (7 is highest)
143 #endif
144 
145 #if !defined(NZSYS_TICK_TMR2AND3_UTICK)
146 #define NZSYS_TICK_TMR2AND3_UTICK ( 0 ) //Use Timer 2 and 3 for a dedicated uTick counter - for utickXxx() functions
147 #endif
148 
149 
150 #elif defined(__PIC32MX__)
151  #if !defined(_T1IF)
152  #define _T1IF IFS0bits.T1IF
153  #endif
154  //IFS0CLR = _IFS0_T1IF_MASK; //Clear interrupt flag
155 #endif
156 
157 typedef WORD TICK16; //Used for tick16 and utick16 variables
158 typedef DWORD TICK32; //Used for tick32 and utick32 variables
159 
160 
165 extern volatile DWORD_VAL tick_val;
166 
167 
171 #define tickService() { \
172  /* Increment tick */ \
173  tick_val.Val++; \
174 }
175 
176 
180 void tickInit(void);
181 
182 
183 
185 // Standard 16-bit, 1ms tick functions
187 
194 TICK16 __INLINE_FUNCTION__ tick16Get(void)
195 {
196  return tick_val.w[0];
197 }
198 
199 
212 #define tick16TestTmr(tmr) ( (((((tmr) - (tick_val.w[0]) ) & 0x8000))==0) ? 0 : 1)
213 //#define tick16HasTmrExpired(tmr) ( (((((tmr) - (tick_val.w[0]) ) & 0x8000))==0) ? 0 : 1)
214 
215 
224 #define tick16SetTmrMS(tmr, msVal) (tmr = (tick16Get() + msVal))
225 
226 
248 #define tick16GetElapsedMS(oldTick) ((WORD)((tick_val.w[0]) - (oldTick)))
249 
250 
251 //The method below DOES NOT work for TRUE comparison!!!! Because value will be 0 or 0x8000, and TRUE is 1!
252 //For example, this will NEVER return qualify: if (tick16TestTmr(100) == TRUE)
253 //#define tick16TestTmr(tmr) ( ((tmr) - (tick_val.w[0]) ) & 0x8000)
254 
266 #define tick16UpdateTmrMS(tmr, msVal) (tmr += msVal)
267 
268 
289 #define tick16ConvertFromMS(msVal) (msVal)
290 
291 
292 
294 // Long 32-bit, 1ms tick functions
296 
303 DWORD tick32Get(void);
304 
305 
311 #define tick32Get_noDisi() (tick_val.Val)
312 
313 
330 BOOL tick32TestTmr(DWORD tmr);
331 
332 
341 #define tick32SetTmrMS(tmr, msVal) (tmr = (tick32Get() + msVal))
342 
343 
350 #define tick32TestTmr_noDisi(tmr) ((((tmr-tick_val.Val)&0x80000000)==0) ? 0 : 1)
351 
352 
364 #define tick32UpdateTmrMS(tmr, msVal) (tmr += msVal)
365 
366 
376 #define tick32UpdateTmrSec(tmr, secVal) (tmr += (((DWORD)secVal)*1000))
377 
378 
385 #define tick32UpdateTmrMin(tmr, minVal) (tmr += ( ((DWORD)minVal)*60000))
386 
387 
408 #define tick32ConvertFromMS(msVal) ((DWORD)(msVal))
409 
410 
411 
413 // Micro Tick functions
415 
438 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
439 #define utick16Get() (TMR2)
440 #else
441 WORD utick16Get(void);
442 #endif
443 
444 
451 WORD __INLINE_FUNCTION__ utick16Get_noDisi(void) {
452 #if (NZSYS_TICK_TMR2AND3_UTICK==0)
453  WORD tmrCpy;
454  tmrCpy = TMR1;
455 
456  //No tick ISR got triggered while we disabled interrupts. This means TMR1 did
457  //NOT roll over!
458  if (_T1IF == 0) {
459  return ((tick_val.word.LW*NZ_UTICKS_PER_MS) + tmrCpy);
460  }
461  //ELSE
462  //The TMR1 IF got triggered recently, and TMR1 ISR has not been executed yet! This
463  //means that TMR1 can be = PR value, or reset to 0. It also means that tick_val was NOT
464  //incremented yet, and will be incremented once TMR ISR is entered.
465 
466  #if (NZ_UTICKS_PER_MS==2000)
467  //T1IF is triggered when TRM1=PR=1999. It will only reset to 0 at next TMR1 clock!
468  tmrCpy = TMR1+1; //Convert from 0-1999 to 1-2000
469  if (tmrCpy==2000) tmrCpy=0; //Convert from 1-2000 to 0-1999
470 
471  return (((tick_val.word.LW+1)*NZ_UTICKS_PER_MS) + tmrCpy);
472  #else
473  #error "NZ_UTICKS_PER_MS not supported!"
474  #endif
475 #else
476  return TMR2;
477 #endif
478 }
479 
480 
491 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
492 DWORD __INLINE_FUNCTION__ utick32Get(void) {
493  DWORD_VAL ret;
494  ret.word.LW = TMR2;
495  ret.word.HW = TMR3HLD;
496  return ret.Val;
497 }
498 #else
499 DWORD utick32Get(void);
500 #endif //#if (NZSYS_TICK_TMR2AND3_UTICK==1)
501 
502 
503 
510 DWORD __INLINE_FUNCTION__ utick32Get_noDisi(void) {
511 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
512  DWORD_VAL ret;
513  ret.word.LW = TMR2;
514  ret.word.HW = TMR3HLD;
515  return ret.Val;
516 #else
517  WORD tmrCpy;
518  tmrCpy = TMR1;
519 
520  //No tick ISR got triggered while we disabled interrupts. This means TMR1 did
521  //NOT roll over!
522  if (_T1IF == 0) {
523  return (DWORD)( ((DWORD)(tick_val.Val*NZ_UTICKS_PER_MS)) + tmrCpy);
524  }
525  //ELSE
526  //The TMR1 IF got triggered recently, and TMR1 ISR has not been executed yet! This
527  //means that TMR1 can be = PR value, or reset to 0. It also means that tick_val was NOT
528  //incremented yet, and will be incremented once TMR ISR is entered.
529 
530  #if (NZ_UTICKS_PER_MS==2000)
531  //T1IF is triggered when TRM1=PR=1999. It will only reset to 0 at next TMR1 clock!
532  tmrCpy = TMR1+1; //Convert from 0-1999 to 1-2000
533  if (tmrCpy==2000) tmrCpy=0; //Convert from 1-2000 to 0-1999
534 
535  return (DWORD)( ((DWORD)((tick_val.Val+1)*NZ_UTICKS_PER_MS)) + tmrCpy);
536  #else
537  #error "NZ_UTICKS_PER_MS not supported!"
538  #endif
539 #endif //#if (NZSYS_TICK_TMR2AND3_UTICK==1)
540 }
541 
542 
555 #define utick16TestTmr(tmr) ( (((((tmr) - (utick16Get()) ) & 0x8000))==0) ? 0 : 1)
556 
557 
582 #define utick16GetElapsedUS(oldTick) utick16ConvertToUS(((WORD)((utick16Get()) - (oldTick))))
583 
584 
592 #define utick16ConvertToUS(utickVal) ( ((WORD)utickVal) / ((WORD)(NZ_UTICKS_PER_MS/1000)) )
593 
594 
604 #define utick16ConvertFromUS(usVal) (WORD)( ((WORD)usVal) * (NZ_UTICKS_PER_MS/1000) )
605 
606 
622 #define utick32TestTmr(tmr) ((((tmr-utick32Get())&0x80000000)==0) ? 0 : 1)
623 
624 
632 #define utick32ConvertToUS(utickVal) ( ((DWORD)utickVal) / ((DWORD)(NZ_UTICKS_PER_MS/1000)) )
633 
634 
644 #define utick32ConvertFromUS(usVal) (DWORD)( ((DWORD)usVal) * (NZ_UTICKS_PER_MS/1000) )
645 
646 
647 
649 // 8us tick functions
651 
652 
659 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
660 WORD __INLINE_FUNCTION__ tick16Get_8us(void) {
661  DWORD_VAL dw;
662  dw.word.LW = TMR2;
663  dw.word.HW = TMR3HLD;
664  #if (NZ_UTICKS_PER_MS==2000)
665  return ((WORD)(dw.Val/16));
666  #else
667  #error "NZ_UTICKS_PER_MS not supported!"
668  #endif
669 }
670 #else
671 WORD tick16Get_8us(void);
672 #endif
673 
674 
681 WORD __INLINE_FUNCTION__ tick16Get_8us_noDisi(void) {
682 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
683  DWORD_VAL dw;
684  dw.word.LW = TMR2;
685  dw.word.HW = TMR3HLD;
686  #if (NZ_UTICKS_PER_MS==2000)
687  return ((WORD)(dw.Val/16));
688  #else
689  #error "NZ_UTICKS_PER_MS not supported!"
690  #endif
691 #else
692  WORD tmrCpy;
693 #if defined(__C30__)
694  tmrCpy = TMR1;
695  if (_T1IF == 0) {
696  //This function is ONLY called from and ISR with higher priority than
697  //the TMR1 interrupt, so not TMR1 interrupt will occur while processing!
698  //At this stage we know that tmrCpy did NOT reset to 0, use it!
699  #if (NZ_UTICKS_PER_MS==2000)
700  /* Convert 1ms resolution to 8us = x * (1000/8) = x * 125 */
701  /* Convert 500ns to 8us */
702  return ((tick_val.word.LW*125) + (tmrCpy/16));
703  #else
704  #error "NZ_UTICKS_PER_MS not supported!"
705  #endif
706  }
707  //ELSE
708  //The TMR1 IF got triggered recently, and TMR1 ISR has not been executed yet! This
709  //means that TMR1 can be = PR value, or reset to 0. It also means that tick_val was NOT
710  //incremented yet, and will be incremented once TMR ISR is entered.
711 
712  #if (NZ_UTICKS_PER_MS==2000)
713  //T1IF is triggered when TRM1=PR=1999. It will only reset to 0 at next TMR1 clock!
714  tmrCpy = TMR1+1; //Convert from 0-1999 to 1-2000
715  if (tmrCpy==2000) tmrCpy=0; //Convert from 1-2000 to 0-1999
716 
717  return (( ((WORD)(tick_val.word.LW+1))*125) + (tmrCpy/16));
718  #else
719  #error "NZ_UTICKS_PER_MS not supported!"
720  #endif
721 #elif defined(__PIC32MX__)
722  return 0;
723 #endif
724 #endif //#if (NZSYS_TICK_TMR2AND3_UTICK==1)
725 }
726 
727 
728 
735 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
736 WORD __INLINE_FUNCTION__ tick32Get_8us(void) {
737  DWORD_VAL dw;
738  dw.word.LW = TMR2;
739  dw.word.HW = TMR3HLD;
740  #if (NZ_UTICKS_PER_MS==2000)
741  return (dw.Val/16);
742  #else
743  #error "NZ_UTICKS_PER_MS not supported!"
744  #endif
745 }
746 #else
747 WORD tick32Get_8us(void);
748 #endif
749 
750 
759 WORD __INLINE_FUNCTION__ tick32Get_8us_noDisi(void) {
760 #if (NZSYS_TICK_TMR2AND3_UTICK==1)
761  DWORD_VAL dw;
762  dw.word.LW = TMR2;
763  dw.word.HW = TMR3HLD;
764  #if (NZ_UTICKS_PER_MS==2000)
765  return (dw.Val/16);
766  #else
767  #error "NZ_UTICKS_PER_MS not supported!"
768  #endif
769 #else
770  WORD tmrCpy;
771 #if defined(__C30__)
772  tmrCpy = TMR1;
773  //No tick ISR got triggered while we disabled interrupts. This means TMR1 did
774  //NOT roll over! Convert 1ms resolution to 8us = x * (1000/8) = x * 125
775  if (_T1IF == 0) {
776  #if (NZ_UTICKS_PER_MS==2000)
777  return (DWORD)( ((DWORD)(tick_val.Val*125)) + tmrCpy/16);
778  #else
779  #error "NZ_UTICKS_PER_MS not supported!"
780  #endif
781  }
782  //ELSE
783  //The TMR1 IF got triggered recently, and TMR1 ISR has not been executed yet! This
784  //means that TMR1 can be = PR value, or reset to 0. It also means that tick_val was NOT
785  //incremented yet, and will be incremented once TMR ISR is entered.
786 
787  #if (NZ_UTICKS_PER_MS==2000)
788  //T1IF is triggered when TRM1=PR=1999. It will only reset to 0 at next TMR1 clock!
789  tmrCpy = TMR1+1; //Convert from 0-1999 to 1-2000
790  if (tmrCpy==2000) tmrCpy=0; //Convert from 1-2000 to 0-1999
791 
792  return ( ((DWORD)((tick_val.Val+1)*125)) + (tmrCpy/16));
793  #else
794  #error "NZ_UTICKS_PER_MS not supported!"
795  #endif
796 #elif defined(__PIC32MX__)
797 #endif
798 #endif //#if (NZSYS_TICK_TMR2AND3_UTICK==1)
799 }
800 
801 #endif //end of #define _NZDEFAULT_TICK_H_