1 /**********************************************************************
\r
2 * $Id$ lpc18xx_i2c.h 2011-06-02
\r
4 * @file lpc18xx_i2c.h
\r
5 * @brief Contains all macro definitions and function prototypes
\r
6 * support for I2C firmware library on LPC18xx
\r
8 * @date 02. June. 2011
\r
9 * @author NXP MCU SW Application Team
\r
11 * Copyright(C) 2011, NXP Semiconductor
\r
12 * All rights reserved.
\r
14 ***********************************************************************
\r
15 * Software that is described herein is for illustrative purposes only
\r
16 * which provides customers with programming information regarding the
\r
17 * products. This software is supplied "AS IS" without any warranties.
\r
18 * NXP Semiconductors assumes no responsibility or liability for the
\r
19 * use of the software, conveys no license or title under any patent,
\r
20 * copyright, or mask work right to the product. NXP Semiconductors
\r
21 * reserves the right to make changes in the software without
\r
22 * notification. NXP Semiconductors also make no representation or
\r
23 * warranty that such application will be suitable for the specified
\r
24 * use without further testing or modification.
\r
25 **********************************************************************/
\r
27 /* Peripheral group ----------------------------------------------------------- */
\r
28 /** @defgroup I2C I2C (Inter-Integrated Circuit)
\r
29 * @ingroup LPC1800CMSIS_FwLib_Drivers
\r
33 #ifndef LPC18XX_I2C_H_
\r
34 #define LPC18XX_I2C_H_
\r
36 /* Includes ------------------------------------------------------------------- */
\r
37 #include "LPC18xx.h"
\r
38 #include "lpc_types.h"
\r
47 /* Private Macros ------------------------------------------------------------- */
\r
48 /** @defgroup I2C_Private_Macros I2C Private Macros
\r
52 /* --------------------- BIT DEFINITIONS -------------------------------------- */
\r
53 /*******************************************************************//**
\r
54 * I2C Control Set register description
\r
55 *********************************************************************/
\r
56 #define I2C_I2CONSET_AA ((0x04)) /*!< Assert acknowledge flag */
\r
57 #define I2C_I2CONSET_SI ((0x08)) /*!< I2C interrupt flag */
\r
58 #define I2C_I2CONSET_STO ((0x10)) /*!< STOP flag */
\r
59 #define I2C_I2CONSET_STA ((0x20)) /*!< START flag */
\r
60 #define I2C_I2CONSET_I2EN ((0x40)) /*!< I2C interface enable */
\r
62 /*******************************************************************//**
\r
63 * I2C Control Clear register description
\r
64 *********************************************************************/
\r
65 /** Assert acknowledge Clear bit */
\r
66 #define I2C_I2CONCLR_AAC ((1<<2))
\r
67 /** I2C interrupt Clear bit */
\r
68 #define I2C_I2CONCLR_SIC ((1<<3))
\r
69 /** START flag Clear bit */
\r
70 #define I2C_I2CONCLR_STAC ((1<<5))
\r
71 /** I2C interface Disable bit */
\r
72 #define I2C_I2CONCLR_I2ENC ((1<<6))
\r
74 /********************************************************************//**
\r
75 * I2C Status Code definition (I2C Status register)
\r
76 *********************************************************************/
\r
77 /* Return Code in I2C status register */
\r
78 #define I2C_STAT_CODE_BITMASK ((0xF8))
\r
80 /* I2C return status code definitions ----------------------------- */
\r
82 /** No relevant information */
\r
83 #define I2C_I2STAT_NO_INF ((0xF8))
\r
85 /* Master transmit mode -------------------------------------------- */
\r
86 /** A start condition has been transmitted */
\r
87 #define I2C_I2STAT_M_TX_START ((0x08))
\r
88 /** A repeat start condition has been transmitted */
\r
89 #define I2C_I2STAT_M_TX_RESTART ((0x10))
\r
90 /** SLA+W has been transmitted, ACK has been received */
\r
91 #define I2C_I2STAT_M_TX_SLAW_ACK ((0x18))
\r
92 /** SLA+W has been transmitted, NACK has been received */
\r
93 #define I2C_I2STAT_M_TX_SLAW_NACK ((0x20))
\r
94 /** Data has been transmitted, ACK has been received */
\r
95 #define I2C_I2STAT_M_TX_DAT_ACK ((0x28))
\r
96 /** Data has been transmitted, NACK has been received */
\r
97 #define I2C_I2STAT_M_TX_DAT_NACK ((0x30))
\r
98 /** Arbitration lost in SLA+R/W or Data bytes */
\r
99 #define I2C_I2STAT_M_TX_ARB_LOST ((0x38))
\r
101 /* Master receive mode -------------------------------------------- */
\r
102 /** A start condition has been transmitted */
\r
103 #define I2C_I2STAT_M_RX_START ((0x08))
\r
104 /** A repeat start condition has been transmitted */
\r
105 #define I2C_I2STAT_M_RX_RESTART ((0x10))
\r
106 /** Arbitration lost */
\r
107 #define I2C_I2STAT_M_RX_ARB_LOST ((0x38))
\r
108 /** SLA+R has been transmitted, ACK has been received */
\r
109 #define I2C_I2STAT_M_RX_SLAR_ACK ((0x40))
\r
110 /** SLA+R has been transmitted, NACK has been received */
\r
111 #define I2C_I2STAT_M_RX_SLAR_NACK ((0x48))
\r
112 /** Data has been received, ACK has been returned */
\r
113 #define I2C_I2STAT_M_RX_DAT_ACK ((0x50))
\r
114 /** Data has been received, NACK has been return */
\r
115 #define I2C_I2STAT_M_RX_DAT_NACK ((0x58))
\r
117 /* Slave receive mode -------------------------------------------- */
\r
118 /** Own slave address has been received, ACK has been returned */
\r
119 #define I2C_I2STAT_S_RX_SLAW_ACK ((0x60))
\r
121 /** Arbitration lost in SLA+R/W as master */
\r
122 #define I2C_I2STAT_S_RX_ARB_LOST_M_SLA ((0x68))
\r
123 /** Own SLA+W has been received, ACK returned */
\r
124 //#define I2C_I2STAT_S_RX_SLAW_ACK ((0x68))
\r
126 /** General call address has been received, ACK has been returned */
\r
127 #define I2C_I2STAT_S_RX_GENCALL_ACK ((0x70))
\r
129 /** Arbitration lost in SLA+R/W (GENERAL CALL) as master */
\r
130 #define I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL ((0x78))
\r
131 /** General call address has been received, ACK has been returned */
\r
132 //#define I2C_I2STAT_S_RX_GENCALL_ACK ((0x78))
\r
134 /** Previously addressed with own SLV address;
\r
135 * Data has been received, ACK has been return */
\r
136 #define I2C_I2STAT_S_RX_PRE_SLA_DAT_ACK ((0x80))
\r
137 /** Previously addressed with own SLA;
\r
138 * Data has been received and NOT ACK has been return */
\r
139 #define I2C_I2STAT_S_RX_PRE_SLA_DAT_NACK ((0x88))
\r
140 /** Previously addressed with General Call;
\r
141 * Data has been received and ACK has been return */
\r
142 #define I2C_I2STAT_S_RX_PRE_GENCALL_DAT_ACK ((0x90))
\r
143 /** Previously addressed with General Call;
\r
144 * Data has been received and NOT ACK has been return */
\r
145 #define I2C_I2STAT_S_RX_PRE_GENCALL_DAT_NACK ((0x98))
\r
146 /** A STOP condition or repeated START condition has
\r
147 * been received while still addressed as SLV/REC
\r
148 * (Slave Receive) or SLV/TRX (Slave Transmit) */
\r
149 #define I2C_I2STAT_S_RX_STA_STO_SLVREC_SLVTRX ((0xA0))
\r
151 /** Slave transmit mode */
\r
152 /** Own SLA+R has been received, ACK has been returned */
\r
153 #define I2C_I2STAT_S_TX_SLAR_ACK ((0xA8))
\r
155 /** Arbitration lost in SLA+R/W as master */
\r
156 #define I2C_I2STAT_S_TX_ARB_LOST_M_SLA ((0xB0))
\r
157 /** Own SLA+R has been received, ACK has been returned */
\r
158 //#define I2C_I2STAT_S_TX_SLAR_ACK ((0xB0))
\r
160 /** Data has been transmitted, ACK has been received */
\r
161 #define I2C_I2STAT_S_TX_DAT_ACK ((0xB8))
\r
162 /** Data has been transmitted, NACK has been received */
\r
163 #define I2C_I2STAT_S_TX_DAT_NACK ((0xC0))
\r
164 /** Last data byte in I2DAT has been transmitted (AA = 0);
\r
165 ACK has been received */
\r
166 #define I2C_I2STAT_S_TX_LAST_DAT_ACK ((0xC8))
\r
168 /** Time out in case of using I2C slave mode */
\r
169 #define I2C_SLAVE_TIME_OUT 0x10000UL
\r
171 /********************************************************************//**
\r
172 * I2C Data register definition
\r
173 *********************************************************************/
\r
174 /** Mask for I2DAT register*/
\r
175 #define I2C_I2DAT_BITMASK ((0xFF))
\r
177 /** Idle data value will be send out in slave mode in case of the actual
\r
178 * expecting data requested from the master is greater than its sending data
\r
179 * length that can be supported */
\r
180 #define I2C_I2DAT_IDLE_CHAR (0xFF)
\r
182 /********************************************************************//**
\r
183 * I2C Monitor mode control register description
\r
184 *********************************************************************/
\r
185 #define I2C_I2MMCTRL_MM_ENA ((1<<0)) /**< Monitor mode enable */
\r
186 #define I2C_I2MMCTRL_ENA_SCL ((1<<1)) /**< SCL output enable */
\r
187 #define I2C_I2MMCTRL_MATCH_ALL ((1<<2)) /**< Select interrupt register match */
\r
188 #define I2C_I2MMCTRL_BITMASK ((0x07)) /**< Mask for I2MMCTRL register */
\r
190 /********************************************************************//**
\r
191 * I2C Data buffer register description
\r
192 *********************************************************************/
\r
193 /** I2C Data buffer register bit mask */
\r
194 #define I2DATA_BUFFER_BITMASK ((0xFF))
\r
196 /********************************************************************//**
\r
197 * I2C Slave Address registers definition
\r
198 *********************************************************************/
\r
199 /** General Call enable bit */
\r
200 #define I2C_I2ADR_GC ((1<<0))
\r
201 /** I2C Slave Address registers bit mask */
\r
202 #define I2C_I2ADR_BITMASK ((0xFF))
\r
204 /********************************************************************//**
\r
205 * I2C Mask Register definition
\r
206 *********************************************************************/
\r
207 /** I2C Mask Register mask field */
\r
208 #define I2C_I2MASK_MASK(n) ((n&0xFE))
\r
210 /********************************************************************//**
\r
211 * I2C SCL HIGH duty cycle Register definition
\r
212 *********************************************************************/
\r
213 /** I2C SCL HIGH duty cycle Register bit mask */
\r
214 #define I2C_I2SCLH_BITMASK ((0xFFFF))
\r
216 /********************************************************************//**
\r
217 * I2C SCL LOW duty cycle Register definition
\r
218 *********************************************************************/
\r
219 /** I2C SCL LOW duty cycle Register bit mask */
\r
220 #define I2C_I2SCLL_BITMASK ((0xFFFF))
\r
222 /* I2C status values */
\r
223 #define I2C_SETUP_STATUS_ARBF (1<<8) /**< Arbitration false */
\r
224 #define I2C_SETUP_STATUS_NOACKF (1<<9) /**< No ACK returned */
\r
225 #define I2C_SETUP_STATUS_DONE (1<<10) /**< Status DONE */
\r
227 /*********************************************************************//**
\r
228 * I2C monitor control configuration defines
\r
229 **********************************************************************/
\r
230 #define I2C_MONITOR_CFG_SCL_OUTPUT I2C_I2MMCTRL_ENA_SCL /**< SCL output enable */
\r
231 #define I2C_MONITOR_CFG_MATCHALL I2C_I2MMCTRL_MATCH_ALL /**< Select interrupt register match */
\r
233 /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
\r
234 /* Macros check I2C slave address */
\r
235 #define PARAM_I2C_SLAVEADDR_CH(n) (n<=3)
\r
237 /** Macro to determine if it is valid SSP port number */
\r
238 #define PARAM_I2Cx(n) ((((uint32_t *)n)==((uint32_t *)LPC_I2C0)) \
\r
239 || (((uint32_t *)n)==((uint32_t *)LPC_I2C1)))
\r
241 /* Macros check I2C monitor configuration type */
\r
242 #define PARAM_I2C_MONITOR_CFG(n) ((n==I2C_MONITOR_CFG_SCL_OUTPUT) || (I2C_MONITOR_CFG_MATCHALL))
\r
250 /* Public Types --------------------------------------------------------------- */
\r
251 /** @defgroup I2C_Public_Types I2C Public Types
\r
256 * @brief I2C Own slave address setting structure
259 uint8_t SlaveAddrChannel; /**< Slave Address channel in I2C control,
\r
260 should be in range from 0..3
\r
262 uint8_t SlaveAddr_7bit; /**< Value of 7-bit slave address */
\r
263 uint8_t GeneralCallState; /**< Enable/Disable General Call Functionality
\r
264 when I2C control being in Slave mode, should be:
\r
265 - ENABLE: Enable General Call function.
\r
266 - DISABLE: Disable General Call function.
\r
268 uint8_t SlaveAddrMaskValue; /**< Any bit in this 8-bit value (bit 7:1)
\r
269 which is set to '1' will cause an automatic compare on
\r
270 the corresponding bit of the received address when it
\r
271 is compared to the SlaveAddr_7bit value associated with this
\r
272 mask register. In other words, bits in SlaveAddr_7bit value
\r
273 which are masked are not taken into account in determining
\r
276 } I2C_OWNSLAVEADDR_CFG_Type;
\r
280 * @brief Master transfer setup data structure definitions
\r
284 uint32_t sl_addr7bit; /**< Slave address in 7bit mode */
\r
285 uint8_t* tx_data; /**< Pointer to Transmit data - NULL if data transmit
\r
287 uint32_t tx_length; /**< Transmit data length - 0 if data transmit
\r
289 uint32_t tx_count; /**< Current Transmit data counter */
\r
290 uint8_t* rx_data; /**< Pointer to Receive data - NULL if data receive
\r
292 uint32_t rx_length; /**< Receive data length - 0 if data receive is
\r
294 uint32_t rx_count; /**< Current Receive data counter */
\r
295 uint32_t retransmissions_max; /**< Max Re-Transmission value */
\r
296 uint32_t retransmissions_count; /**< Current Re-Transmission counter */
\r
297 uint32_t status; /**< Current status of I2C activity */
\r
298 void (*callback)(void); /**< Pointer to Call back function when transmission complete
\r
299 used in interrupt transfer mode */
\r
300 } I2C_M_SETUP_Type;
\r
304 * @brief Slave transfer setup data structure definitions
\r
308 uint8_t* tx_data; /**< Pointer to transmit data - NULL if data transmit is not used */
\r
309 uint32_t tx_length; /**< Transmit data length - 0 if data transmit is not used */
\r
310 uint32_t tx_count; /**< Current transmit data counter */
\r
311 uint8_t* rx_data; /**< Pointer to receive data - NULL if data received is not used */
\r
312 uint32_t rx_length; /**< Receive data length - 0 if data receive is not used */
\r
313 uint32_t rx_count; /**< Current receive data counter */
\r
314 uint32_t status; /**< Current status of I2C activity */
\r
315 void (*callback)(void); /**< Pointer to call-back function when transmission complete
\r
316 used by interrupt transfer mode */
\r
317 } I2C_S_SETUP_Type;
\r
320 * @brief Transfer option type definitions
\r
323 I2C_TRANSFER_POLLING = 0, /**< Transfer in polling mode */
\r
324 I2C_TRANSFER_INTERRUPT /**< Transfer in interrupt mode */
\r
325 } I2C_TRANSFER_OPT_Type;
\r
333 /* Public Functions ----------------------------------------------------------- */
\r
334 /** @defgroup I2C_Public_Functions I2C Public Functions
\r
338 /* I2C Init/DeInit functions ---------- */
\r
339 void I2C_Init(LPC_I2Cn_Type *I2Cx, uint32_t clockrate);
\r
340 void I2C_DeInit(LPC_I2Cn_Type* I2Cx);
\r
341 //void I2C_SetClock (LPC_I2Cn_Type *I2Cx, uint32_t target_clock);
\r
342 void I2C_Cmd(LPC_I2Cn_Type* I2Cx, FunctionalState NewState);
\r
344 /* I2C transfer data functions -------- */
\r
345 Status I2C_MasterTransferData(LPC_I2Cn_Type *I2Cx, \
\r
346 I2C_M_SETUP_Type *TransferCfg, I2C_TRANSFER_OPT_Type Opt);
\r
347 Status I2C_SlaveTransferData(LPC_I2Cn_Type *I2Cx, \
\r
348 I2C_S_SETUP_Type *TransferCfg, I2C_TRANSFER_OPT_Type Opt);
\r
349 uint32_t I2C_MasterTransferComplete(LPC_I2Cn_Type *I2Cx);
\r
350 uint32_t I2C_SlaveTransferComplete(LPC_I2Cn_Type *I2Cx);
\r
353 void I2C_SetOwnSlaveAddr(LPC_I2Cn_Type *I2Cx, I2C_OWNSLAVEADDR_CFG_Type *OwnSlaveAddrConfigStruct);
\r
354 uint8_t I2C_GetLastStatusCode(LPC_I2Cn_Type* I2Cx);
\r
356 /* I2C Monitor functions ---------------*/
\r
357 void I2C_MonitorModeConfig(LPC_I2Cn_Type *I2Cx, uint32_t MonitorCfgType, FunctionalState NewState);
\r
358 void I2C_MonitorModeCmd(LPC_I2Cn_Type *I2Cx, FunctionalState NewState);
\r
359 uint8_t I2C_MonitorGetDatabuffer(LPC_I2Cn_Type *I2Cx);
\r
360 BOOL_8 I2C_MonitorHandler(LPC_I2Cn_Type *I2Cx, uint8_t *buffer, uint32_t size);
\r
362 /* I2C Interrupt handler functions ------*/
\r
363 void I2C_IntCmd (LPC_I2Cn_Type *I2Cx, Bool NewState);
\r
364 void I2C_MasterHandler (LPC_I2Cn_Type *I2Cx);
\r
365 void I2C_SlaveHandler (LPC_I2Cn_Type *I2Cx);
\r
377 #endif /* LPC18XX_I2C_H_ */
\r
383 /* --------------------------------- End Of File ------------------------------ */
\r