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