Rev 196 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
196 | killagreg | 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** |
1 | ingob | 2 | * File Name : 91x_i2c.c |
3 | * Author : MCD Application Team |
||
196 | killagreg | 4 | * Version : V2.1 |
5 | * Date : 12/22/2008 |
||
6 | * Description : This file provides all the I2C firmware functions. |
||
1 | ingob | 7 | ******************************************************************************** |
8 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
||
9 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. |
||
10 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, |
||
11 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE |
||
12 | * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING |
||
13 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
||
14 | *******************************************************************************/ |
||
15 | |||
16 | /* Includes ------------------------------------------------------------------*/ |
||
17 | #include "91x_i2c.h" |
||
18 | #include "91x_scu.h" |
||
19 | |||
20 | /* Private typedef -----------------------------------------------------------*/ |
||
21 | /* Private define ------------------------------------------------------------*/ |
||
22 | |||
23 | /* I2C IT enable */ |
||
24 | #define I2C_IT_Enable 0x01 |
||
25 | #define I2C_IT_Disable 0xFE |
||
26 | |||
27 | /* I2C Peripheral Enable/Disable */ |
||
28 | #define I2C_PE_Set 0x20 |
||
29 | #define I2C_PE_Reset 0xDF |
||
30 | |||
31 | /* Address direction bit */ |
||
32 | #define I2C_ADD0_Set 0x01 |
||
33 | #define I2C_ADD0_Reset 0xFE |
||
34 | |||
35 | /* I2C START Enable/Disable */ |
||
36 | #define I2C_Start_Enable 0x08 |
||
37 | #define I2C_Start_Disable 0xF7 |
||
38 | |||
39 | /* I2C STOP Enable/Disable */ |
||
40 | #define I2C_Stop_Enable 0x02 |
||
41 | #define I2C_Stop_Disable 0xFD |
||
42 | |||
43 | /* I2C Masks */ |
||
44 | #define I2C_Frequency_Mask 0x1F |
||
45 | #define I2C_AddressHigh_Mask 0xF9 |
||
46 | #define I2C_OwnAddress_Mask 0x0300 |
||
47 | #define I2C_StandardMode_Mask 0x7f |
||
48 | #define I2C_FastMode_Mask 0x80 |
||
49 | #define I2C_Event_Mask 0x3FFF |
||
50 | #define I2C_HeaderSet_Mask 0xF1 |
||
51 | #define I2C_HeaderReset_Mask 0xFE |
||
52 | |||
53 | /* Private macro -------------------------------------------------------------*/ |
||
54 | /* Private variables ---------------------------------------------------------*/ |
||
55 | /* Private function prototypes -----------------------------------------------*/ |
||
56 | /* Private functions ---------------------------------------------------------*/ |
||
57 | /******************************************************************************* |
||
58 | * Function Name : I2C_DeInit |
||
59 | * Description : Deinitializes the I2C peripheral registers to their default |
||
60 | * reset values. |
||
61 | * Input :- I2Cx: I2C peripheral can be: |
||
62 | * - I2C0 |
||
63 | * - I2C1 |
||
64 | * Output : None |
||
65 | * Return : None |
||
66 | *******************************************************************************/ |
||
67 | void I2C_DeInit(I2C_TypeDef* I2Cx) |
||
68 | { |
||
69 | if (I2Cx == I2C0) |
||
70 | { |
||
71 | /* Reset the I2C0 registers values */ |
||
72 | SCU_APBPeriphReset(__I2C0, ENABLE); |
||
73 | SCU_APBPeriphReset(__I2C0, DISABLE); |
||
74 | } |
||
75 | if (I2Cx == I2C1) |
||
76 | { |
||
77 | /* Reset the I2C1 registers values */ |
||
78 | SCU_APBPeriphReset(__I2C1, ENABLE); |
||
79 | SCU_APBPeriphReset(__I2C1, DISABLE); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /******************************************************************************* |
||
84 | * Function Name : I2C_Init |
||
85 | * Description : Initializes the I2C peripheral according to the specified |
||
86 | * parameters in the I2C_InitTypeDef structure. |
||
87 | * Input :- I2Cx: I2C peripheral can be: |
||
88 | * - I2C0 |
||
89 | * - I2C1 |
||
90 | * |
||
91 | * - I2C_InitStruct: pointer to an I2C_InitTypeDef structure that |
||
92 | * contains the configuration information for the specified I2C |
||
93 | * peripheral. |
||
94 | * Output : None |
||
95 | * Return : None |
||
96 | *******************************************************************************/ |
||
97 | void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct) |
||
98 | { |
||
99 | u16 wResult = 0x0F; |
||
100 | u32 dPCLK = 25000000; |
||
101 | |||
102 | /* Get PCLK frequency value */ |
||
103 | dPCLK = SCU_GetPCLKFreqValue()*1000; |
||
104 | /* Disable I2C peripheral to set FR[2:0] bits */ |
||
105 | I2C_Cmd (I2Cx, DISABLE); |
||
106 | /* Clear frequency FR[2:0] bits */ |
||
107 | I2Cx->OAR2 &= I2C_Frequency_Mask; |
||
108 | /* Set frequency bits depending on PCLK value */ |
||
109 | if ((dPCLK <1667000) & (dPCLK > 10000000)) |
||
110 | I2Cx->OAR2 |= 0x20; |
||
111 | else if (dPCLK < 26670000) |
||
112 | I2Cx->OAR2 |= 0x40; |
||
113 | else if (dPCLK < 40000000) |
||
114 | I2Cx->OAR2 |= 0x60; |
||
115 | else if (dPCLK < 53330000) |
||
116 | I2Cx->OAR2 |= 0x80; |
||
117 | else if (dPCLK < 66000000) |
||
118 | I2Cx->OAR2 |= 0xA0; |
||
119 | else if (dPCLK < 80000000) |
||
120 | I2Cx->OAR2 |= 0xC0; |
||
121 | else if (dPCLK < 100000000) |
||
122 | I2Cx->OAR2 |= 0xE0; |
||
123 | I2C_Cmd (I2Cx, ENABLE); |
||
124 | |||
125 | /* Configure general call */ |
||
126 | if (I2C_InitStruct->I2C_GeneralCall == I2C_GeneralCall_Enable) |
||
127 | { |
||
128 | /* Enable general call */ |
||
129 | I2Cx->CR |= I2C_GeneralCall_Enable; |
||
130 | } |
||
131 | else |
||
132 | { |
||
133 | /* Disable general call */ |
||
134 | I2Cx->CR &= I2C_GeneralCall_Disable; |
||
135 | } |
||
136 | /* Configure acknowledgement */ |
||
137 | if (I2C_InitStruct->I2C_Ack == I2C_Ack_Enable) |
||
138 | { |
||
139 | /* Enable acknowledgement */ |
||
140 | I2Cx->CR |= I2C_Ack_Enable; |
||
141 | } |
||
142 | else |
||
143 | { |
||
144 | /* Disable acknowledgement */ |
||
145 | I2Cx->CR &= I2C_Ack_Disable; |
||
146 | } |
||
147 | |||
148 | /* Configure LSB own address */ |
||
149 | I2Cx->OAR1 = I2C_InitStruct->I2C_OwnAddress; |
||
150 | /* Clear MSB own address ADD[9:8] bits */ |
||
151 | I2Cx->OAR2 &= I2C_AddressHigh_Mask; |
||
152 | /* Set MSB own address value */ |
||
153 | I2Cx->OAR2 |= (I2C_InitStruct->I2C_OwnAddress & I2C_OwnAddress_Mask)>>7; |
||
154 | |||
155 | /* Configure speed in standard mode */ |
||
156 | if (I2C_InitStruct->I2C_CLKSpeed <= 100000) |
||
157 | { |
||
158 | /* Standard mode speed calculate */ |
||
159 | wResult = ((dPCLK/I2C_InitStruct->I2C_CLKSpeed)-7)/2; |
||
160 | /* Set speed value and clear FM/SM bit for standard mode in LSB clock divider */ |
||
161 | I2Cx->CCR = wResult & I2C_StandardMode_Mask; |
||
162 | } |
||
163 | /* Configure speed in fast mode */ |
||
164 | else if (I2C_InitStruct->I2C_CLKSpeed <= 400000) |
||
165 | { |
||
166 | /* Fast mode speed calculate */ |
||
167 | wResult = ((dPCLK/I2C_InitStruct->I2C_CLKSpeed)-9)/3; |
||
168 | /* Set speed value and set FM/SM bit for fast mode in LSB clock divider */ |
||
169 | I2Cx->CCR = wResult | I2C_FastMode_Mask; |
||
170 | } |
||
171 | /* Set speed in MSB clock divider */ |
||
172 | I2Cx->ECCR = wResult >>7; |
||
173 | } |
||
174 | |||
175 | /******************************************************************************* |
||
176 | * Function Name : I2C_StructInit |
||
177 | * Description : Initialize the I2C Init Structure parameters |
||
178 | * Input : - I2C_InitStruct: pointer to an I2C_InitTypeDef structure |
||
179 | which will be initialized. |
||
180 | * Output : None |
||
181 | * Return : None. |
||
182 | *******************************************************************************/ |
||
183 | void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct) |
||
184 | { |
||
185 | /* Initialize the I2C_CLKSpeed member */ |
||
186 | I2C_InitStruct->I2C_CLKSpeed = 5000; |
||
187 | |||
188 | /* Initialize the I2C_OwnAddress member */ |
||
189 | I2C_InitStruct->I2C_OwnAddress = 0x0; |
||
190 | |||
191 | /* Initialize the I2C_GeneralCall member */ |
||
192 | I2C_InitStruct->I2C_GeneralCall = I2C_GeneralCall_Disable; |
||
193 | |||
194 | /* Initialize the I2C_Ack member */ |
||
195 | I2C_InitStruct->I2C_Ack = I2C_Ack_Disable; |
||
196 | } |
||
197 | |||
198 | /******************************************************************************* |
||
199 | * Function Name : I2C_Cmd |
||
200 | * Description : Enables or disables the specified I2C peripheral. |
||
201 | * Input :- I2Cx: I2C peripheral can be: |
||
202 | * - I2C0 |
||
203 | * - I2C1 |
||
204 | * - NewState: new state of the I2C peripheral. This parameter |
||
205 | * can be: ENABLE or DISABLE. |
||
206 | * Output : None |
||
207 | * Return : None. |
||
208 | *******************************************************************************/ |
||
209 | void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState) |
||
210 | { |
||
211 | if (NewState == ENABLE) |
||
212 | { |
||
213 | /* Enable the I2C peripheral by setting twice the PE bit on the CR register */ |
||
214 | I2Cx->CR |= I2C_PE_Set; |
||
215 | I2Cx->CR |= I2C_PE_Set; |
||
216 | } |
||
217 | else |
||
218 | { |
||
219 | /* Disable the I2C peripheral */ |
||
220 | I2Cx->CR &= I2C_PE_Reset; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /******************************************************************************* |
||
225 | * Function Name : I2C_GenerateSTART |
||
226 | * Description : Generates I2C communication START condition. |
||
227 | * Input :- I2Cx: I2C peripheral can be: |
||
228 | * - I2C0 |
||
229 | * - I2C1 |
||
230 | * |
||
231 | * - NewState: new state of the Start condition. This parameter |
||
232 | * can be: ENABLE or DISABLE. |
||
233 | * Output : None |
||
234 | * Return : None. |
||
235 | *******************************************************************************/ |
||
236 | void I2C_GenerateStart(I2C_TypeDef* I2Cx, FunctionalState NewState) |
||
237 | { |
||
238 | if (NewState == ENABLE) |
||
239 | { |
||
240 | /* Generate a START condition */ |
||
241 | I2Cx->CR |= I2C_Start_Enable; |
||
242 | } |
||
243 | else |
||
244 | { |
||
245 | /* Disable the START condition generation */ |
||
246 | I2Cx->CR &= I2C_Start_Disable; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /******************************************************************************* |
||
251 | * Function Name : I2C_GenerateSTOP |
||
252 | * Description : Generates I2C communication STOP condition. |
||
253 | * Input :- I2Cx: I2C peripheral can be: |
||
254 | * - I2C0 |
||
255 | * - I2C1 |
||
256 | * |
||
257 | * - NewState: new state of the Stop condition. This parameter |
||
258 | * can be: ENABLE or DISABLE. |
||
259 | * Output : None |
||
260 | * Return : None. |
||
261 | *******************************************************************************/ |
||
262 | void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState) |
||
263 | { |
||
264 | if (NewState == ENABLE) |
||
265 | { |
||
266 | /* Generate a SIOP condition */ |
||
267 | I2Cx->CR |= I2C_Stop_Enable; |
||
268 | } |
||
269 | else |
||
270 | { |
||
271 | /* Disable the STOP condition generation */ |
||
272 | I2Cx->CR &= I2C_Stop_Disable; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /******************************************************************************* |
||
277 | * Function Name : I2C_AcknowledgeConfig |
||
278 | * Description : Enables or disables I2C acknowledge feature. |
||
279 | * Input :- I2Cx: I2C peripheral can be: |
||
280 | * - I2C0 |
||
281 | * - I2C1 |
||
282 | * - NewState: new state of the Acknowledgement. This parameter |
||
283 | * can be: ENABLE or DISABLE. |
||
284 | * Output : None |
||
285 | * Return : None. |
||
286 | *******************************************************************************/ |
||
287 | void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState) |
||
288 | { |
||
289 | if (NewState == ENABLE) |
||
290 | { |
||
291 | /* Enable the acknowledgement */ |
||
292 | I2Cx->CR |= I2C_Ack_Enable; |
||
293 | } |
||
294 | else |
||
295 | { |
||
296 | /* Disable the acknowledgement */ |
||
297 | I2Cx->CR &= I2C_Ack_Disable; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /******************************************************************************* |
||
302 | * Function Name : I2C_ITConfig |
||
303 | * Description : Enables or disables I2C interrupt feature. |
||
304 | * Input :- I2Cx: I2C peripheral can be: |
||
305 | * - I2C0 |
||
306 | * - I2C1 |
||
307 | * - NewState: new state of the specified I2C interrupt. |
||
308 | * This parameter can be: ENABLE or DISABLE. |
||
309 | * Output : None |
||
310 | * Return : None. |
||
311 | *******************************************************************************/ |
||
312 | void I2C_ITConfig(I2C_TypeDef *I2Cx, FunctionalState NewState) |
||
313 | { |
||
314 | if (NewState == ENABLE) |
||
315 | { |
||
316 | /* Enable the I2C interrupt */ |
||
317 | I2Cx->CR |= I2C_IT_Enable; |
||
318 | } |
||
319 | else |
||
320 | { |
||
321 | /* Disable the I2C interrupt */ |
||
322 | I2Cx->CR &= I2C_IT_Disable; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /******************************************************************************* |
||
327 | * Function Name : I2C_ReadRegister |
||
328 | * Description : Reads any I2C register and returns its value. |
||
329 | * Input :- I2Cx: I2C peripheral can be: |
||
330 | * - I2C0 |
||
331 | * - I2C1 |
||
332 | * - I2C_Register: the I2C register to be read. This parameter |
||
333 | * can be one of the following values: |
||
334 | * - I2C_CR: CR register. |
||
335 | * - I2C_SR1: SR1 register. |
||
336 | * - I2C_SR2: SR2 register. |
||
337 | * - I2C_CCR: CCR register. |
||
338 | * - I2C_OAR1: OAR1 register. |
||
339 | * - I2C_OAR2: OAR2 register. |
||
340 | * - I2C_DR: DR register. |
||
341 | * - I2C_ECCR: ECCR register. |
||
342 | * Output : None |
||
343 | * Return : The value of the register passed as parameter |
||
344 | *******************************************************************************/ |
||
345 | u8 I2C_ReadRegister(I2C_TypeDef* I2Cx, u8 I2C_Register) |
||
346 | { |
||
347 | /* Return the selected register value */ |
||
348 | if (I2Cx == I2C0) |
||
349 | { |
||
350 | return (*(u8 *)(I2C0_BASE + I2C_Register)); |
||
351 | } |
||
352 | if (I2Cx == I2C1) |
||
353 | { |
||
354 | return (*(u8 *)(I2C1_BASE + I2C_Register)); |
||
355 | } |
||
356 | return 0; |
||
357 | } |
||
358 | |||
359 | /******************************************************************************* |
||
360 | * Function Name : I2C_GetFlagStatus |
||
361 | * Description : Checks whether the specified I2C flag is set or not. |
||
362 | * Input :- I2Cx: I2C peripheral can be: |
||
363 | * - I2C0 |
||
364 | * - I2C1 |
||
365 | * - I2C_FLAG: flag to check. This parameter can be one of the |
||
366 | * following values: |
||
367 | * - I2C_FLAG_SB: Start bit flag |
||
368 | * - I2C_FLAG_M_SL: Master/Slave flag |
||
369 | * - I2C_FLAG_ADSL: Adress matched flag |
||
370 | * - I2C_FLAG_BTF: Byte transfer finished flag |
||
371 | * - I2C_FLAG_BUSY: Bus busy flag |
||
372 | * - I2C_FLAG_TRA: Transmitter/Receiver flag |
||
373 | * - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag |
||
374 | * - I2C_FLAG_EVF: Event flag |
||
375 | * - I2C_FLAG_GCAL: General call flag |
||
376 | * - I2C_FLAG_BERR: Bus error flag |
||
377 | * - I2C_FLAG_ARLO: Arbitration lost flag |
||
378 | * - I2C_FLAG_STOPF: Stop detection flag |
||
379 | * - I2C_FLAG_AF: Acknowledge failure flag |
||
380 | * - I2C_FLAG_ENDAD: End of address transmission flag |
||
381 | * - I2C_FLAG_ACK: Acknowledge enable flag |
||
382 | * Output : None |
||
383 | * Return : The NewState of the I2C_Flag (SET or RESET). |
||
384 | *******************************************************************************/ |
||
385 | FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u16 I2C_FLAG) |
||
386 | { |
||
387 | u16 wFlag1=0, wFlag2=0, wTmp=0; |
||
388 | |||
389 | wFlag1 = I2Cx->SR2; |
||
390 | wFlag1 = wFlag1<<8; |
||
391 | wFlag2 = I2Cx->CR & 0x04; |
||
392 | |||
393 | /* Get all the I2C flags in a unique register*/ |
||
394 | wTmp = (((I2Cx->SR1 | (wFlag1)) & I2C_Event_Mask) | (wFlag2<<12)); |
||
395 | |||
396 | /* Check the status of the specified I2C flag */ |
||
397 | if((wTmp & I2C_FLAG) != RESET) |
||
398 | { |
||
399 | /* Return SET if I2C_FLAG is set */ |
||
400 | return SET; |
||
401 | } |
||
402 | else |
||
403 | { |
||
404 | /* Return RESET if I2C_FLAG is reset */ |
||
405 | return RESET; |
||
406 | } |
||
407 | } |
||
408 | |||
409 | /******************************************************************************* |
||
410 | * Function Name : I2C_ClearFlag |
||
411 | * Description : Clears the I2C Flag passed as a parameter |
||
412 | * Input :- I2Cx: I2C peripheral can be: |
||
413 | * - I2C0 |
||
414 | * - I2C1 |
||
415 | * - I2C_FLAG: flag to check. This parameter can be one of the |
||
416 | * following values: |
||
417 | * - I2C_FLAG_SB: Start bit flag |
||
418 | * - I2C_FLAG_M_SL: Master/Slave flag |
||
419 | * - I2C_FLAG_ADSL: Adress matched flag |
||
420 | * - I2C_FLAG_BTF: Byte transfer finished flag |
||
421 | * - I2C_FLAG_BUSY: Bus busy flag |
||
422 | * - I2C_FLAG_TRA: Transmitter/Receiver flag |
||
423 | * - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag |
||
424 | * - I2C_FLAG_EVF: Event flag |
||
425 | * - I2C_FLAG_GCAL: General call flag |
||
426 | * - I2C_FLAG_BERR: Bus error flag |
||
427 | * - I2C_FLAG_ARLO: Arbitration lost flag |
||
428 | * - I2C_FLAG_STOPF: Stop detection flag |
||
429 | * - I2C_FLAG_AF: Acknowledge failure flag |
||
430 | * - I2C_FLAG_ENDAD: End of address transmission flag |
||
431 | * - I2C_FLAG_ACK: Acknowledge enable flag |
||
432 | * - parameter needed in the case that the flag to be cleared |
||
433 | * need a write in one register |
||
434 | * Output : None |
||
435 | * Return : None. |
||
436 | *******************************************************************************/ |
||
437 | void I2C_ClearFlag(I2C_TypeDef* I2Cx, u16 I2C_FLAG, ...) |
||
438 | { |
||
439 | u8 bTmp = (u8)*((u32 *) & I2C_FLAG + sizeof(I2C_FLAG)); |
||
440 | |||
441 | /* flags that need a read of the SR2 register to be cleared */ |
||
442 | if ((I2C_FLAG==I2C_FLAG_ADD10) || (I2C_FLAG==I2C_FLAG_EVF) || (I2C_FLAG==I2C_FLAG_BERR) || (I2C_FLAG==I2C_FLAG_ARLO) | |
||
443 | (I2C_FLAG==I2C_FLAG_STOPF) ||(I2C_FLAG==I2C_FLAG_AF) || (I2C_FLAG==I2C_FLAG_ENDAD)) |
||
444 | { |
||
445 | /* Read the SR2 register */ |
||
446 | I2Cx->SR2; |
||
447 | |||
448 | /* Two flags need a second step to be cleared */ |
||
449 | switch (I2C_FLAG) |
||
450 | { |
||
451 | case I2C_FLAG_ADD10: |
||
452 | /* Send the MSB 10bit address passed as second parameter */ |
||
453 | I2Cx->DR = bTmp; |
||
454 | break; |
||
455 | case I2C_FLAG_ENDAD: |
||
456 | /* Write to the I2C_CR register by setting PE bit */ |
||
457 | I2Cx->CR |= I2C_PE_Set; |
||
458 | break; |
||
459 | } |
||
460 | } |
||
461 | |||
462 | /* flags that need a read of the SR1 register to be cleared */ |
||
463 | else if (I2C_FLAG==I2C_FLAG_SB || I2C_FLAG==I2C_FLAG_ADSL || I2C_FLAG==I2C_FLAG_BTF || I2C_FLAG==I2C_FLAG_TRA) |
||
464 | { |
||
465 | /* Read the SR1 register */ |
||
466 | (void)I2Cx->SR1; |
||
467 | |||
468 | /* three flags need a second step to be cleared */ |
||
469 | if (I2C_FLAG == I2C_FLAG_SB) |
||
470 | { |
||
471 | /* Send the address byte passed as second parameter */ |
||
472 | I2Cx->DR = bTmp; |
||
473 | } |
||
474 | else if (I2C_FLAG==I2C_FLAG_BTF || I2C_FLAG==I2C_FLAG_TRA) |
||
475 | { |
||
476 | /* return the received byte in the variable passed as second parameter */ |
||
477 | bTmp=I2Cx->DR; |
||
478 | } |
||
479 | } |
||
480 | |||
481 | /* flags that need to disable the I2C interface */ |
||
482 | else if ( I2C_FLAG==I2C_FLAG_M_SL || I2C_FLAG==I2C_FLAG_GCAL) |
||
483 | { |
||
484 | I2C_Cmd(I2Cx, DISABLE); |
||
485 | I2C_Cmd(I2Cx, ENABLE); |
||
486 | } |
||
487 | } |
||
488 | |||
489 | /******************************************************************************* |
||
490 | * Function Name : I2C_Send7bitAddress |
||
491 | * Description : Transmits the address byte to select the slave device. |
||
492 | * Input :- I2Cx: I2C peripheral can be: |
||
493 | * - I2C0 |
||
494 | * - I2C1 |
||
495 | * - Address: specifies the slave address which will be transmitted |
||
496 | * - Direction: specifies whether the I2C device will be a |
||
497 | * Transmitter or a Receiver. This parameter can be one of the |
||
498 | * following values |
||
499 | * - I2C_MODE_TRANSMITTER: Transmitter mode |
||
500 | * - I2C_MODE_RECEIVER: Receiver mode |
||
501 | * Output : None |
||
502 | * Return : None. |
||
503 | *******************************************************************************/ |
||
504 | void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, u8 Address, u8 Direction) |
||
505 | { |
||
506 | /* Test on the direction to define the read/write bit */ |
||
507 | if (Direction == I2C_MODE_RECEIVER) |
||
508 | { |
||
509 | /* Set the address bit0 for read */ |
||
510 | Address |= I2C_ADD0_Set; |
||
511 | } |
||
512 | else |
||
513 | { |
||
514 | /* Reset the address bit0 for write */ |
||
515 | Address &= I2C_ADD0_Reset; |
||
516 | } |
||
517 | /* Send the address */ |
||
518 | I2Cx->DR = Address; |
||
519 | } |
||
520 | |||
521 | /******************************************************************************* |
||
522 | * Function Name : I2C_SendData |
||
523 | * Description : Send a data byte. |
||
524 | * Input :- I2Cx: I2C peripheral can be: |
||
525 | * - I2C0 |
||
526 | * - I2C1 |
||
527 | * - bData : the byte to be sent |
||
528 | * Output : None |
||
529 | * Return : None. |
||
530 | *******************************************************************************/ |
||
531 | void I2C_SendData(I2C_TypeDef* I2Cx, u8 bData) |
||
532 | { |
||
533 | /* Write in the DR register the byte to be sent */ |
||
534 | I2Cx->DR = bData; |
||
535 | } |
||
536 | |||
537 | /******************************************************************************* |
||
538 | * Function Name : I2C_ReceiveData |
||
539 | * Description : Read the received byte. |
||
540 | * Input : - I2Cx: I2C peripheral can be: |
||
541 | * - I2C0 |
||
542 | * - I2C1 |
||
543 | * Output : None |
||
544 | * Return : The received byte |
||
545 | *******************************************************************************/ |
||
546 | u8 I2C_ReceiveData(I2C_TypeDef* I2Cx) |
||
547 | { |
||
548 | /* Return from the DR register the received byte */ |
||
549 | return I2Cx->DR; |
||
550 | } |
||
551 | |||
552 | /******************************************************************************* |
||
553 | * Function Name : I2C_GetLastEvent |
||
554 | * Description : Get the Last happened I2C Event. |
||
555 | * Input :- I2Cx: I2C peripheral can be: |
||
556 | * - I2C0 |
||
557 | * - I2C1 |
||
558 | * Output : None |
||
559 | * Return : The Last happened Event. |
||
560 | *******************************************************************************/ |
||
561 | u16 I2C_GetLastEvent(I2C_TypeDef* I2Cx) |
||
562 | { |
||
563 | u16 wFlag1=0, wFlag2 =0, wLastEvent=0; |
||
564 | wFlag2 = I2Cx->SR1; |
||
565 | wFlag1 = I2Cx->SR2; |
||
566 | wFlag1 = wFlag1<<8; |
||
567 | /* Get the last event value from I2C status register */ |
||
568 | wLastEvent = ((( wFlag2 | (wFlag1)) & I2C_Event_Mask)); |
||
569 | /* Return the last event */ |
||
570 | return wLastEvent; |
||
571 | } |
||
572 | |||
573 | /******************************************************************************* |
||
574 | * Function Name : I2C_CheckEvent |
||
575 | * Description : Checks whether the Last I2C Event is equal to the one passed |
||
576 | * as parameter. |
||
577 | * Input :- I2Cx: I2C peripheral can be: |
||
578 | * - I2C0 |
||
579 | * - I2C1 |
||
580 | * - I2C_EVENT: the event to check. This parameter can be one of |
||
581 | * the following values: |
||
582 | * - I2C_EVENT_SLAVE_ADDRESS_MATCHED |
||
583 | * - I2C_EVENT_SLAVE_BYTE_RECEIVED |
||
584 | * - I2C_EVENT_SLAVE_BYTE_TRANSMITTED |
||
585 | * - I2C_EVENT_MASTER_MODE_SELECT |
||
586 | * - I2C_EVENT_MASTER_MODE_SELECTED |
||
587 | * - I2C_EVENT_MASTER_BYTE_RECEIVED |
||
588 | * - I2C_EVENT_MASTER_BYTE_TRANSMITTED |
||
589 | * - I2C_EVENT_MASTER_MODE_ADDRESS10 |
||
590 | * - I2C_EVENT_SLAVE_STOP_DETECTED |
||
591 | * - I2C_EVENT_SLAVE_ACK_FAILURE |
||
196 | killagreg | 592 | - I2C_EV31 |
1 | ingob | 593 | * Output : None |
594 | * Return : An ErrorStatus enumuration value: |
||
595 | * - SUCCESS: Last event is equal to the I2C_Event |
||
596 | * - ERROR: Last event is different from the I2C_Event |
||
597 | *******************************************************************************/ |
||
598 | ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx,u16 I2C_EVENT) |
||
599 | { |
||
600 | u16 wLastEvent = I2C_GetLastEvent(I2Cx); |
||
601 | |||
602 | /* Check whther the last event is equal to I2C_EVENT */ |
||
603 | if (wLastEvent == I2C_EVENT) |
||
604 | { |
||
605 | /* Return SUCCESS when last event is equal to I2C_EVENT */ |
||
606 | return SUCCESS; |
||
607 | } |
||
608 | else |
||
609 | { |
||
610 | /* Return ERROR when last event is different from I2C_EVENT */ |
||
611 | return ERROR; |
||
612 | } |
||
613 | } |
||
614 | |||
196 | killagreg | 615 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ |