Firmware SDK
twr_module_x1.c
1 #include <twr_module_x1.h>
2 #include <twr_onewire_ds2484.h>
3 #include <twr_tca9534a.h>
4 #include <twr_log.h>
5 
6 static struct
7 {
8  bool initialized;
9  twr_tca9534a_t tca9534a;
10  twr_ds2484_t ds2484;
11  twr_onewire_t onewire;
12  bool onewire_initialized;
13  int onewire_power_semaphore;
14 
15 } _twr_module_x1 = { .initialized = false };
16 
17 static bool _twr_module_x1_set_slpz(void *ctx, bool state);
18 
20 {
21  if (_twr_module_x1.initialized) {
22  return true;
23  }
24 
25  memset(&_twr_module_x1, 0, sizeof(_twr_module_x1));
26 
27  if (!twr_tca9534a_init(&_twr_module_x1.tca9534a, TWR_I2C_I2C0, 0x3B)) {
28  twr_log_error("X1: Expander init");
29  return false;
30  }
31 
32  if (!twr_tca9534a_write_port(&_twr_module_x1.tca9534a, 0x48)) {
33  twr_log_error("X1: Expander write_port");
34  return false;
35  }
36 
37  if (!twr_tca9534a_set_port_direction(&_twr_module_x1.tca9534a, 0x00)) {
38  twr_log_error("X1: Expander port_direction");
39  return false;
40  }
41 
42  twr_onewire_ds2484_init(&_twr_module_x1.onewire, &_twr_module_x1.ds2484);
43  twr_ds2484_set_slpz_handler(&_twr_module_x1.ds2484, _twr_module_x1_set_slpz, NULL);
44 
45  _twr_module_x1.initialized = true;
46 
47  return true;
48 }
49 
51 {
52  return &_twr_module_x1.onewire;
53 }
54 
55 static bool _twr_module_x1_set_slpz(void *ctx, bool state)
56 {
57  (void) ctx;
58 
59  if (state) {
60  if (!twr_tca9534a_write_port(&_twr_module_x1.tca9534a, 0xc8)) {
61  twr_log_error("X1: set_slzp: Expander write failed");
62  return false;
63  }
64  } else {
65  if (!twr_tca9534a_write_port(&_twr_module_x1.tca9534a, 0x48)) {
66  twr_log_error("X1: set_slzp: Expander write failed");
67  return false;
68  }
69  }
70 
71  return true;
72 }
struct twr_ds2484_t twr_ds2484_t
TMP112 instance.
Definition: twr_ds2484.h:23
@ TWR_I2C_I2C0
I2C channel I2C0.
Definition: twr_i2c.h:18
void void void void void twr_log_error(const char *format,...) __attribute__((format(printf
Log ERROR message (annotated in log as <E>)
bool twr_module_x1_init(void)
Initialize X1 Module.
Definition: twr_module_x1.c:19
twr_onewire_t * twr_module_x1_get_onewire(void)
Initialize and get Instance 1-Wire for channel B.
Definition: twr_module_x1.c:50
struct twr_onewire_t twr_onewire_t
1-Wire instance
Definition: twr_onewire.h:14
void twr_onewire_ds2484_init(twr_onewire_t *onewire, twr_ds2484_t *twr_ds2484)
Initialize 1-Wire.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
Definition: twr_tca9534a.c:35
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
Definition: twr_tca9534a.c:8
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
Definition: twr_tca9534a.c:87
Pin state.
Definition: twr_tca9534a.h:43