lpc_i2c.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**********************************************************************
  2. * $Id$ lpc_i2c.h 2011-06-02
  3. *//**
  4. * @file lpc_i2c.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for I2C firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @defgroup I2C I2C (Inter-IC Control bus)
  34. * @ingroup LPC_CMSIS_FwLib_Drivers
  35. * @{
  36. */
  37. #ifndef __LPC_I2C_H_
  38. #define __LPC_I2C_H_
  39. /* Includes ------------------------------------------------------------------- */
  40. #include "LPC407x_8x_177x_8x.h"
  41. #include "lpc_types.h"
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. /* Private Macros ------------------------------------------------------------- */
  47. /** @defgroup I2C_Private_Macros I2C Private Macros
  48. * @{
  49. */
  50. /* --------------------- BIT DEFINITIONS -------------------------------------- */
  51. /*******************************************************************//**
  52. * I2C Control Set register description
  53. *********************************************************************/
  54. #define I2C_I2CONSET_AA ((0x04)) /*!< Assert acknowledge flag */
  55. #define I2C_I2CONSET_SI ((0x08)) /*!< I2C interrupt flag */
  56. #define I2C_I2CONSET_STO ((0x10)) /*!< STOP flag */
  57. #define I2C_I2CONSET_STA ((0x20)) /*!< START flag */
  58. #define I2C_I2CONSET_I2EN ((0x40)) /*!< I2C interface enable */
  59. /*******************************************************************//**
  60. * I2C Control Clear register description
  61. *********************************************************************/
  62. /** Assert acknowledge Clear bit */
  63. #define I2C_I2CONCLR_AAC ((1<<2))
  64. /** I2C interrupt Clear bit */
  65. #define I2C_I2CONCLR_SIC ((1<<3))
  66. /** I2C STOP Clear bit */
  67. #define I2C_I2CONCLR_STOC ((1<<4))
  68. /** START flag Clear bit */
  69. #define I2C_I2CONCLR_STAC ((1<<5))
  70. /** I2C interface Disable bit */
  71. #define I2C_I2CONCLR_I2ENC ((1<<6))
  72. /********************************************************************//**
  73. * I2C Status Code definition (I2C Status register)
  74. *********************************************************************/
  75. /* Return Code in I2C status register */
  76. #define I2C_STAT_CODE_BITMASK ((0xF8))
  77. /* I2C return status code definitions ----------------------------- */
  78. /** No relevant information */
  79. #define I2C_I2STAT_NO_INF ((0xF8))
  80. /** Bus Error */
  81. #define I2C_I2STAT_BUS_ERROR ((0x00))
  82. /* Master transmit mode -------------------------------------------- */
  83. /** A start condition has been transmitted */
  84. #define I2C_I2STAT_M_TX_START ((0x08))
  85. /** A repeat start condition has been transmitted */
  86. #define I2C_I2STAT_M_TX_RESTART ((0x10))
  87. /** SLA+W has been transmitted, ACK has been received */
  88. #define I2C_I2STAT_M_TX_SLAW_ACK ((0x18))
  89. /** SLA+W has been transmitted, NACK has been received */
  90. #define I2C_I2STAT_M_TX_SLAW_NACK ((0x20))
  91. /** Data has been transmitted, ACK has been received */
  92. #define I2C_I2STAT_M_TX_DAT_ACK ((0x28))
  93. /** Data has been transmitted, NACK has been received */
  94. #define I2C_I2STAT_M_TX_DAT_NACK ((0x30))
  95. /** Arbitration lost in SLA+R/W or Data bytes */
  96. #define I2C_I2STAT_M_TX_ARB_LOST ((0x38))
  97. /* Master receive mode -------------------------------------------- */
  98. /** A start condition has been transmitted */
  99. #define I2C_I2STAT_M_RX_START ((0x08))
  100. /** A repeat start condition has been transmitted */
  101. #define I2C_I2STAT_M_RX_RESTART ((0x10))
  102. /** Arbitration lost */
  103. #define I2C_I2STAT_M_RX_ARB_LOST ((0x38))
  104. /** SLA+R has been transmitted, ACK has been received */
  105. #define I2C_I2STAT_M_RX_SLAR_ACK ((0x40))
  106. /** SLA+R has been transmitted, NACK has been received */
  107. #define I2C_I2STAT_M_RX_SLAR_NACK ((0x48))
  108. /** Data has been received, ACK has been returned */
  109. #define I2C_I2STAT_M_RX_DAT_ACK ((0x50))
  110. /** Data has been received, NACK has been return */
  111. #define I2C_I2STAT_M_RX_DAT_NACK ((0x58))
  112. /* Slave receive mode -------------------------------------------- */
  113. /** Own slave address has been received, ACK has been returned */
  114. #define I2C_I2STAT_S_RX_SLAW_ACK ((0x60))
  115. /** Arbitration lost in SLA+R/W as master */
  116. #define I2C_I2STAT_S_RX_ARB_LOST_M_SLA ((0x68))
  117. /** General call address has been received, ACK has been returned */
  118. #define I2C_I2STAT_S_RX_GENCALL_ACK ((0x70))
  119. /** Arbitration lost in SLA+R/W (GENERAL CALL) as master */
  120. #define I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL ((0x78))
  121. /** Previously addressed with own SLV address;
  122. * Data has been received, ACK has been return */
  123. #define I2C_I2STAT_S_RX_PRE_SLA_DAT_ACK ((0x80))
  124. /** Previously addressed with own SLA;
  125. * Data has been received and NOT ACK has been return */
  126. #define I2C_I2STAT_S_RX_PRE_SLA_DAT_NACK ((0x88))
  127. /** Previously addressed with General Call;
  128. * Data has been received and ACK has been return */
  129. #define I2C_I2STAT_S_RX_PRE_GENCALL_DAT_ACK ((0x90))
  130. /** Previously addressed with General Call;
  131. * Data has been received and NOT ACK has been return */
  132. #define I2C_I2STAT_S_RX_PRE_GENCALL_DAT_NACK ((0x98))
  133. /** A STOP condition or repeated START condition has
  134. * been received while still addressed as SLV/REC
  135. * (Slave Receive) or SLV/TRX (Slave Transmit) */
  136. #define I2C_I2STAT_S_RX_STA_STO_SLVREC_SLVTRX ((0xA0))
  137. /** Slave transmit mode */
  138. /** Own SLA+R has been received, ACK has been returned */
  139. #define I2C_I2STAT_S_TX_SLAR_ACK ((0xA8))
  140. /** Arbitration lost in SLA+R/W as master */
  141. #define I2C_I2STAT_S_TX_ARB_LOST_M_SLA ((0xB0))
  142. /** Data has been transmitted, ACK has been received */
  143. #define I2C_I2STAT_S_TX_DAT_ACK ((0xB8))
  144. /** Data has been transmitted, NACK has been received */
  145. #define I2C_I2STAT_S_TX_DAT_NACK ((0xC0))
  146. /** Last data byte in I2DAT has been transmitted (AA = 0);
  147. ACK has been received */
  148. #define I2C_I2STAT_S_TX_LAST_DAT_ACK ((0xC8))
  149. /** Time out in case of using I2C slave mode */
  150. #define I2C_SLAVE_TIME_OUT 0x10000UL
  151. /********************************************************************//**
  152. * I2C Data register definition
  153. *********************************************************************/
  154. /** Mask for I2DAT register*/
  155. #define I2C_I2DAT_BITMASK ((0xFF))
  156. /** Idle data value will be send out in slave mode in case of the actual
  157. * expecting data requested from the master is greater than its sending data
  158. * length that can be supported */
  159. #define I2C_I2DAT_IDLE_CHAR (0xFF)
  160. /********************************************************************//**
  161. * I2C Monitor mode control register description
  162. *********************************************************************/
  163. #define I2C_I2MMCTRL_MM_ENA ((1<<0)) /**< Monitor mode enable */
  164. #define I2C_I2MMCTRL_ENA_SCL ((1<<1)) /**< SCL output enable */
  165. #define I2C_I2MMCTRL_MATCH_ALL ((1<<2)) /**< Select interrupt register match */
  166. #define I2C_I2MMCTRL_BITMASK ((0x07)) /**< Mask for I2MMCTRL register */
  167. /********************************************************************//**
  168. * I2C Data buffer register description
  169. *********************************************************************/
  170. /** I2C Data buffer register bit mask */
  171. #define I2DATA_BUFFER_BITMASK ((0xFF))
  172. /********************************************************************//**
  173. * I2C Slave Address registers definition
  174. *********************************************************************/
  175. /** General Call enable bit */
  176. #define I2C_I2ADR_GC ((1<<0))
  177. /** I2C Slave Address registers bit mask */
  178. #define I2C_I2ADR_BITMASK ((0xFF))
  179. /********************************************************************//**
  180. * I2C Mask Register definition
  181. *********************************************************************/
  182. /** I2C Mask Register mask field */
  183. #define I2C_I2MASK_MASK(n) ((n&0xFE))
  184. /********************************************************************//**
  185. * I2C SCL HIGH duty cycle Register definition
  186. *********************************************************************/
  187. /** I2C SCL HIGH duty cycle Register bit mask */
  188. #define I2C_I2SCLH_BITMASK ((0xFFFF))
  189. /********************************************************************//**
  190. * I2C SCL LOW duty cycle Register definition
  191. *********************************************************************/
  192. /** I2C SCL LOW duty cycle Register bit mask */
  193. #define I2C_I2SCLL_BITMASK ((0xFFFF))
  194. /* I2C status values */
  195. #define I2C_SETUP_STATUS_ARBF (1<<8) /**< Arbitration false */
  196. #define I2C_SETUP_STATUS_NOACKF (1<<9) /**< No ACK returned */
  197. #define I2C_SETUP_STATUS_DONE (1<<10) /**< Status DONE */
  198. /* I2C state handle return values */
  199. #define I2C_OK 0x00
  200. #define I2C_BYTE_SENT 0x01
  201. #define I2C_BYTE_RECV 0x02
  202. #define I2C_LAST_BYTE_RECV 0x04
  203. #define I2C_SEND_END 0x08
  204. #define I2C_RECV_END 0x10
  205. #define I2C_STA_STO_RECV 0x20
  206. #define I2C_ERR (0x10000000)
  207. #define I2C_NAK_RECV (0x10000000 |0x01)
  208. #define I2C_CheckError(ErrorCode) (ErrorCode & 0x10000000)
  209. /**
  210. * @}
  211. */
  212. /* Public Types --------------------------------------------------------------- */
  213. /** @defgroup I2C_Public_Types I2C Public Types
  214. * @{
  215. */
  216. typedef enum
  217. {
  218. I2C_0 = 0,
  219. I2C_1,
  220. I2C_2
  221. } en_I2C_unitId;
  222. typedef enum
  223. {
  224. I2C_MASTER_MODE,
  225. I2C_SLAVE_MODE,
  226. I2C_GENERAL_MODE,
  227. } en_I2C_Mode;
  228. /**
  229. * @brief I2C Own slave address setting structure
  230. */
  231. typedef struct
  232. {
  233. uint8_t SlaveAddrChannel; /**< Slave Address channel in I2C control,
  234. should be in range from 0..3
  235. */
  236. uint8_t SlaveAddr_7bit; /**< Value of 7-bit slave address */
  237. uint8_t GeneralCallState; /**< Enable/Disable General Call Functionality
  238. when I2C control being in Slave mode, should be:
  239. - ENABLE: Enable General Call function.
  240. - DISABLE: Disable General Call function.
  241. */
  242. uint8_t SlaveAddrMaskValue; /**< Any bit in this 8-bit value (bit 7:1)
  243. which is set to '1' will cause an automatic compare on
  244. the corresponding bit of the received address when it
  245. is compared to the SlaveAddr_7bit value associated with this
  246. mask register. In other words, bits in SlaveAddr_7bit value
  247. which are masked are not taken into account in determining
  248. an address match
  249. */
  250. } I2C_OWNSLAVEADDR_CFG_Type;
  251. /**
  252. * @brief Master transfer setup data structure definitions
  253. */
  254. typedef struct
  255. {
  256. uint32_t sl_addr7bit; /**< Slave address in 7bit mode */
  257. __IO uint8_t* tx_data; /**< Pointer to Transmit data - NULL if data transmit
  258. is not used */
  259. uint32_t tx_length; /**< Transmit data length - 0 if data transmit
  260. is not used*/
  261. __IO uint32_t tx_count; /**< Current Transmit data counter */
  262. __IO uint8_t* rx_data; /**< Pointer to Receive data - NULL if data receive
  263. is not used */
  264. uint32_t rx_length; /**< Receive data length - 0 if data receive is
  265. not used */
  266. __IO uint32_t rx_count; /**< Current Receive data counter */
  267. uint32_t retransmissions_max; /**< Max Re-Transmission value */
  268. uint32_t retransmissions_count; /**< Current Re-Transmission counter */
  269. __IO uint32_t status; /**< Current status of I2C activity */
  270. void (*callback)(void); /**< Pointer to Call back function when transmission complete
  271. used in interrupt transfer mode */
  272. } I2C_M_SETUP_Type;
  273. /**
  274. * @brief Slave transfer setup data structure definitions
  275. */
  276. typedef struct
  277. {
  278. __IO uint8_t* tx_data;
  279. uint32_t tx_length;
  280. __IO uint32_t tx_count;
  281. __IO uint8_t* rx_data;
  282. uint32_t rx_length;
  283. __IO uint32_t rx_count;
  284. __IO uint32_t status;
  285. void (*callback)(void);
  286. } I2C_S_SETUP_Type;
  287. /**
  288. * @brief Transfer option type definitions
  289. */
  290. typedef enum
  291. {
  292. I2C_TRANSFER_POLLING = 0, /**< Transfer in polling mode */
  293. I2C_TRANSFER_INTERRUPT /**< Transfer in interrupt mode */
  294. } I2C_TRANSFER_OPT_Type;
  295. /**
  296. * @}
  297. */
  298. /* Public Functions ----------------------------------------------------------- */
  299. /** @defgroup I2C_Public_Functions I2C Public Functions
  300. * @{
  301. */
  302. /* I2C Init/DeInit functions ---------- */
  303. void I2C_Init(en_I2C_unitId i2cId, uint32_t clockrate);
  304. void I2C_DeInit(en_I2C_unitId i2cId);
  305. void I2C_Cmd(en_I2C_unitId i2cId, en_I2C_Mode Mode, FunctionalState NewState);
  306. /* I2C transfer data functions -------- */
  307. Status I2C_MasterTransferData(en_I2C_unitId i2cId,
  308. I2C_M_SETUP_Type *TransferCfg, I2C_TRANSFER_OPT_Type Opt);
  309. Status I2C_SlaveTransferData(en_I2C_unitId i2cId,
  310. I2C_S_SETUP_Type *TransferCfg, I2C_TRANSFER_OPT_Type Opt);
  311. uint32_t I2C_MasterTransferComplete(en_I2C_unitId i2cId);
  312. uint32_t I2C_SlaveTransferComplete(en_I2C_unitId i2cId);
  313. void I2C_SetOwnSlaveAddr(en_I2C_unitId i2cId, I2C_OWNSLAVEADDR_CFG_Type *OwnSlaveAddrConfigStruct);
  314. uint8_t I2C_GetLastStatusCode(en_I2C_unitId i2cId);
  315. /* I2C Monitor functions ---------------*/
  316. void I2C_MonitorModeConfig(en_I2C_unitId i2cId, uint32_t MonitorCfgType, FunctionalState NewState);
  317. void I2C_MonitorModeCmd(en_I2C_unitId i2cId, FunctionalState NewState);
  318. uint8_t I2C_MonitorGetDatabuffer(en_I2C_unitId i2cId);
  319. BOOL_8 I2C_MonitorHandler(en_I2C_unitId i2cId, uint8_t *buffer, uint32_t size);
  320. /* I2C Interrupt handler functions ------*/
  321. void I2C_IntCmd (en_I2C_unitId i2cId, Bool NewState);
  322. void I2C_MasterHandler (en_I2C_unitId i2cId);
  323. void I2C_SlaveHandler (en_I2C_unitId i2cId);
  324. /**
  325. * @}
  326. */
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. #endif /* __LPC_I2C_H_ */
  331. /**
  332. * @}
  333. */
  334. /* --------------------------------- End Of File ------------------------------ */