2014-10-12 17:11:10 +00:00
|
|
|
/*
|
2015-01-11 13:56:55 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
|
2014-10-12 17:11:10 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Concepts and parts of this file have been contributed by Uladzimir Pylinsky
|
|
|
|
aka barthess.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
This demo:
|
|
|
|
1) turns LED on when you incline (~45 deg.) or shake the board;
|
|
|
|
2) correctly handles absence of slave device on bus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#include "lis3.h"
|
|
|
|
#include "fake.h"
|
|
|
|
|
|
|
|
/* measured acceleration components {x,y,z} */
|
|
|
|
static float acc[3];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accelerometer polling thread.
|
|
|
|
*/
|
|
|
|
static THD_WORKING_AREA(PollAccelThreadWA, 256);
|
|
|
|
static THD_FUNCTION(PollAccelThread, arg) {
|
2015-04-03 12:48:22 +00:00
|
|
|
|
2014-10-12 17:11:10 +00:00
|
|
|
(void)arg;
|
|
|
|
|
2015-04-03 12:48:22 +00:00
|
|
|
chRegSetThreadName("PollAccel");
|
|
|
|
while (true) {
|
2014-10-12 17:11:10 +00:00
|
|
|
osalThreadSleepMilliseconds(32);
|
|
|
|
lis3GetAcc(acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fake polling thread.
|
|
|
|
*/
|
|
|
|
static THD_WORKING_AREA(PollFakeThreadWA, 256);
|
|
|
|
static THD_FUNCTION(PollFakeThread, arg) {
|
2015-04-03 12:48:22 +00:00
|
|
|
|
2014-10-12 17:11:10 +00:00
|
|
|
(void)arg;
|
|
|
|
|
2015-04-03 12:48:22 +00:00
|
|
|
chRegSetThreadName("PollFake");
|
|
|
|
while (true) {
|
2014-10-12 17:11:10 +00:00
|
|
|
osalThreadSleepMilliseconds(16);
|
|
|
|
request_fake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I2C1 config.
|
|
|
|
*/
|
|
|
|
static const I2CConfig i2cfg1 = {
|
|
|
|
OPMODE_I2C,
|
|
|
|
400000,
|
|
|
|
FAST_DUTY_CYCLE_2,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry point, note, the main() function is already a thread in the system
|
|
|
|
* on entry.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
|
|
|
|
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
|
|
|
i2cStart(&I2CD1, &i2cfg1);
|
|
|
|
lis3Start();
|
|
|
|
|
|
|
|
/* Create accelerometer thread. */
|
|
|
|
chThdCreateStatic(PollAccelThreadWA,
|
|
|
|
sizeof(PollAccelThreadWA),
|
|
|
|
NORMALPRIO,
|
|
|
|
PollAccelThread,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Create not responding thread. */
|
|
|
|
chThdCreateStatic(PollFakeThreadWA,
|
|
|
|
sizeof(PollFakeThreadWA),
|
|
|
|
NORMALPRIO,
|
|
|
|
PollFakeThread,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* main loop handles LED */
|
2015-04-03 12:48:22 +00:00
|
|
|
while (true) {
|
2014-10-12 17:11:10 +00:00
|
|
|
if (sqrtf(acc[0]*acc[0] + acc[1]*acc[1]) > 0.5)
|
|
|
|
palClearPad(IOPORT3, GPIOC_LED); /* on */
|
|
|
|
else
|
|
|
|
palSetPad(IOPORT3, GPIOC_LED); /* off */
|
|
|
|
osalThreadSleepMilliseconds(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|