EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
i2cspm.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include <stddef.h>
17 #include "em_cmu.h"
18 #include "em_gpio.h"
19 #include "i2cspmconfig.h"
20 #include "i2cspm.h"
21 #include "em_assert.h"
22 
23 /*******************************************************************************
24  ************************** GLOBAL FUNCTIONS *******************************
25  ******************************************************************************/
26 
27 /***************************************************************************/
40 {
41  int i;
42  CMU_Clock_TypeDef i2cClock;
43  I2C_Init_TypeDef i2cInit;
44 
45  EFM_ASSERT(init != NULL);
46 
48 
49  /* Select I2C peripheral clock */
50  if (false)
51  {
52 #if defined( I2C0 )
53  }
54  else if (init->port == I2C0)
55  {
56  i2cClock = cmuClock_I2C0;
57 #endif
58 #if defined( I2C1 )
59  }
60  else if (init->port == I2C1)
61  {
62  i2cClock = cmuClock_I2C1;
63 #endif
64  }
65  else
66  {
67  /* I2C clock is not defined */
68  EFM_ASSERT(false);
69  return;
70  }
71  CMU_ClockEnable(i2cClock, true);
72 
73  /* Output value must be set to 1 to not drive lines low. Set
74  SCL first, to ensure it is high before changing SDA. */
77 
78  /* In some situations, after a reset during an I2C transfer, the slave
79  device may be left in an unknown state. Send 9 clock pulses to
80  set slave in a defined state. */
81  for (i = 0; i < 9; i++)
82  {
83  GPIO_PinOutSet(init->sclPort, init->sclPin);
84  GPIO_PinOutClear(init->sclPort, init->sclPin);
85  }
86 
87  /* Enable pins and set location */
88 #if defined (_I2C_ROUTEPEN_MASK)
89  init->port->ROUTEPEN = I2C_ROUTEPEN_SDAPEN | I2C_ROUTEPEN_SCLPEN;
90  init->port->ROUTELOC0 = (init->portLocationSda << _I2C_ROUTELOC0_SDALOC_SHIFT)
91  | (init->portLocationScl << _I2C_ROUTELOC0_SCLLOC_SHIFT);
92 #else
93  init->port->ROUTE = I2C_ROUTE_SDAPEN |
96 #endif
97 
98  /* Set emlib init parameters */
99  i2cInit.enable = true;
100  i2cInit.master = true; /* master mode only */
101  i2cInit.freq = init->i2cMaxFreq;
102  i2cInit.refFreq = init->i2cRefFreq;
103  i2cInit.clhr = init->i2cClhr;
104 
105  I2C_Init(init->port, &i2cInit);
106 }
107 
108 
109 /***************************************************************************/
125 {
127  uint32_t timeout = I2CSPM_TRANSFER_TIMEOUT;
128  /* Do a polled transfer */
129  ret = I2C_TransferInit(i2c, seq);
130  while (ret == i2cTransferInProgress && timeout--)
131  {
132  ret = I2C_Transfer(i2c);
133  }
134  return ret;
135 }
Clock management unit (CMU) API.
#define I2C0
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
Emlib peripheral API "assert" implementation.
GPIO_Port_TypeDef sclPort
Definition: i2cspm.h:54
void I2CSPM_Init(I2CSPM_Init_TypeDef *init)
Initalize I2C peripheral.
Definition: i2cspm.c:39
I2C_TransferReturn_TypeDef I2C_TransferInit(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Prepare and start an I2C transfer (single master mode only).
Definition: em_i2c.c:807
#define _I2C_ROUTE_LOCATION_SHIFT
Definition: efm32hg_i2c.h:684
GPIO_Port_TypeDef sdaPort
Definition: i2cspm.h:56
__IOM uint32_t ROUTE
Definition: efm32hg_i2c.h:57
#define I2C_ROUTE_SDAPEN
Definition: efm32hg_i2c.h:674
I2C_TransferReturn_TypeDef
Definition: em_i2c.h:179
uint8_t sclPin
Definition: i2cspm.h:55
I2C_ClockHLR_TypeDef i2cClhr
Definition: i2cspm.h:66
uint8_t portLocation
Definition: i2cspm.h:62
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
Definition: em_gpio.c:269
I2C_ClockHLR_TypeDef clhr
Definition: em_i2c.h:223
void I2C_Init(I2C_TypeDef *i2c, const I2C_Init_TypeDef *init)
Initialize I2C.
Definition: em_i2c.c:353
General Purpose IO (GPIO) peripheral API.
I2C simple poll-based master mode driver for the DK/STK.
CMU_Clock_TypeDef
Definition: em_cmu.h:257
__STATIC_INLINE void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out register to 1.
Definition: em_gpio.h:856
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Definition: em_cmu.c:1453
I2C_TypeDef * port
Definition: i2cspm.h:53
uint32_t freq
Definition: em_i2c.h:220
uint32_t i2cMaxFreq
Definition: i2cspm.h:65
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
uint32_t refFreq
Definition: em_i2c.h:214
uint8_t sdaPin
Definition: i2cspm.h:57
uint32_t i2cRefFreq
Definition: i2cspm.h:64
__STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out port register to 0.
Definition: em_gpio.h:811
I2C_TransferReturn_TypeDef I2C_Transfer(I2C_TypeDef *i2c)
Continue an initiated I2C transfer (single master mode only).
Definition: em_i2c.c:428
#define I2C_ROUTE_SCLPEN
Definition: efm32hg_i2c.h:679