uart rx receive data then send data from UARTSERVICE
Dependencies: BLE_API mbed nRF51822
main.cpp@0:34ffb40dd1ac, 2016-11-10 (annotated)
- Committer:
- li991001
- Date:
- Thu Nov 10 00:42:03 2016 +0000
- Revision:
- 0:34ffb40dd1ac
add uart rx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
li991001 | 0:34ffb40dd1ac | 1 | /* mbed Microcontroller Library |
li991001 | 0:34ffb40dd1ac | 2 | * Copyright (c) 2006-2013 ARM Limited |
li991001 | 0:34ffb40dd1ac | 3 | * |
li991001 | 0:34ffb40dd1ac | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
li991001 | 0:34ffb40dd1ac | 5 | * you may not use this file except in compliance with the License. |
li991001 | 0:34ffb40dd1ac | 6 | * You may obtain a copy of the License at |
li991001 | 0:34ffb40dd1ac | 7 | * |
li991001 | 0:34ffb40dd1ac | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
li991001 | 0:34ffb40dd1ac | 9 | * |
li991001 | 0:34ffb40dd1ac | 10 | * Unless required by applicable law or agreed to in writing, software |
li991001 | 0:34ffb40dd1ac | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
li991001 | 0:34ffb40dd1ac | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
li991001 | 0:34ffb40dd1ac | 13 | * See the License for the specific language governing permissions and |
li991001 | 0:34ffb40dd1ac | 14 | * limitations under the License. |
li991001 | 0:34ffb40dd1ac | 15 | */ |
li991001 | 0:34ffb40dd1ac | 16 | |
li991001 | 0:34ffb40dd1ac | 17 | #include "mbed.h" |
li991001 | 0:34ffb40dd1ac | 18 | #include "ble/BLE.h" |
li991001 | 0:34ffb40dd1ac | 19 | |
li991001 | 0:34ffb40dd1ac | 20 | #include "ble/services/UARTService.h" |
li991001 | 0:34ffb40dd1ac | 21 | #include "Serial.h" |
li991001 | 0:34ffb40dd1ac | 22 | |
li991001 | 0:34ffb40dd1ac | 23 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
li991001 | 0:34ffb40dd1ac | 24 | * it will have an impact on code-size and power consumption. */ |
li991001 | 0:34ffb40dd1ac | 25 | |
li991001 | 0:34ffb40dd1ac | 26 | #if NEED_CONSOLE_OUTPUT |
li991001 | 0:34ffb40dd1ac | 27 | #define DEBUG(...) { printf(__VA_ARGS__); } |
li991001 | 0:34ffb40dd1ac | 28 | #else |
li991001 | 0:34ffb40dd1ac | 29 | #define DEBUG(...) /* nothing */ |
li991001 | 0:34ffb40dd1ac | 30 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
li991001 | 0:34ffb40dd1ac | 31 | |
li991001 | 0:34ffb40dd1ac | 32 | BLEDevice ble; |
li991001 | 0:34ffb40dd1ac | 33 | DigitalOut led1(LED1); |
li991001 | 0:34ffb40dd1ac | 34 | |
li991001 | 0:34ffb40dd1ac | 35 | Serial uart1(USBTX,USBRX); |
li991001 | 0:34ffb40dd1ac | 36 | |
li991001 | 0:34ffb40dd1ac | 37 | UARTService *uartServicePtr; |
li991001 | 0:34ffb40dd1ac | 38 | |
li991001 | 0:34ffb40dd1ac | 39 | uint8_t sFlag = 0; |
li991001 | 0:34ffb40dd1ac | 40 | |
li991001 | 0:34ffb40dd1ac | 41 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
li991001 | 0:34ffb40dd1ac | 42 | { |
li991001 | 0:34ffb40dd1ac | 43 | DEBUG("Disconnected!\n\r"); |
li991001 | 0:34ffb40dd1ac | 44 | DEBUG("Restarting the advertising process\n\r"); |
li991001 | 0:34ffb40dd1ac | 45 | ble.startAdvertising(); |
li991001 | 0:34ffb40dd1ac | 46 | } |
li991001 | 0:34ffb40dd1ac | 47 | |
li991001 | 0:34ffb40dd1ac | 48 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
li991001 | 0:34ffb40dd1ac | 49 | |
li991001 | 0:34ffb40dd1ac | 50 | DEBUG("Connected!\n\r"); |
li991001 | 0:34ffb40dd1ac | 51 | |
li991001 | 0:34ffb40dd1ac | 52 | } |
li991001 | 0:34ffb40dd1ac | 53 | |
li991001 | 0:34ffb40dd1ac | 54 | |
li991001 | 0:34ffb40dd1ac | 55 | uint8_t b[40]={'a','d','q','w'}; |
li991001 | 0:34ffb40dd1ac | 56 | void onDataWritten1(const GattWriteCallbackParams *params) |
li991001 | 0:34ffb40dd1ac | 57 | { |
li991001 | 0:34ffb40dd1ac | 58 | |
li991001 | 0:34ffb40dd1ac | 59 | |
li991001 | 0:34ffb40dd1ac | 60 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
li991001 | 0:34ffb40dd1ac | 61 | uint16_t bytesRead = params->len; |
li991001 | 0:34ffb40dd1ac | 62 | DEBUG("received %u bytes %s\n\r", bytesRead,params->data); |
li991001 | 0:34ffb40dd1ac | 63 | // if(sFlag == 1) |
li991001 | 0:34ffb40dd1ac | 64 | // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, 4/*bytesRead*/); |
li991001 | 0:34ffb40dd1ac | 65 | } |
li991001 | 0:34ffb40dd1ac | 66 | } |
li991001 | 0:34ffb40dd1ac | 67 | |
li991001 | 0:34ffb40dd1ac | 68 | |
li991001 | 0:34ffb40dd1ac | 69 | void periodicCallback(void) |
li991001 | 0:34ffb40dd1ac | 70 | { |
li991001 | 0:34ffb40dd1ac | 71 | led1 = !led1; |
li991001 | 0:34ffb40dd1ac | 72 | |
li991001 | 0:34ffb40dd1ac | 73 | } |
li991001 | 0:34ffb40dd1ac | 74 | uint8_t c; |
li991001 | 0:34ffb40dd1ac | 75 | void uartRx(void) |
li991001 | 0:34ffb40dd1ac | 76 | { |
li991001 | 0:34ffb40dd1ac | 77 | |
li991001 | 0:34ffb40dd1ac | 78 | c = uart1.getc(); |
li991001 | 0:34ffb40dd1ac | 79 | |
li991001 | 0:34ffb40dd1ac | 80 | if(sFlag < 39) |
li991001 | 0:34ffb40dd1ac | 81 | b[sFlag++] = c; |
li991001 | 0:34ffb40dd1ac | 82 | |
li991001 | 0:34ffb40dd1ac | 83 | if(c == '\r' || c == '\n' || sFlag == 15) |
li991001 | 0:34ffb40dd1ac | 84 | { |
li991001 | 0:34ffb40dd1ac | 85 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, sFlag/*bytesRead*/); |
li991001 | 0:34ffb40dd1ac | 86 | sFlag = 0; |
li991001 | 0:34ffb40dd1ac | 87 | } |
li991001 | 0:34ffb40dd1ac | 88 | } |
li991001 | 0:34ffb40dd1ac | 89 | |
li991001 | 0:34ffb40dd1ac | 90 | int main(void) |
li991001 | 0:34ffb40dd1ac | 91 | { |
li991001 | 0:34ffb40dd1ac | 92 | led1 = 1; |
li991001 | 0:34ffb40dd1ac | 93 | uart1.baud(9600); |
li991001 | 0:34ffb40dd1ac | 94 | Ticker ticker; |
li991001 | 0:34ffb40dd1ac | 95 | ticker.attach(periodicCallback, 1); |
li991001 | 0:34ffb40dd1ac | 96 | |
li991001 | 0:34ffb40dd1ac | 97 | uart1.attach(uartRx,Serial::RxIrq); |
li991001 | 0:34ffb40dd1ac | 98 | |
li991001 | 0:34ffb40dd1ac | 99 | DEBUG("Initialising the nRF51822\n\r"); |
li991001 | 0:34ffb40dd1ac | 100 | ble.init(); |
li991001 | 0:34ffb40dd1ac | 101 | ble.onDisconnection(disconnectionCallback); |
li991001 | 0:34ffb40dd1ac | 102 | ble.onConnection(connectionCallback); |
li991001 | 0:34ffb40dd1ac | 103 | ble.onDataWritten(onDataWritten1); |
li991001 | 0:34ffb40dd1ac | 104 | |
li991001 | 0:34ffb40dd1ac | 105 | /* setup advertising */ |
li991001 | 0:34ffb40dd1ac | 106 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
li991001 | 0:34ffb40dd1ac | 107 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
li991001 | 0:34ffb40dd1ac | 108 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
li991001 | 0:34ffb40dd1ac | 109 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
li991001 | 0:34ffb40dd1ac | 110 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
li991001 | 0:34ffb40dd1ac | 111 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
li991001 | 0:34ffb40dd1ac | 112 | |
li991001 | 0:34ffb40dd1ac | 113 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
li991001 | 0:34ffb40dd1ac | 114 | ble.startAdvertising(); |
li991001 | 0:34ffb40dd1ac | 115 | |
li991001 | 0:34ffb40dd1ac | 116 | UARTService uartService(ble); |
li991001 | 0:34ffb40dd1ac | 117 | uartServicePtr = &uartService; |
li991001 | 0:34ffb40dd1ac | 118 | |
li991001 | 0:34ffb40dd1ac | 119 | while (true) { |
li991001 | 0:34ffb40dd1ac | 120 | ble.waitForEvent(); |
li991001 | 0:34ffb40dd1ac | 121 | } |
li991001 | 0:34ffb40dd1ac | 122 | } |