test

Dependencies:   BufferedSerial WatchdogTimer

Revision:
5:3ba4c087853b
Parent:
4:1920e4a63bde
--- a/main.cpp	Thu Jun 14 07:06:25 2018 +0000
+++ b/main.cpp	Tue Sep 29 23:56:23 2020 +0000
@@ -1,9 +1,12 @@
 //  Include --------------------------------------------------------------------
 #include "mbed.h"
 #include "BLE.h"
+#include "nrf_ble_gap.h"
 #include "DiscoveredCharacteristic.h"
 #include "DiscoveredService.h"
 #include "UARTService.h"
+#include "WatchdogTimer.h"
+#include "BufferedSerial.h"
 
 //  Definition -----------------------------------------------------------------
 #define     NUM_ONCE            20
@@ -11,10 +14,13 @@
 
 #define SOFT_DEVICE_FATHER_HANDLE   3
 
+#define VER     12   // ソフトバージョン(3桁 1.00 ->  100) // Ver12 20190314
+
 //  Object ---------------------------------------------------------------------
 BLE&        ble_uart = BLE::Instance();
 //Serial      pc(P0_1, P0_3, 115200);     // DEBUG BOARD
-Serial      pc(P0_9, P0_11, 9600);  // NEW BOARD
+//Serial      pc(P0_9, P0_11, 9600);  // NEW BOARD
+BufferedSerial      pc(P0_9, P0_11, 256);  // NEW BOARD
 Ticker      main_timer;      // メインタイ216
 
 DigitalOut led(P0_6);
@@ -22,9 +28,8 @@
 //DigitalOut state(P0_24);    // DEBUG BOARD
 DigitalOut state(P0_3);   // NEW BOARD
 
-
 //  ROM / Constant data --------------------------------------------------------
-Gap::Address_t    mac_board_0   = {0xFF, 0x42, 0xD5, 0x9C, 0x9C, 0xE1};
+Gap::Address_t    mac_board_0   = {0x72, 0x00, 0xD4, 0x34, 0x0B, 0xF7};
 
 //  RAM ------------------------------------------------------------------------
 Gap::Handle_t   connectionHandle        = 0xFFFF;
@@ -32,6 +37,7 @@
 DiscoveredCharacteristic uartRXCharacteristic;
 bool            foundUartRXCharacteristic = false;
 bool            connected2server        = false;
+bool            connected2server_d      = false;
 bool            connection_tx           = false;
 bool            connection_rx           = false;
 UARTService *   uartServicePtr          = NULL;
@@ -46,6 +52,9 @@
 unsigned long mac_2;
 unsigned long mac_3;
 
+Mutex           bletx_mutex;
+
+int connect_check = 0;
 
 #define SIO_BUF_SIZE    256     /* SIO リングバッファーサイズ (1K byte) */
 
@@ -64,6 +73,9 @@
 
 int timer_cnt;
 int timer_cnt_d;
+int read_cnt = 0;
+
+bool start_flag = false;
 
 //  Function prototypes --------------------------------------------------------
 //      BLE
@@ -87,6 +99,18 @@
 void pc_rs232c_cmd();
 void ble_write(char* dat);
 
+void led_p( int n )
+{
+    for( int i=0; i<n; i++ )
+    {
+        led = 1;
+        led = 0;
+    }
+    
+    led = 0;
+    led = 0;
+}
+
 //------------------------------------------------------------------------------
 //  Control Program
 //------------------------------------------------------------------------------
@@ -94,14 +118,20 @@
 {
     uart_init();
 
-    pc.attach( pc_rx, Serial::RxIrq );
+    //NVIC_SetPriority(UART0_IRQn,1);
+    //NVIC_SetPriority(RADIO_IRQn,1);
+    //NVIC_SetPriority(TIMER0_IRQn,1);
+    //NVIC_SetPriority(TIMER1_IRQn,1);
+    //NVIC_SetPriority(TIMER2_IRQn,1);
+
+    //pc.attach( pc_rx, Serial::RxIrq );
     
     // メインタイマー設定(1ms)
-    main_timer.attach(&main_timer_proc, 0.001);
-    
+    //main_timer.attach(&main_timer_proc, 0.001);
+        
     // opening message
-    pc.printf("UART Communication / Client(Central) side\r\n");
-    pc.printf("  need Server module (run BLE_Uart_Server program)\r\n");
+    //pc.printf("UART Communication / Client(Central) side\r\n");
+    //pc.printf("  need Server module (run BLE_Uart_Server program)\r\n");
     // Mixed role **************************************************************
     ble_uart.init();
     ble_uart.gap().onConnection(connectionCallback);
@@ -111,13 +141,33 @@
     ble_uart.gap().setScanParams(500, 450);
     //ble_uart.gap().startScan(advertisementCallback);
     
-    int cnt = 0;
+//    WatchdogTimer  wdt( 3 );
+    
+    uint32_t res = sd_ble_gap_tx_power_set( 4 );
+    
+    //pc.printf( "tx_power_set_res = %d\r", res );
+    
+    int cnt = 0;  
+    
+    Timer tt;
+    int msec;
     
+    connect_check = 0;
+    start_flag = false;
+     
+    int wait_cnt = 0;
+        
     while(true) 
-    {
+    {        
+//        wdt.kick();
+        
+        //led = !led;
+       
         // allow notifications from Server(Peripheral)
         if (foundUartRXCharacteristic &&
                 !ble_uart.gattClient().isServiceDiscoveryActive()) {
+                    
+            //pc.printf("--allow notifications-S\r");
             // need to do the following only once
             foundUartRXCharacteristic = false;
             uint16_t value = BLE_HVX_NOTIFICATION;
@@ -128,40 +178,129 @@
                 sizeof(uint16_t),
                 reinterpret_cast<const uint8_t *>(&value)
             );
+//led_p(6);
+//pc.printf( "notifications\r" );            
+            //pc.printf("--allow notifications-E\r");
+                       
+            connected2server = true;
         }
 
-        // タイマーカウント更新時(1ms以上経過している)
-        if( timer_cnt != timer_cnt_d )
-        {
-            timer_cnt_d = timer_cnt;
-        
-            state = connected2server;
-            led = connected2server;
+        // タイマーカウント更新時(10ms以上経過している)
+        //if( timer_cnt != timer_cnt_d )
+        //{
+            //timer_cnt_d = timer_cnt;
+            //pc.printf("%d\r", cnt);
+ #if 0
+            //if( connected2server == true )
+            //{
+                if( ( cnt % 2000 ) == 0 )
+                {               
+                    int8_t rssi;
+                    sd_ble_gap_rssi_get( connectionHandle, &rssi );
+                    
+                    pc.printf( "rssi = %d\r", rssi );
+                }
+            //}        
+ #endif       
+            if( ( connected2server_d == false ) && ( connected2server == true ) )
+            {
+                connect_check = 1;             
+            }
+            else if( ( connected2server_d == true ) && ( connected2server == false ) )
+            {
+                state = connected2server;
+                connect_check = 0;
+            }
+            
+            connected2server_d = connected2server;
             
-            //ble_rs232c_cmd();
-            pc_rs232c_cmd();
+            if( connect_check == 1 )
+            {
+                tt.stop();
+                tt.reset();
+                tt.start();
+
+                ble_write( "STOP\r" );
+                connect_check = 2;               
+            }
+            else if( connect_check == 2 )
+            {
+                msec = tt.read_ms();
 
-            //pc.printf( "BLE = %d\r", cnt );
-            
+                if( msec >= 500 )
+                {
+                    connect_check = 1;
+                }                
+            }
+            else if( connect_check == 3 )
+            {
+                msec = tt.read_ms();
+
+                if( msec >= 500 )
+                {
+                    connect_check = 0;
+                    
+                    state = connected2server;
+                }                        
+            }            
+           
+            ble_rs232c_cmd();
+          
+            pc_rs232c_cmd();
+           
             cnt++;
             
-            led = (cnt >> 10 ) & 0x0001;
-        }
+            //led = ( cnt >> 8 ) & 0x0001;         
+            
+            //led = !led;
+            
+            //wait(0.002);
+        //}
 
         ble_uart.waitForEvent();
+       
     }
 }
 
 void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *params)
 {
+    //pc.printf("onReceivedDataFromDeviceCallback\r");
+    
     if (params->type == BLE_HVX_NOTIFICATION) {
         if ((params->handle
                 == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) {
-            uart_bf_len = params->len;
-            strcpy((char *)uart_buffer, (char *)params->data);
-            received_uart_dat = true;         
+            
+            if( params->len <= BFSIZE )
+            {
+                uart_bf_len = params->len;
+     
+                //strcpy((char *)uart_buffer, (char *)params->data);
+                memcpy((char *)uart_buffer, (char *)params->data, uart_bf_len );
+                received_uart_dat = true;         
             
-            ble_rs232c_cmd();
+                //ble_rs232c_cmd();
+      
+                for( int i=0; i<uart_bf_len; i++ )
+                {
+                    //pc.putc(uart_buffer[i]);
+                    ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i];
+                    ble_uart_buf.data_cnt++;
+            
+                    if( ble_uart_buf.wp == SIO_BUF_SIZE-1 )
+                    {
+                        ble_uart_buf.wp = 0;
+                    }
+                    else
+                    {
+                        ble_uart_buf.wp++;
+                    }
+                }
+                uart_bf_len = 0;
+            }
+            else
+            {
+                
+            }   
         }
     }
 }
@@ -209,16 +348,16 @@
     // connections
     int peer_board_index = get_board_index(params->peerAddr);
     if (peer_board_index != -1) {
-        pc.printf("");
-        pc.printf(
-            "adv peerAddr [%02x %02x %02x %02x %02x %02x]\r\n",
-            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3],
-            params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]
-        );
-        pc.printf(
-            "rssi=%+4d, isScanResponse %u, AdvertisementType %u\r\n",
-            params->rssi, params->isScanResponse, params->type
-        );
+        //pc.printf("");
+        //pc.printf(
+        //    "adv peerAddr [%02x %02x %02x %02x %02x %02x]\r\n",
+        //    params->peerAddr[5], params->peerAddr[4], params->peerAddr[3],
+        //    params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]
+        //);
+        //pc.printf(
+        //    "rssi=%+4d, isScanResponse %u, AdvertisementType %u\r\n",
+        //    params->rssi, params->isScanResponse, params->type
+        //);
         ble_uart.gap().connect(
             params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
     }
@@ -226,6 +365,8 @@
 
 void serviceDiscoveryCallback(const DiscoveredService *service)
 {
+    //pc.printf("serviceDiscoveryCallback\r");
+    
     if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
         
     } else {
@@ -234,35 +375,45 @@
             
         }
     }
+    
+    //pc.printf("--serviceDiscoveryCallback\r");
 }
 
 void characteristicDiscoveryCallback(
     const DiscoveredCharacteristic *characteristicP)
-{
-    
+{ 
     if (characteristicP->getUUID().getShortUUID()
             == UARTServiceTXCharacteristicShortUUID) {
         uartTXCharacteristic = *characteristicP;
         connection_tx = true;
+//led_p(4);
+//pc.printf( "connection_tx\r" );      
+        //pc.printf("--connection_tx = true\r");
     } else if (characteristicP->getUUID().getShortUUID()
                == UARTServiceRXCharacteristicShortUUID) {
         uartRXCharacteristic = *characteristicP;
         foundUartRXCharacteristic = true;
         connection_rx = true;
+//led_p(5);
+//pc.printf( "connection_rx\r" );         
+        //pc.printf("--connection_rx = true\r");
     }
 }
 
 void discoveryTerminationCallback(Gap::Handle_t connectionHandle)
 {
+    //pc.printf("discoveryTerminationCallback\r");
 }
 
 // Mixed role ******************************************************************
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
     if (params->role == Gap::CENTRAL) {
-        pc.printf("connected as Client(Central) (handle = %d)\r\n\r",
-                  params->handle);
-        connected2server = true;
+
+//pc.printf( "connected\r" );
+        //pc.printf("connected as Client(Central) (handle = %d)\r\n\r",
+        //          params->handle);
+        //connected2server = true;
         connectionHandle = params->handle;
         ble_uart.gattClient().onServiceDiscoveryTermination(
             discoveryTerminationCallback);
@@ -272,21 +423,29 @@
             characteristicDiscoveryCallback
         );
     }
-    pc.printf(
-        "Client(Central/Myself)       %02x:%02x:%02x:%02x:%02x:%02x\r\n",
-        params->ownAddr[5], params->ownAddr[4], params->ownAddr[3],
-        params->ownAddr[2], params->ownAddr[1], params->ownAddr[0]
-    );
-    pc.printf(
-        "Connected Server(Peripheral) %02x:%02x:%02x:%02x:%02x:%02x\r\n",
-        params->peerAddr[5], params->peerAddr[4], params->peerAddr[3],
-        params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]
-    );
+    //pc.printf(
+    //    "Client(Central/Myself)       %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+    //    params->ownAddr[5], params->ownAddr[4], params->ownAddr[3],
+    //    params->ownAddr[2], params->ownAddr[1], params->ownAddr[0]
+    //);
+    //pc.printf(
+    //    "Connected Server(Peripheral) %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+    //    params->peerAddr[5], params->peerAddr[4], params->peerAddr[3],
+    //    params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]
+    //);
+    
+//led_p(3);
+
+    uint32_t res = sd_ble_gap_rssi_start( connectionHandle, 0, 0 );
+
+    //pc.printf( "rssi_start_res = %d\r", res );
 }
 
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    pc.printf(" -> disconnected\r\n", params->handle);
+//pc.printf( "disconnected\r" );
+    //pc.printf(" -> disconnected\r\n", params->handle);
     connected2server = false;
 //    connection_1st = false;
     connection_tx = false;
@@ -356,7 +515,9 @@
 /****************************************************************************/
 void main_timer_proc()
 {
+
      timer_cnt++;       // タイマカウントを加算
+
 }
 
 /****************************************************************************/
@@ -461,8 +622,28 @@
     return(ret_data);
 }
 
+#define CMD_NUM_BLE     19
+
 char ble_str[32][10];
-char ble_str2[10][8] = { "P      ", "U      ", "       ", "RESP   ", "       ", "       ", "       ", "       ", "       ", "       " };
+char ble_str2[CMD_NUM_BLE][8] = {   "START  ",     // 0
+                                    "STOP   ",     // 1
+                                    "       ",     // 2
+                                    "ISETL  ",     // 3
+                                    "ISETR  ",     // 4
+                                    "       ",     // 5
+                                    "TC     ",     // 6
+                                    "TS     ",     // 7
+                                    "IDGET  ",     // 8
+                                    "P      ",     // 9
+                                    "U      ",     // 10
+                                    "RSSI   ",     // 11
+                                    "M      ",     // 12
+                                    "X      ",     // 13
+                                    "PAUSE  ",     // 14
+                                    "REG09  ",     // 15    Ver12
+                                    "REG0A  ",     // 16    Ver12
+                                    "REG0B  ",     // 17    Ver12
+                                    "ICMR   "};    // 18    Ver12
 
 /****************************************************************************/
 /*  関数名 : rs232c_cmd                                                     */
@@ -475,6 +656,8 @@
     unsigned int c, i, j;
     int rdat;
 
+#if 0
+
     for( int i=0; i<uart_bf_len; i++ )
     {
         //pc.putc(uart_buffer[i]);
@@ -492,13 +675,17 @@
     }
 
     uart_bf_len = 0;   
+
+#endif 
     
     if( ble_uart_buf.data_cnt > 0 )
-    {
+    {                 
         unsigned int len = ble_uart_buf.data_cnt;
         
         for( c=0; c<len; c++ )
         {
+            //NVIC_DisableIRQ(RADIO_IRQn);   // 割り込み禁止
+                
             rdat = ble_uart_buf.buf[ble_uart_buf.rp];
             ble_uart_buf.data_cnt--;
 
@@ -511,6 +698,8 @@
                 ble_uart_buf.rp++;
             }
 
+            //NVIC_EnableIRQ(RADIO_IRQn);    // 割り込み許可
+
             if( rdat != '\n' )
             {
                 ble_cmd_buf.buf[ble_cmd_buf.data_cnt++] = rdat;
@@ -551,7 +740,7 @@
                     int ret;
                     int cmd_no = 0xFF;
 
-                    for( j=0; j<10; j++ )
+                    for( j=0; j<CMD_NUM_BLE; j++ )
                     {
                         ret = 1;
 
@@ -573,64 +762,171 @@
                         }
                     }
 
-                    // コマンド P
+                    // コマンド START
                     if( cmd_no == 0 )
                     {
-                        int data_cnt     = atow(ble_str[1], strlen(ble_str[1]));
-                        int left         = atow(ble_str[2], strlen(ble_str[2]));
-                        int right        = atow(ble_str[3], strlen(ble_str[3]));
-                        
-                        pc.printf( "POINT %d %d %d\r", data_cnt, left, right ); 
+                        start_flag = true;
+                        pc.printf( "START\r" );
                     }
-                    // コマンド U
+                    // コマンド STOP
                     else if( cmd_no == 1 )
                     {
-                        unsigned int high = atoh(ble_str[1], strlen(ble_str[1]));
-                        unsigned int low  = atoh(ble_str[2], strlen(ble_str[2]));
-
-                        pc.printf( "UID %08X %08X\r\n", high, low );
+                        if( connect_check == 0 )
+                        {
+                            pc.printf( "STOP\r" );
+                        }
+                        else
+                        {
+                            connect_check = 3;
+                        }
                     }
                     // コマンド
                     else if( cmd_no == 2 )
                     {
 
                     }
-                    // コマンド  RESP
+                    // コマンド ISETL
                     else if( cmd_no == 3 )
                     {
-                        pc.printf( "RESP\r" );
+                        pc.printf( "ISETL\r" );
                     }
-                    // コマンド
+                    // コマンド ISETR
                     else if( cmd_no == 4 )
                     {
-
+                        pc.printf( "ISETR\r" );
                     }
                     // コマンド
                     else if( cmd_no == 5 )
                     {
 
                     }
-
-                    // コマンド
+                    // コマンド TC
                     else if( cmd_no == 6 )
                     {
-
+                        pc.printf( "TC\r" );
                     }
-
-                    // コマンド
+                    // コマンド TS
                     else if( cmd_no == 7 )
                     {
-
+                        pc.printf( "TS\r" );
                     }
-                    // コマンド
+                    // コマンド IDGET
                     else if( cmd_no == 8 )
                     {
+                        pc.printf( "IDGET\r" );
+                    }
+                    // コマンド P
+                    else if( cmd_no == 9 )
+                    {
+                        int data_cnt     = atow(ble_str[1], strlen(ble_str[1]));
+                        int left         = atow(ble_str[2], strlen(ble_str[2]));
+                        int right        = atow(ble_str[3], strlen(ble_str[3]));
+                        
+                        led = 1;
+                        
+                        int8_t rssi;
+                        sd_ble_gap_rssi_get( connectionHandle, &rssi );
+                        
+                        if( rssi < 0 )
+                        {
+                            if( rssi < -99 )
+                            {
+                                rssi = -99;
+                            }
+                            
+                            rssi += 100;
+                        }
+                        else
+                        {
+                            rssi = 0;
+                        }                     
+                        
+                        pc.printf( "POINT %d %d %d %d\r", data_cnt, left, right, rssi );
+                                            
+                        led = 0;
+                        
+                        //wait(0.001);                     
+                    }
+                    // コマンド U
+                    else if( cmd_no == 10 )
+                    {
+                        unsigned int high = atoh(ble_str[1], strlen(ble_str[1]));
+                        unsigned int low  = atoh(ble_str[2], strlen(ble_str[2]));
 
+                        pc.printf( "UID %08X %08X\r\n", high, low );
+                    }
+                    // コマンド RSSI
+                    else if( cmd_no == 11 )
+                    {
+                        int8_t rssi;
+                        sd_ble_gap_rssi_get( connectionHandle, &rssi );
+                        
+                        if( rssi < 0 )
+                        {
+                            if( rssi < -99 )
+                            {
+                                rssi = -99;
+                            }
+                            
+                            rssi += 100;
+                        }
+                        else
+                        {
+                            rssi = 0;
+                        }
+                        
+                        pc.printf( "RSSI %d\r\n", rssi );                  
+                    }
+                    // コマンド MES
+                    else if( cmd_no == 12 )
+                    {
+                        unsigned int level = atow(ble_str[1], strlen(ble_str[1]));
+                        unsigned long uid_L  = atoh(ble_str[2], strlen(ble_str[2]));
+                        
+                        pc.printf( "MES %d %08X\r\n", level, uid_L );
                     }
                     // コマンド
-                    else if( cmd_no == 9 )
+                    else if( cmd_no == 13 )
+                    {
+                        unsigned int high = atoh(ble_str[1], strlen(ble_str[1]));
+                        unsigned int low  = atoh(ble_str[2], strlen(ble_str[2]));
+
+                        pc.printf( "MMR %08X %08X\r\n", high, low );
+                    }
+                    // コマンド PAUSE
+                    else if( cmd_no == 14 )
                     {
-
+                        if( connect_check == 0 )
+                        {
+                            pc.printf( "PAUSE\r" );
+                        }
+                        else
+                        {
+                            connect_check = 3;
+                        }
+                    }
+                    // コマンド REG09  Ver12
+                    else if( cmd_no == 15 )
+                    {
+                        pc.printf( "REG09\r" );
+                    }
+                    // コマンド REG0A  Ver12
+                    else if( cmd_no == 16 )
+                    {
+                        pc.printf( "REG0A\r" );
+                    }
+                    // コマンド REG0B  Ver12
+                    else if( cmd_no == 17 )
+                    {
+                        pc.printf( "REG0B\r" );
+                    }
+                    // コマンド ICMR  Ver12
+                    else if( cmd_no == 18 )
+                    {
+                        unsigned int adr = atow(ble_str[1], strlen(ble_str[1]));
+                        unsigned int dat = atoh(ble_str[2], strlen(ble_str[2]));
+                        
+                        pc.printf( "ICMR %d %08X\r", adr, dat );                                         
                     }
                     else
                     {
@@ -644,7 +940,7 @@
 
 void pc_rx()
 {
-    NVIC_DisableIRQ(UART0_IRQn);   // 割り込み禁止
+    //NVIC_DisableIRQ(UART0_IRQn);   // 割り込み禁止
 
     pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc();
     pc_uart_buf.data_cnt++;
@@ -658,36 +954,33 @@
         pc_uart_buf.wp++;
     }
 
-    NVIC_EnableIRQ(UART0_IRQn);    // 割り込み許可
+    //NVIC_EnableIRQ(UART0_IRQn);    // 割り込み許可
 }
 
-#define CMD_NUM     24
+#define CMD_NUM     21
 
 char pc_str[16][10];
 char pc_str2[CMD_NUM][8] = { "START  ",     // 0
                              "STOP   ",     // 1
-                             "       ",     // 2
-                             "MSET   ",     // 3
-                             "       ",     // 4
-                             "       ",     // 5
-                             "       ",     // 6
-                             "ISETL  ",     // 7
-                             "ISETR  ",     // 8
-                             "       ",     // 9
-                             "       ",     // 10
-                             "CONNECT",     // 11
-                             "DISCO  ",     // 12
-                             "INTVL  ",     // 13
-                             "NTC    ",     // 14
-                             "OTC    ",     // 15
-                             "       ",     // 16
-                             "       ",     // 17
-                             "IDGET  ",     // 18
-                             "       ",     // 19
-                             "       ",     // 20
-                             "       ",     // 21
-                             "       ",     // 22
-                             "       "};    // 23
+                             "VER    ",     // 2
+                             "ISETL  ",     // 3
+                             "ISETR  ",     // 4
+                             "CONNECT",     // 5
+                             "DISC   ",     // 6
+                             "MMR    ",     // 7
+                             "TC     ",     // 8
+                             "MES    ",     // 9
+                             "IDGET  ",     // 10
+                             "RSSI   ",     // 11
+                             "RES    ",     // 12
+                             "READ   ",     // 13
+                             "PRES   ",     // 14
+                             "TS     ",     // 15
+                             "PAUSE  ",     // 16
+                             "REG09  ",     // 17   Ver12
+                             "REG0A  ",     // 18   Ver12
+                             "REG0B  ",     // 19   Ver12
+                             "ICMR   "};    // 20   Ver12
 
 /****************************************************************************/
 /*  関数名 : rs232c_cmd                                                     */
@@ -700,13 +993,32 @@
     unsigned int c, i, j;
     int rdat;
 
+#if 1
+    while( pc.readable() )
+    {
+        pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc();
+        pc_uart_buf.data_cnt++;
+ 
+        //pc.putc(pc_uart_buf.buf[pc_uart_buf.wp]);
+        
+        if( pc_uart_buf.wp == SIO_BUF_SIZE-1 )
+        {
+            pc_uart_buf.wp = 0;
+        }
+        else
+        {
+            pc_uart_buf.wp++;
+        }
+    }
+#endif
+
     if( pc_uart_buf.data_cnt > 0 )
-    {        
+    {   
         unsigned int len = pc_uart_buf.data_cnt;
         
         for( c=0; c<len; c++ )
         {
-            NVIC_DisableIRQ(UART0_IRQn);   // 割り込み禁止
+            //NVIC_DisableIRQ(UART0_IRQn);   // 割り込み禁止
             
             rdat = pc_uart_buf.buf[pc_uart_buf.rp];
             pc_uart_buf.data_cnt--;
@@ -720,7 +1032,7 @@
                 pc_uart_buf.rp++;
             }
 
-            NVIC_EnableIRQ(UART0_IRQn);    // 割り込み許可
+            //NVIC_EnableIRQ(UART0_IRQn);    // 割り込み許可
             
             if( rdat != '\n' )
             {
@@ -789,23 +1101,87 @@
                     {
                         ble_write( "START\r" );
 
-                        pc.printf( "START OK\r\n" );
+                        //pc.printf( "START OK\r\n" );
                     }
                     // コマンド STOP
                     else if( cmd_no == 1 )
                     {
                         ble_write( "STOP\r" );
-
-                        pc.printf( "STOP OK\r\n" );
                     }
-                    // コマンド
-                    else if (cmd_no == 2)
+                    // コマンド VER
+                    else if( cmd_no == 2 )
                     {
-
+                        pc.printf( "VER %03d\r", VER );
                     }
-                    // コマンド MSET
+                    // コマンド ISETL
                     else if( cmd_no == 3 )
                     {
+                        int no = atow(pc_str[1], strlen(pc_str[1]));
+                        int id_LH = atoh(pc_str[2], strlen(pc_str[2]));
+                        int id_LL = atoh(pc_str[3], strlen(pc_str[3]));
+
+                        char buf[BFSIZE];
+                        
+                        if( no == 0 )
+                        {
+                            sprintf( buf, "A %8X %8X\r", id_LH, id_LL );
+                        }
+                        else if( no == 1 )
+                        {
+                            sprintf( buf, "B %8X %8X\r", id_LH, id_LL );
+                        }
+                        else if( no == 2 )
+                        {
+                            sprintf( buf, "C %8X %8X\r", id_LH, id_LL );
+                        }
+                        else if( no == 3 )
+                        {
+                            sprintf( buf, "D %8X %8X\r", id_LH, id_LL );
+                        }
+                           
+                        //sprintf( buf, "L %8X %8X\r", id_LH, id_LL );
+ 
+                        ble_write( buf );
+                        
+                        //pc.printf( "ISETL OK\r\n" );
+                    }
+                    // コマンド ISETR
+                    else if( cmd_no == 4 )
+                    {
+                        int no = atow(pc_str[1], strlen(pc_str[1]));
+                        int id_RH = atoh(pc_str[2], strlen(pc_str[2]));
+                        int id_RL = atoh(pc_str[3], strlen(pc_str[3]));
+
+                        char buf[BFSIZE];
+                        
+                        if( no == 0 )
+                        {
+                            sprintf( buf, "E %8X %8X\r", id_RH, id_RL );
+                        }
+                        else if( no == 1 )
+                        {
+                            sprintf( buf, "F %8X %8X\r", id_RH, id_RL );
+                        }
+                        else if( no == 2 )
+                        {
+                            sprintf( buf, "G %8X %8X\r", id_RH, id_RL );
+                        }
+                        else if( no == 3 )
+                        {
+                            sprintf( buf, "H %8X %8X\r", id_RH, id_RL );
+                        } 
+                                                      
+                        //sprintf( buf, "R %8X %8X\r", id_RH, id_RL );
+ 
+                        ble_write( buf );
+                        
+                        //pc.printf( "ISETR OK\r\n" );
+                    }
+                    // コマンド CONNECT
+                    else if( cmd_no == 5 )
+                    {
+//pc.printf( "CONNECT\r" );
+//led_p(1);
                         mac_1 = atoh(pc_str[1], strlen(pc_str[1]));
                         mac_2 = atoh(pc_str[2], strlen(pc_str[2]));
                         mac_3 = atoh(pc_str[3], strlen(pc_str[3]));
@@ -817,167 +1193,124 @@
                         mac_board_0[4] = ( mac_1 >> 0 ) & 0x00FF;
                         mac_board_0[5] = ( mac_1 >> 8 ) & 0x00FF;
 
-                        pc.printf( "%02X %02X %02X %02X %02X %02X\r", 
-                            mac_board_0[5],
-                            mac_board_0[4],
-                            mac_board_0[3],
-                            mac_board_0[2],
-                            mac_board_0[1],
-                            mac_board_0[0] );
+                        // CONNECT処理 
+                        ble_uart.gap().connect( mac_board_0, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+//led_p(2);
                         
-                        pc.printf( "MSET OK\r\n" );
+     
                     }
-                    // コマンド
-                    else if( cmd_no == 4 )
-                    {
-
-                    }
-                    // コマンド
-                    else if( cmd_no == 5 )
-                    {
-
-                    }
-                    // コマンド
+                    // コマンド DISC
                     else if( cmd_no == 6 )
                     {
-
+                        // DISC処理
+                        Gap::DisconnectionReason_t reason;
+                        ble_uart.gap().disconnect( reason );
                     }
-                    // コマンド ISETL
+                    // コマンド MMR
                     else if( cmd_no == 7 )
                     {
-                        int id_LH = atoh(pc_str[1], strlen(pc_str[1]));
-                        int id_LL = atoh(pc_str[2], strlen(pc_str[2]));
-
-                        char buf[BFSIZE];
-                                
-                        sprintf( buf, "L %8X %8X\r", id_LH, id_LL );
- 
+                        int no = atow(pc_str[1], strlen(pc_str[1]));
+                        
+                        char buf[BFSIZE];  
+                        sprintf( buf, "MMR %d\r", no);
                         ble_write( buf );
-                        
-                        pc.printf( "ISETL OK\r\n" );
                     }
-                    // コマンド ISETR
+                    // コマンド  TC
                     else if( cmd_no == 8 )
                     {
-                        int id_RH = atoh(pc_str[1], strlen(pc_str[1]));
-                        int id_RL = atoh(pc_str[2], strlen(pc_str[2]));
+                        unsigned int not_touch_cnt = atow(pc_str[1], strlen(pc_str[1]));
+                        unsigned int touch_cnt = atow(pc_str[2], strlen(pc_str[2]));
+                        
+                        char buf[BFSIZE];                               
+                        sprintf( buf, "TC %d %d\r", not_touch_cnt, touch_cnt);
+                        ble_write( buf );
 
-                        char buf[BFSIZE];
-                                
-                        sprintf( buf, "R %8X %8X\r", id_RH, id_RL );
- 
-                        ble_write( buf );
-                        
-                        pc.printf( "ISETR OK\r\n" );
+                        //pc.printf( "NTC OK\r\n" );
                     }
-                    // コマンド
+                    // コマンド MES
                     else if( cmd_no == 9 )
                     {
-
+                        ble_write( "MES\r" );
                     }
-                    // コマンド
+                    // コマンド IDGET
                     else if( cmd_no == 10 )
                     {
-
+                        ble_write( "IDGET\r" );
                     }
-                    // コマンド CONNECT
+                    // コマンド RSSI
                     else if( cmd_no == 11 )
                     {
-                        if( connected2server == false )
-                        {
-                            // CONNECT処理 
-                            ble_uart.gap().connect( mac_board_0, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
-                            
-                            pc.printf( "CONNECT OK\r\n" );
-                        }
-                        else
-                        {
-                            // DISC処理
-                            Gap::DisconnectionReason_t reason; 
-                            ble_uart.gap().disconnect( reason );
-                             
-                            pc.printf( "CONNECT NG\r\n" );
-                        }
+                        ble_write( "RSSI\r" );
                     }
-                    // コマンド DISC
+                    // コマンド
                     else if( cmd_no == 12 )
                     {
-                        if( connected2server == true )
-                        {
-                            // DISC処理
-                            Gap::DisconnectionReason_t reason; 
-                            ble_uart.gap().disconnect( reason );
-                        }
-                        
-                        pc.printf( "DISC OK\r\n" );
+                        pc.printf( "RES\r" );
                     }
-                    // コマンド  INTVL
+                    // コマンド READ
                     else if( cmd_no == 13 )
                     {
-                        unsigned int interval = atow(pc_str[1], strlen(pc_str[1]));
+                        //led = 1;
+                        ble_write( "READ\r" );
+                        
+                        //pc.printf( "READ_BLE\r" );
+                        //char buf[BFSIZE];                               
+                        //sprintf( buf, "READ %d\r", read_cnt++ );
+                        //ble_write( buf );
 
-                        char buf[BFSIZE];                               
-                        sprintf( buf, "INTVL %d\r", interval );
-                        ble_write( buf );
-
-                        pc.printf( "INTVL OK\r\n" );
+                        //ble_write( "~R\r" );
                     }
-                    // コマンド  NTC
+                    // コマンド PRES
                     else if( cmd_no == 14 )
                     {
-                        unsigned int not_touch_cnt = atow(pc_str[1], strlen(pc_str[1]));
-
-                        char buf[BFSIZE];                               
-                        sprintf( buf, "NTC %d\r", not_touch_cnt );
-                        ble_write( buf );
-
-                        pc.printf( "NTC OK\r\n" );
+                        ble_write( "PRES\r" );
                     }
-                    // コマンド OTC
+                    // コマンド TS
                     else if( cmd_no == 15 )
                     {
-                        unsigned int touch_cnt = atow(pc_str[1], strlen(pc_str[1]));
-
+                        unsigned int touch_start = atow(pc_str[1], strlen(pc_str[1]));
+                        
                         char buf[BFSIZE];                               
-                        sprintf( buf, "OTC %d\r", touch_cnt );
-                        ble_write( buf );
-
-                        pc.printf( "OTC OK\r\n" );
+                        sprintf( buf, "TS %d\r", touch_start );
+                        ble_write( buf );                      
                     }
-                    // コマンド
+                    // コマンド PAUSE
                     else if( cmd_no == 16 )
                     {
-                        
+                        ble_write( "PAUSE\r" );
                     }
-                    // コマンド
+                    // コマンド REG09  Ver12
                     else if( cmd_no == 17 )
                     {
-                    
+                        char reg09 = ( char )( atoh(pc_str[1], strlen(pc_str[1])) );
+                        char buf[BFSIZE];                               
+                        sprintf( buf, "REG09 %02X\r", reg09 );
+                        ble_write( buf );
                     }
-                    // コマンド IDGET
+                    // コマンド REG0A  Ver12
                     else if( cmd_no == 18 )
                     {
-                        ble_write( "IDGET\r\n" );
+                        char reg0A = ( char )( atoh(pc_str[1], strlen(pc_str[1])) );
+                        char buf[BFSIZE];                               
+                        sprintf( buf, "REG0A %02X\r", reg0A );
+                        ble_write( buf );  
                     }
-                    // コマンド
+                    // コマンド REG0B  Ver12
                     else if( cmd_no == 19 )
                     {
-
+                        char reg0B = ( char )( atoh(pc_str[1], strlen(pc_str[1])) );
+                        char buf[BFSIZE];                               
+                        sprintf( buf, "REG0B %02X\r", reg0B );
+                        ble_write( buf );  
                     }
-                    // コマンド
+                    // コマンド ICMR  Ver12
                     else if( cmd_no == 20 )
                     {
-                        
-                    }
-                    // コマンド
-                    else if( cmd_no == 21 )
-                    {
+                        unsigned int adr = atow(pc_str[1], strlen(pc_str[1]));
                         
-                    }
-                    // コマンド
-                    else if( cmd_no == 22 )
-                    {
-                    
+                        char buf[BFSIZE];                               
+                        sprintf( buf, "ICMR %d\r", adr );
+                        ble_write( buf );                       
                     }
                     else
                     {
@@ -997,6 +1330,10 @@
     
     if( connected2server == true )
     {
+        bletx_mutex.lock();
+                   
         uartTXCharacteristic.write(NUM_ONCE, linebf_irq);
+        
+        bletx_mutex.unlock();
     }   
 }
\ No newline at end of file