2011-12-31 13:12:11 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2011-12-31 13:12:11 +00:00
|
|
|
|
2013-03-30 10:32:37 +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
|
2011-12-31 13:12:11 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2011-12-31 13:12:11 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
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.
|
2011-12-31 13:12:11 +00:00
|
|
|
*/
|
2011-12-31 13:15:54 +00:00
|
|
|
/*
|
|
|
|
Concepts and parts of this file have been contributed by Uladzimir Pylinsky
|
|
|
|
aka barthess.
|
|
|
|
*/
|
2011-12-31 13:12:11 +00:00
|
|
|
|
2012-01-05 17:51:00 +00:00
|
|
|
/**
|
|
|
|
* This demo acquire data from accelerometer and prints it in shell.
|
|
|
|
*/
|
|
|
|
|
2011-12-31 13:12:11 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
/* buffers depth */
|
|
|
|
#define ACCEL_RX_DEPTH 6
|
|
|
|
#define ACCEL_TX_DEPTH 4
|
|
|
|
|
|
|
|
/* mma8451q specific addresses */
|
|
|
|
#define ACCEL_OUT_DATA 0x01
|
|
|
|
#define ACCEL_CTRL_REG1 0x2A
|
|
|
|
|
|
|
|
static uint8_t rxbuf[ACCEL_RX_DEPTH];
|
|
|
|
static uint8_t txbuf[ACCEL_TX_DEPTH];
|
|
|
|
static i2cflags_t errors = 0;
|
|
|
|
static int16_t acceleration_x, acceleration_y, acceleration_z;
|
|
|
|
#define mma8451_addr 0b0011100
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void print(char *p) {
|
|
|
|
|
|
|
|
while (*p) {
|
2012-07-06 13:13:48 +00:00
|
|
|
sdPut(&SD2, *p++);
|
2011-12-31 13:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void println(char *p) {
|
|
|
|
|
|
|
|
while (*p) {
|
2012-07-06 13:13:48 +00:00
|
|
|
sdPut(&SD2, *p++);
|
2011-12-31 13:12:11 +00:00
|
|
|
}
|
2012-07-06 13:13:48 +00:00
|
|
|
sdWriteTimeout(&SD2, (uint8_t *)"\r\n", 2, TIME_INFINITE);
|
2011-12-31 13:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void printn(int16_t n) {
|
|
|
|
char buf[16], *p;
|
|
|
|
|
|
|
|
if (n > 0)
|
2012-07-06 13:13:48 +00:00
|
|
|
sdPut(&SD2, '+');
|
2011-12-31 13:12:11 +00:00
|
|
|
else{
|
2012-07-06 13:13:48 +00:00
|
|
|
sdPut(&SD2, '-');
|
2011-12-31 13:12:11 +00:00
|
|
|
n = abs(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!n)
|
2012-07-06 13:13:48 +00:00
|
|
|
sdPut(&SD2, '0');
|
2011-12-31 13:12:11 +00:00
|
|
|
else {
|
|
|
|
p = buf;
|
|
|
|
while (n)
|
|
|
|
*p++ = (n % 10) + '0', n /= 10;
|
|
|
|
while (p > buf)
|
2012-07-06 13:13:48 +00:00
|
|
|
sdPut(&SD2, *--p);
|
2011-12-31 13:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts data from 2complemented representation to signed integer
|
|
|
|
*/
|
|
|
|
int16_t complement2signed(uint8_t msb, uint8_t lsb){
|
|
|
|
uint16_t word = 0;
|
|
|
|
word = (msb << 8) + lsb;
|
|
|
|
if (msb > 0x7F){
|
|
|
|
return -1 * ((int16_t)((~word) + 1));
|
|
|
|
}
|
|
|
|
return (int16_t)word;
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:13:48 +00:00
|
|
|
/* I2C interface #2 */
|
|
|
|
static const I2CConfig i2cfg2 = {
|
|
|
|
OPMODE_I2C,
|
|
|
|
400000,
|
|
|
|
FAST_DUTY_CYCLE_2,
|
|
|
|
};
|
|
|
|
|
2011-12-31 13:12:11 +00:00
|
|
|
/*
|
|
|
|
* Application entry point.
|
|
|
|
*/
|
|
|
|
int main(void) {
|
|
|
|
msg_t status = RDY_OK;
|
2012-01-05 17:51:00 +00:00
|
|
|
systime_t tmo = MS2ST(4);
|
2011-12-31 13:12:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* System initializations.
|
|
|
|
* - HAL initialization, this also initializes the configured device drivers
|
|
|
|
* and performs the board-specific initializations.
|
|
|
|
* - Kernel initialization, the main() function becomes a thread and the
|
|
|
|
* RTOS is active.
|
|
|
|
*/
|
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
2012-07-06 13:13:48 +00:00
|
|
|
/*
|
|
|
|
* Starts I2C
|
|
|
|
*/
|
|
|
|
i2cStart(&I2CD2, &i2cfg2);
|
|
|
|
|
2011-12-31 13:12:11 +00:00
|
|
|
/*
|
|
|
|
* Prepares the Serial driver 2
|
|
|
|
*/
|
|
|
|
sdStart(&SD2, NULL); /* Default is 38400-8-N-1.*/
|
|
|
|
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
|
|
|
|
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the accelerometer
|
|
|
|
*/
|
2011-12-31 13:15:54 +00:00
|
|
|
txbuf[0] = ACCEL_CTRL_REG1; /* register address */
|
2011-12-31 13:12:11 +00:00
|
|
|
txbuf[1] = 0x1;
|
|
|
|
i2cAcquireBus(&I2CD2);
|
2012-01-05 17:51:00 +00:00
|
|
|
status = i2cMasterTransmitTimeout(&I2CD2, mma8451_addr, txbuf, 2, rxbuf, 0, tmo);
|
2011-12-31 13:12:11 +00:00
|
|
|
i2cReleaseBus(&I2CD2);
|
|
|
|
|
2012-01-05 17:51:00 +00:00
|
|
|
if (status != RDY_OK){
|
|
|
|
errors = i2cGetErrors(&I2CD2);
|
|
|
|
}
|
|
|
|
|
2011-12-31 13:12:11 +00:00
|
|
|
/*
|
|
|
|
* Normal main() thread activity, nothing in this test.
|
|
|
|
*/
|
|
|
|
while (TRUE) {
|
2012-01-05 17:51:00 +00:00
|
|
|
palTogglePad(GPIOB, GPIOB_LED_B);
|
2011-12-31 13:12:11 +00:00
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
|
2011-12-31 13:15:54 +00:00
|
|
|
txbuf[0] = ACCEL_OUT_DATA; /* register address */
|
2011-12-31 13:12:11 +00:00
|
|
|
i2cAcquireBus(&I2CD2);
|
2012-01-05 17:51:00 +00:00
|
|
|
status = i2cMasterTransmitTimeout(&I2CD2, mma8451_addr, txbuf, 1, rxbuf, 6, tmo);
|
2011-12-31 13:12:11 +00:00
|
|
|
i2cReleaseBus(&I2CD2);
|
|
|
|
|
|
|
|
if (status != RDY_OK){
|
|
|
|
errors = i2cGetErrors(&I2CD2);
|
|
|
|
}
|
|
|
|
|
|
|
|
acceleration_x = complement2signed(rxbuf[0], rxbuf[1]);
|
|
|
|
acceleration_y = complement2signed(rxbuf[2], rxbuf[3]);
|
|
|
|
acceleration_z = complement2signed(rxbuf[4], rxbuf[5]);
|
|
|
|
|
|
|
|
print("x: ");
|
|
|
|
printn(acceleration_x);
|
|
|
|
print(" y: ");
|
|
|
|
printn(acceleration_y);
|
|
|
|
print(" z: ");
|
|
|
|
printn(acceleration_z);
|
|
|
|
println("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|