OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / i2c_io.c
1 /*
2  * OpenI2CRADIO
3  * I2C Handler
4  * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
5  * License: GPL2+LE
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2,
10  *  or (at your option) any later version.
11  *  This library / program 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.
14  *  See the 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(includeed from sdcc) library
22  *  with other files, some of which are compiled with SDCC,
23  *  to produce an executable, this library does not by itself cause
24  *  the resulting executable to be covered by the GNU General Public License.
25  *  This exception does not however invalidate any other reasons why
26  *  the executable file might be covered by the GNU General Public License.
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef __SDCC
32 #include <delay.h>
33 #include <pic18fregs.h>
34 #endif
35
36 #include "i2c_io.h"
37
38
39
40 #ifdef _I2C_IO_TWO_MSSPS
41 void i2c1_init(void)
42 {
43     unsigned char b;
44 //    b = SSP1CON1;
45     b = _SSPEN | _SSPM3;
46     SSP1CON1 = b;
47     SSP1ADD = 19; // Fosc:8000[KHz] / (4 * I2C:Clock:100[KHz]) - 1
48     PIR1bits.SSPIF = 0;
49     PIE1bits.SSPIE = 0;
50     IPR1bits.SSPIP = 1;
51 }
52 void i2c2_init(void)
53 {
54     unsigned char b;
55     b =  _SSPEN | _SSPM3;
56     SSP2CON1 = b;
57     SSP2ADD = 4; // Fosc:8000[KHz] / (4 * I2C:Clock:400[KHz]) - 1
58 }
59 #else
60  #ifdef _I2C_IO_ONE_MSSP
61 void i2c1_init(void)
62 {
63  //   unsigned char b;
64 //    b = _SSPEN | _SSPM3;
65  //   SSPCON1bits.SSPEN = 1;
66 //    SSPCON1bits.SSPM3 = 1;
67 //    SSPADD = 4; // Fosc:8000[KHz] / (4 * I2C:Clock:400[KHz]) - 1
68 //    SSPSTAT = 0x80;
69     PIR1bits.SSPIF = 0;
70     PIE1bits.SSPIE = 0;
71     IPR1bits.SSPIP = 1;
72 }
73 void i2c2_init(void)
74 {
75     i2c1_init();
76 }
77
78  #else // None MSSP, Software I2C
79 void i2c1_init(void)
80 {
81 }
82 void i2c2_init(void)
83 {
84 }
85  #endif
86 // I2C_IO
87 void i2c_send_byte(unsigned char addr, unsigned char reg, unsigned char data)
88 {
89 #ifdef __SDCC
90     i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
91     I2C_START();
92     i2c_writechar(addr);
93     i2c_writechar(reg);
94     i2c_writechar(data);
95     I2C_STOP();
96     i2c_close();
97 //    delay100tcy(2);
98 #else
99     OpenI2C(MASTER, SLEW_ON);
100     SSPADD = 0x5;
101 //    IdleI2C();
102     StartI2C();
103     while (SSPCON2bits.SEN);
104     WriteI2C(addr);
105     while(SSPCON2bits.SEN);
106     WriteI2C(reg);
107     while(SSPCON2bits.SEN);
108     WriteI2C(data);
109     StopI2C();
110     while (SSPCON2bits.PEN);
111     CloseI2C();
112 #endif  //    i2c_idle();
113 }
114
115 unsigned char i2c_read_byte(unsigned char addr, unsigned char reg)
116 {
117     unsigned char c;
118   #ifdef __SDCC
119     i2c_open(I2C_MASTER, I2C_SLEW_ON, 5);
120     I2C_START();
121     i2c_writechar(addr);
122     i2c_writechar(reg);
123     I2C_STOP();
124     i2c_idle();
125 //   delay100tcy(2);
126     I2C_START();
127     i2c_writechar(addr | 1);
128     c = i2c_readchar();
129     I2C_ACK();
130     I2C_STOP();
131     i2c_close();
132 #else
133     OpenI2C(MASTER, SLEW_ON);
134     SSPADD = 0x5;
135     StartI2C();
136     while(SSPCON2bits.SEN);
137     WriteI2C(addr);
138   //  delay1ktcy(8);
139     while(SSPCON2bits.SEN);
140     WriteI2C(reg);
141   //  delay1ktcy(8);
142     StopI2C();
143     IdleI2C();
144     StartI2C();
145     WriteI2C(addr | 1);
146     if (!SSPCON2bits.ACKSTAT){
147       SSPCON2bits.RCEN = 1;
148       while(SSPCON2bits.RCEN);
149       NotAckI2C();
150       while (SSPCON2bits.ACKEN);
151       StopI2C();
152       while (SSPCON2bits.PEN);
153     }
154     c = SSPBUF;
155   //  delay1ktcy(8);
156  //   delay1ktcy(8);
157     CloseI2C();
158 #endif
159     //    CLOSEASMASTER();
160     return c;
161
162 }
163
164 #endif