main(870).c 59 KB

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