BLE Client UART function
This is a BLE Client (Central) program for nRF51.
You can communicate with mbed BLE using "BLE_Uart_Server" program as follows.
/users/kenjiArai/code/BLE_Uart_Server/
Please refer following my notebook.
/users/kenjiArai/notebook/ble-client-and-peripheral-using-switch-sience-ty51/#
Diff: main.cpp
- Revision:
- 2:6fb0b87b041d
- Parent:
- 1:f68a5e55a60e
- Child:
- 3:9236f8e65c80
--- a/main.cpp Thu Apr 21 11:20:46 2016 +0000 +++ b/main.cpp Mon Jun 13 09:53:27 2016 +0000 @@ -1,5 +1,5 @@ /* - * ------- BLE Client UART function -------------------------------- + * ------- BLE Client UART function ------------------------------------- * --- Tested on Switch Science mbed TY51822r3 --- * * Modified by Kenji Arai @@ -7,7 +7,7 @@ * http://mbed.org/users/kenjiArai/ * * Started: April 8th, 2016 - * Revised: April 21st, 2016 + * Revised: June 13th, 2016 * * Original program (see original.cpp file): * S130 potential unstability case [closed] by Fabien Comte @@ -26,16 +26,40 @@ #include "UARTService.h" #include "ble/DiscoveredCharacteristic.h" #include "ble/DiscoveredService.h" +#include "nRF51_lowpwr.h" // Definition ------------------------------------------------------------------------------------ -#define NEED_CONSOLE_OUTPUT 0 +// Before using this function, please specify your program are used following functions or not. +#define USE_DEVICE_STDIO_MESSAGES 0 // printf +#define USE_DEVICE_SERIAL 1 // Serial or DEBUG & etc. +#define USE_DEVICE_I2C 0 // Sensors with I2C, LCD, EEPROM, Driver chips & etc. +#define USE_DEVICE_SPI 0 // Sensors with SPI, LCD, EEPROM, Driver chips & etc. +#define USE_DEVICE_SPISLAVE 0 // Communication with master vis SPI +#define USE_DEVICE_PWMOUT 0 // PWM duty output, Serve & etc. +#define USE_DEVICE_ANALOGIN 0 // Analog adc -#if NEED_CONSOLE_OUTPUT -#define DEBUG(...) { printf(__VA_ARGS__); } +#if USE_DEVICE_STDIO_MESSAGES +#define DEBUG(...) { printf(__VA_ARGS__); } #else #define DEBUG(...) #endif +#if USE_DEVICE_SERIAL +#define BAUD(x) pc.baud(x) +#define GETC(x) pc.getc(x) +#define PUTC(x) pc.putc(x) +#define PRINTF(...) { pc.printf(__VA_ARGS__); } +#define READABLE(x) pc.readable(x) +#define ATTACH(x,y) pc.attach(x, y); +#else +#define BAUD(x) +#define GETC(x) 'c' +#define PUTC(x) +#define PRINTF(...) +#define READABLE(x) +#define ATTACH(x,y) +#endif + #define SOFT_DEVICE_FATHER_HANDLE 3 #define BOARDS_COUNT 3 @@ -51,20 +75,64 @@ // ROM / Constant data --------------------------------------------------------------------------- #warning "You need to modify below value based on your board." const Gap::Address_t mac_board_0 = {0x59, 0x2c, 0xa8, 0x0e, 0xe2, 0xef}; +//const Gap::Address_t mac_board_0 = {0x30, 0x74, 0x6d, 0xbd, 0x83, 0xf4}; +//const Gap::Address_t mac_board_0 = {0xe4, 0x34, 0xbc, 0xb9, 0xc4, 0xf1}; const Gap::Address_t mac_board_1 = {0x50, 0x2b, 0xea, 0x14, 0x95, 0xd2}; -const Gap::Address_t mac_board_2 = {0x30, 0x74, 0x6d, 0xbd, 0x83, 0xf4}; -const Gap::Address_t mac_board_3 = {0x0f, 0x72, 0xbf, 0x43, 0xbc, 0xd0}; -const Gap::Address_t mac_board_4 = {0x98, 0x1a, 0x92, 0x10, 0x7f, 0xd5}; +const Gap::Address_t mac_board_2 = {0x0f, 0x72, 0xbf, 0x43, 0xbc, 0xd0}; +const Gap::Address_t mac_board_3 = {0x83, 0xc9, 0x1a, 0x90, 0xdf, 0xd6}; // TY small with LiC +const Gap::Address_t mac_board_4 = {0x43, 0xa4, 0x36, 0x11, 0x5b, 0xeb}; // TY small +//const Gap::Address_t mac_board_4 = {0x98, 0x1a, 0x92, 0x10, 0x7f, 0xd5}; #warning "You need to confirm your device name." const static char DEVICE_NAME[] = "UART_C"; static const uint16_t uuid16_list[] = {UARTServiceShortUUID}; +const nRF51_LOWPWR_TypeDef lowpwr_table = + { + #if USE_DEVICE_STDIO_MESSAGES + true, + #else + false, + #endif + #if USE_DEVICE_SERIAL + true, + #else + false, + #endif + #if USE_DEVICE_I2C + true, + #else + false, + #endif + #if USE_DEVICE_SPI + true, + #else + false, + #endif + #if USE_DEVICE_SPISLAVE + true, + #else + false, + #endif + #if USE_DEVICE_PWMOUT + true, + #else + false, + #endif + #if USE_DEVICE_ANALOGIN + true + #else + false + #endif + }; // RAM ------------------------------------------------------------------------------------------- Gap::Handle_t connectionHandle = 0xFFFF; DiscoveredCharacteristic uartTXCharacteristic; DiscoveredCharacteristic uartRXCharacteristic; bool foundUartRXCharacteristic = false; -bool connected2server = false; +bool connected2server = false; +bool connection_1st = false; +bool connection_tx = false; +bool connection_rx = false; UARTService * uartServicePtr = NULL; Gap::Address_t my_mac; int my_board_index = -1; @@ -102,12 +170,13 @@ // Control Program //------------------------------------------------------------------------------------------------- int main(void){ + LowPwr set_lowpwr(&lowpwr_table); alivenessLED = 0; - pc.attach(&serialRxCallback, Serial::RxIrq); + ATTACH(&serialRxCallback, Serial::RxIrq); ticker.attach(periodicCallback, 1); - for (int k = 0; k < 20; k++) { pc.printf("\r\n");} // clear terminal output - pc.printf("UART Communication / Client side\r\n"); // opening message - pc.printf(" Client(Central) and Server(Peripheral)\r\n"); // opening message + for (int k = 0; k < 20; k++) { PRINTF("\r\n");} // clear terminal output + PRINTF("UART Communication / Client side\r\n"); // opening message + PRINTF(" Client(Central) and Server(Peripheral)\r\n"); // opening message // Mixed role ************************************************************* ble.init(); Gap::AddressType_t my_mac_type; @@ -159,16 +228,16 @@ if (recieved_uart_dat0 == true){ recieved_uart_dat0 = false; for(int i = 0; i < uart_bf0_len; i++){ - pc.printf("%c", uart_buffer0[i]); + PRINTF("%c", uart_buffer0[i]); } - pc.printf("\r\n"); + PRINTF("\r\n"); } if (recieved_uart_dat1 == true){ recieved_uart_dat1 = false; for(int i = 0; i < uart_bf1_len; i++){ - pc.printf("%c", uart_buffer1[i]); + PRINTF("%c", uart_buffer1[i]); } - pc.printf("\r\n"); + PRINTF("\r\n"); } if (line_input == true){ line_input = false; @@ -202,29 +271,38 @@ } } } + if ((connection_tx == true) && (connection_rx == true) && (connection_1st == false)){ + PRINTF("Send *? Command\r\n"); + connection_1st = true; + wait(3.0); + // Client to Server(central to peripheral) + // 12345678901234567890 + const uint8_t *msg = "*? "; + uartTXCharacteristic.write(20, msg); + } ble.waitForEvent(); // save power } } void serialRxCallback(){ - char c = pc.getc(); + char c = GETC(); if (c == '\r') { linebuf_irq[linebf_irq_len++] = c; - pc.printf("\r\n"); + PRINTF("\r\n"); linebf_len = linebf_irq_len; strcpy((char *)linebuf, (char *)linebuf_irq); linebf_irq_len = 0; line_input = true; } else if ((c == '\b') && linebf_irq_len) { linebf_irq_len--; - pc.putc(c); - pc.putc(' '); - pc.putc(c); + PUTC(c); + PUTC(' '); + PUTC(c); } else if (((uint8_t)c >= ' ') && (linebf_irq_len < 20)) { linebuf_irq[linebf_irq_len++] = c; - pc.putc(c); + PUTC(c); } else if ( c == 0x1f ){ // Control+? - SCB->AIRCR = 0x05fa0004; // System RESET!! + NVIC_SystemReset(); // Reset } linebuf_irq[linebf_irq_len] = 0; } @@ -314,8 +392,8 @@ // connections int peer_board_index = get_board_index(params->peerAddr); if (peer_board_index != -1){ - pc.printf("adv peerAddr"); - pc.printf( + PRINTF("adv peerAddr"); + PRINTF( "[%02x %02x %02x %02x %02x %02x] rssi %+4d, isScanResponse %u, AdvertisementType %u", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], @@ -323,7 +401,7 @@ params->isScanResponse, params->type ); - pc.printf("\r\n"); + PRINTF("\r\n"); ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); } } @@ -357,14 +435,16 @@ if (characteristicP->getUUID().getShortUUID() == UARTServiceTXCharacteristicShortUUID) { - pc.printf("Sevice TX 0x%04x\r\n", UARTServiceTXCharacteristicShortUUID); + PRINTF("Sevice TX 0x%04x\r\n", UARTServiceTXCharacteristicShortUUID); uartTXCharacteristic = *characteristicP; + connection_tx = true; } else if (characteristicP->getUUID().getShortUUID() == UARTServiceRXCharacteristicShortUUID) { - pc.printf("Sevice RX 0x%04x\r\n", UARTServiceRXCharacteristicShortUUID); + PRINTF("Sevice RX 0x%04x\r\n", UARTServiceRXCharacteristicShortUUID); uartRXCharacteristic = *characteristicP; foundUartRXCharacteristic = true; + connection_rx = true; } } @@ -416,8 +496,11 @@ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ DEBUG("handle = %d ", params->handle); - pc.printf(" -> disconnected\r\n", params->handle); + PRINTF(" -> disconnected\r\n", params->handle); connected2server = false; + connection_1st = false; + connection_tx = false; + connection_rx = false; if (params->handle == SOFT_DEVICE_FATHER_HANDLE) { ble.startAdvertising(); // restart advertising } else {