![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp@4:65f91237b687, 2016-06-23 (annotated)
- Committer:
- 5hel2l2y
- Date:
- Thu Jun 23 22:29:04 2016 +0000
- Revision:
- 4:65f91237b687
- Parent:
- 3:c0d70871aa4d
- Child:
- 5:cd414aaa44f6
Added interrupts on gyroscope.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:514af7294ad0 | 23 | BNO055 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 | 4:65f91237b687 | 36 | // Status lights |
5hel2l2y | 4:65f91237b687 | 37 | DigitalOut ledAlive(LED1); |
5hel2l2y | 4:65f91237b687 | 38 | DigitalOut ledIntrG(LED3); |
5hel2l2y | 4:65f91237b687 | 39 | // Interrupt pin connection |
5hel2l2y | 4:65f91237b687 | 40 | InterruptIn intrGX(p14); //INT (Gyro & Accel Interrupt) |
5hel2l2y | 0:d0291b4a856a | 41 | |
5hel2l2y | 0:d0291b4a856a | 42 | UARTService *uartServicePtr; |
5hel2l2y | 0:d0291b4a856a | 43 | |
5hel2l2y | 0:d0291b4a856a | 44 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
5hel2l2y | 0:d0291b4a856a | 45 | { |
5hel2l2y | 0:d0291b4a856a | 46 | DEBUG("Disconnected!\n\r"); |
5hel2l2y | 0:d0291b4a856a | 47 | DEBUG("Restarting the advertising process\n\r"); |
5hel2l2y | 0:d0291b4a856a | 48 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 49 | } |
5hel2l2y | 0:d0291b4a856a | 50 | |
5hel2l2y | 0:d0291b4a856a | 51 | void onDataWritten(const GattWriteCallbackParams *params) |
5hel2l2y | 3:c0d70871aa4d | 52 | { |
5hel2l2y | 0:d0291b4a856a | 53 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
5hel2l2y | 0:d0291b4a856a | 54 | uint16_t bytesRead = params->len; |
5hel2l2y | 0:d0291b4a856a | 55 | uint8_t value = *(params->data); |
5hel2l2y | 0:d0291b4a856a | 56 | DEBUG("received %u bytes\n\r", bytesRead); |
5hel2l2y | 0:d0291b4a856a | 57 | DEBUG("recevied %d data\n\r", value); |
5hel2l2y | 3:c0d70871aa4d | 58 | |
5hel2l2y | 3:c0d70871aa4d | 59 | imu.reset(); |
5hel2l2y | 0:d0291b4a856a | 60 | |
5hel2l2y | 2:514af7294ad0 | 61 | // Set power mode, POWER_MODE_NORMAL(default), POWER_MODE_LOWPOWER, or POWER_MODE_SUSPEND(sleep) |
5hel2l2y | 2:514af7294ad0 | 62 | // By default, I2C will pull high when bus is free(pg90 of datasheet) |
5hel2l2y | 0:d0291b4a856a | 63 | switch(value) { |
5hel2l2y | 0:d0291b4a856a | 64 | case 49: |
5hel2l2y | 0:d0291b4a856a | 65 | DEBUG("Off power\n\r"); |
5hel2l2y | 2:514af7294ad0 | 66 | imu.setpowermode(POWER_MODE_SUSPEND); |
5hel2l2y | 0:d0291b4a856a | 67 | break; |
5hel2l2y | 0:d0291b4a856a | 68 | case 50: |
5hel2l2y | 0:d0291b4a856a | 69 | DEBUG("Low power\n\r"); |
5hel2l2y | 2:514af7294ad0 | 70 | imu.setpowermode(POWER_MODE_LOWPOWER); |
5hel2l2y | 0:d0291b4a856a | 71 | break; |
5hel2l2y | 0:d0291b4a856a | 72 | case 51: |
5hel2l2y | 4:65f91237b687 | 73 | DEBUG("No high power available for this sensor\n\r"); |
5hel2l2y | 2:514af7294ad0 | 74 | imu.setpowermode(POWER_MODE_NORMAL); |
5hel2l2y | 0:d0291b4a856a | 75 | break; |
5hel2l2y | 0:d0291b4a856a | 76 | default: |
5hel2l2y | 4:65f91237b687 | 77 | DEBUG("Default power\n\r"); |
5hel2l2y | 4:65f91237b687 | 78 | imu.setpowermode(POWER_MODE_NORMAL); |
5hel2l2y | 0:d0291b4a856a | 79 | break; |
5hel2l2y | 0:d0291b4a856a | 80 | } |
5hel2l2y | 0:d0291b4a856a | 81 | |
5hel2l2y | 2:514af7294ad0 | 82 | if (imu.check()) |
5hel2l2y | 2:514af7294ad0 | 83 | { |
5hel2l2y | 4:65f91237b687 | 84 | pc.printf("BNO055 WHO_AM_I's returned: 0x%X\r\n", imu.ID.id); |
5hel2l2y | 4:65f91237b687 | 85 | pc.printf("Should be 0xA0\r\n"); |
5hel2l2y | 4:65f91237b687 | 86 | imu.initIntr(); |
5hel2l2y | 2:514af7294ad0 | 87 | } |
5hel2l2y | 0:d0291b4a856a | 88 | |
5hel2l2y | 0:d0291b4a856a | 89 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
5hel2l2y | 0:d0291b4a856a | 90 | } |
5hel2l2y | 0:d0291b4a856a | 91 | } |
5hel2l2y | 0:d0291b4a856a | 92 | |
5hel2l2y | 0:d0291b4a856a | 93 | void periodicCallback(void) |
5hel2l2y | 0:d0291b4a856a | 94 | { |
5hel2l2y | 4:65f91237b687 | 95 | ledAlive = !ledAlive; |
5hel2l2y | 0:d0291b4a856a | 96 | } |
5hel2l2y | 0:d0291b4a856a | 97 | |
5hel2l2y | 3:c0d70871aa4d | 98 | // Interrupt signal |
5hel2l2y | 4:65f91237b687 | 99 | void flip(DigitalOut led) { |
5hel2l2y | 4:65f91237b687 | 100 | led = !led; |
5hel2l2y | 4:65f91237b687 | 101 | } |
5hel2l2y | 4:65f91237b687 | 102 | |
5hel2l2y | 4:65f91237b687 | 103 | void flip4() { |
5hel2l2y | 4:65f91237b687 | 104 | // pc.printf(" ++++ G/X Triggered!! ++++ \n\r"); |
5hel2l2y | 4:65f91237b687 | 105 | flip(ledIntrG); |
5hel2l2y | 4:65f91237b687 | 106 | imu.resetIntr(); |
5hel2l2y | 3:c0d70871aa4d | 107 | } |
5hel2l2y | 3:c0d70871aa4d | 108 | |
5hel2l2y | 0:d0291b4a856a | 109 | int main(void) |
5hel2l2y | 0:d0291b4a856a | 110 | { |
5hel2l2y | 4:65f91237b687 | 111 | // turn all LED off |
5hel2l2y | 4:65f91237b687 | 112 | ledAlive = 1; |
5hel2l2y | 4:65f91237b687 | 113 | ledIntrG = 1; |
5hel2l2y | 4:65f91237b687 | 114 | |
5hel2l2y | 0:d0291b4a856a | 115 | Ticker ticker; |
5hel2l2y | 0:d0291b4a856a | 116 | ticker.attach(periodicCallback, 1); |
5hel2l2y | 0:d0291b4a856a | 117 | |
5hel2l2y | 0:d0291b4a856a | 118 | DEBUG("Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 119 | pc.printf("PC: Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 120 | ble.init(); |
5hel2l2y | 0:d0291b4a856a | 121 | ble.onDisconnection(disconnectionCallback); |
5hel2l2y | 0:d0291b4a856a | 122 | ble.onDataWritten(onDataWritten); |
5hel2l2y | 0:d0291b4a856a | 123 | |
5hel2l2y | 0:d0291b4a856a | 124 | /* setup advertising */ |
5hel2l2y | 0:d0291b4a856a | 125 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
5hel2l2y | 0:d0291b4a856a | 126 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
5hel2l2y | 0:d0291b4a856a | 127 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
5hel2l2y | 0:d0291b4a856a | 128 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
5hel2l2y | 0:d0291b4a856a | 129 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
5hel2l2y | 0:d0291b4a856a | 130 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
5hel2l2y | 0:d0291b4a856a | 131 | |
5hel2l2y | 0:d0291b4a856a | 132 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
5hel2l2y | 0:d0291b4a856a | 133 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 134 | |
5hel2l2y | 0:d0291b4a856a | 135 | UARTService uartService(ble); |
5hel2l2y | 0:d0291b4a856a | 136 | uartServicePtr = &uartService; |
5hel2l2y | 2:514af7294ad0 | 137 | |
5hel2l2y | 2:514af7294ad0 | 138 | // setup baud rate and reset sensor |
5hel2l2y | 2:514af7294ad0 | 139 | pc.baud(115200); |
5hel2l2y | 4:65f91237b687 | 140 | |
5hel2l2y | 4:65f91237b687 | 141 | // setup Interrupt mode |
5hel2l2y | 4:65f91237b687 | 142 | intrGX.mode(PullUp); |
5hel2l2y | 4:65f91237b687 | 143 | |
5hel2l2y | 4:65f91237b687 | 144 | // status light will be lit based on sensor/data change |
5hel2l2y | 4:65f91237b687 | 145 | intrGX.rise(&flip4); |
5hel2l2y | 0:d0291b4a856a | 146 | |
5hel2l2y | 0:d0291b4a856a | 147 | while (true) { |
5hel2l2y | 0:d0291b4a856a | 148 | ble.waitForEvent(); |
5hel2l2y | 3:c0d70871aa4d | 149 | |
5hel2l2y | 3:c0d70871aa4d | 150 | imu.setmode(OPERATION_MODE_AMG); |
5hel2l2y | 3:c0d70871aa4d | 151 | imu.get_accel(); |
5hel2l2y | 3:c0d70871aa4d | 152 | pc.printf("%2f,%2f,%2f\r\n", imu.accel.x, imu.accel.y, imu.accel.z); |
5hel2l2y | 3:c0d70871aa4d | 153 | imu.get_gyro(); |
5hel2l2y | 3:c0d70871aa4d | 154 | pc.printf("%2f,%2f,%2f\r\n", imu.gyro.x, imu.gyro.y, imu.gyro.z); |
5hel2l2y | 3:c0d70871aa4d | 155 | imu.get_mag(); |
5hel2l2y | 3:c0d70871aa4d | 156 | pc.printf("%2f,%2f,%2f\r\n", imu.mag.x, imu.mag.y, imu.mag.z); |
5hel2l2y | 4:65f91237b687 | 157 | |
5hel2l2y | 3:c0d70871aa4d | 158 | // Interrupt stats: This is only enabled in Low Power Mode |
5hel2l2y | 3:c0d70871aa4d | 159 | // 0 - no interrupt |
5hel2l2y | 3:c0d70871aa4d | 160 | // 64 - interrupt |
5hel2l2y | 3:c0d70871aa4d | 161 | // 128 - sleep |
5hel2l2y | 4:65f91237b687 | 162 | //imu.get_intr(); |
5hel2l2y | 4:65f91237b687 | 163 | // pc.printf("intr: %d\r\n", imu.intr); |
5hel2l2y | 4:65f91237b687 | 164 | // if(imu.intr == 64) { |
5hel2l2y | 4:65f91237b687 | 165 | // pc.printf(" == interrupted == \r\n"); |
5hel2l2y | 4:65f91237b687 | 166 | // imu.resetIntr(); |
5hel2l2y | 4:65f91237b687 | 167 | // pc.printf("rx before: %d\r\n", imu.test3); |
5hel2l2y | 4:65f91237b687 | 168 | // pc.printf("rx after: %d\r\n", imu.test4); |
5hel2l2y | 4:65f91237b687 | 169 | // } |
5hel2l2y | 3:c0d70871aa4d | 170 | |
5hel2l2y | 3:c0d70871aa4d | 171 | wait(1.0); |
5hel2l2y | 0:d0291b4a856a | 172 | } |
5hel2l2y | 0:d0291b4a856a | 173 | } |