46 #include "platform-conf.h"
48 #include <cc1120-arch.h>
50 #include "isr_compat.h"
53 #define PRINTFDEBUG(...) printf(__VA_ARGS)
55 #define PRINTFDEBUG(...)
59 void (*accm_int1_cb)(uint8_t reg);
60 void (*accm_int2_cb)(uint8_t reg);
62 process_event_t int1_event, int2_event;
65 static uint16_t int1_mask = 0, int2_mask = 0;
74 enum ADXL345_STATUSTYPES {
85 static enum ADXL345_STATUSTYPES _ADXL345_STATUS = 0x00;
89 static uint8_t adxl345_default_settings[] = {
94 ADXL345_THRESH_TAP_DEFAULT,
99 ADXL345_LATENT_DEFAULT,
100 ADXL345_WINDOW_DEFAULT,
101 ADXL345_THRESH_ACT_DEFAULT,
102 ADXL345_THRESH_INACT_DEFAULT,
103 ADXL345_TIME_INACT_DEFAULT,
104 ADXL345_ACT_INACT_CTL_DEFAULT,
105 ADXL345_THRESH_FF_DEFAULT,
106 ADXL345_TIME_FF_DEFAULT,
107 ADXL345_TAP_AXES_DEFAULT,
111 ADXL345_BW_RATE_DEFAULT,
112 ADXL345_POWER_CTL_DEFAULT,
113 ADXL345_INT_ENABLE_DEFAULT,
114 ADXL345_INT_MAP_DEFAULT,
117 ADXL345_DATA_FORMAT_DEFAULT,
118 ADXL345_FIFO_CTL_DEFAULT
123 PROCESS(accmeter_process,
"Accelerometer process");
132 accm_write_reg(uint8_t reg, uint8_t val) {
133 uint8_t tx_buf[] = {reg, val};
135 i2c_transmitinit(ADXL345_ADDR);
137 PRINTFDEBUG(
"I2C Ready to TX\n");
139 i2c_transmit_n(2, tx_buf);
141 PRINTFDEBUG(
"WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg);
153 accm_write_stream(uint8_t len, uint8_t *data) {
154 i2c_transmitinit(ADXL345_ADDR);
156 PRINTFDEBUG(
"I2C Ready to TX(stream)\n");
158 i2c_transmit_n(len, data);
160 PRINTFDEBUG(
"WRITE_STR %u B to 0x%02X\n", len, data[0]);
171 accm_read_reg(uint8_t reg) {
174 PRINTFDEBUG(
"READ_REG 0x%02X\n", reg);
177 i2c_transmitinit(ADXL345_ADDR);
179 i2c_transmit_n(1, &rtx);
183 i2c_receiveinit(ADXL345_ADDR);
185 i2c_receive_n(1, &retVal);
200 accm_read_stream(uint8_t reg, uint8_t len, uint8_t *whereto) {
202 PRINTFDEBUG(
"READ_STR %u B from 0x%02X\n", len, reg);
205 i2c_transmitinit(ADXL345_ADDR);
207 i2c_transmit_n(1, &rtx);
211 i2c_receiveinit(ADXL345_ADDR);
213 i2c_receive_n(len, whereto);
224 accm_read_axis(
enum ADXL345_AXIS axis){
230 accm_read_stream(ADXL345_DATAX0 + axis, 2, &tmp[0]);
231 rd = (int16_t)(tmp[0] | (tmp[1]<<8));
247 accm_set_grange(uint8_t grange){
248 if(grange > ADXL345_RANGE_16G) {
250 PRINTFDEBUG(
"ADXL grange invalid: %u\n", grange);
256 tempreg = (accm_read_reg(ADXL345_DATA_FORMAT) & 0xFC);
258 accm_write_reg(ADXL345_DATA_FORMAT, tempreg);
267 if(!(_ADXL345_STATUS & INITED)){
268 PRINTFDEBUG(
"ADXL345 init\n");
269 _ADXL345_STATUS |= INITED;
276 ADXL345_DIR &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN);
277 ADXL345_SEL &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN);
278 ADXL345_SEL2 &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN);
284 accm_write_stream(15, &adxl345_default_settings[0]);
285 accm_write_stream(5, &adxl345_default_settings[15]);
286 accm_write_reg(ADXL345_DATA_FORMAT, adxl345_default_settings[20]);
287 accm_write_reg(ADXL345_FIFO_CTL, adxl345_default_settings[21]);
293 ADXL345_IES &=~ (ADXL345_INT1_PIN | ADXL345_INT2_PIN);
294 ADXL345_IE |= (ADXL345_INT1_PIN | ADXL345_INT2_PIN);
304 accm_set_irq(uint8_t int1, uint8_t int2){
306 PRINTFDEBUG(
"IRQs set to INT1: 0x%02X IRQ2: 0x%02X\n", int1, int2);
311 accm_write_reg(ADXL345_INT_ENABLE, (int1 | int2));
312 accm_write_reg(ADXL345_INT_MAP, int2);
323 static volatile clock_time_t ints_backoffs[] = {ADXL345_INT_OVERRUN_BACKOFF, ADXL345_INT_WATERMARK_BACKOFF,
324 ADXL345_INT_FREEFALL_BACKOFF, ADXL345_INT_INACTIVITY_BACKOFF,
325 ADXL345_INT_ACTIVITY_BACKOFF, ADXL345_INT_DOUBLETAP_BACKOFF,
326 ADXL345_INT_TAP_BACKOFF, ADXL345_INT_DATAREADY_BACKOFF};
333 backoff_passed(clocktime_t happenedAt,
const clocktime_t backoff){
334 if(timenow-lasttime >= backoff) {
337 return (timenow-lasttime);
350 ireg = accm_read_reg(ADXL345_INT_SOURCE);
354 if(ireg & int1_mask){
355 if(accm_int1_cb !=
NULL){
356 PRINTFDEBUG(
"INT1 cb invoked\n");
359 }
else if(ireg & int2_mask){
360 if(accm_int2_cb !=
NULL){
361 PRINTFDEBUG(
"INT2 cb invoked\n");
386 static struct timer suppressTimer1, suppressTimer2;
388 ISR(PORT1, port1_isr)
390 ENERGEST_ON(ENERGEST_TYPE_IRQ);
392 if ((ADXL345_IFG & ADXL345_INT1_PIN) && !(CC1120_GDO0_PORT(IFG) & BV(CC1120_GDO0_PIN))){
395 timer_set(&suppressTimer1, SUPPRESS_TIME_INT1);
396 ADXL345_IFG &= ~ADXL345_INT1_PIN;
400 }
else if ((ADXL345_IFG & ADXL345_INT2_PIN) && !(CC1120_GDO0_PORT(IFG) & BV(CC1120_GDO0_PIN))){
403 timer_set(&suppressTimer2, SUPPRESS_TIME_INT2);
404 ADXL345_IFG &= ~ADXL345_INT2_PIN;
410 if(cc1120_interrupt_handler()) {
414 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
void process_poll(struct process *p)
Request a process to be polled.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
#define PROCESS_END()
Define the end of a process.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
I2C communication device driver header file for Zolertia Z1 sensor node.
#define PROCESS_POLLHANDLER(handler)
Specify an action when a process is polled.
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
process_event_t process_alloc_event(void)
Allocate a global event number.
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
void i2c_enable(void)
Configure serial controller in I2C mode and set I2C speed.
Device drivers header file for adxl345 accelerometer in Zolertia Z1.
#define NULL
The null pointer.
void process_start(struct process *p, process_data_t data)
Start a process.
int timer_expired(struct timer *t)
Check if a timer has expired.
#define PROCESS_BEGIN()
Define the beginning of a process.