i2cdrv.c

Go to the documentation of this file.
00001 /***************************************************************************/
00036 #include <stddef.h>
00037 #include "em_cmu.h"
00038 #include "em_gpio.h"
00039 #include "bsp.h"
00040 #include "i2cdrv.h"
00041 
00042 /*******************************************************************************
00043  **************************   GLOBAL FUNCTIONS   *******************************
00044  ******************************************************************************/
00045 
00046 /***************************************************************************/
00058 void I2CDRV_Init(const I2C_Init_TypeDef *init)
00059 {
00060   int i;
00061 
00062   BSP_PeripheralAccess(BSP_I2C, true);
00063   CMU_ClockEnable(cmuClock_HFPER, true);
00064   CMU_ClockEnable(cmuClock_I2C0, true);
00065 
00066   /* Use location 3: SDA - Pin D14, SCL - Pin D15 */
00067   /* Output value must be set to 1 to not drive lines low... We set */
00068   /* SCL first, to ensure it is high before changing SDA. */
00069   GPIO_PinModeSet(gpioPortD, 15, gpioModeWiredAnd, 1);
00070   GPIO_PinModeSet(gpioPortD, 14, gpioModeWiredAnd, 1);
00071 
00072   /* In some situations (after a reset during an I2C transfer), the slave */
00073   /* device may be left in an unknown state. Send 9 clock pulses just in case. */
00074   for (i = 0; i < 9; i++)
00075   {
00076     /*
00077      * TBD: Seems to be clocking at appr 80kHz-120kHz depending on compiler
00078      * optimization when running at 14MHz. A bit high for standard mode devices,
00079      * but DK only has fast mode devices. Need however to add some time
00080      * measurement in order to not be dependable on frequency and code executed.
00081      */
00082     GPIO_PinModeSet(gpioPortD, 15, gpioModeWiredAnd, 0);
00083     GPIO_PinModeSet(gpioPortD, 15, gpioModeWiredAnd, 1);
00084   }
00085 
00086   /* Enable pins at location 3 (which is used on the DK) */
00087   I2C0->ROUTE = I2C_ROUTE_SDAPEN |
00088                 I2C_ROUTE_SCLPEN |
00089                 (3 << _I2C_ROUTE_LOCATION_SHIFT);
00090 
00091   I2C_Init(I2C0, init);
00092 }
00093 
00094 
00095 /***************************************************************************/
00107 I2C_TransferReturn_TypeDef I2CDRV_Transfer(I2C_TransferSeq_TypeDef *seq)
00108 {
00109   I2C_TransferReturn_TypeDef ret;
00110   uint32_t                   timeout = 300000;
00111   /* Do a polled transfer */
00112   ret = I2C_TransferInit(I2C0, seq);
00113   while (ret == i2cTransferInProgress && timeout--)
00114   {
00115     ret = I2C_Transfer(I2C0);
00116   }
00117 
00118   return(ret);
00119 }