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