EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
bsp_stk_ioexp.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include "em_device.h"
17 #include "em_cmu.h"
18 #include "em_gpio.h"
19 #include "bsp.h"
20 #include "bsp_stk_ioexp.h"
21 #include "i2cspm.h"
22 
25 /***************************************************************************/
32 static inline void ioexpSleep(void)
33 {
34  GPIO_PinOutSet(BSP_IOEXP_WAKE_PORT, BSP_IOEXP_WAKE_PIN);
35  return;
36 }
37 
38 /***************************************************************************/
45 static inline void ioexpWake(void)
46 {
47  GPIO_PinOutClear(BSP_IOEXP_WAKE_PORT, BSP_IOEXP_WAKE_PIN);
48  return;
49 }
50 
51 /***************************************************************************/
64 static int i2cReadReg(uint8_t addr, uint8_t *value)
65 {
68  int status;
69 
70  seq.addr = 0x90;
72  seq.buf[0].len = 1;
73  seq.buf[0].data = &addr;
74  seq.buf[1].len = 1;
75  seq.buf[1].data = value;
76 
77  ret = I2CSPM_Transfer(BSP_IOEXP_I2C_DEVICE, &seq);
78 
79  if (ret == i2cTransferDone)
80  {
81  status = BSP_STATUS_OK;
82  }
83  else
84  {
85  status = BSP_STATUS_IOEXP_FAILURE;
86  }
87 
88  return status;
89 }
90 
91 /***************************************************************************/
104 static int i2cWriteReg(uint8_t addr, uint8_t value)
105 {
108  int status;
109 
110  seq.addr = 0x90;
112  seq.buf[0].len = 1;
113  seq.buf[0].data = &addr;
114  seq.buf[1].len = 1;
115  seq.buf[1].data = &value;
116 
117  ret = I2CSPM_Transfer(BSP_IOEXP_I2C_DEVICE, &seq);
118 
119  if (ret == i2cTransferDone)
120  {
121  status = BSP_STATUS_OK;
122  }
123  else
124  {
125  status = BSP_STATUS_IOEXP_FAILURE;
126  }
127 
128  return status;
129 }
130 
131 /***************************************************************************/
138 uint32_t ioexpGetDeviceId(void)
139 {
140  uint32_t result;
141  uint8_t *pU8;
142 
143  pU8 = (uint8_t*)&result;
144 
145  ioexpReadReg(BSP_IOEXP_REG_DEVICE_ID0, pU8++);
146  ioexpReadReg(BSP_IOEXP_REG_DEVICE_ID1, pU8++);
147  ioexpReadReg(BSP_IOEXP_REG_DEVICE_ID2, pU8++);
148  ioexpReadReg(BSP_IOEXP_REG_DEVICE_ID3, pU8++);
149 
150  return result;
151 }
152 
153 /***************************************************************************/
166 int ioexpReadReg(uint8_t reg, uint8_t *result)
167 {
168  uint32_t status;
169 
170  ioexpWake();
171  status = i2cReadReg(reg, result);
172  ioexpSleep();
173 
174  return status;
175 }
176 /***************************************************************************/
192 int ioexpRegBitsSet(uint8_t addr, bool set, uint8_t bitMask)
193 {
194  uint32_t status;
195  uint8_t value;
196 
197  ioexpWake();
198 
199  status = i2cReadReg(addr, &value);
200  if (status != BSP_STATUS_OK)
201  {
202  goto cleanup;
203  }
204 
205  if (set)
206  {
207  value |= bitMask;
208  }
209  else
210  {
211  value &= ~bitMask;
212  }
213 
214  status = i2cWriteReg(addr, value);
215 
216 cleanup:
217  ioexpSleep();
218  return status;
219 }
220 
221 /***************************************************************************/
234 int ioexpWriteReg(uint8_t reg, uint8_t value)
235 {
236  uint32_t status;
237 
238  ioexpWake();
239  status = i2cWriteReg(reg, value);
240  ioexpSleep();
241 
242  return status;
243 }
244 
245 /***************************************************************************/
249 void ioexpDisable(void)
250 {
251  /* Reset IO-expander registers */
252  ioexpWake();
253  i2cWriteReg(BSP_IOEXP_REG_VCOM_CTRL, 1);
254  i2cWriteReg(BSP_IOEXP_REG_DISP_CTRL, 0);
255  i2cWriteReg(BSP_IOEXP_REG_SENSOR_CTRL, 0);
256  i2cWriteReg(BSP_IOEXP_REG_LED_CTRL, 0);
257  ioexpSleep();
258 }
259 
260 /***************************************************************************/
264 int ioexpEnable(void)
265 {
267 
268  /* Enable GPIO clock */
270 
271  /* Setup GPIO pin used for waking up the io-expander */
272  GPIO_PinModeSet(BSP_IOEXP_WAKE_PORT, BSP_IOEXP_WAKE_PIN,
273  gpioModeWiredAnd, 1);
274 
275  /* Initialize the I2C Simple Polled Mode driver */
276  I2CSPM_Init(&i2cInit);
277 
278  /* Check that the device is responding */
279  if (ioexpGetDeviceId() != BSP_IOEXP_DEVICE_ID)
280  {
282  }
283 
284  /* Set IOEXP default register content */
285  ioexpWake();
286  i2cWriteReg(BSP_IOEXP_REG_VCOM_CTRL, 1);
287  i2cWriteReg(BSP_IOEXP_REG_DISP_CTRL, 0);
288  i2cWriteReg(BSP_IOEXP_REG_SENSOR_CTRL, 0);
289  i2cWriteReg(BSP_IOEXP_REG_LED_CTRL, 0);
290  ioexpSleep();
291 
292  return BSP_STATUS_OK;
293 }
294 
Clock management unit (CMU) API.
Board support package API definitions.
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
Board support package API implementation for STK's.
void I2CSPM_Init(I2CSPM_Init_TypeDef *init)
Initalize I2C peripheral.
Definition: i2cspm.c:39
#define BSP_STATUS_IOEXP_FAILURE
Definition: bsp.h:49
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
I2C_TransferReturn_TypeDef
Definition: em_i2c.h:179
#define BSP_STATUS_OK
Definition: bsp.h:45
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
General Purpose IO (GPIO) peripheral API.
I2C simple poll-based master mode driver for the DK/STK.
#define I2CSPM_INIT_DEFAULT
Definition: i2cspm.h:73
__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
struct I2C_TransferSeq_TypeDef::@0 buf[2]
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Definition: em_cmu.c:1453
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
#define I2C_FLAG_WRITE_WRITE
Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
Definition: em_i2c.h:159
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
Definition: em_i2c.h:148
__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
uint16_t addr
Address to use after (repeated) start.
Definition: em_i2c.h:262