Change LSM6DS3 power mode using nRF51-DK BLE.

Dependencies:   BLE_API LSM6DS3 mbed nRF51822 nrf51_rtc

Committer:
5hel2l2y
Date:
Mon Jul 11 21:03:17 2016 +0000
Revision:
5:e2ba3c39e3b3
Parent:
4:46dd8065e05b
Modified read time.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
5hel2l2y 0:d0291b4a856a 1 /* mbed Microcontroller Library
5hel2l2y 0:d0291b4a856a 2 * Copyright (c) 2006-2013 ARM Limited
5hel2l2y 0:d0291b4a856a 3 *
5hel2l2y 0:d0291b4a856a 4 * Licensed under the Apache License, Version 2.0 (the "License");
5hel2l2y 0:d0291b4a856a 5 * you may not use this file except in compliance with the License.
5hel2l2y 0:d0291b4a856a 6 * You may obtain a copy of the License at
5hel2l2y 0:d0291b4a856a 7 *
5hel2l2y 0:d0291b4a856a 8 * http://www.apache.org/licenses/LICENSE-2.0
5hel2l2y 0:d0291b4a856a 9 *
5hel2l2y 0:d0291b4a856a 10 * Unless required by applicable law or agreed to in writing, software
5hel2l2y 0:d0291b4a856a 11 * distributed under the License is distributed on an "AS IS" BASIS,
5hel2l2y 0:d0291b4a856a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5hel2l2y 0:d0291b4a856a 13 * See the License for the specific language governing permissions and
5hel2l2y 0:d0291b4a856a 14 * limitations under the License.
5hel2l2y 0:d0291b4a856a 15 */
5hel2l2y 0:d0291b4a856a 16
5hel2l2y 0:d0291b4a856a 17 #include "mbed.h"
5hel2l2y 0:d0291b4a856a 18 #include "ble/BLE.h"
5hel2l2y 0:d0291b4a856a 19
5hel2l2y 0:d0291b4a856a 20 #include "ble/services/UARTService.h"
5hel2l2y 1:7562cc147e26 21 #include "LSM6DS3/LSM6DS3.h"
5hel2l2y 0:d0291b4a856a 22
5hel2l2y 5:e2ba3c39e3b3 23 #include "nrf51_rtc.h"
5hel2l2y 5:e2ba3c39e3b3 24
5hel2l2y 1:7562cc147e26 25 LSM6DS3 imu(p30, p7);
5hel2l2y 0:d0291b4a856a 26 Serial pc(p9, p11);
5hel2l2y 0:d0291b4a856a 27
5hel2l2y 0:d0291b4a856a 28 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
5hel2l2y 0:d0291b4a856a 29 * it will have an impact on code-size and power consumption. */
5hel2l2y 0:d0291b4a856a 30
5hel2l2y 0:d0291b4a856a 31 #if NEED_CONSOLE_OUTPUT
5hel2l2y 0:d0291b4a856a 32 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
5hel2l2y 0:d0291b4a856a 33 #else
5hel2l2y 0:d0291b4a856a 34 #define DEBUG(...) /* nothing */
5hel2l2y 0:d0291b4a856a 35 #endif /* #if NEED_CONSOLE_OUTPUT */
5hel2l2y 0:d0291b4a856a 36
5hel2l2y 0:d0291b4a856a 37 BLEDevice ble;
5hel2l2y 3:a43f11f5378f 38 // Status lights
5hel2l2y 3:a43f11f5378f 39 DigitalOut ledAlive(LED1);
5hel2l2y 3:a43f11f5378f 40 DigitalOut ledIntrG(LED3);
5hel2l2y 3:a43f11f5378f 41 DigitalOut ledIntrO(LED4);
5hel2l2y 3:a43f11f5378f 42 // Interrupt pin connection
5hel2l2y 3:a43f11f5378f 43 InterruptIn intrGX(p14); //INT1 (Gyro & Accel Interrupt)
5hel2l2y 3:a43f11f5378f 44 InterruptIn intrGXO(p13); //INT2 (Gyro & Accel Data Ready)
5hel2l2y 0:d0291b4a856a 45
5hel2l2y 0:d0291b4a856a 46 UARTService *uartServicePtr;
5hel2l2y 0:d0291b4a856a 47
5hel2l2y 0:d0291b4a856a 48 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
5hel2l2y 0:d0291b4a856a 49 {
5hel2l2y 0:d0291b4a856a 50 DEBUG("Disconnected!\n\r");
5hel2l2y 0:d0291b4a856a 51 DEBUG("Restarting the advertising process\n\r");
5hel2l2y 0:d0291b4a856a 52 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 53 }
5hel2l2y 0:d0291b4a856a 54
5hel2l2y 0:d0291b4a856a 55 void onDataWritten(const GattWriteCallbackParams *params)
5hel2l2y 0:d0291b4a856a 56 {
5hel2l2y 0:d0291b4a856a 57 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
5hel2l2y 0:d0291b4a856a 58 uint16_t bytesRead = params->len;
5hel2l2y 0:d0291b4a856a 59 uint8_t value = *(params->data);
5hel2l2y 0:d0291b4a856a 60 uint16_t status;
5hel2l2y 0:d0291b4a856a 61 DEBUG("received %u bytes\n\r", bytesRead);
5hel2l2y 0:d0291b4a856a 62 DEBUG("recevied %d data\n\r", value);
5hel2l2y 0:d0291b4a856a 63
5hel2l2y 0:d0291b4a856a 64 switch(value) {
5hel2l2y 0:d0291b4a856a 65 case 49:
5hel2l2y 0:d0291b4a856a 66 DEBUG("Off power\n\r");
5hel2l2y 1:7562cc147e26 67 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G,
5hel2l2y 1:7562cc147e26 68 imu.G_POWER_DOWN, imu.A_POWER_DOWN);
5hel2l2y 0:d0291b4a856a 69 break;
5hel2l2y 0:d0291b4a856a 70 case 50:
5hel2l2y 4:46dd8065e05b 71 DEBUG("Accelerometer Low, Gyro Off\n\r");
5hel2l2y 1:7562cc147e26 72 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G,
5hel2l2y 4:46dd8065e05b 73 imu.G_POWER_DOWN, imu.A_ODR_13);
5hel2l2y 0:d0291b4a856a 74 break;
5hel2l2y 0:d0291b4a856a 75 case 51:
5hel2l2y 4:46dd8065e05b 76 DEBUG("Accelerometer High, Gyro Off\n\r");
5hel2l2y 4:46dd8065e05b 77 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_8G,
5hel2l2y 4:46dd8065e05b 78 imu.G_POWER_DOWN, imu.A_ODR_6660);
5hel2l2y 4:46dd8065e05b 79 break;
5hel2l2y 4:46dd8065e05b 80 case 52:
5hel2l2y 4:46dd8065e05b 81 DEBUG("Accelerometer High, Gyro High\n\r");
5hel2l2y 1:7562cc147e26 82 status = imu.begin(imu.G_SCALE_2000DPS, imu.A_SCALE_8G,
5hel2l2y 1:7562cc147e26 83 imu.G_ODR_1660, imu.A_ODR_6660);
5hel2l2y 0:d0291b4a856a 84 break;
5hel2l2y 0:d0291b4a856a 85 default:
5hel2l2y 3:a43f11f5378f 86 DEBUG("Default power\n\r");
5hel2l2y 3:a43f11f5378f 87 status = imu.begin();
5hel2l2y 0:d0291b4a856a 88 break;
5hel2l2y 0:d0291b4a856a 89 }
5hel2l2y 0:d0291b4a856a 90
5hel2l2y 1:7562cc147e26 91 pc.printf("LSM6DS3 WHO_AM_I's returned: 0x%X\r\n", status);
5hel2l2y 1:7562cc147e26 92 pc.printf("Should be 0x69\r\n");
5hel2l2y 0:d0291b4a856a 93
5hel2l2y 0:d0291b4a856a 94 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
5hel2l2y 0:d0291b4a856a 95 }
5hel2l2y 0:d0291b4a856a 96 }
5hel2l2y 0:d0291b4a856a 97
5hel2l2y 0:d0291b4a856a 98 void periodicCallback(void)
5hel2l2y 0:d0291b4a856a 99 {
5hel2l2y 5:e2ba3c39e3b3 100 ble.waitForEvent();
5hel2l2y 3:a43f11f5378f 101 ledAlive = !ledAlive;
5hel2l2y 0:d0291b4a856a 102 }
5hel2l2y 0:d0291b4a856a 103
5hel2l2y 2:874137e546ea 104 // Interrupt signal
5hel2l2y 3:a43f11f5378f 105 void flip(DigitalOut led) {
5hel2l2y 3:a43f11f5378f 106 led = !led;
5hel2l2y 3:a43f11f5378f 107 }
5hel2l2y 3:a43f11f5378f 108
5hel2l2y 3:a43f11f5378f 109 void flip3() {
5hel2l2y 3:a43f11f5378f 110 // pc.printf(" ++++ Data Change Triggered!! ++++ \n\r");
5hel2l2y 3:a43f11f5378f 111 flip(ledIntrO);
5hel2l2y 3:a43f11f5378f 112 }
5hel2l2y 3:a43f11f5378f 113
5hel2l2y 3:a43f11f5378f 114 void flip4() {
5hel2l2y 3:a43f11f5378f 115 // pc.printf(" ++++ G/X Triggered!! ++++ \n\r");
5hel2l2y 3:a43f11f5378f 116 flip(ledIntrG);
5hel2l2y 2:874137e546ea 117 }
5hel2l2y 2:874137e546ea 118
5hel2l2y 0:d0291b4a856a 119 int main(void)
5hel2l2y 0:d0291b4a856a 120 {
5hel2l2y 5:e2ba3c39e3b3 121 time_t rawtime = 0;
5hel2l2y 5:e2ba3c39e3b3 122
5hel2l2y 3:a43f11f5378f 123 // turn all LED off
5hel2l2y 3:a43f11f5378f 124 ledAlive = 1;
5hel2l2y 3:a43f11f5378f 125 ledIntrG = 1;
5hel2l2y 3:a43f11f5378f 126 ledIntrO = 1;
5hel2l2y 3:a43f11f5378f 127
5hel2l2y 0:d0291b4a856a 128 Ticker ticker;
5hel2l2y 0:d0291b4a856a 129 ticker.attach(periodicCallback, 1);
5hel2l2y 0:d0291b4a856a 130
5hel2l2y 0:d0291b4a856a 131 DEBUG("Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 132 pc.printf("PC: Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 133 ble.init();
5hel2l2y 0:d0291b4a856a 134 ble.onDisconnection(disconnectionCallback);
5hel2l2y 0:d0291b4a856a 135 ble.onDataWritten(onDataWritten);
5hel2l2y 0:d0291b4a856a 136
5hel2l2y 0:d0291b4a856a 137 /* setup advertising */
5hel2l2y 0:d0291b4a856a 138 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
5hel2l2y 0:d0291b4a856a 139 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
5hel2l2y 0:d0291b4a856a 140 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
5hel2l2y 0:d0291b4a856a 141 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
5hel2l2y 0:d0291b4a856a 142 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
5hel2l2y 0:d0291b4a856a 143 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
5hel2l2y 0:d0291b4a856a 144
5hel2l2y 0:d0291b4a856a 145 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
5hel2l2y 0:d0291b4a856a 146 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 147
5hel2l2y 0:d0291b4a856a 148 UARTService uartService(ble);
5hel2l2y 0:d0291b4a856a 149 uartServicePtr = &uartService;
5hel2l2y 2:874137e546ea 150
5hel2l2y 3:a43f11f5378f 151 // setup baud rate
5hel2l2y 2:874137e546ea 152 pc.baud(115200);
5hel2l2y 3:a43f11f5378f 153
5hel2l2y 3:a43f11f5378f 154 // setup Interrupt mode
5hel2l2y 3:a43f11f5378f 155 intrGXO.mode(PullUp);
5hel2l2y 3:a43f11f5378f 156 intrGX.mode(PullUp);
5hel2l2y 3:a43f11f5378f 157
5hel2l2y 3:a43f11f5378f 158 // status light will be lit based on sensor/data change
5hel2l2y 3:a43f11f5378f 159 intrGX.rise(&flip4);
5hel2l2y 3:a43f11f5378f 160 intrGXO.rise(&flip3);
5hel2l2y 0:d0291b4a856a 161
5hel2l2y 0:d0291b4a856a 162 while (true) {
5hel2l2y 5:e2ba3c39e3b3 163 rawtime = rtc.time();
5hel2l2y 5:e2ba3c39e3b3 164
5hel2l2y 2:874137e546ea 165 imu.readAccel();
5hel2l2y 2:874137e546ea 166 imu.readGyro();
5hel2l2y 2:874137e546ea 167 // no magnetometer available for this sensor
5hel2l2y 5:e2ba3c39e3b3 168 pc.printf("%d,%d,%d,%d,%d,%d,%d,0.000000,0.000000,0.000000\r\n", rawtime,imu.ax_raw,imu.ay_raw,imu.az_raw,imu.gx_raw,imu.gy_raw,imu.gz_raw);
5hel2l2y 5:e2ba3c39e3b3 169
5hel2l2y 3:a43f11f5378f 170 //imu.readIntr();
5hel2l2y 2:874137e546ea 171 // pc.printf("intr: %f\r\n", imu.intr);
5hel2l2y 3:a43f11f5378f 172 // if(imu.intr > 0) {
5hel2l2y 2:874137e546ea 173 // pc.printf(" == interrupted == \r\n");
5hel2l2y 3:a43f11f5378f 174 // flip(LED2);
5hel2l2y 3:a43f11f5378f 175 // }
5hel2l2y 0:d0291b4a856a 176 }
5hel2l2y 0:d0291b4a856a 177 }