1 #include <twr_pyq1648.h>
4 #include <twr_system.h>
8 #define TWR_PYQ1648_BPF 0x00
9 #define TWR_PYQ1648_LPF 0x01
10 #define TWR_PYQ1648_WAKE_UP_MODE 0x02
12 #define TWR_PYQ1648_DELAY_RUN 50
13 #define TWR_PYQ1648_DELAY_INITIALIZATION 10
14 #define TWR_PYQ1648_UPDATE_INTERVAL 100
17 static inline void _twr_pyq1648_dev_init(
twr_pyq1648_t *
self);
18 static inline void _twr_pyq1648_compose_event_unit_config(
twr_pyq1648_t *
self);
19 static void _twr_pyq1648_task(
void *param);
21 static const uint8_t _twr_pyq1648_sensitivity_table[4] =
29 extern GPIO_TypeDef * twr_gpio_port[];
30 extern uint16_t twr_gpio_16_bit_mask[];
31 extern uint32_t twr_gpio_32_bit_upper_mask[];
33 static GPIO_TypeDef **_pyq1648_gpiox_table = twr_gpio_port;
34 static uint16_t *_pyq1648_set_mask = twr_gpio_16_bit_mask;
35 static uint32_t *_pyq1648_reset_mask = twr_gpio_32_bit_upper_mask;
40 memset(
self, 0,
sizeof(*
self));
44 self->_blank_period = 1000;
47 self->_gpio_channel_serin = gpio_channel_serin;
48 self->_gpio_channel_dl = gpio_channel_dl;
62 self->_event_handler = event_handler;
65 self->_event_param = event_param;
71 self->_sensitivity = _twr_pyq1648_sensitivity_table[sensitivity];
73 self->_state = TWR_PYQ1648_STATE_INITIALIZE;
81 self->_blank_period = blank_period;
84 void _twr_pyq1648_compose_event_unit_config(
twr_pyq1648_t *
self)
94 self->_config = 0x00000000;
95 self->_config |= (
self->_sensitivity << 17) | (TWR_PYQ1648_WAKE_UP_MODE << 7) | (TWR_PYQ1648_BPF << 5) |0x10;
119 static inline void _twr_pyq1648_dev_init(
twr_pyq1648_t *
self)
125 twr_system_pll_enable();
128 uint32_t regval =
self->_config;
129 uint32_t regmask = 0x1000000;
133 uint32_t bsrr_mask[2] =
135 [0] = _pyq1648_reset_mask[
self->_gpio_channel_serin],
136 [1] = _pyq1648_set_mask[
self->_gpio_channel_serin]
138 GPIO_TypeDef *GPIOx = _pyq1648_gpiox_table[
self->_gpio_channel_serin];
139 volatile uint32_t *GPIOx_BSRR = &GPIOx->BSRR;
142 _twr_pyq1648_msp_init(self->_gpio_channel_serin, self->_gpio_channel_dl);
145 for (
int i = 0; i < 25; i++)
147 next_bit = (regval & regmask) != 0 ?
true :
false;
152 *GPIOx_BSRR = bsrr_mask[0];
159 *GPIOx_BSRR = bsrr_mask[1];
166 *GPIOx_BSRR = bsrr_mask[next_bit];
179 twr_system_pll_disable();
185 static void _twr_pyq1648_task(
void *param)
191 switch (self->_state)
193 case TWR_PYQ1648_STATE_ERROR:
195 if (self->_event_handler != NULL)
200 self->_state = TWR_PYQ1648_STATE_INITIALIZE;
206 case TWR_PYQ1648_STATE_INITIALIZE:
208 self->_state = TWR_PYQ1648_STATE_ERROR;
210 _twr_pyq1648_compose_event_unit_config(
self);
212 _twr_pyq1648_dev_init(
self);
214 self->_state = TWR_PYQ1648_STATE_IGNORE;
220 case TWR_PYQ1648_STATE_IGNORE:
222 self->_state = TWR_PYQ1648_STATE_ERROR;
224 if (self->_ignore_untill == 0)
232 _twr_pyq1648_clear_event(
self);
237 self->_state = TWR_PYQ1648_STATE_CHECK;
241 self->_state = TWR_PYQ1648_STATE_IGNORE;
248 case TWR_PYQ1648_STATE_CHECK:
252 self->_state = TWR_PYQ1648_STATE_ERROR;
256 if (tick_now >= self->_aware_time)
258 if (self->_event_handler != NULL)
263 self->_aware_time = tick_now +
self->_blank_period;
265 _twr_pyq1648_clear_event(
self);
268 self->_state = TWR_PYQ1648_STATE_CHECK;
276 self->_state = TWR_PYQ1648_STATE_ERROR;
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_channel_t
GPIO channels.
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_INPUT
GPIO channel operates as input.
@ TWR_GPIO_MODE_OUTPUT
GPIO channel operates as output.
@ TWR_GPIO_PULL_DOWN
GPIO channel has pull-down.
void twr_irq_disable(void)
Disable interrupt requests globally (call can be nested)
void twr_irq_enable(void)
Enable interrupt requests globally (call can be nested)
struct twr_pyq1648_t twr_pyq1648_t
PYQ1648 instance.
void twr_pyq1648_set_sensitivity(twr_pyq1648_t *self, twr_pyq1648_sensitivity_t sensitivity)
Set PYQ1648 sensitivity.
void twr_pyq1648_set_blank_period(twr_pyq1648_t *self, twr_tick_t blank_period)
Set blank period (for how long alarm events will be ignored)
void twr_pyq1648_init(twr_pyq1648_t *self, twr_gpio_channel_t gpio_channel_serin, twr_gpio_channel_t gpio_channel_dl)
Initialize PYQ1648 driver.
twr_pyq1648_sensitivity_t
Possible sensitivities.
void twr_pyq1648_set_event_handler(twr_pyq1648_t *self, void(*event_handler)(twr_pyq1648_t *, twr_pyq1648_event_t, void *), void *event_param)
Set callback function.
twr_pyq1648_event_t
Callback events.
@ TWR_PYQ1648_SENSITIVITY_HIGH
High sensitivity.
@ TWR_PYQ1648_SENSITIVITY_VERY_HIGH
Very high sensitivity.
@ TWR_PYQ1648_SENSITIVITY_LOW
Low sensitivity.
@ TWR_PYQ1648_SENSITIVITY_MEDIUM
Medium sensitivity.
@ TWR_PYQ1648_EVENT_ERROR
Error event.
@ TWR_PYQ1648_EVENT_MOTION
Update event.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified 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.
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_stop(void)
Stop timer.
void twr_timer_start(void)
Start timer.
uint16_t twr_timer_get_microseconds(void)
Get actual tick of timer.