1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/**
******************************************************************************
* @file HAL_i2c.h
* @author IC Applications Department
* @version V0.8
* @date 2019_08_02
* @brief This file contains all the functions prototypes for the I2C firmware
* library.
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, HOLOCENE SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2016 HOLOCENE</center></h2>
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __HAL_I2C_H
#define __HAL_I2C_H
/* Includes ------------------------------------------------------------------*/
#include "HAL_device.h"
/** @addtogroup StdPeriph_Driver
* @{
*/
/** @addtogroup I2C
* @{
*/
/** @defgroup I2C_Exported_Types
* @{
*/
/**
* @brief I2C Init structure definition
*/
/*
typedef struct
{
uint16_t I2C_Mode;
uint16_t I2C_DutyCycle;
uint16_t I2C_OwnAddress1;
uint16_t I2C_Ack;
uint16_t I2C_AcknowledgedAddress;
uint32_t I2C_ClockSpeed;
}I2C_InitTypeDef;
*/
typedef struct
{
uint16_t I2C_Mode;
uint16_t I2C_Speed;
uint16_t I2C_OwnAddress;
uint32_t I2C_ClockSpeed;
}I2C_InitTypeDef;
/**
* @}
*/
/** @defgroup I2C_Exported_Constants
* @{
*/
#define IS_I2C_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == I2C1_BASE) || \
((*(uint32_t*)&(PERIPH)) == I2C2_BASE))
/** @defgroup I2C_modes
* @{
*/
#define TX_EMPTY_CTRL (0x0001<<8)
#define IC_SLAVE_DISABLE (0x0001<<6)
#define IC_SLAVE_ENABLE (0x0000<<6)
#define IC_RESTART_EN (0x0001<<5)
#define IC_7BITADDR_MASTER (0x0000<<4)
#define IC_7BITADDR_SLAVE (0x0000<<3)
#define I2C_Speed_STANDARD ((uint16_t)0x0002)
#define I2C_Speed_FAST ((uint16_t)0x0004)
#define I2C_Mode_MASTER ((uint16_t)0x0001)
#define I2C_Mode_SLAVE ((uint16_t)0x0000)
#define TDMAE_SET ((uint16_t)0x0002)
#define RDMAE_SET ((uint16_t)0x0001)
#define CMD_READ ((uint16_t)0x0100)
#define CMD_WRITE ((uint16_t)0x0000)
#define I2C_Mode_I2C ((uint16_t)0x0000)
#define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C))
/**
* @}
*/
/** @defgroup I2C_transfer_direction
* @{
*/
#define I2C_Direction_Transmitter ((uint8_t)0x00)
#define I2C_Direction_Receiver ((uint8_t)0x01)
#define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
((DIRECTION) == I2C_Direction_Receiver))
/**
* @}
*/
/** @defgroup I2C_acknowledged_address_defines
* @{
*/
#define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
#define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
#define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
((ADDRESS) == I2C_AcknowledgedAddress_10bit))
/**
* @}
*/
/** @defgroup I2C_interrupts_definition
* @{
*/
#define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
/**
* @}
*/
/** @defgroup I2C_interrupts_definition
* @{
*/
#define I2C_IT_RX_UNDER ((uint16_t)0x0001)
#define I2C_IT_RX_OVER ((uint16_t)0x0002)
#define I2C_IT_RX_FULL ((uint16_t)0x0004)
#define I2C_IT_TX_OVER ((uint16_t)0x0008)
#define I2C_IT_TX_EMPTY ((uint16_t)0x0010)
#define I2C_IT_RD_REQ ((uint16_t)0x0020)
#define I2C_IT_TX_ABRT ((uint16_t)0x0040)
#define I2C_IT_RX_DONE ((uint16_t)0x0080)
#define I2C_IT_ACTIVITY ((uint16_t)0x0100)
#define I2C_IT_STOP_DET ((uint16_t)0x0200)
#define I2C_IT_START_DET ((uint16_t)0x0400)
#define I2C_IT_GEN_CALL ((uint16_t)0x0800)
#define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0xF000) == 0x00) && ((IT) != (uint16_t)0x00))
#define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_RX_UNDER) || ((IT) == I2C_IT_RX_OVER) || \
((IT) == I2C_IT_RX_FULL) || ((IT) == I2C_IT_TX_OVER) || \
((IT) == I2C_IT_TX_EMPTY) || ((IT) == I2C_IT_RD_REQ) || \
((IT) == I2C_IT_TX_ABRT) || ((IT) == I2C_IT_RX_DONE) || \
((IT) == I2C_IT_ACTIVITY) || ((IT) == I2C_IT_STOP_DET) || \
((IT) == I2C_IT_START_DET) || ((IT) == I2C_IT_GEN_CALL))
/**
* @}
*/
/** @defgroup I2C_flags_definition
* @{
*/
#define I2C_FLAG_RX_UNDER ((uint16_t)0x0001)
#define I2C_FLAG_RX_OVER ((uint16_t)0x0002)
#define I2C_FLAG_RX_FULL ((uint16_t)0x0004)
#define I2C_FLAG_TX_OVER ((uint16_t)0x0008)
#define I2C_FLAG_TX_EMPTY ((uint16_t)0x0010)
#define I2C_FLAG_RD_REQ ((uint16_t)0x0020)
#define I2C_FLAG_TX_ABRT ((uint16_t)0x0040)
#define I2C_FLAG_RX_DONE ((uint16_t)0x0080)
#define I2C_FLAG_ACTIVITY ((uint16_t)0x0100)
#define I2C_FLAG_STOP_DET ((uint16_t)0x0200)
#define I2C_FLAG_START_DET ((uint16_t)0x0400)
#define I2C_FLAG_GEN_CALL ((uint16_t)0x0800)
#define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xF000) == 0x00) && ((FLAG) != (uint16_t)0x00))
#define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_RX_UNDER) || ((FLAG) == I2C_FLAG_RX_OVER) || \
((FLAG) == I2C_FLAG_RX_FULL) || ((FLAG) == I2C_FLAG_TX_OVER) || \
((FLAG) == I2C_FLAG_TX_EMPTY) || ((FLAG) == I2C_FLAG_RD_REQ) || \
((FLAG) == I2C_FLAG_TX_ABRT) || ((FLAG) == I2C_FLAG_RX_DONE) || \
((FLAG) == I2C_FLAG_ACTIVITY) || ((FLAG) == I2C_FLAG_STOP_DET) || \
((FLAG) == I2C_FLAG_START_DET) || ((FLAG) == I2C_FLAG_GEN_CALL))
/** @defgroup I2C_Statusflags_definition
* @{
*/
#define I2C_STATUS_FLAG_ACTIVITY ((uint16_t)0x0001)
#define I2C_STATUS_FLAG_TFNF ((uint16_t)0x0002)
#define I2C_STATUS_FLAG_TFE ((uint16_t)0x0004)
#define I2C_STATUS_FLAG_RFNE ((uint16_t)0x0008)
#define I2C_STATUS_FLAG_RFF ((uint16_t)0x0010)
#define I2C_STATUS_FLAG_M_ACTIVITY ((uint16_t)0x0020)
#define I2C_STATUS_FLAG_S_ACTIVITY ((uint16_t)0x0040)
/**
* @}
*/
/** @defgroup I2C_Events
* @{
*/
#define I2C_EVENT_RX_UNDER ((uint32_t)0x0001)
#define I2C_EVENT_RX_OVER ((uint32_t)0x0002)
#define I2C_EVENT_RX_FULL ((uint32_t)0x0004)
#define I2C_EVENT_TX_OVER ((uint32_t)0x0008)
#define I2C_EVENT_TX_EMPTY ((uint32_t)0x0010)
#define I2C_EVENT_RD_REQ ((uint32_t)0x0020)
#define I2C_EVENT_TX_ABRT ((uint32_t)0x0040)
#define I2C_EVENT_RX_DONE ((uint32_t)0x0080)
#define I2C_EVENT_ACTIVITY ((uint32_t)0x0100)
#define I2C_EVENT_STOP_DET ((uint32_t)0x0200)
#define I2C_EVENT_START_DET ((uint32_t)0x0400)
#define I2C_EVENT_GEN_CALL ((uint32_t)0x0800)
#define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_RX_UNDER) || \
((EVENT) == I2C_EVENT_RX_OVER) || \
((EVENT) == I2C_EVENT_RX_FULL) || \
((EVENT) == I2C_EVENT_TX_OVER) || \
((EVENT) == I2C_EVENT_RD_REQ) || \
((EVENT) == I2C_EVENT_TX_ABRT) || \
((EVENT) == I2C_EVENT_RX_DONE) || \
((EVENT) == (I2C_EVENT_ACTIVITY | I2C_EVENT_STOP_DET)) || \
((EVENT) == (I2C_EVENT_START_DET | I2C_EVENT_GEN_CALL)))
/**
* @}
*/
/** @defgroup I2C_own_address1
* @{
*/
#define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF)
/**
* @}
*/
/** @defgroup I2C_clock_speed
* @{
*/
#define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))
/**
* @}
*/
/**
* @}
*/
/** @defgroup I2C_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup I2C_Exported_Functions
* @{
*/
void I2C_DeInit(I2C_TypeDef* I2Cx);
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
void I2C_ReadCmd(I2C_TypeDef* I2Cx);
#endif /*__HAL_I2C_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/*-------------------------(C) COPYRIGHT 2016 HOLOCENE ----------------------*/
|