Firmware for the mbed in the BlueSync Sensor platform. Intended to communicate with a BlueGiga BLE112 Bluetooth LE module over UART.
Dependencies: TimerCapture mbed
main.cpp@6:51884d3fd44d, 2015-06-12 (annotated)
- Committer:
- dishbreak
- Date:
- Fri Jun 12 05:14:50 2015 +0000
- Revision:
- 6:51884d3fd44d
- Parent:
- 5:fd4773c12f93
got everything but offset reception working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dishbreak | 0:2bc22bc992ac | 1 | #include "mbed.h" |
dishbreak | 0:2bc22bc992ac | 2 | #include "TimerCapture.h" |
dishbreak | 1:cb941edd7bce | 3 | #include "bluesync_types.h" |
dishbreak | 0:2bc22bc992ac | 4 | |
dishbreak | 1:cb941edd7bce | 5 | /** @file main.cpp |
dishbreak | 1:cb941edd7bce | 6 | @brief Main for the BlueSync mbed application. */ |
dishbreak | 1:cb941edd7bce | 7 | |
dishbreak | 1:cb941edd7bce | 8 | Serial ble112(p9, p10); ///< Serial connection for communicating with the BLE112. |
dishbreak | 1:cb941edd7bce | 9 | TimerCapture * capPin; ///< Capture pin, wired to GPIO pin on BLE112. |
dishbreak | 5:fd4773c12f93 | 10 | TimerCapture * evtPin; |
dishbreak | 2:ac474fccf29b | 11 | DigitalOut bleSlaveLed(LED1); |
dishbreak | 2:ac474fccf29b | 12 | DigitalOut bleScanningLed(LED2); |
dishbreak | 2:ac474fccf29b | 13 | DigitalOut bleAdvRecv(LED3); |
dishbreak | 6:51884d3fd44d | 14 | DigitalOut statusLed(LED4); |
dishbreak | 3:54fef6ac433b | 15 | Ticker ticker; |
dishbreak | 5:fd4773c12f93 | 16 | InterruptIn evtWatchPin(p28); |
dishbreak | 2:ac474fccf29b | 17 | |
dishbreak | 2:ac474fccf29b | 18 | void blankOutLeds() { |
dishbreak | 2:ac474fccf29b | 19 | bleSlaveLed = 0; |
dishbreak | 2:ac474fccf29b | 20 | bleScanningLed = 0; |
dishbreak | 2:ac474fccf29b | 21 | bleAdvRecv = 0; |
dishbreak | 6:51884d3fd44d | 22 | //statusLed = 0; |
dishbreak | 2:ac474fccf29b | 23 | } |
dishbreak | 0:2bc22bc992ac | 24 | |
dishbreak | 2:ac474fccf29b | 25 | /*uint8_t[] toByteArray(uint32_t input) { |
dishbreak | 2:ac474fccf29b | 26 | uint8_t byte[4]; |
dishbreak | 2:ac474fccf29b | 27 | |
dishbreak | 2:ac474fccf29b | 28 | byte[0] = input & 0x000000FF; |
dishbreak | 2:ac474fccf29b | 29 | byte[1] = (input & 0x0000FF00) >> 8; |
dishbreak | 2:ac474fccf29b | 30 | byte[2] = (input & 0x00FF0000) >> 16; |
dishbreak | 2:ac474fccf29b | 31 | byte[3] = (input & 0xFF000000) >> 24; |
dishbreak | 2:ac474fccf29b | 32 | |
dishbreak | 2:ac474fccf29b | 33 | return byte; |
dishbreak | 2:ac474fccf29b | 34 | }*/ |
dishbreak | 2:ac474fccf29b | 35 | |
dishbreak | 2:ac474fccf29b | 36 | void on_adv_recv() { |
dishbreak | 2:ac474fccf29b | 37 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 38 | bleAdvRecv = 1; |
dishbreak | 0:2bc22bc992ac | 39 | uint8_t hw_addr[6]; |
dishbreak | 0:2bc22bc992ac | 40 | for (int i = 5; i >=0; i--) { |
dishbreak | 0:2bc22bc992ac | 41 | hw_addr[i] = ble112.getc(); |
dishbreak | 0:2bc22bc992ac | 42 | } |
dishbreak | 0:2bc22bc992ac | 43 | |
dishbreak | 0:2bc22bc992ac | 44 | printf("***************\r\n"); |
dishbreak | 0:2bc22bc992ac | 45 | printf("HW Addr: %02x:%02x:%02x:%02x:%02x:%02x ", |
dishbreak | 0:2bc22bc992ac | 46 | hw_addr[0], |
dishbreak | 0:2bc22bc992ac | 47 | hw_addr[1], |
dishbreak | 0:2bc22bc992ac | 48 | hw_addr[2], |
dishbreak | 0:2bc22bc992ac | 49 | hw_addr[3], |
dishbreak | 0:2bc22bc992ac | 50 | hw_addr[4], |
dishbreak | 0:2bc22bc992ac | 51 | hw_addr[5]); |
dishbreak | 0:2bc22bc992ac | 52 | printf("Obs At: %d\r\n", capPin->getTime()); |
dishbreak | 2:ac474fccf29b | 53 | } |
dishbreak | 2:ac474fccf29b | 54 | |
dishbreak | 2:ac474fccf29b | 55 | void on_master_mode() { |
dishbreak | 2:ac474fccf29b | 56 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 57 | bleScanningLed = 1; |
dishbreak | 2:ac474fccf29b | 58 | } |
dishbreak | 2:ac474fccf29b | 59 | |
dishbreak | 2:ac474fccf29b | 60 | void on_slave_mode() { |
dishbreak | 2:ac474fccf29b | 61 | blankOutLeds(); |
dishbreak | 2:ac474fccf29b | 62 | bleSlaveLed = 1; |
dishbreak | 1:cb941edd7bce | 63 | intByteArray bitArray; |
dishbreak | 1:cb941edd7bce | 64 | bitArray.integer = capPin->getTime(); |
dishbreak | 3:54fef6ac433b | 65 | printf("Sending timestamp %d (%08x)\r\n", bitArray.integer, bitArray.integer); |
dishbreak | 2:ac474fccf29b | 66 | uint8_t event = (uint8_t) EventCode::SET_TIMESTAMP; |
dishbreak | 2:ac474fccf29b | 67 | printf("Sending event: %x\r\n", event); |
dishbreak | 2:ac474fccf29b | 68 | ble112.putc(0x05); |
dishbreak | 3:54fef6ac433b | 69 | for (int i = 0; i < 4; i++) { |
dishbreak | 1:cb941edd7bce | 70 | ble112.putc(bitArray.byte[i]); |
dishbreak | 1:cb941edd7bce | 71 | } |
dishbreak | 0:2bc22bc992ac | 72 | } |
dishbreak | 0:2bc22bc992ac | 73 | |
dishbreak | 2:ac474fccf29b | 74 | void on_offset_recv() { |
dishbreak | 2:ac474fccf29b | 75 | blankOutLeds(); |
dishbreak | 6:51884d3fd44d | 76 | statusLed = 1; |
dishbreak | 2:ac474fccf29b | 77 | intByteArray bitArray; |
dishbreak | 4:778626dfcc92 | 78 | printf("Trying to get offset\r\n"); |
dishbreak | 4:778626dfcc92 | 79 | for (int i = 0; i < 4; i++) { |
dishbreak | 4:778626dfcc92 | 80 | printf("Reading byte[%d]\r\n", i); |
dishbreak | 2:ac474fccf29b | 81 | bitArray.byte[i] = ble112.getc(); |
dishbreak | 2:ac474fccf29b | 82 | } |
dishbreak | 2:ac474fccf29b | 83 | printf("Got offset %d\r\n", bitArray.signed_integer); |
dishbreak | 4:778626dfcc92 | 84 | |
dishbreak | 4:778626dfcc92 | 85 | LPC_TIM2->TC += bitArray.signed_integer; |
dishbreak | 2:ac474fccf29b | 86 | } |
dishbreak | 2:ac474fccf29b | 87 | |
dishbreak | 5:fd4773c12f93 | 88 | void on_interrupt_recv() { |
dishbreak | 5:fd4773c12f93 | 89 | uint8_t event = (uint8_t) EventCode::SET_SENSOR_TIME; |
dishbreak | 5:fd4773c12f93 | 90 | intByteArray bitArray; |
dishbreak | 5:fd4773c12f93 | 91 | bitArray.integer = evtPin->getTime(); |
dishbreak | 5:fd4773c12f93 | 92 | printf("Sending sensor time %d (%08x)\r\n", bitArray.integer, bitArray.integer); |
dishbreak | 5:fd4773c12f93 | 93 | ble112.putc(event); |
dishbreak | 5:fd4773c12f93 | 94 | for (int i = 0; i < 4; i++) { |
dishbreak | 5:fd4773c12f93 | 95 | ble112.putc(bitArray.byte[i]); |
dishbreak | 5:fd4773c12f93 | 96 | } |
dishbreak | 5:fd4773c12f93 | 97 | } |
dishbreak | 2:ac474fccf29b | 98 | |
dishbreak | 2:ac474fccf29b | 99 | void on_serial_rcv() { |
dishbreak | 2:ac474fccf29b | 100 | uint8_t command = ble112.getc(); |
dishbreak | 2:ac474fccf29b | 101 | printf("Command: %x\r\n", command); |
dishbreak | 2:ac474fccf29b | 102 | switch (command) { |
dishbreak | 2:ac474fccf29b | 103 | case EventCode::ADV_RECV: |
dishbreak | 2:ac474fccf29b | 104 | on_adv_recv(); |
dishbreak | 2:ac474fccf29b | 105 | break; |
dishbreak | 2:ac474fccf29b | 106 | case EventCode::SLAVE_MODE: |
dishbreak | 2:ac474fccf29b | 107 | on_slave_mode(); |
dishbreak | 2:ac474fccf29b | 108 | break; |
dishbreak | 2:ac474fccf29b | 109 | case EventCode::MASTER_MODE: |
dishbreak | 2:ac474fccf29b | 110 | on_master_mode(); |
dishbreak | 2:ac474fccf29b | 111 | break; |
dishbreak | 2:ac474fccf29b | 112 | case EventCode::SET_TIMESTAMP: |
dishbreak | 2:ac474fccf29b | 113 | |
dishbreak | 2:ac474fccf29b | 114 | break; |
dishbreak | 2:ac474fccf29b | 115 | case EventCode::OFFSET_RECV: |
dishbreak | 2:ac474fccf29b | 116 | on_offset_recv(); |
dishbreak | 2:ac474fccf29b | 117 | break; |
dishbreak | 2:ac474fccf29b | 118 | default: |
dishbreak | 2:ac474fccf29b | 119 | printf("Got unexpected byte '%x' from terminal!\r\n",command); |
dishbreak | 2:ac474fccf29b | 120 | break; |
dishbreak | 2:ac474fccf29b | 121 | } |
dishbreak | 2:ac474fccf29b | 122 | } |
dishbreak | 2:ac474fccf29b | 123 | |
dishbreak | 3:54fef6ac433b | 124 | void on_tick() { |
dishbreak | 3:54fef6ac433b | 125 | uint32_t timestamp = capPin->getTime(); |
dishbreak | 5:fd4773c12f93 | 126 | printf("+++Capture register: %d (%08x) timer: %d (%08x)\r\n", timestamp, timestamp, |
dishbreak | 5:fd4773c12f93 | 127 | LPC_TIM2->TC, LPC_TIM2->TC |
dishbreak | 5:fd4773c12f93 | 128 | ); |
dishbreak | 6:51884d3fd44d | 129 | statusLed = !statusLed; |
dishbreak | 3:54fef6ac433b | 130 | } |
dishbreak | 3:54fef6ac433b | 131 | |
dishbreak | 0:2bc22bc992ac | 132 | int main() { |
dishbreak | 2:ac474fccf29b | 133 | printf("Booted!\r\n"); |
dishbreak | 5:fd4773c12f93 | 134 | printf("SystemCoreClock is %d, %d ticks per ms\r\n", SystemCoreClock, SystemCoreClock/1000); |
dishbreak | 3:54fef6ac433b | 135 | ticker.attach(&on_tick, 5.0); |
dishbreak | 6:51884d3fd44d | 136 | statusLed = 1; |
dishbreak | 0:2bc22bc992ac | 137 | TimerCapture::startTimer(); |
dishbreak | 0:2bc22bc992ac | 138 | capPin = new TimerCapture(p30); |
dishbreak | 5:fd4773c12f93 | 139 | evtPin = new TimerCapture(p29); |
dishbreak | 5:fd4773c12f93 | 140 | evtWatchPin.rise(&on_interrupt_recv); |
dishbreak | 3:54fef6ac433b | 141 | ble112.baud(9600); |
dishbreak | 3:54fef6ac433b | 142 | ble112.set_flow_control(SerialBase::RTSCTS, p7, p8); |
dishbreak | 0:2bc22bc992ac | 143 | ble112.attach(&on_serial_rcv); |
dishbreak | 2:ac474fccf29b | 144 | uint8_t command = (uint8_t) EventCode::GET_STATE; |
dishbreak | 2:ac474fccf29b | 145 | printf("Sending command %x\r\n", command); |
dishbreak | 2:ac474fccf29b | 146 | ble112.putc(command); |
dishbreak | 2:ac474fccf29b | 147 | for (int i = 0; i < 4; i++) { |
dishbreak | 2:ac474fccf29b | 148 | ble112.putc(0x00); |
dishbreak | 2:ac474fccf29b | 149 | } |
dishbreak | 0:2bc22bc992ac | 150 | } |