TABLE OF CONTENTS


/Timer [ Modules ]

[ Top ] [ Modules ]

DESCRIPTION


Timer/Calculations [ Constants ]

[ Top ] [ Timer ] [ Constants ]

DESCRIPTION

Calculate the prescale and preload values

DECLARATION

Const Os_timer_cyclespertick = _xtal / Os_timer_frequency   ' clock cycles needed for desired frequency   '
#if Os_timer_hwtimernr = 0                                  ' Set the Timer register size   '
   Const Os_timer_register = 256
#endif
#if Os_timer_hwtimernr = 1
   Const Os_timer_register = 65536
#endif
#if Os_timer_hwtimernr = 2
   Const Os_timer_register = 256
#endif

#if(os_timer_cyclespertick / 1) < Os_timer_register         ' search for the right prescale value   '
   Const Os_timer_prescale = 1
#else
   #if(os_timer_cyclespertick / 8) < Os_timer_register
      Const Os_timer_prescale = 8
   #else
      #if(os_timer_cyclespertick / 64) < Os_timer_register
         Const Os_timer_prescale = 64
      #else
         #if(os_timer_cyclespertick / 256) < Os_timer_register
            Const Os_timer_prescale = 256
         #else
            #if(os_timer_cyclespertick / 1024) < Os_timer_register
               Const Os_timer_prescale = 1024
            #endif
         #endif
      #endif
   #endif
#endif

Const Os_timer_tickfrequency = _xtal / Os_timer_prescale    ' Timer clock frequency   '
Const Os_timer_ticktime = 1 / Os_timer_tickfrequency        ' time of one Timer clock   '
Const Os_timer_desiredtime = 1 / Os_timer_frequency         ' desired time in sec   '
Const Os_timer_tickcount = Os_timer_desiredtime / Os_timer_ticktime       ' number of Ticks for desired time   '
Const Os_timer_preload = Os_timer_register - Os_timer_tickcount       ' start-value for the Timer   '

Timer/Settings [ Constants ]

[ Top ] [ Timer ] [ Constants ]

DESCRIPTION

Timer related Settings

DECLARATION

#if Varexist( "Os_timer_hwtimernr") = False
   Const Os_timer_hwtimernr = 0                             ' choose which hw Timer to use (0, 2: 8 bit; 1: 16 bit)   '
#endif

#if Varexist( "Os_timer_frequency") = False
   Const Os_timer_frequency = 1000                          ' interrupts per second   '
#endif

Timer/Os_timer_systemtime [ Variables ]

[ Top ] [ Timer ] [ Variables ]

DESCRIPTION

Counts the system up-time in Timer ticks

DECLARATION

Dim Os_timer_systemtime As Dword                            ' counts the up-time (+1 per tick)   '

Timer/Os_timer_create [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Creates a new software Timer object

DECLARATION

Function Os_timer_create(byval Interval As Word) As Word
   Local Timerblock As Word
   Timerblock = Malloc(os_timer_hdr_size)
   If Timerblock <> 0 Then
      Os_mem_clear Timerblock , Os_timer_hdr_size
      Setword Timerblock , Os_timer_hdr_interval_ptr , Interval
   End If
   Os_timer_create = Timerblock
End Function

Timer/Os_timer_create_at [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Creates a new software Timer object at a specific address

DECLARATION

Sub Os_timer_create_at(byref Timerblock As Word , Byval Interval As Word)
   Os_mem_clear Timerblock , Os_timer_hdr_size
   Setword Timerblock , Os_timer_hdr_interval_ptr , Interval
End Sub

Timer/Os_timer_get_delta [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Returns the time span between a previous captured up-time count and now

DECLARATION

Function Os_timer_get_delta(byval Snap_time As Dword) As Dword
   Os_enter_critical
   If Snap_time < Os_timer_systemtime Then
      Snap_time = Os_timer_systemtime - Snap_time
   Else
      ' overflow occured   '
      Snap_time = Snap_time - Os_timer_systemtime
      Snap_time = &HFFFFFFFF - Snap_time
   End If
   Os_exit_critical
   Os_timer_get_delta = Snap_time
End Function

Timer/Os_timer_gettrigger [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Returns true and decrements the Timer event count if the software Timer has elapsed. Each Timer event increments the count.

DECLARATION

Function Os_timer_gettrigger(byref Timerblock As Word) As Byte
   Local Triggered As Byte
   Os_enter_critical
   Triggered = Getbyte(timerblock , Os_timer_hdr_triggered_ptr)
   If Triggered <> 0 And Triggered <> 255 Then
      Decr Triggered
      Setbyte Timerblock , Os_timer_hdr_triggered_ptr , Triggered
      Os_exit_critical
      Os_timer_gettrigger = True
   Else
      Os_exit_critical
      Os_timer_gettrigger = False
   End If
End Function

Timer/Os_timer_init [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Enables the hw Timer Interrupt

DECLARATION

Sub Os_timer_init()
   Os_timer_hwtimer = Os_timer_preload                      ' reset Timer register   '
   #if Os_timer_hwtimernr = 0
      Tifr = Tifr And &B00000001                            ' clear TIFR.0, Timer0 Interrupt request flag   '
      Enable Timer0
   #endif
   #if Os_timer_hwtimernr = 1
      Tifr = Tifr And &B00000100                            ' clear TIFR.2, Timer1 Interrupt request flag   '
      Enable Timer1
   #endif
   #if Os_timer_hwtimernr = 2
      Tifr = Tifr And &B00100000                            ' clear TIFR.6, Timer2 Interrupt request flag   '
      Enable Timer2
   #endif
End Sub

Timer/Os_timer_isr [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

the heartbeat of the operating system, the process contexts are saved/restored, scheduling strategy is applied here and the software timers are updated

DECLARATION

Os_timer_isr:
   ' save used registers   '
   push R23
   push R24
   push R25
   in R25, Sreg
   push R25

   ' reset the hw Timer   '
   Os_timer_hwtimer = Os_timer_preload                      ' reset the Timer register with calculated preload value   '

   ' is a software Timer event waiting?   '
   Incr Os_timer_systemtime
   If Os_timer_next_event <> 0 Then
      ' software Timer in use   '
      Decr Os_timer_next_event
      If Os_timer_next_event = 0 Then Os_timer_enable_kernel = Os_timer_enable_kernel + 2
   End If

   ' is a Task ready to execute?   '
   Os_sched_check

   ' need to switch to kernel mode?   '
   lds R23, {os_timer_enable_kernel}
   cpi R23, 1
   BRsh Os_timer_isr_enter_kernel                           ' yes, save actual context and restore the next one   '

   ' restore used registers   '
   pop R25
   !Out Sreg , R25
   pop R25
   pop R24
   pop R23
Return                                                      ' = RETI   '

Timer/Os_timer_isr_enter_kernel [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Timer ISR kernel functions. Calls the Timer and/or scheduler update functions if neccessary and performs the context switch.

DECLARATION

Os_timer_isr_enter_kernel:
   ' restore used registers   '
   pop R25
   !Out Sreg , R25
   pop R25
   pop R24
   pop R23

   ' save process context   '
   Os_task_save_context_full

   ' software Timer functions   '
   If Os_timer_enable_kernel >= 2 Then
      Os_timer_update
      Os_timer_enable_kernel = Os_timer_enable_kernel - 2
   End If

   ' determine the next Task to give cpu time   '
   If Os_timer_enable_kernel = 1 Then
      Os_sched_update
      Os_timer_enable_kernel = 0
   End If

   ' restore process context   '
   Os_multitasking_init:                                    ' this label is called to do the initial context switch at system startup   '
   Os_task_load_context_full
reti

Timer/Os_timer_kill [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Kills a software Timer object

DECLARATION

Sub Os_timer_kill(byref Timerblock As Word)
   Os_timer_stop Timerblock
   Free Timerblock
End Sub

Timer/Os_timer_start [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Starts a software Timer

DECLARATION

Sub Os_timer_start(byref Timerblock As Word)                ' enables and resets a sw-Timer   '
   Local Tempblock As Word
   ' reset Timer interval   '
   Tempblock = Getword(timerblock , Os_timer_hdr_interval_ptr)
   Setword Timerblock , Os_timer_hdr_time_ptr , Tempblock
   If Tempblock < Os_timer_next_event Then
      ' interval is shorter than next Timer update, recalculate timings   '
      Os_timer_next_event = Tempblock
      Os_timer_elapsed_time = Tempblock
   End If
   If Os_timer_next_event = 0 Then Os_timer_next_event = Tempblock       ' no Timer running   '

   ' insert into running list if not already in it   '
   Tempblock = Getword(timerblock , Os_timer_hdr_next_ptr)
   If Tempblock = 0 Then Os_list_insert Os_timer_listhead , Timerblock , Os_timer_hdr_next_ptr
End Sub

Timer/Os_timer_stop [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Stops a software Timer

DECLARATION

Sub Os_timer_stop(byref Timerblock As Word)
   Os_list_remove Os_timer_listhead , Timerblock , Os_timer_hdr_next_ptr
   Os_enter_critical
   If Os_timer_listhead = 0 Then Os_timer_next_event = 0    ' no timers left   '
   Os_exit_critical
End Sub

Timer/Os_timer_update [ Functions ]

[ Top ] [ Timer ] [ Functions ]

DESCRIPTION

Updates all active software Timer objects, raises timed Task events, increments the system up-time count and determines when to call the update again.

DECLARATION

Sub Os_timer_update()
   Os_timer_next_event = &HFFFF
   Os_task_tempword_isr = Os_timer_listhead
   Do
      Os_task_tempword2_isr = Getword(os_task_tempword_isr , Os_timer_hdr_time_ptr)       ' update Timer count   '
      Os_task_tempword2_isr = Os_task_tempword2_isr - Os_timer_elapsed_time
      If Os_task_tempword2_isr = 0 Then                     ' Timer event triggered   '
         Os_task_tempbyte_isr = Getbyte(os_task_tempword_isr , Os_timer_hdr_triggered_ptr)
         If Os_task_tempbyte_isr = &HFF Then
            Os_event_task = Os_task_tempword_isr            ' Timer triggers a Task event   '
            Os_task_event
         Else
            Incr Os_task_tempbyte_isr                       ' Timer is not Task related   '
            If Os_task_tempbyte_isr < 255 Then Setbyte Os_task_tempword_isr , Os_timer_hdr_triggered_ptr , Os_task_tempbyte_isr
         End If
         Os_task_tempword2_isr = Getword(os_task_tempword_isr , Os_timer_hdr_interval_ptr)       ' reset Timer count   '
      End If
      If Os_task_tempword2_isr < Os_timer_next_event Then Os_timer_next_event = Os_task_tempword2_isr       ' search shortest time to next Timer event   '
      Setword Os_task_tempword_isr , Os_timer_hdr_time_ptr , Os_task_tempword2_isr       ' store Timer count   '
      Os_task_tempword_isr = Getword(os_task_tempword_isr , Os_timer_hdr_next_ptr)       ' get next Timer   '
      If Os_task_tempword_isr = 0 Then Exit Do              ' end of list reached   '
   Loop
   Os_timer_elapsed_time = Os_timer_next_event
End Sub