NRF51822 as bluetooth to UART
Dependencies: BLE_API BLE_LoopbackUART mbed nRF51822
Fork of BLE_LoopbackUART by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "BLEDevice.h" 00019 00020 #include "UARTService.h" 00021 00022 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; 00023 * it will have an impact on code-size and power consumption. */ 00024 00025 #if NEED_CONSOLE_OUTPUT 00026 #define DEBUG(...) { printf(__VA_ARGS__); } 00027 #else 00028 #define DEBUG(...) /* nothing */ 00029 #endif /* #if NEED_CONSOLE_OUTPUT */ 00030 00031 BLEDevice ble; 00032 DigitalOut led1(LED1); 00033 Serial pc(USBTX, USBRX); 00034 00035 static uint8_t rx_buf[20]; 00036 static uint8_t rx_len=0; 00037 UARTService *uartServicePtr; 00038 00039 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00040 { 00041 DEBUG("Disconnected!\n\r"); 00042 DEBUG("Restarting the advertising process\n\r"); 00043 ble.startAdvertising(); 00044 } 00045 00046 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00047 { 00048 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { 00049 uint16_t bytesRead = params->len; 00050 for(uint16_t index=0; index<bytesRead; index++) 00051 { 00052 pc.putc(params->data[index]); 00053 } 00054 } 00055 } 00056 00057 void uartCB(void) 00058 { 00059 while(pc.readable()) 00060 { 00061 rx_buf[rx_len++] = pc.getc(); 00062 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') 00063 { 00064 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len); 00065 rx_len = 0; 00066 break; 00067 } 00068 } 00069 } 00070 void periodicCallback(void) 00071 { 00072 led1 = !led1; 00073 } 00074 00075 int main(void) 00076 { 00077 led1 = 1; 00078 pc.baud(115200); 00079 00080 pc.attach( uartCB , pc.RxIrq); 00081 Ticker ticker; 00082 ticker.attach(periodicCallback, 1); 00083 00084 DEBUG("Initialising the nRF51822\n\r"); 00085 ble.init(); 00086 ble.onDisconnection(disconnectionCallback); 00087 ble.onDataWritten(onDataWritten); 00088 00089 /* setup advertising */ 00090 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00091 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00092 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00093 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); 00094 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00095 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00096 00097 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); 00098 ble.startAdvertising(); 00099 00100 UARTService uartService(ble); 00101 uartServicePtr = &uartService; 00102 00103 while (true) { 00104 ble.waitForEvent(); 00105 } 00106 }
Generated on Thu Jul 21 2022 14:29:07 by 1.7.2