![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Change LSM6DS3 power mode using nRF51-DK BLE.
Dependencies: BLE_API LSM6DS3 mbed nRF51822 nrf51_rtc
main.cpp@3:a43f11f5378f, 2016-06-22 (annotated)
- Committer:
- 5hel2l2y
- Date:
- Wed Jun 22 22:32:58 2016 +0000
- Revision:
- 3:a43f11f5378f
- Parent:
- 2:874137e546ea
- Child:
- 4:46dd8065e05b
Modified to trigger interrupt on board.
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 | 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 | 0:d0291b4a856a | 69 | DEBUG("Low power\n\r"); |
5hel2l2y | 1:7562cc147e26 | 70 | status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G, |
5hel2l2y | 1:7562cc147e26 | 71 | imu.G_ODR_13_BW_0, imu.A_ODR_13); |
5hel2l2y | 0:d0291b4a856a | 72 | break; |
5hel2l2y | 0:d0291b4a856a | 73 | case 51: |
5hel2l2y | 0:d0291b4a856a | 74 | DEBUG("High power\n\r"); |
5hel2l2y | 1:7562cc147e26 | 75 | status = imu.begin(imu.G_SCALE_2000DPS, imu.A_SCALE_8G, |
5hel2l2y | 1:7562cc147e26 | 76 | imu.G_ODR_1660, imu.A_ODR_6660); |
5hel2l2y | 0:d0291b4a856a | 77 | break; |
5hel2l2y | 0:d0291b4a856a | 78 | default: |
5hel2l2y | 3:a43f11f5378f | 79 | DEBUG("Default power\n\r"); |
5hel2l2y | 3:a43f11f5378f | 80 | status = imu.begin(); |
5hel2l2y | 0:d0291b4a856a | 81 | break; |
5hel2l2y | 0:d0291b4a856a | 82 | } |
5hel2l2y | 0:d0291b4a856a | 83 | |
5hel2l2y | 1:7562cc147e26 | 84 | pc.printf("LSM6DS3 WHO_AM_I's returned: 0x%X\r\n", status); |
5hel2l2y | 1:7562cc147e26 | 85 | pc.printf("Should be 0x69\r\n"); |
5hel2l2y | 0:d0291b4a856a | 86 | |
5hel2l2y | 0:d0291b4a856a | 87 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
5hel2l2y | 0:d0291b4a856a | 88 | } |
5hel2l2y | 0:d0291b4a856a | 89 | } |
5hel2l2y | 0:d0291b4a856a | 90 | |
5hel2l2y | 0:d0291b4a856a | 91 | void periodicCallback(void) |
5hel2l2y | 0:d0291b4a856a | 92 | { |
5hel2l2y | 3:a43f11f5378f | 93 | ledAlive = !ledAlive; |
5hel2l2y | 0:d0291b4a856a | 94 | } |
5hel2l2y | 0:d0291b4a856a | 95 | |
5hel2l2y | 2:874137e546ea | 96 | // Interrupt signal |
5hel2l2y | 3:a43f11f5378f | 97 | void flip(DigitalOut led) { |
5hel2l2y | 3:a43f11f5378f | 98 | led = !led; |
5hel2l2y | 3:a43f11f5378f | 99 | } |
5hel2l2y | 3:a43f11f5378f | 100 | |
5hel2l2y | 3:a43f11f5378f | 101 | void flip3() { |
5hel2l2y | 3:a43f11f5378f | 102 | // pc.printf(" ++++ Data Change Triggered!! ++++ \n\r"); |
5hel2l2y | 3:a43f11f5378f | 103 | flip(ledIntrO); |
5hel2l2y | 3:a43f11f5378f | 104 | } |
5hel2l2y | 3:a43f11f5378f | 105 | |
5hel2l2y | 3:a43f11f5378f | 106 | void flip4() { |
5hel2l2y | 3:a43f11f5378f | 107 | // pc.printf(" ++++ G/X Triggered!! ++++ \n\r"); |
5hel2l2y | 3:a43f11f5378f | 108 | flip(ledIntrG); |
5hel2l2y | 2:874137e546ea | 109 | } |
5hel2l2y | 2:874137e546ea | 110 | |
5hel2l2y | 0:d0291b4a856a | 111 | int main(void) |
5hel2l2y | 0:d0291b4a856a | 112 | { |
5hel2l2y | 3:a43f11f5378f | 113 | // turn all LED off |
5hel2l2y | 3:a43f11f5378f | 114 | ledAlive = 1; |
5hel2l2y | 3:a43f11f5378f | 115 | ledIntrG = 1; |
5hel2l2y | 3:a43f11f5378f | 116 | ledIntrO = 1; |
5hel2l2y | 3:a43f11f5378f | 117 | |
5hel2l2y | 0:d0291b4a856a | 118 | Ticker ticker; |
5hel2l2y | 0:d0291b4a856a | 119 | ticker.attach(periodicCallback, 1); |
5hel2l2y | 0:d0291b4a856a | 120 | |
5hel2l2y | 0:d0291b4a856a | 121 | DEBUG("Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 122 | pc.printf("PC: Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 123 | ble.init(); |
5hel2l2y | 0:d0291b4a856a | 124 | ble.onDisconnection(disconnectionCallback); |
5hel2l2y | 0:d0291b4a856a | 125 | ble.onDataWritten(onDataWritten); |
5hel2l2y | 0:d0291b4a856a | 126 | |
5hel2l2y | 0:d0291b4a856a | 127 | /* setup advertising */ |
5hel2l2y | 0:d0291b4a856a | 128 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
5hel2l2y | 0:d0291b4a856a | 129 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
5hel2l2y | 0:d0291b4a856a | 130 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
5hel2l2y | 0:d0291b4a856a | 131 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
5hel2l2y | 0:d0291b4a856a | 132 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
5hel2l2y | 0:d0291b4a856a | 133 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
5hel2l2y | 0:d0291b4a856a | 134 | |
5hel2l2y | 0:d0291b4a856a | 135 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
5hel2l2y | 0:d0291b4a856a | 136 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 137 | |
5hel2l2y | 0:d0291b4a856a | 138 | UARTService uartService(ble); |
5hel2l2y | 0:d0291b4a856a | 139 | uartServicePtr = &uartService; |
5hel2l2y | 2:874137e546ea | 140 | |
5hel2l2y | 3:a43f11f5378f | 141 | // setup baud rate |
5hel2l2y | 2:874137e546ea | 142 | pc.baud(115200); |
5hel2l2y | 3:a43f11f5378f | 143 | |
5hel2l2y | 3:a43f11f5378f | 144 | // setup Interrupt mode |
5hel2l2y | 3:a43f11f5378f | 145 | intrGXO.mode(PullUp); |
5hel2l2y | 3:a43f11f5378f | 146 | intrGX.mode(PullUp); |
5hel2l2y | 3:a43f11f5378f | 147 | |
5hel2l2y | 3:a43f11f5378f | 148 | // status light will be lit based on sensor/data change |
5hel2l2y | 3:a43f11f5378f | 149 | intrGX.rise(&flip4); |
5hel2l2y | 3:a43f11f5378f | 150 | intrGXO.rise(&flip3); |
5hel2l2y | 0:d0291b4a856a | 151 | |
5hel2l2y | 0:d0291b4a856a | 152 | while (true) { |
5hel2l2y | 0:d0291b4a856a | 153 | ble.waitForEvent(); |
5hel2l2y | 2:874137e546ea | 154 | |
5hel2l2y | 2:874137e546ea | 155 | imu.readAccel(); |
5hel2l2y | 2:874137e546ea | 156 | pc.printf("%2f,%2f,%2f\r\n", imu.ax,imu.ay,imu.az); |
5hel2l2y | 2:874137e546ea | 157 | imu.readGyro(); |
5hel2l2y | 2:874137e546ea | 158 | pc.printf("%2f,%2f,%2f\r\n", imu.gx,imu.gy,imu.gz); |
5hel2l2y | 2:874137e546ea | 159 | // no magnetometer available for this sensor |
5hel2l2y | 2:874137e546ea | 160 | pc.printf("0.000000,0.000000,0.000000\r\n"); |
5hel2l2y | 3:a43f11f5378f | 161 | //imu.readIntr(); |
5hel2l2y | 2:874137e546ea | 162 | // pc.printf("intr: %f\r\n", imu.intr); |
5hel2l2y | 3:a43f11f5378f | 163 | // if(imu.intr > 0) { |
5hel2l2y | 2:874137e546ea | 164 | // pc.printf(" == interrupted == \r\n"); |
5hel2l2y | 3:a43f11f5378f | 165 | // flip(LED2); |
5hel2l2y | 3:a43f11f5378f | 166 | // } |
5hel2l2y | 2:874137e546ea | 167 | |
5hel2l2y | 2:874137e546ea | 168 | wait(1.0); |
5hel2l2y | 0:d0291b4a856a | 169 | } |
5hel2l2y | 0:d0291b4a856a | 170 | } |