portexpander.c 1.32 KB
Newer Older
1
2
3
4
5
6
7
#include "portexpander.h"

#include "i2c.h"

#include <stdio.h>
#include <stdint.h>
#include <string.h>
8
9
10
#include <stdbool.h>

static bool detected = false;
11
12
13
14

void portexpander_init(void)
{
    uint8_t addr = 0x21;
15
    int ret;
16
17
18

    // Enable pull-ups for buttons
    uint8_t command[] = {0x43, 0x68};
19
20
21
22
23
24
25
26
    ret = I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);

    if(ret != 2) {
        printf("portexpander NOT detected\n");
        detected = false;
        return;
    }
    detected = true;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    // Set _all_ outputs to open-drain to support the high side p-channel transistors.
    command[0] = 0x4F; command[1] = 0x01;
    I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);

    // Enable outputs for transistors and the LED
    command[0] = 0x03; command[1] = 0x68;
    I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);

    // Set outputs to high (i.e. open-drain)
    command[0] = 0x01; command[1] = 0x97;
    I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
}

uint8_t portexpander_get(void)
{
    uint8_t addr = 0x21;
    uint8_t command[] = {0x00};
45
46
47
48
49
50
51
    uint8_t buf = 0xFF;

    if(detected) {
        I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 1, 1);
        I2C_MasterRead(MXC_I2C1_BUS0, addr << 1, &buf, 1, 0);
    }

52
53
54
    return buf;
}

55
56
57
58
bool portexpander_detected(void)
{
    return detected;
}