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