1 #include <twr_module_sensor.h>
2 #include <twr_tca9534a.h>
3 #include <twr_onewire_gpio.h>
7 #define _TWR_MODULE_SENSOR_INITIALIZED_STATE 0xff
8 #define _TWR_MODULE_SENSOR_INITIALIZED_DIRECTION 0x00
9 #define _TWR_MODULE_SENSOR_VDD (1 << 3)
10 #define _TWR_MODULE_SENSOR_CH_A_56R (1 << 4)
11 #define _TWR_MODULE_SENSOR_CH_A_4K7 (1 << 5)
12 #define _TWR_MODULE_SENSOR_CH_B_4K7 (1 << 6)
13 #define _TWR_MODULE_SENSOR_CH_B_56R (1 << 7)
20 int onewire_power_semaphore;
21 bool onewire_initialized;
24 } _twr_module_sensor = {
26 .onewire_initialized =
false
38 if (!_twr_module_sensor.initialized)
59 _twr_module_sensor.initialized =
true;
67 _twr_module_sensor.initialized =
false;
75 port_actual = _twr_module_sensor.tca9534a._output_port;
78 port_new = port_actual;
84 port_new |= _TWR_MODULE_SENSOR_CH_A_4K7 | _TWR_MODULE_SENSOR_CH_A_56R;
89 port_new |= _TWR_MODULE_SENSOR_CH_B_4K7 | _TWR_MODULE_SENSOR_CH_B_56R;
115 port_new &= ~_TWR_MODULE_SENSOR_CH_A_4K7;
120 port_new &= ~_TWR_MODULE_SENSOR_CH_B_4K7;
137 port_new &= ~_TWR_MODULE_SENSOR_CH_A_56R;
142 port_new &= ~_TWR_MODULE_SENSOR_CH_B_56R;
169 if (port_actual != port_new)
182 uint8_t port_actual = _twr_module_sensor.tca9534a._output_port;
184 if ((port_actual & _TWR_MODULE_SENSOR_CH_A_4K7) == 0)
189 if ((port_actual & _TWR_MODULE_SENSOR_CH_A_56R) == 0)
196 uint8_t port_actual = _twr_module_sensor.tca9534a._output_port;
198 if ((port_actual & _TWR_MODULE_SENSOR_CH_B_4K7) == 0)
203 if ((port_actual & _TWR_MODULE_SENSOR_CH_B_56R) == 0)
247 port_actual = _twr_module_sensor.tca9534a._output_port;
251 port_new = port_actual & ~_TWR_MODULE_SENSOR_VDD;
255 port_new = port_actual | _TWR_MODULE_SENSOR_VDD;
258 if (port_actual != port_new)
268 if (!_twr_module_sensor.initialized)
280 uint8_t test_vector = 0x86;
284 for (
size_t i = 0; i <
sizeof(test_vector); i++)
286 value_output = (test_vector >> i) & 0x01;
298 if (value_output != value_input)
302 return _twr_module_sensor.revision;
309 return _twr_module_sensor.revision;
314 if (!_twr_module_sensor.onewire_initialized)
318 _twr_module_sensor.onewire_initialized =
true;
321 return &_twr_module_sensor.onewire;
326 if (++_twr_module_sensor.onewire_power_semaphore == 1)
365 if (_twr_module_sensor.onewire_power_semaphore < 1) twr_error(TWR_ERROR_ERROR_UNLOCK);
367 if (--_twr_module_sensor.onewire_power_semaphore == 0)
twr_gpio_pull_t twr_gpio_get_pull(twr_gpio_channel_t channel)
Get pull-up/pull-down configuration for GPIO channel.
void twr_gpio_set_output(twr_gpio_channel_t channel, int state)
Set output state for GPIO channel.
void twr_gpio_set_pull(twr_gpio_channel_t channel, twr_gpio_pull_t pull)
Set pull-up/pull-down configuration for GPIO channel.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
twr_gpio_mode_t
GPIO mode of operation.
twr_gpio_channel_t
GPIO channels.
void twr_gpio_toggle_output(twr_gpio_channel_t channel)
Toggle output state for GPIO channel.
int twr_gpio_get_output(twr_gpio_channel_t channel)
Get output state for GPIO channel.
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
@ TWR_GPIO_MODE_ANALOG
GPIO channel operates in analog mode.
@ TWR_GPIO_P5
GPIO channel P5, A5, DAC1.
@ TWR_GPIO_P4
GPIO channel P4, A4, DAC0.
@ TWR_GPIO_P7
GPIO channel P7, A6.
@ TWR_GPIO_PULL_NONE
GPIO channel has no pull-up/pull-down.
@ TWR_GPIO_PULL_DOWN
GPIO channel has pull-down.
@ TWR_GPIO_PULL_UP
GPIO channel has pull-up.
@ TWR_I2C_I2C0
I2C channel I2C0.
twr_module_sensor_revision_t twr_module_sensor_get_revision(void)
Get Sensor Module revision.
void twr_module_sensor_set_mode(twr_module_sensor_channel_t channel, twr_module_sensor_mode_t mode)
Set output mode of Sensor Module channel.
int twr_module_sensor_get_output(twr_module_sensor_channel_t channel)
Get output state for Sensor Module channel.
void twr_module_sensor_toggle_output(twr_module_sensor_channel_t channel)
Toggle output state for Sensor Module channel.
twr_module_sensor_revision_t
Sensor Module hardware revision.
int twr_module_sensor_get_input(twr_module_sensor_channel_t channel)
Get input of Sensor Module channel.
twr_module_sensor_pull_t twr_module_sensor_get_pull(twr_module_sensor_channel_t channel)
Get pull-up/pull-down configuration for Sensor Module channel.
twr_onewire_t * twr_module_sensor_get_onewire(void)
Initialize and get Instance 1-Wire for channel B.
void twr_module_sensor_set_output(twr_module_sensor_channel_t channel, int state)
Set output state of Sensor Module channel.
bool twr_module_sensor_onewire_power_down(void)
Semaphore for 1Wire Power down: for R1.1 set VDD Off, for R1.0 pull none on channel A.
bool twr_module_sensor_onewire_power_up(void)
Semaphore for 1Wire Power up: for R1.1 set VDD On, for R1.0 pull up 56R on channel A.
bool twr_module_sensor_set_vdd(bool on)
Set VDD On / Off.
bool twr_module_sensor_init(void)
Initialize Sensor Module.
twr_module_sensor_mode_t
Sensor Module mode of operation.
twr_module_sensor_pull_t
Sensor module pull.
bool twr_module_sensor_set_pull(twr_module_sensor_channel_t channel, twr_module_sensor_pull_t pull)
Set pull of Sensor Module channel.
twr_module_sensor_channel_t
Sensor Module channels.
void twr_module_sensor_deinit(void)
Deinitialize Sensor Module.
@ TWR_MODULE_SENSOR_REVISION_UNKNOWN
Hardware revision unknown.
@ TWR_MODULE_SENSOR_REVISION_R1_0
Hardware revision R1.0.
@ TWR_MODULE_SENSOR_REVISION_R1_1
Hardware revision R1.1.
@ TWR_MODULE_SENSOR_PULL_UP_4K7
Channel has pull-up 4k7.
@ TWR_MODULE_SENSOR_PULL_DOWN_INTERNAL
Channel has internal pull-down.
@ TWR_MODULE_SENSOR_PULL_UP_INTERNAL
Channel has internal pull-up.
@ TWR_MODULE_SENSOR_PULL_NONE
Channel has no pull.
@ TWR_MODULE_SENSOR_PULL_UP_56R
Channel has pull-up 56R.
@ TWR_MODULE_SENSOR_CHANNEL_B
Channel B.
@ TWR_MODULE_SENSOR_CHANNEL_A
Channel A.
@ TWR_MODULE_SENSOR_CHANNEL_C
Channel C.
struct twr_onewire_t twr_onewire_t
1-Wire instance
void twr_onewire_gpio_init(twr_onewire_t *onewire, twr_gpio_channel_t channel)
Initialize 1-Wire.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
bool twr_tca9534a_read_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int *state)
Read pin state.
bool twr_tca9534a_set_pin_direction(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, twr_tca9534a_pin_direction_t direction)
Set pin direction.
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
bool twr_tca9534a_write_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int state)
Write pin state.