1 #include <twr_zssc3123.h>
6 static void _twr_zssc3123_task(
void *param);
10 memset(
self, 0,
sizeof(*
self));
12 self->_i2c_channel = i2c_channel;
13 self->_i2c_address = i2c_address;
21 self->_data_fetch_delay = 100;
39 self->_data_fetch_delay = interval;
44 self->_event_handler = event_handler;
45 self->_event_param = event_param;
50 self->_update_interval = interval;
54 if (!self->_measurement_active)
67 if (self->_measurement_active)
72 self->_measurement_active =
true;
98 return (_twr_zssc3123_get_response(
self) >> 6) == 2;
132 transfer.
length =
sizeof(buffer);
140 if ((buffer[0] & 0x03) != 1)
145 *word = ((uint16_t) buffer[1]) << 8 | buffer[2];
163 return (_twr_zssc3123_get_response(
self) & 0x03) == 1;
213 transfer.
length =
sizeof(buffer);
220 if ((buffer[0] & 0xc0) == 0)
224 self->_raw = (uint16_t) (buffer[0] & 0x3f) << 8 | buffer[1];
230 static bool _twr_zssc3123_measurement_request(
twr_zssc3123_t *
self)
234 uint8_t buffer[1] = {
self->_i2c_address << 1 };
240 transfer.
length =
sizeof(buffer);
250 static void _twr_zssc3123_task(
void *param)
256 switch (self->_state)
258 case TWR_ZSSC3123_STATE_ERROR:
260 self->_valid =
false;
262 self->_measurement_active =
false;
264 if (self->_event_handler != NULL)
269 self->_state = TWR_ZSSC3123_STATE_INITIALIZE;
275 case TWR_ZSSC3123_STATE_INITIALIZE:
277 self->_next_update_start =
twr_tick_get() +
self->_update_interval;
279 if (!_twr_zssc3123_data_fetch(
self))
281 self->_state = TWR_ZSSC3123_STATE_ERROR;
286 self->_state = TWR_ZSSC3123_STATE_MEASURE;
292 case TWR_ZSSC3123_STATE_MEASURE:
294 self->_next_update_start =
twr_tick_get() +
self->_update_interval;
296 self->_measurement_active =
true;
298 self->_valid =
false;
300 if (!_twr_zssc3123_measurement_request(
self))
302 self->_state = TWR_ZSSC3123_STATE_ERROR;
307 self->_state = TWR_ZSSC3123_STATE_READ;
313 case TWR_ZSSC3123_STATE_READ:
315 if (!_twr_zssc3123_data_fetch(
self))
317 self->_state = TWR_ZSSC3123_STATE_ERROR;
322 self->_measurement_active =
false;
324 self->_state = TWR_ZSSC3123_STATE_MEASURE;
328 if (self->_event_handler != NULL)
337 self->_state = TWR_ZSSC3123_STATE_ERROR;
void twr_i2c_deinit(twr_i2c_channel_t channel)
Deitialize I2C channel.
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_read(twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Read from I2C channel.
bool twr_i2c_memory_write_16b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t data)
Memory write 2 bytes to I2C channel.
bool twr_i2c_write(twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Write to I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_current_absolute(twr_tick_t tick)
Schedule current task to absolute tick.
void twr_scheduler_plan_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
void twr_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
#define TWR_TICK_INFINITY
Maximum timestamp value.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.
void twr_timer_init(void)
Initialize timer.
void twr_timer_delay(uint16_t microseconds)
Relative delay.
void twr_timer_stop(void)
Stop timer.
void twr_timer_start(void)
Start timer.
bool twr_zssc3123_deinit(twr_zssc3123_t *self)
Deitialize ZSSC3123.
void twr_zssc3123_set_event_handler(twr_zssc3123_t *self, void(*event_handler)(twr_zssc3123_t *, twr_zssc3123_event_t, void *), void *event_param)
Set callback function.
bool twr_zssc3123_get_raw_cap_data(twr_zssc3123_t *self, uint16_t *raw)
Get capacitance data as raw value.
bool twr_zssc3123_eeprom_read(twr_zssc3123_t *self, uint8_t adr, uint16_t *word)
Write to eeprom, work only with command mode.
bool twr_zssc3123_end_cm(twr_zssc3123_t *self)
Ends Command Mode.
void twr_zssc3123_set_update_interval(twr_zssc3123_t *self, twr_tick_t interval)
Set scan interval.
bool twr_zssc3123_measure(twr_zssc3123_t *self)
Start measurement manually.
struct twr_zssc3123_t twr_zssc3123_t
ZSSC3123 instance.
bool twr_zssc3123_unlock_eeprom(twr_zssc3123_t *self)
Unlock eerpom, work only with command mode.
twr_zssc3123_event_t
Callback events.
void twr_zssc3123_set_data_fetch_delay(twr_zssc3123_t *self, twr_tick_t interval)
Set data fetch delay.
bool twr_zssc3123_init(twr_zssc3123_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize ZSSC3123.
bool twr_zssc3123_start_cm(twr_zssc3123_t *self)
Start Command Mode.
bool twr_zssc3123_eeprom_write(twr_zssc3123_t *self, uint8_t address, uint16_t word)
Read from eeprm, work only with command mode.
@ TWR_ZSSC3123_EVENT_ERROR
Error event.
@ TWR_ZSSC3123_EVENT_UPDATE
Update event.
void * buffer
Pointer to buffer which is being written or read.
uint8_t device_address
7-bit I2C device address
size_t length
Length of buffer which is being written or read.