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