Statistics
| Branch: | Revision:

root / toolbox / freemodbus / port / porttimer.c @ 036b4f9a

History | View | Annotate | Download (2.64 KB)

1
/*
2
 * FreeModbus Libary: ATMega168 Port
3
 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 *
19
 * File: $Id: porttimer.c,v 1.4 2006/09/03 11:53:10 wolti Exp $
20
 */
21

    
22
/* ----------------------- AVR includes -------------------------------------*/
23
#include <avr/io.h>
24
#include <avr/interrupt.h>
25

    
26
/* ----------------------- Platform includes --------------------------------*/
27
#include "port.h"
28

    
29
/* ----------------------- Modbus includes ----------------------------------*/
30
#include "mb.h"
31
#include "mbport.h"
32

    
33
/* ----------------------- Defines ------------------------------------------*/
34
#define MB_TIMER_PRESCALER      ( 1024UL )
35
#define MB_TIMER_TICKS          ( F_CPU / MB_TIMER_PRESCALER )
36
#define MB_50US_TICKS           ( 20000UL )
37

    
38
/* ----------------------- Static variables ---------------------------------*/
39
static USHORT   usTimerOCRADelta;
40
//static USHORT   usTimerOCRBDelta;
41

    
42
/* ----------------------- Start implementation -----------------------------*/
43
BOOL
44
xMBPortTimersInit( USHORT usTim1Timerout50us )
45
{
46
    /* Calculate overflow counter an OCR values for Timer1. */
47
    usTimerOCRADelta =
48
        ( MB_TIMER_TICKS * usTim1Timerout50us ) / ( MB_50US_TICKS );
49

    
50
    TCCR1A = 0x00;
51
    TCCR1B = 0x00;
52
    TCCR1C = 0x00;
53

    
54
    vMBPortTimersDisable(  );
55

    
56
    return TRUE;
57
}
58

    
59

    
60
inline void
61
vMBPortTimersEnable(  )
62
{
63
    TCNT1 = 0x0000;
64
    if( usTimerOCRADelta > 0 )
65
    {
66
        TIMSK1 |= _BV( OCIE1A );
67
        OCR1A = usTimerOCRADelta;
68
    }
69

    
70
    TCCR1B |= _BV( CS12 ) | _BV( CS10 );
71
}
72

    
73
inline void
74
vMBPortTimersDisable(  )
75
{
76
    /* Disable the timer. */
77
    TCCR1B &= ~( _BV( CS12 ) | _BV( CS10 ) );
78
    /* Disable the output compare interrupts for channel A/B. */
79
    TIMSK1 &= ~( _BV( OCIE1A ) );
80
    /* Clear output compare flags for channel A/B. */
81
    TIFR1 |= _BV( OCF1A ) ;
82
}
83

    
84
SIGNAL( SIG_OUTPUT_COMPARE1A )
85
{
86
    ( void )pxMBPortCBTimerExpired(  );
87
}
88