ald_i2c.h 17.8 KB
Newer Older
W
wangyq2018 已提交
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
/**
 *********************************************************************************
 *
 * @file    ald_i2c.h
 * @brief   Header file of I2C driver
 *
 * @version V1.0
 * @date    15 Nov 2017
 * @author  AE Team
 * @note
 *
 * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
 *
 ********************************************************************************
 */

#ifndef __ALD_I2C_H__
#define __ALD_I2C_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "utils.h"
#include "ald_dma.h"
#include "ald_cmu.h"

/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @addtogroup I2C
  * @{
  */

/** @defgroup I2C_Public_Types I2C Public Types
  * @{
  */
/**
  * @brief I2C Error Code
  */
W
wangyq2018 已提交
42 43 44 45 46 47 48 49
typedef enum {
	I2C_ERROR_NONE    = 0x0U, 	/**< No error */
	I2C_ERROR_BERR    = 0x1U, 	/**< Berr error */
	I2C_ERROR_ARLO    = 0x2U, 	/**< Arlo error */
	I2C_ERROR_AF      = 0x4U, 	/**< Af error */
	I2C_ERROR_OVR     = 0x8U, 	/**< Ovr error */
	I2C_ERROR_DMA     = 0x10U,	/**< Dma error */
	I2C_ERROR_TIMEOUT = 0x20U,	/**< Timeout error */
W
wangyq2018 已提交
50 51 52 53 54
} i2c_error_t;

/**
  * @brief I2C state structure definition
  */
W
wangyq2018 已提交
55 56 57 58 59 60 61 62
typedef enum {
	I2C_STATE_RESET   = 0x0U, 	/**< Peripheral is not yet Initialized */
	I2C_STATE_READY   = 0x1U,	/**< Peripheral Initialized and ready for use */
	I2C_STATE_BUSY    = 0x2U,	/**< An internal process is ongoing */
	I2C_STATE_BUSY_TX = 0x3U,	/**< Data Transmission process is ongoing */
	I2C_STATE_BUSY_RX = 0x4U,	/**< Data Reception process is ongoing */
	I2C_STATE_TIMEOUT = 0x5U,	/**< timeout state */
	I2C_STATE_ERROR   = 0x6U,	/**< Error */
W
wangyq2018 已提交
63 64 65 66 67
} i2c_state_t;

/**
  * @brief I2C Duty Cycle
  */
W
wangyq2018 已提交
68 69 70
typedef enum {
	I2C_DUTYCYCLE_2    = 0x0U,   	/**< duty cycle is 2 */
	I2C_DUTYCYCLE_16_9 = 0x4000U,	/**< duty cycle is 16/9 */
W
wangyq2018 已提交
71 72 73 74 75
} i2c_duty_t;

/**
  * @brief I2C Addressing Mode
  */
W
wangyq2018 已提交
76 77 78
typedef enum {
	I2C_ADDR_7BIT  = 0x1U,	/**< 7 bit address */
	I2C_ADDR_10BIT = 0x2U,	/**< 10 bit address */
W
wangyq2018 已提交
79 80 81 82 83
} i2c_addr_t;

/**
  * @brief I2C Dual Addressing Mode
  */
W
wangyq2018 已提交
84 85 86
typedef enum {
	I2C_DUALADDR_DISABLE = 0x0U,	/**< dual address is disable */
	I2C_DUALADDR_ENABLE  = 0x1U,	/**< dual address is enable */
W
wangyq2018 已提交
87 88 89 90 91
} i2c_dual_addr_t;

/**
  * @brief I2C General Call Addressing mode
  */
W
wangyq2018 已提交
92 93 94
typedef enum {
	I2C_GENERALCALL_DISABLE = 0x0U, 	/**< feneral call address is disable */
	I2C_GENERALCALL_ENABLE  = 0x40U,	/**< feneral call address is enable */
W
wangyq2018 已提交
95 96 97 98 99
} i2c_general_addr_t;

/**
  * @brief I2C Nostretch Mode
  */
W
wangyq2018 已提交
100 101 102
typedef enum {
	I2C_NOSTRETCH_DISABLE = 0x0U, 	/**< Nostretch disable */
	I2C_NOSTRETCH_ENABLE  = 0x80U,	/**< Nostretch enable */
W
wangyq2018 已提交
103 104 105 106 107
} i2c_nostretch_t;

/**
  * @brief I2C Memory Address Size
  */
W
wangyq2018 已提交
108 109 110
typedef enum {
	I2C_MEMADD_SIZE_8BIT  = 0x1U,	/**< 8 bit memory address size */
	I2C_MEMADD_SIZE_16BIT = 0x10U,	/**< 10 bit memory address size */
W
wangyq2018 已提交
111 112 113 114 115
} i2c_addr_size_t;

/**
  * @brief I2C Flag Definition
  */
W
wangyq2018 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
typedef enum {
	I2C_FLAG_SB         = (1U << 0),
	I2C_FLAG_ADDR       = (1U << 1),
	I2C_FLAG_BTF        = (1U << 2),
	I2C_FLAG_ADD10      = (1U << 3),
	I2C_FLAG_STOPF      = (1U << 4),
	I2C_FLAG_RXNE       = (1U << 6),
	I2C_FLAG_TXE        = (1U << 7),
	I2C_FLAG_BERR       = (1U << 8),
	I2C_FLAG_ARLO       = (1U << 9),
	I2C_FLAG_AF         = (1U << 10),
	I2C_FLAG_OVR        = (1U << 11),
	I2C_FLAG_PECERR     = (1U << 12),
	I2C_FLAG_TIMEOUT    = (1U << 14),
	I2C_FLAG_SMBALERT   = (1U << 15),
	I2C_FLAG_MSL        = (1U << 16),
	I2C_FLAG_BUSY       = (1U << 17),
	I2C_FLAG_TRA        = (1U << 18),
	I2C_FLAG_GENCALL    = (1U << 20),
	I2C_FLAG_SMBDEFAULT = (1U << 21),
	I2C_FLAG_SMBHOST    = (1U << 22),
	I2C_FLAG_DUALF      = (1U << 23),
W
wangyq2018 已提交
138 139 140 141 142 143 144
} i2c_flag_t;

/**
  * @brief I2C mode structure definition
  */
typedef enum
{
W
wangyq2018 已提交
145 146 147 148
	I2C_MODE_NONE   = 0x0U, 	/**< No I2C communication on going */
	I2C_MODE_MASTER = 0x10U,	/**< I2C communication is in Master mode */
	I2C_MODE_SLAVE  = 0x20U,	/**< I2C communication is in Slave mode */
	I2C_MODE_MEM    = 0x40U,	/**< I2C communication is in Memory mode */
W
wangyq2018 已提交
149 150 151 152 153
} i2c_mode_t;

/**
  * @brief I2C Clock
  */
W
wangyq2018 已提交
154 155 156
typedef enum {
	I2C_STANDARD_MODE_MAX_CLK = 100000U,	/**< Standard mode clock */
	I2C_FAST_MODE_MAX_CLK     = 400000U,	/**< Fast mode clock */
W
wangyq2018 已提交
157 158 159 160 161
} i2c_clock_t;

/**
  * @brief Interrupt Configuration Definition
  */
W
wangyq2018 已提交
162 163 164 165
typedef enum {
	I2C_IT_BUF = (1U << 10),	/**< Buffer interrupt */
	I2C_IT_EVT = (1U << 9), 	/**< Event interrupt */
	I2C_IT_ERR = (1U << 8), 	/**< Error interrupt */
W
wangyq2018 已提交
166 167 168 169 170
} i2c_interrupt_t;

/**
  * @brief I2C CON1 Register
  */
W
wangyq2018 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
typedef enum {
	I2C_CON1_PEN    = (1U << 0), 	/**< PEN BIT */
	I2C_CON1_PMOD   = (1U << 1), 	/**< PMOD BIT */
	I2C_CON1_SMBMOD = (1U << 3), 	/**< SMBMOD  BIT */
	I2C_CON1_ARPEN  = (1U << 4), 	/**< ARPEN BIT */
	I2C_CON1_PECEN  = (1U << 5), 	/**< PECEN BIT */
	I2C_CON1_GCEN   = (1U << 6), 	/**< GCEN  BIT */
	I2C_CON1_DISCS  = (1U << 7), 	/**< DISCS BIT */
	I2C_CON1_START  = (1U << 8), 	/**< START BIT */
	I2C_CON1_STOP   = (1U << 9), 	/**< STOP BIT */
	I2C_CON1_ACKEN  = (1U << 10),	/**< ACKEN  BIT */
	I2C_CON1_POSAP  = (1U << 11),	/**< POSAP BIT */
	I2C_CON1_TRPEC  = (1U << 12),	/**< TRPEC  BIT */
	I2C_CON1_ALARM  = (1U << 13),	/**< ALARM BIT */
	I2C_CON1_SRST   = (1U << 15),	/**< SRST BIT */
W
wangyq2018 已提交
186 187 188 189 190
} i2c_con1_t;

/**
  * @brief I2C CON2 Register
  */
W
wangyq2018 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203
typedef enum {
	I2C_CON2_CLKF    =  0x3FU,     	/**< CLKF BITS */
	I2C_CON2_CLKF_0  = (1U << 0), 	/**< CLKF_0 BIT */
	I2C_CON2_CLKF_1  = (1U << 1), 	/**< CLKF_1 BIT */
	I2C_CON2_CLKF_2  = (1U << 2), 	/**< CLKF_2 BIT */
	I2C_CON2_CLKF_3  = (1U << 3), 	/**< CLKF_3 BIT */
	I2C_CON2_CLKF_4  = (1U << 4), 	/**< CLKF_4 BIT */
	I2C_CON2_CLKF_5  = (1U << 5), 	/**< CLKF_5 BIT */
	I2C_CON2_ERRIE   = (1U << 8), 	/**< ERRIE BIT */
	I2C_CON2_EVTIE   = (1U << 9), 	/**< EVTIE BIT */
	I2C_CON2_BUFIE   = (1U << 10),	/**< BUFIE BIT */
	I2C_CON2_DMAEN   = (1U << 11),	/**< DMAEN BIT */
	I2C_CON2_LDMA    = (1U << 12),	/**< LDMA BIT */
W
wangyq2018 已提交
204 205 206 207 208
} i2c_con2_t;

/**
  * @brief I2C ADDR1 Register
  */
W
wangyq2018 已提交
209 210 211 212 213 214 215 216 217 218 219 220
typedef enum {
	I2C_ADDR1_ADDH0   = (1U << 0), 	/**< ADDH0 BIT */
	I2C_ADDR1_ADDH1   = (1U << 1), 	/**< ADDH1 BIT */
	I2C_ADDR1_ADDH2   = (1U << 2), 	/**< ADDH2 BIT */
	I2C_ADDR1_ADDH3   = (1U << 3), 	/**< ADDH3 BIT */
	I2C_ADDR1_ADDH4   = (1U << 4), 	/**< ADDH4 BIT */
	I2C_ADDR1_ADDH5   = (1U << 5), 	/**< ADDH5 BIT */
	I2C_ADDR1_ADDH6   = (1U << 6), 	/**< ADDH6 BIT */
	I2C_ADDR1_ADDH7   = (1U << 7), 	/**< ADDH7 BIT */
	I2C_ADDR1_ADDH8   = (1U << 8), /**< ADDH8 BIT */
	I2C_ADDR1_ADDH9   = (1U << 9),	/**< ADDH9 BIT */
	I2C_ADDR1_ADDTYPE = (1U << 15),	/**< ADDTYPE BIT */
W
wangyq2018 已提交
221 222 223 224 225
} i2c_addr1_t;

/**
  * @brief I2C ADDR2 Register
  */
W
wangyq2018 已提交
226 227 228
typedef enum {
	I2C_ADDR2_DUALEN = (1U << 0),	/**< DUALEN BIT */
	I2C_ADDR2_ADD    = (1U << 1),	/**< ADD BIT */
W
wangyq2018 已提交
229 230 231 232 233
} i2c_addr2_t;

/**
  * @brief I2C STAT1 Register
  */
W
wangyq2018 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
typedef enum {
	I2C_STAT1_SB        = (1U << 0), 	/**< SB BIT */
	I2C_STAT1_ADDR      = (1U << 1), 	/**< ADDR  BIT */
	I2C_STAT1_BTC       = (1U << 2), 	/**< BTC BIT */
	I2C_STAT1_SENDADD10 = (1U << 3), 	/**< SENDADD10 BIT */
	I2C_STAT1_DETSTP    = (1U << 4), 	/**< DETSTP BIT */
	I2C_STAT1_RXBNE     = (1U << 6), 	/**< RXBNE BIT */
	I2C_STAT1_TXBE      = (1U << 7), 	/**< TXBE BIT */
	I2C_STAT1_BUSERR    = (1U << 8), 	/**< BUSERR BIT */
	I2C_STAT1_LARB      = (1U << 9), 	/**< LARB BIT */
	I2C_STAT1_ACKERR    = (1U << 10),	/**< ACKERR BIT */
	I2C_STAT1_ROUERR    = (1U << 11),	/**< ROUERR BIT */
	I2C_STAT1_PECERR    = (1U << 12),	/**< PECERR BIT */
	I2C_STAT1_SMBTO     = (1U << 14),	/**< SMBTO BIT */
	I2C_STAT1_SMBALARM  = (1U << 15),	/**< SMBALARM BIT */
W
wangyq2018 已提交
249 250 251 252 253
} i2c_stat1_t;

/**
  * @brief I2C STAT2 Register
  */
W
wangyq2018 已提交
254 255 256 257 258 259 260 261 262
typedef enum {
	I2C_STAT2_MASTER = (1U << 0),	/**< MASTER BIT */
	I2C_STAT2_BSYF   = (1U << 1),	/**< BSYF  BIT */
	I2C_STAT2_TRF    = (1U << 2),	/**< TRF BIT */
	I2C_STAT2_RXGCF  = (1U << 4),	/**< RXGCF BIT */
	I2C_STAT2_SMBDEF = (1U << 5),	/**< SMBDEF BIT */
	I2C_STAT2_SMBHH  = (1U << 6),	/**< SMBHH BIT */
	I2C_STAT2_DUALF  = (1U << 7),	/**< DMF BIT */
	I2C_STAT2_PECV   = (1U << 8),	/**< PECV BIT */
W
wangyq2018 已提交
263 264 265 266 267
} i2c_stat2_t;

/**
  * @brief I2C CKCFG Register
  */
W
wangyq2018 已提交
268 269 270 271
typedef enum {
	I2C_CKCFG_CLKSET =  0xFFFU,    	/**< CLKSET BITS */
	I2C_CKCFG_DUTY   = (1U << 14),	/**< DUTY BIT */
	I2C_CKCFG_CLKMOD = (1U << 15),	/**< CLKMOD BIT */
W
wangyq2018 已提交
272 273 274 275 276
} i2c_ckcfg_t;

/**
  * @brief I2C RT Register
  */
W
wangyq2018 已提交
277 278
typedef enum {
	I2C_RT_RISET = 0x3FU,	/**< RISET BITS */
W
wangyq2018 已提交
279 280 281 282 283
} i2c_trise_t;

/**
 * @brief I2C Configuration Structure definition
 */
W
wangyq2018 已提交
284 285 286 287 288 289 290 291 292
typedef struct {
	uint32_t clk_speed;                	/**< Specifies the clock frequency */
	i2c_duty_t duty;			/**< Specifies the I2C fast mode duty cycle */
	uint32_t own_addr1;               	/**< Specifies the first device own address */
	i2c_addr_t addr_mode;			/**< Specifies addressing mode */
	i2c_dual_addr_t dual_addr;		/**< Specifies if dual addressing mode is selected */
	uint32_t own_addr2;               	/**< Specifies the second device own address */
	i2c_general_addr_t general_call;	/**< Specifies if general call mode is selected */
	i2c_nostretch_t no_stretch;     	/**< Specifies if nostretch mode is selected */
W
wangyq2018 已提交
293 294 295 296 297
} i2c_init_t;

/**
  * @brief  I2C handle Structure definition
  */
W
wangyq2018 已提交
298 299 300 301 302 303
typedef struct i2c_handle_s {
	I2C_TypeDef *perh;       	/**< I2C registers base address */
	i2c_init_t init;         	/**< I2C communication parameters */
	uint8_t *p_buff;         	/**< Pointer to I2C transfer buffer */
	uint16_t xfer_size;      	/**< I2C transfer size */
	__IO uint16_t xfer_count;	/**< I2C transfer counter */
W
wangyq2018 已提交
304
#ifdef ALD_DMA
W
wangyq2018 已提交
305 306
	dma_handle_t hdmatx;		/**< I2C Tx DMA handle parameters */
	dma_handle_t hdmarx;		/**< I2C Rx DMA handle parameters */
W
wangyq2018 已提交
307
#endif
W
wangyq2018 已提交
308 309 310 311 312 313 314 315 316 317 318 319
	lock_state_t lock;       	/**< I2C locking object */
	__IO i2c_state_t state;  	/**< I2C communication state */
	__IO i2c_mode_t mode;    	/**< I2C communication mode */
	__IO uint32_t error_code;	/**< I2C Error code */

	void (*master_tx_cplt_cbk)(struct i2c_handle_s *arg);	/**< Master Tx completed callback */
	void (*master_rx_cplt_cbk)(struct i2c_handle_s *arg);	/**< Master Rx completed callback */
	void (*slave_tx_cplt_cbk)(struct i2c_handle_s *arg); 	/**< Slave Tx completed callback */
	void (*slave_rx_cplt_cbk)(struct i2c_handle_s *arg); 	/**< Slave Rx completed callback */
	void (*mem_tx_cplt_cbk)(struct i2c_handle_s *arg);   	/**< Tx to Memory completed callback */
	void (*mem_rx_cplt_cbk)(struct i2c_handle_s *arg);   	/**< Rx from Memory completed callback */
	void (*error_callback)(struct i2c_handle_s *arg);    	/**< Error callback */
W
wangyq2018 已提交
320 321 322 323 324 325 326 327 328 329 330
} i2c_handle_t;

/**
 * @}
 */

/** @defgroup I2C_Public_Macro I2C Public Macros
  * @{
  */
#define I2C_RESET_HANDLE_STATE(x) ((x)->state = I2C_STATE_RESET)
#define I2C_CLEAR_ADDRFLAG(x)		\
W
wangyq2018 已提交
331 332 333 334 335 336
do {					\
	__IO uint32_t tmpreg;		\
	tmpreg = (x)->perh->STAT1;	\
	tmpreg = (x)->perh->STAT2;	\
	UNUSED(tmpreg);			\
} while (0)
W
wangyq2018 已提交
337
#define __I2C_CLEAR_STOPFLAG(x)					\
W
wangyq2018 已提交
338 339 340 341 342 343
do {								\
	__IO uint32_t tmpreg;					\
	tmpreg = (x)->perh->STAT1;				\
	tmpreg = SET_BIT((x)->perh->CON1, I2C_CON1_PEN);	\
	UNUSED(tmpreg);						\
} while (0)
W
wangyq2018 已提交
344 345 346 347 348 349 350 351 352 353
#define I2C_ENABLE(x)  (SET_BIT((x)->perh->CON1, I2C_CON1_PEN_MSK))
#define I2C_DISABLE(x) (CLEAR_BIT((x)->perh->CON1, I2C_CON1_PEN_MSK))
/**
  * @}
  */

/** @defgroup I2C_Private_Macro I2C Private Macros
  * @{
  */
#define IS_I2C_TYPE(x)						(((x) == I2C0) || \
W
wangyq2018 已提交
354
								 ((x) == I2C1))
W
wangyq2018 已提交
355
#define IS_I2C_ADDRESSING_MODE(x)				(((x) == I2C_ADDR_7BIT) || \
W
wangyq2018 已提交
356
								 ((x) == I2C_ADDR_10BIT))
W
wangyq2018 已提交
357
#define IS_I2C_DUAL_ADDRESS(x)					(((x) == I2C_DUALADDR_DISABLE) || \
W
wangyq2018 已提交
358
								 ((x) == I2C_DUALADDR_ENABLE))
W
wangyq2018 已提交
359
#define IS_I2C_GENERAL_CALL(x)					(((x) == I2C_GENERALCALL_DISABLE) || \
W
wangyq2018 已提交
360
								 ((x) == I2C_GENERALCALL_ENABLE))
W
wangyq2018 已提交
361
#define IS_I2C_MEMADD_size(x)					(((x) == I2C_MEMADD_SIZE_8BIT) || \
W
wangyq2018 已提交
362
								 ((x) == I2C_MEMADD_SIZE_16BIT))
W
wangyq2018 已提交
363
#define IS_I2C_NO_STRETCH(x)					(((x) == I2C_NOSTRETCH_DISABLE) || \
W
wangyq2018 已提交
364
								 ((x) == I2C_NOSTRETCH_ENABLE))
W
wangyq2018 已提交
365 366 367 368
#define IS_I2C_OWN_ADDRESS1(x)					(((x) & (uint32_t)(0xFFFFFC00)) == 0)
#define IS_I2C_OWN_ADDRESS2(x)					(((x) & (uint32_t)(0xFFFFFF01)) == 0)
#define IS_I2C_CLOCK_SPEED(x)					(((x) > 0) && ((x) <= I2C_FAST_MODE_MAX_CLK))
#define IS_I2C_DUTY_CYCLE(x)					(((x) == I2C_DUTYCYCLE_2) || \
W
wangyq2018 已提交
369
								 ((x) == I2C_DUTYCYCLE_16_9))
W
wangyq2018 已提交
370
#define IS_I2C_IT_TYPE(x)					(((x) == I2C_IT_BUF) || \
W
wangyq2018 已提交
371 372
								 ((x) == I2C_IT_EVT) || \
								 ((x) == I2C_IT_ERR))
W
wangyq2018 已提交
373
#define IS_I2C_FLAG(x)						(((x) == I2C_FLAG_SB)         || \
W
wangyq2018 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
								 ((x) == I2C_FLAG_ADDR)       || \
								 ((x) == I2C_FLAG_BTF)        || \
								 ((x) == I2C_FLAG_ADD10)      || \
								 ((x) == I2C_FLAG_STOPF)      || \
								 ((x) == I2C_FLAG_RXNE)       || \
								 ((x) == I2C_FLAG_TXE)        || \
								 ((x) == I2C_FLAG_BERR)       || \
								 ((x) == I2C_FLAG_ARLO)       || \
								 ((x) == I2C_FLAG_AF)         || \
								 ((x) == I2C_FLAG_OVR)        || \
								 ((x) == I2C_FLAG_PECERR)     || \
								 ((x) == I2C_FLAG_TIMEOUT)    || \
								 ((x) == I2C_FLAG_SMBALERT)   || \
								 ((x) == I2C_FLAG_MSL)        || \
								 ((x) == I2C_FLAG_BUSY)       || \
								 ((x) == I2C_FLAG_TRA)        || \
								 ((x) == I2C_FLAG_GENCALL)    || \
								 ((x) == I2C_FLAG_SMBDEFAULT) || \
								 ((x) == I2C_FLAG_SMBHOST)    || \
								 ((x) == I2C_FLAG_DUALF))
W
wangyq2018 已提交
394 395 396 397 398

#define I2C_FREQ_RANGE(x)					((x) / 1000000)
#define I2C_RISE_TIME(x, u)					(((u) <= I2C_STANDARD_MODE_MAX_CLK) ? ((x) + 1) :\
        ((((x) * 300) / 1000) + 1))
#define I2C_SPEED_STANDARD(x, y)				(((((x) / ((y) << 1)) & I2C_CKCFG_CLKSET) < 4) ? 4:\
W
wangyq2018 已提交
399
	((x) / ((y) << 1)))
W
wangyq2018 已提交
400 401 402
#define I2C_SPEED_FAST(x, y, z)					(((z) == I2C_DUTYCYCLE_2) ? ((x) / ((y) * 3)) :\
        (((x) / ((y) * 25)) | I2C_DUTYCYCLE_16_9))
#define I2C_SPEED(x, y, z)					(((y) <= 100000) ? (I2C_SPEED_STANDARD((x), (y))) :\
W
wangyq2018 已提交
403
	((I2C_SPEED_FAST((x), (y), (z)) & I2C_CKCFG_CLKSET) == 0) ? 1 : \
W
wangyq2018 已提交
404 405 406 407 408 409 410 411 412 413
        ((I2C_SPEED_FAST((x), (y), (z))) | I2C_CKCFG_CLKMOD))
#define I2C_MEM_ADD_MSB(x)					((uint8_t)((uint16_t)(((uint16_t)((x) &\
        (uint16_t)(0xFF00))) >> 8)))
#define I2C_MEM_ADD_LSB(x)					((uint8_t)((uint16_t)((x) & (uint16_t)(0x00FF))))
#define I2C_7BIT_ADD_WRITE(x)					((uint8_t)((x) & (~I2C_ADDR1_ADDH0)))
#define I2C_7BIT_ADD_READ(x)					((uint8_t)((x) | I2C_ADDR1_ADDH0))
#define I2C_10BIT_ADDRESS(x)					((uint8_t)((uint16_t)((x) & (uint16_t)(0x00FF))))
#define I2C_10BIT_HEADER_WRITE(x)				((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((x) &\
        (uint16_t)(0x0300))) >> 7) | (uint16_t)(0xF0))))
#define I2C_10BIT_HEADER_READ(x)				((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((x) &\
W
wangyq2018 已提交
414
       (uint16_t)(0x0300))) >> 7) | (uint16_t)(0xF1))))
W
wangyq2018 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/**
  * @}
  */

/** @addtogroup I2C_Public_Functions
  * @{
  */

/** @addtogroup I2C_Public_Functions_Group1
  * @{
  */
ald_status_t ald_i2c_init(i2c_handle_t *hperh);
ald_status_t ald_i2c_reset(i2c_handle_t *hperh);

/**
 * @}
 */

/** @addtogroup I2C_Public_Functions_Group2
 * @{
 */
W
wangyq2018 已提交
436
 /** Blocking mode: Polling */
W
wangyq2018 已提交
437 438 439
ald_status_t ald_i2c_master_send(i2c_handle_t *hperh, uint16_t dev_addr,
                                 uint8_t *buf, uint16_t size, uint32_t timeout);
ald_status_t ald_i2c_master_recv(i2c_handle_t *hperh, uint16_t dev_addr,
W
wangyq2018 已提交
440
                                uint8_t *buf, uint16_t size, uint32_t timeout);
W
wangyq2018 已提交
441 442 443
ald_status_t ald_i2c_slave_send(i2c_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout);
ald_status_t ald_i2c_slave_recv(i2c_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout);
ald_status_t ald_i2c_mem_write(i2c_handle_t *hperh, uint16_t dev_addr, uint16_t mem_addr,
W
wangyq2018 已提交
444
                           i2c_addr_size_t add_size, uint8_t *buf, uint16_t size, uint32_t timeout);
W
wangyq2018 已提交
445
ald_status_t ald_i2c_mem_read(i2c_handle_t *hperh, uint16_t dev_addr, uint16_t mem_addr,
W
wangyq2018 已提交
446
                          i2c_addr_size_t add_size, uint8_t *buf, uint16_t size, uint32_t timeout);
W
wangyq2018 已提交
447 448
ald_status_t ald_i2c_is_device_ready(i2c_handle_t *hperh, uint16_t dev_addr, uint32_t trials, uint32_t timeout);

W
wangyq2018 已提交
449
 /** Non-Blocking mode: Interrupt */
W
wangyq2018 已提交
450 451 452 453 454
ald_status_t ald_i2c_master_send_by_it(i2c_handle_t *hperh, uint16_t dev_addr, uint8_t *buf, uint16_t size);
ald_status_t ald_i2c_master_recv_by_it(i2c_handle_t *hperh, uint16_t dev_addr, uint8_t *buf, uint16_t size);
ald_status_t ald_i2c_slave_send_by_it(i2c_handle_t *hperh, uint8_t *buf, uint16_t size);
ald_status_t ald_i2c_slave_recv_by_it(i2c_handle_t *hperh, uint8_t *buf, uint16_t size);
ald_status_t ald_i2c_mem_write_by_it(i2c_handle_t *hperh, uint16_t dev_addr, uint16_t mem_addr,
W
wangyq2018 已提交
455
                              i2c_addr_size_t add_size, uint8_t *buf, uint16_t size);
W
wangyq2018 已提交
456
ald_status_t ald_i2c_mem_read_by_it(i2c_handle_t *hperh, uint16_t dev_addr, uint16_t mem_addr,
W
wangyq2018 已提交
457
                             i2c_addr_size_t add_size, uint8_t *buf, uint16_t size);
W
wangyq2018 已提交
458 459

#ifdef ALD_DMA
W
wangyq2018 已提交
460
 /** Non-Blocking mode: DMA */
W
wangyq2018 已提交
461
ald_status_t ald_i2c_master_send_by_dma(i2c_handle_t *hperh, uint16_t dev_addr,
W
wangyq2018 已提交
462
                                     uint8_t *buf, uint16_t size, uint8_t channel);
W
wangyq2018 已提交
463
ald_status_t ald_i2c_master_recv_by_dma(i2c_handle_t *hperh, uint16_t dev_addr,
W
wangyq2018 已提交
464
                                    uint8_t *buf, uint16_t size, uint8_t channel);
W
wangyq2018 已提交
465 466 467
ald_status_t ald_i2c_slave_send_by_dma(i2c_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel);
ald_status_t ald_i2c_slave_recv_by_dma(i2c_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel);
ald_status_t ald_i2c_mem_write_by_dma(i2c_handle_t *hperh, uint16_t dev_addr, uint16_t mem_addr, i2c_addr_size_t add_size,
W
wangyq2018 已提交
468
                                 uint8_t *buf, uint16_t size, uint8_t channel);
W
wangyq2018 已提交
469
ald_status_t ald_i2c_mem_read_by_dma(i2c_handle_t *hperh, uint16_t dev_addr, uint16_t mem_addr,
W
wangyq2018 已提交
470
                                i2c_addr_size_t add_size, uint8_t *buf, uint16_t size, uint8_t channel);
W
wangyq2018 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
#endif
/**
 * @}
 */

/** @addtogroup I2C_Public_Functions_Group3
  * @{
  */
i2c_state_t ald_i2c_get_state(i2c_handle_t *hperh);
uint32_t ald_i2c_get_error(i2c_handle_t *hperh);
flag_status_t ald_i2c_get_flag_status(i2c_handle_t *hperh, i2c_flag_t flag);
flag_status_t ald_i2c_get_it_status(i2c_handle_t *hperh, i2c_interrupt_t it);
void ald_i2c_clear_flag_status(i2c_handle_t *hperh, i2c_flag_t flag);
/**
 * @}
 */

/** @addtogroup I2C_Public_Functions_Group4
 * @{
 */
void ald_i2c_interrupt_config(i2c_handle_t *hperh, i2c_interrupt_t it, type_func_t state);
void ald_i2c_ev_irq_handler(i2c_handle_t *hperh);
void ald_i2c_er_irq_handler(i2c_handle_t *hperh);
/**
 * @}
 */

/**
 * @}
 */

/**
  * @}
  */

/**
 * @}
 */
#ifdef __cplusplus
}
#endif

#endif /* __ALD_I2C_H__ */