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 X_NUCLEO_IDB0XA1 mbed
Fork of Amaldi_6_BLE_UART_IDB0XA1 by
Revision 7:8490fe113598, committed 2017-04-25
- Comitter:
- waby38
- Date:
- Tue Apr 25 15:26:50 2017 +0000
- Parent:
- 6:e696cac5ef17
- Child:
- 8:60033323cbcb
- Child:
- 9:46f0c7908f89
- Commit message:
- UART BLE implementation on X-NUCLEO-IDB0XA1
; Device is bounded when LED2 is blinking
; Com RX/TX on STLINK COM @ 9600 baud
; Android phone, can use nRF UART/UART-BLE/AppInventor UART-BLE
Changed in this revision
--- a/LEDService.h Fri Sep 16 10:36:43 2016 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __BLE_LED_SERVICE_H__
-#define __BLE_LED_SERVICE_H__
-
-class LEDService {
-public:
- const static uint16_t LED_SERVICE_UUID = 0xA000;
- const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001;
-
- LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) :
- ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic)
- {
- GattCharacteristic *charTable[] = {&ledState};
- GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
- ble.addService(ledService);
- }
-
- GattAttribute::Handle_t getValueHandle() const {
- return ledState.getValueHandle();
- }
-
-private:
- BLEDevice &ble;
- ReadWriteGattCharacteristic<bool> ledState;
-};
-
-#endif /* #ifndef __BLE_LED_SERVICE_H__ */
--- a/X_NUCLEO_IDB0XA1.lib Fri Sep 16 10:36:43 2016 +0000 +++ b/X_NUCLEO_IDB0XA1.lib Tue Apr 25 15:26:50 2017 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/#fa98703ece8e +http://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/#4606b082c3e6
--- a/main.cpp Fri Sep 16 10:36:43 2016 +0000
+++ b/main.cpp Tue Apr 25 15:26:50 2017 +0000
@@ -16,44 +16,77 @@
#include "mbed.h"
#include "ble/BLE.h"
-#include "LEDService.h"
+#include "ble/services/UARTService.h"
+#include "Serial.h"
+
+#define UART_BUFFER (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN*10)
-DigitalOut actuatedLED(LED1, 0);
-
-const static char DEVICE_NAME[] = "LED";
-static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
+const static char DEVICE_NAME[] = "UART";
+UARTService *uartServicePtr;
+static uint8_t uartBuff[UART_BUFFER];
+static uint8_t uartBuffPos=0;
-LEDService *ledServicePtr;
+void onBleError(ble_error_t error);
+
+DigitalOut led1(LED1);
+Serial uart1(USBTX,USBRX);
+Ticker ticker;
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+#define DEBUG uart1.printf
+
+/* Ticker */
+void periodicCallback(void)
{
- (void)params;
- BLE::Instance().gap().startAdvertising(); // restart advertising
+ led1 = !led1;
}
-/**
- * This callback allows the LEDService to receive updates to the ledState Characteristic.
- *
- * @param[in] params
- * Information about the characterisitc being updated.
- */
-void onDataWrittenCallback(const GattWriteCallbackParams *params) {
- if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
- actuatedLED = *(params->data);
+/* UART */
+void uartRx(void)
+{
+ if(uart1.readable()){
+ uartBuff[uartBuffPos] = uart1.getc();
+ if((uartBuff[uartBuffPos] == '\r') || (uartBuff[uartBuffPos] == '\n') || (uartBuffPos >= UART_BUFFER)) {
+ uartBuff[uartBuffPos] = '\0';
+ /* We are sending the whole string even if less than BLE_UART_SERVICE_MAX_DATA_LEN otherwise we need to wait */
+ uartServicePtr->write(uartBuff, (uartBuffPos/UARTService::BLE_UART_SERVICE_MAX_DATA_LEN +1) * UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
+ DEBUG("TX : %s\r\n", uartBuff);
+ memset(uartBuff, 0, UART_BUFFER);
+ uartBuffPos = 0;
+ }
+ else
+ uartBuffPos++;
}
}
-/**
- * This function is called when the ble initialization process has failled
- */
-void onBleInitError(BLE &ble, ble_error_t error)
-{
- /* Initialization error handling should go here */
+
+/* BLE */
+void BleConnectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+ DEBUG("BLE Client Connected!\n\r");
+ DEBUG("Please type a string and press return\r\n");
+
+ ticker.attach(periodicCallback, 1);
+}
+
+void BleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+ (void)params;
+ DEBUG("BLE Client Disconnected!\r\n");
+ ticker.detach();
+ BLE::Instance().gap().startAdvertising(); // restart advertising
+ led1=0;
+}
+
+void BleOnDataWrittenCallback(const GattWriteCallbackParams *params) {
+ if (params->handle == uartServicePtr->getTXCharacteristicHandle()){
+ DEBUG("RX: %s\r\n", params->data);
+ }
+}
+
+void onBleError(ble_error_t error) {
+ DEBUG("BLE Error: %d\r\n");
+ /* Handle error now */
}
-/**
- * Callback triggered when the ble initialization process has finished
- */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
@@ -61,7 +94,7 @@
if (error != BLE_ERROR_NONE) {
/* In case of error, forward the error handling to onBleInitError */
- onBleInitError(ble, error);
+ onBleError(error);
return;
}
@@ -70,18 +103,22 @@
return;
}
- ble.gap().onDisconnection(disconnectionCallback);
- ble.gattServer().onDataWritten(onDataWrittenCallback);
+ ble.gap().onConnection(BleConnectionCallback);
+ ble.gap().onDisconnection(BleDisconnectionCallback);
+ ble.gattServer().onDataWritten(BleOnDataWrittenCallback);
- bool initialValueForLEDCharacteristic = true;
- ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
+ DEBUG("BLE UARTService: ");
+ /* Setup primary service. */
+ UARTService uartService(ble);
+ uartServicePtr = &uartService;
+ DEBUG("Started\r\n");
/* setup advertising */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
+ ble.gap().setAdvertisingInterval(500); /* 500ms. */
ble.gap().startAdvertising();
while (true) {
@@ -91,8 +128,11 @@
int main(void)
{
+ led1=0;
+
+ uart1.baud(9600);
+ uart1.attach(uartRx,Serial::RxIrq);
BLE &ble = BLE::Instance();
-
ble.init(bleInitComplete);
}
