this is a new program based on BLE_Uart_Peripheral. Change program name due to relation Client&Peripheral to Client&Server

Dependencies:   RingBuffer nRF51_Vdd nRF51_WakeUp

This is a BLE Server (Device) program for nRF51.
You can communicate with mbed BLE using "BLE_Uart_Client" program as follows.
/users/kenjiArai/code/BLE_Uart_Client/
Please refer following my notebook.
/users/kenjiArai/notebook/ble-client-and-peripheral-using-switch-sience-ty51/#

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Mon Jun 13 09:54:32 2016 +0000
Parent:
4:a1b642566003
Child:
6:16e1f9d065a3
Commit message:
added nRF51_LowPwr library and updated other libraries.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51_LowPwr.lib Show annotated file Show diff for this revision Revisions of this file
nRF51_Vdd.lib Show annotated file Show diff for this revision Revisions of this file
nRF51_WakeUp.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Apr 21 11:29:38 2016 +0000
+++ b/main.cpp	Mon Jun 13 09:54:32 2016 +0000
@@ -22,7 +22,7 @@
  *      http://mbed.org/users/kenjiArai/
  *
  *      Started:  March     7th, 2016
- *      Revised:  April    21st, 2016
+ *      Revised:  June     13th, 2016
  *
  *  Original program:
  *      BLE_LoopbackUART
@@ -39,12 +39,22 @@
 #include "UARTService.h"
 #include "nRF51_Vdd.h"
 #include "nRF51_WakeUp.h"
+#include "nRF51_lowpwr.h"
 
 //  Definition ------------------------------------------------------------------------------------
-#define GOTO_SLEEP_MODE         0
-#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    GOTO_SLEEP_MODE                 0   // If you set 1, you need to connected LED1 and P0_0
+                                               // Please refer nRF51_WakeUP library
+
+#if USE_DEVICE_STDIO_MESSAGES
 #define DEBUG(...) { printf(__VA_ARGS__); }
 #else
 #define DEBUG(...)
@@ -63,7 +73,44 @@
 //  ROM / Constant data ---------------------------------------------------------------------------
 #warning "You need to confirm your device name."
 const static char       DEVICE_NAME[] = "UART_S";
-
+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::Address_t   my_mac;
@@ -81,6 +128,7 @@
 int              linebf_irq_len  = 0;
 uint8_t          linebuf[24];
 int              linebf_len      = 0;
+uint32_t         sleep_time      = 30;   // unit:second
 
 //  Function prototypes ---------------------------------------------------------------------------
 //      BLE
@@ -92,10 +140,10 @@
 void action_tx_help(void);
 void action_tx_vdd(void);
 void action_tx_temperature(void);
-void action_tx_pwrup(void);
-void action_tx_pwrdwn(void);
+void action_tx_wait_time(void);
 void fill_space_n(uint8_t *bf, uint8_t n);
 void Update_Values(void);
+int  xatoi (char **str, int32_t *res);
 //      Interrupt related
 void action_tx_quit(void);
 void interrupt_by_sw(void);
@@ -105,6 +153,7 @@
 //  Control Program
 //-------------------------------------------------------------------------------------------------
 int main(void){
+    LowPwr set_lowpwr(&lowpwr_table);
     connectedLED = 0;
     pc.attach(&serialRxCallback, Serial::RxIrq);
     ticker.attach(periodicCallback, 1);
@@ -150,7 +199,7 @@
     while(true){
         if (time_out){
 #if GOTO_SLEEP_MODE
-            wakeup.set_and_wait(120);
+            wakeup.set_and_wait(sleep_time);
             while(true){    // never come here but just in case
                 deepsleep();
             }
@@ -179,6 +228,9 @@
                     case 'q':
                         action_tx_quit();
                         break;
+                    case 'w':
+                        action_tx_wait_time();
+                        break;
                     case 'h':
                     case '?':
                         action_tx_help();
@@ -262,6 +314,11 @@
     Update_Values();
     wait(0.2);
                 //          12345678901234567890
+    sprintf((char *)tx_buf,"*w:wait time/ w 120");
+    tx_len = strlen((const char *)tx_buf); 
+    Update_Values();
+    wait(0.2);
+                //          12345678901234567890
     sprintf((char *)tx_buf,"*q:quit/sleep");
     tx_len = strlen((const char *)tx_buf); 
     Update_Values();
@@ -287,6 +344,93 @@
     Update_Values();
 }
 
+
+//  Change string -> integer
+int xatoi (char **str, int32_t *res){
+    unsigned long val;
+    unsigned char c, radix, s = 0;
+
+    for (;;){
+        c = **str;
+        if (c == 0){ return 0;}
+        if (c == '-'){ break;}
+        if (c == '+'){
+            (*str)++;
+            c = **str;
+        }
+        if (c>='0'&& c<='9'){
+            break;
+        } else {
+            (*str)++;
+            c = **str;
+        }
+    }
+    if (c == '-') {
+        s = 1;
+        c = *(++(*str));
+    }
+    if (c == '0') {
+        c = *(++(*str));
+        if (c <= ' ') {
+            *res = 0;
+            return 1;
+        }
+        if (c == 'x') {
+            radix = 16;
+            c = *(++(*str));
+        } else {
+            if (c == 'b') {
+                radix = 2;
+                c = *(++(*str));
+            } else {
+                if ((c >= '0')&&(c <= '9')) {
+                    radix = 8;
+                }   else {
+                    return 0;
+                }
+            }
+        }
+    } else {
+        if ((c < '1')||(c > '9')) {
+            return 0;
+        }
+        radix = 10;
+    }
+    val = 0;
+    while (c > ' ') {
+        if (c >= 'a') c -= 0x20;
+        c -= '0';
+        if (c >= 17) {
+            c -= 7;
+            if (c <= 9) return 0;
+        }
+        if (c >= radix) return 0;
+        val = val * radix + c;
+        c = *(++(*str));
+    }
+    if (s) val = -val;
+    *res = val;
+    return 1;
+}
+
+void action_tx_wait_time(){
+    int32_t dt;
+    char *p;
+
+    p = (char *)(rx_buf);
+    p += 2; // point to rx_buf[2]
+    if (xatoi(&p, &dt)){
+        sleep_time = dt;    // set next wake-up period
+    } else {
+        DEBUG("data is unknown!\r\n");
+    }
+    DEBUG("slp_t:%d\r\n", sleep_time);
+                //          12345678901234567890
+    sprintf((char *)tx_buf, "W=%d", sleep_time);
+    tx_len = strlen((const char *)tx_buf);
+    Update_Values();
+}
+
 void action_tx_quit(){
 #if GOTO_SLEEP_MODE 
     ticker.detach();
@@ -295,7 +439,7 @@
     tx_len = strlen((const char *)tx_buf); 
     Update_Values();
     wait(1.0);
-    wakeup.set_and_wait(120);
+    wakeup.set_and_wait(sleep_time);
     while(true){    // never come here but just in case
         deepsleep();
     }
--- a/mbed.bld	Thu Apr 21 11:29:38 2016 +0000
+++ b/mbed.bld	Mon Jun 13 09:54:32 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/082adc85693f
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nRF51_LowPwr.lib	Mon Jun 13 09:54:32 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/kenjiArai/code/nRF51_LowPwr/#ffd5168de563
--- a/nRF51_Vdd.lib	Thu Apr 21 11:29:38 2016 +0000
+++ b/nRF51_Vdd.lib	Mon Jun 13 09:54:32 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/kenjiArai/code/nRF51_Vdd/#11c9f031975d
+https://developer.mbed.org/users/kenjiArai/code/nRF51_Vdd/#9b1e219a7695
--- a/nRF51_WakeUp.lib	Thu Apr 21 11:29:38 2016 +0000
+++ b/nRF51_WakeUp.lib	Mon Jun 13 09:54:32 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/kenjiArai/code/nRF51_WakeUp/#f50677171f2d
+https://developer.mbed.org/users/kenjiArai/code/nRF51_WakeUp/#cc54cd1df555