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
Fork of BLE_LEDBlinker by
main.cpp
00001 // nRF51822 lib revision 534 00002 00003 #include "mbed.h" 00004 #include "ble/BLE.h" 00005 #include "ble/DiscoveredCharacteristic.h" 00006 #include "ble/DiscoveredService.h" 00007 #include "ble/services/UARTService.h" 00008 #include "ble/services/DFUService.h" 00009 #include "Buffer.h" 00010 00011 00012 #define LOG(args...) // printf(args) 00013 00014 00015 BLE ble; 00016 UARTService *bleuart; 00017 00018 bool triggerLedCharacteristic = false; 00019 DiscoveredCharacteristic ledCharacteristic; 00020 int saved_test_result = 0; 00021 volatile int current_test_status = 1; 00022 uint32_t device_scan_time = 0; 00023 uint32_t device_found_time = 0; 00024 uint32_t device_connected_time = 0; 00025 00026 int test_is_passed(); 00027 int test_save_result(int result); 00028 int test_check_io(); 00029 int test_check_vcc(); 00030 int test_output_result(int result); 00031 00032 Serial *uart; 00033 Buffer <char> uartRxBuffer(0x100); 00034 00035 uint8_t bleIsConnected = 0; 00036 uint8_t bleTxFlag = 0; 00037 00038 bool rxPayloadUpdated = false; 00039 uint8_t rxPayload[20 + 1] = {0,}; 00040 uint8_t txPayload[20 + 1] = {0,}; 00041 00042 void uart2ble(void) 00043 { 00044 uint16_t bytesToWrite = 0; 00045 for (int i = 0; i < 20; i++) { 00046 if (uartRxBuffer.available()) { 00047 txPayload[bytesToWrite] = uartRxBuffer; 00048 bytesToWrite++; 00049 } 00050 } 00051 00052 if (bytesToWrite != 0) { 00053 bleTxFlag = 1; 00054 00055 ble.updateCharacteristicValue(bleuart->getRXCharacteristicHandle(), txPayload, bytesToWrite); 00056 } else { 00057 bleTxFlag = 0; 00058 } 00059 } 00060 00061 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) 00062 { 00063 if (params->peerAddr[0] != params->peerAddr[5]) { /* !ALERT! Alter this filter to suit your device. */ 00064 return; 00065 } 00066 device_found_time = us_ticker_read(); 00067 00068 LOG("device found @ %d\r\n", device_found_time); 00069 00070 LOG("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", 00071 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], 00072 params->rssi, params->isScanResponse, params->type); 00073 00074 ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); 00075 } 00076 00077 00078 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00079 { 00080 if (params->role == Gap::CENTRAL) { 00081 device_connected_time = us_ticker_read(); 00082 00083 LOG("device connected @ %d\r\n", device_connected_time); 00084 if (current_test_status == 3) 00085 { 00086 current_test_status = 0; 00087 test_output_result(0); 00088 test_save_result(0); 00089 } 00090 00091 } else { 00092 LOG("connected\r\n"); 00093 00094 bleIsConnected = 1; 00095 bleTxFlag = 0; 00096 uartRxBuffer.clear(); 00097 } 00098 } 00099 00100 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00101 { 00102 LOG("Disconnected!\n\r"); 00103 LOG("Restarting the advertising process\n\r"); 00104 00105 bleTxFlag = 0; 00106 bleIsConnected = 0; 00107 00108 ble.startAdvertising(); 00109 00110 } 00111 00112 void onDataWritten(const GattWriteCallbackParams *params) 00113 { 00114 if ((bleuart != NULL) && (params->handle == bleuart->getTXCharacteristicHandle())) { 00115 uint16_t bytesRead = params->len; 00116 LOG("received %u bytes\n\r", bytesRead); 00117 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); 00118 00119 for (int i = 0; i < bytesRead; i++) { 00120 uart->putc(params->data[i]); 00121 } 00122 } 00123 } 00124 00125 void onDataSent(unsigned count) 00126 { 00127 LOG("onDataSent\r\n"); 00128 00129 uart2ble(); 00130 } 00131 00132 int main(void) 00133 { 00134 int io_status; 00135 int vcc_status; 00136 00137 saved_test_result = test_is_passed(); 00138 if (!saved_test_result) { 00139 { 00140 uart = new Serial(p20, p21); // release USBTX and USBRX 00141 uart->baud(38400); 00142 } 00143 00144 io_status = test_check_io(); 00145 vcc_status = test_check_vcc(); 00146 00147 if (io_status) { 00148 test_output_result(1); 00149 } else { 00150 current_test_status = 2; 00151 if (vcc_status) { 00152 test_output_result(2); 00153 } else { 00154 current_test_status = 3; 00155 } 00156 } 00157 00158 } else { 00159 test_output_result(0); 00160 uart = new Serial(p8, p7); 00161 uart->baud(38400); 00162 } 00163 00164 00165 00166 00167 ble.init(); 00168 ble.gap().onConnection(connectionCallback); 00169 ble.gap().onDisconnection(disconnectionCallback); 00170 ble.onDataWritten(onDataWritten); 00171 ble.onDataSent(onDataSent); 00172 00173 if (!saved_test_result) { 00174 LOG("io test %s\r\n", io_status ? "failed" : "ok"); 00175 LOG("vcc test %s\r\n", vcc_status ? "failed" : "ok"); 00176 00177 LOG("enable ble centre role\r\n"); 00178 00179 ble.gap().setScanParams(500, 400); 00180 ble.gap().startScan(advertisementCallback); 00181 device_scan_time = us_ticker_read(); 00182 LOG("scan device @ %d\r\n", device_scan_time); 00183 00184 { 00185 int wait_time_us = 0; 00186 while (current_test_status == 3 && wait_time_us < 10000000) { 00187 wait_us(1); 00188 wait_time_us++; 00189 } 00190 00191 if (current_test_status == 0) { 00192 LOG("from scan to connected: %d", device_connected_time - device_scan_time); 00193 } else if (current_test_status == 3) { 00194 test_output_result(3); 00195 } 00196 } 00197 } 00198 00199 /* setup advertising */ 00200 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00201 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00202 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00203 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); 00204 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00205 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00206 00207 bleuart = new UARTService(ble); 00208 DFUService *dfu = new DFUService(ble); 00209 00210 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00211 ble.startAdvertising(); 00212 00213 while (true) { 00214 if (bleIsConnected) { 00215 uint32_t timeout = 1000; 00216 while (timeout) { 00217 timeout--; 00218 if (uart->readable()) { 00219 uartRxBuffer.put((char)uart->getc()); 00220 timeout = 1000; 00221 } 00222 } 00223 00224 if (bleIsConnected && bleTxFlag == 0 && uartRxBuffer.available()) { 00225 uart2ble(); 00226 bleTxFlag = 1; 00227 } 00228 } else { 00229 ble.waitForEvent(); 00230 } 00231 } 00232 } 00233 00234 extern "C" void HardFault_Handler() 00235 { 00236 NVIC_SystemReset(); 00237 }
Generated on Tue Jul 12 2022 19:00:01 by
1.7.2
