main(3887).c 58 KB

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