Rev 248 | Rev 326 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 248 | Rev 252 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "i2c.h" |
59 | #include "i2c.h" |
60 | #include "uart1.h" |
60 | #include "uart1.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | #include "config.h" |
62 | #include "config.h" |
63 | #include "led.h" |
63 | #include "led.h" |
64 | 64 | ||
65 | 65 | ||
66 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
66 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
- | 67 | volatile u8 I2C_Error = I2C_ERROR_NOACK; // only one byte! |
|
67 | 68 | ||
68 | // number of bytes to send |
69 | // number of bytes to send |
69 | volatile u8 I2C_TxBufferSize; |
70 | volatile u8 I2C_TxBufferSize; |
70 | // number of bytes to receive |
71 | // number of bytes to receive |
71 | volatile u8 I2C_RxBufferSize; |
72 | volatile u8 I2C_RxBufferSize; |
72 | // the transfer buffer |
73 | // the transfer buffer |
73 | volatile u8 I2C_Buffer[I2C_BUFFER_LEN]; |
74 | volatile u8 I2C_Buffer[I2C_BUFFER_LEN]; |
74 | // the transfer direction |
75 | // the transfer direction |
75 | volatile u8 I2C_Direction; |
76 | volatile u8 I2C_Direction; |
76 | // the slave address |
77 | // the slave address |
77 | volatile u8 I2C_SlaveAddress = 0x00; |
78 | volatile u8 I2C_SlaveAddress = 0x00; |
78 | // function pointer to process the received bytes |
79 | // function pointer to process the received bytes |
79 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
80 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
80 | // goblal timeout |
81 | // goblal timeout |
81 | volatile u32 I2C1_Timeout = 0; |
82 | volatile u32 I2C1_Timeout = 0; |
82 | 83 | ||
83 | //-------------------------------------------------------------- |
84 | //-------------------------------------------------------------- |
84 | void I2C1_Init(void) |
85 | void I2C1_Init(void) |
85 | { |
86 | { |
86 | I2C_InitTypeDef I2C_Struct; |
87 | I2C_InitTypeDef I2C_Struct; |
87 | GPIO_InitTypeDef GPIO_InitStructure; |
88 | GPIO_InitTypeDef GPIO_InitStructure; |
88 | 89 | ||
89 | I2C_State = I2C_STATE_OFF; |
90 | I2C_State = I2C_STATE_OFF; |
90 | 91 | ||
91 | UART1_PutString("\r\n I2C init..."); |
92 | UART1_PutString("\r\n I2C init..."); |
92 | // enable Port 2 peripherie |
93 | // enable Port 2 peripherie |
93 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
94 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
94 | // disable a reset state |
95 | // disable a reset state |
95 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
96 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
96 | 97 | ||
97 | // free a busy bus |
98 | // free a busy bus |
98 | 99 | ||
99 | // At switch on I2C devices can get in a state where they |
100 | // At switch on I2C devices can get in a state where they |
100 | // are still waiting for a command due to all the bus lines bouncing |
101 | // are still waiting for a command due to all the bus lines bouncing |
101 | // around at startup have started clocking data into the device(s). |
102 | // around at startup have started clocking data into the device(s). |
102 | // Enable the ports as open collector port outputs |
103 | // Enable the ports as open collector port outputs |
103 | // and clock out at least 9 SCL pulses, then generate a stop |
104 | // and clock out at least 9 SCL pulses, then generate a stop |
104 | // condition and then leave the clock line high. |
105 | // condition and then leave the clock line high. |
105 | 106 | ||
106 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
107 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
107 | GPIO_StructInit(&GPIO_InitStructure); |
108 | GPIO_StructInit(&GPIO_InitStructure); |
108 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
109 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
109 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
110 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
110 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
111 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
111 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
112 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
112 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
113 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
113 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
114 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
114 | 115 | ||
115 | u8 i; |
116 | u8 i; |
116 | u32 delay; |
117 | u32 delay; |
117 | // set SCL high and then SDA to low (start condition) |
118 | // set SCL high and then SDA to low (start condition) |
118 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
119 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
119 | delay = SetDelay(1); |
120 | delay = SetDelay(1); |
120 | while (!CheckDelay(delay)); |
121 | while (!CheckDelay(delay)); |
121 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
122 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
122 | // toggle SCL at least 10 times from high to low to high |
123 | // toggle SCL at least 10 times from high to low to high |
123 | for(i = 0; i < 10; i++) |
124 | for(i = 0; i < 10; i++) |
124 | { |
125 | { |
125 | delay = SetDelay(1); |
126 | delay = SetDelay(1); |
126 | while (!CheckDelay(delay)); |
127 | while (!CheckDelay(delay)); |
127 | 128 | ||
128 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
129 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
129 | delay = SetDelay(1); |
130 | delay = SetDelay(1); |
130 | while (!CheckDelay(delay)); |
131 | while (!CheckDelay(delay)); |
131 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
132 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
132 | } |
133 | } |
133 | delay = SetDelay(1); |
134 | delay = SetDelay(1); |
134 | while (!CheckDelay(delay)); |
135 | while (!CheckDelay(delay)); |
135 | // create stop condition setting SDA HIGH when SCL is HIGH |
136 | // create stop condition setting SDA HIGH when SCL is HIGH |
136 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
137 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
137 | 138 | ||
138 | 139 | ||
139 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
140 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
140 | GPIO_StructInit(&GPIO_InitStructure); |
141 | GPIO_StructInit(&GPIO_InitStructure); |
141 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
142 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
142 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
143 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
143 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
144 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
144 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable; |
145 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable; |
145 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
146 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
146 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
147 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
147 | 148 | ||
148 | // enable I2C peripherie |
149 | // enable I2C peripherie |
149 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
150 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
150 | // reset I2C peripherie |
151 | // reset I2C peripherie |
151 | SCU_APBPeriphReset(__I2C1,ENABLE); |
152 | SCU_APBPeriphReset(__I2C1,ENABLE); |
152 | SCU_APBPeriphReset(__I2C1,DISABLE); |
153 | SCU_APBPeriphReset(__I2C1,DISABLE); |
153 | 154 | ||
154 | I2C_DeInit(I2C1); |
155 | I2C_DeInit(I2C1); |
155 | I2C_StructInit(&I2C_Struct); |
156 | I2C_StructInit(&I2C_Struct); |
156 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
157 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
157 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
158 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
158 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
159 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
159 | I2C_Struct.I2C_OwnAddress = 0x00; |
160 | I2C_Struct.I2C_OwnAddress = 0x00; |
160 | I2C_Init(I2C1, &I2C_Struct); |
161 | I2C_Init(I2C1, &I2C_Struct); |
161 | 162 | ||
162 | // empty rx and tx buffer counters |
163 | // empty rx and tx buffer counters |
163 | I2C_TxBufferSize = 0; |
164 | I2C_TxBufferSize = 0; |
164 | I2C_RxBufferSize = 0; |
165 | I2C_RxBufferSize = 0; |
165 | 166 | ||
166 | I2C_Cmd(I2C1, ENABLE); |
167 | I2C_Cmd(I2C1, ENABLE); |
167 | I2C_ITConfig(I2C1, ENABLE); |
168 | I2C_ITConfig(I2C1, ENABLE); |
168 | 169 | ||
169 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
170 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
170 | 171 | ||
171 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
172 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
172 | I2C_GenerateSTOP(I2C1, ENABLE); |
173 | I2C_GenerateSTOP(I2C1, ENABLE); |
173 | I2C_State = I2C_STATE_IDLE; |
174 | I2C_State = I2C_STATE_IDLE; |
174 | 175 | ||
175 | // start some dummy transmissions cycles |
176 | // start some dummy transmissions cycles |
176 | // to get the irq routine to work |
177 | // to get the irq routine to work |
177 | for(i=0;i<10;i++) |
178 | for(i=0;i<10;i++) |
178 | { |
179 | { |
179 | I2C_State = I2C_STATE_BUFFBUSY; |
180 | I2C_State = I2C_STATE_BUFFBUSY; |
180 | I2C_Transmission(0,1,0,1); |
181 | I2C_Transmission(0,1,0,1); |
181 | if(I2C_WaitForEndOfTransmission(10)) break; |
182 | if(I2C_WaitForEndOfTransmission(10)) break; |
182 | UART1_Putchar('.'); |
183 | UART1_Putchar('.'); |
183 | } |
184 | } |
184 | UART1_PutString("ok"); |
185 | UART1_PutString("ok"); |
185 | } |
186 | } |
186 | 187 | ||
187 | 188 | ||
188 | //-------------------------------------------------------------- |
189 | //-------------------------------------------------------------- |
189 | void I2C1_Deinit(void) |
190 | void I2C1_Deinit(void) |
190 | { |
191 | { |
191 | GPIO_InitTypeDef GPIO_InitStructure; |
192 | GPIO_InitTypeDef GPIO_InitStructure; |
192 | UART1_PutString("\r\n I2C deinit..."); |
193 | UART1_PutString("\r\n I2C deinit..."); |
193 | I2C_GenerateStart(I2C1, DISABLE); |
194 | I2C_GenerateStart(I2C1, DISABLE); |
194 | I2C_GenerateSTOP(I2C1, ENABLE); |
195 | I2C_GenerateSTOP(I2C1, ENABLE); |
195 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
196 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
196 | I2C_State = I2C_STATE_OFF; |
197 | I2C_State = I2C_STATE_OFF; |
197 | I2C_ITConfig(I2C1, DISABLE); |
198 | I2C_ITConfig(I2C1, DISABLE); |
198 | I2C_Cmd(I2C1, DISABLE); |
199 | I2C_Cmd(I2C1, DISABLE); |
199 | I2C_DeInit(I2C1); |
200 | I2C_DeInit(I2C1); |
200 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
201 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
201 | 202 | ||
202 | // set ports to input |
203 | // set ports to input |
203 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
204 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
204 | GPIO_StructInit(&GPIO_InitStructure); |
205 | GPIO_StructInit(&GPIO_InitStructure); |
205 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
206 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
206 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
207 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
207 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
208 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
208 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
209 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
209 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
210 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
210 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
211 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
211 | 212 | ||
212 | // empty rx and tx buffer |
213 | // empty rx and tx buffer |
213 | I2C_TxBufferSize = 0; |
214 | I2C_TxBufferSize = 0; |
214 | I2C_RxBufferSize = 0; |
215 | I2C_RxBufferSize = 0; |
215 | 216 | ||
216 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
217 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
217 | 218 | ||
218 | UART1_PutString("ok"); |
219 | UART1_PutString("ok"); |
219 | } |
220 | } |
220 | 221 | ||
221 | //-------------------------------------------------------------- |
222 | //-------------------------------------------------------------- |
222 | void I2C1_IRQHandler(void) |
223 | void I2C1_IRQHandler(void) |
223 | { |
224 | { |
224 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
225 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
225 | u16 status; |
226 | u16 status; |
226 | 227 | ||
227 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
228 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
228 | // detemine I2C State |
229 | // detemine I2C State |
229 | status = I2C_GetLastEvent(I2C1); |
230 | status = I2C_GetLastEvent(I2C1); |
230 | 231 | ||
231 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
232 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
232 | { // Set and subsequently clear the STOP bit while BTF is set. |
233 | { // Set and subsequently clear the STOP bit while BTF is set. |
233 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
234 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
234 | { |
235 | { |
235 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
236 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
236 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
237 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
237 | } |
238 | } |
238 | I2C_State = I2C_STATE_IDLE; |
239 | I2C_State = I2C_STATE_IDLE; |
- | 240 | I2C_Error = I2C_ERROR_NOACK; |
|
239 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
241 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
240 | LED_GRN_OFF; |
242 | LED_GRN_OFF; |
241 | return; |
243 | return; |
242 | } |
244 | } |
243 | else |
245 | else |
244 | { // depending on current i2c state |
246 | { // depending on current i2c state |
245 | switch(status) |
247 | switch(status) |
246 | { |
248 | { |
247 | // the start condition was initiated on the bus |
249 | // the start condition was initiated on the bus |
248 | case I2C_EVENT_MASTER_MODE_SELECT: |
250 | case I2C_EVENT_MASTER_MODE_SELECT: |
249 | LED_GRN_ON; |
251 | LED_GRN_ON; |
250 | // update current bus state variable |
252 | // update current bus state variable |
251 | // jump to rx state if there is nothing to send |
253 | // jump to rx state if there is nothing to send |
252 | switch(I2C_Direction) |
254 | switch(I2C_Direction) |
253 | { |
255 | { |
254 | case I2C_MODE_TRANSMITTER: |
256 | case I2C_MODE_TRANSMITTER: |
255 | I2C_State = I2C_STATE_TX_PROGRESS; |
257 | I2C_State = I2C_STATE_TX_PROGRESS; |
256 | break; |
258 | break; |
257 | 259 | ||
258 | case I2C_MODE_RECEIVER: |
260 | case I2C_MODE_RECEIVER: |
259 | if (I2C_RxBufferSize == 0) // nothing to send? |
261 | if (I2C_RxBufferSize == 0) // nothing to send? |
260 | { |
262 | { |
261 | I2C_GenerateSTOP (I2C1, ENABLE); |
263 | I2C_GenerateSTOP (I2C1, ENABLE); |
262 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
264 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
263 | LED_GRN_OFF; |
265 | LED_GRN_OFF; |
264 | I2C_State = I2C_STATE_IDLE; |
266 | I2C_State = I2C_STATE_IDLE; |
- | 267 | I2C_Error = I2C_ERROR_NONE; |
|
265 | return; |
268 | return; |
266 | } |
269 | } |
267 | else |
270 | else |
268 | { |
271 | { |
269 | I2C_State = I2C_STATE_RX_PROGRESS; |
272 | I2C_State = I2C_STATE_RX_PROGRESS; |
270 | } |
273 | } |
271 | break; |
274 | break; |
272 | 275 | ||
273 | default: // invalid direction |
276 | default: // invalid direction |
274 | I2C_GenerateSTOP (I2C1, ENABLE); |
277 | I2C_GenerateSTOP (I2C1, ENABLE); |
275 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
278 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
276 | LED_GRN_OFF; |
279 | LED_GRN_OFF; |
277 | I2C_State = I2C_STATE_IDLE; |
280 | I2C_State = I2C_STATE_IDLE; |
- | 281 | I2C_Error = I2C_ERROR_UNKNOWN; |
|
278 | return; |
282 | return; |
279 | } |
283 | } |
280 | // enable acknowledge |
284 | // enable acknowledge |
281 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
285 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
282 | // send address/direction byte on the bus |
286 | // send address/direction byte on the bus |
283 | I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction); |
287 | I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction); |
284 | break; |
288 | break; |
285 | 289 | ||
286 | // the address byte was send |
290 | // the address byte was send |
287 | case I2C_EVENT_MASTER_MODE_SELECTED: |
291 | case I2C_EVENT_MASTER_MODE_SELECTED: |
288 | // Clear EV6 by set again the PE bit |
292 | // Clear EV6 by set again the PE bit |
289 | I2C_Cmd(I2C1, ENABLE); |
293 | I2C_Cmd(I2C1, ENABLE); |
290 | switch(I2C_State) |
294 | switch(I2C_State) |
291 | { |
295 | { |
292 | case I2C_STATE_TX_PROGRESS: |
296 | case I2C_STATE_TX_PROGRESS: |
293 | // send 1st data byte |
297 | // send 1st data byte |
294 | Tx_Idx = 0; |
298 | Tx_Idx = 0; |
295 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
299 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
296 | Tx_Idx++; |
300 | Tx_Idx++; |
297 | // reset timeout |
301 | // reset timeout |
298 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
302 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
299 | break; |
303 | break; |
300 | 304 | ||
301 | case I2C_STATE_RX_PROGRESS: |
305 | case I2C_STATE_RX_PROGRESS: |
302 | Rx_Idx = 0; |
306 | Rx_Idx = 0; |
303 | // disable acknoledge if only one byte has to be read |
307 | // disable acknoledge if only one byte has to be read |
304 | if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE); |
308 | if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE); |
305 | break; |
309 | break; |
306 | 310 | ||
307 | default: // unknown I2C state |
311 | default: // unknown I2C state |
308 | // should never happen |
312 | // should never happen |
309 | I2C_GenerateSTOP (I2C1, ENABLE); |
313 | I2C_GenerateSTOP (I2C1, ENABLE); |
310 | LED_GRN_OFF; |
314 | LED_GRN_OFF; |
311 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
315 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
312 | I2C_State = I2C_STATE_IDLE; |
316 | I2C_State = I2C_STATE_IDLE; |
- | 317 | I2C_Error = I2C_ERROR_UNKNOWN; |
|
313 | return; |
318 | return; |
314 | break; |
319 | break; |
315 | } |
320 | } |
316 | break; |
321 | break; |
317 | 322 | ||
318 | // the master has transmitted a byte and slave has been acknowledged |
323 | // the master has transmitted a byte and slave has been acknowledged |
319 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
324 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
320 | 325 | ||
321 | // some bytes have to be transmitted |
326 | // some bytes have to be transmitted |
322 | if(Tx_Idx < I2C_TxBufferSize) |
327 | if(Tx_Idx < I2C_TxBufferSize) |
323 | { |
328 | { |
324 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
329 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
325 | Tx_Idx++; |
330 | Tx_Idx++; |
326 | } |
331 | } |
327 | else // last byte was send |
332 | else // last byte was send |
328 | { |
333 | { |
329 | // generate stop or repeated start condition |
334 | // generate stop or repeated start condition |
330 | if (I2C_RxBufferSize > 0) // is any answer byte expected? |
335 | if (I2C_RxBufferSize > 0) // is any answer byte expected? |
331 | { |
336 | { |
332 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
337 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
333 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
338 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
334 | } |
339 | } |
335 | else |
340 | else |
336 | { // stop communication |
341 | { // stop communication |
337 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
342 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
338 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
343 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
339 | LED_GRN_OFF; |
344 | LED_GRN_OFF; |
340 | I2C_State = I2C_STATE_IDLE; // ready for new actions |
345 | I2C_State = I2C_STATE_IDLE; // ready for new actions |
- | 346 | I2C_Error = I2C_ERROR_NONE; |
|
341 | } |
347 | } |
342 | } |
348 | } |
343 | break; |
349 | break; |
344 | 350 | ||
345 | // the master has received a byte from the slave |
351 | // the master has received a byte from the slave |
346 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
352 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
347 | // some bytes have to be received |
353 | // some bytes have to be received |
348 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
354 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
349 | { // copy received byte from the data register to the rx-buffer |
355 | { // copy received byte from the data register to the rx-buffer |
350 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
356 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
351 | } |
357 | } |
352 | else // if the last byte was received |
358 | else // if the last byte was received |
353 | { |
359 | { |
354 | // generate a STOP condition on the bus before reading data register |
360 | // generate a STOP condition on the bus before reading data register |
355 | I2C_GenerateSTOP(I2C1, ENABLE); |
361 | I2C_GenerateSTOP(I2C1, ENABLE); |
356 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
362 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
357 | // call the rx handler function to process recieved data |
363 | // call the rx handler function to process recieved data |
358 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_Buffer, I2C_RxBufferSize); |
364 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_Buffer, I2C_RxBufferSize); |
359 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
365 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
360 | DebugOut.Analog[15]++; |
366 | DebugOut.Analog[15]++; |
361 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
367 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
362 | LED_GRN_OFF; |
368 | LED_GRN_OFF; |
363 | I2C_State = I2C_STATE_IDLE; |
369 | I2C_State = I2C_STATE_IDLE; |
- | 370 | I2C_Error = I2C_ERROR_NONE; |
|
364 | return; |
371 | return; |
365 | } |
372 | } |
366 | Rx_Idx++; |
373 | Rx_Idx++; |
367 | // if the 2nd last byte was received disable acknowledge for the last one |
374 | // if the 2nd last byte was received disable acknowledge for the last one |
368 | if ( (Rx_Idx + 1) == I2C_RxBufferSize ) |
375 | if ( (Rx_Idx + 1) == I2C_RxBufferSize ) |
369 | { |
376 | { |
370 | I2C_AcknowledgeConfig(I2C1, DISABLE); |
377 | I2C_AcknowledgeConfig(I2C1, DISABLE); |
371 | } |
378 | } |
372 | break; |
379 | break; |
373 | 380 | ||
374 | default:// unknown event |
381 | default:// unknown event |
375 | // should never happen |
382 | // should never happen |
376 | I2C_GenerateSTOP (I2C1, ENABLE); |
383 | I2C_GenerateSTOP (I2C1, ENABLE); |
377 | LED_GRN_OFF; |
384 | LED_GRN_OFF; |
378 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
385 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
379 | I2C_State = I2C_STATE_IDLE; |
386 | I2C_State = I2C_STATE_IDLE; |
- | 387 | I2C_Error = I2C_ERROR_UNKNOWN; |
|
380 | break; |
388 | break; |
381 | } |
389 | } |
382 | } |
390 | } |
383 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
391 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
384 | } |
392 | } |
385 | 393 | ||
386 | // ---------------------------------------------------------------------------------------- |
394 | // ---------------------------------------------------------------------------------------- |
387 | // wait for end of transmission |
395 | // wait for end of transmission |
388 | u8 I2C_WaitForEndOfTransmission(u32 timeout) |
396 | u8 I2C_WaitForEndOfTransmission(u32 timeout) |
389 | { |
397 | { |
390 | u32 time = SetDelay(timeout); |
398 | u32 time = SetDelay(timeout); |
391 | while(I2C_State != I2C_STATE_IDLE) |
399 | while(I2C_State != I2C_STATE_IDLE) |
392 | { |
400 | { |
393 | if(CheckDelay(time)) return(0); |
401 | if(CheckDelay(time)) return(0); |
394 | } |
402 | } |
395 | return(1); |
403 | return(1); |
396 | } |
404 | } |
397 | 405 | ||
398 | // ---------------------------------------------------------------------------------------- |
406 | // ---------------------------------------------------------------------------------------- |
399 | // try to get access to the transfer buffer within a timeout limit |
407 | // try to get access to the transfer buffer within a timeout limit |
400 | // returs 1 on success and 0 on error/timeout |
408 | // returs 1 on success and 0 on error/timeout |
401 | u8 I2C_LockBuffer(u32 timeout) |
409 | u8 I2C_LockBuffer(u32 timeout) |
402 | { |
410 | { |
403 | if(I2C_WaitForEndOfTransmission(timeout)) |
411 | if(I2C_WaitForEndOfTransmission(timeout)) |
404 | { |
412 | { |
405 | I2C_State = I2C_STATE_BUFFBUSY; |
413 | I2C_State = I2C_STATE_BUFFBUSY; |
- | 414 | I2C_Error = I2C_ERROR_UNKNOWN; |
|
406 | return(1); |
415 | return(1); |
407 | } |
416 | } |
408 | else return(0); |
417 | else return(0); |
409 | } |
418 | } |
410 | // ---------------------------------------------------------------------------------------- |
419 | // ---------------------------------------------------------------------------------------- |
411 | // initate an i2c transmission |
420 | // initate an i2c transmission |
412 | u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
421 | u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
413 | { |
422 | { |
414 | u8 retval = 0; |
423 | u8 retval = 0; |
415 | if(I2C_State == I2C_STATE_BUFFBUSY) |
424 | if(I2C_State == I2C_STATE_BUFFBUSY) |
416 | { |
425 | { |
417 | if((RxBytes > I2C_BUFFER_LEN) || (TxBytes > I2C_BUFFER_LEN)) |
426 | if((RxBytes > I2C_BUFFER_LEN) || (TxBytes > I2C_BUFFER_LEN)) |
418 | { |
427 | { |
419 | I2C_State = I2C_STATE_IDLE; |
428 | I2C_State = I2C_STATE_IDLE; |
420 | return(retval); |
429 | return(retval); |
421 | } |
430 | } |
422 | I2C_RxBufferSize = RxBytes; |
431 | I2C_RxBufferSize = RxBytes; |
423 | I2C_TxBufferSize = TxBytes; |
432 | I2C_TxBufferSize = TxBytes; |
424 | // set direction to master transmitter |
433 | // set direction to master transmitter |
425 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
434 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
426 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
435 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
427 | else // nothing to send or receive |
436 | else // nothing to send or receive |
428 | { |
437 | { |
429 | I2C_State = I2C_STATE_IDLE; |
438 | I2C_State = I2C_STATE_IDLE; |
- | 439 | I2C_Error = I2C_ERROR_NONE; |
|
430 | I2C_TxBufferSize = 0; |
440 | I2C_TxBufferSize = 0; |
431 | I2C_RxBufferSize = 0; |
441 | I2C_RxBufferSize = 0; |
432 | return(retval); |
442 | return(retval); |
433 | } |
443 | } |
434 | // update slave address and rx data handler funbction pointer |
444 | // update slave address and rx data handler funbction pointer |
435 | I2C_SlaveAddress = SlaveAddr; |
445 | I2C_SlaveAddress = SlaveAddr; |
436 | I2C_pRxHandler = pRxHandler; |
446 | I2C_pRxHandler = pRxHandler; |
437 | // test on busy flag and clear it |
447 | // test on busy flag and clear it |
438 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
448 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
439 | // enable I2C IRQ |
449 | // enable I2C IRQ |
440 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
450 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
441 | // initiate start condition on the bus |
451 | // initiate start condition on the bus |
442 | I2C_GenerateStart(I2C1, ENABLE); |
452 | I2C_GenerateStart(I2C1, ENABLE); |
443 | retval = 1; |
453 | retval = 1; |
444 | } |
454 | } |
445 | return(retval); |
455 | return(retval); |
446 | } |
456 | } |
447 | 457 |