Change LSM6DS3 power mode using nRF51-DK BLE.

Dependencies:   BLE_API LSM6DS3 mbed nRF51822 nrf51_rtc

Committer:
5hel2l2y
Date:
Fri Jun 17 22:14:12 2016 +0000
Revision:
0:d0291b4a856a
Child:
1:7562cc147e26
Modified to communicate with LSM9DS1 sensor

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 0:d0291b4a856a 21 #include "LSM9DS1/LSM9DS1.h"
5hel2l2y 0:d0291b4a856a 22
5hel2l2y 0:d0291b4a856a 23 LSM9DS1 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 0:d0291b4a856a 36 DigitalOut led1(LED1);
5hel2l2y 0:d0291b4a856a 37
5hel2l2y 0:d0291b4a856a 38 UARTService *uartServicePtr;
5hel2l2y 0:d0291b4a856a 39
5hel2l2y 0:d0291b4a856a 40 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
5hel2l2y 0:d0291b4a856a 41 {
5hel2l2y 0:d0291b4a856a 42 DEBUG("Disconnected!\n\r");
5hel2l2y 0:d0291b4a856a 43 DEBUG("Restarting the advertising process\n\r");
5hel2l2y 0:d0291b4a856a 44 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 45 }
5hel2l2y 0:d0291b4a856a 46
5hel2l2y 0:d0291b4a856a 47 void onDataWritten(const GattWriteCallbackParams *params)
5hel2l2y 0:d0291b4a856a 48 {
5hel2l2y 0:d0291b4a856a 49 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
5hel2l2y 0:d0291b4a856a 50 uint16_t bytesRead = params->len;
5hel2l2y 0:d0291b4a856a 51 uint8_t value = *(params->data);
5hel2l2y 0:d0291b4a856a 52 uint16_t status;
5hel2l2y 0:d0291b4a856a 53 DEBUG("received %u bytes\n\r", bytesRead);
5hel2l2y 0:d0291b4a856a 54 DEBUG("recevied %d data\n\r", value);
5hel2l2y 0:d0291b4a856a 55
5hel2l2y 0:d0291b4a856a 56 switch(value) {
5hel2l2y 0:d0291b4a856a 57 case 49:
5hel2l2y 0:d0291b4a856a 58 DEBUG("Off power\n\r");
5hel2l2y 0:d0291b4a856a 59 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G, imu.M_SCALE_4GS,
5hel2l2y 0:d0291b4a856a 60 imu.G_POWER_DOWN, imu.A_POWER_DOWN, imu.M_ODR_0625);
5hel2l2y 0:d0291b4a856a 61 break;
5hel2l2y 0:d0291b4a856a 62 case 50:
5hel2l2y 0:d0291b4a856a 63 DEBUG("Low power\n\r");
5hel2l2y 0:d0291b4a856a 64 status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G, imu.M_SCALE_4GS,
5hel2l2y 0:d0291b4a856a 65 imu.G_ODR_15_BW_0, imu.A_POWER_DOWN, imu.M_ODR_0625);
5hel2l2y 0:d0291b4a856a 66 break;
5hel2l2y 0:d0291b4a856a 67 case 51:
5hel2l2y 0:d0291b4a856a 68 DEBUG("High power\n\r");
5hel2l2y 0:d0291b4a856a 69 status = imu.begin(imu.G_SCALE_2000DPS, imu.A_SCALE_8G, imu.M_SCALE_16GS,
5hel2l2y 0:d0291b4a856a 70 imu.G_ODR_952_BW_100, imu.A_ODR_952, imu.M_ODR_80);
5hel2l2y 0:d0291b4a856a 71 break;
5hel2l2y 0:d0291b4a856a 72 default:
5hel2l2y 0:d0291b4a856a 73 DEBUG("Nothing happened\n\r");
5hel2l2y 0:d0291b4a856a 74 break;
5hel2l2y 0:d0291b4a856a 75 }
5hel2l2y 0:d0291b4a856a 76
5hel2l2y 0:d0291b4a856a 77 pc.printf("LSM9DS1 WHO_AM_I's returned: 0x%X\r\n", status);
5hel2l2y 0:d0291b4a856a 78 pc.printf("Should be 0x683D\r\n");
5hel2l2y 0:d0291b4a856a 79
5hel2l2y 0:d0291b4a856a 80 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
5hel2l2y 0:d0291b4a856a 81 }
5hel2l2y 0:d0291b4a856a 82 }
5hel2l2y 0:d0291b4a856a 83
5hel2l2y 0:d0291b4a856a 84 void periodicCallback(void)
5hel2l2y 0:d0291b4a856a 85 {
5hel2l2y 0:d0291b4a856a 86 led1 = !led1;
5hel2l2y 0:d0291b4a856a 87 }
5hel2l2y 0:d0291b4a856a 88
5hel2l2y 0:d0291b4a856a 89 int main(void)
5hel2l2y 0:d0291b4a856a 90 {
5hel2l2y 0:d0291b4a856a 91 led1 = 1;
5hel2l2y 0:d0291b4a856a 92 Ticker ticker;
5hel2l2y 0:d0291b4a856a 93 ticker.attach(periodicCallback, 1);
5hel2l2y 0:d0291b4a856a 94
5hel2l2y 0:d0291b4a856a 95 DEBUG("Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 96 pc.printf("PC: Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 97 ble.init();
5hel2l2y 0:d0291b4a856a 98 ble.onDisconnection(disconnectionCallback);
5hel2l2y 0:d0291b4a856a 99 ble.onDataWritten(onDataWritten);
5hel2l2y 0:d0291b4a856a 100
5hel2l2y 0:d0291b4a856a 101 /* setup advertising */
5hel2l2y 0:d0291b4a856a 102 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
5hel2l2y 0:d0291b4a856a 103 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
5hel2l2y 0:d0291b4a856a 104 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
5hel2l2y 0:d0291b4a856a 105 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
5hel2l2y 0:d0291b4a856a 106 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
5hel2l2y 0:d0291b4a856a 107 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
5hel2l2y 0:d0291b4a856a 108
5hel2l2y 0:d0291b4a856a 109 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
5hel2l2y 0:d0291b4a856a 110 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 111
5hel2l2y 0:d0291b4a856a 112 UARTService uartService(ble);
5hel2l2y 0:d0291b4a856a 113 uartServicePtr = &uartService;
5hel2l2y 0:d0291b4a856a 114
5hel2l2y 0:d0291b4a856a 115 while (true) {
5hel2l2y 0:d0291b4a856a 116 ble.waitForEvent();
5hel2l2y 0:d0291b4a856a 117 }
5hel2l2y 0:d0291b4a856a 118 }