EVE 1.0
io-expander-pca9535a.h
Go to the documentation of this file.
1 #ifndef DRIVER_IO_EXPANDER_PCA9535A_H
2 #define DRIVER_IO_EXPANDER_PCA9535A_H
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2014, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief Driver for I2C-based IO expander type NXP PCA9535A.
36  *
37  * @author KLO, Jetro as
38  */ /******************************************************************/
39 
40 #include <stdbool.h>
41 #include <string.h>
42 #include <em_device.h>
43 
44 /**
45  * This driver supports the I2C-based IO expander type NXP PCA9535A.
46  * The IO expander has 2 8-bits IO ports, 1 open drain interrupt output and
47  * 3 address lines.
48  * The I2C address range is 0x20 - 0x27. Max baudrate is 400 kbit/s.
49  * The port bits could be set to inputs or outputs individually, and
50  * there are no pullups when used as inputs.
51  *
52  * Both initialization, writing to one or both ports and reading from
53  * one or both ports are supported.
54  *
55  * The selected I2C port must be initialized before using the driver.
56  */
57 
58 /***********************************************************************
59  * Global defines
60 ***********************************************************************/
61 /**
62  * io_expander_t structure holds I2C port and slave address to be used,
63  * and the initial data for PORT0 and PORT1 for the IO expander.
64  *
65  */
66 
68 {
69  const struct i2c_t *I2c; // I2C port I2C0 or I2C1
70  uint8_t SlaveAddress; // I2C slave address 0x20 to 0x27
71  uint8_t Port0Init; // Initializes PORT0 IO pins (0 = logic low, 1 = logic high)
72  uint8_t Port1Init; // Initializes PORT1 IO pins (0 = logic low, 1 = logic high)
73  uint8_t Port0Invert; // Configures invertion of PORT0 input pins (0 = non-inverted, 1 = inverted)
74  uint8_t Port1Invert; // Configures invertion of PORT1 input pins (0 = non-inverted, 1 = inverted)
75  uint8_t Port0Config; // Configures the direction of PORT0 IO pins (0 = output, 1 = input)
76  uint8_t Port1Config; // Configures the direction of PORT1 IO pins (0 = output, 1 = input)
77 };
78 
79 /**
80  * @name Functions called from application programs
81  * @{
82  */
83 
84 /**********************************************************************/
85 /**
86  * @brief Name: IoExpPortInit\n
87  * Initializes I2C-based IO expander type NXP PCA9535A.
88  * I2C address range is 0x20 - 0x27. Max baudrate is 400 kbit/s.
89  *
90  * @param IoExpander Parameters for the IO port expander to be used
91  * @return true if OK, else false
92  ***********************************************************************/
93 extern bool IoExpPortInit(const struct io_expander_t *IoExpander);
94 
95 /**********************************************************************/
96 /**
97  * @brief Name: IoExpPortWrite\n
98  * Write to IO expander PORT0 and PORT1.
99  *
100  * @param IoExpander Parameters for the IO port expander to be used
101  * @param Port0 Data to be written to PORT0
102  * @param Port1 Data to be written to PORT1
103  * @return true if OK, else false
104  ***********************************************************************/
105 extern bool IoExpPortWrite(const struct io_expander_t *IoExpander, const uint8_t Port0, const uint8_t Port1);
106 
107 /**********************************************************************/
108 /**
109  * @brief Name: IoExpPort0Write\n
110  * Write to IO expander PORT0.
111  *
112  * @param IoExpander Parameters for the IO port expander to be used
113  * @param Port0 Data to be written to PORT0
114  * @return true if OK, else false
115  ***********************************************************************/
116 extern bool IoExpPort0Write(const struct io_expander_t *IoExpander, const uint8_t Port0);
117 
118 /**********************************************************************/
119 /**
120  * @brief Name: IoExpPort1Write\n
121  * Write to IO expander PORT1.
122  *
123  * @param IoExpander Parameters for the IO port expander to be used
124  * @param Port1 Data to be written to PORT1
125  * @return true if OK, else false
126  ***********************************************************************/
127 extern bool IoExpPort1Write(const struct io_expander_t *IoExpander, const uint8_t Port1);
128 
129 /**********************************************************************/
130 /**
131  * @brief Name: IoExpPortRead\n
132  * Read IO expander PORT0 and PORT1.
133  *
134  * @param IoExpander Parameters for the IO port expander to be used
135  * @param Port0 Pointer to put data read from IO expander PORT0
136  * @param Port1 Pointer to put data read from IO expander PORT1
137  * @return true if OK, else false
138  ***********************************************************************/
139 extern bool IoExpPortRead(const struct io_expander_t *IoExpander, uint8_t *Port0, uint8_t *Port1);
140 
141 /**********************************************************************/
142 /**
143  * @brief Name: IoExpPort0Read\n
144  * Read IO expander PORT0.
145  *
146  * @param IoExpander Parameters for the IO port expander to be used
147  * @param Port0 Pointer to put data read from IO expander PORT0
148  * @return true if OK, else false
149  ***********************************************************************/
150 static inline bool IoExpPort0Read(const struct io_expander_t *IoExpander, uint8_t *Port0)
151 {
152  return IoExpPortRead(IoExpander, Port0, NULL);
153 }
154 
155 /**********************************************************************/
156 /**
157  * @brief Name: IoExpPort1Read\n
158  * Read IO expander PORT1.
159  *
160  * @param IoExpander Parameters for the IO port expander to be used
161  * @param Port1 Pointer to put data read from IO expander PORT1
162  * @return true if OK, else false
163  ***********************************************************************/
164 static inline bool IoExpPort1Read(const struct io_expander_t *IoExpander, uint8_t *Port1)
165 {
166  return IoExpPortRead(IoExpander, NULL, Port1);
167 }
168 
169 /** @} */
170 #endif //DRIVER_IO_EXPANDER_PCA9535A_H
bool IoExpPortWrite(const struct io_expander_t *IoExpander, const uint8_t Port0, const uint8_t Port1)
Name: IoExpPortWrite Write to IO expander PORT0 and PORT1.
uint8_t SlaveAddress
I2C slave address 0x20 to 0x27.
bool IoExpPortRead(const struct io_expander_t *IoExpander, uint8_t *Port0, uint8_t *Port1)
Name: IoExpPortRead Read IO expander PORT0 and PORT1.
const struct i2c_t * I2c
I2C port I2C0 or I2C1.
bool IoExpPortInit(const struct io_expander_t *IoExpander)
Name: IoExpPortInit Initializes I2C-based IO expander type NXP PCA9535A. I2C address range is 0x20 - ...
bool IoExpPort0Write(const struct io_expander_t *IoExpander, const uint8_t Port0)
Name: IoExpPort0Write Write to IO expander PORT0.
static bool IoExpPort1Read(const struct io_expander_t *IoExpander, uint8_t *Port1)
Name: IoExpPort1Read Read IO expander PORT1.
bool IoExpPort1Write(const struct io_expander_t *IoExpander, const uint8_t Port1)
Name: IoExpPort1Write Write to IO expander PORT1.
static bool IoExpPort0Read(const struct io_expander_t *IoExpander, uint8_t *Port0)
Name: IoExpPort0Read Read IO expander PORT0.