OSDN Git Service

[Build][WIP] Start to support SDCC 3.2.x. But, i2c_open of this don't support PIC18F4...
[openi2cradio/OpenI2CRadio.git] / idle.c
1 /*
2  * OpenI2CRADIO
3  * Idle routine.
4  * Using timer0.
5  * Copyright (C) 2013-06-11 K.Ohta <whatisthis.sowhat ai gmail.com>
6  * License: GPL2+LE
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2,
11  *  or (at your option) any later version.
12  *  This library / program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *  See the GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this library; see the file COPYING. If not, write to the
19  *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20  *  MA 02110-1301, USA.
21  *
22  *  As a special exception, if you link this(includeed from sdcc) library
23  *  with other files, some of which are compiled with SDCC,
24  *  to produce an executable, this library does not by itself cause
25  *  the resulting executable to be covered by the GNU General Public License.
26  *  This exception does not however invalidate any other reasons why
27  *  the executable file might be covered by the GNU General Public License.
28  */
29 #if defined(__SDCC)
30 #include <sdcc-lib.h>
31 #include <pic18fregs.h> /* ONLY FOR PIC18x */
32 #else
33 #include <htc.h>
34 #endif
35 #include <signal.h>
36 #include "iodef.h"
37
38
39 void idle_init(void)
40 {
41 #if defined(__SDCC)
42     INTCONbits.TMR0IF = 0;
43     INTCONbits.RBIF = 0;
44     INTCON2bits.TMR0IP = 0;
45 //   INTCON = INTCON & ~(_TMR0IF |  _RBIF); // Enable tmr0 as interrupt and clear interrupt flags.
46 //   INTCON2 = ~_TMR0IP & _TMR0IP; // Interrupt is lower.
47 #else
48    INTCON = INTCON & ~(_INTCON_TMR0IF_MASK |  _INTCON_RBIF_MASK); // Enable tmr0 as interrupt and clear interrupt flags.
49    INTCON2bits.TMR0IP = 0; // Interrupt is lower.
50 #endif
51    WDTCON = 1; // OK? WDT=Disabled.
52 }
53
54
55 void stop_idle(void)
56 {
57     T0CONbits.TMR0ON = 0;
58 }
59
60
61 void idle(unsigned int initial)
62 {
63 //   unsigned char osccon;
64    unsigned char  contword;
65    unsigned int i;
66
67
68    WDTCONbits.SWDTEN = 0; // Lame WDT OFF.
69    /* Enable IDLE */
70    OSCCONbits.IDLEN = 1;
71    INTCONbits.TMR0IF = 0;
72    INTCONbits.TMR0IE = 1; // Enable tmr0 as interrupt and clear interrupt flags.
73    /* Set TMR0 for interrupt*/
74    /* Pre-scaler: 1/16, PSA=0(ON), TOSE=0, T0CS=0(INTERNAL), T08BIT=0(16bit), TMR0ON=1(START) */
75    T0CONbits.T0PS0 = 1;
76    T0CONbits.T0PS1 = 1;
77    T0CONbits.T0PS2 = 1;
78 #if defined(__SDCC)
79 //   contword = _T0PS0 | _T0PS1 | _T0PS2 | _TMR0ON; // Prescaler = 1:256.
80 #else
81    contword = _T0CON_T0PS0_MASK | _T0CON_T0PS1_MASK | _T0CON_T0PS2_MASK | _T0CON_TMR0ON_MASK; // Prescaler = 1:256.
82 #endif   //contword =  _T0PS2 | _TMR0ON; // Pre-scakler is 1:32.
83    //TMR0H = initial >> 8;
84    i = initial;
85    TMR0L = initial & 0xff;
86    TMR0H = initial >> 8; // Write order : L->H
87    T0CONbits.TMR0ON = 1; // Start
88 //   do {
89        Sleep();
90 //       i = TMR0H << 8 + TMR0L; // Check if IDLE-Timer was elapsed.
91 //   } while(i < 3); // Dead area : 0-2.
92    WDTCONbits.SWDTEN = 1; // WDT ON.
93 }
94
95
96 void idle_time_ms(unsigned int ms)
97 {
98     unsigned int tim;
99     unsigned char upper;
100
101     if(ms == 0) return;
102     upper = (ms & 0xe000) >> 13;
103 //    unsigned int upper;
104     tim = ms << 3 - ms >> 4 - ms >>3;
105     tim = 65535 - tim + 1; // tim = 65536 - tim;
106     while(upper > 0) {
107         idle(0x0000); // Upper is 65536 tick.
108     }
109     idle(tim);
110 }
111
112 void idle_time_62_5ms(void)
113 {
114     // Tim = 1ms * 64 - 1ms - 0.5ms
115     // Tim = 0.128ms * (488 + 2.2)
116     //     =
117     idle(65535 - 488 + 4);
118 }
119
120 void idle_time_35ms(void)
121 {
122     // Tim = 35 / 0.128 = 273.44
123     idle(65535 - 274 + 1);
124 }