test

Dependencies:   BufferedSerial WatchdogTimer

Revision:
4:1920e4a63bde
Parent:
3:455575531b33
Child:
5:3ba4c087853b
diff -r 455575531b33 -r 1920e4a63bde main.cpp
--- a/main.cpp	Fri Jun 08 05:36:22 2018 +0000
+++ b/main.cpp	Thu Jun 14 07:06:25 2018 +0000
@@ -13,14 +13,14 @@
 
 //  Object ---------------------------------------------------------------------
 BLE&        ble_uart = BLE::Instance();
-Serial      pc(P0_1, P0_3, 115200);     // DEBUG BOARD
-//Serial      pc(P0_9, P0_11, 115200);  // NEW BOARD
+//Serial      pc(P0_1, P0_3, 115200);     // DEBUG BOARD
+Serial      pc(P0_9, P0_11, 9600);  // NEW BOARD
 Ticker      main_timer;      // メインタイ216
 
 DigitalOut led(P0_6);
 
-DigitalOut state(P0_24);    // DEBUG BOARD
-//DigitalOut state(P0_3);   // NEW BOARD
+//DigitalOut state(P0_24);    // DEBUG BOARD
+DigitalOut state(P0_3);   // NEW BOARD
 
 
 //  ROM / Constant data --------------------------------------------------------
@@ -83,6 +83,7 @@
 unsigned int atow(char *str,unsigned short leng);
 unsigned long atoh(char *str,unsigned short leng);
 void ble_rs232c_cmd();
+void pc_rx();
 void pc_rs232c_cmd();
 void ble_write(char* dat);
 
@@ -92,6 +93,8 @@
 int main(void)
 {
     uart_init();
+
+    pc.attach( pc_rx, Serial::RxIrq );
     
     // メインタイマー設定(1ms)
     main_timer.attach(&main_timer_proc, 0.001);
@@ -107,7 +110,9 @@
     ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback);
     ble_uart.gap().setScanParams(500, 450);
     //ble_uart.gap().startScan(advertisementCallback);
-       
+    
+    int cnt = 0;
+    
     while(true) 
     {
         // allow notifications from Server(Peripheral)
@@ -135,6 +140,12 @@
             
             //ble_rs232c_cmd();
             pc_rs232c_cmd();
+
+            //pc.printf( "BLE = %d\r", cnt );
+            
+            cnt++;
+            
+            led = (cnt >> 10 ) & 0x0001;
         }
 
         ble_uart.waitForEvent();
@@ -301,10 +312,10 @@
     int k;
 
     // 初期化時のごみを出力
-    while( pc.readable() )
-    {
-        pc.getc();
-    }
+//    while( pc.readable() )
+//    {
+//        pc.getc();
+//    }
 
     // UART バッファ初期化 ///////////////////////////////////////////////
     pc_uart_buf.data_cnt = 0;
@@ -569,7 +580,7 @@
                         int left         = atow(ble_str[2], strlen(ble_str[2]));
                         int right        = atow(ble_str[3], strlen(ble_str[3]));
                         
-                        pc.printf( "POINT %4d %d %d\r", data_cnt, left, right ); 
+                        pc.printf( "POINT %d %d %d\r", data_cnt, left, right ); 
                     }
                     // コマンド U
                     else if( cmd_no == 1 )
@@ -631,6 +642,25 @@
     }
 }
 
+void pc_rx()
+{
+    NVIC_DisableIRQ(UART0_IRQn);   // 割り込み禁止
+
+    pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc();
+    pc_uart_buf.data_cnt++;
+    
+    if( pc_uart_buf.wp == SIO_BUF_SIZE-1 )
+    {
+        pc_uart_buf.wp = 0;
+    }
+    else
+    {
+        pc_uart_buf.wp++;
+    }
+
+    NVIC_EnableIRQ(UART0_IRQn);    // 割り込み許可
+}
+
 #define CMD_NUM     24
 
 char pc_str[16][10];
@@ -670,26 +700,14 @@
     unsigned int c, i, j;
     int rdat;
 
-    while( pc.readable() )
-    {
-        pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc();
-        pc_uart_buf.data_cnt++;
-
-        if( pc_uart_buf.wp == SIO_BUF_SIZE-1 )
-        {
-            pc_uart_buf.wp = 0;
-        }
-        else
-        {
-            pc_uart_buf.wp++;
-        }
-    }
-
     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);   // 割り込み禁止
+            
             rdat = pc_uart_buf.buf[pc_uart_buf.rp];
             pc_uart_buf.data_cnt--;
 
@@ -702,6 +720,8 @@
                 pc_uart_buf.rp++;
             }
 
+            NVIC_EnableIRQ(UART0_IRQn);    // 割り込み許可
+            
             if( rdat != '\n' )
             {
                 pc_cmd_buf.buf[pc_cmd_buf.data_cnt++] = rdat;
@@ -711,17 +731,20 @@
             {
                 int k = 0;
                 int l = 0;
-                for (i=0;i<pc_cmd_buf.data_cnt;i++)
+                int p = 0;
+
+                for( i=0;i<pc_cmd_buf.data_cnt;i++ )
                 {
-                    if( pc_cmd_buf.buf[i] == ' ' ||
-                        pc_cmd_buf.buf[i] == '\r' ||
-                        pc_cmd_buf.buf[i] == '\t' )
+                    if( ( ( pc_cmd_buf.buf[i] == ' ' ) && ( p > 0 ) ) || 
+                          ( pc_cmd_buf.buf[i] == '\r') )
                     {
                         pc_str[k++][l] = '\0';
                         l = 0;
+                        p = 0;
                     }
                     else
                     {
+                        p++;
                         pc_str[k][l++] = pc_cmd_buf.buf[i];
                     }
                 }
@@ -801,7 +824,7 @@
                             mac_board_0[2],
                             mac_board_0[1],
                             mac_board_0[0] );
-
+                        
                         pc.printf( "MSET OK\r\n" );
                     }
                     // コマンド
@@ -958,7 +981,7 @@
                     }
                     else
                     {
-                        //pc.printf("CMD_ERR\r");
+                        pc.printf("CMD_ERR_BLE\r");
                     }
                 }
             }