Statistics
| Branch: | Revision:

root / freemodbus / port / porttimer.c @ 20e5429c

History | View | Annotate | Download (2.67 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
#include <avr/signal.h>
26

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

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

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

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

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

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

    
55
    vMBPortTimersDisable(  );
56

    
57
    return TRUE;
58
}
59

    
60

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

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

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

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