i2c_8xx.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * @brief LPC15xx I2C driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __I2C_COMMON_8XX_H_
  32. #define __I2C_COMMON_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup I2C_15XX CHIP: LPC15xx I2C driver
  37. * @ingroup CHIP_15XX_Drivers
  38. * @{
  39. */
  40. /**
  41. * @brief I2C register block structure
  42. */
  43. typedef struct { /* I2C0 Structure */
  44. __IO uint32_t CFG; /*!< I2C Configuration Register common for Master, Slave and Monitor */
  45. __IO uint32_t STAT; /*!< I2C Status Register common for Master, Slave and Monitor */
  46. __IO uint32_t INTENSET; /*!< I2C Interrupt Enable Set Register common for Master, Slave and Monitor */
  47. __O uint32_t INTENCLR; /*!< I2C Interrupt Enable Clear Register common for Master, Slave and Monitor */
  48. __IO uint32_t TIMEOUT; /*!< I2C Timeout value Register */
  49. __IO uint32_t CLKDIV; /*!< I2C Clock Divider Register */
  50. __I uint32_t INTSTAT; /*!< I2C Interrupt Status Register */
  51. __I uint32_t RESERVED0;
  52. __IO uint32_t MSTCTL; /*!< I2C Master Control Register */
  53. __IO uint32_t MSTTIME; /*!< I2C Master Time Register for SCL */
  54. __IO uint32_t MSTDAT; /*!< I2C Master Data Register */
  55. __I uint32_t RESERVED1[5];
  56. __IO uint32_t SLVCTL; /*!< I2C Slave Control Register */
  57. __IO uint32_t SLVDAT; /*!< I2C Slave Data Register */
  58. __IO uint32_t SLVADR[4]; /*!< I2C Slave Address Registers */
  59. __IO uint32_t SLVQUAL0; /*!< I2C Slave Address Qualifier 0 Register */
  60. __I uint32_t RESERVED2[9];
  61. __I uint32_t MONRXDAT; /*!< I2C Monitor Data Register */
  62. } LPC_I2C_T;
  63. /* Reserved bits masks for registers */
  64. #define I2C_CFG_RESERVED (~0x1f)
  65. #define I2C_STAT_RESERVED ((1<<5)|(1<<7)|(0xf<<20)|(0x3fu<<26))
  66. #define I2C_INTENSET_RESERVED ((7<<1)|(1<<5)|(1<<7)|(3<<9)|(7<<12)|(1<<18)|(0xf<<20)|(0x3fu<<26))
  67. #define I2C_INTENCLR_RESERVED ((7<<1)|(1<<5)|(1<<7)|(3<<9)|(7<<12)|(1<<18)|(0xf<<20)|(0x3fu<<26))
  68. #define I2C_TIMEOUT_RESERVED 0xffff0000
  69. #define I2C_CLKDIV_RESERVED 0xffff0000
  70. #define I2C_INTSTAT_RESERVED ((7<<1)|(1<<5)|(1<<7)|(3<<9)|(7<<12)|(1<<18)|(0xf<<20)|(0x3fu<<26))
  71. #define I2C_MSTCTL_RESERVED (~7)
  72. #define I2C_MSTTIME_RESERVED (~0x7f)
  73. #define I2C_MSTDAT_RESERVED (~0xff)
  74. #define I2C_SLVCTL_RESERVED (~3)
  75. #define I2C_SLVDAT_RESERVED (~0xff)
  76. #define I2C_SLVADR_RESERVED (~0xff)
  77. #define I2C_SLVQUAL0_RESERVED (~0xff)
  78. /*
  79. * @brief I2C Configuration register Bit definition
  80. */
  81. #define I2C_CFG_MSTEN (1 << 0) /*!< Master Enable/Disable Bit */
  82. #define I2C_CFG_SLVEN (1 << 1) /*!< Slave Enable/Disable Bit */
  83. #define I2C_CFG_MONEN (1 << 2) /*!< Monitor Enable/Disable Bit */
  84. #define I2C_CFG_TIMEOUTEN (1 << 3) /*!< Timeout Enable/Disable Bit */
  85. #define I2C_CFG_MONCLKSTR (1 << 4) /*!< Monitor Clock Stretching Bit */
  86. #define I2C_CFG_MASK ((uint32_t) 0x1F) /*!< Configuration register Mask */
  87. /*
  88. * @brief I2C Status register Bit definition
  89. */
  90. #define I2C_STAT_MSTPENDING (1 << 0) /*!< Master Pending Status Bit */
  91. #define I2C_STAT_MSTSTATE (0x7 << 1) /*!< Master State Code */
  92. #define I2C_STAT_MSTRARBLOSS (1 << 4) /*!< Master Arbitration Loss Bit */
  93. #define I2C_STAT_MSTSTSTPERR (1 << 6) /*!< Master Start Stop Error Bit */
  94. #define I2C_STAT_SLVPENDING (1 << 8) /*!< Slave Pending Status Bit */
  95. #define I2C_STAT_SLVSTATE (0x3 << 9) /*!< Slave State Code */
  96. #define I2C_STAT_SLVNOTSTR (1 << 11) /*!< Slave not stretching Clock Bit */
  97. #define I2C_STAT_SLVIDX (0x3 << 12) /*!< Slave Address Index */
  98. #define I2C_STAT_SLVSEL (1 << 14) /*!< Slave Selected Bit */
  99. #define I2C_STAT_SLVDESEL (1 << 15) /*!< Slave Deselect Bit */
  100. #define I2C_STAT_MONRDY (1 << 16) /*!< Monitor Ready Bit */
  101. #define I2C_STAT_MONOV (1 << 17) /*!< Monitor Overflow Flag */
  102. #define I2C_STAT_MONACTIVE (1 << 18) /*!< Monitor Active Flag */
  103. #define I2C_STAT_MONIDLE (1 << 19) /*!< Monitor Idle Flag */
  104. #define I2C_STAT_EVENTTIMEOUT (1 << 24) /*!< Event Timeout Interrupt Flag */
  105. #define I2C_STAT_SCLTIMEOUT (1 << 25) /*!< SCL Timeout Interrupt Flag */
  106. #define I2C_STAT_MSTCODE_IDLE (0) /*!< Master Idle State Code */
  107. #define I2C_STAT_MSTCODE_RXREADY (1) /*!< Master Receive Ready State Code */
  108. #define I2C_STAT_MSTCODE_TXREADY (2) /*!< Master Transmit Ready State Code */
  109. #define I2C_STAT_MSTCODE_NACKADR (3) /*!< Master NACK by slave on address State Code */
  110. #define I2C_STAT_MSTCODE_NACKDAT (4) /*!< Master NACK by slave on data State Code */
  111. #define I2C_STAT_SLVCODE_ADDR (0) /*!< Master Idle State Code */
  112. #define I2C_STAT_SLVCODE_RX (1) /*!< Received data is available Code */
  113. #define I2C_STAT_SLVCODE_TX (2) /*!< Data can be transmitted Code */
  114. /*
  115. * @brief I2C Interrupt Enable Set register Bit definition
  116. */
  117. #define I2C_INTENSET_MSTPENDING (1 << 0) /*!< Master Pending Interrupt Enable Bit */
  118. #define I2C_INTENSET_MSTRARBLOSS (1 << 4) /*!< Master Arbitration Loss Interrupt Enable Bit */
  119. #define I2C_INTENSET_MSTSTSTPERR (1 << 6) /*!< Master Start Stop Error Interrupt Enable Bit */
  120. #define I2C_INTENSET_SLVPENDING (1 << 8) /*!< Slave Pending Interrupt Enable Bit */
  121. #define I2C_INTENSET_SLVNOTSTR (1 << 11) /*!< Slave not stretching Clock Interrupt Enable Bit */
  122. #define I2C_INTENSET_SLVDESEL (1 << 15) /*!< Slave Deselect Interrupt Enable Bit */
  123. #define I2C_INTENSET_MONRDY (1 << 16) /*!< Monitor Ready Interrupt Enable Bit */
  124. #define I2C_INTENSET_MONOV (1 << 17) /*!< Monitor Overflow Interrupt Enable Bit */
  125. #define I2C_INTENSET_MONIDLE (1 << 19) /*!< Monitor Idle Interrupt Enable Bit */
  126. #define I2C_INTENSET_EVENTTIMEOUT (1 << 24) /*!< Event Timeout Interrupt Enable Bit */
  127. #define I2C_INTENSET_SCLTIMEOUT (1 << 25) /*!< SCL Timeout Interrupt Enable Bit */
  128. /*
  129. * @brief I2C Interrupt Enable Clear register Bit definition
  130. */
  131. #define I2C_INTENCLR_MSTPENDING (1 << 0) /*!< Master Pending Interrupt Clear Bit */
  132. #define I2C_INTENCLR_MSTRARBLOSS (1 << 4) /*!< Master Arbitration Loss Interrupt Clear Bit */
  133. #define I2C_INTENCLR_MSTSTSTPERR (1 << 6) /*!< Master Start Stop Error Interrupt Clear Bit */
  134. #define I2C_INTENCLR_SLVPENDING (1 << 8) /*!< Slave Pending Interrupt Clear Bit */
  135. #define I2C_INTENCLR_SLVNOTSTR (1 << 11) /*!< Slave not stretching Clock Interrupt Clear Bit */
  136. #define I2C_INTENCLR_SLVDESEL (1 << 15) /*!< Slave Deselect Interrupt Clear Bit */
  137. #define I2C_INTENCLR_MONRDY (1 << 16) /*!< Monitor Ready Interrupt Clear Bit */
  138. #define I2C_INTENCLR_MONOV (1 << 17) /*!< Monitor Overflow Interrupt Clear Bit */
  139. #define I2C_INTENCLR_MONIDLE (1 << 19) /*!< Monitor Idle Interrupt Clear Bit */
  140. #define I2C_INTENCLR_EVENTTIMEOUT (1 << 24) /*!< Event Timeout Interrupt Clear Bit */
  141. #define I2C_INTENCLR_SCLTIMEOUT (1 << 25) /*!< SCL Timeout Interrupt Clear Bit */
  142. /*
  143. * @brief I2C TimeOut Value Macro
  144. */
  145. #define I2C_TIMEOUT_VAL(n) (((uint32_t) ((n) - 1) & 0xFFF0) | 0x000F) /*!< Macro for Timeout value register */
  146. /*
  147. * @brief I2C Interrupt Status register Bit definition
  148. */
  149. #define I2C_INTSTAT_MSTPENDING (1 << 0) /*!< Master Pending Interrupt Status Bit */
  150. #define I2C_INTSTAT_MSTRARBLOSS (1 << 4) /*!< Master Arbitration Loss Interrupt Status Bit */
  151. #define I2C_INTSTAT_MSTSTSTPERR (1 << 6) /*!< Master Start Stop Error Interrupt Status Bit */
  152. #define I2C_INTSTAT_SLVPENDING (1 << 8) /*!< Slave Pending Interrupt Status Bit */
  153. #define I2C_INTSTAT_SLVNOTSTR (1 << 11) /*!< Slave not stretching Clock Interrupt Status Bit */
  154. #define I2C_INTSTAT_SLVDESEL (1 << 15) /*!< Slave Deselect Interrupt Status Bit */
  155. #define I2C_INTSTAT_MONRDY (1 << 16) /*!< Monitor Ready Interrupt Status Bit */
  156. #define I2C_INTSTAT_MONOV (1 << 17) /*!< Monitor Overflow Interrupt Status Bit */
  157. #define I2C_INTSTAT_MONIDLE (1 << 19) /*!< Monitor Idle Interrupt Status Bit */
  158. #define I2C_INTSTAT_EVENTTIMEOUT (1 << 24) /*!< Event Timeout Interrupt Status Bit */
  159. #define I2C_INTSTAT_SCLTIMEOUT (1 << 25) /*!< SCL Timeout Interrupt Status Bit */
  160. /*
  161. * @brief I2C Master Control register Bit definition
  162. */
  163. #define I2C_MSTCTL_MSTCONTINUE (1 << 0) /*!< Master Continue Bit */
  164. #define I2C_MSTCTL_MSTSTART (1 << 1) /*!< Master Start Control Bit */
  165. #define I2C_MSTCTL_MSTSTOP (1 << 2) /*!< Master Stop Control Bit */
  166. #define I2C_MSTCTL_MSTDMA (1 << 3) /*!< Master DMA Enable Bit */
  167. /*
  168. * @brief I2C Master Time Register Field definition
  169. */
  170. #define I2C_MSTTIME_MSTSCLLOW (0x07 << 0) /*!< Master SCL Low Time field */
  171. #define I2C_MSTTIME_MSTSCLHIGH (0x07 << 4) /*!< Master SCL High Time field */
  172. /*
  173. * @brief I2C Master Data Mask
  174. */
  175. #define I2C_MSTDAT_DATAMASK ((uint32_t) 0x00FF << 0) /*!< Master data mask */
  176. /*
  177. * @brief I2C Slave Control register Bit definition
  178. */
  179. #define I2C_SLVCTL_SLVCONTINUE (1 << 0) /*!< Slave Continue Bit */
  180. #define I2C_SLVCTL_SLVNACK (1 << 1) /*!< Slave NACK Bit */
  181. #define I2C_SLVCTL_SLVDMA (1 << 3) /*!< Slave DMA Enable Bit */
  182. /*
  183. * @brief I2C Slave Data Mask
  184. */
  185. #define I2C_SLVDAT_DATAMASK ((uint32_t) 0x00FF << 0) /*!< Slave data mask */
  186. /*
  187. * @brief I2C Slave Address register Bit definition
  188. */
  189. #define I2C_SLVADR_SADISABLE (1 << 0) /*!< Slave Address n Disable Bit */
  190. #define I2C_SLVADR_SLVADR (0x7F << 1) /*!< Slave Address field */
  191. #define I2C_SLVADR_MASK ((uint32_t) 0x00FF) /*!< Slave Address Mask */
  192. /*
  193. * @brief I2C Slave Address Qualifier 0 Register Bit definition
  194. */
  195. #define I2C_SLVQUAL_QUALMODE0 (1 << 0) /*!< Slave Qualifier Mode Enable Bit */
  196. #define I2C_SLVQUAL_SLVQUAL0 (0x7F << 1) /*!< Slave Qualifier Address for Address 0 */
  197. /*
  198. * @brief I2C Monitor Data Register Bit definition
  199. */
  200. #define I2C_MONRXDAT_DATA (0xFF << 0) /*!< Monitor Function Receive Data Field */
  201. #define I2C_MONRXDAT_MONSTART (1 << 8) /*!< Monitor Received Start Bit */
  202. #define I2C_MONRXDAT_MONRESTART (1 << 9) /*!< Monitor Received Repeated Start Bit */
  203. #define I2C_MONRXDAT_MONNACK (1 << 10) /*!< Monitor Received Nack Bit */
  204. /**
  205. * @brief Initialize I2C Interface
  206. * @param pI2C : Pointer to selected I2C peripheral
  207. * @return Nothing
  208. * @note This function enables the I2C clock for both the master and
  209. * slave interfaces of the given I2C peripheral. LPC_I2C1, LPC_I2C2 and
  210. * LPC_I2C3 are available only on LPC82X devices.
  211. */
  212. void Chip_I2C_Init(LPC_I2C_T *pI2C);
  213. /**
  214. * @brief Shutdown I2C Interface
  215. * @param pI2C : Pointer to selected I2C peripheral
  216. * @return Nothing
  217. * @note This function disables the I2C clock for both the master and
  218. * slave interfaces of the given I2C peripheral. Only LPC_I2C0 is available
  219. * on LPC81X devices.
  220. */
  221. void Chip_I2C_DeInit(LPC_I2C_T *pI2C);
  222. /**
  223. * @brief Sets I2C Clock Divider registers
  224. * @param pI2C : Pointer to selected I2C peripheral
  225. * @param clkdiv : Clock Divider value for I2C, value is between (1 - 65536)
  226. * @return Nothing
  227. * @note The clock to I2C block is determined by the following formula (I2C_PCLK
  228. * is the frequency of the system clock): <br>
  229. * I2C Clock Frequency = (I2C_PCLK)/clkdiv;
  230. */
  231. static INLINE void Chip_I2C_SetClockDiv(LPC_I2C_T *pI2C, uint32_t clkdiv)
  232. {
  233. if ((clkdiv >= 1) && (clkdiv <= 65536)) {
  234. pI2C->CLKDIV = clkdiv - 1;
  235. }
  236. else {
  237. pI2C->CLKDIV = 0;
  238. }
  239. }
  240. /**
  241. * @brief Get I2C Clock Divider registers
  242. * @param pI2C : Pointer to selected I2C peripheral
  243. * @return Clock Divider value
  244. * @note Return the divider value for the I2C block
  245. * It is the CLKDIV register value + 1
  246. */
  247. static INLINE uint32_t Chip_I2C_GetClockDiv(LPC_I2C_T *pI2C)
  248. {
  249. return (pI2C->CLKDIV & 0xFFFF) + 1;
  250. }
  251. /**
  252. * @brief Enable I2C Interrupts
  253. * @param pI2C : Pointer to selected I2C peripheral
  254. * @param intEn : ORed Value of I2C_INTENSET_* values to enable
  255. * @return Nothing
  256. */
  257. static INLINE void Chip_I2C_EnableInt(LPC_I2C_T *pI2C, uint32_t intEn)
  258. {
  259. pI2C->INTENSET = intEn;
  260. }
  261. /**
  262. * @brief Disable I2C Interrupts
  263. * @param pI2C : Pointer to selected I2C peripheral
  264. * @param intClr : ORed Value of I2C_INTENSET_* values to disable
  265. * @return Nothing
  266. */
  267. static INLINE void Chip_I2C_DisableInt(LPC_I2C_T *pI2C, uint32_t intClr)
  268. {
  269. pI2C->INTENCLR = intClr;
  270. }
  271. /**
  272. * @brief Disable I2C Interrupts
  273. * @param pI2C : Pointer to selected I2C peripheral
  274. * @param intClr : ORed Value of I2C_INTENSET_* values to disable
  275. * @return Nothing
  276. * @note It is recommended to use the Chip_I2C_DisableInt() function
  277. * instead of this function.
  278. */
  279. static INLINE void Chip_I2C_ClearInt(LPC_I2C_T *pI2C, uint32_t intClr)
  280. {
  281. Chip_I2C_DisableInt(pI2C, intClr);
  282. }
  283. /**
  284. * @brief Returns pending I2C Interrupts
  285. * @param pI2C : Pointer to selected I2C peripheral
  286. * @return All pending interrupts, mask with I2C_INTENSET_* to determine specific interrupts
  287. */
  288. static INLINE uint32_t Chip_I2C_GetPendingInt(LPC_I2C_T *pI2C)
  289. {
  290. return pI2C->INTSTAT & ~I2C_INTSTAT_RESERVED;
  291. }
  292. /**
  293. * @}
  294. */
  295. /** @defgroup I2CM_8XX CHIP: LPC8XX I2C master-only driver
  296. * @ingroup I2C_8XX
  297. * This driver only works in master mode. To describe the I2C transactions
  298. * following symbols are used in driver documentation.
  299. *
  300. * Key to symbols
  301. * ==============
  302. * S (1 bit) : Start bit
  303. * P (1 bit) : Stop bit
  304. * Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.
  305. * A, NA (1 bit) : Acknowledge and Not-Acknowledge bit.
  306. * Addr (7 bits): I2C 7 bit address. Note that this can be expanded as usual to
  307. * get a 10 bit I2C address.
  308. * Data (8 bits): A plain data byte. Sometimes, I write DataLow, DataHigh
  309. * for 16 bit data.
  310. * [..]: Data sent by I2C device, as opposed to data sent by the host adapter.
  311. * @{
  312. */
  313. /** I2CM_8XX_STATUS_TYPES I2C master transfer status types
  314. * @{
  315. */
  316. #define I2CM_STATUS_OK 0x00 /*!< Requested Request was executed successfully. */
  317. #define I2CM_STATUS_ERROR 0x01 /*!< Unknown error condition. */
  318. #define I2CM_STATUS_NAK_ADR 0x02 /*!< No acknowledgement received from slave during address phase. */
  319. #define I2CM_STATUS_BUS_ERROR 0x03 /*!< I2C bus error */
  320. #define I2CM_STATUS_NAK_DAT 0x04 /*!< No acknowledgement received from slave during address phase. */
  321. #define I2CM_STATUS_ARBLOST 0x05 /*!< Arbitration lost. */
  322. #define I2CM_STATUS_BUSY 0xFF /*!< I2C transmistter is busy. */
  323. /**
  324. * @}
  325. */
  326. /**
  327. * @brief Master transfer data structure definitions
  328. */
  329. typedef struct {
  330. const uint8_t *txBuff; /*!< Pointer to array of bytes to be transmitted */
  331. uint8_t *rxBuff; /*!< Pointer memory where bytes received from I2C be stored */
  332. uint16_t txSz; /*!< Number of bytes in transmit array,
  333. if 0 only receive transfer will be carried on */
  334. uint16_t rxSz; /*!< Number of bytes to received,
  335. if 0 only transmission we be carried on */
  336. uint16_t status; /*!< Status of the current I2C transfer */
  337. uint8_t slaveAddr; /*!< 7-bit I2C Slave address */
  338. } I2CM_XFER_T;
  339. /**
  340. * @brief Set up bus speed for LPC_I2C controller
  341. * @param pI2C : Pointer to selected I2C peripheral
  342. * @param busSpeed : I2C bus clock rate
  343. * @return Nothing
  344. * @note Per I2C specification the busSpeed should be
  345. * @li 100000 for Standard mode
  346. * @li 400000 for Fast mode
  347. * @li 1000000 for Fast mode plus
  348. * IOCON registers corresponding to I2C pads should be updated
  349. * according to the bus mode.
  350. */
  351. void Chip_I2CM_SetBusSpeed(LPC_I2C_T *pI2C, uint32_t busSpeed);
  352. /**
  353. * @brief Enable I2C Master interface
  354. * @param pI2C : Pointer to selected I2C peripheral
  355. * @return Nothing
  356. * @note
  357. */
  358. static INLINE void Chip_I2CM_Enable(LPC_I2C_T *pI2C)
  359. {
  360. pI2C->CFG = (pI2C->CFG & I2C_CFG_MASK) | I2C_CFG_MSTEN;
  361. }
  362. /**
  363. * @brief Disable I2C Master interface
  364. * @param pI2C : Pointer to selected I2C peripheral
  365. * @return Nothing
  366. * @note
  367. */
  368. static INLINE void Chip_I2CM_Disable(LPC_I2C_T *pI2C)
  369. {
  370. pI2C->CFG = (pI2C->CFG & I2C_CFG_MASK) & ~I2C_CFG_MSTEN;
  371. }
  372. /**
  373. * @brief Get I2C Status
  374. * @param pI2C : Pointer to selected I2C peripheral
  375. * @return I2C Status register value
  376. * @note This function returns the value of the status register.
  377. */
  378. static INLINE uint32_t Chip_I2CM_GetStatus(LPC_I2C_T *pI2C)
  379. {
  380. return pI2C->STAT & ~I2C_STAT_RESERVED;
  381. }
  382. /**
  383. * @brief Clear I2C status bits (master)
  384. * @param pI2C : Pointer to selected I2C peripheral
  385. * @param clrStatus : Status bit to clear, ORed Value of I2C_STAT_MSTRARBLOSS and I2C_STAT_MSTSTSTPERR
  386. * @return Nothing
  387. * @note This function clears selected status flags.
  388. */
  389. static INLINE void Chip_I2CM_ClearStatus(LPC_I2C_T *pI2C, uint32_t clrStatus)
  390. {
  391. /* Clear Master Arbitration Loss and Start, Stop Error */
  392. pI2C->STAT = clrStatus & (I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);
  393. }
  394. /**
  395. * @brief Check if I2C Master is pending
  396. * @param pI2C : Pointer to selected I2C peripheral
  397. * @return Returns TRUE if the Master is pending else returns FALSE
  398. * @note
  399. */
  400. static INLINE bool Chip_I2CM_IsMasterPending(LPC_I2C_T *pI2C)
  401. {
  402. return (pI2C->STAT & I2C_STAT_MSTPENDING) != 0;
  403. }
  404. /**
  405. * @brief Get current state of the I2C Master
  406. * @param pI2C : Pointer to selected I2C peripheral
  407. * @return Master State Code, a value in the range of 0 - 4
  408. * @note After the Master is pending this state code tells the reason
  409. * for Master pending.
  410. */
  411. static INLINE uint32_t Chip_I2CM_GetMasterState(LPC_I2C_T *pI2C)
  412. {
  413. return (pI2C->STAT & I2C_STAT_MSTSTATE) >> 1;
  414. }
  415. /**
  416. * @brief Transmit START or Repeat-START signal on I2C bus
  417. * @param pI2C : Pointer to selected I2C peripheral
  418. * @return Nothing
  419. * @note This function sets the controller to transmit START condition when
  420. * the bus becomes free. This should be called only when master is pending.
  421. * The function writes a complete value to Master Control register, ORing is not advised.
  422. */
  423. static INLINE void Chip_I2CM_SendStart(LPC_I2C_T *pI2C)
  424. {
  425. pI2C->MSTCTL = I2C_MSTCTL_MSTSTART;
  426. }
  427. /**
  428. * @brief Transmit STOP signal on I2C bus
  429. * @param pI2C : Pointer to selected I2C peripheral
  430. * @return Nothing
  431. * @note This function sets the controller to transmit STOP condition.
  432. * This should be called only when master is pending. The function writes a
  433. * complete value to Master Control register, ORing is not advised.
  434. */
  435. static INLINE void Chip_I2CM_SendStop(LPC_I2C_T *pI2C)
  436. {
  437. pI2C->MSTCTL = I2C_MSTCTL_MSTSTOP;
  438. }
  439. /**
  440. * @brief Master Continue transfer operation
  441. * @param pI2C : Pointer to selected I2C peripheral
  442. * @return Nothing
  443. * @note This function sets the master controller to continue transmission.
  444. * This should be called only when master is pending. The function writes a
  445. * complete value to Master Control register, ORing is not advised.
  446. */
  447. static INLINE void Chip_I2CM_MasterContinue(LPC_I2C_T *pI2C)
  448. {
  449. pI2C->MSTCTL = I2C_MSTCTL_MSTCONTINUE;
  450. }
  451. /**
  452. * @brief Transmit a single data byte through the I2C peripheral (master)
  453. * @param pI2C : Pointer to selected I2C peripheral
  454. * @param data : Byte to transmit
  455. * @return Nothing
  456. * @note This function attempts to place a byte into the I2C Master
  457. * Data Register
  458. *
  459. */
  460. static INLINE void Chip_I2CM_WriteByte(LPC_I2C_T *pI2C, uint8_t data)
  461. {
  462. pI2C->MSTDAT = (uint32_t) data;
  463. }
  464. /**
  465. * @brief Read a single byte data from the I2C peripheral (master)
  466. * @param pI2C : Pointer to selected I2C peripheral
  467. * @return A single byte of data read
  468. * @note This function reads a byte from the I2C receive hold register
  469. * regardless of I2C state.
  470. */
  471. static INLINE uint8_t Chip_I2CM_ReadByte(LPC_I2C_T *pI2C)
  472. {
  473. return (uint8_t) (pI2C->MSTDAT & I2C_MSTDAT_DATAMASK);
  474. }
  475. /**
  476. * @brief Transfer state change handler
  477. * @param pI2C : Pointer to selected I2C peripheral
  478. * @param xfer : Pointer to a I2CM_XFER_T structure see notes below
  479. * @return Returns non-zero value on completion of transfer. The @a status
  480. * member of @a xfer structure contains the current status of the
  481. * transfer at the end of the call.
  482. * @note
  483. * The parameter @a xfer should be same as the one passed to Chip_I2CM_Xfer()
  484. * routine. This function should be called from the I2C interrupt handler
  485. * only when a master interrupt occurs.
  486. */
  487. uint32_t Chip_I2CM_XferHandler(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer);
  488. /**
  489. * @brief Transmit and Receive data in master mode
  490. * @param pI2C : Pointer to selected I2C peripheral
  491. * @param xfer : Pointer to a I2CM_XFER_T structure see notes below
  492. * @return Nothing
  493. * @note
  494. * The parameter @a xfer should have its member @a slaveAddr initialized
  495. * to the 7-Bit slave address to which the master will do the xfer, Bit0
  496. * to bit6 should have the address and Bit8 is ignored. During the transfer
  497. * no code (like event handler) must change the content of the memory
  498. * pointed to by @a xfer. The member of @a xfer, @a txBuff and @a txSz be
  499. * initialized to the memory from which the I2C must pick the data to be
  500. * transfered to slave and the number of bytes to send respectively, similarly
  501. * @a rxBuff and @a rxSz must have pointer to memory where data received
  502. * from slave be stored and the number of data to get from slave respectilvely.
  503. * Following types of transfers are possible:
  504. * - Write-only transfer: When @a rxSz member of @a xfer is set to 0.
  505. *
  506. * S Addr Wr [A] txBuff0 [A] txBuff1 [A] ... txBuffN [A] P
  507. *
  508. * - If I2CM_XFER_OPTION_IGNORE_NACK is set in @a options memeber
  509. *
  510. * S Addr Wr [A] txBuff0 [A or NA] ... txBuffN [A or NA] P
  511. *
  512. * - Read-only transfer: When @a txSz member of @a xfer is set to 0.
  513. *
  514. * S Addr Rd [A] [rxBuff0] A [rxBuff1] A ... [rxBuffN] NA P
  515. *
  516. * - If I2CM_XFER_OPTION_LAST_RX_ACK is set in @a options memeber
  517. *
  518. * S Addr Rd [A] [rxBuff0] A [rxBuff1] A ... [rxBuffN] A P
  519. *
  520. * - Read-Write transfer: When @a rxSz and @ txSz members of @a xfer are non-zero.
  521. *
  522. * S Addr Wr [A] txBuff0 [A] txBuff1 [A] ... txBuffN [A]
  523. * S Addr Rd [A] [rxBuff0] A [rxBuff1] A ... [rxBuffN] NA P
  524. *
  525. */
  526. void Chip_I2CM_Xfer(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer);
  527. /**
  528. * @brief Transmit and Receive data in master mode
  529. * @param pI2C : Pointer to selected I2C peripheral
  530. * @param xfer : Pointer to a I2CM_XFER_T structure see notes below
  531. * @return Returns non-zero value on succesful completion of transfer.
  532. * @note
  533. * This function operates same as Chip_I2CM_Xfer(), but is a blocking call.
  534. */
  535. uint32_t Chip_I2CM_XferBlocking(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer);
  536. /**
  537. * @}
  538. */
  539. /** @defgroup I2CS_8XX CHIP: LPC8XX I2C slave-only driver
  540. * @ingroup I2C_8XX
  541. * This driver only works in slave mode.
  542. * @{
  543. */
  544. /** @brief I2C slave service start callback
  545. * This callback is called from the I2C slave handler when an I2C slave address is
  546. * received and needs servicing. It's used to indicate the start of a slave transfer
  547. * that will happen on the slave bus.
  548. */
  549. typedef void (*I2CSlaveXferStart)(uint8_t addr);
  550. /** @brief I2C slave send data callback
  551. * This callback is called from the I2C slave handler when an I2C slave address needs
  552. * data to send. Return 0 to NACK the master and terminate the transfer, or return
  553. * a non-0 value with the value to send in *data.
  554. */
  555. typedef uint8_t (*I2CSlaveXferSend)(uint8_t *data);
  556. /** @brief I2C slave receive data callback
  557. * This callback is called from the I2C slave handler when an I2C slave address has
  558. * receive data. Return 0 to NACK the master and terminate the transfer, or return
  559. * a non-0 value to continue the transfer.
  560. */
  561. typedef uint8_t (*I2CSlaveXferRecv)(uint8_t data);
  562. /** @brief I2C slave service done callback
  563. * This callback is called from the I2C slave handler when an I2C slave transfer is
  564. * completed. It's used to indicate the end of a slave transfer.
  565. */
  566. typedef void (*I2CSlaveXferDone)(void);
  567. /**
  568. * Slave transfer are performed using 3 callbacks. These 3 callbacks handle most I2C
  569. * slave transfer cases. When the slave is setup and a slave interrupt is receive
  570. * and processed with the Chip_I2CS_XferHandler() function in the I2C interrupt handler,
  571. * one of these 3 callbacks is called. The callbacks can be used for unsized transfers
  572. * from the master.
  573. *
  574. * When an address is received, the SlaveXferAddr() callback is called with the
  575. * received address. Only addresses enabled in the slave controller will be handled.
  576. * The slave controller can support up to 4 slave addresses.
  577. *
  578. * If the master is going to perform a read operation, the SlaveXferSend() callback
  579. * is called. Place the data byte to send in *data and return a non-0 value to the
  580. * caller, or return 0 to NACK the master. (Note the master ACKS/NACKS to slave
  581. * on reads, so this won't necessarily stop the slave transfer.)<br>
  582. *
  583. * If the master performs a write operation, the SlaveXferRecv() callback is called
  584. * with the received data. Return a non-0 value to the caller, or return 0 to NACK
  585. * the master.<br>
  586. *
  587. * Once the transfer completes, the SlaveXferDone() callback will be called.<br>
  588. */
  589. typedef struct {
  590. I2CSlaveXferStart slaveStart; /*!< Called when an matching I2C slave address is received */
  591. I2CSlaveXferSend slaveSend; /*!< Called when a byte is needed to send to master */
  592. I2CSlaveXferRecv slaveRecv; /*!< Called when a byte is received from master */
  593. I2CSlaveXferDone slaveDone; /*!< Called when a slave transfer is complete */
  594. } I2CS_XFER_T;
  595. /**
  596. * @brief Enable I2C slave interface
  597. * @param pI2C : Pointer to selected I2C peripheral
  598. * @return Nothing
  599. * @note Do not call this function until the slave interface is fully configured.
  600. */
  601. STATIC INLINE void Chip_I2CS_Enable(LPC_I2C_T *pI2C)
  602. {
  603. pI2C->CFG = (pI2C->CFG & I2C_CFG_MASK) | I2C_CFG_SLVEN;
  604. }
  605. /**
  606. * @brief Disable I2C slave interface
  607. * @param pI2C : Pointer to selected I2C peripheral
  608. * @return Nothing
  609. */
  610. STATIC INLINE void Chip_I2CS_Disable(LPC_I2C_T *pI2C)
  611. {
  612. pI2C->CFG = (pI2C->CFG & I2C_CFG_MASK) & ~I2C_CFG_SLVEN;
  613. }
  614. /**
  615. * @brief Get I2C Status
  616. * @param pI2C : Pointer to selected I2C peripheral
  617. * @return I2C Status register value
  618. * @note This function returns the value of the status register.
  619. */
  620. STATIC INLINE uint32_t Chip_I2CS_GetStatus(LPC_I2C_T *pI2C)
  621. {
  622. return pI2C->STAT & ~I2C_STAT_RESERVED;
  623. }
  624. /**
  625. * @brief Clear I2C status bits (slave)
  626. * @param pI2C : Pointer to selected I2C peripheral
  627. * @param clrStatus : Status bit to clear, must be I2C_STAT_SLVDESEL
  628. * @return Nothing
  629. * @note This function clears selected status flags.
  630. */
  631. STATIC INLINE void Chip_I2CS_ClearStatus(LPC_I2C_T *pI2C, uint32_t clrStatus)
  632. {
  633. pI2C->STAT = clrStatus & I2C_STAT_SLVDESEL;
  634. }
  635. /**
  636. * @brief Check if I2C slave is pending
  637. * @param pI2C : Pointer to selected I2C peripheral
  638. * @return Returns TRUE if the slave is pending else returns FALSE
  639. * @note
  640. */
  641. STATIC INLINE bool Chip_I2CS_IsSlavePending(LPC_I2C_T *pI2C)
  642. {
  643. return (pI2C->STAT & I2C_STAT_SLVPENDING) != 0;
  644. }
  645. /**
  646. * @brief Check if I2C slave is selected
  647. * @param pI2C : Pointer to selected I2C peripheral
  648. * @return Returns TRUE if the slave is is selected, otherwise FALSE
  649. * @note
  650. */
  651. STATIC INLINE bool Chip_I2CS_IsSlaveSelected(LPC_I2C_T *pI2C)
  652. {
  653. return (pI2C->STAT & I2C_STAT_SLVSEL) != 0;
  654. }
  655. /**
  656. * @brief Check if I2C slave is deselected
  657. * @param pI2C : Pointer to selected I2C peripheral
  658. * @return Returns TRUE if the slave is is deselected, otherwise FALSE
  659. * @note
  660. */
  661. STATIC INLINE bool Chip_I2CS_IsSlaveDeSelected(LPC_I2C_T *pI2C)
  662. {
  663. return (pI2C->STAT & I2C_STAT_SLVDESEL) != 0;
  664. }
  665. /**
  666. * @brief Get current state of the I2C slave
  667. * @param pI2C : Pointer to selected I2C peripheral
  668. * @return slave State Code, a value of type I2C_STAT_SLVCODE_*
  669. * @note After the slave is pending this state code tells the reason
  670. * for slave pending.
  671. */
  672. STATIC INLINE uint32_t Chip_I2CS_GetSlaveState(LPC_I2C_T *pI2C)
  673. {
  674. return (pI2C->STAT & I2C_STAT_SLVSTATE) >> 9;
  675. }
  676. /**
  677. * @brief Returns the current slave address match index
  678. * @param pI2C : Pointer to selected I2C peripheral
  679. * @return slave match index, 0 - 3
  680. */
  681. STATIC INLINE uint32_t Chip_I2CS_GetSlaveMatchIndex(LPC_I2C_T *pI2C)
  682. {
  683. return (pI2C->STAT & I2C_STAT_SLVIDX) >> 12;
  684. }
  685. /**
  686. * @brief Slave Continue transfer operation (ACK)
  687. * @param pI2C : Pointer to selected I2C peripheral
  688. * @return Nothing
  689. * @note This function sets the slave controller to continue transmission.
  690. * This should be called only when slave is pending. The function writes a
  691. * complete value to slave Control register, ORing is not advised.
  692. */
  693. STATIC INLINE void Chip_I2CS_SlaveContinue(LPC_I2C_T *pI2C)
  694. {
  695. pI2C->SLVCTL = I2C_SLVCTL_SLVCONTINUE;
  696. }
  697. /**
  698. * @brief Slave NACK operation
  699. * @param pI2C : Pointer to selected I2C peripheral
  700. * @return Nothing
  701. * @note This function sets the slave controller to NAK the master.
  702. */
  703. STATIC INLINE void Chip_I2CS_SlaveNACK(LPC_I2C_T *pI2C)
  704. {
  705. pI2C->SLVCTL = I2C_SLVCTL_SLVNACK;
  706. }
  707. /**
  708. * @brief Transmit a single data byte through the I2C peripheral (slave)
  709. * @param pI2C : Pointer to selected I2C peripheral
  710. * @param data : Byte to transmit
  711. * @return Nothing
  712. * @note This function attempts to place a byte into the I2C slave
  713. * Data Register
  714. *
  715. */
  716. STATIC INLINE void Chip_I2CS_WriteByte(LPC_I2C_T *pI2C, uint8_t data)
  717. {
  718. pI2C->SLVDAT = (uint32_t) data;
  719. }
  720. /**
  721. * @brief Read a single byte data from the I2C peripheral (slave)
  722. * @param pI2C : Pointer to selected I2C peripheral
  723. * @return A single byte of data read
  724. * @note This function reads a byte from the I2C receive hold register
  725. * regardless of I2C state.
  726. */
  727. STATIC INLINE uint8_t Chip_I2CS_ReadByte(LPC_I2C_T *pI2C)
  728. {
  729. return (uint8_t) (pI2C->SLVDAT & I2C_SLVDAT_DATAMASK);
  730. }
  731. /**
  732. * @brief Set a I2C slave address for slave operation
  733. * @param pI2C : Pointer to selected I2C peripheral
  734. * @param slvNum : Possible slave address number, between 0 - 3
  735. * @param slvAddr : Slave Address for the index (7-bits, bit 7 = 0)
  736. * @return Nothing
  737. * @note Setting a slave address also enables the slave address. Do
  738. * not 'pre-shift' the slave address.
  739. */
  740. STATIC INLINE void Chip_I2CS_SetSlaveAddr(LPC_I2C_T *pI2C, uint8_t slvNum, uint8_t slvAddr)
  741. {
  742. pI2C->SLVADR[slvNum] = (uint32_t) (slvAddr << 1);
  743. }
  744. /**
  745. * @brief Return a I2C programmed slave address
  746. * @param pI2C : Pointer to selected I2C peripheral
  747. * @param slvNum : Possible slave address number, between 0 - 3
  748. * @return Nothing
  749. */
  750. STATIC INLINE uint8_t Chip_I2CS_GetSlaveAddr(LPC_I2C_T *pI2C, uint8_t slvNum)
  751. {
  752. return (pI2C->SLVADR[slvNum] >> 1) & 0x7F;
  753. }
  754. /**
  755. * @brief Enable a I2C address
  756. * @param pI2C : Pointer to selected I2C peripheral
  757. * @param slvNum : Possible slave address number, between 0 - 3
  758. * @return Nothing
  759. */
  760. STATIC INLINE void Chip_I2CS_EnableSlaveAddr(LPC_I2C_T *pI2C, uint8_t slvNum)
  761. {
  762. pI2C->SLVADR[slvNum] = (pI2C->SLVADR[slvNum] & I2C_SLVADR_MASK) & ~I2C_SLVADR_SADISABLE;
  763. }
  764. /**
  765. * @brief Disable a I2C address
  766. * @param pI2C : Pointer to selected I2C peripheral
  767. * @param slvNum : Possible slave address number, between 0 - 3
  768. * @return Nothing
  769. */
  770. STATIC INLINE void Chip_I2CS_DisableSlaveAddr(LPC_I2C_T *pI2C, uint8_t slvNum)
  771. {
  772. pI2C->SLVADR[slvNum] = (pI2C->SLVADR[slvNum] & I2C_SLVADR_MASK) | I2C_SLVADR_SADISABLE;
  773. }
  774. /**
  775. * @brief Setup slave qialifier address
  776. * @param pI2C : Pointer to selected I2C peripheral
  777. * @param extend : true to extend I2C slave detect address 0 range, or false to match to corresponding bits
  778. * @param slvNum : Slave address qualifier, see SLVQUAL0 register in User Manual
  779. * @return Nothing
  780. * @note Do not 'pre-shift' the slave address.
  781. */
  782. STATIC INLINE void Chip_I2CS_SetSlaveQual0(LPC_I2C_T *pI2C, bool extend, uint8_t slvNum)
  783. {
  784. slvNum = slvNum << 1;
  785. if (extend) {
  786. slvNum |= I2C_SLVQUAL_QUALMODE0;
  787. }
  788. pI2C->SLVQUAL0 = slvNum;
  789. }
  790. /**
  791. * @brief Slave transfer state change handler
  792. * @param pI2C : Pointer to selected I2C peripheral
  793. * @param xfers : Pointer to a I2CS_MULTI_XFER_T structure see notes below
  794. * @return Returns non-zero value on completion of transfer
  795. * @note See @ref I2CS_XFER_T for more information on this function. When using
  796. * this function, the I2C_INTENSET_SLVPENDING and I2C_INTENSET_SLVDESEL interrupts
  797. * should be enabled and setup in the I2C interrupt handler to call this function
  798. * when they fire.
  799. */
  800. uint32_t Chip_I2CS_XferHandler(LPC_I2C_T *pI2C, const I2CS_XFER_T *xfers);
  801. /**
  802. * @}
  803. */
  804. #ifdef __cplusplus
  805. }
  806. #endif
  807. #endif /* __I2C_COMMON_8XX_H_ */