1
0

em_i2c.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Inter-intergrated 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. #ifndef __EM_I2C_H
  34. #define __EM_I2C_H
  35. #include <stdbool.h>
  36. #include "em_part.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /***************************************************************************//**
  41. * @addtogroup EM_Library
  42. * @{
  43. ******************************************************************************/
  44. /***************************************************************************//**
  45. * @addtogroup I2C
  46. * @{
  47. ******************************************************************************/
  48. /*******************************************************************************
  49. ******************************* DEFINES ***********************************
  50. ******************************************************************************/
  51. /**
  52. * @brief
  53. * Standard mode max frequency assuming using 4:4 ratio for Nlow:Nhigh.
  54. * @details
  55. * From I2C specification: Min Tlow = 4.7us, min Thigh = 4.0us,
  56. * max Trise=1.0us, max Tfall=0.3us. Since ratio is 4:4, have to use
  57. * worst case value of Tlow or Thigh as base.
  58. *
  59. * 1/(Tlow + Thigh + 1us + 0.3us) = 1/(4.7 + 4.7 + 1.3)us = 93458Hz
  60. */
  61. #define I2C_FREQ_STANDARD_MAX 93500
  62. /**
  63. * @brief
  64. * Fast mode max frequency assuming using 6:3 ratio for Nlow:Nhigh.
  65. * @details
  66. * From I2C specification: Min Tlow = 1.3us, min Thigh = 0.6us,
  67. * max Trise=0.3us, max Tfall=0.3us. Since ratio is 6:3, have to use
  68. * worst case value of Tlow or 2xThigh as base.
  69. *
  70. * 1/(Tlow + Thigh + 0.3us + 0.3us) = 1/(1.3 + 0.65 + 0.6)us = 392157Hz
  71. */
  72. #define I2C_FREQ_FAST_MAX 392500
  73. /**
  74. * @brief
  75. * Fast mode+ max frequency assuming using 11:6 ratio for Nlow:Nhigh.
  76. * @details
  77. * From I2C specification: Min Tlow = 0.5us, min Thigh = 0.26us,
  78. * max Trise=0.012us, max Tfall=0.12us. Since ratio is 11:6, have to use
  79. * worst case value of Tlow or (11/6)xThigh as base.
  80. *
  81. * 1/(Tlow + Thigh + 0.12us + 0.12us) = 1/(0.5 + 0.273 + 0.24)us = 987167Hz
  82. */
  83. #define I2C_FREQ_FASTPLUS_MAX 987500
  84. /**
  85. * @brief
  86. * Indicate plain write sequence: S+ADDR(W)+DATA0+P.
  87. * @details
  88. * @li S - Start
  89. * @li ADDR(W) - address with W/R bit cleared
  90. * @li DATA0 - Data taken from buffer with index 0
  91. * @li P - Stop
  92. */
  93. #define I2C_FLAG_WRITE 0x0001
  94. /**
  95. * @brief
  96. * Indicate plain read sequence: S+ADDR(R)+DATA0+P.
  97. * @details
  98. * @li S - Start
  99. * @li ADDR(R) - address with W/R bit set
  100. * @li DATA0 - Data read into buffer with index 0
  101. * @li P - Stop
  102. */
  103. #define I2C_FLAG_READ 0x0002
  104. /**
  105. * @brief
  106. * Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
  107. * @details
  108. * @li S - Start
  109. * @li Sr - Repeated start
  110. * @li ADDR(W) - address with W/R bit cleared
  111. * @li ADDR(R) - address with W/R bit set
  112. * @li DATAn - Data written from/read into buffer with index n
  113. * @li P - Stop
  114. */
  115. #define I2C_FLAG_WRITE_READ 0x0004
  116. /**
  117. * @brief
  118. * Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
  119. * @details
  120. * @li S - Start
  121. * @li ADDR(W) - address with W/R bit cleared
  122. * @li DATAn - Data written from buffer with index n
  123. * @li P - Stop
  124. */
  125. #define I2C_FLAG_WRITE_WRITE 0x0008
  126. /** Use 10 bit address. */
  127. #define I2C_FLAG_10BIT_ADDR 0x0010
  128. /*******************************************************************************
  129. ******************************** ENUMS ************************************
  130. ******************************************************************************/
  131. /** Clock low to high ratio settings. */
  132. typedef enum
  133. {
  134. i2cClockHLRStandard = _I2C_CTRL_CLHR_STANDARD, /**< Ratio is 4:4 */
  135. i2cClockHLRAsymetric = _I2C_CTRL_CLHR_ASYMMETRIC, /**< Ratio is 6:3 */
  136. i2cClockHLRFast = _I2C_CTRL_CLHR_FAST /**< Ratio is 11:3 */
  137. } I2C_ClockHLR_TypeDef;
  138. /** Return codes for single master mode transfer function. */
  139. typedef enum
  140. {
  141. /* In progress code (>0) */
  142. i2cTransferInProgress = 1, /**< Transfer in progress. */
  143. /* Complete code (=0) */
  144. i2cTransferDone = 0, /**< Transfer completed successfully. */
  145. /* Transfer error codes (<0) */
  146. i2cTransferNack = -1, /**< NACK received during transfer. */
  147. i2cTransferBusErr = -2, /**< Bus error during transfer (misplaced START/STOP). */
  148. i2cTransferArbLost = -3, /**< Arbitration lost during transfer. */
  149. i2cTransferUsageFault = -4, /**< Usage fault. */
  150. i2cTransferSwFault = -5 /**< SW fault. */
  151. } I2C_TransferReturn_TypeDef;
  152. /*******************************************************************************
  153. ******************************* STRUCTS ***********************************
  154. ******************************************************************************/
  155. /** I2C initialization structure. */
  156. typedef struct
  157. {
  158. /** Enable I2C peripheral when init completed. */
  159. bool enable;
  160. /** Set to master (true) or slave (false) mode */
  161. bool master;
  162. /**
  163. * I2C reference clock assumed when configuring bus frequency setup.
  164. * Set it to 0 if currently configurated reference clock shall be used
  165. * This parameter is only applicable if operating in master mode.
  166. */
  167. uint32_t refFreq;
  168. /**
  169. * (Max) I2C bus frequency to use. This parameter is only applicable
  170. * if operating in master mode.
  171. */
  172. uint32_t freq;
  173. /** Clock low/high ratio control. */
  174. I2C_ClockHLR_TypeDef clhr;
  175. } I2C_Init_TypeDef;
  176. /** Suggested default config for I2C init structure. */
  177. #define I2C_INIT_DEFAULT \
  178. { true, /* Enable when init done */ \
  179. true, /* Set to master mode */ \
  180. 0, /* Use currently configured reference clock */ \
  181. I2C_FREQ_STANDARD_MAX, /* Set to standard rate assuring being */ \
  182. /* within I2C spec */ \
  183. i2cClockHLRStandard /* Set to use 4:4 low/high duty cycle */ \
  184. }
  185. /**
  186. * @brief
  187. * Master mode transfer message structure used to define a complete
  188. * I2C transfer sequence (from start to stop).
  189. * @details
  190. * The structure allows for defining the following types of sequences,
  191. * please refer to defines for sequence details.
  192. * @li #I2C_FLAG_READ - data read into buf[0].data
  193. * @li #I2C_FLAG_WRITE - data written from buf[0].data
  194. * @li #I2C_FLAG_WRITE_READ - data written from buf[0].data and read
  195. * into buf[1].data
  196. * @li #I2C_FLAG_WRITE_WRITE - data written from buf[0].data and
  197. * buf[1].data
  198. */
  199. typedef struct
  200. {
  201. /**
  202. * @brief
  203. * Address to use after (repeated) start.
  204. * @details
  205. * Layout details, A = address bit, X = don't care bit (set to 0):
  206. * @li 7 bit address - use format AAAA AAAX.
  207. * @li 10 bit address - use format XXXX XAAX AAAA AAAA
  208. */
  209. uint16_t addr;
  210. /** Flags defining sequence type and details, see I2C_FLAG_... defines. */
  211. uint16_t flags;
  212. /**
  213. * Buffers used to hold data to send from or receive into depending
  214. * on sequence type.
  215. */
  216. struct
  217. {
  218. /** Buffer used for data to transmit/receive, must be @p len long. */
  219. uint8_t *data;
  220. /**
  221. * Number of bytes in @p data to send or receive. Notice that when
  222. * receiving data to this buffer, at least 1 byte must be received.
  223. * Setting @p len to 0 in the receive case is considered a usage fault.
  224. * Transmitting 0 bytes is legal, in which case only the address
  225. * is transmitted after the start condition.
  226. */
  227. uint16_t len;
  228. } buf[2];
  229. } I2C_TransferSeq_TypeDef;
  230. /*******************************************************************************
  231. ***************************** PROTOTYPES **********************************
  232. ******************************************************************************/
  233. uint32_t I2C_BusFreqGet(I2C_TypeDef *i2c);
  234. void I2C_BusFreqSet(I2C_TypeDef *i2c,
  235. uint32_t refFreq,
  236. uint32_t freq,
  237. I2C_ClockHLR_TypeDef type);
  238. void I2C_Enable(I2C_TypeDef *i2c, bool enable);
  239. void I2C_Init(I2C_TypeDef *i2c, const I2C_Init_TypeDef *init);
  240. /***************************************************************************//**
  241. * @brief
  242. * Clear one or more pending I2C interrupts.
  243. *
  244. * @param[in] i2c
  245. * Pointer to I2C peripheral register block.
  246. *
  247. * @param[in] flags
  248. * Pending I2C interrupt source to clear. Use a bitwse logic OR combination of
  249. * valid interrupt flags for the I2C module (I2C_IF_nnn).
  250. ******************************************************************************/
  251. __STATIC_INLINE void I2C_IntClear(I2C_TypeDef *i2c, uint32_t flags)
  252. {
  253. i2c->IFC = flags;
  254. }
  255. /***************************************************************************//**
  256. * @brief
  257. * Disable one or more I2C interrupts.
  258. *
  259. * @param[in] i2c
  260. * Pointer to I2C peripheral register block.
  261. *
  262. * @param[in] flags
  263. * I2C interrupt sources to disable. Use a bitwise logic OR combination of
  264. * valid interrupt flags for the I2C module (I2C_IF_nnn).
  265. ******************************************************************************/
  266. __STATIC_INLINE void I2C_IntDisable(I2C_TypeDef *i2c, uint32_t flags)
  267. {
  268. i2c->IEN &= ~(flags);
  269. }
  270. /***************************************************************************//**
  271. * @brief
  272. * Enable one or more I2C interrupts.
  273. *
  274. * @note
  275. * Depending on the use, a pending interrupt may already be set prior to
  276. * enabling the interrupt. Consider using I2C_IntClear() prior to enabling
  277. * if such a pending interrupt should be ignored.
  278. *
  279. * @param[in] i2c
  280. * Pointer to I2C peripheral register block.
  281. *
  282. * @param[in] flags
  283. * I2C interrupt sources to enable. Use a bitwise logic OR combination of
  284. * valid interrupt flags for the I2C module (I2C_IF_nnn).
  285. ******************************************************************************/
  286. __STATIC_INLINE void I2C_IntEnable(I2C_TypeDef *i2c, uint32_t flags)
  287. {
  288. i2c->IEN |= flags;
  289. }
  290. /***************************************************************************//**
  291. * @brief
  292. * Get pending I2C interrupt flags.
  293. *
  294. * @note
  295. * The event bits are not cleared by the use of this function.
  296. *
  297. * @param[in] i2c
  298. * Pointer to I2C peripheral register block.
  299. *
  300. * @return
  301. * I2C interrupt sources pending. A bitwise logic OR combination of valid
  302. * interrupt flags for the I2C module (I2C_IF_nnn).
  303. ******************************************************************************/
  304. __STATIC_INLINE uint32_t I2C_IntGet(I2C_TypeDef *i2c)
  305. {
  306. return(i2c->IF);
  307. }
  308. /***************************************************************************//**
  309. * @brief
  310. * Set one or more pending I2C interrupts from SW.
  311. *
  312. * @param[in] i2c
  313. * Pointer to I2C peripheral register block.
  314. *
  315. * @param[in] flags
  316. * I2C interrupt sources to set to pending. Use a bitwise logic OR combination
  317. * of valid interrupt flags for the I2C module (I2C_IF_nnn).
  318. ******************************************************************************/
  319. __STATIC_INLINE void I2C_IntSet(I2C_TypeDef *i2c, uint32_t flags)
  320. {
  321. i2c->IFS = flags;
  322. }
  323. void I2C_Reset(I2C_TypeDef *i2c);
  324. /***************************************************************************//**
  325. * @brief
  326. * Get slave address used for I2C peripheral (when operating in slave mode).
  327. *
  328. * @details
  329. * For 10 bit addressing mode, the address is split in two bytes, and only
  330. * the first byte setting is fetched, effectively only controlling the 2 most
  331. * significant bits of the 10 bit address. Full handling of 10 bit addressing
  332. * in slave mode requires additional SW handling.
  333. *
  334. * @param[in] i2c
  335. * Pointer to I2C peripheral register block.
  336. *
  337. * @return
  338. * I2C slave address in use. The 7 most significant bits define the actual
  339. * address, the least significant bit is reserved and always returned as 0.
  340. ******************************************************************************/
  341. __STATIC_INLINE uint8_t I2C_SlaveAddressGet(I2C_TypeDef *i2c)
  342. {
  343. return((uint8_t)(i2c->SADDR));
  344. }
  345. /***************************************************************************//**
  346. * @brief
  347. * Set slave address to use for I2C peripheral (when operating in slave mode).
  348. *
  349. * @details
  350. * For 10 bit addressing mode, the address is split in two bytes, and only
  351. * the first byte is set, effectively only controlling the 2 most significant
  352. * bits of the 10 bit address. Full handling of 10 bit addressing in slave
  353. * mode requires additional SW handling.
  354. *
  355. * @param[in] i2c
  356. * Pointer to I2C peripheral register block.
  357. *
  358. * @param[in] addr
  359. * I2C slave address to use. The 7 most significant bits define the actual
  360. * address, the least significant bit is reserved and always set to 0.
  361. ******************************************************************************/
  362. __STATIC_INLINE void I2C_SlaveAddressSet(I2C_TypeDef *i2c, uint8_t addr)
  363. {
  364. i2c->SADDR = (uint32_t)addr & 0xfe;
  365. }
  366. /***************************************************************************//**
  367. * @brief
  368. * Get slave address mask used for I2C peripheral (when operating in slave
  369. * mode).
  370. *
  371. * @details
  372. * The address mask defines how the comparator works. A bit position with
  373. * value 0 means that the corresponding slave address bit is ignored during
  374. * comparison (don't care). A bit position with value 1 means that the
  375. * corresponding slave address bit must match.
  376. *
  377. * For 10 bit addressing mode, the address is split in two bytes, and only
  378. * the mask for the first address byte is fetched, effectively only
  379. * controlling the 2 most significant bits of the 10 bit address.
  380. *
  381. * @param[in] i2c
  382. * Pointer to I2C peripheral register block.
  383. *
  384. * @return
  385. * I2C slave address mask in use. The 7 most significant bits define the
  386. * actual address mask, the least significant bit is reserved and always
  387. * returned as 0.
  388. ******************************************************************************/
  389. __STATIC_INLINE uint8_t I2C_SlaveAddressMaskGet(I2C_TypeDef *i2c)
  390. {
  391. return((uint8_t)(i2c->SADDRMASK));
  392. }
  393. /***************************************************************************//**
  394. * @brief
  395. * Set slave address mask used for I2C peripheral (when operating in slave
  396. * mode).
  397. *
  398. * @details
  399. * The address mask defines how the comparator works. A bit position with
  400. * value 0 means that the corresponding slave address bit is ignored during
  401. * comparison (don't care). A bit position with value 1 means that the
  402. * corresponding slave address bit must match.
  403. *
  404. * For 10 bit addressing mode, the address is split in two bytes, and only
  405. * the mask for the first address byte is set, effectively only controlling
  406. * the 2 most significant bits of the 10 bit address.
  407. *
  408. * @param[in] i2c
  409. * Pointer to I2C peripheral register block.
  410. *
  411. * @param[in] mask
  412. * I2C slave address mask to use. The 7 most significant bits define the
  413. * actual address mask, the least significant bit is reserved and should
  414. * be 0.
  415. ******************************************************************************/
  416. __STATIC_INLINE void I2C_SlaveAddressMaskSet(I2C_TypeDef *i2c, uint8_t mask)
  417. {
  418. i2c->SADDRMASK = (uint32_t)mask & 0xfe;
  419. }
  420. I2C_TransferReturn_TypeDef I2C_Transfer(I2C_TypeDef *i2c);
  421. I2C_TransferReturn_TypeDef I2C_TransferInit(I2C_TypeDef *i2c,
  422. I2C_TransferSeq_TypeDef *seq);
  423. /** @} (end addtogroup I2C) */
  424. /** @} (end addtogroup EM_Library) */
  425. #ifdef __cplusplus
  426. }
  427. #endif
  428. #endif /* __EM_I2C_H */