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: 1_cubebite_ev-cog-ad3029lz_433MHz_1nd_certTest stm-spirit1-rf-driver_for_cubebite2
Fork of cubebite_rnd_ev-cog-ad3029lz_433MHz_2nd_testing2_RF10CountSensingCheckOK_180619_initReviseTest by
main.cpp
00001 //HJM : 선풍기 에이징 테스트 용 소스임. 00002 //180615 HJM : 선풍기 에이징 용 온도 1개 진동 10개 전송용 테스트 임. 00003 // CUBEBITE_TEMPERATURE_DISABLE define 값으로 온도 센서 (연결 및 미연결) 시 설정을 할 수 있음. 00004 //180717 HJM : 인증(온도) 테스트용 소스 버전 관리, 0.1 -> 온도(저온) 테스트 00005 //180717 HJM : 수정 1 00006 00007 00008 #include "mbed.h" 00009 #include "ADXL362.h" 00010 #include "SimpleSpirit1.h" 00011 #include "main.h" 00012 00013 00014 #define FIRMWARE_VERSION_MAJOR 0 00015 #define FIRMWARE_VERSION_MINOR 1 00016 #define FIRMWARE_VERSION_REVISE 0 00017 00018 //#define FIRMWARE_VERSION_PRINT(x) printf(#x " = %s\n", x); 00019 00020 //************************************************************************************* 00021 //Define : Test, Compile 00022 //************************************************************************************* 00023 //#define CUBEBITE_DEBUG 00024 //#define HEAVY_DEBUG 00025 00026 00027 /* 180108 cubebite HJM : cubebite Testing define, 1개씩만 켜야 함. */ 00028 #define CUBEBITE_RF_SPEED_TEST_SEND_ON 00029 //#define CUBEBITE_RF_SPEED_TEST_RECV_ON 00030 //#define CUBEBITE_RF_SPEED_TEST_SEND_AND_RECV_ON //HJM : 추후 만들 예정.. 00031 00032 /* 180109 cubebite HJM : cubebite Compile define, 1개씩만 켜야 함. */ 00033 #define CUBEBITE_BOARD_AD3029LZ 00034 00035 00036 #define CUBEBITE_RF_SETTING_VALUE_PRINT 00037 //#define CUBEBITE_TEMPERATURE_DISABLE 00038 00039 00040 //180411 HJM : 현재 1~3 까지 3대까지 가능 -> 게이트웨이에서 3대까지만 처리하게끔 되어있 00041 //#define CUBEBITE_SENSOR_UUID 1 //3180621 HJM : 온도 및 진동 테스트 용 00042 #define CUBEBITE_SENSOR_UUID 2 //180621 HJM : 철기연 테스트 용, 방대리님 테스트 용 00043 //************************************************************************************* 00044 00045 00046 00047 00048 //HJM : 타이머 인터럽트 테스트 용 00049 #ifdef CUBEBITE_DEBUG 00050 DigitalOut led1(LED1); 00051 #endif 00052 00053 00054 00055 00056 //************************************************************************************* 00057 //HJM SUDO code : 3개 객체 생성->RF, 온도, 진동 00058 //************************************************************************************* 00059 00060 00061 00062 //온도 객체 00063 #ifndef CUBEBITE_TEMPERATURE_DISABLE 00064 I2C i2c(I2C_SDA, I2C_SCL); //HJM : 테스트 용 임, 서대리님이 클래스 정리해서 줄 예정 00065 #endif 00066 00067 //진동 객체(Accelerometer (mosi, miso, sclk, cs), SPI 1) 00068 ADXL362 adxl362(P1_07, P1_08, P1_06, P1_10); //HJM : 테스트 용 임, 서대리님이 클래스 정리해서 줄 예정 00069 00070 //************************************************************************************* 00071 00072 00073 00074 00075 00076 00077 //************************************************************************************* 00078 //HJM SUDO code : main 사용자 함수 선언 00079 //************************************************************************************* 00080 00081 00082 static void callback_func(int event); //RF user callback 함수임. 00083 void UserTimerInterrupt(void); //1초, 타이머 인터럽트 임 00084 int UserTimerInterrupt_Execution(void); 00085 int UserTimerInterrupt_Execution2(void); 00086 //void UserTimerInterrupt2(void); 00087 static int SendData(SimpleSpirit1& myspirit); 00088 00089 00090 //************************************************************************************* 00091 00092 00093 00094 00095 00096 00097 00098 00099 //************************************************************************************* 00100 //(테스트용)HJM SUDO code : 전역 변수, 진동, 온도 쪽 테스트용임. 추후 서대리님이 클래스 재 정의시 없어질 예정 00101 //************************************************************************************* 00102 00103 //RF 객체 00104 static SimpleSpirit1 &myspirit = SimpleSpirit1::CreateInstance(P0_01, P0_02, P0_00, P1_12 ,P2_08, P1_14); 00105 // P0_01[SPI0_MOSI] 00106 // P0_02[SPI0_MISO], 00107 // P0_00[SPI0_CLK], 00108 // P1_12[GPIO 28, RF(GPIO 0)]->IRQ로 설정, 00109 // P2_08[SPI0_CS2], 00110 // P1_14[GPIO 30, RF (SDN)] 00111 00112 00113 //진동 00114 uint16_t vCnt = 0; 00115 uint8_t iCnt = 0; 00116 00117 //int16_t x16 = 0; 00118 //int16_t y16 = 0; 00119 //int16_t z16 = 0; 00120 int8_t x8 = 0; 00121 int8_t y8 = 0; 00122 int8_t z8 = 0; 00123 00124 00125 int8_t int8tX8Result = 0; 00126 int8_t int8tY8Result = 0; 00127 int8_t int8tZ8Result = 0; 00128 00129 00130 int16_t temp_x16[3] = {0,}; 00131 int16_t temp_y16[3] = {0,}; 00132 int16_t temp_z16[3] = {0,}; 00133 00134 int Comp1_x = 0, Comp2_x = 0; 00135 int Comp1_y = 0, Comp2_y = 0; 00136 int Comp1_z = 0, Comp2_z = 0; 00137 00138 float float_x = 0; 00139 float float_y = 0; 00140 float float_z = 0; 00141 00142 float G_x = 0; 00143 float G_y = 0; 00144 float G_z = 0; 00145 00146 float Output = 0; 00147 00148 double dVibOutputSqrt = 0; 00149 00150 00151 //온도 00152 char wReg[2] = {0,}; 00153 char rReg[2] = {0,}; 00154 int wACK = 0; 00155 int rACK = 0; 00156 int16_t Tem16; 00157 float TempData = 0; 00158 00159 #define busAddr 0x4F 00160 00161 #define ID_DATA_ADDR 0x00 00162 #define ID_CON_ADDR 0x01 00163 #define ID_REG_ADDR 0x07 00164 00165 #define RESET_HW_OFF 1 00166 #define RESET_HW_ON 0 00167 00168 #define SPIRIT1_POWER_OFF 1 00169 #define SPIRIT1_POWER_ON 2 00170 00171 00172 //RF->이거도 클래스 내부에 넣기 00173 static volatile bool bRxDoneFlag = false; 00174 static volatile bool bTxDoneFlag = false; 00175 static volatile bool bSendDataFlag = false; 00176 00177 //************************************************************************************* 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 //HJM : I2C PullUp 용.. RF IRQ 도 이 핀 이지만 사용안하기 때문에 괜찮음. 00188 DigitalOut resetHW(P1_12); 00189 //prit1(P1_11); //GPIO27 00190 00191 bool iTempFlag = false; 00192 bool isTimerInterruptStarted = false; 00193 bool bIs1SecTimerSet = false; 00194 int iTempInitCount = 0; 00195 bool isTempInitOk = false; 00196 00197 int iMainCounting = 1; 00198 static int iIsResetStartCounting = 0; 00199 //static int iIsResetStartCountingInterrupt = 0; 00200 int main(void) 00201 { 00202 //************************************************************************************* 00203 //HJM SUDO code : 진동, 온도, RF 초기화 00204 //************************************************************************************* 00205 00206 //필수!!! 00207 resetHW.write(RESET_HW_OFF); 00208 00209 printf("\n\n FIRMWARE_VERSION : %d.%d.%d\n\n", FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_REVISE); 00210 00211 00212 00213 00214 // FIRMWARE_VERSION_PRINT(FIRMWARE_VERSION); 00215 00216 00217 00218 00219 //진동 init_시작 00220 // set up SPI interface 00221 adxl362.init_spi(); 00222 // Set up accelerometer 00223 adxl362.init_adxl362(); 00224 00225 // wait_ms(25); 00226 //진동 init_끝 00227 00228 00229 //온도 init_시작 00230 //Power supply for the sensor is given from GPIO28(VDD_MCU_B). 00231 // JMOD Init 00232 #ifndef CUBEBITE_TEMPERATURE_DISABLE 00233 printf("[Temperature] Init start"); 00234 00235 i2c.frequency(100000); //100khz 00236 wReg[0] = ID_CON_ADDR; 00237 00238 while(rReg[0] != 0x03) 00239 { 00240 //write 0x00(ID_CON_ADDR) 00241 wReg[1] = 0x00; 00242 wACK = i2c.write(busAddr, wReg, 2); 00243 00244 //read 0x00(ID_CON_ADDR) 00245 wACK = i2c.write(busAddr, wReg, 1); 00246 rACK = i2c.read(busAddr, rReg, 1); 00247 00248 wait_ms(250); 00249 00250 //write 0x03(ID_CON_ADDR) 00251 wReg[1] = 0x03; 00252 wACK = i2c.write(busAddr, wReg, 2); 00253 00254 //read 0x03(ID_CON_ADDR) 00255 wACK = i2c.write(busAddr, wReg, 1); 00256 rACK = i2c.read(busAddr, rReg, 1); 00257 00258 i2c.write(busAddr, wReg, 2); 00259 00260 if(rReg[0] == 0x03) 00261 { 00262 // printf(" (Vib Success!!)\n\n"); 00263 00264 //write 0x00(ID_CON_ADDR) 00265 wReg[1] = 0x00; 00266 wACK = i2c.write(busAddr, wReg, 2); 00267 isTempInitOk = true; 00268 // wait_ms(250); 00269 // wait_ms(60); 00270 wait_ms(150); 00271 } 00272 else if (10 == iTempInitCount) 00273 { 00274 isTempInitOk = false; 00275 break; 00276 } 00277 else 00278 { 00279 printf("."); 00280 ++iTempInitCount; 00281 } 00282 00283 // wait_ms(500); 00284 // wait_ms(80); 00285 wait_ms(200); 00286 } 00287 #endif 00288 printf("(OK)\n"); 00289 //온도 init_끝 00290 00291 00292 00293 00294 //RF init_시작 00295 // myspirit.attach_irq_callback(callback_func); 00296 if (myspirit.on()) 00297 { 00298 printf("ERROR.\n"); 00299 } 00300 else 00301 { 00302 printf("OK.\n"); 00303 } 00304 //RF init_끝 00305 //************************************************************************************* 00306 00307 00308 00309 //************************************************************************************* 00310 //HJM SUDO code : 1초 타이머 인터럽트 생성, 1초 간격 호출 함수 : UserTimerInterrupt() 00311 //************************************************************************************* 00312 // Timeout timeoutUserInterrupt; //1번만 실행 00313 Ticker timeoutUserInterrupt; //매번 실행 00314 // Ticker timeoutUserInterrupt2; //매번 실행 00315 // timeoutUserInterrupt.attach(&UserTimerInterrupt, 1.0); //1 sec 00316 timeoutUserInterrupt.attach(&UserTimerInterrupt, 900.0); //15 minuite, reset test 00317 // timeoutUserInterrupt2.attach(&UserTimerInterrupt2, 1.0); //1 sec 00318 // timeoutUserInterrupt.attach(&UserTimerInterrupt, 5.0); //test, 5sec 00319 //************************************************************************************* 00320 00321 // wait(1); 00322 00323 00324 while(1) 00325 { 00326 __WFE(); //HJM : sleep 모드 ON 00327 00328 UserTimerInterrupt_Execution(); 00329 UserTimerInterrupt_Execution2(); 00330 00331 // myspirit.off(); 00332 // myspirit.on(); 00333 00334 //if (0 == (iMainCounting % 10)) 00335 // { 00336 //// myspirit.reset_board(); 00337 //// iMainCounting = 0; 00338 // iMainCounting = 1; 00339 // myspirit.off(); 00340 // myspirit.on(); 00341 // } 00342 00343 if (true == isTimerInterruptStarted) 00344 { 00345 isTimerInterruptStarted = false; 00346 SendData(myspirit); 00347 } 00348 } 00349 00350 00351 // return 0; //unreachable 00352 } 00353 00354 static void callback_func(int event) 00355 { 00356 #ifdef CUBEBITE_DEBUG 00357 printf("[CUBEBITE] callback_func(int event) \n"); 00358 #endif 00359 00360 if(event == SimpleSpirit1::RX_DONE) 00361 { 00362 bRxDoneFlag = 1; 00363 } 00364 else if (event == SimpleSpirit1::TX_DONE) 00365 { 00366 bTxDoneFlag = 1; 00367 } 00368 } 00369 00370 00371 00372 void UserTimerInterrupt(void) 00373 { 00374 resetHW.write(RESET_HW_ON); 00375 00376 return; 00377 } 00378 00379 00380 00381 int UserTimerInterrupt_Execution(void) 00382 { 00383 if (true == isTempInitOk) 00384 { 00385 wReg[0] = ID_DATA_ADDR; 00386 00387 i2c.write(busAddr, wReg, 1); 00388 i2c.read(busAddr, rReg, 2); 00389 00390 Tem16 = (rReg[0] << 8) | (rReg[1]); 00391 Tem16 >>= 5; 00392 TempData = Tem16; 00393 00394 TempData = TempData *0.125; 00395 } 00396 00397 return 0; 00398 } 00399 00400 00401 volatile int iFunctionCallCounting = 0; 00402 bool isTempOk = false; 00403 int UserTimerInterrupt_Execution2(void) 00404 { 00405 // printf("UserTimerInterrupt_Execution 호출, dataCount : [%d]\n", dataCount); 00406 ++iFunctionCallCounting; 00407 00408 if (false == isTimerInterruptStarted) 00409 { 00410 //진동 00411 adxl362.ACC_GetXYZ8(&x8, &y8, &z8); 00412 if (0 == x8) 00413 { 00414 uiSendBuf[dataCount++] = '0'; 00415 uiSendBuf[dataCount++] = 'x'; 00416 uiSendBuf[dataCount++] = '@'; 00417 } 00418 else 00419 { 00420 int8tX8Result = x8; 00421 00422 uiSendBuf[dataCount++] = (uint8_t)int8tX8Result; 00423 } 00424 if (0 == y8) 00425 { 00426 uiSendBuf[dataCount++] = '0'; 00427 uiSendBuf[dataCount++] = 'y'; 00428 uiSendBuf[dataCount++] = '@'; 00429 } 00430 else 00431 { 00432 int8tY8Result = y8; 00433 00434 uiSendBuf[dataCount++] = (uint8_t)int8tY8Result; 00435 } 00436 if (0 == z8) 00437 { 00438 uiSendBuf[dataCount++] = '0'; 00439 uiSendBuf[dataCount++] = 'z'; 00440 uiSendBuf[dataCount++] = '@'; 00441 } 00442 else 00443 { 00444 int8tZ8Result = z8; 00445 00446 uiSendBuf[dataCount++] = (uint8_t)int8tZ8Result; 00447 } 00448 00449 wait(0.025); // 25ms, Wait is required in this mode 00450 00451 //온도 00452 // temperature data receive 00453 if (true == isTempInitOk) 00454 { 00455 // wReg[0] = ID_DATA_ADDR; 00456 // 00457 // i2c.write(busAddr, wReg, 1); 00458 // i2c.read(busAddr, rReg, 2); 00459 // 00460 // Tem16 = (rReg[0] << 8) | (rReg[1]); 00461 // Tem16 >>= 5; 00462 // TempData = Tem16; 00463 // 00464 // TempData = TempData *0.125; 00465 00466 if (0 == rReg[0]) 00467 { 00468 uiSendBuf[dataCount++] = '@'; 00469 uiSendBuf[dataCount++] = '0'; 00470 // uiSendBuf[dataCount++] = '1'; 00471 } 00472 else 00473 { 00474 uiSendBuf[dataCount++] = (uint8_t)rReg[0]; 00475 } 00476 00477 00478 00479 if (0 == rReg[1]) 00480 { 00481 uiSendBuf[dataCount++] = '@'; 00482 uiSendBuf[dataCount++] = '0'; 00483 // uiSendBuf[dataCount++] = '2'; 00484 } 00485 else 00486 { 00487 uiSendBuf[dataCount++] = (uint8_t)rReg[1]; 00488 } 00489 } 00490 else 00491 { 00492 uiSendBuf[dataCount++] = '@'; 00493 uiSendBuf[dataCount++] = '0'; 00494 // uiSendBuf[dataCount++] = '1'; 00495 uiSendBuf[dataCount++] = '@'; 00496 uiSendBuf[dataCount++] = '0'; 00497 // uiSendBuf[dataCount++] = '2'; 00498 } 00499 00500 uiSendBuf[dataCount++] = '#'; 00501 uiSendBuf[dataCount++] = '#'; 00502 00503 wait(0.025); // 25ms, Wait is required in this mode 00504 } 00505 00506 00507 00508 00509 00510 // if (iFunctionCallCounting >= 10) //10번의 측정이 끝나면, 00511 if (iFunctionCallCounting >= 8) //8번의 측정이 끝나면, 180703 온도 0도 표현시 버그 발생.. 00512 { 00513 isTimerInterruptStarted = true; 00514 // printf("if in!!\n"); 00515 00516 uiSendBuf[dataCount++] = '*'; 00517 uiSendBuf[dataCount++] = '*'; 00518 00519 uiSendBuf[dataCount] = CUBEBITE_SENSOR_UUID; 00520 00521 iFunctionCallCounting = 0; 00522 } 00523 00524 return 0; 00525 } 00526 00527 00528 00529 00530 00531 00532 00533 00534 00535 00536 00537 00538 00539 00540 00541 /** 00542 * @brief SendData 00543 * @param None 00544 * @retval None 00545 */ 00546 static int SendData(SimpleSpirit1& myspirit) 00547 { 00548 // ++iMainCounting; 00549 // printf("iMainCounting : %d\n", iMainCounting); 00550 // ++iIsResetStartCountingInterrupt; 00551 00552 isTempOk = false; 00553 #ifdef CUBEBITE_DEBUG 00554 printf("=========================================\n"); 00555 #endif //CUBEBITE_DEBUG 00556 00557 #ifdef CUBEBITE_DEBUG 00558 printf("[CUBEBITE] SendData() 호출 됨.\n"); 00559 #endif //CUBEBITE_DEBUG 00560 00561 int iRet; 00562 00563 // while(1 == myspirit.channel_clear()); 00564 // while(myspirit.is_receiving()); /* wait for ongoing RX ends 180110 HJM 아마 이 전에 보내던 거 마저 다 보내려고 하는 거 같다. */ 00565 00566 00567 00568 00569 size_t curr_len = strlen((const char*)uiSendBuf) + 1; 00570 // printf("curr_len : [%d]\n", curr_len); 00571 00572 00573 00574 iRet = myspirit.send(uiSendBuf, curr_len); 00575 00576 00577 dataCount = 0; 00578 for (int iTemp = 0; iTemp < TEST_STR_LEN; ++iTemp) 00579 { 00580 uiSendBuf[iTemp] = 0; 00581 } 00582 00583 printf("iIsResetStartCounting : %d\n", iIsResetStartCounting); 00584 if (10 < iIsResetStartCounting) 00585 {//HJM : 아예 뻗으면 하드웨어 리셋으로 00586 iIsResetStartCounting = 0; 00587 //180709 HJM : 하드웨어 리셋이 아니고 SPIRIT1 만 리셋하는 걸로 수정 00588 resetHW.write(RESET_HW_ON); 00589 00590 return RADIO_TX_ERR_RESET; 00591 } 00592 00593 00594 if (iRet == RADIO_TX_OK) 00595 { 00596 iIsResetStartCounting = 0; 00597 return RADIO_TX_OK; 00598 } 00599 else if (iRet == RADIO_TX_ERR) 00600 { 00601 myspirit.off(); 00602 myspirit.on(); 00603 //HJM : 테스트 !!, 00604 // while(1 == myspirit.channel_clear()); 00605 ++iIsResetStartCounting; 00606 return RADIO_TX_ERR; 00607 } 00608 else if (iRet == RADIO_TX_COLLISION) 00609 { 00610 return RADIO_TX_COLLISION; 00611 } 00612 else if (iRet == RADIO_TX_NOACK) 00613 { 00614 ++iIsResetStartCounting; 00615 return RADIO_TX_NOACK; 00616 } 00617 else if (iRet == RADIO_TX_ERR_RESET) 00618 { 00619 printf("RADIO_TX_ERR_RESET return.\n"); 00620 ++iIsResetStartCounting; 00621 00622 //소프트웨어 리셋 00623 myspirit.reset_board(); 00624 myspirit.off(); 00625 myspirit.on(); 00626 00627 //180703 HJM : 하드웨어 리셋으로 바꿈 00628 // resetHW.write(SPIRIT1_POWER_OFF); 00629 return RADIO_TX_ERR_RESET; 00630 } 00631 00632 dataCount = 0; 00633 00634 for (int iTemp = 0; iTemp < TEST_STR_LEN; ++iTemp) 00635 { 00636 uiSendBuf[iTemp] = 0; 00637 } 00638 00639 00640 00641 00642 00643 #ifdef CUBEBITE_DEBUG 00644 printf("[CUBEBITE] SendData() 호출 끝.\n"); 00645 #endif 00646 00647 return -10; 00648 } 00649 00650 00651
Generated on Sun Jul 17 2022 05:57:50 by
