em_i2c.h

Go to the documentation of this file.
00001 /***************************************************************************/
00034 #ifndef __EM_I2C_H
00035 #define __EM_I2C_H
00036 
00037 #include "em_device.h"
00038 #if defined(I2C_COUNT) && (I2C_COUNT > 0)
00039 
00040 #include <stdbool.h>
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 /***************************************************************************/
00051 /***************************************************************************/
00056 /*******************************************************************************
00057  *******************************   DEFINES   ***********************************
00058  ******************************************************************************/
00059 
00070 #define I2C_FREQ_STANDARD_MAX    93500
00071 
00082 #define I2C_FREQ_FAST_MAX        392500
00083 
00084 
00095 #define I2C_FREQ_FASTPLUS_MAX    987500
00096 
00097 
00107 #define I2C_FLAG_WRITE          0x0001
00108 
00118 #define I2C_FLAG_READ           0x0002
00119 
00131 #define I2C_FLAG_WRITE_READ     0x0004
00132 
00142 #define I2C_FLAG_WRITE_WRITE    0x0008
00143 
00145 #define I2C_FLAG_10BIT_ADDR     0x0010
00146 
00147 
00148 /*******************************************************************************
00149  ********************************   ENUMS   ************************************
00150  ******************************************************************************/
00151 
00153 typedef enum
00154 {
00155   i2cClockHLRStandard  = _I2C_CTRL_CLHR_STANDARD,      
00156   i2cClockHLRAsymetric = _I2C_CTRL_CLHR_ASYMMETRIC,    
00157   i2cClockHLRFast      = _I2C_CTRL_CLHR_FAST           
00158 } I2C_ClockHLR_TypeDef;
00159 
00160 
00162 typedef enum
00163 {
00164   /* In progress code (>0) */
00165   i2cTransferInProgress = 1,    
00167   /* Complete code (=0) */
00168   i2cTransferDone       = 0,    
00170   /* Transfer error codes (<0) */
00171   i2cTransferNack       = -1,   
00172   i2cTransferBusErr     = -2,   
00173   i2cTransferArbLost    = -3,   
00174   i2cTransferUsageFault = -4,   
00175   i2cTransferSwFault    = -5    
00176 } I2C_TransferReturn_TypeDef;
00177 
00178 
00179 /*******************************************************************************
00180  *******************************   STRUCTS   ***********************************
00181  ******************************************************************************/
00182 
00184 typedef struct
00185 {
00187   bool                 enable;
00188 
00190   bool                 master;
00191 
00197   uint32_t             refFreq;
00198 
00203   uint32_t             freq;
00204 
00206   I2C_ClockHLR_TypeDef clhr;
00207 } I2C_Init_TypeDef;
00208 
00210 #define I2C_INIT_DEFAULT                                                    \
00211   { true,                    /* Enable when init done */                    \
00212     true,                    /* Set to master mode */                       \
00213     0,                       /* Use currently configured reference clock */ \
00214     I2C_FREQ_STANDARD_MAX,   /* Set to standard rate assuring being */      \
00215                              /* within I2C spec */                          \
00216     i2cClockHLRStandard      /* Set to use 4:4 low/high duty cycle */       \
00217   }
00218 
00219 
00234 typedef struct
00235 {
00244   uint16_t addr;
00245 
00247   uint16_t flags;
00248 
00253   struct
00254   {
00256     uint8_t  *data;
00257 
00265     uint16_t len;
00266   } buf[2];
00267 } I2C_TransferSeq_TypeDef;
00268 
00269 
00270 /*******************************************************************************
00271  *****************************   PROTOTYPES   **********************************
00272  ******************************************************************************/
00273 
00274 uint32_t I2C_BusFreqGet(I2C_TypeDef *i2c);
00275 void I2C_BusFreqSet(I2C_TypeDef *i2c,
00276                     uint32_t refFreq,
00277                     uint32_t freq,
00278                     I2C_ClockHLR_TypeDef type);
00279 void I2C_Enable(I2C_TypeDef *i2c, bool enable);
00280 void I2C_Init(I2C_TypeDef *i2c, const I2C_Init_TypeDef *init);
00281 
00282 /***************************************************************************/
00293 __STATIC_INLINE void I2C_IntClear(I2C_TypeDef *i2c, uint32_t flags)
00294 {
00295   i2c->IFC = flags;
00296 }
00297 
00298 
00299 /***************************************************************************/
00310 __STATIC_INLINE void I2C_IntDisable(I2C_TypeDef *i2c, uint32_t flags)
00311 {
00312   i2c->IEN &= ~(flags);
00313 }
00314 
00315 
00316 /***************************************************************************/
00332 __STATIC_INLINE void I2C_IntEnable(I2C_TypeDef *i2c, uint32_t flags)
00333 {
00334   i2c->IEN |= flags;
00335 }
00336 
00337 
00338 /***************************************************************************/
00352 __STATIC_INLINE uint32_t I2C_IntGet(I2C_TypeDef *i2c)
00353 {
00354   return(i2c->IF);
00355 }
00356 
00357 
00358 /***************************************************************************/
00369 __STATIC_INLINE void I2C_IntSet(I2C_TypeDef *i2c, uint32_t flags)
00370 {
00371   i2c->IFS = flags;
00372 }
00373 
00374 void I2C_Reset(I2C_TypeDef *i2c);
00375 
00376 /***************************************************************************/
00393 __STATIC_INLINE uint8_t I2C_SlaveAddressGet(I2C_TypeDef *i2c)
00394 {
00395   return((uint8_t)(i2c->SADDR));
00396 }
00397 
00398 
00399 /***************************************************************************/
00416 __STATIC_INLINE void I2C_SlaveAddressSet(I2C_TypeDef *i2c, uint8_t addr)
00417 {
00418   i2c->SADDR = (uint32_t)addr & 0xfe;
00419 }
00420 
00421 
00422 /***************************************************************************/
00445 __STATIC_INLINE uint8_t I2C_SlaveAddressMaskGet(I2C_TypeDef *i2c)
00446 {
00447   return((uint8_t)(i2c->SADDRMASK));
00448 }
00449 
00450 
00451 /***************************************************************************/
00474 __STATIC_INLINE void I2C_SlaveAddressMaskSet(I2C_TypeDef *i2c, uint8_t mask)
00475 {
00476   i2c->SADDRMASK = (uint32_t)mask & 0xfe;
00477 }
00478 
00479 
00480 I2C_TransferReturn_TypeDef I2C_Transfer(I2C_TypeDef *i2c);
00481 I2C_TransferReturn_TypeDef I2C_TransferInit(I2C_TypeDef *i2c,
00482                                             I2C_TransferSeq_TypeDef *seq);
00483 
00487 #ifdef __cplusplus
00488 }
00489 #endif
00490 
00491 #endif /* defined(I2C_COUNT) && (I2C_COUNT > 0) */
00492 #endif /* __EM_I2C_H */