| 260 |
Kevin |
1 |
#include "defines.h"
|
|
|
2 |
#include "MCP23009.h"
|
|
|
3 |
#include "I2C2.h"
|
|
|
4 |
|
| 273 |
Kevin |
5 |
static BTN_STATUS *status_ptr;
|
| 260 |
Kevin |
6 |
|
| 273 |
Kevin |
7 |
void MCP23009_Init(BTN_STATUS *status) {
|
|
|
8 |
status_ptr = status;
|
|
|
9 |
|
| 260 |
Kevin |
10 |
uint8_t buffer[8];
|
|
|
11 |
|
|
|
12 |
buffer[0] = 0x00; // Starting register address
|
| 273 |
Kevin |
13 |
buffer[1] = 0xFF; // IODIR, Set all pins as inputs
|
|
|
14 |
buffer[2] = 0xFF; // IPOL, Reported values are inverted
|
| 275 |
Kevin |
15 |
// buffer[3] = 0xFF; // GPINTEN, Enable interrupt-on-change
|
|
|
16 |
buffer[3] = 0x00; // GPINTEN, Disable interrupt-on-change
|
| 273 |
Kevin |
17 |
buffer[4] = 0x00; // DEFVAL, IOC default values
|
|
|
18 |
buffer[5] = 0x00; // INTCON, IOC compare to previous value
|
|
|
19 |
buffer[6] = 0x00; // IOCON, Incrementing address, active-low interrupt cleared on GPIO read
|
|
|
20 |
buffer[7] = 0xFF; // GPPU, Enable pull-ups on all pins
|
| 260 |
Kevin |
21 |
|
|
|
22 |
I2C2_Master_Send(MCP23009_ADDR, 8, buffer);
|
|
|
23 |
uint8_t result;
|
|
|
24 |
do {
|
|
|
25 |
result = I2C2_Get_Status();
|
|
|
26 |
} while (!result);
|
|
|
27 |
}
|
|
|
28 |
|
| 273 |
Kevin |
29 |
void MCP23009_Query(void) {
|
|
|
30 |
uint8_t buffer[2];
|
| 260 |
Kevin |
31 |
|
| 273 |
Kevin |
32 |
I2C2_Master_Restart(MCP23009_ADDR, MCP23009_GPIOA, 1);
|
| 260 |
Kevin |
33 |
uint8_t result;
|
|
|
34 |
do {
|
|
|
35 |
result = I2C2_Get_Status();
|
|
|
36 |
} while (!result);
|
|
|
37 |
I2C2_Read_Buffer(buffer);
|
|
|
38 |
|
| 273 |
Kevin |
39 |
status_ptr->w = buffer[0];
|
|
|
40 |
}
|