Imported Upstream version 2.9.0
[debian/cc1111] / device / include / pic16 / i2c.h
1
2 /*
3  * I2C communications module library header
4  *
5  * written by Vangelis Rokas, 2005 <vrokas AT otenet.gr>
6  *
7  * Devices implemented:
8  *      PIC18F[24][45][28]
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 /*
27 ** $Id: i2c.h 3714 2005-04-02 13:13:53Z vrokas $
28 */
29
30
31 #ifndef __I2C_H__
32 #define __I2C_H__
33
34 /* link the I/O library */
35 #pragma library io
36
37 #include <pic18fregs.h>
38
39
40 #define _I2CPARAM_SPEC  __data
41
42
43 /* I2C modes of operation */
44 #define I2C_SLAVE10B_INT        0x0f
45 #define I2C_SLAVE7B_INT         0x0e
46 #define I2C_SLAVE_IDLE          0x0b
47 #define I2C_MASTER              0x08
48 #define I2C_SLAVE10B            0x07
49 #define I2C_SLAVE7B             0x06
50
51
52 /* slew rate control */
53 #define I2C_SLEW_OFF    0xc0
54 #define I2C_SLEW_ON     0x00
55
56 /* macros to generate hardware conditions on I2C module */
57
58 /* generate stop condition */
59 #define I2C_STOP()      SSPCON2bits.PEN=1
60
61 /* generate start condition */
62 #define I2C_START()     SSPCON2bits.SEN=1
63
64 /* generate restart condition */
65 #define I2C_RESTART()   SSPCON2bits.RSEN=1
66
67 /* generate not acknoledge condition */
68 #define I2C_NACK()      SSPCON2bits.ACKDT=1; SSPCON2bits.ACKEN=1
69
70 /* generate acknoledge condition */
71 #define I2C_ACK()       SSPCON2bits.ACKDT=0; SSPCON2bits.ACKEN=1
72
73 /* wait until I2C is idle */
74 #define I2C_IDLE()      while((SSPCON2 & 0x1f) | (SSPSTATbits.R_W));
75
76 /* is data ready from I2C module ?? */
77 #define I2C_DRDY()      (SSPSTATbits.BF)
78
79
80 /* function equivalent to macros for generating hardware conditions */
81
82 /* stop */
83 void i2c_stop(void);
84
85 /* start */
86 void i2c_start(void);
87
88 /* restart */
89 void i2c_restart(void);
90
91 /* not acknoledge */
92 void i2c_nack(void);
93
94 /* acknoledge */
95 void i2c_ack(void);
96
97 /* wait until I2C goes idle */
98 void i2c_idle(void);
99
100 /* is character ready in I2C buffer ?? */
101 unsigned char i2c_drdy(void);
102
103 /* read a character from I2C module */
104 unsigned char i2c_readchar(void);
105
106 /* read a string from I2C module */
107 char i2c_readstr(_I2CPARAM_SPEC unsigned char *ptr, unsigned char len);
108
109 /* write a character to I2C module */
110 char i2c_writechar(unsigned char dat);
111
112 /* write a string to I2C module */
113 char i2c_writestr(unsigned char *ptr);
114
115 /* configure I2C port for operation */
116 void i2c_open(unsigned char mode, unsigned char slew, unsigned char addr_brd);
117
118 void i2c_close(void);
119
120
121 #endif  /* __I2C_H__ */