1 #include <twr_module_infra_grid.h>
12 #define _TWR_AMG88xx_ADDR 0x68
14 #define _TWR_AMG88xx_PCLT 0x00
15 #define _TWR_AMG88xx_RST 0x01
16 #define _TWR_AMG88xx_FPSC 0x02
17 #define _TWR_AMG88xx_INTC 0x03
18 #define _TWR_AMG88xx_STAT 0x04
19 #define _TWR_AMG88xx_SCLR 0x05
20 #define _TWR_AMG88xx_AVE 0x07
21 #define _TWR_AMG88xx_INTHL 0x08
22 #define _TWR_AMG88xx_TTHL 0x0e
23 #define _TWR_AMG88xx_TTHH 0x0f
24 #define _TWR_AMG88xx_INT0 0x10
25 #define _TWR_AMG88xx_AVG 0x1f
26 #define _TWR_AMG88xx_T01L 0x80
28 #define _TWR_MODULE_INFRA_GRID_DELAY_RUN 50
29 #define _TWR_MODULE_INFRA_GRID_DELAY_INITIALIZATION 50
30 #define _TWR_MODULE_INFRA_GRID_DELAY_MEASUREMENT 5
32 #define _TWR_MODULE_INFRA_GRID_DELAY_MODE_CHANGE 50
33 #define _TWR_MODULE_INFRA_GRID_DELAY_POWER_UP 50
34 #define _TWR_MODULE_INFRA_GRID_DELAY_INITIAL_RESET 10
35 #define _TWR_MODULE_INFRA_GRID_DELAY_FLAG_RESET 110
37 #define _TWR_MODULE_INFRA_GRID_PIN_POWER TWR_TCA9534A_PIN_P7
39 static void _twr_module_infra_grid_task_interval(
void *param);
40 static void _twr_module_infra_grid_task_measure(
void *param);
44 memset(
self, 0,
sizeof(*
self));
47 self->_i2c_address = _TWR_AMG88xx_ADDR;
48 self->_cmd_sleep =
true;
51 self->_task_id_measure =
twr_scheduler_register(_twr_module_infra_grid_task_measure,
self, _TWR_MODULE_INFRA_GRID_DELAY_RUN);
53 self->_tick_ready = _TWR_MODULE_INFRA_GRID_DELAY_RUN;
59 self->_event_handler = event_handler;
60 self->_event_param = event_param;
65 self->_update_interval = interval;
67 if (self->_update_interval >= 1000)
69 self->_cmd_sleep =
true;
85 if (self->_measurement_active)
90 self->_measurement_active =
true;
100 int8_t temperature[2];
102 twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, _TWR_AMG88xx_TTHL, (uint8_t *) &temperature[0]);
103 twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, _TWR_AMG88xx_TTHH, (uint8_t *) &temperature[1]);
105 return (temperature[0] | temperature[1] << 8) * 0.0625f;
114 transfer.
buffer =
self->_sensor_data;
122 if (!self->_temperature_valid)
127 for (
int i = 0; i < 64 ;i++)
130 int16_t temporary_data =
self->_sensor_data[i];
132 if (temporary_data > 0x200)
134 temperature = (-temporary_data + 0xfff) * -0.25f;
138 temperature = temporary_data * 0.25f;
141 values[i] = temperature;
147 static void _twr_module_infra_grid_task_interval(
void *param)
154 static void _twr_module_infra_grid_task_measure(
void *param)
160 switch (self->_state)
162 case TWR_MODULE_INFRA_GRID_STATE_ERROR:
164 self->_temperature_valid =
false;
166 self->_measurement_active =
false;
168 if (self->_event_handler != NULL)
173 self->_state = TWR_MODULE_INFRA_GRID_STATE_INITIALIZE;
177 case TWR_MODULE_INFRA_GRID_STATE_INITIALIZE:
179 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
203 if (self->_enable_sleep != self->_cmd_sleep)
205 self->_enable_sleep =
self->_cmd_sleep;
208 if (self->_enable_sleep)
245 self->_state = TWR_MODULE_INFRA_GRID_STATE_MODE_CHANGE;
247 self->_tick_ready =
twr_tick_get() + _TWR_MODULE_INFRA_GRID_DELAY_INITIALIZATION;
249 if (self->_measurement_active)
256 case TWR_MODULE_INFRA_GRID_STATE_MODE_CHANGE:
259 if (!self->_enable_sleep)
261 self->_state = TWR_MODULE_INFRA_GRID_STATE_READ;
268 self->_state = TWR_MODULE_INFRA_GRID_STATE_POWER_UP;
276 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
279 self->_state = TWR_MODULE_INFRA_GRID_STATE_POWER_UP;
285 case TWR_MODULE_INFRA_GRID_STATE_POWER_UP:
287 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
294 self->_state = TWR_MODULE_INFRA_GRID_STATE_INITIAL_RESET;
298 case TWR_MODULE_INFRA_GRID_STATE_INITIAL_RESET:
300 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
308 self->_state = TWR_MODULE_INFRA_GRID_STATE_FLAG_RESET;
312 case TWR_MODULE_INFRA_GRID_STATE_FLAG_RESET:
314 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
322 self->_state = TWR_MODULE_INFRA_GRID_STATE_MEASURE;
326 case TWR_MODULE_INFRA_GRID_STATE_MEASURE:
328 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
344 self->_state = TWR_MODULE_INFRA_GRID_STATE_READ;
350 case TWR_MODULE_INFRA_GRID_STATE_READ:
352 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
357 transfer.
buffer =
self->_sensor_data;
366 if (self->_enable_sleep)
384 self->_temperature_valid =
true;
385 self->_state = TWR_MODULE_INFRA_GRID_STATE_UPDATE;
389 case TWR_MODULE_INFRA_GRID_STATE_UPDATE:
391 self->_measurement_active =
false;
393 if (self->_event_handler != NULL)
399 if (self->_enable_sleep != self->_cmd_sleep)
401 self->_enable_sleep =
self->_cmd_sleep;
404 self->_state = TWR_MODULE_INFRA_GRID_STATE_MODE_CHANGE;
409 self->_state = TWR_MODULE_INFRA_GRID_STATE_ERROR;
417 return self->_revision;
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_memory_read(twr_i2c_channel_t channel, const twr_i2c_memory_transfer_t *transfer)
Memory read from I2C channel.
bool twr_i2c_memory_write_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
bool twr_i2c_memory_read_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data)
Memory read 1 byte from I2C channel.
@ TWR_I2C_SPEED_100_KHZ
I2C communication speed is 100 kHz.
@ TWR_I2C_I2C0
I2C channel I2C0.
twr_module_infra_grid_revision_t twr_module_infra_grid_get_revision(twr_module_infra_grid_t *self)
Get module revision.
bool twr_module_infra_grid_get_temperatures_celsius(twr_module_infra_grid_t *self, float *values)
Get measured temperature in degrees of Celsius as a array of float numbers.
void twr_module_infra_grid_set_update_interval(twr_module_infra_grid_t *self, twr_tick_t interval)
Set measurement interval.
float twr_module_infra_grid_read_thermistor(twr_module_infra_grid_t *self)
Read and return thermistor temperature sensor value.
twr_module_infra_grid_event_t
Callback events.
bool twr_module_infra_grid_measure(twr_module_infra_grid_t *self)
Start measurement manually.
twr_module_infra_grid_revision_t
Infragrid Module Revision.
void twr_module_infra_grid_init(twr_module_infra_grid_t *self)
Initialize Infra Grid Module.
bool twr_module_infra_grid_read_values(twr_module_infra_grid_t *self)
Read measured data from sensor to the buffer.
void twr_module_infra_grid_set_event_handler(twr_module_infra_grid_t *self, void(*event_handler)(twr_module_infra_grid_t *, twr_module_infra_grid_event_t, void *), void *event_param)
Set callback function.
struct twr_module_infra_grid_t twr_module_infra_grid_t
Infra Grid Module instance.
@ TWR_MODULE_INFRA_GRID_EVENT_UPDATE
Update event.
@ TWR_MODULE_INFRA_GRID_EVENT_ERROR
Error event.
@ TWR_MODULE_INFRA_GRID_REVISION_R1_1
Revision 1.1.
@ TWR_MODULE_INFRA_GRID_REVISION_R1_0
Revision 1.0.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
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_plan_relative(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to tick relative from current spin.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
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_write_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int state)
Write pin state.
#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.
I2C memory transfer parameters.
uint32_t memory_address
8-bit I2C memory address (it can be extended to 16-bit format if OR-ed with TWR_I2C_MEMORY_ADDRESS_16...
uint8_t device_address
7-bit I2C device address
size_t length
Length of buffer which is being written or read.
void * buffer
Pointer to buffer which is being written or read.