efm32_i2c.c 25 KB

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