GPIO_NXP_Arduino 0.1.0
GPIO device operation sample code for Arduino
Loading...
Searching...
No Matches
GPIO_NXP.cpp
1#include "GPIO_NXP.h"
2
3/* ******** GPIO_base ******** */
4
5GPIO_base::GPIO_base( uint8_t i2c_address, const int nbits, const uint8_t* ar, uint8_t ai ) :
6 I2C_device( i2c_address ),
7 n_bits( nbits ),
8 n_ports( (nbits + 7) / 8 ),
9 arp( ar ),
10 auto_increment( ai )
11{
12 constexpr uint16_t i = 0x0001;
13 uint8_t* tp = (uint8_t*)(&i);
14
15 endian = tp[ 0 ]; // 'true' if LittleEndian
16}
17
19{
20}
21
22void GPIO_base::begin( board env )
23{
24 if ( env ) {
25 // NEED INVESTIGATION LATER
26 // NO ENOUGH CURRENT ON PIN8 OUTPUT?
27 // BECAUSE IT CANNOT ASSERT LOW (LITTLE LOWER VOLTAGE)
28 // SAME THING HAPPENS ON OTHER PCAL6XXX-ARD BOARDS
29 // THE SIGNAL CAN BE LOW WHEN THE ARD BOARD IS NOT CONNECTED
30 // INSTEAD OF USING THIS MECHANISM, USE "I2C_device::scan()" TO CONFIRM THE TARGET ADDRESS
31
32#if 1
33 digitalWrite( ADDR_PIN , 0 );
34#else
35 digitalWrite( ADDR_PIN , 0 );
36 delay( 1 );
37
38 digitalWrite( RESET_PIN , 0 );
39 delay( 1 ); // reset time = 500ns(min)
40 digitalWrite( RESET_PIN , 1 );
41 delay( 1 ); // reset recovery time = 600ns(min)
42#endif
43 }
44}
45
46void GPIO_base::output( int port, uint8_t value, uint8_t mask )
47{
48 if ( mask )
49 bit_op8( *(arp + OUT) + port, mask, value );
50
51 write_r8( *(arp + OUT) + port, value );
52}
53
54void GPIO_base::output( uint8_t *vp )
55{
56 write_port( OUT, vp );
57}
58
59uint8_t GPIO_base::input( int port )
60{
61 return read_r8( *(arp + IN) + port );
62}
63
64uint8_t* GPIO_base::input( uint8_t *vp )
65{
66 read_port( IN, vp );
67
68 return vp;
69}
70
71void GPIO_base::config( int port, uint8_t config, uint8_t mask )
72{
73 if ( mask )
74 bit_op8( *(arp + CONFIG) + port, mask, config );
75
76 write_r8( *(arp + CONFIG) + port, config );
77}
78
79void GPIO_base::config( uint8_t* vp )
80{
81 write_port( CONFIG, vp );
82}
83
84void GPIO_base::write_port( access_word w, uint8_t* vp )
85{
86 if ( auto_increment ) {
87 reg_w( auto_increment | *(arp + w), vp, n_ports );
88 }
89 else {
90 for ( int i = 0; i < n_ports; i++ )
91 write_r8( *(arp + w) + i, *vp++ );
92 }
93}
94
95void GPIO_base::write_port16( access_word w, uint16_t* vp )
96{
97 if ( endian ) {
98 uint16_t temp;
99 for ( int i = 0; i < n_ports; i++ ) {
100 temp = vp[ i ] << 8;
101 vp[ i ] = temp | vp[ i ] >> 8;
102 }
103 }
104
105 int n_bytes = (n_bits * 2 + 7) / 8;
106
107 if ( auto_increment ) {
108 reg_w( auto_increment | *(arp + w), (uint8_t*)vp, n_bytes );
109 }
110 else {
111 for ( int i = 0; i < n_bytes; i++ )
112 write_r8( *(arp + w) + i, *vp++ );
113 }
114}
115
116uint8_t* GPIO_base::read_port( access_word w, uint8_t* vp )
117{
118 if ( auto_increment ) {
119 reg_r( auto_increment | *(arp + w), vp, n_ports );
120 }
121 else {
122 for ( int i = 0; i < n_ports; i++ )
123 *(vp + i) = read_r8( *(arp + w) + i );
124 }
125
126 return vp;
127}
128
129uint16_t* GPIO_base::read_port16( access_word w, uint16_t* vp )
130{
131 int n_bytes = (n_bits * 2 + 7) / 8;
132
133 if ( auto_increment ) {
134 reg_r( auto_increment | *(arp + w), (uint8_t*)vp, n_bytes );
135 }
136 else {
137 for ( int i = 0; i < n_bytes; i++ )
138 *(vp + i) = read_r8( *(arp + w) + i );
139 }
140
141
142 if ( endian ) {
143 uint16_t temp;
144
145 for ( int i = 0; i < n_ports; i++ ) {
146 temp = vp[ i ] << 8;
147 vp[ i ] = temp | vp[ i ] >> 8;
148 }
149 }
150
151 return vp;
152}
153
154void GPIO_base::write_port( access_word w, uint8_t value, int port_num )
155{
156 write_r8( *(arp + w) + port_num, value );
157}
158
159void GPIO_base::write_port16( access_word w, uint16_t value, int port_num )
160{
161 write_r16( *(arp + w) + port_num, value );
162}
163
164uint8_t GPIO_base::read_port( access_word w, int port_num )
165{
166 return read_r8( *(arp + w) + port_num );
167}
168
169uint16_t GPIO_base::read_port16( access_word w, int port_num )
170{
171 return read_r16( *(arp + w) + port_num );
172}
173
174void GPIO_base::print_bin( uint8_t v )
175{
176 Serial.print(" 0b");
177 for (int i = 7; 0 <= i; i-- )
178 Serial.print(((v >> i) & 0x1) ? "1" : "0");
179}
180
181
182/* ******** PCAL6xxx_base ******** */
183
184PCAL6xxx_base::PCAL6xxx_base( uint8_t i2c_address, const int nbits, const uint8_t arp[], uint8_t ai ) :
185 GPIO_base( i2c_address, nbits, arp, ai )
186{
187}
188
189PCAL6xxx_base::~PCAL6xxx_base()
190{
191}
192
193
194/* ******** PCAL6408A ******** */
195
196PCAL6408A::PCAL6408A( uint8_t i2c_address ) :
197 PCAL6xxx_base( i2c_address, 8, access_ref, 0 )
198{
199}
200
201PCAL6408A::~PCAL6408A()
202{
203}
204
205constexpr uint8_t PCAL6408A::access_ref[];
206
207
208/* ******** PCAL6416A ******** */
209
210PCAL6416A::PCAL6416A( uint8_t i2c_address ) :
211 PCAL6xxx_base( i2c_address, 16, access_ref, 0 )
212{
213}
214
215PCAL6416A::~PCAL6416A()
216{
217}
218
219constexpr uint8_t PCAL6416A::access_ref[];
220
221
222/* ******** PCAL6524 ******** */
223
224PCAL6524::PCAL6524( uint8_t i2c_address ) :
225 PCAL6xxx_base( i2c_address, 24, access_ref, 0x80 )
226{
227}
228
229PCAL6524::~PCAL6524()
230{
231}
232
233constexpr uint8_t PCAL6524::access_ref[];
234
235
236/* ******** PCAL6534 ******** */
237
238PCAL6534::PCAL6534( uint8_t i2c_address ) :
239 PCAL6xxx_base( i2c_address, 34, access_ref, 0x80 )
240{
241}
242
243PCAL6534::~PCAL6534()
244{
245}
246
247constexpr uint8_t PCAL6534::access_ref[];
GPIO_base(uint8_t i2c_address, const int nbits, const uint8_t *arp, uint8_t ai)
Definition: GPIO_NXP.cpp:5
uint16_t * read_port16(access_word w, uint16_t *vp)
Definition: GPIO_NXP.cpp:129
const int n_bits
Definition: GPIO_NXP.h:53
void config(int port, uint8_t config, uint8_t mask=0)
Definition: GPIO_NXP.cpp:71
const int n_ports
Definition: GPIO_NXP.h:56
virtual ~GPIO_base()
Definition: GPIO_NXP.cpp:18
void write_port16(access_word w, uint16_t *vp)
Definition: GPIO_NXP.cpp:95
void output(int port, uint8_t value, uint8_t mask=0)
Definition: GPIO_NXP.cpp:46
void write_port(access_word w, uint8_t *vp)
Definition: GPIO_NXP.cpp:84
void begin(board env=NONE)
Definition: GPIO_NXP.cpp:22
uint8_t input(int port)
Definition: GPIO_NXP.cpp:59
uint8_t * read_port(access_word w, uint8_t *vp)
Definition: GPIO_NXP.cpp:116