em_i2c.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Inter-integrated Circuit (I2C) Peripheral API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #include "em_part.h"
  34. #include "em_i2c.h"
  35. #include "em_cmu.h"
  36. #include "em_bitband.h"
  37. #include "em_assert.h"
  38. /***************************************************************************//**
  39. * @addtogroup EM_Library
  40. * @{
  41. ******************************************************************************/
  42. /***************************************************************************//**
  43. * @addtogroup I2C
  44. * @brief Inter-integrated Circuit (I2C) Peripheral API
  45. * @{
  46. ******************************************************************************/
  47. /*******************************************************************************
  48. ******************************* DEFINES ***********************************
  49. ******************************************************************************/
  50. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  51. /** Validation of I2C register block pointer reference for assert statements. */
  52. #if defined(_EFM32_GECKO_FAMILY) || defined(_EFM32_TINY_FAMILY)
  53. #define I2C_REF_VALID(ref) ((ref) == I2C0)
  54. #endif
  55. #if defined(_EFM32_GIANT_FAMILY)
  56. #define I2C_REF_VALID(ref) ((ref == I2C0) || (ref == I2C1))
  57. #endif
  58. /** Error flags indicating I2C transfer has failed somehow. */
  59. /* Notice that I2C_IF_TXOF (transmit overflow) is not really possible with */
  60. /* this SW supporting master mode. Likewise for I2C_IF_RXUF (receive underflow) */
  61. /* RXUF is only likely to occur with this SW if using a debugger peeking into */
  62. /* RXDATA register. Thus, we ignore those types of fault. */
  63. #define I2C_IF_ERRORS (I2C_IF_BUSERR | I2C_IF_ARBLOST)
  64. /** @endcond */
  65. /*******************************************************************************
  66. ******************************** ENUMS ************************************
  67. ******************************************************************************/
  68. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  69. /** Master mode transfer states. */
  70. typedef enum
  71. {
  72. i2cStateStartAddrSend, /**< Send start + (first part of) address. */
  73. i2cStateAddrWFAckNack, /**< Wait for ACK/NACK on (first part of) address. */
  74. i2cStateAddrWF2ndAckNack, /**< Wait for ACK/NACK on second part of 10 bit address. */
  75. i2cStateRStartAddrSend, /**< Send repeated start + (first part of) address. */
  76. i2cStateRAddrWFAckNack, /**< Wait for ACK/NACK on address sent after repeated start. */
  77. i2cStateDataSend, /**< Send data. */
  78. i2cStateDataWFAckNack, /**< Wait for ACK/NACK on data sent. */
  79. i2cStateWFData, /**< Wait for data. */
  80. i2cStateWFStopSent, /**< Wait for STOP to have been transmitted. */
  81. i2cStateDone /**< Transfer completed successfully. */
  82. } I2C_TransferState_TypeDef;
  83. /** @endcond */
  84. /*******************************************************************************
  85. ******************************* STRUCTS ***********************************
  86. ******************************************************************************/
  87. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  88. /** Structure used to store state information on an ongoing master mode transfer. */
  89. typedef struct
  90. {
  91. /** Current state. */
  92. I2C_TransferState_TypeDef state;
  93. /** Result return code. */
  94. I2C_TransferReturn_TypeDef result;
  95. /** Offset in current sequence buffer. */
  96. uint16_t offset;
  97. /* Index to current sequence buffer in use. */
  98. uint8_t bufIndx;
  99. /** Reference to I2C transfer sequence definition provided by user. */
  100. I2C_TransferSeq_TypeDef *seq;
  101. } I2C_Transfer_TypeDef;
  102. /** @endcond */
  103. /*******************************************************************************
  104. ***************************** LOCAL DATA *******^**************************
  105. ******************************************************************************/
  106. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  107. /**
  108. * Lookup table for Nlow + Nhigh setting defined by CLHR. Set undefined
  109. * index (0x3) to reflect default setting just in case.
  110. */
  111. static const uint8_t i2cNSum[] = { 4 + 4, 6 + 3, 11 + 3, 4 + 4 };
  112. /** Transfer state info for ongoing master mode transfer */
  113. static I2C_Transfer_TypeDef i2cTransfer[I2C_COUNT];
  114. /** @endcond */
  115. /*******************************************************************************
  116. ************************** GLOBAL FUNCTIONS *******************************
  117. ******************************************************************************/
  118. /***************************************************************************//**
  119. * @brief
  120. * Get current configured I2C bus frequency.
  121. *
  122. * @details
  123. * This frequency is only of relevance when acting as master.
  124. *
  125. * @param[in] i2c
  126. * Pointer to I2C peripheral register block.
  127. *
  128. * @return
  129. * Current I2C frequency in Hz.
  130. ******************************************************************************/
  131. uint32_t I2C_BusFreqGet(I2C_TypeDef *i2c)
  132. {
  133. uint32_t hfperclk;
  134. uint32_t n;
  135. /* Max frequency is given by fSCL = fHFPERCLK/((Nlow + Nhigh)(DIV + 1) + 4) */
  136. hfperclk = CMU_ClockFreqGet(cmuClock_HFPER);
  137. n = (uint32_t)(i2cNSum[(i2c->CTRL & _I2C_CTRL_CLHR_MASK) >> _I2C_CTRL_CLHR_SHIFT]);
  138. return(hfperclk / ((n * (i2c->CLKDIV + 1)) + 4));
  139. }
  140. /***************************************************************************//**
  141. * @brief
  142. * Set I2C bus frequency.
  143. *
  144. * @details
  145. * The bus frequency is only of relevance when acting as a master. The bus
  146. * frequency should not be set higher than the max frequency accepted by the
  147. * slowest device on the bus.
  148. *
  149. * Notice that due to asymmetric requirements on low and high I2C clock
  150. * cycles by the I2C specification, the actual max frequency allowed in order
  151. * to comply with the specification may be somewhat lower than expected.
  152. *
  153. * Please refer to the reference manual, details on I2C clock generation,
  154. * for max allowed theoretical frequencies for different modes.
  155. *
  156. * @param[in] i2c
  157. * Pointer to I2C peripheral register block.
  158. *
  159. * @param[in] refFreq
  160. * I2C reference clock frequency in Hz that will be used. If set to 0,
  161. * the currently configured reference clock is assumed. Setting it to a higher
  162. * than actual configured value only has the consequence of reducing the real
  163. * I2C frequency.
  164. *
  165. * @param[in] freq
  166. * Bus frequency to set (actual bus speed may be lower due to integer
  167. * prescaling). Safe (according to I2C specification) max frequencies for
  168. * standard, fast and fast+ modes are available using I2C_FREQ_ defines.
  169. * (Using I2C_FREQ_ defines requires corresponding setting of @p type.)
  170. * Slowest slave device on bus must always be considered.
  171. *
  172. * @param[in] type
  173. * Clock low to high ratio type to use. If not using i2cClockHLRStandard,
  174. * make sure all devices on the bus support the specified mode. Using a
  175. * non-standard ratio is useful to achieve higher bus clock in fast and
  176. * fast+ modes.
  177. ******************************************************************************/
  178. void I2C_BusFreqSet(I2C_TypeDef *i2c,
  179. uint32_t refFreq,
  180. uint32_t freq,
  181. I2C_ClockHLR_TypeDef type)
  182. {
  183. uint32_t n;
  184. uint32_t div;
  185. /* Unused parameter */
  186. (void)type;
  187. /* Avoid divide by 0 */
  188. EFM_ASSERT(freq);
  189. if (!freq)
  190. {
  191. return;
  192. }
  193. /* Frequency is given by fSCL = fHFPERCLK/((Nlow + Nhigh)(DIV + 1) + 4), thus */
  194. /* DIV = ((fHFPERCLK - 4fSCL)/((Nlow + Nhigh)fSCL)) - 1 */
  195. if (!refFreq)
  196. {
  197. refFreq = CMU_ClockFreqGet(cmuClock_HFPER);
  198. }
  199. n = (uint32_t)(i2cNSum[(i2c->CTRL & _I2C_CTRL_CLHR_MASK) >> _I2C_CTRL_CLHR_SHIFT]);
  200. div = (refFreq - (4 * freq)) / (n * freq);
  201. EFM_ASSERT(div);
  202. if (div)
  203. {
  204. div--;
  205. }
  206. /* Clock divisor must be at least 1 in slave mode according to reference */
  207. /* manual (in which case there is normally no need to set bus frequency). */
  208. if ((i2c->CTRL & I2C_CTRL_SLAVE) && !div)
  209. {
  210. div = 1;
  211. }
  212. EFM_ASSERT(div <= _I2C_CLKDIV_DIV_MASK);
  213. i2c->CLKDIV = div;
  214. }
  215. /***************************************************************************//**
  216. * @brief
  217. * Enable/disable I2C.
  218. *
  219. * @note
  220. * After enabling the I2C (from being disabled), the I2C is in BUSY state.
  221. *
  222. * @param[in] i2c
  223. * Pointer to I2C peripheral register block.
  224. *
  225. * @param[in] enable
  226. * true to enable counting, false to disable.
  227. ******************************************************************************/
  228. void I2C_Enable(I2C_TypeDef *i2c, bool enable)
  229. {
  230. EFM_ASSERT(I2C_REF_VALID(i2c));
  231. BITBAND_Peripheral(&(i2c->CTRL), _I2C_CTRL_EN_SHIFT, (unsigned int)enable);
  232. }
  233. /***************************************************************************//**
  234. * @brief
  235. * Initialize I2C.
  236. *
  237. * @param[in] i2c
  238. * Pointer to I2C peripheral register block.
  239. *
  240. * @param[in] init
  241. * Pointer to I2C initialization structure.
  242. ******************************************************************************/
  243. void I2C_Init(I2C_TypeDef *i2c, const I2C_Init_TypeDef *init)
  244. {
  245. EFM_ASSERT(I2C_REF_VALID(i2c));
  246. i2c->IEN = 0;
  247. i2c->IFC = _I2C_IFC_MASK;
  248. I2C_BusFreqSet(i2c, init->refFreq, init->freq, init->clhr);
  249. BITBAND_Peripheral(&(i2c->CTRL),
  250. _I2C_CTRL_SLAVE_SHIFT,
  251. ~((unsigned int)(init->master)));
  252. BITBAND_Peripheral(&(i2c->CTRL),
  253. _I2C_CTRL_EN_SHIFT,
  254. (unsigned int)(init->enable));
  255. }
  256. /***************************************************************************//**
  257. * @brief
  258. * Reset I2C to same state as after a HW reset.
  259. *
  260. * @note
  261. * The ROUTE register is NOT reset by this function, in order to allow for
  262. * centralized setup of this feature.
  263. *
  264. * @param[in] i2c
  265. * Pointer to I2C peripheral register block.
  266. ******************************************************************************/
  267. void I2C_Reset(I2C_TypeDef *i2c)
  268. {
  269. i2c->CTRL = _I2C_CTRL_RESETVALUE;
  270. i2c->CLKDIV = _I2C_CLKDIV_RESETVALUE;
  271. i2c->SADDR = _I2C_SADDR_RESETVALUE;
  272. i2c->SADDRMASK = _I2C_SADDRMASK_RESETVALUE;
  273. i2c->IEN = _I2C_IEN_RESETVALUE;
  274. i2c->IFC = _I2C_IFC_MASK;
  275. /* Do not reset route register, setting should be done independently */
  276. }
  277. /***************************************************************************//**
  278. * @brief
  279. * Continue an initiated I2C transfer (single master mode only).
  280. *
  281. * @details
  282. * This function is used repeatedly after a I2C_TransferInit() in order to
  283. * complete a transfer. It may be used in polled mode as the below example
  284. * shows:
  285. * @verbatim
  286. * I2C_TransferReturn_TypeDef ret;
  287. *
  288. * // Do a polled transfer
  289. * ret = I2C_TransferInit(I2C0, seq);
  290. * while (ret == i2cTransferInProgress)
  291. * {
  292. * ret = I2C_Transfer(I2C0);
  293. * }
  294. * @endverbatim
  295. * It may also be used in interrupt driven mode, where this function is invoked
  296. * from the interrupt handler. Notice that if used in interrupt mode, NVIC
  297. * interrupts must be configured and enabled for the I2C bus used. I2C
  298. * peripheral specific interrupts are managed by this SW.
  299. *
  300. * @note
  301. * Only single master mode is supported.
  302. *
  303. * @param[in] i2c
  304. * Pointer to I2C peripheral register block.
  305. *
  306. * @return
  307. * Returns status for ongoing transfer.
  308. * @li #i2cTransferInProgress - indicates that transfer not finished.
  309. * @li #i2cTransferDone - transfer completed successfully.
  310. * @li otherwise some sort of error has occurred.
  311. *
  312. ******************************************************************************/
  313. I2C_TransferReturn_TypeDef I2C_Transfer(I2C_TypeDef *i2c)
  314. {
  315. uint32_t tmp;
  316. uint32_t pending;
  317. I2C_Transfer_TypeDef *transfer;
  318. I2C_TransferSeq_TypeDef *seq;
  319. EFM_ASSERT(I2C_REF_VALID(i2c));
  320. /* Support up to 2 I2C buses */
  321. if (i2c == I2C0)
  322. {
  323. transfer = i2cTransfer;
  324. }
  325. #if (I2C_COUNT > 1)
  326. else if (i2c == I2C1)
  327. {
  328. transfer = i2cTransfer + 1;
  329. }
  330. #endif
  331. else
  332. {
  333. return(i2cTransferUsageFault);
  334. }
  335. seq = transfer->seq;
  336. for (;; )
  337. {
  338. pending = i2c->IF;
  339. /* If some sort of fault, abort transfer. */
  340. if (pending & I2C_IF_ERRORS)
  341. {
  342. if (pending & I2C_IF_ARBLOST)
  343. {
  344. /* If arbitration fault, it indicates either a slave device */
  345. /* not responding as expected, or other master which is not */
  346. /* supported by this SW. */
  347. transfer->result = i2cTransferArbLost;
  348. }
  349. else if (pending & I2C_IF_BUSERR)
  350. {
  351. /* A bus error indicates a misplaced start or stop, which should */
  352. /* not occur in master mode controlled by this SW. */
  353. transfer->result = i2cTransferBusErr;
  354. }
  355. /* If error situation occurred, it is difficult to know */
  356. /* exact cause and how to resolve. It will be up to a wrapper */
  357. /* to determine how to handle a fault/recovery if possible. */
  358. transfer->state = i2cStateDone;
  359. goto done;
  360. }
  361. switch (transfer->state)
  362. {
  363. /***************************************************/
  364. /* Send first start+address (first byte if 10 bit) */
  365. /***************************************************/
  366. case i2cStateStartAddrSend:
  367. if (seq->flags & I2C_FLAG_10BIT_ADDR)
  368. {
  369. tmp = (((uint32_t)(seq->addr) >> 8) & 0x06) | 0xf0;
  370. /* In 10 bit address mode, the address following the first */
  371. /* start always indicate write. */
  372. }
  373. else
  374. {
  375. tmp = (uint32_t)(seq->addr) & 0xfe;
  376. if (seq->flags & I2C_FLAG_READ)
  377. {
  378. /* Indicate read request */
  379. tmp |= 1;
  380. }
  381. }
  382. transfer->state = i2cStateAddrWFAckNack;
  383. i2c->TXDATA = tmp; /* Data not transmitted until START sent */
  384. i2c->CMD = I2C_CMD_START;
  385. goto done;
  386. /*******************************************************/
  387. /* Wait for ACK/NACK on address (first byte if 10 bit) */
  388. /*******************************************************/
  389. case i2cStateAddrWFAckNack:
  390. if (pending & I2C_IF_NACK)
  391. {
  392. i2c->IFC = I2C_IFC_NACK;
  393. transfer->result = i2cTransferNack;
  394. transfer->state = i2cStateWFStopSent;
  395. i2c->CMD = I2C_CMD_STOP;
  396. }
  397. else if (pending & I2C_IF_ACK)
  398. {
  399. i2c->IFC = I2C_IFC_ACK;
  400. /* If 10 bit address, send 2nd byte of address. */
  401. if (seq->flags & I2C_FLAG_10BIT_ADDR)
  402. {
  403. transfer->state = i2cStateAddrWF2ndAckNack;
  404. i2c->TXDATA = (uint32_t)(seq->addr) & 0xff;
  405. }
  406. else
  407. {
  408. /* Determine whether receiving or sending data */
  409. if (seq->flags & I2C_FLAG_READ)
  410. {
  411. transfer->state = i2cStateWFData;
  412. }
  413. else
  414. {
  415. transfer->state = i2cStateDataSend;
  416. continue;
  417. }
  418. }
  419. }
  420. goto done;
  421. /******************************************************/
  422. /* Wait for ACK/NACK on second byte of 10 bit address */
  423. /******************************************************/
  424. case i2cStateAddrWF2ndAckNack:
  425. if (pending & I2C_IF_NACK)
  426. {
  427. i2c->IFC = I2C_IFC_NACK;
  428. transfer->result = i2cTransferNack;
  429. transfer->state = i2cStateWFStopSent;
  430. i2c->CMD = I2C_CMD_STOP;
  431. }
  432. else if (pending & I2C_IF_ACK)
  433. {
  434. i2c->IFC = I2C_IFC_ACK;
  435. /* If using plain read sequence with 10 bit address, switch to send */
  436. /* repeated start. */
  437. if (seq->flags & I2C_FLAG_READ)
  438. {
  439. transfer->state = i2cStateRStartAddrSend;
  440. }
  441. /* Otherwise expected to write 0 or more bytes */
  442. else
  443. {
  444. transfer->state = i2cStateDataSend;
  445. }
  446. continue;
  447. }
  448. goto done;
  449. /*******************************/
  450. /* Send repeated start+address */
  451. /*******************************/
  452. case i2cStateRStartAddrSend:
  453. if (seq->flags & I2C_FLAG_10BIT_ADDR)
  454. {
  455. tmp = ((seq->addr >> 8) & 0x06) | 0xf0;
  456. }
  457. else
  458. {
  459. tmp = seq->addr & 0xfe;
  460. }
  461. /* If this is a write+read combined sequence, then read is about to start */
  462. if (seq->flags & I2C_FLAG_WRITE_READ)
  463. {
  464. /* Indicate read request */
  465. tmp |= 1;
  466. }
  467. transfer->state = i2cStateRAddrWFAckNack;
  468. /* We have to write START cmd first since repeated start, otherwise */
  469. /* data would be sent first. */
  470. i2c->CMD = I2C_CMD_START;
  471. i2c->TXDATA = tmp;
  472. goto done;
  473. /**********************************************************************/
  474. /* Wait for ACK/NACK on repeated start+address (first byte if 10 bit) */
  475. /**********************************************************************/
  476. case i2cStateRAddrWFAckNack:
  477. if (pending & I2C_IF_NACK)
  478. {
  479. i2c->IFC = I2C_IFC_NACK;
  480. transfer->result = i2cTransferNack;
  481. transfer->state = i2cStateWFStopSent;
  482. i2c->CMD = I2C_CMD_STOP;
  483. }
  484. else if (pending & I2C_IF_ACK)
  485. {
  486. i2c->IFC = I2C_IFC_ACK;
  487. /* Determine whether receiving or sending data */
  488. if (seq->flags & I2C_FLAG_WRITE_READ)
  489. {
  490. transfer->state = i2cStateWFData;
  491. }
  492. else
  493. {
  494. transfer->state = i2cStateDataSend;
  495. continue;
  496. }
  497. }
  498. goto done;
  499. /*****************************/
  500. /* Send a data byte to slave */
  501. /*****************************/
  502. case i2cStateDataSend:
  503. /* Reached end of data buffer? */
  504. if (transfer->offset >= seq->buf[transfer->bufIndx].len)
  505. {
  506. /* Move to next message part */
  507. transfer->offset = 0;
  508. transfer->bufIndx++;
  509. /* Send repeated start when switching to read mode on 2nd buffer */
  510. if (seq->flags & I2C_FLAG_WRITE_READ)
  511. {
  512. transfer->state = i2cStateRStartAddrSend;
  513. continue;
  514. }
  515. /* Only writing from one buffer, or finished both buffers */
  516. if ((seq->flags & I2C_FLAG_WRITE) || (transfer->bufIndx > 1))
  517. {
  518. transfer->state = i2cStateWFStopSent;
  519. i2c->CMD = I2C_CMD_STOP;
  520. goto done;
  521. }
  522. /* Reprocess in case next buffer is empty */
  523. continue;
  524. }
  525. /* Send byte */
  526. i2c->TXDATA = (uint32_t)(seq->buf[transfer->bufIndx].data[transfer->offset++]);
  527. transfer->state = i2cStateDataWFAckNack;
  528. goto done;
  529. /*********************************************************/
  530. /* Wait for ACK/NACK from slave after sending data to it */
  531. /*********************************************************/
  532. case i2cStateDataWFAckNack:
  533. if (pending & I2C_IF_NACK)
  534. {
  535. i2c->IFC = I2C_IFC_NACK;
  536. transfer->result = i2cTransferNack;
  537. transfer->state = i2cStateWFStopSent;
  538. i2c->CMD = I2C_CMD_STOP;
  539. }
  540. else if (pending & I2C_IF_ACK)
  541. {
  542. i2c->IFC = I2C_IFC_ACK;
  543. transfer->state = i2cStateDataSend;
  544. continue;
  545. }
  546. goto done;
  547. /****************************/
  548. /* Wait for data from slave */
  549. /****************************/
  550. case i2cStateWFData:
  551. if (pending & I2C_IF_RXDATAV)
  552. {
  553. uint8_t data;
  554. /* Must read out data in order to not block further progress */
  555. data = (uint8_t)(i2c->RXDATA);
  556. /* Make sure not storing beyond end of buffer just in case */
  557. if (transfer->offset < seq->buf[transfer->bufIndx].len)
  558. {
  559. seq->buf[transfer->bufIndx].data[transfer->offset++] = data;
  560. }
  561. /* If we have read all requested data, then the sequence should end */
  562. if (transfer->offset >= seq->buf[transfer->bufIndx].len)
  563. {
  564. transfer->state = i2cStateWFStopSent;
  565. i2c->CMD = I2C_CMD_NACK;
  566. i2c->CMD = I2C_CMD_STOP;
  567. }
  568. else
  569. {
  570. /* Send ACK and wait for next byte */
  571. i2c->CMD = I2C_CMD_ACK;
  572. }
  573. }
  574. goto done;
  575. /***********************************/
  576. /* Wait for STOP to have been sent */
  577. /***********************************/
  578. case i2cStateWFStopSent:
  579. if (pending & I2C_IF_MSTOP)
  580. {
  581. i2c->IFC = I2C_IFC_MSTOP;
  582. transfer->state = i2cStateDone;
  583. }
  584. goto done;
  585. /******************************/
  586. /* Unexpected state, SW fault */
  587. /******************************/
  588. default:
  589. transfer->result = i2cTransferSwFault;
  590. transfer->state = i2cStateDone;
  591. goto done;
  592. }
  593. }
  594. done:
  595. if (transfer->state == i2cStateDone)
  596. {
  597. /* Disable interrupt sources when done */
  598. i2c->IEN = 0;
  599. /* Update result unless some fault already occurred */
  600. if (transfer->result == i2cTransferInProgress)
  601. {
  602. transfer->result = i2cTransferDone;
  603. }
  604. }
  605. /* Until transfer is done keep returning i2cTransferInProgress */
  606. else
  607. {
  608. return(i2cTransferInProgress);
  609. }
  610. return transfer->result;
  611. }
  612. /***************************************************************************//**
  613. * @brief
  614. * Prepare and start an I2C transfer (single master mode only).
  615. *
  616. * @details
  617. * This function must be invoked in order to start an I2C transfer
  618. * sequence. In order to actually complete the transfer, I2C_Transfer() must
  619. * be used either in polled mode or by adding a small driver wrapper utilizing
  620. * interrupts.
  621. *
  622. * @note
  623. * Only single master mode is supported.
  624. *
  625. * @param[in] i2c
  626. * Pointer to I2C peripheral register block.
  627. *
  628. * @param[in] seq
  629. * Pointer to sequence structure defining the I2C transfer to take place. The
  630. * referenced structure must exist until the transfer has fully completed.
  631. *
  632. * @return
  633. * Returns status for ongoing transfer:
  634. * @li #i2cTransferInProgress - indicates that transfer not finished.
  635. * @li otherwise some sort of error has occurred.
  636. ******************************************************************************/
  637. I2C_TransferReturn_TypeDef I2C_TransferInit(I2C_TypeDef *i2c,
  638. I2C_TransferSeq_TypeDef *seq)
  639. {
  640. I2C_Transfer_TypeDef *transfer;
  641. EFM_ASSERT(I2C_REF_VALID(i2c));
  642. EFM_ASSERT(seq);
  643. /* Support up to 2 I2C buses */
  644. if (i2c == I2C0)
  645. {
  646. transfer = i2cTransfer;
  647. }
  648. #if (I2C_COUNT > 1)
  649. else if (i2c == I2C1)
  650. {
  651. transfer = i2cTransfer + 1;
  652. }
  653. #endif
  654. else
  655. {
  656. return(i2cTransferUsageFault);
  657. }
  658. /* Check if in busy state. Since this SW assumes single master, we can */
  659. /* just issue an abort. The BUSY state is normal after a reset. */
  660. if (i2c->STATE & I2C_STATE_BUSY)
  661. {
  662. i2c->CMD = I2C_CMD_ABORT;
  663. }
  664. /* Make sure user is not trying to read 0 bytes, it is not */
  665. /* possible according to I2C spec, since slave will always start */
  666. /* sending first byte ACK on address. The read operation can */
  667. /* only be stopped by NACKing a received byte, ie minimum 1 byte. */
  668. if (((seq->flags & I2C_FLAG_READ) && !(seq->buf[0].len)) ||
  669. ((seq->flags & I2C_FLAG_WRITE_READ) && !(seq->buf[1].len))
  670. )
  671. {
  672. return(i2cTransferUsageFault);
  673. }
  674. /* Prepare for a transfer */
  675. transfer->state = i2cStateStartAddrSend;
  676. transfer->result = i2cTransferInProgress;
  677. transfer->offset = 0;
  678. transfer->bufIndx = 0;
  679. transfer->seq = seq;
  680. /* Ensure buffers are empty */
  681. i2c->CMD = I2C_CMD_CLEARPC | I2C_CMD_CLEARTX;
  682. if (i2c->IF & I2C_IF_RXDATAV)
  683. {
  684. i2c->RXDATA;
  685. }
  686. /* Clear all pending interrupts prior to starting transfer. */
  687. i2c->IFC = _I2C_IFC_MASK;
  688. /* Enable those interrupts we are interested in throughout transfer. */
  689. /* Notice that the I2C interrupt must also be enabled in the NVIC, but */
  690. /* that is left for an additional driver wrapper. */
  691. i2c->IEN = I2C_IF_NACK | I2C_IF_ACK | I2C_IF_MSTOP |
  692. I2C_IF_RXDATAV | I2C_IF_ERRORS;
  693. /* Start transfer */
  694. return(I2C_Transfer(i2c));
  695. }
  696. /** @} (end addtogroup I2C) */
  697. /** @} (end addtogroup EM_Library) */