2 * @brief I2C registers and driver functions
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * All rights reserved.
\r
9 * Software that is described herein is for illustrative purposes only
\r
10 * which provides customers with programming information regarding the
\r
11 * LPC products. This software is supplied "AS IS" without any warranties of
\r
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
13 * all warranties, express or implied, including all implied warranties of
\r
14 * merchantability, fitness for a particular purpose and non-infringement of
\r
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
16 * or liability for the use of the software, conveys no license or rights under any
\r
17 * patent, copyright, mask work right, or any other intellectual property rights in
\r
18 * or to any products. NXP Semiconductors reserves the right to make changes
\r
19 * in the software without notification. NXP Semiconductors also makes no
\r
20 * representation or warranty that such application will be suitable for the
\r
21 * specified use without further testing or modification.
\r
24 * Permission to use, copy, modify, and distribute this software and its
\r
25 * documentation is hereby granted, under NXP Semiconductors' and its
\r
26 * licensor's relevant copyrights in the software, without fee, provided that it
\r
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
28 * copyright, permission, and disclaimer notice must appear in all copies of
\r
32 #ifndef __I2C_001_H_
\r
33 #define __I2C_001_H_
\r
35 #include "sys_config.h"
\r
42 /** @defgroup IP_I2C_001 IP: I2C register block and driver
\r
43 * @ingroup IP_Drivers
\r
48 * @brief I2C register block structure
\r
50 typedef struct { /* I2C0 Structure */
\r
51 __IO uint32_t CONSET; /*!< I2C Control Set Register. When a one is written to a bit of this register, the corresponding bit in the I2C control register is set. Writing a zero has no effect on the corresponding bit in the I2C control register. */
\r
52 __I uint32_t STAT; /*!< I2C Status Register. During I2C operation, this register provides detailed status codes that allow software to determine the next action needed. */
\r
53 __IO uint32_t DAT; /*!< I2C Data Register. During master or slave transmit mode, data to be transmitted is written to this register. During master or slave receive mode, data that has been received may be read from this register. */
\r
54 __IO uint32_t ADR0; /*!< I2C Slave Address Register 0. Contains the 7-bit slave address for operation of the I2C interface in slave mode, and is not used in master mode. The least significant bit determines whether a slave responds to the General Call address. */
\r
55 __IO uint32_t SCLH; /*!< SCH Duty Cycle Register High Half Word. Determines the high time of the I2C clock. */
\r
56 __IO uint32_t SCLL; /*!< SCL Duty Cycle Register Low Half Word. Determines the low time of the I2C clock. SCLL and SCLH together determine the clock frequency generated by an I2C master and certain times used in slave mode. */
\r
57 __O uint32_t CONCLR; /*!< I2C Control Clear Register. When a one is written to a bit of this register, the corresponding bit in the I2C control register is cleared. Writing a zero has no effect on the corresponding bit in the I2C control register. */
\r
58 __IO uint32_t MMCTRL; /*!< Monitor mode control register. */
\r
59 __IO uint32_t ADR1; /*!< I2C Slave Address Register. Contains the 7-bit slave address for operation of the I2C interface in slave mode, and is not used in master mode. The least significant bit determines whether a slave responds to the General Call address. */
\r
60 __IO uint32_t ADR2; /*!< I2C Slave Address Register. Contains the 7-bit slave address for operation of the I2C interface in slave mode, and is not used in master mode. The least significant bit determines whether a slave responds to the General Call address. */
\r
61 __IO uint32_t ADR3; /*!< I2C Slave Address Register. Contains the 7-bit slave address for operation of the I2C interface in slave mode, and is not used in master mode. The least significant bit determines whether a slave responds to the General Call address. */
\r
62 __I uint32_t DATA_BUFFER; /*!< Data buffer register. The contents of the 8 MSBs of the DAT shift register will be transferred to the DATA_BUFFER automatically after every nine bits (8 bits of data plus ACK or NACK) has been received on the bus. */
\r
63 __IO uint32_t MASK[4]; /*!< I2C Slave address mask register */
\r
67 * @brief Return values for SLAVE handler
\r
69 * Chip drivers will usally be designed to match their events with this value
\r
71 #define RET_SLAVE_TX 6 /**< Return value, when 1 byte TX'd successfully */
\r
72 #define RET_SLAVE_RX 5 /**< Return value, when 1 byte RX'd successfully */
\r
73 #define RET_SLAVE_IDLE 2 /**< Return value, when slave enter idle mode */
\r
74 #define RET_SLAVE_BUSY 0 /**< Return value, when slave is busy */
\r
77 * @brief I2C state handle return values
\r
79 #define I2C_STA_STO_RECV 0x20
\r
82 * @brief I2C Control Set register description
\r
84 #define I2C_I2CONSET_AA ((0x04))/*!< Assert acknowledge flag */
\r
85 #define I2C_I2CONSET_SI ((0x08))/*!< I2C interrupt flag */
\r
86 #define I2C_I2CONSET_STO ((0x10))/*!< STOP flag */
\r
87 #define I2C_I2CONSET_STA ((0x20))/*!< START flag */
\r
88 #define I2C_I2CONSET_I2EN ((0x40))/*!< I2C interface enable */
\r
91 * @brief I2C Control Clear register description
\r
93 #define I2C_I2CONCLR_AAC ((1 << 2)) /*!< Assert acknowledge Clear bit */
\r
94 #define I2C_I2CONCLR_SIC ((1 << 3)) /*!< I2C interrupt Clear bit */
\r
95 #define I2C_I2CONCLR_STOC ((1 << 4)) /*!< I2C STOP Clear bit */
\r
96 #define I2C_I2CONCLR_STAC ((1 << 5)) /*!< START flag Clear bit */
\r
97 #define I2C_I2CONCLR_I2ENC ((1 << 6)) /*!< I2C interface Disable bit */
\r
100 * @brief I2C Common Control register description
\r
102 #define I2C_CON_AA (1UL << 2) /*!< Assert acknowledge bit */
\r
103 #define I2C_CON_SI (1UL << 3) /*!< I2C interrupt bit */
\r
104 #define I2C_CON_STO (1UL << 4) /*!< I2C STOP bit */
\r
105 #define I2C_CON_STA (1UL << 5) /*!< START flag bit */
\r
106 #define I2C_CON_I2EN (1UL << 6) /*!< I2C interface bit */
\r
109 * @brief I2C Status Code definition (I2C Status register)
\r
111 #define I2C_STAT_CODE_BITMASK ((0xF8))/*!< Return Code mask in I2C status register */
\r
112 #define I2C_STAT_CODE_ERROR ((0xFF))/*!< Return Code error mask in I2C status register */
\r
115 * @brief I2C return status code definitions
\r
117 #define I2C_I2STAT_NO_INF ((0xF8))/*!< No relevant information */
\r
118 #define I2C_I2STAT_BUS_ERROR ((0x00))/*!< Bus Error */
\r
121 * @brief I2C Master transmit mode
\r
123 #define I2C_I2STAT_M_TX_START ((0x08))/*!< A start condition has been transmitted */
\r
124 #define I2C_I2STAT_M_TX_RESTART ((0x10))/*!< A repeat start condition has been transmitted */
\r
125 #define I2C_I2STAT_M_TX_SLAW_ACK ((0x18))/*!< SLA+W has been transmitted, ACK has been received */
\r
126 #define I2C_I2STAT_M_TX_SLAW_NACK ((0x20))/*!< SLA+W has been transmitted, NACK has been received */
\r
127 #define I2C_I2STAT_M_TX_DAT_ACK ((0x28))/*!< Data has been transmitted, ACK has been received */
\r
128 #define I2C_I2STAT_M_TX_DAT_NACK ((0x30))/*!< Data has been transmitted, NACK has been received */
\r
129 #define I2C_I2STAT_M_TX_ARB_LOST ((0x38))/*!< Arbitration lost in SLA+R/W or Data bytes */
\r
132 * @brief I2C Master receive mode
\r
134 #define I2C_I2STAT_M_RX_START ((0x08))/*!< A start condition has been transmitted */
\r
135 #define I2C_I2STAT_M_RX_RESTART ((0x10))/*!< A repeat start condition has been transmitted */
\r
136 #define I2C_I2STAT_M_RX_ARB_LOST ((0x38))/*!< Arbitration lost */
\r
137 #define I2C_I2STAT_M_RX_SLAR_ACK ((0x40))/*!< SLA+R has been transmitted, ACK has been received */
\r
138 #define I2C_I2STAT_M_RX_SLAR_NACK ((0x48))/*!< SLA+R has been transmitted, NACK has been received */
\r
139 #define I2C_I2STAT_M_RX_DAT_ACK ((0x50))/*!< Data has been received, ACK has been returned */
\r
140 #define I2C_I2STAT_M_RX_DAT_NACK ((0x58))/*!< Data has been received, NACK has been returned */
\r
143 * @brief I2C Slave receive mode
\r
145 #define I2C_I2STAT_S_RX_SLAW_ACK ((0x60))/*!< Own slave address has been received, ACK has been returned */
\r
146 #define I2C_I2STAT_S_RX_ARB_LOST_M_SLA ((0x68))/*!< Arbitration lost in SLA+R/W as master */
\r
147 // #define I2C_I2STAT_S_RX_SLAW_ACK ((0x68)) /*!< Own SLA+W has been received, ACK returned */
\r
148 #define I2C_I2STAT_S_RX_GENCALL_ACK ((0x70))/*!< General call address has been received, ACK has been returned */
\r
149 #define I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL ((0x78))/*!< Arbitration lost in SLA+R/W (GENERAL CALL) as master */
\r
150 // #define I2C_I2STAT_S_RX_GENCALL_ACK ((0x78)) /*!< General call address has been received, ACK has been returned */
\r
151 #define I2C_I2STAT_S_RX_PRE_SLA_DAT_ACK ((0x80))/*!< Previously addressed with own SLA; Data has been received, ACK has been returned */
\r
152 #define I2C_I2STAT_S_RX_PRE_SLA_DAT_NACK ((0x88))/*!< Previously addressed with own SLA;Data has been received and NOT ACK has been returned */
\r
153 #define I2C_I2STAT_S_RX_PRE_GENCALL_DAT_ACK ((0x90))/*!< Previously addressed with General Call; Data has been received and ACK has been returned */
\r
154 #define I2C_I2STAT_S_RX_PRE_GENCALL_DAT_NACK ((0x98))/*!< Previously addressed with General Call; Data has been received and NOT ACK has been returned */
\r
155 #define I2C_I2STAT_S_RX_STA_STO_SLVREC_SLVTRX ((0xA0))/*!< A STOP condition or repeated START condition has been received while still addressed as SLV/REC (Slave Receive) or
\r
156 SLV/TRX (Slave Transmit) */
\r
159 * @brief I2C Slave transmit mode
\r
161 #define I2C_I2STAT_S_TX_SLAR_ACK ((0xA8))/*!< Own SLA+R has been received, ACK has been returned */
\r
162 #define I2C_I2STAT_S_TX_ARB_LOST_M_SLA ((0xB0))/*!< Arbitration lost in SLA+R/W as master */
\r
163 // #define I2C_I2STAT_S_TX_SLAR_ACK ((0xB0)) /*!< Own SLA+R has been received, ACK has been returned */
\r
164 #define I2C_I2STAT_S_TX_DAT_ACK ((0xB8))/*!< Data has been transmitted, ACK has been received */
\r
165 #define I2C_I2STAT_S_TX_DAT_NACK ((0xC0))/*!< Data has been transmitted, NACK has been received */
\r
166 #define I2C_I2STAT_S_TX_LAST_DAT_ACK ((0xC8))/*!< Last data byte in I2DAT has been transmitted (AA = 0); ACK has been received */
\r
167 #define I2C_SLAVE_TIME_OUT 0x10000000UL/*!< Time out in case of using I2C slave mode */
\r
170 * @brief I2C Data register definition
\r
172 #define I2C_I2DAT_BITMASK ((0xFF))/*!< Mask for I2DAT register */
\r
173 #define I2C_I2DAT_IDLE_CHAR (0xFF) /*!< Idle data value will be send out in slave mode in case of the actual expecting data requested from the master is greater than
\r
174 its sending data length that can be supported */
\r
177 * @brief I2C Monitor mode control register description
\r
179 #define I2C_I2MMCTRL_MM_ENA ((1 << 0)) /**< Monitor mode enable */
\r
180 #define I2C_I2MMCTRL_ENA_SCL ((1 << 1)) /**< SCL output enable */
\r
181 #define I2C_I2MMCTRL_MATCH_ALL ((1 << 2)) /**< Select interrupt register match */
\r
182 #define I2C_I2MMCTRL_BITMASK ((0x07)) /**< Mask for I2MMCTRL register */
\r
185 * @brief I2C Data buffer register description
\r
187 #define I2DATA_BUFFER_BITMASK ((0xFF))/*!< I2C Data buffer register bit mask */
\r
190 * @brief I2C Slave Address registers definition
\r
192 #define I2C_I2ADR_GC ((1 << 0)) /*!< General Call enable bit */
\r
193 #define I2C_I2ADR_BITMASK ((0xFF))/*!< I2C Slave Address registers bit mask */
\r
196 * @brief I2C Mask Register definition
\r
198 #define I2C_I2MASK_MASK(n) ((n & 0xFE))/*!< I2C Mask Register mask field */
\r
201 * @brief I2C SCL HIGH duty cycle Register definition
\r
203 #define I2C_I2SCLH_BITMASK ((0xFFFF)) /*!< I2C SCL HIGH duty cycle Register bit mask */
\r
206 * @brief I2C SCL LOW duty cycle Register definition
\r
208 #define I2C_I2SCLL_BITMASK ((0xFFFF)) /*!< I2C SCL LOW duty cycle Register bit mask */
\r
211 * @brief I2C status values
\r
213 #define I2C_SETUP_STATUS_ARBF (1 << 8) /**< Arbitration false */
\r
214 #define I2C_SETUP_STATUS_NOACKF (1 << 9) /**< No ACK returned */
\r
215 #define I2C_SETUP_STATUS_DONE (1 << 10) /**< Status DONE */
\r
218 * @brief I2C state handle return values
\r
220 #define I2C_OK 0x00
\r
221 #define I2C_BYTE_SENT 0x01
\r
222 #define I2C_BYTE_RECV 0x02
\r
223 #define I2C_LAST_BYTE_RECV 0x04
\r
224 #define I2C_SEND_END 0x08
\r
225 #define I2C_RECV_END 0x10
\r
226 #define I2C_STA_STO_RECV 0x20
\r
228 #define I2C_ERR (0x10000000)
\r
229 #define I2C_NAK_RECV (0x10000000 | 0x01)
\r
231 #define I2C_CheckError(ErrorCode) (ErrorCode & 0x10000000)
\r
234 * @brief I2C monitor control configuration defines
\r
236 #define I2C_MONITOR_CFG_SCL_OUTPUT I2C_I2MMCTRL_ENA_SCL /**< SCL output enable */
\r
237 #define I2C_MONITOR_CFG_MATCHALL I2C_I2MMCTRL_MATCH_ALL /**< Select interrupt register match */
\r
240 * @brief I2C Slave Identifiers
\r
243 I2C_SLAVE_GENERAL, /**< Slave ID for general calls */
\r
244 I2C_SLAVE_0, /**< Slave ID fo Slave Address 0 */
\r
245 I2C_SLAVE_1, /**< Slave ID fo Slave Address 1 */
\r
246 I2C_SLAVE_2, /**< Slave ID fo Slave Address 2 */
\r
247 I2C_SLAVE_3, /**< Slave ID fo Slave Address 3 */
\r
248 I2C_SLAVE_NUM_INTERFACE /**< Number of slave interfaces */
\r
252 * @brief I2C transfer status
\r
255 I2C_STATUS_DONE, /**< Transfer done successfully */
\r
256 I2C_STATUS_NAK, /**< NAK received during transfer */
\r
257 I2C_STATUS_ARBLOST, /**< Aribitration lost during transfer */
\r
258 I2C_STATUS_BUSERR, /**< Bus error in I2C transfer */
\r
259 I2C_STATUS_BUSY, /**< I2C is busy doing transfer */
\r
263 * @brief Master transfer data structure definitions
\r
266 uint8_t slaveAddr; /**< 7-bit I2C Slave address */
\r
267 const uint8_t *txBuff; /**< Pointer to array of bytes to be transmitted */
\r
268 int txSz; /**< Number of bytes in transmit array,
\r
269 if 0 only receive transfer will be carried on */
\r
270 uint8_t *rxBuff; /**< Pointer memory where bytes received from I2C be stored */
\r
271 int rxSz; /**< Number of bytes to received,
\r
272 if 0 only transmission we be carried on */
\r
273 I2C_STATUS_T status; /**< Status of the current I2C transfer */
\r
277 * @brief Initializes the pI2C peripheral.
\r
278 * @param pI2C : Pointer to selected I2Cx peripheral
\r
281 STATIC INLINE void IP_I2C_Init(IP_I2C_001_T *pI2C)
\r
283 /* Set I2C operation to default */
\r
284 pI2C->CONCLR = (I2C_CON_AA | I2C_CON_SI | I2C_CON_STA | I2C_CON_I2EN);
\r
288 * @brief De-initializes the I2C peripheral registers to their default reset values.
\r
289 * @param pI2C : Pointer to selected I2Cx peripheral
\r
292 STATIC INLINE void IP_I2C_DeInit(IP_I2C_001_T *pI2C)
\r
294 /* Disable I2C control */
\r
295 pI2C->CONCLR = I2C_CON_I2EN | I2C_CON_SI | I2C_CON_STO | I2C_CON_STA | I2C_CON_AA;
\r
299 * @brief Set up clock rate for I2Cx.
\r
300 * @param pI2C : Pointer to selected I2Cx peripheral
\r
301 * @param SCLValue : Value of I2CSCL register
\r
304 STATIC INLINE void IP_I2C_SetClockRate(IP_I2C_001_T *pI2C, uint32_t SCLValue)
\r
306 pI2C->SCLH = (uint32_t) (SCLValue >> 1);
\r
307 pI2C->SCLL = (uint32_t) (SCLValue - pI2C->SCLH);
\r
311 * @brief Get current divisor clock value
\r
312 * @param pI2C : Pointer to selected I2Cx peripheral
\r
313 * @return Clock value divider used by I2C peripheral
\r
315 STATIC INLINE uint32_t IP_I2C_GetClockDiv(IP_I2C_001_T *pI2C)
\r
317 return pI2C->SCLH + pI2C->SCLL;
\r
321 * @brief Enable I2C and start master transfer
\r
322 * @param pI2C : Pointer to selected I2C peripheral
\r
325 STATIC INLINE void IP_I2C_Master_StartXfer(IP_I2C_001_T *pI2C)
\r
327 /* Reset STA, STO, SI */
\r
328 pI2C->CONCLR = I2C_CON_SI | I2C_CON_STO | I2C_CON_STA | I2C_CON_AA;
\r
330 /* Enter to Master Transmitter mode */
\r
331 pI2C->CONSET = I2C_CON_I2EN | I2C_CON_STA;
\r
335 * @brief Master transfer state change handler
\r
336 * @param pI2C : Pointer to selected I2C peripheral
\r
337 * @param xfer : Pointer to transfer structure
\r
339 * 0 when transfer is done, non-zero when transfer is still going on.
\r
341 * Values pointed to by @a xfer will be changed based on the state
\r
342 * that was handled.
\r
344 int IP_I2C_MasterXfer_StateHandler(IP_I2C_001_T *pI2C, I2C_XFER_T *xfer);
\r
347 * @brief I2C State change checking
\r
348 * @param pI2C : Pointer to selected I2C peripheral
\r
349 * @return 1 when there is a state change, else 0
\r
351 STATIC INLINE int IP_I2C_IsStateChanged(IP_I2C_001_T *pI2C)
\r
353 return (pI2C->CONSET & I2C_CON_SI) != 0;
\r
357 * @brief Disable I2C peripheral's operation
\r
358 * @param pI2C : Pointer to selected I2C peripheral
\r
361 STATIC INLINE void IP_I2C_Disable(IP_I2C_001_T *pI2C)
\r
363 pI2C->CONCLR = I2C_I2CONCLR_I2ENC;
\r
367 * @brief Set OWN slave address for specific slave ID
\r
368 * @param pI2C : Pointer to selected I2C peripheral
\r
369 * @param sid : Slave ID (see #I2C_SLAVE_ID)
\r
370 * @param addr : 7-bit slave address from bits (1 - 7)
\r
371 * bit 0 having 1 to enable general call, 0 to disable it.
\r
372 * @param mask : Address mask
\r
375 STATIC INLINE void IP_I2C_SetSlaveAddress(IP_I2C_001_T *pI2C, I2C_SLAVE_ID sid, uint8_t addr, uint8_t mask)
\r
377 uint32_t index = (uint32_t) sid - 1;
\r
378 pI2C->MASK[index] = mask;
\r
379 if (sid == I2C_SLAVE_0) {
\r
383 volatile uint32_t *abase = &pI2C->ADR1;
\r
384 abase[index - 1] = addr;
\r
389 * @brief Get current state of the I2C peripheral
\r
390 * @param pI2C : Pointer to selected I2C peripheral
\r
391 * @return Current state code of I2C peripheral
\r
393 STATIC INLINE int IP_I2C_GetCurrentState(IP_I2C_001_T *pI2C)
\r
395 return (int) (pI2C->STAT & I2C_STAT_CODE_BITMASK);
\r
399 * @brief Get the slave ID of the active slave
\r
400 * @param pI2C : Pointer to selected I2C peripheral
\r
401 * @return Current active slave id
\r
403 I2C_SLAVE_ID IP_I2C_GetSlaveIndex(IP_I2C_001_T *pI2C);
\r
406 * @brief Slave transfer state change handler
\r
407 * @param pI2C : Pointer to selected I2C peripheral
\r
408 * @param xfer : Pointer to transfer structure
\r
410 * #RET_SLAVE_BUSY when transfer is in progress, #RET_SLAVE_IDLE when
\r
411 * transfer done and slave enters idle mode, #RET_SLAVE_RX when a byte
\r
412 * is received from master, #RET_SLAVE_TX when a byte is sent to master.
\r
414 * Argument @a xfer must have txBuff and rxBuff pointing to a valid memory
\r
415 * except for General call handler which can have txBuff as NULL as the
\r
416 * slave will not transfer anything to master using General call address.
\r
417 * Structure pointed by @a xfer will have its values changed based on the
\r
418 * event that was handled.
\r
420 int IP_I2C_SlaveXfer_StateHandler(IP_I2C_001_T *pI2C, I2C_XFER_T *xfer);
\r
423 * @brief Enable I2C and enable slave transfers
\r
424 * @param pI2C : Pointer to selected I2C peripheral
\r
427 STATIC INLINE void IP_I2C_Slave_StartXfer(IP_I2C_001_T *pI2C)
\r
429 /* Reset STA, STO, SI */
\r
430 pI2C->CONCLR = I2C_CON_SI | I2C_CON_STO | I2C_CON_STA;
\r
432 /* Enter to Master Transmitter mode */
\r
433 pI2C->CONSET = I2C_CON_I2EN | I2C_CON_AA;
\r
437 * @brief Check if I2C bus is free
\r
438 * @param pI2C : Pointer to selected I2C peripheral
\r
439 * @return 1 if I2C bus is free, 0 if busy
\r
441 STATIC INLINE int IP_I2C_BusFree(IP_I2C_001_T *pI2C)
\r
443 return !(pI2C->CONSET & I2C_CON_STO);
\r
447 * @brief Check if the active state belongs to master mode
\r
448 * @param pI2C : Pointer to selected I2C peripheral
\r
449 * @return 1 if the I2C is in master mode, 0 if I2C is in slave mode
\r
451 STATIC INLINE int IP_I2C_IsMasterState(IP_I2C_001_T *pI2C)
\r
453 return IP_I2C_GetCurrentState(pI2C) < 0x60;
\r
464 #endif /* __I2C_001_H_ */
\r