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: BufferedSerial WatchdogTimer
main.cpp
00001 // Include -------------------------------------------------------------------- 00002 #include "mbed.h" 00003 #include "BLE.h" 00004 #include "nrf_ble_gap.h" 00005 #include "DiscoveredCharacteristic.h" 00006 #include "DiscoveredService.h" 00007 #include "UARTService.h" 00008 #include "WatchdogTimer.h" 00009 #include "BufferedSerial.h" 00010 00011 // Definition ----------------------------------------------------------------- 00012 #define NUM_ONCE 20 00013 #define BFSIZE (NUM_ONCE+4) 00014 00015 #define SOFT_DEVICE_FATHER_HANDLE 3 00016 00017 #define VER 12 // ソフトバージョン(3桁 1.00 -> 100) // Ver12 20190314 00018 00019 // Object --------------------------------------------------------------------- 00020 BLE& ble_uart = BLE::Instance(); 00021 //Serial pc(P0_1, P0_3, 115200); // DEBUG BOARD 00022 //Serial pc(P0_9, P0_11, 9600); // NEW BOARD 00023 BufferedSerial pc(P0_9, P0_11, 256); // NEW BOARD 00024 Ticker main_timer; // メインタイ216 00025 00026 DigitalOut led(P0_6); 00027 00028 //DigitalOut state(P0_24); // DEBUG BOARD 00029 DigitalOut state(P0_3); // NEW BOARD 00030 00031 // ROM / Constant data -------------------------------------------------------- 00032 Gap::Address_t mac_board_0 = {0x72, 0x00, 0xD4, 0x34, 0x0B, 0xF7}; 00033 00034 // RAM ------------------------------------------------------------------------ 00035 Gap::Handle_t connectionHandle = 0xFFFF; 00036 DiscoveredCharacteristic uartTXCharacteristic; 00037 DiscoveredCharacteristic uartRXCharacteristic; 00038 bool foundUartRXCharacteristic = false; 00039 bool connected2server = false; 00040 bool connected2server_d = false; 00041 bool connection_tx = false; 00042 bool connection_rx = false; 00043 UARTService * uartServicePtr = NULL; 00044 Gap::Address_t my_mac; 00045 int my_board_index = -1; 00046 bool received_uart_dat = false; 00047 int8_t uart_buffer[BFSIZE]; 00048 uint8_t uart_bf_len; 00049 volatile bool rx_isr_busy = false; 00050 00051 unsigned long mac_1; 00052 unsigned long mac_2; 00053 unsigned long mac_3; 00054 00055 Mutex bletx_mutex; 00056 00057 int connect_check = 0; 00058 00059 #define SIO_BUF_SIZE 256 /* SIO リングバッファーサイズ (1K byte) */ 00060 00061 typedef struct { 00062 unsigned short data_cnt; /* リング・バッファ データカウント */ 00063 unsigned short rp; /* リング・バッファ Read ポインタ */ 00064 unsigned short wp; /* リング・バッファ Write ポインタ */ 00065 unsigned char buf[SIO_BUF_SIZE]; /* リング・バッファ */ 00066 } ring_cnt_t; /* リング・バッファ 制御用構造体 */ 00067 00068 ring_cnt_t pc_uart_buf; // 通信用リングバッファ 00069 ring_cnt_t pc_cmd_buf; // 通信用コマンドバッファ 00070 00071 ring_cnt_t ble_uart_buf; // 通信用リングバッファ 00072 ring_cnt_t ble_cmd_buf; // 通信用コマンドバッファ 00073 00074 int timer_cnt; 00075 int timer_cnt_d; 00076 int read_cnt = 0; 00077 00078 bool start_flag = false; 00079 00080 // Function prototypes -------------------------------------------------------- 00081 // BLE 00082 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *); 00083 void serviceDiscoveryCallback(const DiscoveredService *); 00084 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *); 00085 void discoveryTerminationCallback(Gap::Handle_t ); 00086 void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *); 00087 void connectionCallback(const Gap::ConnectionCallbackParams_t *); 00088 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); 00089 // Pre-check 00090 bool mac_equals(const Gap::Address_t, const Gap::Address_t); 00091 int get_board_index(const Gap::Address_t); 00092 00093 void uart_init(); 00094 void main_timer_proc(); 00095 unsigned int atow(char *str,unsigned short leng); 00096 unsigned long atoh(char *str,unsigned short leng); 00097 void ble_rs232c_cmd(); 00098 void pc_rx(); 00099 void pc_rs232c_cmd(); 00100 void ble_write(char* dat); 00101 00102 void led_p( int n ) 00103 { 00104 for( int i=0; i<n; i++ ) 00105 { 00106 led = 1; 00107 led = 0; 00108 } 00109 00110 led = 0; 00111 led = 0; 00112 } 00113 00114 //------------------------------------------------------------------------------ 00115 // Control Program 00116 //------------------------------------------------------------------------------ 00117 int main(void) 00118 { 00119 uart_init(); 00120 00121 //NVIC_SetPriority(UART0_IRQn,1); 00122 //NVIC_SetPriority(RADIO_IRQn,1); 00123 //NVIC_SetPriority(TIMER0_IRQn,1); 00124 //NVIC_SetPriority(TIMER1_IRQn,1); 00125 //NVIC_SetPriority(TIMER2_IRQn,1); 00126 00127 //pc.attach( pc_rx, Serial::RxIrq ); 00128 00129 // メインタイマー設定(1ms) 00130 //main_timer.attach(&main_timer_proc, 0.001); 00131 00132 // opening message 00133 //pc.printf("UART Communication / Client(Central) side\r\n"); 00134 //pc.printf(" need Server module (run BLE_Uart_Server program)\r\n"); 00135 // Mixed role ************************************************************** 00136 ble_uart.init(); 00137 ble_uart.gap().onConnection(connectionCallback); 00138 ble_uart.gap().onDisconnection(disconnectionCallback); 00139 // Client(Central) role **************************************************** 00140 ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback); 00141 ble_uart.gap().setScanParams(500, 450); 00142 //ble_uart.gap().startScan(advertisementCallback); 00143 00144 // WatchdogTimer wdt( 3 ); 00145 00146 uint32_t res = sd_ble_gap_tx_power_set( 4 ); 00147 00148 //pc.printf( "tx_power_set_res = %d\r", res ); 00149 00150 int cnt = 0; 00151 00152 Timer tt; 00153 int msec; 00154 00155 connect_check = 0; 00156 start_flag = false; 00157 00158 int wait_cnt = 0; 00159 00160 while(true) 00161 { 00162 // wdt.kick(); 00163 00164 //led = !led; 00165 00166 // allow notifications from Server(Peripheral) 00167 if (foundUartRXCharacteristic && 00168 !ble_uart.gattClient().isServiceDiscoveryActive()) { 00169 00170 //pc.printf("--allow notifications-S\r"); 00171 // need to do the following only once 00172 foundUartRXCharacteristic = false; 00173 uint16_t value = BLE_HVX_NOTIFICATION; 00174 ble_uart.gattClient().write( 00175 GattClient::GATT_OP_WRITE_REQ, 00176 connectionHandle, 00177 uartRXCharacteristic.getValueHandle() + 1, 00178 sizeof(uint16_t), 00179 reinterpret_cast<const uint8_t *>(&value) 00180 ); 00181 //led_p(6); 00182 //pc.printf( "notifications\r" ); 00183 //pc.printf("--allow notifications-E\r"); 00184 00185 connected2server = true; 00186 } 00187 00188 // タイマーカウント更新時(10ms以上経過している) 00189 //if( timer_cnt != timer_cnt_d ) 00190 //{ 00191 //timer_cnt_d = timer_cnt; 00192 //pc.printf("%d\r", cnt); 00193 #if 0 00194 //if( connected2server == true ) 00195 //{ 00196 if( ( cnt % 2000 ) == 0 ) 00197 { 00198 int8_t rssi; 00199 sd_ble_gap_rssi_get( connectionHandle, &rssi ); 00200 00201 pc.printf( "rssi = %d\r", rssi ); 00202 } 00203 //} 00204 #endif 00205 if( ( connected2server_d == false ) && ( connected2server == true ) ) 00206 { 00207 connect_check = 1; 00208 } 00209 else if( ( connected2server_d == true ) && ( connected2server == false ) ) 00210 { 00211 state = connected2server; 00212 connect_check = 0; 00213 } 00214 00215 connected2server_d = connected2server; 00216 00217 if( connect_check == 1 ) 00218 { 00219 tt.stop(); 00220 tt.reset(); 00221 tt.start(); 00222 00223 ble_write( "STOP\r" ); 00224 connect_check = 2; 00225 } 00226 else if( connect_check == 2 ) 00227 { 00228 msec = tt.read_ms(); 00229 00230 if( msec >= 500 ) 00231 { 00232 connect_check = 1; 00233 } 00234 } 00235 else if( connect_check == 3 ) 00236 { 00237 msec = tt.read_ms(); 00238 00239 if( msec >= 500 ) 00240 { 00241 connect_check = 0; 00242 00243 state = connected2server; 00244 } 00245 } 00246 00247 ble_rs232c_cmd(); 00248 00249 pc_rs232c_cmd(); 00250 00251 cnt++; 00252 00253 //led = ( cnt >> 8 ) & 0x0001; 00254 00255 //led = !led; 00256 00257 //wait(0.002); 00258 //} 00259 00260 ble_uart.waitForEvent(); 00261 00262 } 00263 } 00264 00265 void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *params) 00266 { 00267 //pc.printf("onReceivedDataFromDeviceCallback\r"); 00268 00269 if (params->type == BLE_HVX_NOTIFICATION) { 00270 if ((params->handle 00271 == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) { 00272 00273 if( params->len <= BFSIZE ) 00274 { 00275 uart_bf_len = params->len; 00276 00277 //strcpy((char *)uart_buffer, (char *)params->data); 00278 memcpy((char *)uart_buffer, (char *)params->data, uart_bf_len ); 00279 received_uart_dat = true; 00280 00281 //ble_rs232c_cmd(); 00282 00283 for( int i=0; i<uart_bf_len; i++ ) 00284 { 00285 //pc.putc(uart_buffer[i]); 00286 ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i]; 00287 ble_uart_buf.data_cnt++; 00288 00289 if( ble_uart_buf.wp == SIO_BUF_SIZE-1 ) 00290 { 00291 ble_uart_buf.wp = 0; 00292 } 00293 else 00294 { 00295 ble_uart_buf.wp++; 00296 } 00297 } 00298 uart_bf_len = 0; 00299 } 00300 else 00301 { 00302 00303 } 00304 } 00305 } 00306 } 00307 00308 bool mac_equals(const Gap::Address_t mac_1, const Gap::Address_t mac_2) 00309 { 00310 for (int i = 0; i < 6; i++) { 00311 00312 } 00313 00314 for (int i = 0; i < 6; i++) { 00315 if (mac_1[i] != mac_2[i]) { 00316 return false; 00317 } else { 00318 } 00319 } 00320 return true; 00321 } 00322 00323 int get_board_index(const Gap::Address_t mac) 00324 { 00325 if (mac_equals(mac, mac_board_0)) { 00326 return 0; 00327 } 00328 #if 0 00329 if (mac_equals(mac, mac_board_1)) { 00330 return 1; 00331 } 00332 if (mac_equals(mac, mac_board_2)) { 00333 return 2; 00334 } 00335 if (mac_equals(mac, mac_board_3)) { 00336 return 3; 00337 } 00338 if (mac_equals(mac, mac_board_4)) { 00339 return 4; 00340 } 00341 #endif 00342 return -1; 00343 } 00344 00345 // Client(Central) role ******************************************************** 00346 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) 00347 { 00348 // connections 00349 int peer_board_index = get_board_index(params->peerAddr); 00350 if (peer_board_index != -1) { 00351 //pc.printf(""); 00352 //pc.printf( 00353 // "adv peerAddr [%02x %02x %02x %02x %02x %02x]\r\n", 00354 // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], 00355 // params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] 00356 //); 00357 //pc.printf( 00358 // "rssi=%+4d, isScanResponse %u, AdvertisementType %u\r\n", 00359 // params->rssi, params->isScanResponse, params->type 00360 //); 00361 ble_uart.gap().connect( 00362 params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); 00363 } 00364 } 00365 00366 void serviceDiscoveryCallback(const DiscoveredService *service) 00367 { 00368 //pc.printf("serviceDiscoveryCallback\r"); 00369 00370 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 00371 00372 } else { 00373 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 00374 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 00375 00376 } 00377 } 00378 00379 //pc.printf("--serviceDiscoveryCallback\r"); 00380 } 00381 00382 void characteristicDiscoveryCallback( 00383 const DiscoveredCharacteristic *characteristicP) 00384 { 00385 if (characteristicP->getUUID().getShortUUID() 00386 == UARTServiceTXCharacteristicShortUUID) { 00387 uartTXCharacteristic = *characteristicP; 00388 connection_tx = true; 00389 //led_p(4); 00390 //pc.printf( "connection_tx\r" ); 00391 //pc.printf("--connection_tx = true\r"); 00392 } else if (characteristicP->getUUID().getShortUUID() 00393 == UARTServiceRXCharacteristicShortUUID) { 00394 uartRXCharacteristic = *characteristicP; 00395 foundUartRXCharacteristic = true; 00396 connection_rx = true; 00397 //led_p(5); 00398 //pc.printf( "connection_rx\r" ); 00399 //pc.printf("--connection_rx = true\r"); 00400 } 00401 } 00402 00403 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) 00404 { 00405 //pc.printf("discoveryTerminationCallback\r"); 00406 } 00407 00408 // Mixed role ****************************************************************** 00409 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00410 { 00411 if (params->role == Gap::CENTRAL) { 00412 00413 //pc.printf( "connected\r" ); 00414 //pc.printf("connected as Client(Central) (handle = %d)\r\n\r", 00415 // params->handle); 00416 //connected2server = true; 00417 connectionHandle = params->handle; 00418 ble_uart.gattClient().onServiceDiscoveryTermination( 00419 discoveryTerminationCallback); 00420 ble_uart.gattClient().launchServiceDiscovery( 00421 params->handle, 00422 serviceDiscoveryCallback, 00423 characteristicDiscoveryCallback 00424 ); 00425 } 00426 //pc.printf( 00427 // "Client(Central/Myself) %02x:%02x:%02x:%02x:%02x:%02x\r\n", 00428 // params->ownAddr[5], params->ownAddr[4], params->ownAddr[3], 00429 // params->ownAddr[2], params->ownAddr[1], params->ownAddr[0] 00430 //); 00431 //pc.printf( 00432 // "Connected Server(Peripheral) %02x:%02x:%02x:%02x:%02x:%02x\r\n", 00433 // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], 00434 // params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] 00435 //); 00436 00437 //led_p(3); 00438 00439 uint32_t res = sd_ble_gap_rssi_start( connectionHandle, 0, 0 ); 00440 00441 //pc.printf( "rssi_start_res = %d\r", res ); 00442 } 00443 00444 00445 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00446 { 00447 //pc.printf( "disconnected\r" ); 00448 //pc.printf(" -> disconnected\r\n", params->handle); 00449 connected2server = false; 00450 // connection_1st = false; 00451 connection_tx = false; 00452 connection_rx = false; 00453 00454 #if 0 00455 if (params->handle == SOFT_DEVICE_FATHER_HANDLE) { 00456 ble_uart.startAdvertising(); // restart advertising 00457 } else { 00458 ble_uart.gap().startScan(advertisementCallback);// restart scan 00459 } 00460 #endif 00461 } 00462 00463 /****************************************************************************/ 00464 /* 関数名 : uart_init */ 00465 /* 概要 : 通信初期化 */ 00466 /* 作成者 : JPMS H.Harada */ 00467 /* 作成日 : 2017.08.30 */ 00468 /****************************************************************************/ 00469 void uart_init() 00470 { 00471 int k; 00472 00473 // 初期化時のごみを出力 00474 // while( pc.readable() ) 00475 // { 00476 // pc.getc(); 00477 // } 00478 00479 // UART バッファ初期化 /////////////////////////////////////////////// 00480 pc_uart_buf.data_cnt = 0; 00481 pc_uart_buf.rp = 0; 00482 pc_uart_buf.wp = 0; 00483 00484 ble_uart_buf.data_cnt = 0; 00485 ble_uart_buf.rp = 0; 00486 ble_uart_buf.wp = 0; 00487 00488 for(k=0;k<SIO_BUF_SIZE;k++) { 00489 pc_uart_buf.buf[k] = 0; 00490 ble_uart_buf.buf[k] = 0; 00491 } 00492 00493 // コマンド バッファ初期化 /////////////////////////////////////////////// 00494 pc_cmd_buf.data_cnt = 0; 00495 pc_cmd_buf.rp = 0; 00496 pc_cmd_buf.wp = 0; 00497 00498 ble_cmd_buf.data_cnt = 0; 00499 ble_cmd_buf.rp = 0; 00500 ble_cmd_buf.wp = 0; 00501 00502 for(k=0;k<SIO_BUF_SIZE;k++) { 00503 pc_cmd_buf.buf[k] = 0; 00504 ble_cmd_buf.buf[k] = 0; 00505 } 00506 00507 ///////////////////////////////////////////////////////////////// 00508 } 00509 00510 /****************************************************************************/ 00511 /* 関数名 : main_timer_proc */ 00512 /* 概要 : メインタイマ処理 */ 00513 /* 作成者 : JPMS H.Harada */ 00514 /* 作成日 : 2018.02.22 */ 00515 /****************************************************************************/ 00516 void main_timer_proc() 00517 { 00518 00519 timer_cnt++; // タイマカウントを加算 00520 00521 } 00522 00523 /****************************************************************************/ 00524 /* 関数名 : atow */ 00525 /* 概要 : アスキー変換処理 */ 00526 /* 作成者 : JPMS H.Harada */ 00527 /* 作成日 : 2017.08.30 */ 00528 /****************************************************************************/ 00529 unsigned int atow(char *str,unsigned short leng) 00530 { 00531 register unsigned short i; 00532 unsigned int ret_data = 0; 00533 00534 /*---------------- データー長エラー -----------------*/ 00535 // if(leng >= 11) 00536 // { 00537 // ret_data = ATOW_ERROR; /* データエラー */ 00538 // } 00539 /*---------------- データー長 OK ------------------*/ 00540 // else 00541 // { 00542 i = leng; 00543 00544 while(i) 00545 { 00546 ret_data *= 10; /* 10倍 */ 00547 00548 /*------ データ '0' ~ '9' 範囲内 -----*/ 00549 if( (*str >= '0') && (*str <= '9') ) 00550 { 00551 ret_data += (*str) - '0'; 00552 str++; 00553 } 00554 /*------ データ '0' ~ '9' 範囲外 -----*/ 00555 else 00556 { 00557 // ret_data = ATOW_ERROR; /* データエラー */ 00558 break; 00559 } 00560 i--; 00561 } 00562 /*-------------- データオバーフロー ----------*/ 00563 // if(ret_data & 0x8000) 00564 // { 00565 // ret_data = ATOW_ERROR; /* データエラー */ 00566 // } 00567 // } 00568 return(ret_data); 00569 } 00570 00571 /****************************************************************************/ 00572 /* 関数名 : atoh */ 00573 /* 概要 : アスキー変換処理 */ 00574 /* 作成者 : JPMS H.Harada */ 00575 /* 作成日 : 2017.08.30 */ 00576 /****************************************************************************/ 00577 unsigned long atoh(char *str,unsigned short leng) 00578 { 00579 register unsigned short i; 00580 unsigned long ret_data = 0; 00581 00582 /*---------------- データー長エラー -----------------*/ 00583 // if(leng >= 11) 00584 // { 00585 // ret_data = ATOW_ERROR; /* データエラー */ 00586 // } 00587 /*---------------- データー長 OK ------------------*/ 00588 // else 00589 // { 00590 i = leng; 00591 00592 while(i) 00593 { 00594 ret_data *= 16; /* 16倍 */ 00595 00596 /*------ データ '0' ~ '9' 範囲内 -----*/ 00597 if( (*str >= '0') && (*str <= '9') ) 00598 { 00599 ret_data += (*str) - '0'; 00600 str++; 00601 } 00602 /*------ データ 'A' ~ 'F' 範囲内 -----*/ 00603 else if( (*str >= 'A') && (*str <= 'F') ) 00604 { 00605 ret_data += (*str) - 'A' + 10; 00606 str++; 00607 } 00608 /*------ データ '0' ~ '9' 範囲外 -----*/ 00609 else 00610 { 00611 // ret_data = ATOW_ERROR; /* データエラー */ 00612 break; 00613 } 00614 i--; 00615 } 00616 /*-------------- データオバーフロー ----------*/ 00617 // if(ret_data & 0x8000) 00618 // { 00619 // ret_data = ATOW_ERROR; /* データエラー */ 00620 // } 00621 // } 00622 return(ret_data); 00623 } 00624 00625 #define CMD_NUM_BLE 19 00626 00627 char ble_str[32][10]; 00628 char ble_str2[CMD_NUM_BLE][8] = { "START ", // 0 00629 "STOP ", // 1 00630 " ", // 2 00631 "ISETL ", // 3 00632 "ISETR ", // 4 00633 " ", // 5 00634 "TC ", // 6 00635 "TS ", // 7 00636 "IDGET ", // 8 00637 "P ", // 9 00638 "U ", // 10 00639 "RSSI ", // 11 00640 "M ", // 12 00641 "X ", // 13 00642 "PAUSE ", // 14 00643 "REG09 ", // 15 Ver12 00644 "REG0A ", // 16 Ver12 00645 "REG0B ", // 17 Ver12 00646 "ICMR "}; // 18 Ver12 00647 00648 /****************************************************************************/ 00649 /* 関数名 : rs232c_cmd */ 00650 /* 概要 : 通信コマンド処理 */ 00651 /* 作成者 : JPMS H.Harada */ 00652 /* 作成日 : 2017.08.30 */ 00653 /****************************************************************************/ 00654 void ble_rs232c_cmd() 00655 { 00656 unsigned int c, i, j; 00657 int rdat; 00658 00659 #if 0 00660 00661 for( int i=0; i<uart_bf_len; i++ ) 00662 { 00663 //pc.putc(uart_buffer[i]); 00664 ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i]; 00665 ble_uart_buf.data_cnt++; 00666 00667 if( ble_uart_buf.wp == SIO_BUF_SIZE-1 ) 00668 { 00669 ble_uart_buf.wp = 0; 00670 } 00671 else 00672 { 00673 ble_uart_buf.wp++; 00674 } 00675 } 00676 00677 uart_bf_len = 0; 00678 00679 #endif 00680 00681 if( ble_uart_buf.data_cnt > 0 ) 00682 { 00683 unsigned int len = ble_uart_buf.data_cnt; 00684 00685 for( c=0; c<len; c++ ) 00686 { 00687 //NVIC_DisableIRQ(RADIO_IRQn); // 割り込み禁止 00688 00689 rdat = ble_uart_buf.buf[ble_uart_buf.rp]; 00690 ble_uart_buf.data_cnt--; 00691 00692 if( ble_uart_buf.rp == SIO_BUF_SIZE-1 ) 00693 { 00694 ble_uart_buf.rp = 0; 00695 } 00696 else 00697 { 00698 ble_uart_buf.rp++; 00699 } 00700 00701 //NVIC_EnableIRQ(RADIO_IRQn); // 割り込み許可 00702 00703 if( rdat != '\n' ) 00704 { 00705 ble_cmd_buf.buf[ble_cmd_buf.data_cnt++] = rdat; 00706 } 00707 00708 if (rdat == '\r') 00709 { 00710 int k = 0; 00711 int l = 0; 00712 int p = 0; 00713 00714 for (i=0;i<ble_cmd_buf.data_cnt;i++) 00715 { 00716 if( ( ( ble_cmd_buf.buf[i] == ' ' ) && ( p > 0 ) ) || 00717 ( ble_cmd_buf.buf[i] == '\r') ) 00718 { 00719 ble_str[k++][l] = '\0'; 00720 l = 0; 00721 p = 0; 00722 } 00723 else 00724 { 00725 p++; 00726 ble_str[k][l++] = ble_cmd_buf.buf[i]; 00727 } 00728 } 00729 00730 if( ble_cmd_buf.data_cnt == 1 ) 00731 { 00732 k = 0; 00733 } 00734 00735 ble_cmd_buf.data_cnt = 0; 00736 ble_cmd_buf.buf[0] = '\0'; 00737 00738 if (k > 0) 00739 { 00740 int ret; 00741 int cmd_no = 0xFF; 00742 00743 for( j=0; j<CMD_NUM_BLE; j++ ) 00744 { 00745 ret = 1; 00746 00747 if( strlen(ble_str[0]) > 0 ) 00748 { 00749 for( i=0; i<strlen(ble_str[0]); i++ ) 00750 { 00751 if( ble_str[0][i] != ble_str2[j][i] ) 00752 { 00753 ret = 0; 00754 } 00755 } 00756 00757 if( ret ) 00758 { 00759 cmd_no = j; 00760 break; 00761 } 00762 } 00763 } 00764 00765 // コマンド START 00766 if( cmd_no == 0 ) 00767 { 00768 start_flag = true; 00769 pc.printf( "START\r" ); 00770 } 00771 // コマンド STOP 00772 else if( cmd_no == 1 ) 00773 { 00774 if( connect_check == 0 ) 00775 { 00776 pc.printf( "STOP\r" ); 00777 } 00778 else 00779 { 00780 connect_check = 3; 00781 } 00782 } 00783 // コマンド 00784 else if( cmd_no == 2 ) 00785 { 00786 00787 } 00788 // コマンド ISETL 00789 else if( cmd_no == 3 ) 00790 { 00791 pc.printf( "ISETL\r" ); 00792 } 00793 // コマンド ISETR 00794 else if( cmd_no == 4 ) 00795 { 00796 pc.printf( "ISETR\r" ); 00797 } 00798 // コマンド 00799 else if( cmd_no == 5 ) 00800 { 00801 00802 } 00803 // コマンド TC 00804 else if( cmd_no == 6 ) 00805 { 00806 pc.printf( "TC\r" ); 00807 } 00808 // コマンド TS 00809 else if( cmd_no == 7 ) 00810 { 00811 pc.printf( "TS\r" ); 00812 } 00813 // コマンド IDGET 00814 else if( cmd_no == 8 ) 00815 { 00816 pc.printf( "IDGET\r" ); 00817 } 00818 // コマンド P 00819 else if( cmd_no == 9 ) 00820 { 00821 int data_cnt = atow(ble_str[1], strlen(ble_str[1])); 00822 int left = atow(ble_str[2], strlen(ble_str[2])); 00823 int right = atow(ble_str[3], strlen(ble_str[3])); 00824 00825 led = 1; 00826 00827 int8_t rssi; 00828 sd_ble_gap_rssi_get( connectionHandle, &rssi ); 00829 00830 if( rssi < 0 ) 00831 { 00832 if( rssi < -99 ) 00833 { 00834 rssi = -99; 00835 } 00836 00837 rssi += 100; 00838 } 00839 else 00840 { 00841 rssi = 0; 00842 } 00843 00844 pc.printf( "POINT %d %d %d %d\r", data_cnt, left, right, rssi ); 00845 00846 led = 0; 00847 00848 //wait(0.001); 00849 } 00850 // コマンド U 00851 else if( cmd_no == 10 ) 00852 { 00853 unsigned int high = atoh(ble_str[1], strlen(ble_str[1])); 00854 unsigned int low = atoh(ble_str[2], strlen(ble_str[2])); 00855 00856 pc.printf( "UID %08X %08X\r\n", high, low ); 00857 } 00858 // コマンド RSSI 00859 else if( cmd_no == 11 ) 00860 { 00861 int8_t rssi; 00862 sd_ble_gap_rssi_get( connectionHandle, &rssi ); 00863 00864 if( rssi < 0 ) 00865 { 00866 if( rssi < -99 ) 00867 { 00868 rssi = -99; 00869 } 00870 00871 rssi += 100; 00872 } 00873 else 00874 { 00875 rssi = 0; 00876 } 00877 00878 pc.printf( "RSSI %d\r\n", rssi ); 00879 } 00880 // コマンド MES 00881 else if( cmd_no == 12 ) 00882 { 00883 unsigned int level = atow(ble_str[1], strlen(ble_str[1])); 00884 unsigned long uid_L = atoh(ble_str[2], strlen(ble_str[2])); 00885 00886 pc.printf( "MES %d %08X\r\n", level, uid_L ); 00887 } 00888 // コマンド 00889 else if( cmd_no == 13 ) 00890 { 00891 unsigned int high = atoh(ble_str[1], strlen(ble_str[1])); 00892 unsigned int low = atoh(ble_str[2], strlen(ble_str[2])); 00893 00894 pc.printf( "MMR %08X %08X\r\n", high, low ); 00895 } 00896 // コマンド PAUSE 00897 else if( cmd_no == 14 ) 00898 { 00899 if( connect_check == 0 ) 00900 { 00901 pc.printf( "PAUSE\r" ); 00902 } 00903 else 00904 { 00905 connect_check = 3; 00906 } 00907 } 00908 // コマンド REG09 Ver12 00909 else if( cmd_no == 15 ) 00910 { 00911 pc.printf( "REG09\r" ); 00912 } 00913 // コマンド REG0A Ver12 00914 else if( cmd_no == 16 ) 00915 { 00916 pc.printf( "REG0A\r" ); 00917 } 00918 // コマンド REG0B Ver12 00919 else if( cmd_no == 17 ) 00920 { 00921 pc.printf( "REG0B\r" ); 00922 } 00923 // コマンド ICMR Ver12 00924 else if( cmd_no == 18 ) 00925 { 00926 unsigned int adr = atow(ble_str[1], strlen(ble_str[1])); 00927 unsigned int dat = atoh(ble_str[2], strlen(ble_str[2])); 00928 00929 pc.printf( "ICMR %d %08X\r", adr, dat ); 00930 } 00931 else 00932 { 00933 //pc.printf("CMD_ERR\r\n"); 00934 } 00935 } 00936 } 00937 } 00938 } 00939 } 00940 00941 void pc_rx() 00942 { 00943 //NVIC_DisableIRQ(UART0_IRQn); // 割り込み禁止 00944 00945 pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc(); 00946 pc_uart_buf.data_cnt++; 00947 00948 if( pc_uart_buf.wp == SIO_BUF_SIZE-1 ) 00949 { 00950 pc_uart_buf.wp = 0; 00951 } 00952 else 00953 { 00954 pc_uart_buf.wp++; 00955 } 00956 00957 //NVIC_EnableIRQ(UART0_IRQn); // 割り込み許可 00958 } 00959 00960 #define CMD_NUM 21 00961 00962 char pc_str[16][10]; 00963 char pc_str2[CMD_NUM][8] = { "START ", // 0 00964 "STOP ", // 1 00965 "VER ", // 2 00966 "ISETL ", // 3 00967 "ISETR ", // 4 00968 "CONNECT", // 5 00969 "DISC ", // 6 00970 "MMR ", // 7 00971 "TC ", // 8 00972 "MES ", // 9 00973 "IDGET ", // 10 00974 "RSSI ", // 11 00975 "RES ", // 12 00976 "READ ", // 13 00977 "PRES ", // 14 00978 "TS ", // 15 00979 "PAUSE ", // 16 00980 "REG09 ", // 17 Ver12 00981 "REG0A ", // 18 Ver12 00982 "REG0B ", // 19 Ver12 00983 "ICMR "}; // 20 Ver12 00984 00985 /****************************************************************************/ 00986 /* 関数名 : rs232c_cmd */ 00987 /* 概要 : 通信コマンド処理 */ 00988 /* 作成者 : JPMS H.Harada */ 00989 /* 作成日 : 2017.08.30 */ 00990 /****************************************************************************/ 00991 void pc_rs232c_cmd() 00992 { 00993 unsigned int c, i, j; 00994 int rdat; 00995 00996 #if 1 00997 while( pc.readable() ) 00998 { 00999 pc_uart_buf.buf[pc_uart_buf.wp] = pc.getc(); 01000 pc_uart_buf.data_cnt++; 01001 01002 //pc.putc(pc_uart_buf.buf[pc_uart_buf.wp]); 01003 01004 if( pc_uart_buf.wp == SIO_BUF_SIZE-1 ) 01005 { 01006 pc_uart_buf.wp = 0; 01007 } 01008 else 01009 { 01010 pc_uart_buf.wp++; 01011 } 01012 } 01013 #endif 01014 01015 if( pc_uart_buf.data_cnt > 0 ) 01016 { 01017 unsigned int len = pc_uart_buf.data_cnt; 01018 01019 for( c=0; c<len; c++ ) 01020 { 01021 //NVIC_DisableIRQ(UART0_IRQn); // 割り込み禁止 01022 01023 rdat = pc_uart_buf.buf[pc_uart_buf.rp]; 01024 pc_uart_buf.data_cnt--; 01025 01026 if( pc_uart_buf.rp == SIO_BUF_SIZE-1 ) 01027 { 01028 pc_uart_buf.rp = 0; 01029 } 01030 else 01031 { 01032 pc_uart_buf.rp++; 01033 } 01034 01035 //NVIC_EnableIRQ(UART0_IRQn); // 割り込み許可 01036 01037 if( rdat != '\n' ) 01038 { 01039 pc_cmd_buf.buf[pc_cmd_buf.data_cnt++] = rdat; 01040 } 01041 01042 if( rdat == '\r' ) 01043 { 01044 int k = 0; 01045 int l = 0; 01046 int p = 0; 01047 01048 for( i=0;i<pc_cmd_buf.data_cnt;i++ ) 01049 { 01050 if( ( ( pc_cmd_buf.buf[i] == ' ' ) && ( p > 0 ) ) || 01051 ( pc_cmd_buf.buf[i] == '\r') ) 01052 { 01053 pc_str[k++][l] = '\0'; 01054 l = 0; 01055 p = 0; 01056 } 01057 else 01058 { 01059 p++; 01060 pc_str[k][l++] = pc_cmd_buf.buf[i]; 01061 } 01062 } 01063 01064 if( pc_cmd_buf.data_cnt == 1 ) 01065 { 01066 k = 0; 01067 } 01068 01069 pc_cmd_buf.data_cnt = 0; 01070 pc_cmd_buf.buf[0] = '\0'; 01071 01072 if (k > 0) 01073 { 01074 int ret; 01075 int cmd_no = 0xFF; 01076 01077 for( j=0; j<CMD_NUM; j++ ) 01078 { 01079 ret = 1; 01080 01081 if( strlen(pc_str[0]) > 0 ) 01082 { 01083 for( i=0; i<strlen(pc_str[0]); i++ ) 01084 { 01085 if( pc_str[0][i] != pc_str2[j][i] ) 01086 { 01087 ret = 0; 01088 } 01089 } 01090 01091 if( ret ) 01092 { 01093 cmd_no = j; 01094 break; 01095 } 01096 } 01097 } 01098 01099 // コマンド START 01100 if (cmd_no == 0) 01101 { 01102 ble_write( "START\r" ); 01103 01104 //pc.printf( "START OK\r\n" ); 01105 } 01106 // コマンド STOP 01107 else if( cmd_no == 1 ) 01108 { 01109 ble_write( "STOP\r" ); 01110 } 01111 // コマンド VER 01112 else if( cmd_no == 2 ) 01113 { 01114 pc.printf( "VER %03d\r", VER ); 01115 } 01116 // コマンド ISETL 01117 else if( cmd_no == 3 ) 01118 { 01119 int no = atow(pc_str[1], strlen(pc_str[1])); 01120 int id_LH = atoh(pc_str[2], strlen(pc_str[2])); 01121 int id_LL = atoh(pc_str[3], strlen(pc_str[3])); 01122 01123 char buf[BFSIZE]; 01124 01125 if( no == 0 ) 01126 { 01127 sprintf( buf, "A %8X %8X\r", id_LH, id_LL ); 01128 } 01129 else if( no == 1 ) 01130 { 01131 sprintf( buf, "B %8X %8X\r", id_LH, id_LL ); 01132 } 01133 else if( no == 2 ) 01134 { 01135 sprintf( buf, "C %8X %8X\r", id_LH, id_LL ); 01136 } 01137 else if( no == 3 ) 01138 { 01139 sprintf( buf, "D %8X %8X\r", id_LH, id_LL ); 01140 } 01141 01142 //sprintf( buf, "L %8X %8X\r", id_LH, id_LL ); 01143 01144 ble_write( buf ); 01145 01146 //pc.printf( "ISETL OK\r\n" ); 01147 } 01148 // コマンド ISETR 01149 else if( cmd_no == 4 ) 01150 { 01151 int no = atow(pc_str[1], strlen(pc_str[1])); 01152 int id_RH = atoh(pc_str[2], strlen(pc_str[2])); 01153 int id_RL = atoh(pc_str[3], strlen(pc_str[3])); 01154 01155 char buf[BFSIZE]; 01156 01157 if( no == 0 ) 01158 { 01159 sprintf( buf, "E %8X %8X\r", id_RH, id_RL ); 01160 } 01161 else if( no == 1 ) 01162 { 01163 sprintf( buf, "F %8X %8X\r", id_RH, id_RL ); 01164 } 01165 else if( no == 2 ) 01166 { 01167 sprintf( buf, "G %8X %8X\r", id_RH, id_RL ); 01168 } 01169 else if( no == 3 ) 01170 { 01171 sprintf( buf, "H %8X %8X\r", id_RH, id_RL ); 01172 } 01173 01174 //sprintf( buf, "R %8X %8X\r", id_RH, id_RL ); 01175 01176 ble_write( buf ); 01177 01178 //pc.printf( "ISETR OK\r\n" ); 01179 } 01180 // コマンド CONNECT 01181 else if( cmd_no == 5 ) 01182 { 01183 //pc.printf( "CONNECT\r" ); 01184 //led_p(1); 01185 mac_1 = atoh(pc_str[1], strlen(pc_str[1])); 01186 mac_2 = atoh(pc_str[2], strlen(pc_str[2])); 01187 mac_3 = atoh(pc_str[3], strlen(pc_str[3])); 01188 01189 mac_board_0[0] = ( mac_3 >> 0 ) & 0x00FF; 01190 mac_board_0[1] = ( mac_3 >> 8 ) & 0x00FF; 01191 mac_board_0[2] = ( mac_2 >> 0 ) & 0x00FF; 01192 mac_board_0[3] = ( mac_2 >> 8 ) & 0x00FF; 01193 mac_board_0[4] = ( mac_1 >> 0 ) & 0x00FF; 01194 mac_board_0[5] = ( mac_1 >> 8 ) & 0x00FF; 01195 01196 // CONNECT処理 01197 ble_uart.gap().connect( mac_board_0, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); 01198 //led_p(2); 01199 01200 01201 } 01202 // コマンド DISC 01203 else if( cmd_no == 6 ) 01204 { 01205 // DISC処理 01206 Gap::DisconnectionReason_t reason; 01207 ble_uart.gap().disconnect( reason ); 01208 } 01209 // コマンド MMR 01210 else if( cmd_no == 7 ) 01211 { 01212 int no = atow(pc_str[1], strlen(pc_str[1])); 01213 01214 char buf[BFSIZE]; 01215 sprintf( buf, "MMR %d\r", no); 01216 ble_write( buf ); 01217 } 01218 // コマンド TC 01219 else if( cmd_no == 8 ) 01220 { 01221 unsigned int not_touch_cnt = atow(pc_str[1], strlen(pc_str[1])); 01222 unsigned int touch_cnt = atow(pc_str[2], strlen(pc_str[2])); 01223 01224 char buf[BFSIZE]; 01225 sprintf( buf, "TC %d %d\r", not_touch_cnt, touch_cnt); 01226 ble_write( buf ); 01227 01228 //pc.printf( "NTC OK\r\n" ); 01229 } 01230 // コマンド MES 01231 else if( cmd_no == 9 ) 01232 { 01233 ble_write( "MES\r" ); 01234 } 01235 // コマンド IDGET 01236 else if( cmd_no == 10 ) 01237 { 01238 ble_write( "IDGET\r" ); 01239 } 01240 // コマンド RSSI 01241 else if( cmd_no == 11 ) 01242 { 01243 ble_write( "RSSI\r" ); 01244 } 01245 // コマンド 01246 else if( cmd_no == 12 ) 01247 { 01248 pc.printf( "RES\r" ); 01249 } 01250 // コマンド READ 01251 else if( cmd_no == 13 ) 01252 { 01253 //led = 1; 01254 ble_write( "READ\r" ); 01255 01256 //pc.printf( "READ_BLE\r" ); 01257 //char buf[BFSIZE]; 01258 //sprintf( buf, "READ %d\r", read_cnt++ ); 01259 //ble_write( buf ); 01260 01261 //ble_write( "~R\r" ); 01262 } 01263 // コマンド PRES 01264 else if( cmd_no == 14 ) 01265 { 01266 ble_write( "PRES\r" ); 01267 } 01268 // コマンド TS 01269 else if( cmd_no == 15 ) 01270 { 01271 unsigned int touch_start = atow(pc_str[1], strlen(pc_str[1])); 01272 01273 char buf[BFSIZE]; 01274 sprintf( buf, "TS %d\r", touch_start ); 01275 ble_write( buf ); 01276 } 01277 // コマンド PAUSE 01278 else if( cmd_no == 16 ) 01279 { 01280 ble_write( "PAUSE\r" ); 01281 } 01282 // コマンド REG09 Ver12 01283 else if( cmd_no == 17 ) 01284 { 01285 char reg09 = ( char )( atoh(pc_str[1], strlen(pc_str[1])) ); 01286 char buf[BFSIZE]; 01287 sprintf( buf, "REG09 %02X\r", reg09 ); 01288 ble_write( buf ); 01289 } 01290 // コマンド REG0A Ver12 01291 else if( cmd_no == 18 ) 01292 { 01293 char reg0A = ( char )( atoh(pc_str[1], strlen(pc_str[1])) ); 01294 char buf[BFSIZE]; 01295 sprintf( buf, "REG0A %02X\r", reg0A ); 01296 ble_write( buf ); 01297 } 01298 // コマンド REG0B Ver12 01299 else if( cmd_no == 19 ) 01300 { 01301 char reg0B = ( char )( atoh(pc_str[1], strlen(pc_str[1])) ); 01302 char buf[BFSIZE]; 01303 sprintf( buf, "REG0B %02X\r", reg0B ); 01304 ble_write( buf ); 01305 } 01306 // コマンド ICMR Ver12 01307 else if( cmd_no == 20 ) 01308 { 01309 unsigned int adr = atow(pc_str[1], strlen(pc_str[1])); 01310 01311 char buf[BFSIZE]; 01312 sprintf( buf, "ICMR %d\r", adr ); 01313 ble_write( buf ); 01314 } 01315 else 01316 { 01317 pc.printf("CMD_ERR_BLE\r"); 01318 } 01319 } 01320 } 01321 } 01322 } 01323 } 01324 01325 void ble_write(char* dat) 01326 { 01327 uint8_t linebf_irq[BFSIZE]; 01328 01329 sprintf( (char *)linebf_irq, "%s", dat); 01330 01331 if( connected2server == true ) 01332 { 01333 bletx_mutex.lock(); 01334 01335 uartTXCharacteristic.write(NUM_ONCE, linebf_irq); 01336 01337 bletx_mutex.unlock(); 01338 } 01339 }
Generated on Thu Aug 18 2022 08:32:37 by
1.7.2