lpc_i2c.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /**********************************************************************
  2. * $Id$ lpc_i2c.c 2011-06-02
  3. *//**
  4. * @file lpc_i2c.c
  5. * @brief Contains all functions support for I2C firmware library
  6. * 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. /** @addtogroup I2C
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _I2C
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_i2c.h"
  44. #include "lpc_clkpwr.h"
  45. #include "lpc_pinsel.h"
  46. /* Private Types -------------------------------------------------------------- */
  47. /** @defgroup I2C_Private_Types I2C Private Types
  48. * @{
  49. */
  50. /**
  51. * @brief I2C device configuration structure type
  52. */
  53. typedef struct
  54. {
  55. uint32_t txrx_setup; /* Transmission setup */
  56. int32_t dir; /* Current direction phase, 0 - write, 1 - read */
  57. } I2C_CFG_T;
  58. /**
  59. * @}
  60. */
  61. /* Private Variables ---------------------------------------------------------- */
  62. /**
  63. * @brief II2C driver data for I2C0, I2C1 and I2C2
  64. */
  65. static I2C_CFG_T i2cdat[3];
  66. static uint32_t I2C_MasterComplete[3];
  67. static uint32_t I2C_SlaveComplete[3];
  68. static uint32_t I2C_MonitorBufferIndex;
  69. /* Private Functions ---------------------------------------------------------- */
  70. /* Get pointer to expected I2C */
  71. static LPC_I2C_TypeDef* I2C_GetPointer(en_I2C_unitId compId);
  72. /* Generate a start condition on I2C bus (in master mode only) */
  73. static uint32_t I2C_Start (LPC_I2C_TypeDef *I2Cx, I2C_TRANSFER_OPT_Type Opt);
  74. /* Generate a stop condition on I2C bus (in master mode only) */
  75. static void I2C_Stop (LPC_I2C_TypeDef *I2Cx, I2C_TRANSFER_OPT_Type Opt);
  76. /* I2C send byte subroutine */
  77. static uint32_t I2C_SendByte (LPC_I2C_TypeDef *I2Cx, uint8_t databyte);
  78. /* I2C get byte subroutine */
  79. static uint32_t I2C_GetByte (LPC_I2C_TypeDef *I2Cx, uint8_t *retdat, Bool ack);
  80. /* I2C set clock (hz) */
  81. static void I2C_SetClock (LPC_I2C_TypeDef *I2Cx, uint32_t target_clock);
  82. /*--------------------------------------------------------------------------------*/
  83. /********************************************************************//**
  84. * @brief Convert from I2C peripheral to number
  85. * @param[in] I2Cx: I2C peripheral selected, should be:
  86. * - LPC_I2C0
  87. * - LPC_I2C1
  88. * - LPC_I2C2
  89. * @return I2C number, could be: 0..2
  90. *********************************************************************/
  91. static LPC_I2C_TypeDef* I2C_GetPointer(en_I2C_unitId compId)
  92. {
  93. LPC_I2C_TypeDef* pI2C;
  94. switch (compId)
  95. {
  96. case I2C_0:
  97. pI2C = LPC_I2C0;
  98. break;
  99. case I2C_1:
  100. pI2C = LPC_I2C1;
  101. break;
  102. case I2C_2:
  103. pI2C = LPC_I2C2;
  104. break;
  105. default:
  106. pI2C = NULL;
  107. break;
  108. }
  109. return pI2C;
  110. }
  111. /********************************************************************//**
  112. * @brief Generate a start condition on I2C bus (in master mode only)
  113. * @param[in] I2Cx: I2C peripheral selected, should be:
  114. * - LPC_I2C0
  115. * - LPC_I2C1
  116. * - LPC_I2C2
  117. * @param[in] Opt a I2C_TRANSFER_OPT_Type type that selected for
  118. * interrupt or polling mode.
  119. * @return value of I2C status register after generate a start condition
  120. *********************************************************************/
  121. static uint32_t I2C_Start (LPC_I2C_TypeDef *I2Cx, I2C_TRANSFER_OPT_Type Opt)
  122. {
  123. // Reset STA, STO, SI
  124. I2Cx->CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
  125. // Enter to Master Transmitter mode
  126. I2Cx->CONSET = I2C_I2CONSET_STA;
  127. if(Opt == I2C_TRANSFER_POLLING)
  128. {
  129. // Wait for complete
  130. while (!(I2Cx->CONSET & I2C_I2CONSET_SI));
  131. }
  132. return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
  133. }
  134. /********************************************************************//**
  135. * @brief Generate a stop condition on I2C bus (in master mode only)
  136. * @param[in] I2Cx: I2C peripheral selected, should be:
  137. * - LPC_I2C0
  138. * - LPC_I2C1
  139. * - LPC_I2C2
  140. * @param[in] Opt a I2C_TRANSFER_OPT_Type type that selected for
  141. * interrupt or polling mode.
  142. * @return None
  143. *********************************************************************/
  144. static void I2C_Stop (LPC_I2C_TypeDef *I2Cx, I2C_TRANSFER_OPT_Type Opt)
  145. {
  146. /* Make sure start bit is not active */
  147. if (I2Cx->CONSET & I2C_I2CONSET_STA)
  148. {
  149. I2Cx->CONCLR = I2C_I2CONCLR_STAC;
  150. }
  151. I2Cx->CONSET = I2C_I2CONSET_STO;
  152. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  153. if(Opt == I2C_TRANSFER_POLLING)
  154. {
  155. // wait for stop is sent
  156. while(I2Cx->CONSET & I2C_I2CONSET_STO)
  157. {
  158. if(I2Cx->CONSET & I2C_I2CONSET_SI)
  159. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  160. }
  161. }
  162. }
  163. /********************************************************************//**
  164. * @brief Send a byte
  165. * @param[in] I2Cx: I2C peripheral selected, should be:
  166. * - LPC_I2C0
  167. * - LPC_I2C1
  168. * - LPC_I2C2
  169. * @param[in] databyte: number of byte
  170. * @return value of I2C status register after sending
  171. *********************************************************************/
  172. static uint32_t I2C_SendByte (LPC_I2C_TypeDef *I2Cx, uint8_t databyte)
  173. {
  174. uint32_t CodeStatus = I2Cx->STAT & I2C_STAT_CODE_BITMASK;
  175. if((CodeStatus != I2C_I2STAT_M_TX_START) &&
  176. (CodeStatus != I2C_I2STAT_M_TX_RESTART) &&
  177. (CodeStatus != I2C_I2STAT_M_TX_SLAW_ACK) &&
  178. (CodeStatus != I2C_I2STAT_M_TX_DAT_ACK) )
  179. {
  180. return CodeStatus;
  181. }
  182. I2Cx->DAT = databyte & I2C_I2DAT_BITMASK;
  183. I2Cx->CONSET = I2C_I2CONSET_AA;
  184. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  185. return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
  186. }
  187. /********************************************************************//**
  188. * @brief Get a byte
  189. * @param[in] I2Cx: I2C peripheral selected, should be:
  190. * - LPC_I2C0
  191. * - LPC_I2C1
  192. * - LPC_I2C2
  193. * @param[out] retdat pointer to return data
  194. * @param[in] ack assert acknowledge or not, should be: TRUE/FALSE
  195. * @return value of I2C status register after sending
  196. *********************************************************************/
  197. static uint32_t I2C_GetByte (LPC_I2C_TypeDef *I2Cx, uint8_t *retdat, Bool ack)
  198. {
  199. *retdat = (uint8_t) (I2Cx->DAT & I2C_I2DAT_BITMASK);
  200. if (ack == TRUE)
  201. {
  202. I2Cx->CONSET = I2C_I2CONSET_AA;
  203. }
  204. else
  205. {
  206. I2Cx->CONCLR = I2C_I2CONCLR_AAC;
  207. }
  208. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  209. return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
  210. }
  211. /*********************************************************************//**
  212. * @brief Setup clock rate for I2C peripheral
  213. * @param[in] I2Cx I2C peripheral selected, should be:
  214. * - LPC_I2C0
  215. * - LPC_I2C1
  216. * - LPC_I2C2
  217. * @param[in] target_clock : clock of SSP (Hz)
  218. * @return None
  219. ***********************************************************************/
  220. static void I2C_SetClock (LPC_I2C_TypeDef *I2Cx, uint32_t target_clock)
  221. {
  222. uint32_t temp;
  223. temp = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER) / target_clock;
  224. /* Set the I2C clock value to register */
  225. I2Cx->SCLH = (uint32_t)(temp / 2);
  226. I2Cx->SCLL = (uint32_t)(temp - I2Cx->SCLH);
  227. }
  228. /* End of Private Functions --------------------------------------------------- */
  229. /* Public Functions ----------------------------------------------------------- */
  230. /** @addtogroup I2C_Public_Functions
  231. * @{
  232. */
  233. /********************************************************************//**
  234. * @brief Initializes the I2Cx peripheral with specified parameter.
  235. * @param[in] I2Cx I2C peripheral selected, should be
  236. * - LPC_I2C0
  237. * - LPC_I2C1
  238. * - LPC_I2C2
  239. * @param[in] clockrate Target clock rate value to initialized I2C
  240. * peripheral (Hz)
  241. * @return None
  242. *********************************************************************/
  243. void I2C_Init(en_I2C_unitId i2cId, uint32_t clockrate)
  244. {
  245. uint32_t clkSetting;
  246. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  247. switch (i2cId)
  248. {
  249. case I2C_0:
  250. clkSetting = CLKPWR_PCONP_PCI2C0;
  251. break;
  252. case I2C_1:
  253. clkSetting = CLKPWR_PCONP_PCI2C1;
  254. break;
  255. case I2C_2:
  256. clkSetting = CLKPWR_PCONP_PCI2C2;
  257. break;
  258. default:
  259. return;
  260. }
  261. CLKPWR_ConfigPPWR (clkSetting, ENABLE);
  262. /* Set clock rate */
  263. I2C_SetClock(I2Cx,clockrate);
  264. /* Set I2C operation to default */
  265. I2Cx->CONCLR = (I2C_I2CONCLR_AAC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_I2ENC);
  266. }
  267. /*********************************************************************//**
  268. * @brief De-initializes the I2C peripheral registers to their
  269. * default reset values.
  270. * @param[in] I2Cx I2C peripheral selected, should be
  271. * - LPC_I2C0
  272. * - LPC_I2C1
  273. * - LPC_I2C2
  274. * @return None
  275. **********************************************************************/
  276. void I2C_DeInit(en_I2C_unitId i2cId)
  277. {
  278. uint32_t clkSetting;
  279. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  280. /* Disable I2C control */
  281. I2Cx->CONCLR = 0xFF;
  282. switch (i2cId)
  283. {
  284. case I2C_0:
  285. clkSetting = CLKPWR_PCONP_PCI2C0;
  286. break;
  287. case I2C_1:
  288. clkSetting = CLKPWR_PCONP_PCI2C1;
  289. break;
  290. case I2C_2:
  291. clkSetting = CLKPWR_PCONP_PCI2C2;
  292. break;
  293. default:
  294. return;
  295. }
  296. CLKPWR_ConfigPPWR (clkSetting, DISABLE);
  297. }
  298. /*********************************************************************//**
  299. * @brief Enable or disable I2C peripheral's operation
  300. * @param[in] I2Cx I2C peripheral selected, should be
  301. * - LPC_I2C0
  302. * - LPC_I2C1
  303. * - LPC_I2C2
  304. * @param[in] NewState New State of I2Cx peripheral's operation
  305. * @return none
  306. **********************************************************************/
  307. void I2C_Cmd(en_I2C_unitId i2cId, en_I2C_Mode Mode, FunctionalState NewState)
  308. {
  309. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  310. if (NewState == ENABLE)
  311. {
  312. if(Mode != I2C_SLAVE_MODE)
  313. I2Cx->CONSET = I2C_I2CONSET_I2EN;
  314. else
  315. I2Cx->CONSET = I2C_I2CONSET_I2EN | I2C_I2CONSET_AA;
  316. }
  317. else
  318. {
  319. I2Cx->CONCLR = I2C_I2CONCLR_I2ENC;
  320. }
  321. }
  322. /*********************************************************************//**
  323. * @brief Enable/Disable interrupt for I2C peripheral
  324. * @param[in] I2Cx I2C peripheral selected, should be:
  325. * - LPC_I2C0
  326. * - LPC_I2C1
  327. * - LPC_I2C2
  328. * @param[in] NewState New State of I2C peripheral interrupt in NVIC core
  329. * should be:
  330. * - ENABLE: enable interrupt for this I2C peripheral
  331. * - DISABLE: disable interrupt for this I2C peripheral
  332. * @return None
  333. **********************************************************************/
  334. void I2C_IntCmd (en_I2C_unitId i2cId, Bool NewState)
  335. {
  336. IRQn_Type irq;
  337. switch (i2cId)
  338. {
  339. case I2C_0:
  340. irq = I2C0_IRQn;
  341. break;
  342. case I2C_1:
  343. irq = I2C1_IRQn;
  344. break;
  345. case I2C_2:
  346. irq = I2C2_IRQn;
  347. break;
  348. default:
  349. return;
  350. }
  351. if (NewState)
  352. {
  353. NVIC_EnableIRQ(irq);
  354. }
  355. else
  356. {
  357. NVIC_DisableIRQ(irq);
  358. }
  359. return;
  360. }
  361. /*********************************************************************//**
  362. * @brief Handle I2C Master states.
  363. * @param[in] I2Cx I2C peripheral selected, should be:
  364. * - I2C_0
  365. * - I2C_1
  366. * - I2C_2
  367. * @param[in] CodeStatus I2C state
  368. * @param[in] TransferCfg Pointer to a I2C_S_SETUP_Type structure that
  369. * contains specified information about the
  370. * configuration for master transfer.
  371. * @param[in] Opt a I2C_TRANSFER_OPT_Type type that selected for
  372. * interrupt or polling mode.
  373. * @return It can be
  374. * - I2C_OK
  375. * -I2C_BYTE_RECV
  376. * -I2C_BYTE_SENT
  377. * -I2C_SEND_END
  378. * -I2C_RECV_END
  379. * - I2C_ERR
  380. * - I2C_NAK_RECV
  381. **********************************************************************/
  382. int32_t I2C_MasterHanleStates(en_I2C_unitId i2cId,
  383. uint32_t CodeStatus,
  384. I2C_M_SETUP_Type *TransferCfg,
  385. I2C_TRANSFER_OPT_Type Opt
  386. )
  387. {
  388. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  389. uint8_t *txdat;
  390. uint8_t *rxdat;
  391. uint8_t tmp;
  392. int32_t Ret = I2C_OK;
  393. //get buffer to send/receive
  394. txdat = (uint8_t *) &TransferCfg->tx_data[TransferCfg->tx_count];
  395. rxdat = (uint8_t *) &TransferCfg->rx_data[TransferCfg->rx_count];
  396. switch(CodeStatus)
  397. {
  398. case I2C_I2STAT_M_TX_START:
  399. case I2C_I2STAT_M_TX_RESTART:
  400. //case I2C_I2STAT_M_RX_START:
  401. //case I2C_I2STAT_M_RX_RESTART
  402. // Send data first
  403. if(TransferCfg->tx_count < TransferCfg->tx_length)
  404. {
  405. /* Send slave address + WR direction bit = 0 ----------------------------------- */
  406. I2C_SendByte(I2Cx, (TransferCfg->sl_addr7bit << 1));
  407. Ret = I2C_BYTE_SENT;
  408. }
  409. else if (TransferCfg->rx_count < TransferCfg->rx_length)
  410. {
  411. /* Send slave address + RD direction bit = 1 ----------------------------------- */
  412. I2C_SendByte(I2Cx, ((TransferCfg->sl_addr7bit << 1) | 0x01));
  413. Ret = I2C_BYTE_SENT;
  414. }
  415. // Clear STA bit after the slave address is sent
  416. I2Cx->CONCLR = I2C_I2CONCLR_STAC;
  417. break;
  418. case I2C_I2STAT_M_TX_SLAW_ACK:
  419. case I2C_I2STAT_M_TX_DAT_ACK:
  420. if(TransferCfg->tx_count < TransferCfg->tx_length)
  421. {
  422. I2C_SendByte(I2Cx, *txdat);
  423. txdat++;
  424. TransferCfg->tx_count++;
  425. Ret = I2C_BYTE_SENT;
  426. }
  427. else
  428. {
  429. if(TransferCfg->rx_count >= TransferCfg->rx_length)
  430. {
  431. I2C_Stop(I2Cx, Opt);
  432. }
  433. Ret = I2C_SEND_END;
  434. }
  435. break;
  436. case I2C_I2STAT_M_TX_DAT_NACK:
  437. if(TransferCfg->rx_count >= TransferCfg->rx_length)
  438. {
  439. I2C_Stop(I2Cx, Opt);
  440. }
  441. Ret = I2C_SEND_END;
  442. break;
  443. case I2C_I2STAT_M_RX_ARB_LOST:
  444. case I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL:
  445. case I2C_I2STAT_S_TX_ARB_LOST_M_SLA:
  446. //case I2C_I2STAT_M_TX_ARB_LOST:
  447. I2C_Stop(I2Cx, Opt);
  448. Ret = I2C_ERR;
  449. break;
  450. case I2C_I2STAT_M_RX_SLAR_ACK:
  451. if(TransferCfg->rx_length > 1)
  452. I2Cx->CONSET = I2C_I2CONSET_AA;
  453. else
  454. I2Cx->CONCLR = I2C_I2CONCLR_AAC;
  455. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  456. Ret = I2C_BYTE_RECV;
  457. break;
  458. case I2C_I2STAT_M_RX_DAT_ACK:
  459. if (TransferCfg->rx_count <TransferCfg->rx_length)
  460. {
  461. if ((TransferCfg->rx_length > 1) && (TransferCfg->rx_count < (TransferCfg->rx_length - 2)))
  462. {
  463. I2C_GetByte(I2Cx, &tmp, TRUE);
  464. Ret = I2C_BYTE_RECV;
  465. }
  466. else // the next byte is the last byte, send NACK instead.
  467. {
  468. I2C_GetByte(I2Cx, &tmp, FALSE);
  469. Ret = I2C_BYTE_RECV;
  470. }
  471. *rxdat++ = tmp;
  472. TransferCfg->rx_count++;
  473. }
  474. else
  475. {
  476. I2C_Stop(I2Cx, Opt);
  477. Ret = I2C_RECV_END;
  478. }
  479. break;
  480. case I2C_I2STAT_M_RX_DAT_NACK:
  481. I2C_GetByte(I2Cx, &tmp, FALSE);
  482. if (TransferCfg->rx_count < TransferCfg->rx_length)
  483. {
  484. *rxdat++ = tmp;
  485. TransferCfg->rx_count++;
  486. }
  487. I2C_Stop(I2Cx, Opt);
  488. Ret = I2C_RECV_END;
  489. break;
  490. case I2C_I2STAT_M_RX_SLAR_NACK:
  491. case I2C_I2STAT_M_TX_SLAW_NACK:
  492. case I2C_I2STAT_BUS_ERROR:
  493. // Send STOP condition
  494. I2C_Stop(I2Cx, Opt);
  495. Ret = I2C_ERR;
  496. break;
  497. /* No status information */
  498. case I2C_I2STAT_NO_INF:
  499. if ((TransferCfg->tx_count <TransferCfg->tx_length)||
  500. (TransferCfg->rx_count <TransferCfg->rx_length))
  501. {
  502. I2C_Stop(I2Cx, Opt);
  503. Ret = I2C_ERR;
  504. }
  505. else
  506. {
  507. Ret = I2C_RECV_END;
  508. }
  509. break;
  510. default:
  511. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  512. break;
  513. }
  514. return Ret;
  515. }
  516. /*********************************************************************//**
  517. * @brief Handle I2C Slave states.
  518. * @param[in] I2Cx I2C peripheral selected, should be:
  519. * - I2C_0
  520. * - I2C_1
  521. * - I2C_2
  522. * @param[in] CodeStatus I2C state
  523. * @param[in] TransferCfg Pointer to a I2C_S_SETUP_Type structure that
  524. * contains specified information about the
  525. * configuration for master transfer.
  526. * @return It can be
  527. * - I2C_OK
  528. * -I2C_BYTE_RECV
  529. * -I2C_BYTE_SENT
  530. * -I2C_SEND_END
  531. * -I2C_RECV_END
  532. * - I2C_ERR
  533. * - I2C_NAK_RECV
  534. **********************************************************************/
  535. int32_t I2C_SlaveHanleStates(en_I2C_unitId i2cId,
  536. uint32_t CodeStatus,
  537. I2C_S_SETUP_Type *TransferCfg)
  538. {
  539. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  540. int32_t Ret = I2C_OK;
  541. uint8_t *txdat;
  542. uint8_t *rxdat;
  543. //get buffer to send/receive
  544. txdat = (uint8_t *) &TransferCfg->tx_data[TransferCfg->tx_count];
  545. rxdat = (uint8_t *) &TransferCfg->rx_data[TransferCfg->rx_count];
  546. switch (CodeStatus)
  547. {
  548. /* Reading phase -------------------------------------------------------- */
  549. /* Own SLA+R has been received, ACK has been returned */
  550. case I2C_I2STAT_S_RX_SLAW_ACK:
  551. /* General call address has been received, ACK has been returned */
  552. case I2C_I2STAT_S_RX_GENCALL_ACK:
  553. I2Cx->CONSET = I2C_I2CONSET_AA;
  554. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  555. break;
  556. /* Arbitration has been lost in Slave Address + R/W bit as bus Master. General Call has
  557. been received and ACK has been returned.*/
  558. case I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL:
  559. I2Cx->CONSET = I2C_I2CONSET_AA|I2C_I2CONSET_STA;
  560. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  561. break;
  562. /* Previously addressed with own SLA;
  563. * DATA byte has been received;
  564. * ACK has been returned */
  565. case I2C_I2STAT_S_RX_ARB_LOST_M_SLA:
  566. case I2C_I2STAT_S_RX_PRE_SLA_DAT_ACK:
  567. /*
  568. * All data bytes that over-flow the specified receive
  569. * data length, just ignore them.
  570. */
  571. if ((TransferCfg->rx_count < TransferCfg->rx_length) && (TransferCfg->rx_data != NULL))
  572. {
  573. *rxdat++ = (uint8_t)I2Cx->DAT;
  574. TransferCfg->rx_count++;
  575. Ret = I2C_BYTE_RECV;
  576. }
  577. if(TransferCfg->rx_count == (TransferCfg->rx_length) ) {
  578. I2Cx->CONCLR = I2C_I2CONCLR_AAC|I2C_I2CONCLR_SIC;
  579. Ret = I2C_BYTE_RECV;
  580. }
  581. else {
  582. I2Cx->CONSET = I2C_I2CONSET_AA;
  583. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  584. }
  585. break;
  586. /* DATA has been received, Only the first data byte will be received with ACK. Additional
  587. data will be received with NOT ACK. */
  588. case I2C_I2STAT_S_RX_PRE_GENCALL_DAT_ACK:
  589. if ((TransferCfg->rx_count < TransferCfg->rx_length) && (TransferCfg->rx_data != NULL))
  590. {
  591. *rxdat++ = (uint8_t)I2Cx->DAT;
  592. TransferCfg->rx_count++;
  593. Ret = I2C_BYTE_RECV;
  594. }
  595. I2Cx->CONCLR = I2C_I2CONCLR_AAC|I2C_I2CONCLR_SIC;
  596. break;
  597. /* Writing phase -------------------------------------------------------- */
  598. /* Own SLA+R has been received, ACK has been returned */
  599. case I2C_I2STAT_S_TX_SLAR_ACK:
  600. /* Data has been transmitted, ACK has been received */
  601. case I2C_I2STAT_S_TX_DAT_ACK:
  602. /*
  603. * All data bytes that over-flow the specified receive
  604. * data length, just ignore them.
  605. */
  606. if ((TransferCfg->tx_count < TransferCfg->tx_length) && (TransferCfg->tx_data != NULL))
  607. {
  608. I2Cx->DAT = *txdat++;
  609. TransferCfg->tx_count++;
  610. Ret = I2C_BYTE_SENT;
  611. }
  612. I2Cx->CONSET = I2C_I2CONSET_AA;
  613. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  614. break;
  615. /* Arbitration lost in Slave Address and R/W bit as bus Master. Own Slave Address + Read
  616. has been received, ACK has been returned. */
  617. case I2C_I2STAT_S_TX_ARB_LOST_M_SLA:
  618. if ((TransferCfg->tx_count < TransferCfg->tx_length) && (TransferCfg->tx_data != NULL))
  619. {
  620. I2Cx->DAT = *txdat++;
  621. TransferCfg->tx_count++;
  622. Ret = I2C_BYTE_SENT;
  623. }
  624. I2Cx->CONSET = I2C_I2CONSET_AA|I2C_I2CONSET_STA;
  625. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  626. break;
  627. case I2C_I2STAT_S_TX_LAST_DAT_ACK:
  628. /* Data has been transmitted, NACK has been received,
  629. * that means there's no more data to send, exit now */
  630. /*
  631. * Note: Don't wait for stop event since in slave transmit mode,
  632. * since there no proof lets us know when a stop signal has been received
  633. * on slave side.
  634. */
  635. case I2C_I2STAT_S_TX_DAT_NACK:
  636. I2Cx->CONSET = I2C_I2CONSET_AA;
  637. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  638. Ret = I2C_SEND_END;
  639. break;
  640. /* Previously addressed with own SLA;
  641. * DATA byte has been received;
  642. * NOT ACK has been returned */
  643. case I2C_I2STAT_S_RX_PRE_SLA_DAT_NACK:
  644. /* DATA has been received, NOT ACK has been returned */
  645. case I2C_I2STAT_S_RX_PRE_GENCALL_DAT_NACK:
  646. I2Cx->CONSET = I2C_I2CONSET_AA;
  647. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  648. Ret = I2C_RECV_END;
  649. break;
  650. /*
  651. * Note that: Return code only let us know a stop condition mixed
  652. * with a repeat start condition in the same code value.
  653. * So we should provide a time-out. In case this is really a stop
  654. * condition, this will return back after time out condition. Otherwise,
  655. * next session that is slave receive data will be completed.
  656. */
  657. /* A Stop or a repeat start condition */
  658. case I2C_I2STAT_S_RX_STA_STO_SLVREC_SLVTRX:
  659. I2Cx->CONSET = I2C_I2CONSET_AA;
  660. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  661. Ret = I2C_STA_STO_RECV;
  662. break;
  663. /* No status information */
  664. case I2C_I2STAT_NO_INF:
  665. /* Other status must be captured */
  666. default:
  667. I2Cx->CONSET = I2C_I2CONSET_AA;
  668. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  669. break;
  670. }
  671. return Ret;
  672. }
  673. /*********************************************************************//**
  674. * @brief General Master Interrupt handler for I2C peripheral
  675. * @param[in] I2Cx I2C peripheral selected, should be:
  676. * - LPC_I2C
  677. * - LPC_I2C1
  678. * - LPC_I2C2
  679. * @return None
  680. **********************************************************************/
  681. void I2C_MasterHandler(en_I2C_unitId i2cId)
  682. {
  683. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  684. uint8_t returnCode;
  685. I2C_M_SETUP_Type *txrx_setup;
  686. int32_t Ret = I2C_OK;
  687. txrx_setup = (I2C_M_SETUP_Type *) i2cdat[i2cId].txrx_setup;
  688. returnCode = (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
  689. // Save current status
  690. txrx_setup->status = returnCode;
  691. Ret = I2C_MasterHanleStates(i2cId, returnCode, txrx_setup, I2C_TRANSFER_INTERRUPT);
  692. if(I2C_CheckError(Ret))
  693. {
  694. if(txrx_setup->retransmissions_count < txrx_setup->retransmissions_max)
  695. {
  696. // Retry
  697. txrx_setup->retransmissions_count ++;
  698. txrx_setup->tx_count = 0;
  699. txrx_setup->rx_count = 0;
  700. // Reset STA, STO, SI
  701. I2C_Start(I2Cx, I2C_TRANSFER_INTERRUPT);
  702. return;
  703. }
  704. else
  705. {
  706. goto s_int_end;
  707. }
  708. }
  709. else if (Ret & I2C_SEND_END)
  710. {
  711. // If no need to wait for data from Slave
  712. if(txrx_setup->rx_count >= (txrx_setup->rx_length))
  713. {
  714. goto s_int_end;
  715. }
  716. else // Start to wait for data from Slave
  717. {
  718. // Reset STA, STO, SI
  719. I2C_Start(I2Cx, I2C_TRANSFER_INTERRUPT);
  720. return;
  721. }
  722. }
  723. else if (Ret & I2C_RECV_END)
  724. {
  725. goto s_int_end;
  726. }
  727. else
  728. {
  729. return;
  730. }
  731. s_int_end:
  732. // Disable interrupt
  733. I2C_IntCmd(i2cId, FALSE);
  734. I2Cx->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  735. I2C_MasterComplete[i2cId] = TRUE;
  736. }
  737. /*********************************************************************//**
  738. * @brief General Slave Interrupt handler for I2C peripheral
  739. * @param[in] I2Cx I2C peripheral selected, should be:
  740. * - LPC_I2C0
  741. * - LPC_I2C1
  742. * - LPC_I2C2
  743. * @return None
  744. **********************************************************************/
  745. void I2C_SlaveHandler (en_I2C_unitId i2cId)
  746. {
  747. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  748. uint8_t returnCode;
  749. I2C_S_SETUP_Type *txrx_setup;
  750. uint32_t timeout;
  751. int32_t Ret = I2C_OK;
  752. txrx_setup = (I2C_S_SETUP_Type *) i2cdat[i2cId].txrx_setup;
  753. handle_state:
  754. returnCode = (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
  755. // Save current status
  756. txrx_setup->status = returnCode;
  757. Ret = I2C_SlaveHanleStates(i2cId, returnCode, txrx_setup);
  758. if(I2C_CheckError(Ret))
  759. {
  760. goto s_int_end;
  761. }
  762. else if (Ret & I2C_STA_STO_RECV)
  763. {
  764. if((txrx_setup->tx_count >= (txrx_setup->tx_length)) &&
  765. (txrx_setup->rx_count >= (txrx_setup->rx_length)))
  766. {
  767. goto s_int_end;
  768. }
  769. // Temporally lock the interrupt for timeout condition
  770. I2C_IntCmd(i2cId, FALSE);
  771. // enable time out
  772. timeout = I2C_SLAVE_TIME_OUT;
  773. while(1)
  774. {
  775. if (I2Cx->CONSET & I2C_I2CONSET_SI)
  776. {
  777. // re-Enable interrupt
  778. I2C_IntCmd(i2cId, TRUE);
  779. goto handle_state;
  780. }
  781. else
  782. {
  783. timeout--;
  784. if (timeout == 0)
  785. {
  786. // timeout occur, it's really a stop condition
  787. txrx_setup->status |= I2C_SETUP_STATUS_DONE;
  788. goto s_int_end;
  789. }
  790. }
  791. }
  792. }
  793. else if(Ret &I2C_SEND_END)
  794. {
  795. goto s_int_end;
  796. }
  797. else
  798. {
  799. return;
  800. }
  801. s_int_end:
  802. // Disable interrupt
  803. I2C_IntCmd(i2cId, FALSE);
  804. I2Cx->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  805. I2C_SlaveComplete[i2cId] = TRUE;
  806. }
  807. /*********************************************************************//**
  808. * @brief Transmit and Receive data in master mode
  809. * @param[in] I2Cx I2C peripheral selected, should be:
  810. * - LPC_I2C0
  811. * - LPC_I2C1
  812. * - LPC_I2C2
  813. * @param[in] TransferCfg Pointer to a I2C_M_SETUP_Type structure that
  814. * contains specified information about the
  815. * configuration for master transfer.
  816. * @param[in] Opt a I2C_TRANSFER_OPT_Type type that selected for
  817. * interrupt or polling mode.
  818. * @return SUCCESS or ERROR
  819. *
  820. * Note:
  821. * - In case of using I2C to transmit data only, either transmit length set to 0
  822. * or transmit data pointer set to NULL.
  823. * - In case of using I2C to receive data only, either receive length set to 0
  824. * or receive data pointer set to NULL.
  825. * - In case of using I2C to transmit followed by receive data, transmit length,
  826. * transmit data pointer, receive length and receive data pointer should be set
  827. * corresponding.
  828. **********************************************************************/
  829. Status I2C_MasterTransferData(en_I2C_unitId i2cId, I2C_M_SETUP_Type *TransferCfg,
  830. I2C_TRANSFER_OPT_Type Opt)
  831. {
  832. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  833. uint32_t CodeStatus;
  834. int32_t Ret = I2C_OK;
  835. // Reset I2C setup value to default state
  836. TransferCfg->tx_count = 0;
  837. TransferCfg->rx_count = 0;
  838. TransferCfg->status = 0;
  839. if (Opt == I2C_TRANSFER_POLLING)
  840. {
  841. /* First Start condition -------------------------------------------------------------- */
  842. TransferCfg->retransmissions_count = 0;
  843. retry:
  844. // Reset I2C setup value to default state
  845. TransferCfg->tx_count = 0;
  846. TransferCfg->rx_count = 0;
  847. // Start command
  848. CodeStatus = I2C_Start(I2Cx, I2C_TRANSFER_POLLING);
  849. while(1) // send data first and then receive data from Slave.
  850. {
  851. Ret = I2C_MasterHanleStates(i2cId, CodeStatus, TransferCfg, I2C_TRANSFER_POLLING);
  852. if(I2C_CheckError(Ret))
  853. {
  854. TransferCfg->retransmissions_count++;
  855. if (TransferCfg->retransmissions_count > TransferCfg->retransmissions_max){
  856. // save status
  857. TransferCfg->status = CodeStatus | I2C_SETUP_STATUS_NOACKF;
  858. goto error;
  859. } else {
  860. goto retry;
  861. }
  862. }
  863. else if( (Ret & I2C_BYTE_SENT) ||
  864. (Ret & I2C_BYTE_RECV))
  865. {
  866. // Wait for sending ends/ Wait for next byte
  867. while (!(I2Cx->CONSET & I2C_I2CONSET_SI));
  868. }
  869. else if (Ret & I2C_SEND_END) // already send all data
  870. {
  871. // If no need to wait for data from Slave
  872. if(TransferCfg->rx_count >= (TransferCfg->rx_length))
  873. {
  874. break;
  875. }
  876. else
  877. {
  878. I2C_Start(I2Cx, I2C_TRANSFER_POLLING);
  879. }
  880. }
  881. else if (Ret & I2C_RECV_END) // already receive all data
  882. {
  883. break;
  884. }
  885. CodeStatus = I2Cx->STAT & I2C_STAT_CODE_BITMASK;
  886. }
  887. return SUCCESS;
  888. error:
  889. return ERROR;
  890. }
  891. else if (Opt == I2C_TRANSFER_INTERRUPT)
  892. {
  893. // Setup tx_rx data, callback and interrupt handler
  894. i2cdat[i2cId].txrx_setup = (uint32_t) TransferCfg;
  895. // Set direction phase, write first
  896. i2cdat[i2cId].dir = 0;
  897. /* First Start condition -------------------------------------------------------------- */
  898. // Reset STA, STO, SI
  899. I2C_Start(I2Cx, I2C_TRANSFER_INTERRUPT);
  900. I2C_IntCmd(i2cId, TRUE);
  901. return (SUCCESS);
  902. }
  903. return ERROR;
  904. }
  905. /*********************************************************************//**
  906. * @brief Receive and Transmit data in slave mode
  907. * @param[in] I2Cx I2C peripheral selected, should be
  908. * - LPC_I2C0
  909. * - LPC_I2C1
  910. * - LPC_I2C2
  911. * @param[in] TransferCfg Pointer to a I2C_S_SETUP_Type structure that
  912. * contains specified information about the
  913. * configuration for master transfer.
  914. * @param[in] Opt I2C_TRANSFER_OPT_Type type that selected for
  915. * interrupt or polling mode.
  916. * @return SUCCESS or ERROR
  917. *
  918. * Note:
  919. * The mode of slave's operation depends on the command sent from master on
  920. * the I2C bus. If the master send a SLA+W command, this sub-routine will
  921. * use receive data length and receive data pointer. If the master send a SLA+R
  922. * command, this sub-routine will use transmit data length and transmit data
  923. * pointer.
  924. * If the master issue an repeat start command or a stop command, the slave will
  925. * enable an time out condition, during time out condition, if there's no activity
  926. * on I2C bus, the slave will exit, otherwise (i.e. the master send a SLA+R/W),
  927. * the slave then switch to relevant operation mode. The time out should be used
  928. * because the return status code can not show difference from stop and repeat
  929. * start command in slave operation.
  930. * In case of the expected data length from master is greater than data length
  931. * that slave can support:
  932. * - In case of reading operation (from master): slave will return I2C_I2DAT_IDLE_CHAR
  933. * value.
  934. * - In case of writing operation (from master): slave will ignore remain data from master.
  935. **********************************************************************/
  936. Status I2C_SlaveTransferData(en_I2C_unitId i2cId, I2C_S_SETUP_Type *TransferCfg,
  937. I2C_TRANSFER_OPT_Type Opt)
  938. {
  939. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  940. int32_t Ret = I2C_OK;
  941. uint32_t CodeStatus;
  942. uint32_t timeout;
  943. int32_t time_en;
  944. // Reset I2C setup value to default state
  945. TransferCfg->tx_count = 0;
  946. TransferCfg->rx_count = 0;
  947. TransferCfg->status = 0;
  948. // Polling option
  949. if (Opt == I2C_TRANSFER_POLLING)
  950. {
  951. /* Set AA bit to ACK command on I2C bus */
  952. I2Cx->CONSET = I2C_I2CONSET_AA;
  953. /* Clear SI bit to be ready ... */
  954. I2Cx->CONCLR = (I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC|I2C_I2CONCLR_STOC);
  955. time_en = 0;
  956. timeout = 0;
  957. while (1)
  958. {
  959. /* Check SI flag ready */
  960. if (I2Cx->CONSET & I2C_I2CONSET_SI)
  961. {
  962. time_en = 0;
  963. CodeStatus = (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
  964. Ret = I2C_SlaveHanleStates(i2cId, CodeStatus, TransferCfg);
  965. if(I2C_CheckError(Ret))
  966. {
  967. goto s_error;
  968. }
  969. else if(Ret & I2C_STA_STO_RECV)
  970. {
  971. if((TransferCfg->tx_count >= (TransferCfg->tx_length)) &&
  972. (TransferCfg->rx_count >= (TransferCfg->rx_length)))
  973. {
  974. goto s_end_stage;
  975. }
  976. time_en = 1;
  977. timeout = 0;
  978. }
  979. else if (Ret & I2C_SEND_END)
  980. {
  981. goto s_end_stage;
  982. }
  983. }
  984. else if (time_en)
  985. {
  986. if (timeout++ > I2C_SLAVE_TIME_OUT)
  987. {
  988. // it's really a stop condition, goto end stage
  989. goto s_end_stage;
  990. }
  991. }
  992. }
  993. s_end_stage:
  994. /* Clear AA bit to disable ACK on I2C bus */
  995. I2Cx->CONCLR = I2C_I2CONCLR_AAC;
  996. // Check if there's no error during operation
  997. // Update status
  998. TransferCfg->status = CodeStatus | I2C_SETUP_STATUS_DONE;
  999. return SUCCESS;
  1000. s_error:
  1001. /* Clear AA bit to disable ACK on I2C bus */
  1002. I2Cx->CONCLR = I2C_I2CONCLR_AAC;
  1003. // Update status
  1004. TransferCfg->status = CodeStatus;
  1005. return ERROR;
  1006. }
  1007. else if (Opt == I2C_TRANSFER_INTERRUPT)
  1008. {
  1009. // Setup tx_rx data, callback and interrupt handler
  1010. i2cdat[i2cId].txrx_setup = (uint32_t) TransferCfg;
  1011. // Set direction phase, read first
  1012. i2cdat[i2cId].dir = 1;
  1013. // Enable AA
  1014. I2Cx->CONSET = I2C_I2CONSET_AA;
  1015. I2Cx->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  1016. I2C_IntCmd(i2cId, TRUE);
  1017. return (SUCCESS);
  1018. }
  1019. return ERROR;
  1020. }
  1021. /*********************************************************************//**
  1022. * @brief Set Own slave address in I2C peripheral corresponding to
  1023. * parameter specified in OwnSlaveAddrConfigStruct.
  1024. * @param[in] I2Cx I2C peripheral selected, should be
  1025. * - LPC_I2C0
  1026. * - LPC_I2C1
  1027. * - LPC_I2C2
  1028. * @param[in] OwnSlaveAddrConfigStruct Pointer to a I2C_OWNSLAVEADDR_CFG_Type
  1029. * structure that contains the configuration information for the
  1030. * specified I2C slave address.
  1031. * @return None
  1032. **********************************************************************/
  1033. void I2C_SetOwnSlaveAddr(en_I2C_unitId i2cId, I2C_OWNSLAVEADDR_CFG_Type *OwnSlaveAddrConfigStruct)
  1034. {
  1035. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  1036. uint32_t tmp;
  1037. tmp = (((uint32_t)(OwnSlaveAddrConfigStruct->SlaveAddr_7bit << 1)) \
  1038. | ((OwnSlaveAddrConfigStruct->GeneralCallState == ENABLE) ? 0x01 : 0x00))& I2C_I2ADR_BITMASK;
  1039. switch (OwnSlaveAddrConfigStruct->SlaveAddrChannel)
  1040. {
  1041. case 0:
  1042. I2Cx->ADR0 = tmp;
  1043. I2Cx->MASK0 = I2C_I2MASK_MASK((uint32_t) \
  1044. (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  1045. break;
  1046. case 1:
  1047. I2Cx->ADR1 = tmp;
  1048. I2Cx->MASK1 = I2C_I2MASK_MASK((uint32_t) \
  1049. (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  1050. break;
  1051. case 2:
  1052. I2Cx->ADR2 = tmp;
  1053. I2Cx->MASK2 = I2C_I2MASK_MASK((uint32_t) \
  1054. (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  1055. break;
  1056. case 3:
  1057. I2Cx->ADR3 = tmp;
  1058. I2Cx->MASK3 = I2C_I2MASK_MASK((uint32_t) \
  1059. (OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
  1060. break;
  1061. }
  1062. }
  1063. /*********************************************************************//**
  1064. * @brief Configures functionality in I2C monitor mode
  1065. * @param[in] I2Cx I2C peripheral selected, should be
  1066. * - LPC_I2C0
  1067. * - LPC_I2C1
  1068. * - LPC_I2C2
  1069. * @param[in] Monitor configuration. It can include:
  1070. * - I2C_I2MMCTRL_ENA_SCL: I2C module can 'stretch'
  1071. * the clock line (hold it low) until it has had time to
  1072. * respond to an I2C interrupt.
  1073. * - I2C_I2MMCTRL_MATCH_ALL: When the I2C is in monitor mode,
  1074. * an interrupt will be generated on ANY address received.
  1075. * @param[in] NewState New State of this function, should be:
  1076. * - ENABLE: Enable this function.
  1077. * - DISABLE: Disable this function.
  1078. * @return None
  1079. **********************************************************************/
  1080. void I2C_MonitorModeConfig(en_I2C_unitId i2cId, uint32_t MonitorCfgType, FunctionalState NewState)
  1081. {
  1082. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  1083. if (NewState == ENABLE)
  1084. {
  1085. I2Cx->MMCTRL |= MonitorCfgType;
  1086. }
  1087. else
  1088. {
  1089. I2Cx->MMCTRL &= (~MonitorCfgType) & I2C_I2MMCTRL_BITMASK;
  1090. }
  1091. }
  1092. /*********************************************************************//**
  1093. * @brief Enable/Disable I2C monitor mode
  1094. * @param[in] I2Cx I2C peripheral selected, should be
  1095. * - LPC_I2C0
  1096. * - LPC_I2C1
  1097. * - LPC_I2C2
  1098. * @param[in] NewState New State of this function, should be:
  1099. * - ENABLE: Enable monitor mode.
  1100. * - DISABLE: Disable monitor mode.
  1101. * @return None
  1102. **********************************************************************/
  1103. void I2C_MonitorModeCmd(en_I2C_unitId i2cId, FunctionalState NewState)
  1104. {
  1105. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  1106. if (NewState == ENABLE)
  1107. {
  1108. I2Cx->MMCTRL |= I2C_I2MMCTRL_MM_ENA;
  1109. I2Cx->CONSET = I2C_I2CONSET_AA;
  1110. I2Cx->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
  1111. }
  1112. else
  1113. {
  1114. I2Cx->MMCTRL &= (~I2C_I2MMCTRL_MM_ENA) & I2C_I2MMCTRL_BITMASK;
  1115. I2Cx->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_AAC;
  1116. }
  1117. I2C_MonitorBufferIndex = 0;
  1118. }
  1119. /*********************************************************************//**
  1120. * @brief Get data from I2C data buffer in monitor mode.
  1121. * @param[in] I2Cx I2C peripheral selected, should be
  1122. * - LPC_I2C0
  1123. * - LPC_I2C1
  1124. * - LPC_I2C2
  1125. * @return None
  1126. * Note: In monitor mode, the I2C module may lose the ability to stretch
  1127. * the clock (stall the bus) if the ENA_SCL bit is not set. This means that
  1128. * the processor will have a limited amount of time to read the contents of
  1129. * the data received on the bus. If the processor reads the DAT shift
  1130. * register, as it ordinarily would, it could have only one bit-time to
  1131. * respond to the interrupt before the received data is overwritten by
  1132. * new data.
  1133. **********************************************************************/
  1134. uint8_t I2C_MonitorGetDatabuffer(en_I2C_unitId i2cId)
  1135. {
  1136. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  1137. return ((uint8_t)(I2Cx->DATA_BUFFER));
  1138. }
  1139. /*********************************************************************//**
  1140. * @brief Get data from I2C data buffer in monitor mode.
  1141. * @param[in] I2Cx I2C peripheral selected, should be
  1142. * - LPC_I2C0
  1143. * - LPC_I2C1
  1144. * - LPC_I2C2
  1145. * @return None
  1146. * Note: In monitor mode, the I2C module may lose the ability to stretch
  1147. * the clock (stall the bus) if the ENA_SCL bit is not set. This means that
  1148. * the processor will have a limited amount of time to read the contents of
  1149. * the data received on the bus. If the processor reads the DAT shift
  1150. * register, as it ordinarily would, it could have only one bit-time to
  1151. * respond to the interrupt before the received data is overwritten by
  1152. * new data.
  1153. **********************************************************************/
  1154. BOOL_8 I2C_MonitorHandler(en_I2C_unitId i2cId, uint8_t *buffer, uint32_t size)
  1155. {
  1156. LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
  1157. BOOL_8 ret=FALSE;
  1158. I2Cx->CONCLR = I2C_I2CONCLR_SIC;
  1159. buffer[I2C_MonitorBufferIndex] = (uint8_t)(I2Cx->DATA_BUFFER);
  1160. I2C_MonitorBufferIndex++;
  1161. if(I2C_MonitorBufferIndex >= size)
  1162. {
  1163. ret = TRUE;
  1164. }
  1165. return ret;
  1166. }
  1167. /*********************************************************************//**
  1168. * @brief Get status of Master Transfer
  1169. * @param[in] I2Cx I2C peripheral selected, should be:
  1170. * - LPC_I2C0
  1171. * - LPC_I2C1
  1172. * - LPC_I2C2
  1173. * @return Master transfer status, could be:
  1174. * - TRUE master transfer completed
  1175. * - FALSE master transfer have not completed yet
  1176. **********************************************************************/
  1177. uint32_t I2C_MasterTransferComplete(en_I2C_unitId i2cId)
  1178. {
  1179. uint32_t retval;
  1180. retval = I2C_MasterComplete[i2cId];
  1181. I2C_MasterComplete[i2cId] = FALSE;
  1182. return retval;
  1183. }
  1184. /*********************************************************************//**
  1185. * @brief Get status of Slave Transfer
  1186. * @param[in] I2Cx I2C peripheral selected, should be:
  1187. * - LPC_I2C0
  1188. * - LPC_I2C1
  1189. * - LPC_I2C2
  1190. * @return Complete status, could be: TRUE/FALSE
  1191. **********************************************************************/
  1192. uint32_t I2C_SlaveTransferComplete(en_I2C_unitId i2cId)
  1193. {
  1194. uint32_t retval;
  1195. retval = I2C_SlaveComplete[i2cId];
  1196. I2C_SlaveComplete[i2cId] = FALSE;
  1197. return retval;
  1198. }
  1199. #endif /*_I2C*/
  1200. /**
  1201. * @}
  1202. */
  1203. /**
  1204. * @}
  1205. */
  1206. /* --------------------------------- End Of File ------------------------------ */