main(6228).c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup ble_sdk_uart_over_ble_main main.c
  43. * @{
  44. * @ingroup ble_sdk_app_nus_eval
  45. * @brief UART over BLE application main file.
  46. *
  47. * This file contains the source code for a sample application that uses the Nordic UART service.
  48. * This application uses the @ref srvlib_conn_params module.
  49. */
  50. /******************************************************************************************************************
  51. *
  52. * V2022/01/25
  53. * @ Battery 연결시 부저음 추가
  54. * @ 와치독 적용
  55. *
  56. *
  57. * V2022/02/10
  58. * @ 비상벨 AES 암호화
  59. *
  60. *
  61. *
  62. *
  63. *
  64. *
  65. *
  66. *
  67. *
  68. *
  69. *
  70. *
  71. *
  72. *
  73. *
  74. *
  75. *
  76. *
  77. *
  78. *
  79. *******************************************************************************************************************/
  80. #include <stdint.h>
  81. #include <string.h>
  82. #include "nordic_common.h"
  83. #include "nrf.h"
  84. #include "ble_hci.h"
  85. #include "ble_advdata.h"
  86. #include "ble_advertising.h"
  87. #include "ble_conn_params.h"
  88. #include "nrf_sdh.h"
  89. #include "nrf_sdh_soc.h"
  90. #include "nrf_sdh_ble.h"
  91. #include "nrf_ble_gatt.h"
  92. #include "nrf_ble_qwr.h"
  93. #include "app_pwm.h"
  94. #include "app_timer.h"
  95. #include "ble_nus.h"
  96. #include "app_uart.h"
  97. #include "app_util_platform.h"
  98. #include "bsp_btn_ble.h"
  99. #include "nrf_pwr_mgmt.h"
  100. #include "nrf_delay.h"
  101. #if defined (UART_PRESENT)
  102. #include "nrf_uart.h"
  103. #endif
  104. #if defined (UARTE_PRESENT)
  105. #include "nrf_uarte.h"
  106. #endif
  107. #include "nrf_log.h"
  108. #include "nrf_log_ctrl.h"
  109. #include "nrf_log_default_backends.h"
  110. #include "nrf_drv_saadc.h"
  111. #include "nrf_drv_rtc.h"
  112. #include "nrf_drv_wdt.h"
  113. #include "main.h"
  114. #include "sysmgr.h"
  115. #include "dio.h"
  116. #include "nrf_power.h"
  117. #include "fds.h"
  118. #include "nrf_fstorage.h"
  119. #define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
  120. //#define DEVICE_NAME "Nordic_UART" /**< Name of device. Will be included in the advertising data. */
  121. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
  122. #define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
  123. #define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
  124. #define APP_ADV_DURATION 300 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
  125. #define APP_TIMER_PRESCALER 0
  126. #define MIN_CONN_INTERVAL MSEC_TO_UNITS(20, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
  127. #define MAX_CONN_INTERVAL MSEC_TO_UNITS(75, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
  128. #define SLAVE_LATENCY 0 /**< Slave latency. */
  129. #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
  130. #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
  131. #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
  132. #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
  133. #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  134. #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
  135. #define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
  136. #define COMPARE_COUNTERTIME (3UL) /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
  137. #define TICK_EVENT_OUTPUT BSP_LED_0 /**< Pin number for indicating tick event. */
  138. #define COMPARE_EVENT_OUTPUT BSP_LED_1
  139. BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); /**< BLE NUS service instance. */
  140. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  141. NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
  142. BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
  143. #define TIMER_MS_INTERVAL APP_TIMER_TICKS(25)
  144. APP_TIMER_DEF(m_ms_timer_id);
  145. APP_TIMER_DEF(wakeup_timer_id);
  146. APP_TIMER_DEF(m_our_char_timer_id);
  147. nrf_drv_wdt_channel_id m_channel_id;
  148. void WdtRunMode();
  149. #define KEEPALIVE_INTERVAL_30_SEC 30*1000
  150. #define KEEPALIVE_INTERVAL_1_MIN 60*1000
  151. #define KEEPALIVE_INTERVAL_30_MIN 30*60*1000
  152. #define KEEPALIVE_INTERVAL_1_HOUR 60*60*1000
  153. #define KEEPALIVE_INTERVAL_1_DAY 24*60*6*1000
  154. #define WAKEUP_TIME_INVERVAL APP_TIMER_TICKS(KEEPALIVE_INTERVAL_1_HOUR)
  155. //#define WAKEUP_TIME_INVERVAL APP_TIMER_TICKS(KEEPALIVE_INTERVAL_30_SEC)
  156. //#define WAKEUP_TIME_INVERVAL APP_TIMER_TICKS(30*1000)
  157. //#define WAKEUP_TIME_INVERVAL APP_TIMER_TICKS(15000)
  158. #define OUR_CHAR_TIMER_INTERVAL APP_TIMER_TICKS(1) // 1 ms intervals
  159. char DEVICE_NAME[] = {'I', 'C', 'S', 'W', 'E', 'S', 0x44,0x33,0xC0,0xFF,0xFF,0xFF };
  160. /* Flag to check fds initialization. */
  161. static bool volatile m_fds_initialized;
  162. /* Array to map FDS events to strings. */
  163. static char const * fds_evt_str[] =
  164. {
  165. "FDS_EVT_INIT",
  166. "FDS_EVT_WRITE",
  167. "FDS_EVT_UPDATE",
  168. "FDS_EVT_DEL_RECORD",
  169. "FDS_EVT_DEL_FILE",
  170. "FDS_EVT_GC",
  171. };
  172. /* Keep track of the progress of a delete_all operation. */
  173. static struct
  174. {
  175. bool delete_next; //!< Delete next record.
  176. bool pending; //!< Waiting for an fds FDS_EVT_DEL_RECORD event, to delete the next record.
  177. } m_delete_all;
  178. static void uart_init(void);
  179. static void advertising_init(void);
  180. static void buttons_leds_init(bool * p_erase_bonds);
  181. static void log_init(void);
  182. static void power_management_init(void);
  183. void wakeup_timer_handler(void * p_context);
  184. static void leds_config(void);
  185. void Battery_Adc_Read(void);
  186. static void idle_state_handle(void);
  187. //static void advertising_start(void);
  188. static void timer_ms_timeout_handler(void * p_context);
  189. static void on_adv_evt(ble_adv_evt_t ble_adv_evt);
  190. uint8_t advertising_flag = 0;
  191. static void fds_evt_handler(fds_evt_t const * p_evt)
  192. {
  193. if (p_evt->result == NRF_SUCCESS)
  194. {
  195. NRF_LOG_INFO("Event: %s received (NRF_SUCCESS)",
  196. fds_evt_str[p_evt->id]);
  197. }
  198. else
  199. {
  200. NRF_LOG_INFO("Event: %s received (%s)",
  201. fds_evt_str[p_evt->id],
  202. fds_err_str(p_evt->result));
  203. }
  204. switch (p_evt->id)
  205. {
  206. case FDS_EVT_INIT:
  207. if (p_evt->result == NRF_SUCCESS)
  208. {
  209. m_fds_initialized = true;
  210. }
  211. break;
  212. case FDS_EVT_WRITE:
  213. {
  214. if (p_evt->result == NRF_SUCCESS)
  215. {
  216. NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->write.record_id);
  217. NRF_LOG_INFO("File ID:\t0x%04x", p_evt->write.file_id);
  218. NRF_LOG_INFO("Record key:\t0x%04x", p_evt->write.record_key);
  219. }
  220. } break;
  221. case FDS_EVT_DEL_RECORD:
  222. {
  223. if (p_evt->result == NRF_SUCCESS)
  224. {
  225. NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->del.record_id);
  226. NRF_LOG_INFO("File ID:\t0x%04x", p_evt->del.file_id);
  227. NRF_LOG_INFO("Record key:\t0x%04x", p_evt->del.record_key);
  228. }
  229. m_delete_all.pending = false;
  230. } break;
  231. default:
  232. break;
  233. }
  234. }
  235. void ReMac_Mapping();
  236. ble_gap_addr_t old_ble_addr;
  237. ble_gap_addr_t new_ble_addr;
  238. int SysStatus;
  239. int SysCounter;
  240. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
  241. static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  242. static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifier. */
  243. {
  244. {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}
  245. };
  246. APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1.
  247. static volatile bool ready_flag; // A flag indicating PWM status.
  248. #define SAMPLES_IN_BUFFER 5
  249. static nrf_saadc_value_t m_buffer_pool[SAMPLES_IN_BUFFER];
  250. int SysCounter;
  251. ble_advertising_t* p_m_advertising;
  252. void SendEmgData(void);
  253. void SendTestData(void);
  254. void SendRegData(void);
  255. void SendAliveData(void);
  256. void bsp_event_handler(bsp_event_t event);
  257. void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
  258. {
  259. if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
  260. {
  261. nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
  262. }
  263. for(uint8_t i = 1 ; i < SAMPLES_IN_BUFFER ; i++)
  264. {
  265. NRF_LOG_INFO("ADC:%04d\r\n",p_event->data.done.p_buffer[i]);
  266. }
  267. }
  268. void SaadcInit(void)
  269. {
  270. nrf_saadc_channel_config_t channel_config =
  271. NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
  272. nrf_drv_saadc_init(NULL, saadc_callback);
  273. nrf_drv_saadc_channel_init(2, &channel_config);
  274. }
  275. void wdt_event_handler(void)
  276. {
  277. //bsp_board_leds_off();
  278. NRF_LOG_INFO("<< wdt_event_handler() >>");
  279. }
  280. void BootLoad_Reboot()
  281. {
  282. WdtRunMode();
  283. nrf_drv_wdt_channel_feed(m_channel_id);
  284. nrf_power_gpregret_set(0xB1);
  285. NVIC_SystemReset();
  286. }
  287. // ALREADY_DONE_FOR_YOU: This is a timer event handler
  288. // 1m sec Interrupt
  289. static void timer_timeout_handler(void * p_context)
  290. {
  291. uint32_t err_code;
  292. SystemTimer.MS_1++;
  293. SystemTimer.UART_LAST_RECV_TIMER++;
  294. SystemTimer.SEND_TIMER++;
  295. SystemTimer.BUZZER_TIMER--;
  296. SystemManager.BleConnectTime++;
  297. SystemTimer.TIM_KEY++;
  298. SystemTimer.SLEEP_TIMER++;
  299. // 500ms 마다 SystemManager.BleSendStatus 상태확인하여 수신기에 데이타 전송
  300. if(SystemManager.BleConnected){
  301. if( SystemManager.BleSendStatus && (SystemTimer.SEND_TIMER%500 ==0))
  302. {
  303. NRF_LOG_INFO("SystemManager.BleConnected:%d\n", SystemManager.BleConnected);
  304. if( SystemManager.EmgSendButton == BUTTON_EMG ) // EMG
  305. {
  306. SendEmgData();
  307. SystemTimer.EMG_SEND_RUN = 0;
  308. }else if( SystemManager.EmgSendButton == BUTTON_TEST ) // TEST
  309. {
  310. SendTestData();
  311. SystemTimer.EMG_SEND_RUN = 0;
  312. //SystemManager.EmgSendButton = BUTTON_NONE;
  313. }else if( SystemManager.EmgSendButton == BUTTON_ALIVE ) // Alive
  314. {
  315. SendAliveData();
  316. SystemTimer.EMG_SEND_RUN = 0;
  317. //SystemManager.EmgSendButton = BUTTON_NONE;
  318. }else if( SystemManager.EmgSendButton == BUTTON_REG ) // 등록
  319. {
  320. SendRegData();
  321. SystemTimer.EMG_SEND_RUN = 0;
  322. //SystemManager.EmgSendButton = 11;
  323. //ParkPowerOff();
  324. }
  325. }
  326. }else{
  327. //NRF_LOG_INFO("SystemManager.BleConnected:%d\n", SystemManager.BleConnected);
  328. // BLE ADVERTISE RECONNECT START
  329. }
  330. if( SystemTimer.SEND_TIMER > 3000 )
  331. {
  332. NVIC_SystemReset(); // SYSTEM RESET
  333. }
  334. switch(SystemManager.BuzzerType){
  335. case BUZZER_EMG: // EMG
  336. if(SystemManager.Batt > BATTERY_LOW_LEVEL){
  337. if( SystemTimer.BUZZER_TIMER == 150 )
  338. {
  339. app_pwm_channel_duty_set(&PWM1, 0, 0);
  340. }else if( SystemTimer.BUZZER_TIMER == 100 )
  341. {
  342. app_pwm_channel_duty_set(&PWM1, 0, 50);
  343. }else if( SystemTimer.BUZZER_TIMER < 50 )
  344. {
  345. app_pwm_channel_duty_set(&PWM1, 0, 0);
  346. SystemManager.BuzzerType = BUZZER_NONE;
  347. app_pwm_disable(&PWM1);
  348. }
  349. }else{
  350. if( SystemTimer.BUZZER_TIMER == 500 )
  351. {
  352. app_pwm_channel_duty_set(&PWM1, 0, 0);
  353. }else if( SystemTimer.BUZZER_TIMER == 450 )
  354. {
  355. app_pwm_channel_duty_set(&PWM1, 0, 50);
  356. }else if( SystemTimer.BUZZER_TIMER == 350 )
  357. {
  358. app_pwm_channel_duty_set(&PWM1, 0, 0);
  359. }else if( SystemTimer.BUZZER_TIMER == 300 )
  360. {
  361. app_pwm_channel_duty_set(&PWM1, 0, 50);
  362. }else if( SystemTimer.BUZZER_TIMER == 200 )
  363. {
  364. app_pwm_channel_duty_set(&PWM1, 0, 0);
  365. }else if( SystemTimer.BUZZER_TIMER == 150 )
  366. {
  367. app_pwm_channel_duty_set(&PWM1, 0, 50);
  368. }else if( SystemTimer.BUZZER_TIMER < 100 )
  369. {
  370. app_pwm_channel_duty_set(&PWM1, 0, 0);
  371. SystemManager.BuzzerType = BUZZER_NONE;
  372. app_pwm_disable(&PWM1);
  373. }
  374. }
  375. break;
  376. case BUZZER_TEST: //TEST
  377. if(SystemManager.Batt > BATTERY_LOW_LEVEL){
  378. if( SystemTimer.BUZZER_TIMER < 10 )
  379. {
  380. app_pwm_channel_duty_set(&PWM1, 0, 0);
  381. SystemManager.BuzzerType = BUZZER_NONE;
  382. app_pwm_disable(&PWM1);
  383. }
  384. }else{ // LOW BATTERY (20% Under)
  385. if( SystemTimer.BUZZER_TIMER == 500 )
  386. {
  387. app_pwm_channel_duty_set(&PWM1, 0, 0);
  388. }else if( SystemTimer.BUZZER_TIMER == 450 )
  389. {
  390. app_pwm_channel_duty_set(&PWM1, 0, 50);
  391. }else if( SystemTimer.BUZZER_TIMER == 350 )
  392. {
  393. app_pwm_channel_duty_set(&PWM1, 0, 0);
  394. }else if( SystemTimer.BUZZER_TIMER == 300 )
  395. {
  396. app_pwm_channel_duty_set(&PWM1, 0, 50);
  397. }else if( SystemTimer.BUZZER_TIMER == 200 )
  398. {
  399. app_pwm_channel_duty_set(&PWM1, 0, 0);
  400. }else if( SystemTimer.BUZZER_TIMER == 150 )
  401. {
  402. app_pwm_channel_duty_set(&PWM1, 0, 50);
  403. }else if( SystemTimer.BUZZER_TIMER < 100 )
  404. {
  405. app_pwm_channel_duty_set(&PWM1, 0, 0);
  406. SystemManager.BuzzerType = BUZZER_NONE;
  407. app_pwm_disable(&PWM1);
  408. }
  409. }
  410. break;
  411. case BUZZER_REG: //등록
  412. if( SystemTimer.BUZZER_TIMER < 100 )
  413. {
  414. app_pwm_channel_duty_set(&PWM1, 0, 0);
  415. }else if( SystemTimer.BUZZER_TIMER == 100 )
  416. {
  417. // app_pwm_channel_duty_set(&PWM1, 0, 50);
  418. }else if( SystemTimer.BUZZER_TIMER < 50 )
  419. {
  420. app_pwm_channel_duty_set(&PWM1, 0, 0);
  421. SystemManager.BuzzerType = BUZZER_NONE;
  422. app_pwm_disable(&PWM1);
  423. }
  424. break;
  425. case BUZZER_BOOT_LOADER: //BOOT LOAD REBOOT
  426. if( SystemTimer.BUZZER_TIMER == 800 )
  427. {
  428. app_pwm_channel_duty_set(&PWM1, 0, 0);
  429. }else if( SystemTimer.BUZZER_TIMER == 700 )
  430. {
  431. app_pwm_channel_duty_set(&PWM1, 0, 50);
  432. }else if( SystemTimer.BUZZER_TIMER == 500 )
  433. {
  434. app_pwm_channel_duty_set(&PWM1, 0, 0);
  435. }else if( SystemTimer.BUZZER_TIMER == 400 )
  436. {
  437. app_pwm_channel_duty_set(&PWM1, 0, 50);
  438. }else if( SystemTimer.BUZZER_TIMER == 200 )
  439. {
  440. app_pwm_channel_duty_set(&PWM1, 0, 0);
  441. }else if( SystemTimer.BUZZER_TIMER == 100 )
  442. {
  443. app_pwm_channel_duty_set(&PWM1, 0, 50);
  444. }else if( SystemTimer.BUZZER_TIMER < 100 )
  445. {
  446. app_pwm_channel_duty_set(&PWM1, 0, 0);
  447. SystemManager.BuzzerType = BUZZER_NONE;
  448. app_pwm_disable(&PWM1);
  449. BootLoad_Reboot();
  450. }
  451. break;
  452. case BUZZER_POWER_ON: // 밧데리 연결시 동작
  453. if( SystemTimer.BUZZER_TIMER == 250 )
  454. {
  455. app_pwm_channel_duty_set(&PWM1, 0, 0);
  456. }else if( SystemTimer.BUZZER_TIMER == 200 )
  457. {
  458. app_pwm_channel_duty_set(&PWM1, 0, 50);
  459. }
  460. else if( SystemTimer.BUZZER_TIMER == 150 )
  461. {
  462. app_pwm_channel_duty_set(&PWM1, 0, 0);
  463. }else if( SystemTimer.BUZZER_TIMER == 100 )
  464. {
  465. app_pwm_channel_duty_set(&PWM1, 0, 50);
  466. }else if( SystemTimer.BUZZER_TIMER < 50 )
  467. {
  468. app_pwm_channel_duty_set(&PWM1, 0, 0);
  469. SystemManager.BuzzerType = BUZZER_NONE;
  470. app_pwm_disable(&PWM1);
  471. SystemManager.GoSleep = 1;
  472. }
  473. break;
  474. }
  475. }
  476. /**@brief Function for assert macro callback.
  477. *
  478. * @details This function will be called in case of an assert in the SoftDevice.
  479. *
  480. * @warning This handler is an example only and does not fit a final product. You need to analyse
  481. * how your product is supposed to react in case of Assert.
  482. * @warning On assert from the SoftDevice, the system can only recover on reset.
  483. *
  484. * @param[in] line_num Line number of the failing ASSERT call.
  485. * @param[in] p_file_name File name of the failing ASSERT call.
  486. */
  487. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  488. {
  489. app_error_handler(DEAD_BEEF, line_num, p_file_name);
  490. }
  491. /**@brief Function for initializing the timer module.
  492. */
  493. static void timers_init(void)
  494. {
  495. ret_code_t err_code = app_timer_init();
  496. APP_ERROR_CHECK(err_code);
  497. app_timer_create(&m_our_char_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
  498. app_timer_create(&m_ms_timer_id, APP_TIMER_MODE_REPEATED, timer_ms_timeout_handler);
  499. }
  500. /**@brief Function for the GAP initialization.
  501. *
  502. * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
  503. * the device. It also sets the permissions and appearance.
  504. */
  505. static void gap_params_init(void)
  506. {
  507. uint32_t err_code;
  508. ble_gap_conn_params_t gap_conn_params;
  509. ble_gap_conn_sec_mode_t sec_mode;
  510. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  511. err_code = sd_ble_gap_device_name_set(&sec_mode,
  512. (const uint8_t *) DEVICE_NAME,
  513. strlen(DEVICE_NAME));
  514. APP_ERROR_CHECK(err_code);
  515. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  516. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  517. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  518. gap_conn_params.slave_latency = SLAVE_LATENCY;
  519. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  520. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  521. APP_ERROR_CHECK(err_code);
  522. }
  523. /**@brief Function for handling Queued Write Module errors.
  524. *
  525. * @details A pointer to this function will be passed to each service which may need to inform the
  526. * application about an error.
  527. *
  528. * @param[in] nrf_error Error code containing information about what went wrong.
  529. */
  530. static void nrf_qwr_error_handler(uint32_t nrf_error)
  531. {
  532. APP_ERROR_HANDLER(nrf_error);
  533. }
  534. /**@brief Function for handling the data from the Nordic UART Service.
  535. *
  536. * @details This function will process the data received from the Nordic UART BLE Service and send
  537. * it to the UART module.
  538. *
  539. * @param[in] p_evt Nordic UART Service event.
  540. */
  541. /**@snippet [Handling the data received over BLE] */
  542. static void nus_data_handler(ble_nus_evt_t * p_evt)
  543. {
  544. uint32_t i;
  545. uint32_t err_code;
  546. uint8_t Aes_temp[16];
  547. uint8_t Aes_Dec_temp[16];
  548. switch(p_evt->type){
  549. case BLE_NUS_EVT_RX_DATA:
  550. NRF_LOG_RAW_INFO("BLE_NUS_EVT_RX_DATA (AES) => ");
  551. for (i = 0; i < p_evt->params.rx_data.length; i++)
  552. {
  553. NRF_LOG_INFO("0x%02x ", *(p_evt->params.rx_data.p_data+i));
  554. }
  555. NRF_LOG_INFO("\n");
  556. for(i=0; i<16; i++){
  557. Aes_temp[i] = *(p_evt->params.rx_data.p_data+i);
  558. }
  559. WesDecryptEcb( &Aes_temp[0], &Aes_Dec_temp[0] );
  560. NRF_LOG_RAW_INFO("BLE_NUS_EVT_RX_DATA (ORG) => ");
  561. for (i=0; i<16; i++)
  562. {
  563. NRF_LOG_RAW_INFO("0x%02x, ", Aes_Dec_temp[i]);
  564. }
  565. NRF_LOG_RAW_INFO("\n");
  566. break;
  567. case BLE_NUS_EVT_TX_RDY:
  568. NRF_LOG_RAW_INFO("BLE_NUS_EVT_TX_RDY ======> \n");
  569. break;
  570. case BLE_NUS_EVT_COMM_STARTED:
  571. NRF_LOG_RAW_INFO("BLE_NUS_EVT_COMM_STARTED ======> \n");
  572. break;
  573. case BLE_NUS_EVT_COMM_STOPPED:
  574. NRF_LOG_RAW_INFO("BLE_NUS_EVT_COMM_STOPPED ======> \n");
  575. break;
  576. }
  577. }
  578. /**@snippet [Handling the data received over BLE] */
  579. /**@brief Function for initializing services that will be used by the application.
  580. */
  581. static void services_init(void)
  582. {
  583. uint32_t err_code;
  584. ble_nus_init_t nus_init;
  585. nrf_ble_qwr_init_t qwr_init = {0};
  586. // Initialize Queued Write Module.
  587. qwr_init.error_handler = nrf_qwr_error_handler;
  588. err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
  589. APP_ERROR_CHECK(err_code);
  590. // Initialize NUS.
  591. memset(&nus_init, 0, sizeof(nus_init));
  592. nus_init.data_handler = nus_data_handler;
  593. err_code = ble_nus_init(&m_nus, &nus_init);
  594. APP_ERROR_CHECK(err_code);
  595. }
  596. /**@brief Function for handling an event from the Connection Parameters Module.
  597. *
  598. * @details This function will be called for all events in the Connection Parameters Module
  599. * which are passed to the application.
  600. *
  601. * @note All this function does is to disconnect. This could have been done by simply setting
  602. * the disconnect_on_fail config parameter, but instead we use the event handler
  603. * mechanism to demonstrate its use.
  604. *
  605. * @param[in] p_evt Event received from the Connection Parameters Module.
  606. */
  607. static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
  608. {
  609. uint32_t err_code;
  610. if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
  611. {
  612. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  613. APP_ERROR_CHECK(err_code);
  614. }
  615. }
  616. /**@brief Function for handling errors from the Connection Parameters module.
  617. *
  618. * @param[in] nrf_error Error code containing information about what went wrong.
  619. */
  620. static void conn_params_error_handler(uint32_t nrf_error)
  621. {
  622. APP_ERROR_HANDLER(nrf_error);
  623. }
  624. /**@brief Function for initializing the Connection Parameters module.
  625. */
  626. static void conn_params_init(void)
  627. {
  628. uint32_t err_code;
  629. ble_conn_params_init_t cp_init;
  630. memset(&cp_init, 0, sizeof(cp_init));
  631. cp_init.p_conn_params = NULL;
  632. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  633. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  634. cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
  635. cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
  636. cp_init.disconnect_on_fail = false;
  637. cp_init.evt_handler = on_conn_params_evt;
  638. cp_init.error_handler = conn_params_error_handler;
  639. err_code = ble_conn_params_init(&cp_init);
  640. APP_ERROR_CHECK(err_code);
  641. }
  642. /**@brief Function for putting the chip into sleep mode.
  643. *
  644. * @note This function will not return.
  645. */
  646. static void sleep_mode_enter(void)
  647. {
  648. uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  649. APP_ERROR_CHECK(err_code);
  650. // Prepare wakeup buttons.
  651. err_code = bsp_btn_ble_sleep_mode_prepare();
  652. APP_ERROR_CHECK(err_code);
  653. SysStatus = 300;
  654. // Go to system-off mode (this function will not return; wakeup will cause a reset).
  655. err_code = sd_power_system_off();
  656. APP_ERROR_CHECK(err_code);
  657. }
  658. /**@brief Function for handling advertising events.
  659. *
  660. * @details This function will be called for advertising events which are passed to the application.
  661. *
  662. * @param[in] ble_adv_evt Advertising event.
  663. */
  664. static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
  665. {
  666. uint32_t err_code;
  667. switch (ble_adv_evt)
  668. {
  669. case BLE_ADV_EVT_FAST:
  670. NRF_LOG_INFO("BLE_ADV_EVT_FAST");
  671. SystemManager.BleConnectTime = 0;
  672. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  673. SysStatus = 200;
  674. APP_ERROR_CHECK(err_code);
  675. break;
  676. case BLE_ADV_EVT_IDLE:
  677. NRF_LOG_INFO("BLE_ADV_EVT_IDLE");
  678. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  679. APP_ERROR_CHECK(err_code);
  680. app_pwm_disable(&PWM1);
  681. app_timer_stop(m_ms_timer_id);
  682. app_timer_stop(m_our_char_timer_id);
  683. NVIC_SystemReset();
  684. app_timer_start(wakeup_timer_id, WAKEUP_TIME_INVERVAL, NULL);
  685. err_code = sd_app_evt_wait();
  686. APP_ERROR_CHECK(err_code);
  687. //StartRTC();
  688. //app_timer_start(wakeup_timer_id, APP_TIMER_TICKS(15000), NULL);
  689. // Enter System ON sleep mode
  690. //__WFE();
  691. // Make sure any pending events are cleared
  692. //__SEV();
  693. // __WFE();
  694. //sleep_mode_enter();
  695. // err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  696. //APP_ERROR_CHECK(err_code);
  697. //app_timer_start(wakeup_timer_id, APP_TIMER_TICKS(5000), NULL);
  698. // err_code = sd_app_evt_wait();
  699. //SysStatus = 201;
  700. //app_uart_close();
  701. //idle_state_handle();
  702. break;
  703. default:
  704. break;
  705. }
  706. }
  707. int localNameChg = 0;
  708. /**@brief Function for handling BLE events.
  709. *
  710. * @param[in] p_ble_evt Bluetooth stack event.
  711. * @param[in] p_context Unused.
  712. */
  713. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  714. {
  715. uint32_t err_code;
  716. switch (p_ble_evt->header.evt_id)
  717. {
  718. case BLE_GAP_EVT_ADV_REPORT :
  719. NRF_LOG_INFO("BLE_GAP_EVT_ADV_REPORT ");
  720. break;
  721. case BLE_GAP_EVT_SCAN_REQ_REPORT:
  722. #if 0
  723. NRF_LOG_INFO("BLE_GAP_EVT_SCAN_REQ_REPORT");
  724. ble_gap_evt_scan_req_report_t * p_scan_req_report_t = (ble_gap_evt_scan_req_report_t *)&p_ble_evt->evt.gap_evt.params.scan_req_report;
  725. NRF_LOG_INFO("Peer Address = 0x%02x:%02x:%02x:%02x:%02x:%02x",
  726. p_scan_req_report_t->peer_addr.addr[0],
  727. p_scan_req_report_t->peer_addr.addr[1],
  728. p_scan_req_report_t->peer_addr.addr[2],
  729. p_scan_req_report_t->peer_addr.addr[3],
  730. p_scan_req_report_t->peer_addr.addr[4],
  731. p_scan_req_report_t->peer_addr.addr[5]);
  732. NRF_LOG_INFO("RSSI value = %d dBm", p_scan_req_report_t->rssi);
  733. SystemManager.PeerAddr[0] = p_scan_req_report_t->peer_addr.addr[0];
  734. SystemManager.PeerAddr[1] = p_scan_req_report_t->peer_addr.addr[1];
  735. SystemManager.PeerAddr[2] = p_scan_req_report_t->peer_addr.addr[2];
  736. SystemManager.PeerAddr[3] = p_scan_req_report_t->peer_addr.addr[3];
  737. SystemManager.PeerAddr[4] = p_scan_req_report_t->peer_addr.addr[4];
  738. SystemManager.PeerAddr[5] = p_scan_req_report_t->peer_addr.addr[5];
  739. sprintf(SystemManager.StrPeerAddr, "%02x%02x%02x%02x%02x%02x", p_scan_req_report_t->peer_addr.addr[0],
  740. p_scan_req_report_t->peer_addr.addr[1],
  741. p_scan_req_report_t->peer_addr.addr[2],
  742. p_scan_req_report_t->peer_addr.addr[3],
  743. p_scan_req_report_t->peer_addr.addr[4],
  744. p_scan_req_report_t->peer_addr.addr[5]);
  745. SystemManager.StrPeerAddr[12] = 0;
  746. if( Key1Flag )
  747. break;
  748. if( strncmp( SystemManager.StrRegPeerAddr, SystemManager.StrPeerAddr, 12 ) != 0 )
  749. {
  750. NRF_LOG_INFO("Reg Receiver Arrived.\n");
  751. break;
  752. }
  753. NRF_LOG_INFO("Reg Receiver Arrived.\n");
  754. if( localNameChg++ == 2 )
  755. {
  756. ble_gap_conn_params_t gap_conn_params;
  757. ble_gap_conn_sec_mode_t sec_mode;
  758. // memcpy( &DEVICE_NAME[6], SystemManager.RegPeerAddr, 6); // 원상복구
  759. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  760. sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
  761. sd_ble_gap_adv_stop(p_m_advertising->adv_handle);
  762. memcpy( &p_m_advertising->adv_data.adv_data.p_data[14], &DEVICE_NAME[6], 6 );
  763. memcpy( &p_m_advertising->adv_data.scan_rsp_data.p_data[14], &DEVICE_NAME[6], 6 );
  764. //ble_advdata_encode(p_m_advertising->p_adv_data, p_m_advertising->adv_data.adv_data.p_data, &p_m_advertising->adv_data.adv_data.len);
  765. sd_ble_gap_adv_set_configure(&p_m_advertising->adv_handle, &p_m_advertising->adv_data, &p_m_advertising->adv_params);
  766. sd_ble_gap_adv_start(p_m_advertising->adv_handle, APP_BLE_CONN_CFG_TAG);
  767. NRF_LOG_INFO("Local Name Change = %d", err_code);
  768. }
  769. else if( localNameChg == 2)
  770. {
  771. }
  772. #endif
  773. break;
  774. case BLE_GAP_EVT_CONNECTED:
  775. NRF_LOG_INFO("<============ BLE_GAP_EVT_CONNECTED =========>");
  776. err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  777. APP_ERROR_CHECK(err_code);
  778. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  779. err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
  780. APP_ERROR_CHECK(err_code);
  781. SystemManager.BleConnected = 1;
  782. SystemTimer.SEND_TIMER = 0;
  783. #if 0
  784. SystemManager.PeerAddr[0] = p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr[0];
  785. SystemManager.PeerAddr[1] = p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr[1];
  786. SystemManager.PeerAddr[2] = p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr[2];
  787. SystemManager.PeerAddr[3] = p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr[3];
  788. SystemManager.PeerAddr[4] = p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr[4];
  789. SystemManager.PeerAddr[5] = p_ble_evt->evt.gap_evt.params.connected.peer_addr.addr[5];
  790. sprintf(SystemManager.StrPeerAddr, "%02x%02x%02x%02x%02x%02x", SystemManager.PeerAddr[0],
  791. SystemManager.PeerAddr[1],
  792. SystemManager.PeerAddr[2],
  793. SystemManager.PeerAddr[3],
  794. SystemManager.PeerAddr[4],
  795. SystemManager.PeerAddr[5]);
  796. SystemManager.StrPeerAddr[12] = 0;
  797. #endif
  798. break;
  799. case BLE_GAP_EVT_DISCONNECTED:
  800. NRF_LOG_INFO("<============ BLE_GAP_EVT_DISCONNECTED =========>");
  801. // LED indication will be changed when advertising starts.
  802. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  803. NVIC_SystemReset();
  804. #if 0
  805. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  806. APP_ERROR_CHECK(err_code);
  807. app_pwm_disable(&PWM1);
  808. app_timer_stop(m_ms_timer_id);
  809. app_timer_stop(m_our_char_timer_id);
  810. app_timer_start(wakeup_timer_id, WAKEUP_TIME_INVERVAL, NULL);
  811. err_code = sd_app_evt_wait();
  812. APP_ERROR_CHECK(err_code);
  813. #endif
  814. break;
  815. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  816. NRF_LOG_DEBUG("PHY update request.");
  817. ble_gap_phys_t const phys =
  818. {
  819. .rx_phys = BLE_GAP_PHY_AUTO,
  820. .tx_phys = BLE_GAP_PHY_AUTO,
  821. };
  822. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  823. APP_ERROR_CHECK(err_code);
  824. break;
  825. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  826. // Pairing not supported
  827. err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  828. APP_ERROR_CHECK(err_code);
  829. break;
  830. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  831. // No system attributes have been stored.
  832. err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  833. APP_ERROR_CHECK(err_code);
  834. break;
  835. case BLE_GATTC_EVT_TIMEOUT:
  836. // Disconnect on GATT Client timeout event.
  837. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
  838. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  839. APP_ERROR_CHECK(err_code);
  840. break;
  841. case BLE_GATTS_EVT_TIMEOUT:
  842. // Disconnect on GATT Server timeout event.
  843. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
  844. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  845. APP_ERROR_CHECK(err_code);
  846. break;
  847. default:
  848. // No implementation needed.
  849. break;
  850. }
  851. }
  852. /**@brief Function for the SoftDevice initialization.
  853. *
  854. * @details This function initializes the SoftDevice and the BLE event interrupt.
  855. */
  856. static void ble_stack_init(void)
  857. {
  858. ret_code_t err_code;
  859. err_code = nrf_sdh_enable_request();
  860. APP_ERROR_CHECK(err_code);
  861. // Configure the BLE stack using the default settings.
  862. // Fetch the start address of the application RAM.
  863. uint32_t ram_start = 0;
  864. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  865. APP_ERROR_CHECK(err_code);
  866. // Enable BLE stack.
  867. err_code = nrf_sdh_ble_enable(&ram_start);
  868. APP_ERROR_CHECK(err_code);
  869. /* enable DCDC to save current */
  870. err_code = sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
  871. APP_ERROR_CHECK(err_code);
  872. // Register a handler for BLE events.
  873. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  874. }
  875. /**@brief Function for handling events from the GATT library. */
  876. void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
  877. {
  878. if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
  879. {
  880. m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  881. NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
  882. }
  883. NRF_LOG_DEBUG("ATT MTU exchange completed. central 0x%x peripheral 0x%x",
  884. p_gatt->att_mtu_desired_central,
  885. p_gatt->att_mtu_desired_periph);
  886. }
  887. /**@brief Function for initializing the GATT library. */
  888. void gatt_init(void)
  889. {
  890. ret_code_t err_code;
  891. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  892. APP_ERROR_CHECK(err_code);
  893. err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  894. APP_ERROR_CHECK(err_code);
  895. }
  896. /**@brief Function for handling events from the BSP module.
  897. *
  898. * @param[in] event Event generated by button press.
  899. */
  900. void bsp_event_handler(bsp_event_t event)
  901. {
  902. uint32_t err_code;
  903. NRF_LOG_INFO("BSP_EVENT_HANDLER => %d", event);
  904. switch (event)
  905. {
  906. case BSP_EVENT_DFU: // 들어오지 않음
  907. NRF_LOG_INFO("@@@@@@@ BSP_EVENT_DFU=>%d", event);
  908. break;
  909. case BSP_EVENT_ADVERTISING_START: // 들어오지 않음
  910. NRF_LOG_INFO("@@@@@@@ BSP_EVENT_ADVERTISING_START=>%d", event);
  911. break;
  912. case BSP_EVENT_ADVERTISING_STOP: // 들어오지 않음
  913. NRF_LOG_INFO("@@@@@@@ BSP_EVENT_ADVERTISING_STOP=>%d", event);
  914. break;
  915. case BSP_EVENT_SLEEP: // 들어오지 않음
  916. NRF_LOG_INFO("@@@@@@@ bsp_event_handler() -- BSP_EVENT_SLEEP");
  917. //sleep_mode_enter();
  918. break;
  919. case BSP_EVENT_WAKEUP: // 들어오지 않음
  920. NRF_LOG_INFO("@@@@@@@ BSP_EVENT_WAKEUP=>%d", event);
  921. advertising_flag = 0;
  922. break;
  923. case BSP_EVENT_DISCONNECT:
  924. NRF_LOG_INFO("bsp_event_handler() -- BSP_EVENT_DISCONNECT");
  925. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  926. if (err_code != NRF_ERROR_INVALID_STATE)
  927. {
  928. APP_ERROR_CHECK(err_code);
  929. }
  930. break;
  931. case BSP_EVENT_WHITELIST_OFF:
  932. NRF_LOG_INFO("bsp_event_handler() -- BSP_EVENT_WHITELIST_OFF");
  933. if (m_conn_handle == BLE_CONN_HANDLE_INVALID)
  934. {
  935. err_code = ble_advertising_restart_without_whitelist(&m_advertising);
  936. if (err_code != NRF_ERROR_INVALID_STATE)
  937. {
  938. APP_ERROR_CHECK(err_code);
  939. }
  940. }
  941. break;
  942. case BSP_EVENT_KEY_1: // event 15 TEST
  943. Battery_Adc_Read();
  944. err_code = app_timer_start(m_ms_timer_id, TIMER_MS_INTERVAL, NULL);
  945. APP_ERROR_CHECK(err_code);
  946. err_code = app_timer_start(m_our_char_timer_id, OUR_CHAR_TIMER_INTERVAL, NULL);
  947. APP_ERROR_CHECK(err_code);
  948. //strncpy( DEVICE_NAME, "ICSWES", 6);
  949. break;
  950. case BSP_EVENT_KEY_2: // event 16 EMG
  951. Battery_Adc_Read();
  952. err_code = app_timer_start(m_ms_timer_id, TIMER_MS_INTERVAL, NULL);
  953. APP_ERROR_CHECK(err_code);
  954. err_code = app_timer_start(m_our_char_timer_id, OUR_CHAR_TIMER_INTERVAL, NULL);
  955. APP_ERROR_CHECK(err_code);
  956. break;
  957. default:
  958. break;
  959. }
  960. }
  961. /* *@brief Undo the changes that we did when advertising has the timeout.
  962. */
  963. void wakeup_timer_handler(void * p_context)
  964. {
  965. uint32_t err_code;
  966. app_timer_stop(wakeup_timer_id);
  967. app_timer_start(m_ms_timer_id, TIMER_MS_INTERVAL, NULL);
  968. app_timer_start(m_our_char_timer_id, OUR_CHAR_TIMER_INTERVAL, NULL);
  969. bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
  970. APP_ERROR_CHECK(err_code);
  971. //err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
  972. err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  973. APP_ERROR_CHECK(err_code);
  974. SystemManager.EmgSendButton = BUTTON_ALIVE;
  975. NRF_LOG_INFO("==> Alive Send");
  976. SystemTimer.EMG_SEND_RUN = 1;
  977. SystemTimer.SEND_TIMER = 0;
  978. }
  979. // 25ms Timer
  980. static void timer_ms_timeout_handler(void * p_context)
  981. {
  982. UNUSED_PARAMETER(p_context);
  983. HSDInRun();
  984. }
  985. /**@brief Function for handling app_uart events.
  986. *
  987. * @details This function will receive a single character from the app_uart module and append it to
  988. * a string. The string will be be sent over BLE when the last character received was a
  989. * 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
  990. */
  991. /**@snippet [Handling the data received over UART] */
  992. void uart_event_handle(app_uart_evt_t * p_event)
  993. {
  994. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  995. static uint8_t index = 0;
  996. uint32_t err_code;
  997. switch (p_event->evt_type)
  998. {
  999. case APP_UART_DATA_READY:
  1000. NRF_LOG_DEBUG("uart_event_handle -- APP_UART_DATA_READY");
  1001. UNUSED_VARIABLE(app_uart_get(&data_array[index]));
  1002. index++;
  1003. if ((data_array[index - 1] == '\n') ||
  1004. (data_array[index - 1] == '\r') ||
  1005. (index >= m_ble_nus_max_data_len))
  1006. {
  1007. if (index > 1)
  1008. {
  1009. NRF_LOG_DEBUG("Ready to send data over BLE NUS");
  1010. NRF_LOG_HEXDUMP_DEBUG(data_array, index);
  1011. do
  1012. {
  1013. uint16_t length = (uint16_t)index;
  1014. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  1015. if ((err_code != NRF_ERROR_INVALID_STATE) &&
  1016. (err_code != NRF_ERROR_RESOURCES) &&
  1017. (err_code != NRF_ERROR_NOT_FOUND))
  1018. {
  1019. APP_ERROR_CHECK(err_code);
  1020. }
  1021. } while (err_code == NRF_ERROR_RESOURCES);
  1022. }
  1023. index = 0;
  1024. }
  1025. break;
  1026. case APP_UART_COMMUNICATION_ERROR:
  1027. NRF_LOG_DEBUG("uart_event_handle -- APP_UART_COMMUNICATION_ERROR");
  1028. APP_ERROR_HANDLER(p_event->data.error_communication);
  1029. break;
  1030. case APP_UART_FIFO_ERROR:
  1031. NRF_LOG_DEBUG("uart_event_handle -- APP_UART_FIFO_ERROR");
  1032. APP_ERROR_HANDLER(p_event->data.error_code);
  1033. break;
  1034. case APP_UART_TX_EMPTY:
  1035. NRF_LOG_DEBUG("uart_event_handle -- APP_UART_TX_EMPTY");
  1036. break;
  1037. case APP_UART_DATA:
  1038. NRF_LOG_DEBUG("uart_event_handle -- APP_UART_DATA");
  1039. break;
  1040. default:
  1041. break;
  1042. }
  1043. }
  1044. /**@snippet [Handling the data received over UART] */
  1045. /**@brief Function for initializing the UART module.
  1046. */
  1047. /**@snippet [UART Initialization] */
  1048. static void uart_init(void)
  1049. {
  1050. uint32_t err_code;
  1051. app_uart_comm_params_t const comm_params =
  1052. {
  1053. .rx_pin_no = RX_PIN_NUMBER,
  1054. .tx_pin_no = TX_PIN_NUMBER,
  1055. .rts_pin_no = RTS_PIN_NUMBER,
  1056. .cts_pin_no = CTS_PIN_NUMBER,
  1057. .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
  1058. .use_parity = false,
  1059. #if defined (UART_PRESENT)
  1060. .baud_rate = NRF_UART_BAUDRATE_115200
  1061. #else
  1062. .baud_rate = NRF_UARTE_BAUDRATE_115200
  1063. #endif
  1064. };
  1065. APP_UART_FIFO_INIT(&comm_params,
  1066. UART_RX_BUF_SIZE,
  1067. UART_TX_BUF_SIZE,
  1068. uart_event_handle,
  1069. APP_IRQ_PRIORITY_LOWEST,
  1070. err_code);
  1071. APP_ERROR_CHECK(err_code);
  1072. }
  1073. /**@snippet [UART Initialization] */
  1074. /**@brief Function for initializing the Advertising functionality.
  1075. */
  1076. static void advertising_init(void)
  1077. {
  1078. //p_m_advertising->adv_data.adv_data.p_data = p_m_advertising->enc_advdata;
  1079. ret_code_t err_code;
  1080. ble_advertising_init_t init;
  1081. char plevel = 0xfe;
  1082. ble_advdata_conn_int_t connParams;
  1083. connParams.min_conn_interval = 0x20;
  1084. connParams.max_conn_interval = 0x28;
  1085. memset(&init, 0, sizeof(init));
  1086. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  1087. init.advdata.include_appearance = false;
  1088. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  1089. init.advdata.p_tx_power_level = &plevel;
  1090. init.advdata.p_slave_conn_int = &connParams;
  1091. // Prepare the scan response manufacturer specific data packet
  1092. ble_advdata_manuf_data_t manuf_data_response;
  1093. uint8_t data_response[] = "M";
  1094. manuf_data_response.company_identifier = 0x0059;
  1095. manuf_data_response.data.p_data = data_response;
  1096. manuf_data_response.data.size = sizeof(data_response);
  1097. init.srdata.name_type = BLE_ADVDATA_FULL_NAME;
  1098. init.srdata.p_manuf_specific_data = &manuf_data_response;
  1099. init.config.ble_adv_fast_enabled = true;
  1100. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  1101. init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
  1102. init.evt_handler = on_adv_evt;
  1103. err_code = ble_advertising_init(&m_advertising, &init);
  1104. APP_ERROR_CHECK(err_code);
  1105. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  1106. p_m_advertising = &m_advertising;
  1107. }
  1108. /**@brief Function for initializing buttons and leds.
  1109. *
  1110. * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up.
  1111. */
  1112. static void buttons_leds_init(bool * p_erase_bonds)
  1113. {
  1114. bsp_event_t startup_event;
  1115. uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
  1116. APP_ERROR_CHECK(err_code);
  1117. err_code = bsp_btn_ble_init(NULL, &startup_event);
  1118. APP_ERROR_CHECK(err_code);
  1119. *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
  1120. }
  1121. /**@brief Function for initializing the nrf log module.
  1122. */
  1123. static void log_init(void)
  1124. {
  1125. ret_code_t err_code = NRF_LOG_INIT(NULL);
  1126. APP_ERROR_CHECK(err_code);
  1127. NRF_LOG_DEFAULT_BACKENDS_INIT();
  1128. }
  1129. /**@brief Function for initializing power management.
  1130. */
  1131. static void power_management_init(void)
  1132. {
  1133. ret_code_t err_code;
  1134. err_code = nrf_pwr_mgmt_init();
  1135. APP_ERROR_CHECK(err_code);
  1136. }
  1137. /**@brief Function for handling the idle state (main loop).
  1138. *
  1139. * @details If there is no pending log operation, then sleep until next the next event occurs.
  1140. */
  1141. static void idle_state_handle(void)
  1142. {
  1143. if (NRF_LOG_PROCESS() == false)
  1144. {
  1145. nrf_pwr_mgmt_run();
  1146. }
  1147. }
  1148. /**@brief Function for starting advertising.
  1149. */
  1150. void advertising_start(void)
  1151. {
  1152. uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  1153. APP_ERROR_CHECK(err_code);
  1154. advertising_flag = 1;
  1155. }
  1156. static void leds_config(void)
  1157. {
  1158. bsp_board_init(BSP_INIT_LEDS);
  1159. }
  1160. void Battery_Adc_Read(void)
  1161. {
  1162. uint32_t avr = 0;
  1163. nrf_delay_ms(5);
  1164. nrf_drv_saadc_sample_convert(NRF_SAADC_INPUT_AIN2, &m_buffer_pool[0]);
  1165. nrf_delay_ms(5);
  1166. nrf_drv_saadc_sample_convert(NRF_SAADC_INPUT_AIN2, &m_buffer_pool[1]);
  1167. nrf_delay_ms(5);
  1168. nrf_drv_saadc_sample_convert(NRF_SAADC_INPUT_AIN2, &m_buffer_pool[2]);
  1169. avr = m_buffer_pool[0] + m_buffer_pool[1] + m_buffer_pool[2];
  1170. avr /= 3;
  1171. SystemManager.Batt = (((float)avr*0.708)-200.0)*(100.0/(330.0-200.0));
  1172. if(SystemManager.Batt > 100)
  1173. SystemManager.Batt = 100;
  1174. NRF_LOG_INFO("Vol:%d -- [%d]\r\n",(int)((float)avr*0.708), SystemManager.Batt);
  1175. #if 0
  1176. if(nrf_drv_saadc_sample_convert(NRF_SAADC_INPUT_AIN2, m_buffer_pool) == NRF_SUCCESS)
  1177. {
  1178. // SystemManager.Batt = (((float)m_buffer_pool[0]*0.708)/330.0)*100;
  1179. SystemManager.Batt = (((float)m_buffer_pool[0]*0.708)-200.0)*(100.0/(330.0-200.0));
  1180. if(SystemManager.Batt > 100)
  1181. SystemManager.Batt = 100;
  1182. NRF_LOG_INFO("Vol:%d -- [%d]\r\n",(int)((float)m_buffer_pool[0]*0.708), SystemManager.Batt);
  1183. }
  1184. #endif
  1185. }
  1186. void Buzzer_Select(uint8_t state)
  1187. {
  1188. switch(state){
  1189. case BUZZER_NONE:
  1190. break;
  1191. case BUZZER_EMG:
  1192. if( SystemManager.BuzzerType == BUZZER_NONE )
  1193. {
  1194. SystemManager.BuzzerType = 1;
  1195. if(SystemManager.Batt > BATTERY_LOW_LEVEL)
  1196. SystemTimer.BUZZER_TIMER = 200;
  1197. else
  1198. SystemTimer.BUZZER_TIMER = 600;
  1199. app_pwm_enable(&PWM1);
  1200. app_pwm_channel_duty_set(&PWM1, 0, 50);
  1201. }
  1202. break;
  1203. case BUZZER_TEST:
  1204. if( SystemManager.BuzzerType == BUZZER_NONE )
  1205. {
  1206. SystemManager.BuzzerType = 2;
  1207. if(SystemManager.Batt > BATTERY_LOW_LEVEL)
  1208. SystemTimer.BUZZER_TIMER = 100;
  1209. else
  1210. SystemTimer.BUZZER_TIMER = 600;
  1211. app_pwm_enable(&PWM1);
  1212. app_pwm_channel_duty_set(&PWM1, 0, 50);
  1213. }
  1214. break;
  1215. case BUZZER_REG:
  1216. if( SystemManager.BuzzerType == BUZZER_NONE )
  1217. {
  1218. SystemManager.BuzzerType = 3;
  1219. SystemTimer.BUZZER_TIMER = 500;
  1220. app_pwm_enable(&PWM1);
  1221. app_pwm_channel_duty_set(&PWM1, 0, 50);
  1222. }
  1223. break;
  1224. case BUZZER_BOOT_LOADER:
  1225. if( SystemManager.BuzzerType == BUZZER_NONE )
  1226. {
  1227. SystemManager.BuzzerType = 4;
  1228. SystemTimer.BUZZER_TIMER = 1000;
  1229. app_pwm_enable(&PWM1);
  1230. app_pwm_channel_duty_set(&PWM1, 0, 50);
  1231. }
  1232. break;
  1233. case BUZZER_POWER_ON:
  1234. if( SystemManager.BuzzerType == BUZZER_NONE )
  1235. {
  1236. SystemManager.BuzzerType = BUZZER_POWER_ON;
  1237. SystemTimer.BUZZER_TIMER = 300;
  1238. app_pwm_enable(&PWM1);
  1239. app_pwm_channel_duty_set(&PWM1, 0, 50);
  1240. }
  1241. break;
  1242. }
  1243. }
  1244. // 송신기 EMG 신호를 전송
  1245. void SendEmgData(void)
  1246. {
  1247. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  1248. static uint8_t index = 0;
  1249. uint32_t err_code;
  1250. uint16_t length;
  1251. int i;
  1252. NRF_LOG_INFO("SendEmgData(%d)", SystemManager.Batt);
  1253. data_array[0] = COMMAND_TYPE;
  1254. data_array[1] = 0;
  1255. data_array[2] = 0;
  1256. data_array[3] = 0;
  1257. data_array[4] = 0;
  1258. data_array[5] = 1;
  1259. data_array[6] = 1;
  1260. data_array[7] = 1;
  1261. data_array[8] = 1;
  1262. data_array[9] = new_ble_addr.addr[3];
  1263. data_array[10] = new_ble_addr.addr[4];
  1264. data_array[11] = new_ble_addr.addr[5];
  1265. data_array[12] = COMMAND_EMG;
  1266. data_array[13] = SystemManager.Batt;
  1267. data_array[14] = VER_HIGH_VALUE;
  1268. data_array[15] = VER_LOW_VALUE;
  1269. data_array[16] = 0x3;
  1270. length = COMMAND_LENGTH;
  1271. WesEncryptEcb( &data_array[1] );
  1272. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  1273. for( i=0; i<17; i++)
  1274. {
  1275. NRF_LOG_RAW_INFO("%02x ", data_array[i]);
  1276. }
  1277. NRF_LOG_RAW_INFO("\n");
  1278. SystemManager.BleSendStatus = err_code;
  1279. if ((err_code != NRF_ERROR_RESOURCES)&&(err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_NOT_FOUND))// (or anything else you want to filter out...)
  1280. {
  1281. APP_ERROR_CHECK(err_code);
  1282. }
  1283. }
  1284. // 송신기 Test 신호를 전송
  1285. void SendTestData(void)
  1286. {
  1287. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  1288. static uint8_t index = 0;
  1289. uint32_t err_code;
  1290. uint16_t length;
  1291. NRF_LOG_INFO("SendTestData()");
  1292. data_array[0] = COMMAND_TYPE;
  1293. data_array[1] = 0;
  1294. data_array[2] = 0;
  1295. data_array[3] = 0;
  1296. data_array[4] = 0;
  1297. data_array[5] = 1;
  1298. data_array[6] = 1;
  1299. data_array[7] = 1;
  1300. data_array[8] = 1;
  1301. data_array[9] = new_ble_addr.addr[3];
  1302. data_array[10] = new_ble_addr.addr[4];
  1303. data_array[11] = new_ble_addr.addr[5];
  1304. data_array[12] = COMMAND_TEST;
  1305. data_array[13] = SystemManager.Batt;
  1306. data_array[14] = VER_HIGH_VALUE;
  1307. data_array[15] = VER_LOW_VALUE;
  1308. data_array[16] = 0x3;
  1309. length = COMMAND_LENGTH;
  1310. WesEncryptEcb( &data_array[1] );
  1311. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  1312. SystemManager.BleSendStatus = err_code;
  1313. if ((err_code != NRF_ERROR_RESOURCES)&&(err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_NOT_FOUND))// (or anything else you want to filter out...)
  1314. {
  1315. APP_ERROR_CHECK(err_code);
  1316. }
  1317. }
  1318. void SendAliveData(void)
  1319. {
  1320. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  1321. static uint8_t index = 0;
  1322. uint32_t err_code;
  1323. uint16_t length;
  1324. NRF_LOG_INFO("SendAliveData()");
  1325. data_array[0] = COMMAND_TYPE;
  1326. data_array[1] = 0;
  1327. data_array[2] = 0;
  1328. data_array[3] = 0;
  1329. data_array[4] = 0;
  1330. data_array[5] = 1;
  1331. data_array[6] = 1;
  1332. data_array[7] = 1;
  1333. data_array[8] = 1;
  1334. data_array[9] = new_ble_addr.addr[3];
  1335. data_array[10] = new_ble_addr.addr[4];
  1336. data_array[11] = new_ble_addr.addr[5];
  1337. data_array[12] = COMMAND_ALIVE;
  1338. data_array[13] = SystemManager.Batt;
  1339. data_array[14] = VER_HIGH_VALUE;
  1340. data_array[15] = VER_LOW_VALUE;
  1341. data_array[16] = 0x3;
  1342. length = COMMAND_LENGTH;
  1343. WesEncryptEcb( &data_array[1] );
  1344. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  1345. SystemManager.BleSendStatus = err_code;
  1346. if ((err_code != NRF_ERROR_RESOURCES)&&(err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_NOT_FOUND))// (or anything else you want to filter out...)
  1347. {
  1348. APP_ERROR_CHECK(err_code);
  1349. }
  1350. }
  1351. // 등록모드
  1352. void SendRegData(void)
  1353. {
  1354. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  1355. static uint8_t index = 0;
  1356. uint32_t err_code;
  1357. uint16_t length;
  1358. NRF_LOG_INFO("SendRegData()");
  1359. #if 0
  1360. if( SystemManager.BuzzerType == 0 )
  1361. {
  1362. SystemManager.BuzzerType = 3;
  1363. SystemTimer.BUZZER_TIMER = 500;
  1364. app_pwm_enable(&PWM1);
  1365. app_pwm_channel_duty_set(&PWM1, 0, 50);
  1366. }
  1367. #endif
  1368. data_array[0] = COMMAND_TYPE;
  1369. data_array[1] = 0;
  1370. data_array[2] = 0;
  1371. data_array[3] = 0;
  1372. data_array[4] = 0;
  1373. data_array[5] = 1;
  1374. data_array[6] = 1;
  1375. data_array[7] = 1;
  1376. data_array[8] = 1;
  1377. data_array[9] = new_ble_addr.addr[3];
  1378. data_array[10] = new_ble_addr.addr[4];
  1379. data_array[11] = new_ble_addr.addr[5];
  1380. data_array[12] = COMMAND_REG;
  1381. data_array[13] = SystemManager.Batt;
  1382. data_array[14] = VER_HIGH_VALUE;
  1383. data_array[15] = VER_LOW_VALUE;
  1384. data_array[16] = 0x3;
  1385. length = COMMAND_LENGTH;
  1386. WesEncryptEcb( &data_array[1] );
  1387. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  1388. SystemManager.BleSendStatus = err_code;
  1389. if ((err_code != NRF_ERROR_RESOURCES)&&(err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_NOT_FOUND))// (or anything else you want to filter out...)
  1390. {
  1391. APP_ERROR_CHECK(err_code);
  1392. // RegMacDelteAll();
  1393. // SaveReceiverMac(SystemManager.StrPeerAddr);
  1394. }else{
  1395. }
  1396. }
  1397. void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
  1398. {
  1399. ready_flag = true;
  1400. }
  1401. void pwmInit(void)
  1402. {
  1403. ret_code_t err_code;
  1404. app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(370L, 7); //1000L -> 1Khz, 500L -> 2Khz, 370L -> 2.7Khz
  1405. /* Switch the polarity of the second channel. */
  1406. //pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;
  1407. pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
  1408. /* Initialize and enable PWM. */
  1409. err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
  1410. APP_ERROR_CHECK(err_code);
  1411. }
  1412. #define SND_WDT_SLEEP_CONFIG \
  1413. { \
  1414. .behaviour = (nrf_wdt_behaviour_t)NRFX_WDT_CONFIG_BEHAVIOUR, \
  1415. .reload_value = SLEEP_WDT_LIMIT, \
  1416. NRFX_WDT_IRQ_CONFIG \
  1417. }
  1418. void WdtRunMode()
  1419. {
  1420. ret_code_t err_code;
  1421. uint64_t time = 10*60*60*1000;
  1422. uint64_t ticks = (time * 32768ULL) / 1000;
  1423. // NRFX_ASSERT(ticks <= UINT32_MAX);
  1424. NRF_LOG_INFO("===>> ticks is [%d]", ticks);
  1425. nrf_wdt_reload_value_set((uint32_t) ticks);
  1426. }
  1427. void WdtSleepMode()
  1428. {
  1429. uint32_t err_code;
  1430. nrf_drv_wdt_config_t config = SND_WDT_SLEEP_CONFIG;
  1431. err_code = nrf_drv_wdt_init(&config, wdt_event_handler);
  1432. APP_ERROR_CHECK(err_code);
  1433. err_code = nrf_drv_wdt_channel_alloc(&m_channel_id);
  1434. APP_ERROR_CHECK(err_code);
  1435. nrf_drv_wdt_enable();
  1436. }
  1437. void ReMac_Mapping()
  1438. {
  1439. ble_gap_addr_t dd;
  1440. dd.addr_id_peer = 0;
  1441. dd.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  1442. sd_ble_gap_addr_get(&old_ble_addr);
  1443. dd.addr[0] = IES200_1ST_MAC;
  1444. dd.addr[1] = IES200_2ST_MAC;
  1445. dd.addr[2] = IES200_3ST_MAC;
  1446. dd.addr[3] = old_ble_addr.addr[3];
  1447. dd.addr[4] = old_ble_addr.addr[4];
  1448. dd.addr[5] = old_ble_addr.addr[5];
  1449. sd_ble_gap_addr_set(&dd);
  1450. sd_ble_gap_addr_get(&new_ble_addr);
  1451. DEVICE_NAME[9] = dd.addr[3];
  1452. DEVICE_NAME[10] = dd.addr[4];
  1453. DEVICE_NAME[11] = dd.addr[5];
  1454. NRF_LOG_INFO("MAC Address : %02X %02X %02X", dd.addr[3], dd.addr[4], dd.addr[5]);
  1455. }
  1456. /**@brief Application main function.
  1457. */
  1458. int main(void)
  1459. {
  1460. uint32_t err_code = NRF_SUCCESS;
  1461. bool erase_bonds;
  1462. uint8_t flash_buf = 0x00;
  1463. NRF_LOG_RAW_INFO("\n===========================================================\n\n");
  1464. NRF_LOG_RAW_INFO("ICS-200B Program Start --- Ver 1.10 2022/03/24");
  1465. NRF_LOG_RAW_INFO("\n===========================================================\n\n");
  1466. // Initialize.
  1467. //uart_init();
  1468. SaadcInit();
  1469. log_init();
  1470. /* Register first to receive an event when initialization is complete. */
  1471. (void) fds_register(fds_evt_handler);
  1472. pwmInit();
  1473. err_code = fds_init();
  1474. APP_ERROR_CHECK(err_code);
  1475. timers_init();
  1476. HSDInInit();
  1477. SystemManager.GpReg = nrf_power_gpregret_get();
  1478. buttons_leds_init(&erase_bonds);
  1479. power_management_init();
  1480. ble_stack_init();
  1481. ReMac_Mapping();
  1482. SystemManager.ResetReason = NRF_POWER->RESETREAS;
  1483. SystemManager.BleSendStatus = -1;
  1484. gap_params_init();
  1485. gatt_init();
  1486. services_init();
  1487. advertising_init();
  1488. conn_params_init();
  1489. err_code = app_timer_create(&wakeup_timer_id, APP_TIMER_MODE_SINGLE_SHOT, wakeup_timer_handler);
  1490. APP_ERROR_CHECK(err_code);
  1491. err_code = app_timer_create(&m_ms_timer_id, APP_TIMER_MODE_REPEATED, timer_ms_timeout_handler);
  1492. APP_ERROR_CHECK(err_code);
  1493. //err_code = app_timer_start(m_ms_timer_id, TIMER_MS_INTERVAL, NULL);
  1494. //APP_ERROR_CHECK(err_code);
  1495. //err_code = app_timer_start(m_our_char_timer_id, OUR_CHAR_TIMER_INTERVAL, NULL);
  1496. //APP_ERROR_CHECK(err_code);
  1497. // Start execution.
  1498. //printf("\r\nUART started.\r\n");
  1499. NRF_LOG_INFO("Debug logging for UART over RTT started.");
  1500. //advertising_start();
  1501. ParkSysInit();
  1502. bsp_board_leds_off();
  1503. SystemManager.SendRepeat = 0;
  1504. SystemManager.SendRepeatTimerLimit = 30000; // on time
  1505. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  1506. APP_ERROR_CHECK(err_code);
  1507. if( SystemManager.GpReg == 0x00 )
  1508. {
  1509. err_code = app_timer_start(m_ms_timer_id, TIMER_MS_INTERVAL, NULL);
  1510. APP_ERROR_CHECK(err_code);
  1511. err_code = app_timer_start(m_our_char_timer_id, OUR_CHAR_TIMER_INTERVAL, NULL);
  1512. APP_ERROR_CHECK(err_code);
  1513. SystemManager.EmgSendButton = BUTTON_EMG;
  1514. SystemTimer.EMG_SEND_RUN = 1;
  1515. SystemTimer.SEND_TIMER = 0;
  1516. Buzzer_Select(BUZZER_POWER_ON);
  1517. for (;;)
  1518. {
  1519. idle_state_handle();
  1520. if( SystemManager.GoSleep == 1 )
  1521. {
  1522. app_timer_stop(m_ms_timer_id);
  1523. app_timer_stop(m_our_char_timer_id);
  1524. break;
  1525. }
  1526. }
  1527. nrf_power_gpregret_set(0x02);
  1528. }
  1529. #if 1
  1530. else if( SystemManager.GpReg == 0x04 )
  1531. {
  1532. err_code = app_timer_start(m_ms_timer_id, TIMER_MS_INTERVAL, NULL);
  1533. APP_ERROR_CHECK(err_code);
  1534. err_code = app_timer_start(m_our_char_timer_id, OUR_CHAR_TIMER_INTERVAL, NULL);
  1535. APP_ERROR_CHECK(err_code);
  1536. SystemManager.EmgSendButton = BUTTON_REG;
  1537. SystemTimer.EMG_SEND_RUN = 1;
  1538. SystemTimer.SEND_TIMER = 0;
  1539. Buzzer_Select(BUZZER_POWER_ON);
  1540. for (;;)
  1541. {
  1542. idle_state_handle();
  1543. if( SystemManager.GoSleep == 1 )
  1544. {
  1545. app_timer_stop(m_ms_timer_id);
  1546. app_timer_stop(m_our_char_timer_id);
  1547. break;
  1548. }
  1549. }
  1550. nrf_power_gpregret_set(0x02);
  1551. }
  1552. #endif
  1553. WdtSleepMode();
  1554. nrf_drv_wdt_channel_feed(m_channel_id);
  1555. // WdtRunMode();
  1556. app_timer_start(wakeup_timer_id, WAKEUP_TIME_INVERVAL, NULL); // Wake Up Timer Reset
  1557. err_code = sd_app_evt_wait(); // jakuja GOTO SLEEP MODE
  1558. APP_ERROR_CHECK(err_code);
  1559. // <---------------------------------------------------------------->
  1560. Battery_Adc_Read();
  1561. // Enter main loop.
  1562. for (;;)
  1563. {
  1564. SysCounter++;
  1565. idle_state_handle();
  1566. }
  1567. }
  1568. /**
  1569. * @}
  1570. */