Change LSM6DS3 power mode using nRF51-DK BLE.

Dependencies:   BLE_API LSM6DS3 mbed nRF51822 nrf51_rtc

Committer:
5hel2l2y
Date:
Fri Jul 08 21:26:38 2016 +0000
Revision:
4:46dd8065e05b
Parent:
3:a43f11f5378f
Child:
5:e2ba3c39e3b3
Modified power settings.

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 1:7562cc147e26 23 LSM6DS3 imu(p30, p7);
5hel2l2y 0:d0291b4a856a 24 Serial pc(p9, p11);
5hel2l2y 0:d0291b4a856a 25
5hel2l2y 0:d0291b4a856a 26 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
5hel2l2y 0:d0291b4a856a 27 * it will have an impact on code-size and power consumption. */
5hel2l2y 0:d0291b4a856a 28
5hel2l2y 0:d0291b4a856a 29 #if NEED_CONSOLE_OUTPUT
5hel2l2y 0:d0291b4a856a 30 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
5hel2l2y 0:d0291b4a856a 31 #else
5hel2l2y 0:d0291b4a856a 32 #define DEBUG(...) /* nothing */
5hel2l2y 0:d0291b4a856a 33 #endif /* #if NEED_CONSOLE_OUTPUT */
5hel2l2y 0:d0291b4a856a 34
5hel2l2y 0:d0291b4a856a 35 BLEDevice ble;
5hel2l2y 3:a43f11f5378f 36 // Status lights
5hel2l2y 3:a43f11f5378f 37 DigitalOut ledAlive(LED1);
5hel2l2y 3:a43f11f5378f 38 DigitalOut ledIntrG(LED3);
5hel2l2y 3:a43f11f5378f 39 DigitalOut ledIntrO(LED4);
5hel2l2y 3:a43f11f5378f 40 // Interrupt pin connection
5hel2l2y 3:a43f11f5378f 41 InterruptIn intrGX(p14); //INT1 (Gyro & Accel Interrupt)
5hel2l2y 3:a43f11f5378f 42 InterruptIn intrGXO(p13); //INT2 (Gyro & Accel Data Ready)
5hel2l2y 0:d0291b4a856a 43
5hel2l2y 0:d0291b4a856a 44 UARTService *uartServicePtr;
5hel2l2y 0:d0291b4a856a 45
5hel2l2y 0:d0291b4a856a 46 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
5hel2l2y 0:d0291b4a856a 47 {
5hel2l2y 0:d0291b4a856a 48 DEBUG("Disconnected!\n\r");
5hel2l2y 0:d0291b4a856a 49 DEBUG("Restarting the advertising process\n\r");
5hel2l2y 0:d0291b4a856a 50 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 51 }
5hel2l2y 0:d0291b4a856a 52
5hel2l2y 0:d0291b4a856a 53 void onDataWritten(const GattWriteCallbackParams *params)
5hel2l2y 0:d0291b4a856a 54 {
5hel2l2y 0:d0291b4a856a 55 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
5hel2l2y 0:d0291b4a856a 56 uint16_t bytesRead = params->len;
5hel2l2y 0:d0291b4a856a 57 uint8_t value = *(params->data);
5hel2l2y 0:d0291b4a856a 58 uint16_t status;
5hel2l2y 0:d0291b4a856a 59 DEBUG("received %u bytes\n\r", bytesRead);
5hel2l2y 0:d0291b4a856a 60 DEBUG("recevied %d data\n\r", value);
5hel2l2y 0:d0291b4a856a 61
5hel2l2y 0:d0291b4a856a 62 switch(value) {
5hel2l2y 0:d0291b4a856a 63 case 49:
5hel2l2y 0:d0291b4a856a 64 DEBUG("Off power\n\r");
5hel2l2y 1:7562cc147e26 65 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G,
5hel2l2y 1:7562cc147e26 66 imu.G_POWER_DOWN, imu.A_POWER_DOWN);
5hel2l2y 0:d0291b4a856a 67 break;
5hel2l2y 0:d0291b4a856a 68 case 50:
5hel2l2y 4:46dd8065e05b 69 DEBUG("Accelerometer Low, Gyro Off\n\r");
5hel2l2y 1:7562cc147e26 70 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G,
5hel2l2y 4:46dd8065e05b 71 imu.G_POWER_DOWN, imu.A_ODR_13);
5hel2l2y 0:d0291b4a856a 72 break;
5hel2l2y 0:d0291b4a856a 73 case 51:
5hel2l2y 4:46dd8065e05b 74 DEBUG("Accelerometer High, Gyro Off\n\r");
5hel2l2y 4:46dd8065e05b 75 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_8G,
5hel2l2y 4:46dd8065e05b 76 imu.G_POWER_DOWN, imu.A_ODR_6660);
5hel2l2y 4:46dd8065e05b 77 break;
5hel2l2y 4:46dd8065e05b 78 case 52:
5hel2l2y 4:46dd8065e05b 79 DEBUG("Accelerometer High, Gyro High\n\r");
5hel2l2y 1:7562cc147e26 80 status = imu.begin(imu.G_SCALE_2000DPS, imu.A_SCALE_8G,
5hel2l2y 1:7562cc147e26 81 imu.G_ODR_1660, imu.A_ODR_6660);
5hel2l2y 0:d0291b4a856a 82 break;
5hel2l2y 0:d0291b4a856a 83 default:
5hel2l2y 3:a43f11f5378f 84 DEBUG("Default power\n\r");
5hel2l2y 3:a43f11f5378f 85 status = imu.begin();
5hel2l2y 0:d0291b4a856a 86 break;
5hel2l2y 0:d0291b4a856a 87 }
5hel2l2y 0:d0291b4a856a 88
5hel2l2y 1:7562cc147e26 89 pc.printf("LSM6DS3 WHO_AM_I's returned: 0x%X\r\n", status);
5hel2l2y 1:7562cc147e26 90 pc.printf("Should be 0x69\r\n");
5hel2l2y 0:d0291b4a856a 91
5hel2l2y 0:d0291b4a856a 92 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
5hel2l2y 0:d0291b4a856a 93 }
5hel2l2y 0:d0291b4a856a 94 }
5hel2l2y 0:d0291b4a856a 95
5hel2l2y 0:d0291b4a856a 96 void periodicCallback(void)
5hel2l2y 0:d0291b4a856a 97 {
5hel2l2y 3:a43f11f5378f 98 ledAlive = !ledAlive;
5hel2l2y 0:d0291b4a856a 99 }
5hel2l2y 0:d0291b4a856a 100
5hel2l2y 2:874137e546ea 101 // Interrupt signal
5hel2l2y 3:a43f11f5378f 102 void flip(DigitalOut led) {
5hel2l2y 3:a43f11f5378f 103 led = !led;
5hel2l2y 3:a43f11f5378f 104 }
5hel2l2y 3:a43f11f5378f 105
5hel2l2y 3:a43f11f5378f 106 void flip3() {
5hel2l2y 3:a43f11f5378f 107 // pc.printf(" ++++ Data Change Triggered!! ++++ \n\r");
5hel2l2y 3:a43f11f5378f 108 flip(ledIntrO);
5hel2l2y 3:a43f11f5378f 109 }
5hel2l2y 3:a43f11f5378f 110
5hel2l2y 3:a43f11f5378f 111 void flip4() {
5hel2l2y 3:a43f11f5378f 112 // pc.printf(" ++++ G/X Triggered!! ++++ \n\r");
5hel2l2y 3:a43f11f5378f 113 flip(ledIntrG);
5hel2l2y 2:874137e546ea 114 }
5hel2l2y 2:874137e546ea 115
5hel2l2y 0:d0291b4a856a 116 int main(void)
5hel2l2y 0:d0291b4a856a 117 {
5hel2l2y 3:a43f11f5378f 118 // turn all LED off
5hel2l2y 3:a43f11f5378f 119 ledAlive = 1;
5hel2l2y 3:a43f11f5378f 120 ledIntrG = 1;
5hel2l2y 3:a43f11f5378f 121 ledIntrO = 1;
5hel2l2y 3:a43f11f5378f 122
5hel2l2y 0:d0291b4a856a 123 Ticker ticker;
5hel2l2y 0:d0291b4a856a 124 ticker.attach(periodicCallback, 1);
5hel2l2y 0:d0291b4a856a 125
5hel2l2y 0:d0291b4a856a 126 DEBUG("Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 127 pc.printf("PC: Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 128 ble.init();
5hel2l2y 0:d0291b4a856a 129 ble.onDisconnection(disconnectionCallback);
5hel2l2y 0:d0291b4a856a 130 ble.onDataWritten(onDataWritten);
5hel2l2y 0:d0291b4a856a 131
5hel2l2y 0:d0291b4a856a 132 /* setup advertising */
5hel2l2y 0:d0291b4a856a 133 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
5hel2l2y 0:d0291b4a856a 134 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
5hel2l2y 0:d0291b4a856a 135 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
5hel2l2y 0:d0291b4a856a 136 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
5hel2l2y 0:d0291b4a856a 137 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
5hel2l2y 0:d0291b4a856a 138 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
5hel2l2y 0:d0291b4a856a 139
5hel2l2y 0:d0291b4a856a 140 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
5hel2l2y 0:d0291b4a856a 141 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 142
5hel2l2y 0:d0291b4a856a 143 UARTService uartService(ble);
5hel2l2y 0:d0291b4a856a 144 uartServicePtr = &uartService;
5hel2l2y 2:874137e546ea 145
5hel2l2y 3:a43f11f5378f 146 // setup baud rate
5hel2l2y 2:874137e546ea 147 pc.baud(115200);
5hel2l2y 3:a43f11f5378f 148
5hel2l2y 3:a43f11f5378f 149 // setup Interrupt mode
5hel2l2y 3:a43f11f5378f 150 intrGXO.mode(PullUp);
5hel2l2y 3:a43f11f5378f 151 intrGX.mode(PullUp);
5hel2l2y 3:a43f11f5378f 152
5hel2l2y 3:a43f11f5378f 153 // status light will be lit based on sensor/data change
5hel2l2y 3:a43f11f5378f 154 intrGX.rise(&flip4);
5hel2l2y 3:a43f11f5378f 155 intrGXO.rise(&flip3);
5hel2l2y 0:d0291b4a856a 156
5hel2l2y 0:d0291b4a856a 157 while (true) {
5hel2l2y 0:d0291b4a856a 158 ble.waitForEvent();
5hel2l2y 2:874137e546ea 159
5hel2l2y 2:874137e546ea 160 imu.readAccel();
5hel2l2y 2:874137e546ea 161 pc.printf("%2f,%2f,%2f\r\n", imu.ax,imu.ay,imu.az);
5hel2l2y 2:874137e546ea 162 imu.readGyro();
5hel2l2y 2:874137e546ea 163 pc.printf("%2f,%2f,%2f\r\n", imu.gx,imu.gy,imu.gz);
5hel2l2y 2:874137e546ea 164 // no magnetometer available for this sensor
5hel2l2y 2:874137e546ea 165 pc.printf("0.000000,0.000000,0.000000\r\n");
5hel2l2y 3:a43f11f5378f 166 //imu.readIntr();
5hel2l2y 2:874137e546ea 167 // pc.printf("intr: %f\r\n", imu.intr);
5hel2l2y 3:a43f11f5378f 168 // if(imu.intr > 0) {
5hel2l2y 2:874137e546ea 169 // pc.printf(" == interrupted == \r\n");
5hel2l2y 3:a43f11f5378f 170 // flip(LED2);
5hel2l2y 3:a43f11f5378f 171 // }
5hel2l2y 2:874137e546ea 172
5hel2l2y 2:874137e546ea 173 wait(1.0);
5hel2l2y 0:d0291b4a856a 174 }
5hel2l2y 0:d0291b4a856a 175 }