Access different power mode of BNO055 via nRF51-DK BLE

Dependencies:   BLE_API BNO055 mbed nRF51822 nrf51_rtc

Fork of BLE_LoopbackUART_with_LSM6DS3 by Sherry Yang

Committer:
5hel2l2y
Date:
Mon Jul 11 21:29:17 2016 +0000
Revision:
5:cd414aaa44f6
Parent:
4:65f91237b687
Modified reading rate.

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 2:514af7294ad0 21 #include "BNO055/BNO055.h"
5hel2l2y 0:d0291b4a856a 22
5hel2l2y 5:cd414aaa44f6 23 #include "nrf51_rtc.h"
5hel2l2y 5:cd414aaa44f6 24
5hel2l2y 2:514af7294ad0 25 BNO055 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 4:65f91237b687 38 // Status lights
5hel2l2y 4:65f91237b687 39 DigitalOut ledAlive(LED1);
5hel2l2y 4:65f91237b687 40 DigitalOut ledIntrG(LED3);
5hel2l2y 4:65f91237b687 41 // Interrupt pin connection
5hel2l2y 4:65f91237b687 42 InterruptIn intrGX(p14); //INT (Gyro & Accel Interrupt)
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 3:c0d70871aa4d 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 DEBUG("received %u bytes\n\r", bytesRead);
5hel2l2y 0:d0291b4a856a 59 DEBUG("recevied %d data\n\r", value);
5hel2l2y 3:c0d70871aa4d 60
5hel2l2y 3:c0d70871aa4d 61 imu.reset();
5hel2l2y 0:d0291b4a856a 62
5hel2l2y 2:514af7294ad0 63 // Set power mode, POWER_MODE_NORMAL(default), POWER_MODE_LOWPOWER, or POWER_MODE_SUSPEND(sleep)
5hel2l2y 2:514af7294ad0 64 // By default, I2C will pull high when bus is free(pg90 of datasheet)
5hel2l2y 0:d0291b4a856a 65 switch(value) {
5hel2l2y 0:d0291b4a856a 66 case 49:
5hel2l2y 0:d0291b4a856a 67 DEBUG("Off power\n\r");
5hel2l2y 2:514af7294ad0 68 imu.setpowermode(POWER_MODE_SUSPEND);
5hel2l2y 0:d0291b4a856a 69 break;
5hel2l2y 0:d0291b4a856a 70 case 50:
5hel2l2y 5:cd414aaa44f6 71 DEBUG("Accelerometer Low, Gyro Off, Magnetometer Off\r");
5hel2l2y 2:514af7294ad0 72 imu.setpowermode(POWER_MODE_LOWPOWER);
5hel2l2y 0:d0291b4a856a 73 break;
5hel2l2y 0:d0291b4a856a 74 case 51:
5hel2l2y 5:cd414aaa44f6 75 DEBUG("Accelerometer High, Gyro Off\n\r");
5hel2l2y 5:cd414aaa44f6 76 // imu.setpowermode(POWER_MODE_NORMAL);
5hel2l2y 5:cd414aaa44f6 77 imu.setpowermode3();
5hel2l2y 5:cd414aaa44f6 78 break;
5hel2l2y 5:cd414aaa44f6 79 case 52:
5hel2l2y 5:cd414aaa44f6 80 DEBUG("Accelerometer High, Gyro High\n\r");
5hel2l2y 2:514af7294ad0 81 imu.setpowermode(POWER_MODE_NORMAL);
5hel2l2y 0:d0291b4a856a 82 break;
5hel2l2y 0:d0291b4a856a 83 default:
5hel2l2y 4:65f91237b687 84 DEBUG("Default power\n\r");
5hel2l2y 4:65f91237b687 85 imu.setpowermode(POWER_MODE_NORMAL);
5hel2l2y 0:d0291b4a856a 86 break;
5hel2l2y 0:d0291b4a856a 87 }
5hel2l2y 0:d0291b4a856a 88
5hel2l2y 2:514af7294ad0 89 if (imu.check())
5hel2l2y 2:514af7294ad0 90 {
5hel2l2y 4:65f91237b687 91 pc.printf("BNO055 WHO_AM_I's returned: 0x%X\r\n", imu.ID.id);
5hel2l2y 4:65f91237b687 92 pc.printf("Should be 0xA0\r\n");
5hel2l2y 4:65f91237b687 93 imu.initIntr();
5hel2l2y 2:514af7294ad0 94 }
5hel2l2y 0:d0291b4a856a 95
5hel2l2y 0:d0291b4a856a 96 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
5hel2l2y 0:d0291b4a856a 97 }
5hel2l2y 0:d0291b4a856a 98 }
5hel2l2y 0:d0291b4a856a 99
5hel2l2y 0:d0291b4a856a 100 void periodicCallback(void)
5hel2l2y 0:d0291b4a856a 101 {
5hel2l2y 5:cd414aaa44f6 102 ble.waitForEvent();
5hel2l2y 4:65f91237b687 103 ledAlive = !ledAlive;
5hel2l2y 0:d0291b4a856a 104 }
5hel2l2y 0:d0291b4a856a 105
5hel2l2y 3:c0d70871aa4d 106 // Interrupt signal
5hel2l2y 4:65f91237b687 107 void flip(DigitalOut led) {
5hel2l2y 4:65f91237b687 108 led = !led;
5hel2l2y 4:65f91237b687 109 }
5hel2l2y 4:65f91237b687 110
5hel2l2y 4:65f91237b687 111 void flip4() {
5hel2l2y 4:65f91237b687 112 // pc.printf(" ++++ G/X Triggered!! ++++ \n\r");
5hel2l2y 4:65f91237b687 113 flip(ledIntrG);
5hel2l2y 4:65f91237b687 114 imu.resetIntr();
5hel2l2y 3:c0d70871aa4d 115 }
5hel2l2y 3:c0d70871aa4d 116
5hel2l2y 0:d0291b4a856a 117 int main(void)
5hel2l2y 0:d0291b4a856a 118 {
5hel2l2y 5:cd414aaa44f6 119 time_t rawtime = 0;
5hel2l2y 5:cd414aaa44f6 120
5hel2l2y 4:65f91237b687 121 // turn all LED off
5hel2l2y 4:65f91237b687 122 ledAlive = 1;
5hel2l2y 4:65f91237b687 123 ledIntrG = 1;
5hel2l2y 4:65f91237b687 124
5hel2l2y 0:d0291b4a856a 125 Ticker ticker;
5hel2l2y 0:d0291b4a856a 126 ticker.attach(periodicCallback, 1);
5hel2l2y 0:d0291b4a856a 127
5hel2l2y 0:d0291b4a856a 128 DEBUG("Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 129 pc.printf("PC: Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 130 ble.init();
5hel2l2y 0:d0291b4a856a 131 ble.onDisconnection(disconnectionCallback);
5hel2l2y 0:d0291b4a856a 132 ble.onDataWritten(onDataWritten);
5hel2l2y 0:d0291b4a856a 133
5hel2l2y 0:d0291b4a856a 134 /* setup advertising */
5hel2l2y 0:d0291b4a856a 135 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
5hel2l2y 0:d0291b4a856a 136 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
5hel2l2y 0:d0291b4a856a 137 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
5hel2l2y 0:d0291b4a856a 138 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
5hel2l2y 0:d0291b4a856a 139 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
5hel2l2y 0:d0291b4a856a 140 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
5hel2l2y 0:d0291b4a856a 141
5hel2l2y 0:d0291b4a856a 142 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
5hel2l2y 0:d0291b4a856a 143 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 144
5hel2l2y 0:d0291b4a856a 145 UARTService uartService(ble);
5hel2l2y 0:d0291b4a856a 146 uartServicePtr = &uartService;
5hel2l2y 2:514af7294ad0 147
5hel2l2y 2:514af7294ad0 148 // setup baud rate and reset sensor
5hel2l2y 2:514af7294ad0 149 pc.baud(115200);
5hel2l2y 4:65f91237b687 150
5hel2l2y 4:65f91237b687 151 // setup Interrupt mode
5hel2l2y 4:65f91237b687 152 intrGX.mode(PullUp);
5hel2l2y 4:65f91237b687 153
5hel2l2y 4:65f91237b687 154 // status light will be lit based on sensor/data change
5hel2l2y 4:65f91237b687 155 intrGX.rise(&flip4);
5hel2l2y 0:d0291b4a856a 156
5hel2l2y 0:d0291b4a856a 157 while (true) {
5hel2l2y 5:cd414aaa44f6 158 rawtime = rtc.time();
5hel2l2y 3:c0d70871aa4d 159
5hel2l2y 3:c0d70871aa4d 160 imu.setmode(OPERATION_MODE_AMG);
5hel2l2y 5:cd414aaa44f6 161
5hel2l2y 3:c0d70871aa4d 162 imu.get_accel();
5hel2l2y 3:c0d70871aa4d 163 imu.get_gyro();
5hel2l2y 3:c0d70871aa4d 164 imu.get_mag();
5hel2l2y 5:cd414aaa44f6 165 pc.printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", rawtime,imu.accel.rawx,imu.accel.rawy,imu.accel.rawz,imu.gyro.rawx, imu.gyro.rawy, imu.gyro.rawz,imu.mag.rawx, imu.mag.rawy, imu.mag.rawz);
5hel2l2y 4:65f91237b687 166
5hel2l2y 3:c0d70871aa4d 167 // Interrupt stats: This is only enabled in Low Power Mode
5hel2l2y 3:c0d70871aa4d 168 // 0 - no interrupt
5hel2l2y 3:c0d70871aa4d 169 // 64 - interrupt
5hel2l2y 3:c0d70871aa4d 170 // 128 - sleep
5hel2l2y 4:65f91237b687 171 //imu.get_intr();
5hel2l2y 4:65f91237b687 172 // pc.printf("intr: %d\r\n", imu.intr);
5hel2l2y 4:65f91237b687 173 // if(imu.intr == 64) {
5hel2l2y 4:65f91237b687 174 // pc.printf(" == interrupted == \r\n");
5hel2l2y 4:65f91237b687 175 // imu.resetIntr();
5hel2l2y 4:65f91237b687 176 // pc.printf("rx before: %d\r\n", imu.test3);
5hel2l2y 4:65f91237b687 177 // pc.printf("rx after: %d\r\n", imu.test4);
5hel2l2y 4:65f91237b687 178 // }
5hel2l2y 0:d0291b4a856a 179 }
5hel2l2y 0:d0291b4a856a 180 }