diff --git a/lib/card10/card10.c b/lib/card10/card10.c index df141a29db8b59b61403981f6f247293d08ee498..cf377e390903d1526be294145a249c776f64e040 100644 --- a/lib/card10/card10.c +++ b/lib/card10/card10.c @@ -1,6 +1,7 @@ #include "pmic.h" #include "bosch.h" #include "display.h" +#include "portexpander.h" #include "bhy_uc_driver.h" #include "Bosch_PCB_7183_di03_BMI160_BMM150-7183_di03.2.1.11696_170103.h" @@ -92,6 +93,8 @@ void card10_init(void) float sic_array[9] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; bhy_set_sic_matrix(sic_array); } + + portexpander_init(); } static uint32_t ecg_read_reg(uint8_t reg) diff --git a/lib/card10/meson.build b/lib/card10/meson.build index 8e6ce61f03e0853b705fd3e0870e95db3649b97e..9797d7bf7384a3a2b2451bf272f47998e7d01bfb 100644 --- a/lib/card10/meson.build +++ b/lib/card10/meson.build @@ -7,6 +7,7 @@ sources = files( 'card10.c', 'leds.c', 'pmic.c', + 'portexpander.c', ) deps = [ diff --git a/lib/card10/portexpander.c b/lib/card10/portexpander.c new file mode 100644 index 0000000000000000000000000000000000000000..60dbfc612177794040b0e7f5d303de6e3060d844 --- /dev/null +++ b/lib/card10/portexpander.c @@ -0,0 +1,40 @@ +#include "portexpander.h" + +#include "i2c.h" + +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +void portexpander_init(void) +{ + uint8_t addr = 0x21; + + // Enable pull-ups for buttons + uint8_t command[] = {0x43, 0x68}; + I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0); + + // 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}; + I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 1, 1); + uint8_t buf; + I2C_MasterRead(MXC_I2C1_BUS0, addr << 1, &buf, 1, 0); + return buf; +} + + diff --git a/lib/card10/portexpander.h b/lib/card10/portexpander.h new file mode 100644 index 0000000000000000000000000000000000000000..a2532cc36b9214ca4089a18081874bbc0fe33a9e --- /dev/null +++ b/lib/card10/portexpander.h @@ -0,0 +1,9 @@ +#ifndef PORTEXPANDER_H +#define PORTEXPANDER_H + +#include <stdint.h> + +void portexpander_init(void); +uint8_t portexpander_get(void); + +#endif