Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API Buffer 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 #include "UARTService.h" 00020 #include "DFUService.h" 00021 #include "Buffer.h" 00022 00023 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; 00024 * it will have an impact on code-size and power consumption. */ 00025 00026 #if NEED_CONSOLE_OUTPUT 00027 #define DEBUG(...) { printf(__VA_ARGS__); } 00028 #else 00029 #define DEBUG(...) /* nothing */ 00030 #endif /* #if NEED_CONSOLE_OUTPUT */ 00031 00032 Buffer <char> uartRxBuffer(0x100); 00033 //Serial uart(p8, p7); 00034 Serial uart(USBTX, USBRX); 00035 00036 BLEDevice ble; 00037 DigitalOut led1(p28); 00038 00039 UARTService *uartServicePtr; 00040 00041 uint8_t bleIsConnected = 0; 00042 uint8_t bleTxFlag = 0; 00043 00044 bool rxPayloadUpdated = false; 00045 uint8_t rxPayload[20 + 1] = {0,}; 00046 uint8_t txPayload[20 + 1] = {0,}; 00047 00048 void uart2ble(void) 00049 { 00050 uint16_t bytesToWrite = 0; 00051 for (int i = 0; i < 20; i++) { 00052 if (uartRxBuffer.available()) { 00053 txPayload[bytesToWrite] = uartRxBuffer; 00054 bytesToWrite++; 00055 } 00056 } 00057 00058 if (bytesToWrite != 0) { 00059 bleTxFlag = 1; 00060 00061 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), txPayload, bytesToWrite); 00062 } else { 00063 bleTxFlag = 0; 00064 } 00065 } 00066 00067 void resetToRunBootloader() 00068 { 00069 DEBUG("Reset to run bootloader\r\n"); 00070 NRF_POWER->GPREGRET = 0x02; 00071 NVIC_SystemReset(); 00072 } 00073 00074 void connectionCallback(Gap::Handle_t, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *) 00075 { 00076 DEBUG("Connected\r\n"); 00077 bleIsConnected = 1; 00078 bleTxFlag = 0; 00079 } 00080 00081 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00082 { 00083 DEBUG("Disconnected!\n\r"); 00084 DEBUG("Restarting the advertising process\n\r"); 00085 00086 bleTxFlag = 0; 00087 bleIsConnected = 0; 00088 00089 ble.startAdvertising(); 00090 } 00091 00092 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00093 { 00094 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { 00095 uint16_t bytesRead = params->len; 00096 DEBUG("received %u bytes\n\r", bytesRead); 00097 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); 00098 00099 for (int i = 0; i < bytesRead; i++) { 00100 uart.putc(params->data[i]); 00101 } 00102 } 00103 } 00104 00105 void onDataSent(unsigned count) 00106 { 00107 DEBUG("onDataSent\r\n"); 00108 00109 uart2ble(); 00110 } 00111 00112 void periodicCallback(void) 00113 { 00114 led1 = !led1; 00115 } 00116 00117 int main(void) 00118 { 00119 led1 = 1; 00120 Ticker ticker; 00121 ticker.attach(periodicCallback, 0.01); 00122 00123 DEBUG("Initialising the nRF51822\n\r"); 00124 ble.init(); 00125 ble.onConnection(connectionCallback); 00126 ble.onDisconnection(disconnectionCallback); 00127 ble.onDataWritten(onDataWritten); 00128 ble.onDataSent(onDataSent); 00129 00130 /* setup advertising */ 00131 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00132 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00133 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00134 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); 00135 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00136 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00137 00138 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); 00139 ble.startAdvertising(); 00140 00141 DFUService dfuService(ble); 00142 UARTService uartService(ble); 00143 uartServicePtr = &uartService; 00144 00145 while (true) { 00146 uint32_t timeout = 1000; 00147 while (timeout) { 00148 timeout--; 00149 if (uart.readable()) { 00150 uartRxBuffer.put((char)uart.getc()); 00151 timeout = 1000; 00152 } 00153 } 00154 00155 if (bleIsConnected && bleTxFlag == 0 && uartRxBuffer.available()) { 00156 uart2ble(); 00157 bleTxFlag = 1; 00158 } 00159 } 00160 } 00161 00162 extern "C" void HardFault_Handler() 00163 { 00164 NVIC_SystemReset(); 00165 }
Generated on Mon Jul 25 2022 12:50:35 by
1.7.2
