Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
gpio_ext_pca95xx.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Tom van Dijk <tomvand@users.noreply.github.com>
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 
29 #include "generated/airframe.h"
30 #include "peripherals/pca95xx.h"
31 #include "mcu_periph/i2c.h"
32 
33 #include <stdbool.h>
34 
35 
36 #ifdef GPIO_EXT_PCA95XX_I2C_PERIPH1
37 #define GPIO_EXT_PCA95XX_I2C_PERIPH1_PTR &(GPIO_EXT_PCA95XX_I2C_PERIPH1)
38 #else
39 #define GPIO_EXT_PCA95XX_I2C_PERIPH1_PTR NULL
40 #endif
41 #ifdef GPIO_EXT_PCA95XX_I2C_PERIPH2
42 #define GPIO_EXT_PCA95XX_I2C_PERIPH2_PTR &(GPIO_EXT_PCA95XX_I2C_PERIPH2)
43 #else
44 #define GPIO_EXT_PCA95XX_I2C_PERIPH2_PTR NULL
45 #endif
46 #ifdef GPIO_EXT_PCA95XX_I2C_PERIPH3
47 #define GPIO_EXT_PCA95XX_I2C_PERIPH3_PTR &(GPIO_EXT_PCA95XX_I2C_PERIPH3)
48 #else
49 #define GPIO_EXT_PCA95XX_I2C_PERIPH3_PTR NULL
50 #endif
51 #ifdef GPIO_EXT_PCA95XX_I2C_PERIPH4
52 #define GPIO_EXT_PCA95XX_I2C_PERIPH4_PTR &(GPIO_EXT_PCA95XX_I2C_PERIPH4)
53 #else
54 #define GPIO_EXT_PCA95XX_I2C_PERIPH4_PTR NULL
55 #endif
56 static struct i2c_periph * const i2c_periph[] = {
61 };
62 
63 #ifndef GPIO_EXT_PCA95XX_I2C_ADDRESS1
64 #define GPIO_EXT_PCA95XX_I2C_ADDRESS1 0x00
65 #endif
66 #ifndef GPIO_EXT_PCA95XX_I2C_ADDRESS2
67 #define GPIO_EXT_PCA95XX_I2C_ADDRESS2 0x00
68 #endif
69 #ifndef GPIO_EXT_PCA95XX_I2C_ADDRESS3
70 #define GPIO_EXT_PCA95XX_I2C_ADDRESS3 0x00
71 #endif
72 #ifndef GPIO_EXT_PCA95XX_I2C_ADDRESS4
73 #define GPIO_EXT_PCA95XX_I2C_ADDRESS4 0x00
74 #endif
75 static const uint8_t i2c_addr[] = {
80 };
81 
82 #ifndef GPIO_EXT_PCA95XX_BLOCKING1
83 #define GPIO_EXT_PCA95XX_BLOCKING1 TRUE
84 #endif
85 #ifndef GPIO_EXT_PCA95XX_BLOCKING2
86 #define GPIO_EXT_PCA95XX_BLOCKING2 TRUE
87 #endif
88 #ifndef GPIO_EXT_PCA95XX_BLOCKING3
89 #define GPIO_EXT_PCA95XX_BLOCKING3 TRUE
90 #endif
91 #ifndef GPIO_EXT_PCA95XX_BLOCKING4
92 #define GPIO_EXT_PCA95XX_BLOCKING4 TRUE
93 #endif
94 static const bool blocking[] = {
99 };
100 
101 
102 static void gpio_ext_pca95xx_setup_output(uint32_t port, uint32_t gpios);
103 static void gpio_ext_pca95xx_setup_input(uint32_t port, uint32_t gpios);
104 static uint32_t gpio_ext_pca95xx_get(uint32_t port, uint32_t gpios);
105 static void gpio_ext_pca95xx_set(uint32_t port, uint32_t gpios);
106 static void gpio_ext_pca95xx_clear(uint32_t port, uint32_t gpios);
107 static void gpio_ext_pca95xx_toggle(uint32_t port, uint32_t gpios);
108 
109 
117 };
118 
119 
121  struct pca95xx periph;
125 };
126 static struct gpio_ext_pca95xx_impl_t impl[GPIOEXT_NB]; // Initialized 0, so .initialized = false!
127 
128 
130  int i = port - GPIOEXT1;
131  if (impl[i].initialized) return;
132  /* Set up pca95xx implementation struct */
133  impl[i].output_reg = 0xFF;
134  impl[i].config_reg = 0xFF;
135  /* Set up pca95xx peripheral */
137  /* Configure pins as input (default) and wait for IC to wake up */
138  do {
139  pca95xx_configure(&impl[i].periph, 0xFF, true);
140  } while (impl[i].periph.i2c_trans.status != I2CTransSuccess);
141  /* Mark initialization complete */
142  impl[i].initialized = true;
143 }
144 
145 
148  int i = port - GPIOEXT1;
149  impl[i].config_reg &= ~gpios;
151 }
152 
155  int i = port - GPIOEXT1;
156  impl[i].config_reg |= gpios;
158 }
159 
161  int i = port - GPIOEXT1;
162  uint8_t result;
163  pca95xx_get_input(&impl[i].periph, gpios, &result);
164  return result;
165 }
166 
167 static void gpio_ext_pca95xx_set(uint32_t port, uint32_t gpios) {
168  int i = port - GPIOEXT1;
169  impl[i].output_reg |= gpios;
171 }
172 
173 static void gpio_ext_pca95xx_clear(uint32_t port, uint32_t gpios) {
174  int i = port - GPIOEXT1;
175  impl[i].output_reg &= ~gpios;
177 }
178 
179 static void gpio_ext_pca95xx_toggle(uint32_t port, uint32_t gpios) {
180  int i = port - GPIOEXT1;
181  impl[i].output_reg ^= gpios;
183 }
184 
185 
186 
187 
188 
pca95xx_set_output
bool pca95xx_set_output(struct pca95xx *dev, uint8_t mask, bool blocking)
Set output value.
Definition: pca95xx.c:60
pca95xx_configure
bool pca95xx_configure(struct pca95xx *dev, uint8_t val, bool blocking)
Configure PCA95XX.
Definition: pca95xx.c:42
GPIO_EXT_PCA95XX_I2C_PERIPH3_PTR
#define GPIO_EXT_PCA95XX_I2C_PERIPH3_PTR
Definition: gpio_ext_pca95xx.c:49
i2c_addr
static const uint8_t i2c_addr[]
Definition: gpio_ext_pca95xx.c:75
gpio_ext_pca95xx_lazy_init
static void gpio_ext_pca95xx_lazy_init(uint32_t port)
Definition: gpio_ext_pca95xx.c:129
uint32_t
unsigned long uint32_t
Definition: types.h:18
gpio_ext_pca95xx_setup_input
static void gpio_ext_pca95xx_setup_input(uint32_t port, uint32_t gpios)
Definition: gpio_ext_pca95xx.c:153
gpio_ext_common.h
GPIO_EXT_PCA95XX_I2C_ADDRESS3
#define GPIO_EXT_PCA95XX_I2C_ADDRESS3
Definition: gpio_ext_pca95xx.c:70
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
gpio_ext_pca95xx_clear
static void gpio_ext_pca95xx_clear(uint32_t port, uint32_t gpios)
Definition: gpio_ext_pca95xx.c:173
gpio_ext_pca95xx_impl_t::output_reg
uint8_t output_reg
Definition: gpio_ext_pca95xx.c:122
gpio_ext_pca95xx_setup_output
static void gpio_ext_pca95xx_setup_output(uint32_t port, uint32_t gpios)
Definition: gpio_ext_pca95xx.c:146
uint8_t
unsigned char uint8_t
Definition: types.h:14
pca95xx_init
void pca95xx_init(struct pca95xx *dev, struct i2c_periph *i2c_p, uint8_t addr)
Init PCA95XX.
Definition: pca95xx.c:30
gpio_ext_pca95xx_impl_t
Definition: gpio_ext_pca95xx.c:120
blocking
static const bool blocking[]
Definition: gpio_ext_pca95xx.c:94
gpio_ext_pca95xx_toggle
static void gpio_ext_pca95xx_toggle(uint32_t port, uint32_t gpios)
Definition: gpio_ext_pca95xx.c:179
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
gpio_ext_pca95xx_impl_t::config_reg
uint8_t config_reg
Definition: gpio_ext_pca95xx.c:123
GPIO_EXT_PCA95XX_I2C_PERIPH2_PTR
#define GPIO_EXT_PCA95XX_I2C_PERIPH2_PTR
Definition: gpio_ext_pca95xx.c:44
gpio_ext_pca95xx.h
gpio_ext_pca95xx_get
static uint32_t gpio_ext_pca95xx_get(uint32_t port, uint32_t gpios)
Definition: gpio_ext_pca95xx.c:160
gpio_ext_pca95xx_set
static void gpio_ext_pca95xx_set(uint32_t port, uint32_t gpios)
Definition: gpio_ext_pca95xx.c:167
pca95xx_functions
struct gpio_ext_functions pca95xx_functions
Definition: gpio_ext_pca95xx.c:110
pca95xx.h
GPIO_EXT_PCA95XX_I2C_PERIPH4_PTR
#define GPIO_EXT_PCA95XX_I2C_PERIPH4_PTR
Definition: gpio_ext_pca95xx.c:54
pca95xx::i2c_trans
struct i2c_transaction i2c_trans
Definition: pca95xx.h:55
GPIO_EXT_PCA95XX_I2C_ADDRESS1
#define GPIO_EXT_PCA95XX_I2C_ADDRESS1
Definition: gpio_ext_pca95xx.c:64
GPIO_EXT_PCA95XX_I2C_ADDRESS2
#define GPIO_EXT_PCA95XX_I2C_ADDRESS2
Definition: gpio_ext_pca95xx.c:67
GPIO_EXT_PCA95XX_BLOCKING1
#define GPIO_EXT_PCA95XX_BLOCKING1
Definition: gpio_ext_pca95xx.c:83
gpio_ext_pca95xx_impl_t::periph
struct pca95xx periph
Definition: gpio_ext_pca95xx.c:121
GPIO_EXT_PCA95XX_I2C_ADDRESS4
#define GPIO_EXT_PCA95XX_I2C_ADDRESS4
Definition: gpio_ext_pca95xx.c:73
gpio_ext_functions
Definition: gpio_ext_common.h:34
pca95xx_get_input
bool pca95xx_get_input(struct pca95xx *dev, uint8_t mask, uint8_t *result)
Get input value.
Definition: pca95xx.c:78
GPIO_EXT_PCA95XX_I2C_PERIPH1_PTR
#define GPIO_EXT_PCA95XX_I2C_PERIPH1_PTR
Definition: gpio_ext_pca95xx.c:39
i2c_periph
Definition: i2c.h:144
i2c.h
pca95xx
PCA95XX structure.
Definition: pca95xx.h:53
impl
static struct gpio_ext_pca95xx_impl_t impl[GPIOEXT_NB]
Definition: gpio_ext_pca95xx.c:126
gpio_ext_pca95xx_impl_t::initialized
bool initialized
Definition: gpio_ext_pca95xx.c:124