i2cdrv.c

Go to the documentation of this file.
00001 /***************************************************************************/
00018 #include <stddef.h>
00019 #include "em_cmu.h"
00020 #include "em_gpio.h"
00021 #include "bsp.h"
00022 #include "i2cdrv.h"
00023 #include "i2cdrvconfig.h"
00024 
00025 /*******************************************************************************
00026  **************************   GLOBAL FUNCTIONS   *******************************
00027  ******************************************************************************/
00028 
00029 /***************************************************************************/
00041 void I2CDRV_Init(const I2C_Init_TypeDef *init)
00042 {
00043   int i;
00044 
00045 #ifndef BSP_STK
00046   BSP_PeripheralAccess(BSP_I2C, true);
00047 #endif
00048   CMU_ClockEnable(cmuClock_HFPER, true);
00049   CMU_ClockEnable(cmuClock_I2C0, true);
00050 
00051   /* Output value must be set to 1 to not drive lines low. Set */
00052   /* SCL first, to ensure it is high before changing SDA. */
00053   GPIO_PinModeSet(I2CDRV_SCL_PORT, I2CDRV_SCL_PIN, gpioModeWiredAndPullUp, 1);
00054   GPIO_PinModeSet(I2CDRV_SDA_PORT, I2CDRV_SDA_PIN, gpioModeWiredAndPullUp, 1);
00055 
00056   /* In some situations (after a reset during an I2C transfer), the slave */
00057   /* device may be left in an unknown state. Send 9 clock pulses just in case. */
00058   for (i = 0; i < 9; i++)
00059   {
00060     /*
00061      * TBD: Seems to be clocking at appr 80kHz-120kHz depending on compiler
00062      * optimization when running at 14MHz. A bit high for standard mode devices,
00063      * but DK only has fast mode devices. Need however to add some time
00064      * measurement in order to not be dependable on frequency and code executed.
00065      */
00066     GPIO_PinOutSet(I2CDRV_SCL_PORT, I2CDRV_SCL_PIN);
00067     GPIO_PinOutClear(I2CDRV_SCL_PORT, I2CDRV_SCL_PIN);
00068   }
00069 
00070   /* Enable pins at config location (3 is default which is the location used on the DK) */
00071   I2C0->ROUTE = I2C_ROUTE_SDAPEN |
00072                 I2C_ROUTE_SCLPEN |
00073                 (I2CDRV_PORT_LOCATION << _I2C_ROUTE_LOCATION_SHIFT);
00074 
00075   I2C_Init(I2C0, init);
00076 }
00077 
00078 
00079 /***************************************************************************/
00091 I2C_TransferReturn_TypeDef I2CDRV_Transfer(I2C_TransferSeq_TypeDef *seq)
00092 {
00093   I2C_TransferReturn_TypeDef ret;
00094   uint32_t                   timeout = I2CDRV_TRANSFER_TIMEOUT;
00095   /* Do a polled transfer */
00096   ret = I2C_TransferInit(I2C0, seq);
00097   while (ret == i2cTransferInProgress && timeout--)
00098   {
00099     ret = I2C_Transfer(I2C0);
00100   }
00101 
00102   return(ret);
00103 }