1 /***************************************************************************//**
\r
3 * @brief Inter-intergrated circuit (I2C) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
33 #ifndef __SILICON_LABS_EM_I2C_H__
\r
34 #define __SILICON_LABS_EM_I2C_H__
\r
36 #include "em_device.h"
\r
37 #if defined(I2C_COUNT) && (I2C_COUNT > 0)
\r
39 #include <stdbool.h>
\r
45 /***************************************************************************//**
\r
46 * @addtogroup EM_Library
\r
48 ******************************************************************************/
\r
50 /***************************************************************************//**
\r
53 ******************************************************************************/
\r
55 /*******************************************************************************
\r
56 ******************************* DEFINES ***********************************
\r
57 ******************************************************************************/
\r
61 * Standard mode max frequency assuming using 4:4 ratio for Nlow:Nhigh.
\r
63 * From I2C specification: Min Tlow = 4.7us, min Thigh = 4.0us,
\r
64 * max Trise=1.0us, max Tfall=0.3us. Since ratio is 4:4, have to use
\r
65 * worst case value of Tlow or Thigh as base.
\r
67 * 1/(Tlow + Thigh + 1us + 0.3us) = 1/(4.7 + 4.7 + 1.3)us = 93458Hz
\r
69 * Due to chip characteristics, the max value is somewhat reduced.
\r
71 #if defined(_EFM32_GECKO_FAMILY) || defined(_EFM32_TINY_FAMILY) \
\r
72 || defined(_EFM32_ZERO_FAMILY) || defined(_EFM32_HAPPY_FAMILY)
\r
73 #define I2C_FREQ_STANDARD_MAX 93000
\r
74 #elif defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_WONDER_FAMILY)
\r
75 #define I2C_FREQ_STANDARD_MAX 92000
\r
76 #elif defined(_SILICON_LABS_32B_PLATFORM_2)
\r
77 // None of the chips on this platform has been characterized on this parameter.
\r
78 // Use same value as on Wonder until further notice.
\r
79 #define I2C_FREQ_STANDARD_MAX 92000
\r
81 #error "Unknown device family."
\r
86 * Fast mode max frequency assuming using 6:3 ratio for Nlow:Nhigh.
\r
88 * From I2C specification: Min Tlow = 1.3us, min Thigh = 0.6us,
\r
89 * max Trise=0.3us, max Tfall=0.3us. Since ratio is 6:3, have to use
\r
90 * worst case value of Tlow or 2xThigh as base.
\r
92 * 1/(Tlow + Thigh + 0.3us + 0.3us) = 1/(1.3 + 0.65 + 0.6)us = 392157Hz
\r
94 #define I2C_FREQ_FAST_MAX 392157
\r
99 * Fast mode+ max frequency assuming using 11:6 ratio for Nlow:Nhigh.
\r
101 * From I2C specification: Min Tlow = 0.5us, min Thigh = 0.26us,
\r
102 * max Trise=0.12us, max Tfall=0.12us. Since ratio is 11:6, have to use
\r
103 * worst case value of Tlow or (11/6)xThigh as base.
\r
105 * 1/(Tlow + Thigh + 0.12us + 0.12us) = 1/(0.5 + 0.273 + 0.24)us = 987167Hz
\r
107 #define I2C_FREQ_FASTPLUS_MAX 987167
\r
112 * Indicate plain write sequence: S+ADDR(W)+DATA0+P.
\r
115 * @li ADDR(W) - address with W/R bit cleared
\r
116 * @li DATA0 - Data taken from buffer with index 0
\r
119 #define I2C_FLAG_WRITE 0x0001
\r
123 * Indicate plain read sequence: S+ADDR(R)+DATA0+P.
\r
126 * @li ADDR(R) - address with W/R bit set
\r
127 * @li DATA0 - Data read into buffer with index 0
\r
130 #define I2C_FLAG_READ 0x0002
\r
134 * Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
\r
137 * @li Sr - Repeated start
\r
138 * @li ADDR(W) - address with W/R bit cleared
\r
139 * @li ADDR(R) - address with W/R bit set
\r
140 * @li DATAn - Data written from/read into buffer with index n
\r
143 #define I2C_FLAG_WRITE_READ 0x0004
\r
147 * Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
\r
150 * @li ADDR(W) - address with W/R bit cleared
\r
151 * @li DATAn - Data written from buffer with index n
\r
154 #define I2C_FLAG_WRITE_WRITE 0x0008
\r
156 /** Use 10 bit address. */
\r
157 #define I2C_FLAG_10BIT_ADDR 0x0010
\r
160 /*******************************************************************************
\r
161 ******************************** ENUMS ************************************
\r
162 ******************************************************************************/
\r
164 /** Clock low to high ratio settings. */
\r
167 i2cClockHLRStandard = _I2C_CTRL_CLHR_STANDARD, /**< Ratio is 4:4 */
\r
168 i2cClockHLRAsymetric = _I2C_CTRL_CLHR_ASYMMETRIC, /**< Ratio is 6:3 */
\r
169 i2cClockHLRFast = _I2C_CTRL_CLHR_FAST /**< Ratio is 11:3 */
\r
170 } I2C_ClockHLR_TypeDef;
\r
173 /** Return codes for single master mode transfer function. */
\r
176 /* In progress code (>0) */
\r
177 i2cTransferInProgress = 1, /**< Transfer in progress. */
\r
179 /* Complete code (=0) */
\r
180 i2cTransferDone = 0, /**< Transfer completed successfully. */
\r
182 /* Transfer error codes (<0) */
\r
183 i2cTransferNack = -1, /**< NACK received during transfer. */
\r
184 i2cTransferBusErr = -2, /**< Bus error during transfer (misplaced START/STOP). */
\r
185 i2cTransferArbLost = -3, /**< Arbitration lost during transfer. */
\r
186 i2cTransferUsageFault = -4, /**< Usage fault. */
\r
187 i2cTransferSwFault = -5 /**< SW fault. */
\r
188 } I2C_TransferReturn_TypeDef;
\r
191 /*******************************************************************************
\r
192 ******************************* STRUCTS ***********************************
\r
193 ******************************************************************************/
\r
195 /** I2C initialization structure. */
\r
198 /** Enable I2C peripheral when init completed. */
\r
201 /** Set to master (true) or slave (false) mode */
\r
205 * I2C reference clock assumed when configuring bus frequency setup.
\r
206 * Set it to 0 if currently configurated reference clock shall be used
\r
207 * This parameter is only applicable if operating in master mode.
\r
212 * (Max) I2C bus frequency to use. This parameter is only applicable
\r
213 * if operating in master mode.
\r
217 /** Clock low/high ratio control. */
\r
218 I2C_ClockHLR_TypeDef clhr;
\r
219 } I2C_Init_TypeDef;
\r
221 /** Suggested default config for I2C init structure. */
\r
222 #define I2C_INIT_DEFAULT \
\r
224 true, /* Enable when init done */ \
\r
225 true, /* Set to master mode */ \
\r
226 0, /* Use currently configured reference clock */ \
\r
227 I2C_FREQ_STANDARD_MAX, /* Set to standard rate assuring being */ \
\r
228 /* within I2C spec */ \
\r
229 i2cClockHLRStandard /* Set to use 4:4 low/high duty cycle */ \
\r
235 * Master mode transfer message structure used to define a complete
\r
236 * I2C transfer sequence (from start to stop).
\r
238 * The structure allows for defining the following types of sequences,
\r
239 * please refer to defines for sequence details.
\r
240 * @li #I2C_FLAG_READ - data read into buf[0].data
\r
241 * @li #I2C_FLAG_WRITE - data written from buf[0].data
\r
242 * @li #I2C_FLAG_WRITE_READ - data written from buf[0].data and read
\r
244 * @li #I2C_FLAG_WRITE_WRITE - data written from buf[0].data and
\r
251 * Address to use after (repeated) start.
\r
253 * Layout details, A = address bit, X = don't care bit (set to 0):
\r
254 * @li 7 bit address - use format AAAA AAAX.
\r
255 * @li 10 bit address - use format XXXX XAAX AAAA AAAA
\r
259 /** Flags defining sequence type and details, see I2C_FLAG_... defines. */
\r
263 * Buffers used to hold data to send from or receive into depending
\r
264 * on sequence type.
\r
268 /** Buffer used for data to transmit/receive, must be @p len long. */
\r
272 * Number of bytes in @p data to send or receive. Notice that when
\r
273 * receiving data to this buffer, at least 1 byte must be received.
\r
274 * Setting @p len to 0 in the receive case is considered a usage fault.
\r
275 * Transmitting 0 bytes is legal, in which case only the address
\r
276 * is transmitted after the start condition.
\r
280 } I2C_TransferSeq_TypeDef;
\r
283 /*******************************************************************************
\r
284 ***************************** PROTOTYPES **********************************
\r
285 ******************************************************************************/
\r
287 uint32_t I2C_BusFreqGet(I2C_TypeDef *i2c);
\r
288 void I2C_BusFreqSet(I2C_TypeDef *i2c,
\r
291 I2C_ClockHLR_TypeDef i2cMode);
\r
292 void I2C_Enable(I2C_TypeDef *i2c, bool enable);
\r
293 void I2C_Init(I2C_TypeDef *i2c, const I2C_Init_TypeDef *init);
\r
295 /***************************************************************************//**
\r
297 * Clear one or more pending I2C interrupts.
\r
300 * Pointer to I2C peripheral register block.
\r
303 * Pending I2C interrupt source to clear. Use a bitwse logic OR combination of
\r
304 * valid interrupt flags for the I2C module (I2C_IF_nnn).
\r
305 ******************************************************************************/
\r
306 __STATIC_INLINE void I2C_IntClear(I2C_TypeDef *i2c, uint32_t flags)
\r
312 /***************************************************************************//**
\r
314 * Disable one or more I2C interrupts.
\r
317 * Pointer to I2C peripheral register block.
\r
320 * I2C interrupt sources to disable. Use a bitwise logic OR combination of
\r
321 * valid interrupt flags for the I2C module (I2C_IF_nnn).
\r
322 ******************************************************************************/
\r
323 __STATIC_INLINE void I2C_IntDisable(I2C_TypeDef *i2c, uint32_t flags)
\r
325 i2c->IEN &= ~(flags);
\r
329 /***************************************************************************//**
\r
331 * Enable one or more I2C interrupts.
\r
334 * Depending on the use, a pending interrupt may already be set prior to
\r
335 * enabling the interrupt. Consider using I2C_IntClear() prior to enabling
\r
336 * if such a pending interrupt should be ignored.
\r
339 * Pointer to I2C peripheral register block.
\r
342 * I2C interrupt sources to enable. Use a bitwise logic OR combination of
\r
343 * valid interrupt flags for the I2C module (I2C_IF_nnn).
\r
344 ******************************************************************************/
\r
345 __STATIC_INLINE void I2C_IntEnable(I2C_TypeDef *i2c, uint32_t flags)
\r
351 /***************************************************************************//**
\r
353 * Get pending I2C interrupt flags.
\r
356 * The event bits are not cleared by the use of this function.
\r
359 * Pointer to I2C peripheral register block.
\r
362 * I2C interrupt sources pending. A bitwise logic OR combination of valid
\r
363 * interrupt flags for the I2C module (I2C_IF_nnn).
\r
364 ******************************************************************************/
\r
365 __STATIC_INLINE uint32_t I2C_IntGet(I2C_TypeDef *i2c)
\r
371 /***************************************************************************//**
\r
373 * Get enabled and pending I2C interrupt flags.
\r
374 * Useful for handling more interrupt sources in the same interrupt handler.
\r
377 * Interrupt flags are not cleared by the use of this function.
\r
380 * Pointer to I2C peripheral register block.
\r
383 * Pending and enabled I2C interrupt sources
\r
384 * The return value is the bitwise AND of
\r
385 * - the enabled interrupt sources in I2Cn_IEN and
\r
386 * - the pending interrupt flags I2Cn_IF
\r
387 ******************************************************************************/
\r
388 __STATIC_INLINE uint32_t I2C_IntGetEnabled(I2C_TypeDef *i2c)
\r
393 return i2c->IF & ien;
\r
397 /***************************************************************************//**
\r
399 * Set one or more pending I2C interrupts from SW.
\r
402 * Pointer to I2C peripheral register block.
\r
405 * I2C interrupt sources to set to pending. Use a bitwise logic OR combination
\r
406 * of valid interrupt flags for the I2C module (I2C_IF_nnn).
\r
407 ******************************************************************************/
\r
408 __STATIC_INLINE void I2C_IntSet(I2C_TypeDef *i2c, uint32_t flags)
\r
413 void I2C_Reset(I2C_TypeDef *i2c);
\r
415 /***************************************************************************//**
\r
417 * Get slave address used for I2C peripheral (when operating in slave mode).
\r
420 * For 10 bit addressing mode, the address is split in two bytes, and only
\r
421 * the first byte setting is fetched, effectively only controlling the 2 most
\r
422 * significant bits of the 10 bit address. Full handling of 10 bit addressing
\r
423 * in slave mode requires additional SW handling.
\r
426 * Pointer to I2C peripheral register block.
\r
429 * I2C slave address in use. The 7 most significant bits define the actual
\r
430 * address, the least significant bit is reserved and always returned as 0.
\r
431 ******************************************************************************/
\r
432 __STATIC_INLINE uint8_t I2C_SlaveAddressGet(I2C_TypeDef *i2c)
\r
434 return ((uint8_t)(i2c->SADDR));
\r
438 /***************************************************************************//**
\r
440 * Set slave address to use for I2C peripheral (when operating in slave mode).
\r
443 * For 10 bit addressing mode, the address is split in two bytes, and only
\r
444 * the first byte is set, effectively only controlling the 2 most significant
\r
445 * bits of the 10 bit address. Full handling of 10 bit addressing in slave
\r
446 * mode requires additional SW handling.
\r
449 * Pointer to I2C peripheral register block.
\r
452 * I2C slave address to use. The 7 most significant bits define the actual
\r
453 * address, the least significant bit is reserved and always set to 0.
\r
454 ******************************************************************************/
\r
455 __STATIC_INLINE void I2C_SlaveAddressSet(I2C_TypeDef *i2c, uint8_t addr)
\r
457 i2c->SADDR = (uint32_t)addr & 0xfe;
\r
461 /***************************************************************************//**
\r
463 * Get slave address mask used for I2C peripheral (when operating in slave
\r
467 * The address mask defines how the comparator works. A bit position with
\r
468 * value 0 means that the corresponding slave address bit is ignored during
\r
469 * comparison (don't care). A bit position with value 1 means that the
\r
470 * corresponding slave address bit must match.
\r
472 * For 10 bit addressing mode, the address is split in two bytes, and only
\r
473 * the mask for the first address byte is fetched, effectively only
\r
474 * controlling the 2 most significant bits of the 10 bit address.
\r
477 * Pointer to I2C peripheral register block.
\r
480 * I2C slave address mask in use. The 7 most significant bits define the
\r
481 * actual address mask, the least significant bit is reserved and always
\r
483 ******************************************************************************/
\r
484 __STATIC_INLINE uint8_t I2C_SlaveAddressMaskGet(I2C_TypeDef *i2c)
\r
486 return ((uint8_t)(i2c->SADDRMASK));
\r
490 /***************************************************************************//**
\r
492 * Set slave address mask used for I2C peripheral (when operating in slave
\r
496 * The address mask defines how the comparator works. A bit position with
\r
497 * value 0 means that the corresponding slave address bit is ignored during
\r
498 * comparison (don't care). A bit position with value 1 means that the
\r
499 * corresponding slave address bit must match.
\r
501 * For 10 bit addressing mode, the address is split in two bytes, and only
\r
502 * the mask for the first address byte is set, effectively only controlling
\r
503 * the 2 most significant bits of the 10 bit address.
\r
506 * Pointer to I2C peripheral register block.
\r
509 * I2C slave address mask to use. The 7 most significant bits define the
\r
510 * actual address mask, the least significant bit is reserved and should
\r
512 ******************************************************************************/
\r
513 __STATIC_INLINE void I2C_SlaveAddressMaskSet(I2C_TypeDef *i2c, uint8_t mask)
\r
515 i2c->SADDRMASK = (uint32_t)mask & 0xfe;
\r
519 I2C_TransferReturn_TypeDef I2C_Transfer(I2C_TypeDef *i2c);
\r
520 I2C_TransferReturn_TypeDef I2C_TransferInit(I2C_TypeDef *i2c,
\r
521 I2C_TransferSeq_TypeDef *seq);
\r
523 /** @} (end addtogroup I2C) */
\r
524 /** @} (end addtogroup EM_Library) */
\r
530 #endif /* defined(I2C_COUNT) && (I2C_COUNT > 0) */
\r
531 #endif /* __SILICON_LABS_EM_I2C_H__ */
\r