Another commit for the interrupt added. Renamed the main branch screwed up somethings..
Dependencies: BLE_API LSM9DS1 mbed nRF51822 nrf51_rtc
main.cpp@4:89be9162c6aa, 2016-07-08 (annotated)
- Committer:
- 5hel2l2y
- Date:
- Fri Jul 08 21:15:16 2016 +0000
- Revision:
- 4:89be9162c6aa
- Parent:
- 3:db2db5c9611f
- Child:
- 5:b0e3d5d682b0
Modified power modes.
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 | 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 | 3:db2db5c9611f | 36 | // Status lights |
5hel2l2y | 3:db2db5c9611f | 37 | DigitalOut ledAlive(LED1); |
5hel2l2y | 3:db2db5c9611f | 38 | DigitalOut ledIntrM(LED2); |
5hel2l2y | 3:db2db5c9611f | 39 | DigitalOut ledIntrG(LED3); |
5hel2l2y | 3:db2db5c9611f | 40 | DigitalOut ledIntrO(LED4); |
5hel2l2y | 3:db2db5c9611f | 41 | // Interrupt pin connection |
5hel2l2y | 3:db2db5c9611f | 42 | InterruptIn intrMO(p16); //RDY |
5hel2l2y | 3:db2db5c9611f | 43 | InterruptIn intrM(p15); //INTM |
5hel2l2y | 3:db2db5c9611f | 44 | InterruptIn intrGX(p14); //INT1 (Gyro & Accel Interrupt) |
5hel2l2y | 3:db2db5c9611f | 45 | InterruptIn intrGXO(p13); //INT2 (Gyro & Accel Data Ready) |
5hel2l2y | 0:d0291b4a856a | 46 | |
5hel2l2y | 0:d0291b4a856a | 47 | UARTService *uartServicePtr; |
5hel2l2y | 0:d0291b4a856a | 48 | |
5hel2l2y | 0:d0291b4a856a | 49 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
5hel2l2y | 0:d0291b4a856a | 50 | { |
5hel2l2y | 0:d0291b4a856a | 51 | DEBUG("Disconnected!\n\r"); |
5hel2l2y | 0:d0291b4a856a | 52 | DEBUG("Restarting the advertising process\n\r"); |
5hel2l2y | 0:d0291b4a856a | 53 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 54 | } |
5hel2l2y | 0:d0291b4a856a | 55 | |
5hel2l2y | 0:d0291b4a856a | 56 | void onDataWritten(const GattWriteCallbackParams *params) |
5hel2l2y | 0:d0291b4a856a | 57 | { |
5hel2l2y | 0:d0291b4a856a | 58 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
5hel2l2y | 0:d0291b4a856a | 59 | uint16_t bytesRead = params->len; |
5hel2l2y | 0:d0291b4a856a | 60 | uint8_t value = *(params->data); |
5hel2l2y | 0:d0291b4a856a | 61 | uint16_t status; |
5hel2l2y | 0:d0291b4a856a | 62 | DEBUG("received %u bytes\n\r", bytesRead); |
5hel2l2y | 0:d0291b4a856a | 63 | DEBUG("recevied %d data\n\r", value); |
5hel2l2y | 0:d0291b4a856a | 64 | |
5hel2l2y | 0:d0291b4a856a | 65 | switch(value) { |
5hel2l2y | 0:d0291b4a856a | 66 | case 49: |
5hel2l2y | 0:d0291b4a856a | 67 | DEBUG("Off power\n\r"); |
5hel2l2y | 0:d0291b4a856a | 68 | status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G, imu.M_SCALE_4GS, |
5hel2l2y | 3:db2db5c9611f | 69 | imu.G_POWER_DOWN, imu.A_POWER_DOWN, imu.M_ODR_0625); |
5hel2l2y | 0:d0291b4a856a | 70 | break; |
5hel2l2y | 0:d0291b4a856a | 71 | case 50: |
5hel2l2y | 4:89be9162c6aa | 72 | DEBUG("Accelerometer Low, Gyro Off, Magnetometer Low\n\r"); |
5hel2l2y | 0:d0291b4a856a | 73 | status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_2G, imu.M_SCALE_4GS, |
5hel2l2y | 4:89be9162c6aa | 74 | imu.G_POWER_DOWN, imu.A_ODR_10, imu.M_ODR_0625); |
5hel2l2y | 0:d0291b4a856a | 75 | break; |
5hel2l2y | 0:d0291b4a856a | 76 | case 51: |
5hel2l2y | 4:89be9162c6aa | 77 | DEBUG("Accelerometer High, Gyro Off, Magnetometer Low\n\r"); |
5hel2l2y | 4:89be9162c6aa | 78 | status = imu.begin(imu.G_SCALE_245DPS, imu.A_SCALE_8G, imu.M_SCALE_4GS, |
5hel2l2y | 4:89be9162c6aa | 79 | imu.G_POWER_DOWN, imu.A_ODR_952, imu.M_ODR_0625); |
5hel2l2y | 4:89be9162c6aa | 80 | break; |
5hel2l2y | 4:89be9162c6aa | 81 | case 52: |
5hel2l2y | 4:89be9162c6aa | 82 | DEBUG("Accelerometer High, Gyro High, Magnetometer Low\n\r"); |
5hel2l2y | 4:89be9162c6aa | 83 | status = imu.begin(imu.G_SCALE_2000DPS, imu.A_SCALE_8G, imu.M_SCALE_4GS, |
5hel2l2y | 4:89be9162c6aa | 84 | imu.G_ODR_952_BW_100, imu.A_ODR_952, imu.M_ODR_0625); |
5hel2l2y | 0:d0291b4a856a | 85 | break; |
5hel2l2y | 0:d0291b4a856a | 86 | default: |
5hel2l2y | 3:db2db5c9611f | 87 | DEBUG("Default power\n\r"); |
5hel2l2y | 3:db2db5c9611f | 88 | status = imu.begin(); |
5hel2l2y | 0:d0291b4a856a | 89 | break; |
5hel2l2y | 0:d0291b4a856a | 90 | } |
5hel2l2y | 0:d0291b4a856a | 91 | |
5hel2l2y | 0:d0291b4a856a | 92 | pc.printf("LSM9DS1 WHO_AM_I's returned: 0x%X\r\n", status); |
5hel2l2y | 0:d0291b4a856a | 93 | pc.printf("Should be 0x683D\r\n"); |
5hel2l2y | 0:d0291b4a856a | 94 | |
5hel2l2y | 0:d0291b4a856a | 95 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
5hel2l2y | 0:d0291b4a856a | 96 | } |
5hel2l2y | 0:d0291b4a856a | 97 | } |
5hel2l2y | 0:d0291b4a856a | 98 | |
5hel2l2y | 0:d0291b4a856a | 99 | void periodicCallback(void) |
5hel2l2y | 0:d0291b4a856a | 100 | { |
5hel2l2y | 3:db2db5c9611f | 101 | ledAlive = !ledAlive; |
5hel2l2y | 0:d0291b4a856a | 102 | } |
5hel2l2y | 0:d0291b4a856a | 103 | |
5hel2l2y | 2:151ea422eb53 | 104 | // Interrupt signal |
5hel2l2y | 3:db2db5c9611f | 105 | void flip(DigitalOut led) { |
5hel2l2y | 3:db2db5c9611f | 106 | led = !led; |
5hel2l2y | 3:db2db5c9611f | 107 | } |
5hel2l2y | 3:db2db5c9611f | 108 | |
5hel2l2y | 3:db2db5c9611f | 109 | void flip2() { |
5hel2l2y | 3:db2db5c9611f | 110 | ledIntrM = !ledIntrM; |
5hel2l2y | 3:db2db5c9611f | 111 | } |
5hel2l2y | 3:db2db5c9611f | 112 | |
5hel2l2y | 3:db2db5c9611f | 113 | void flip3() { |
5hel2l2y | 3:db2db5c9611f | 114 | // pc.printf(" ++++ Data Change Triggered!! ++++ \n\r"); |
5hel2l2y | 3:db2db5c9611f | 115 | flip(ledIntrO); |
5hel2l2y | 3:db2db5c9611f | 116 | } |
5hel2l2y | 3:db2db5c9611f | 117 | |
5hel2l2y | 3:db2db5c9611f | 118 | void flip4() { |
5hel2l2y | 3:db2db5c9611f | 119 | // pc.printf(" ++++ G/X Triggered!! ++++ \n\r"); |
5hel2l2y | 3:db2db5c9611f | 120 | flip(ledIntrG); |
5hel2l2y | 2:151ea422eb53 | 121 | } |
5hel2l2y | 2:151ea422eb53 | 122 | |
5hel2l2y | 0:d0291b4a856a | 123 | int main(void) |
5hel2l2y | 0:d0291b4a856a | 124 | { |
5hel2l2y | 3:db2db5c9611f | 125 | // turn all LED off |
5hel2l2y | 3:db2db5c9611f | 126 | ledAlive = 1; |
5hel2l2y | 3:db2db5c9611f | 127 | ledIntrM = 1; |
5hel2l2y | 3:db2db5c9611f | 128 | ledIntrG = 1; |
5hel2l2y | 3:db2db5c9611f | 129 | ledIntrO = 1; |
5hel2l2y | 3:db2db5c9611f | 130 | |
5hel2l2y | 0:d0291b4a856a | 131 | Ticker ticker; |
5hel2l2y | 0:d0291b4a856a | 132 | ticker.attach(periodicCallback, 1); |
5hel2l2y | 0:d0291b4a856a | 133 | |
5hel2l2y | 0:d0291b4a856a | 134 | DEBUG("Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 135 | pc.printf("PC: Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 136 | ble.init(); |
5hel2l2y | 0:d0291b4a856a | 137 | ble.onDisconnection(disconnectionCallback); |
5hel2l2y | 0:d0291b4a856a | 138 | ble.onDataWritten(onDataWritten); |
5hel2l2y | 0:d0291b4a856a | 139 | |
5hel2l2y | 0:d0291b4a856a | 140 | /* setup advertising */ |
5hel2l2y | 0:d0291b4a856a | 141 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
5hel2l2y | 0:d0291b4a856a | 142 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
5hel2l2y | 0:d0291b4a856a | 143 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
5hel2l2y | 0:d0291b4a856a | 144 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
5hel2l2y | 0:d0291b4a856a | 145 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
5hel2l2y | 0:d0291b4a856a | 146 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
5hel2l2y | 0:d0291b4a856a | 147 | |
5hel2l2y | 0:d0291b4a856a | 148 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
5hel2l2y | 0:d0291b4a856a | 149 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 150 | |
5hel2l2y | 0:d0291b4a856a | 151 | UARTService uartService(ble); |
5hel2l2y | 0:d0291b4a856a | 152 | uartServicePtr = &uartService; |
5hel2l2y | 2:151ea422eb53 | 153 | |
5hel2l2y | 3:db2db5c9611f | 154 | // setup baud rate |
5hel2l2y | 3:db2db5c9611f | 155 | pc.baud(115200); |
5hel2l2y | 3:db2db5c9611f | 156 | |
5hel2l2y | 3:db2db5c9611f | 157 | // setup Interrupt mode |
5hel2l2y | 3:db2db5c9611f | 158 | intrGXO.mode(PullUp); |
5hel2l2y | 3:db2db5c9611f | 159 | intrGX.mode(PullUp); |
5hel2l2y | 3:db2db5c9611f | 160 | intrM.mode(PullUp); |
5hel2l2y | 3:db2db5c9611f | 161 | |
5hel2l2y | 3:db2db5c9611f | 162 | // status light will be lit based on sensor/data change |
5hel2l2y | 3:db2db5c9611f | 163 | intrGX.rise(&flip4); |
5hel2l2y | 3:db2db5c9611f | 164 | intrGXO.rise(&flip3); |
5hel2l2y | 0:d0291b4a856a | 165 | |
5hel2l2y | 0:d0291b4a856a | 166 | while (true) { |
5hel2l2y | 0:d0291b4a856a | 167 | ble.waitForEvent(); |
5hel2l2y | 2:151ea422eb53 | 168 | |
5hel2l2y | 2:151ea422eb53 | 169 | imu.readAccel(); |
5hel2l2y | 2:151ea422eb53 | 170 | pc.printf("%2f,%2f,%2f\r\n", imu.ax,imu.ay,imu.az); |
5hel2l2y | 2:151ea422eb53 | 171 | imu.readGyro(); |
5hel2l2y | 2:151ea422eb53 | 172 | pc.printf("%2f,%2f,%2f\r\n", imu.gx,imu.gy,imu.gz); |
5hel2l2y | 2:151ea422eb53 | 173 | imu.readMag(); |
5hel2l2y | 2:151ea422eb53 | 174 | pc.printf("%2f,%2f,%2f\r\n", imu.mx,imu.my,imu.mz); |
5hel2l2y | 3:db2db5c9611f | 175 | |
5hel2l2y | 2:151ea422eb53 | 176 | // Interrupt stats: This is only enabled in Low Power Mode |
5hel2l2y | 2:151ea422eb53 | 177 | // 38 - no interrupt |
5hel2l2y | 2:151ea422eb53 | 178 | // 42 - interrupt |
5hel2l2y | 2:151ea422eb53 | 179 | // 101 - sleep |
5hel2l2y | 3:db2db5c9611f | 180 | //imu.readIntr(); |
5hel2l2y | 2:151ea422eb53 | 181 | // pc.printf("intr: %f\r\n", imu.intr); |
5hel2l2y | 3:db2db5c9611f | 182 | // if(imu.intr == 42) { |
5hel2l2y | 2:151ea422eb53 | 183 | // pc.printf(" == interrupted == \r\n"); |
5hel2l2y | 3:db2db5c9611f | 184 | // flip(ledIntrO); |
5hel2l2y | 3:db2db5c9611f | 185 | // } |
5hel2l2y | 2:151ea422eb53 | 186 | |
5hel2l2y | 2:151ea422eb53 | 187 | wait(1.0); |
5hel2l2y | 0:d0291b4a856a | 188 | } |
5hel2l2y | 0:d0291b4a856a | 189 | } |