-
Notifications
You must be signed in to change notification settings - Fork 4
/
porttimer.c
89 lines (74 loc) · 2.67 KB
/
porttimer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* FreeModbus Libary: ATMega168 Port
* Copyright (C) 2006 Christian Walter <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id: porttimer.c,v 1.4 2006/09/03 11:53:10 wolti Exp $
*/
/* ----------------------- AVR includes -------------------------------------*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
/* ----------------------- Platform includes --------------------------------*/
#include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
/* ----------------------- Defines ------------------------------------------*/
#define MB_TIMER_PRESCALER ( 1024UL )
#define MB_TIMER_TICKS ( F_CPU / MB_TIMER_PRESCALER )
#define MB_50US_TICKS ( 20000UL )
/* ----------------------- Static variables ---------------------------------*/
static USHORT usTimerOCRADelta;
static USHORT usTimerOCRBDelta;
/* ----------------------- Start implementation -----------------------------*/
BOOL
xMBPortTimersInit( USHORT usTim1Timerout50us )
{
/* Calculate overflow counter an OCR values for Timer1. */
usTimerOCRADelta =
( MB_TIMER_TICKS * usTim1Timerout50us ) / ( MB_50US_TICKS );
TCCR1A = 0x00;
TCCR1B = 0x00;
TCCR1C = 0x00;
vMBPortTimersDisable( );
return TRUE;
}
inline void
vMBPortTimersEnable( )
{
TCNT1 = 0x0000;
if( usTimerOCRADelta > 0 )
{
TIMSK1 |= _BV( OCIE1A );
OCR1A = usTimerOCRADelta;
}
TCCR1B |= _BV( CS12 ) | _BV( CS10 );
}
inline void
vMBPortTimersDisable( )
{
/* Disable the timer. */
TCCR1B &= ~( _BV( CS12 ) | _BV( CS10 ) );
/* Disable the output compare interrupts for channel A/B. */
TIMSK1 &= ~( _BV( OCIE1A ) );
/* Clear output compare flags for channel A/B. */
TIFR1 |= _BV( OCF1A ) ;
}
SIGNAL( SIG_OUTPUT_COMPARE1A )
{
( void )pxMBPortCBTimerExpired( );
}