Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
pca95xx.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
27 #include "peripherals/pca95xx.h"
28 
29 // Init function
30 void pca95xx_init(struct pca95xx *dev, struct i2c_periph *i2c_p, uint8_t addr)
31 {
32  /* set i2c_peripheral */
33  dev->i2c_p = i2c_p;
34 
35  /* slave address */
36  dev->i2c_trans.slave_addr = addr;
37  /* set initial status: Done */
38  dev->i2c_trans.status = I2CTransDone;
39 }
40 
41 // Configure function
43 {
44  if (dev->i2c_trans.status != I2CTransDone &&
45  dev->i2c_trans.status != I2CTransSuccess &&
46  dev->i2c_trans.status != I2CTransFailed) {
47  return false; // previous transaction not finished
48  }
49  // send config value
50  dev->i2c_trans.buf[0] = PCA95XX_CONFIG_REG;
51  dev->i2c_trans.buf[1] = val;
52  if (blocking) {
53  return i2c_blocking_transmit(dev->i2c_p, &dev->i2c_trans, dev->i2c_trans.slave_addr, 2);
54  } else {
55  return i2c_transmit(dev->i2c_p, &dev->i2c_trans, dev->i2c_trans.slave_addr, 2);
56  }
57 }
58 
59 // Set output function
60 bool pca95xx_set_output(struct pca95xx *dev, uint8_t mask, bool blocking)
61 {
62  if (dev->i2c_trans.status != I2CTransDone &&
63  dev->i2c_trans.status != I2CTransSuccess &&
64  dev->i2c_trans.status != I2CTransFailed) {
65  return false; // previous transaction not finished
66  }
67  // send mask value
68  dev->i2c_trans.buf[0] = PCA95XX_OUTPUT_REG;
69  dev->i2c_trans.buf[1] = mask;
70  if (blocking) {
71  return i2c_blocking_transmit(dev->i2c_p, &dev->i2c_trans, dev->i2c_trans.slave_addr, 2);
72  } else {
73  return i2c_transmit(dev->i2c_p, &dev->i2c_trans, dev->i2c_trans.slave_addr, 2);
74  }
75 }
76 
77 // Get input function
78 bool pca95xx_get_input(struct pca95xx *dev, uint8_t mask, uint8_t *result) {
79  if (dev->i2c_trans.status != I2CTransDone &&
80  dev->i2c_trans.status != I2CTransSuccess &&
81  dev->i2c_trans.status != I2CTransFailed) {
82  return false; // previous transaction not finished
83  }
84  // get input register
85  dev->i2c_trans.buf[0] = PCA95XX_INPUT_REG;
86  bool ret = i2c_blocking_transceive(dev->i2c_p, &dev->i2c_trans, dev->i2c_trans.slave_addr, 1, 1);
87  *result = dev->i2c_trans.buf[0] & mask;
88  return ret;
89 }
90 
pca95xx_set_output
bool pca95xx_set_output(struct pca95xx *dev, uint8_t mask, bool blocking)
Set output value.
Definition: pca95xx.c:60
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
pca95xx_configure
bool pca95xx_configure(struct pca95xx *dev, uint8_t val, bool blocking)
Configure PCA95XX.
Definition: pca95xx.c:42
val
uint16_t val[TCOUPLE_NB]
Definition: temp_tcouple_adc.c:49
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
PCA95XX_OUTPUT_REG
#define PCA95XX_OUTPUT_REG
Definition: pca95xx.h:36
i2c_blocking_transceive
bool i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction and wait for it to complete.
Definition: i2c.c:403
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
i2c_transmit
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
pca95xx_init
void pca95xx_init(struct pca95xx *dev, struct i2c_periph *i2c_p, uint8_t addr)
Init PCA95XX.
Definition: pca95xx.c:30
blocking
static const bool blocking[]
Definition: gpio_ext_pca95xx.c:94
PCA95XX_INPUT_REG
#define PCA95XX_INPUT_REG
Definition: pca95xx.h:35
pca95xx.h
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
PCA95XX_CONFIG_REG
#define PCA95XX_CONFIG_REG
Definition: pca95xx.h:38
pca95xx_get_input
bool pca95xx_get_input(struct pca95xx *dev, uint8_t mask, uint8_t *result)
Get input value.
Definition: pca95xx.c:78
i2c_periph
Definition: i2c.h:144
i2c_blocking_transmit
bool i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction and wait for it to complete.
Definition: i2c.c:359
pca95xx
PCA95XX structure.
Definition: pca95xx.h:53