slave.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. #include "ble_comm.h"
  2. #include "ble_advertising.h"
  3. #include "ble_conn_params.h"
  4. #include "nrf_ble_qwr.h"
  5. #include "nrf_fstorage.h"
  6. #include "nrf_soc.h"
  7. #include "ble_nus.h"
  8. #include "bsp_time.h"
  9. #include "system.h"
  10. #include "app_flash.h"
  11. // <<< Use Configuration Wizard in Context Menu >>>\r\n
  12. #define APP_ADV_INTERVAL 320 /**< The advertising interval (in units of 0.625 ms). This value corresponds to 187.5 ms. */
  13. #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
  14. #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(1000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
  15. #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
  16. #define MAX_CONN_PARAMS_UPDATE_COUNT 1
  17. //static char DEVICE_NAME[TARFET_LEN_MAX] = "SH";
  18. #if USE_LADDR == 1
  19. char BleReallyName[TARFET_LEN_MAX] = {0};
  20. #endif
  21. #define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
  22. #define MAX_CONN_INTERVAL MSEC_TO_UNITS(1.25 * 1599, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
  23. #define SLAVE_LATENCY 0 /**< Slave latency. */
  24. #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
  25. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
  26. static ble_uuid_t m_adv_uuids[] =
  27. {
  28. {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
  29. static unsigned char connect_to_client = 0;
  30. static Ble_receive_handler_t Rec_h = NULL;
  31. BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);
  32. BLE_ADVERTISING_DEF(m_advertising);
  33. NRF_BLE_QWRS_DEF(m_qwr, NRF_SDH_BLE_TOTAL_LINK_COUNT);
  34. uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
  35. ble_gap_conn_params_t slave_conn_params = {0};
  36. static void nrf_qwr_error_handler(uint32_t nrf_error) //?óáDD′′í?ó2ù×÷
  37. {
  38. APP_ERROR_HANDLER(nrf_error);
  39. }
  40. //′ó BLE ?óêüêy?Y
  41. static void nus_data_handler(ble_nus_evt_t *p_evt)
  42. {
  43. if (p_evt->type == BLE_NUS_EVT_RX_DATA)
  44. {
  45. Rec_h((unsigned char *)(p_evt->params.rx_data.p_data), p_evt->params.rx_data.length);
  46. }
  47. }
  48. static void services_init(void) //·t??3?ê??ˉ
  49. {
  50. uint32_t err_code;
  51. ble_nus_init_t nus_init;
  52. nrf_ble_qwr_init_t qwr_init = {0};
  53. // Initialize Queued Write Module.
  54. qwr_init.error_handler = nrf_qwr_error_handler;
  55. for (uint32_t i = 0; i < NRF_SDH_BLE_TOTAL_LINK_COUNT; i++)
  56. {
  57. err_code = nrf_ble_qwr_init(&m_qwr[i], &qwr_init);
  58. APP_ERROR_CHECK(err_code);
  59. }
  60. // Initialize NUS.
  61. memset(&nus_init, 0, sizeof(nus_init));
  62. nus_init.data_handler = nus_data_handler;
  63. err_code = ble_nus_init(&m_nus, &nus_init);
  64. APP_ERROR_CHECK(err_code);
  65. }
  66. static void on_adv_evt(ble_adv_evt_t ble_adv_evt) //1?2¥ê??t
  67. {
  68. switch (ble_adv_evt)
  69. {
  70. case BLE_ADV_EVT_FAST:
  71. {
  72. BLE_PRINT("Fast advertising.\r\n");
  73. }
  74. break;
  75. case BLE_ADV_EVT_IDLE:
  76. {
  77. BLE_PRINT("on_adv_evt->BLE_ADV_EVT_IDLE\r\n");
  78. ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST); //?aê?1?2¥
  79. APP_ERROR_CHECK(err_code);
  80. }
  81. break;
  82. default:
  83. // No implementation needed.
  84. break;
  85. }
  86. }
  87. static void advertising_init(void)
  88. {
  89. uint32_t err_code;
  90. ble_advertising_init_t init;
  91. int8_t txpower = 4;
  92. memset(&init, 0, sizeof(init));
  93. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  94. init.advdata.include_appearance = false;
  95. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  96. init.advdata.p_tx_power_level = &txpower;
  97. init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  98. init.srdata.uuids_complete.p_uuids = m_adv_uuids;
  99. init.config.ble_adv_fast_enabled = true;
  100. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  101. init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
  102. init.evt_handler = on_adv_evt;
  103. err_code = ble_advertising_init(&m_advertising, &init);
  104. APP_ERROR_CHECK(err_code);
  105. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  106. }
  107. static void conn_params_error_handler(uint32_t nrf_error)
  108. {
  109. APP_ERROR_HANDLER(nrf_error);
  110. }
  111. static void conn_params_init(void)
  112. {
  113. ret_code_t err_code;
  114. ble_conn_params_init_t cp_init;
  115. memset(&cp_init, 0, sizeof(cp_init));
  116. cp_init.p_conn_params = NULL;
  117. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  118. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  119. cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
  120. cp_init.start_on_notify_cccd_handle = BLE_CONN_HANDLE_INVALID; // Start upon connection.
  121. cp_init.disconnect_on_fail = true;
  122. cp_init.evt_handler = NULL; // Ignore events.
  123. cp_init.error_handler = conn_params_error_handler;
  124. err_code = ble_conn_params_init(&cp_init);
  125. APP_ERROR_CHECK(err_code);
  126. }
  127. void advertising_start(void)
  128. {
  129. ret_code_t err_code;
  130. err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST); //同时开始广播
  131. DEBUG_LOG("advertising_start !\r\n");
  132. if(NRF_ERROR_INVALID_STATE != err_code){
  133. APP_ERROR_CHECK(err_code);
  134. }
  135. }
  136. void advertising_stop(void)
  137. {
  138. ret_code_t err_code;
  139. err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle); //停止广播
  140. DEBUG_LOG("advertising_stop !\r\n");
  141. if(NRF_ERROR_INVALID_STATE != err_code){
  142. APP_ERROR_CHECK(err_code);
  143. }
  144. }
  145. int wireless_pair_advertising_stop(void)
  146. {
  147. ret_code_t err_code;
  148. err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle); //停止广播
  149. //NRF_ERROR_INVALID_STATE:这很可能意味着当您尝试停止该设备时,该设备并未处于广告状态。
  150. //当调用sd_ble_gap_adv_stop时返回NRF_ERROR_INVALID_STATE下,继续调用ble_advertising_init,ble_advertising_init可能会报NRF_ERROR_INVALID_STATE错误。
  151. //已连接状态下,不能调用停止广播,不然会出现NRF_ERROR_INVALID_STATE
  152. if(err_code != NRF_SUCCESS)
  153. {
  154. // SEGGER_RTT_printf(0,"wireless_pair_advertising_stop:%d\r\n",err_code);
  155. return -1;
  156. }
  157. return 0;
  158. }
  159. int wireless_pair_advertising_start(int rsp_onoff)
  160. {
  161. ble_advdata_manuf_data_t id;
  162. ret_code_t err_code;
  163. ble_advertising_init_t init;
  164. int8_t txpower = 4;
  165. ble_gap_addr_t addr;
  166. advertising_stop();
  167. memset(&init, 0, sizeof(init));
  168. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  169. init.advdata.include_appearance = false;
  170. init.advdata.p_tx_power_level = &txpower;
  171. if(rsp_onoff == 0x01)
  172. {
  173. err_code = sd_ble_gap_addr_get(&addr);
  174. if(err_code != NRF_SUCCESS)return -1;
  175. id.data.p_data = addr.addr;
  176. id.data.size = BLE_GAP_ADDR_LEN;
  177. init.config.ble_adv_fast_timeout = 0;
  178. init.config.ble_adv_fast_interval = 32; //20ms,广播间隔设置范围:20ms ~ 10.24s
  179. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
  180. }
  181. else if(rsp_onoff == 0x02)
  182. {
  183. memset(&addr,0xFF,sizeof(addr));
  184. id.data.p_data = addr.addr;
  185. id.data.size = BLE_GAP_ADDR_LEN;
  186. init.config.ble_adv_fast_timeout = 0;
  187. init.config.ble_adv_fast_interval = 32;//20ms,广播间隔设置范围:20ms ~ 10.24s
  188. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
  189. }
  190. else
  191. {
  192. memset(&addr,0xFF,sizeof(addr));
  193. id.data.p_data = addr.addr;
  194. id.data.size = BLE_GAP_ADDR_LEN;
  195. init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
  196. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  197. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  198. }
  199. init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  200. init.srdata.uuids_complete.p_uuids = m_adv_uuids;
  201. init.srdata.p_manuf_specific_data = &id;
  202. init.config.ble_adv_fast_enabled = true;
  203. init.evt_handler = on_adv_evt;
  204. err_code = ble_advertising_init(&m_advertising, &init);
  205. if(err_code != NRF_SUCCESS)return -1;
  206. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  207. err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  208. if(err_code != NRF_SUCCESS)return -1;
  209. return 0;
  210. }
  211. bool ble_evt_is_advertising_timeout(ble_evt_t const *p_ble_evt)
  212. {
  213. return (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_SET_TERMINATED);
  214. }
  215. static void multi_qwr_conn_handle_assign(uint16_t conn_handle)
  216. {
  217. for (uint32_t i = 0; i < NRF_SDH_BLE_TOTAL_LINK_COUNT; i++)
  218. {
  219. if (m_qwr[i].conn_handle == BLE_CONN_HANDLE_INVALID)
  220. {
  221. ret_code_t err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr[i], conn_handle);
  222. APP_ERROR_CHECK(err_code);
  223. break;
  224. }
  225. }
  226. }
  227. #define slave_connected_evt_num_max 16
  228. static uint8_t slave_connected_evt_num = 0;
  229. static Ble_evt_cb ble_Slave_evt_cb[slave_connected_evt_num_max] = {0};
  230. int Ble_Slave_Connectd_Evt_Regist(Ble_evt_cb cb)
  231. {
  232. for (int i = 0; i < slave_connected_evt_num_max; i++)
  233. {
  234. if (ble_Slave_evt_cb[i] == cb)
  235. return -1;
  236. if (ble_Slave_evt_cb[i] == 0)
  237. {
  238. slave_connected_evt_num++;
  239. ble_Slave_evt_cb[i] = cb; //??μ÷oˉêy
  240. return 0;
  241. }
  242. }
  243. DEBUG_LOG( "ble_evt_Regist -> too many!\n");
  244. return -2;
  245. }
  246. void ble_slave_connected_evt_pcs(void)
  247. {
  248. for (int i = 0; i < slave_connected_evt_num; i++)
  249. { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  250. if (ble_Slave_evt_cb[i])
  251. {
  252. ble_Slave_evt_cb[i](); //??μ÷oˉêy
  253. }
  254. }
  255. }
  256. #define slave_disconn_evt_num_max 16
  257. static uint8_t slave_disconn_evt_num = 0;
  258. static Ble_evt_cb ble_Slave_disconn_evt_cb[slave_disconn_evt_num_max] = {0};
  259. int Ble_Slave_Disconn_Evt_Regist(Ble_evt_cb cb)
  260. {
  261. for (int i = 0; i < slave_disconn_evt_num_max; i++)
  262. {
  263. if (ble_Slave_disconn_evt_cb[i] == cb)
  264. return -1;
  265. if (ble_Slave_disconn_evt_cb[i] == 0)
  266. {
  267. slave_disconn_evt_num++;
  268. ble_Slave_disconn_evt_cb[i] = cb; //??μ÷oˉêy
  269. return 0;
  270. }
  271. }
  272. DEBUG_LOG( "Ble_Slave_Disconn_Evt_Regist -> too many!\r\n");
  273. return -2;
  274. }
  275. void ble_slave_dicconn_evt_pcs(void)
  276. {
  277. for (int i = 0; i < slave_disconn_evt_num; i++)
  278. { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
  279. if (ble_Slave_disconn_evt_cb[i])
  280. {
  281. ble_Slave_disconn_evt_cb[i](); //??μ÷oˉêy
  282. }
  283. }
  284. }
  285. unsigned char slave_update_conn_interval_request_sta = 0;
  286. static ble_gap_phys_t const phys =
  287. {
  288. .rx_phys = BLE_GAP_PHY_1MBPS,
  289. .tx_phys = BLE_GAP_PHY_1MBPS,
  290. };
  291. static uint8_t _7_5ms_intervalFlag =0;
  292. uint8_t Slave_Get7_5ms_interval(void){
  293. return _7_5ms_intervalFlag;
  294. }
  295. void on_ble_peripheral_evt(ble_evt_t const *p_ble_evt) //×÷?a′óéè±?μ?′|àí
  296. {
  297. ret_code_t err_code;
  298. ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt;
  299. switch (p_ble_evt->header.evt_id)
  300. {
  301. case BLE_GAP_EVT_CONNECTED:{
  302. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_CONNECTED\r\n");
  303. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  304. multi_qwr_conn_handle_assign(p_ble_evt->evt.gap_evt.conn_handle); //QWR句柄分配
  305. err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN,m_conn_handle,4);
  306. APP_ERROR_CHECK(err_code);
  307. connect_to_client = 1;
  308. ble_slave_connected_evt_pcs();
  309. #if 1
  310. BLE_PRINT("PHY update request.");
  311. err_code = sd_ble_gap_phy_update(p_gap_evt->conn_handle, &phys);
  312. APP_ERROR_CHECK(err_code);
  313. #endif
  314. BLE_PRINT("Connection 0x%x Received ble gap evt data length update request.", p_ble_evt->evt.gap_evt.conn_handle);
  315. ble_gap_data_length_params_t dlp =
  316. {
  317. .max_rx_time_us= BLE_GAP_DATA_LENGTH_AUTO,
  318. .max_tx_time_us= BLE_GAP_DATA_LENGTH_AUTO,
  319. .max_rx_octets = BLE_GAP_DATA_LENGTH_AUTO,
  320. .max_tx_octets = BLE_GAP_DATA_LENGTH_AUTO,
  321. };
  322. err_code = sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, &dlp, NULL);
  323. APP_ERROR_CHECK(err_code);
  324. sd_ble_gap_rssi_start(m_conn_handle, BLE_GAP_RSSI_THRESHOLD_INVALID, 0);
  325. }
  326. break;
  327. case BLE_GAP_EVT_DISCONNECTED:
  328. connect_to_client = 0;
  329. ble_slave_dicconn_evt_pcs();
  330. sd_ble_gap_rssi_stop(m_conn_handle);
  331. _7_5ms_intervalFlag =0;
  332. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DISCONNECTED,reason:%d\r\n",p_gap_evt->params.disconnected.reason);
  333. break;
  334. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  335. {
  336. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_PHY_UPDATE_REQUEST\r\n");
  337. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  338. APP_ERROR_CHECK(err_code);
  339. }
  340. break;
  341. case BLE_GATTC_EVT_TIMEOUT:
  342. // Disconnect on GATT Client timeout event.
  343. BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTC_EVT_TIMEOUT\r\n");
  344. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
  345. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  346. APP_ERROR_CHECK(err_code);
  347. break;
  348. case BLE_GATTS_EVT_TIMEOUT:
  349. // Disconnect on GATT Server timeout event.
  350. BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTS_EVT_TIMEOUT\r\n");
  351. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
  352. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  353. APP_ERROR_CHECK(err_code);
  354. break;
  355. case BLE_GAP_EVT_CONN_PARAM_UPDATE:
  356. {
  357. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE\r\n");
  358. slave_update_conn_interval_request_sta = 0;
  359. memcpy(&slave_conn_params, &p_gap_evt->params.conn_param_update_request.conn_params, sizeof(ble_gap_conn_params_t));
  360. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
  361. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
  362. BLE_PRINT("slave_latency : %d\r\n", p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
  363. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
  364. if(6 == p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval && 6 == p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval)
  365. _7_5ms_intervalFlag =1;
  366. else _7_5ms_intervalFlag =0;
  367. }BLE_PRINT("_7_5ms_intervalFlag : %d\r\n", _7_5ms_intervalFlag);
  368. break;
  369. case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
  370. {
  371. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST\r\n");
  372. ble_gap_conn_params_t params;
  373. params = p_gap_evt->params.conn_param_update_request.conn_params;
  374. err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle, &params);
  375. BLE_PRINT("=====>BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST error:%d\r\n",err_code);
  376. APP_ERROR_CHECK(err_code);
  377. memcpy(&slave_conn_params, &p_gap_evt->params.conn_param_update_request.conn_params, sizeof(ble_gap_conn_params_t));
  378. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
  379. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
  380. BLE_PRINT("slave_latency : %d\r\n", p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
  381. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
  382. } break;
  383. case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
  384. {
  385. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST\r\n");
  386. ble_gap_data_length_params_t const dlp =
  387. {
  388. .max_rx_octets = BLE_GAP_DATA_LENGTH_AUTO,
  389. .max_tx_octets = BLE_GAP_DATA_LENGTH_AUTO,
  390. };
  391. err_code = sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, &dlp, NULL);
  392. APP_ERROR_CHECK(err_code);
  393. }
  394. break;
  395. case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
  396. {
  397. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE\r\n");
  398. BLE_PRINT("max_rx_octets : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_rx_octets);
  399. BLE_PRINT("max_rx_time_us : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_rx_time_us);
  400. BLE_PRINT("max_tx_octets : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_tx_octets);
  401. BLE_PRINT("max_tx_time_us : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_tx_time_us);
  402. }
  403. break;
  404. case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST://?÷?ú?ò′ó?úéê??mtuê±μ?ê??t
  405. {
  406. sd_ble_gatts_exchange_mtu_reply(m_conn_handle, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  407. BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST -> client_rx_mtu=%d\r\n",p_ble_evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu);
  408. }break;
  409. }
  410. }
  411. static ble_gap_addr_t m_my_addr;
  412. void Get_MACaddr(unsigned char *mac)
  413. {
  414. mac[0]=m_my_addr.addr[5];
  415. mac[1]=m_my_addr.addr[4];
  416. mac[2]=m_my_addr.addr[3];
  417. mac[3]=m_my_addr.addr[2];
  418. mac[4]=m_my_addr.addr[1];
  419. mac[5]=m_my_addr.addr[0];
  420. }
  421. #if USE_LADDR == 1
  422. char set_adv_name = 0;
  423. #endif
  424. static void gap_params_init(void) //GAP3?ê??ˉ
  425. {
  426. uint32_t err_code;
  427. ble_gap_conn_params_t gap_conn_params;
  428. ble_gap_conn_sec_mode_t sec_mode;
  429. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  430. #if USE_LADDR == 1
  431. err_code = sd_ble_gap_addr_get(&m_my_addr);
  432. APP_ERROR_CHECK(err_code);
  433. if (set_adv_name == 0)
  434. {
  435. BLE_PRINT("MAC [ %02X %02X %02X %02X %02X %02X ]\r\n", m_my_addr.addr[0], m_my_addr.addr[1], m_my_addr.addr[2], m_my_addr.addr[3], m_my_addr.addr[4], m_my_addr.addr[5]);
  436. memset(BleReallyName,0,sizeof(BleReallyName));
  437. sprintf(BleReallyName, "%02X%02X%02X%02X%02X%02X", m_my_addr.addr[5], m_my_addr.addr[4], m_my_addr.addr[3], m_my_addr.addr[2], m_my_addr.addr[1], m_my_addr.addr[0]);
  438. err_code = sd_ble_gap_device_name_set(&sec_mode,
  439. (const uint8_t *)BleReallyName,
  440. strlen(BleReallyName));
  441. }
  442. else
  443. {
  444. err_code = sd_ble_gap_device_name_set(&sec_mode,
  445. (const uint8_t *)BleReallyName,
  446. strlen(BleReallyName));
  447. }
  448. BLE_PRINT(">>>>>>>name:%d,%s",set_adv_name,BleReallyName);
  449. #else
  450. err_code = sd_ble_gap_device_name_set(&sec_mode,
  451. (const uint8_t *)DEVICE_NAME,
  452. strlen(DEVICE_NAME));
  453. #endif
  454. APP_ERROR_CHECK(err_code);
  455. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  456. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  457. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  458. gap_conn_params.slave_latency = SLAVE_LATENCY;
  459. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  460. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  461. APP_ERROR_CHECK(err_code);
  462. // err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN,m_conn_handle,0);
  463. // APP_ERROR_CHECK(err_code);
  464. }
  465. #if USEFIFO
  466. RINGFRAME_DEF(sbc,ringframe_size_1024);
  467. static unsigned int TIME_GetTicks_ms;
  468. unsigned int send_bytes_client(unsigned char *bytes, uint16_t len)
  469. {
  470. unsigned short length = len;
  471. if (connect_to_client)
  472. {
  473. do
  474. {
  475. if(ringframe_in(&sbc,bytes,length)==0)return 0;
  476. }while(ringframe_throw(&sbc)==0);
  477. Process_SetHoldOn(send_bytes_client_pcs,1);
  478. TIME_GetTicks_ms=TIME_GetTicks();
  479. return 0;
  480. }
  481. else
  482. {
  483. BLE_PRINT("send_bytes_client error. connect_to_client=0\r\n");
  484. return 1;
  485. }
  486. } //作为从机时发送数据给主机
  487. void send_bytes_client_pcs(void)
  488. {
  489. unsigned char sbuff[256];
  490. unsigned char len=0;
  491. while(ringframe_peek(&sbc,sbuff,&len)==0)
  492. {
  493. unsigned short length = len;
  494. uint32_t flag = 0;
  495. flag = ble_nus_data_send(&m_nus, sbuff, &length, m_conn_handle);
  496. if(flag==0)ringframe_throw(&sbc);
  497. else
  498. {
  499. if((TIME_GetTicks()-TIME_GetTicks_ms>100)||(TIME_GetTicks_ms>TIME_GetTicks()))
  500. {
  501. Process_SetHoldOn(send_bytes_client_pcs,0);
  502. }
  503. return;
  504. }
  505. }
  506. Process_SetHoldOn(send_bytes_client_pcs,0);
  507. }
  508. #else
  509. unsigned int send_bytes_client(unsigned char *bytes, uint16_t len)
  510. {
  511. unsigned int rev=0;
  512. unsigned short length = len;
  513. if (connect_to_client){
  514. rev=ble_nus_data_send(&m_nus, bytes, &length, m_conn_handle);
  515. return rev;
  516. }
  517. else{
  518. BLE_PRINT("send_bytes_client error. connect_to_client=0\r\n");
  519. return 1;
  520. }
  521. } //作为从机时发送数据给主机
  522. void send_bytes_client_pcs(void)
  523. {
  524. }
  525. #endif
  526. extern void timer_init(void);
  527. extern void power_management_init(void);
  528. extern void ble_stack_init(void);
  529. extern void gatt_init(void);
  530. extern char ble_stack_init_sta;
  531. extern uint8_t app_Get_isHost(void);
  532. #if USEMACNAME && USE_LADDR != 1
  533. ble_gap_addr_t mAddr;
  534. #endif
  535. void slave_init(Ble_receive_handler_t receive_handler)
  536. {
  537. static unsigned char init = 1;
  538. if (init)
  539. {
  540. if (receive_handler == NULL)
  541. {
  542. BLE_PRINT("slave_init -> param err \r\n");
  543. return;
  544. }
  545. Rec_h = receive_handler;
  546. if (ble_stack_init_sta)
  547. {
  548. timer_init(); //
  549. power_management_init(); //
  550. ble_stack_init(); //
  551. gatt_init(); //
  552. ble_stack_init_sta = 0;
  553. }
  554. #if USEMACNAME && USE_LADDR != 1
  555. if (!app_Get_isHost())
  556. {
  557. sd_ble_gap_addr_get(&mAddr);
  558. memset(DEVICE_NAME, 0, TARFET_LEN_MAX);
  559. sprintf(DEVICE_NAME, "%02X%02X%02X%02X%02X%02X", mAddr.addr[5], mAddr.addr[4], mAddr.addr[3], mAddr.addr[2], mAddr.addr[1], mAddr.addr[0]);
  560. }
  561. #endif
  562. gap_params_init();
  563. services_init();
  564. advertising_init();
  565. conn_params_init();
  566. advertising_start();
  567. init = 0;
  568. #if USE_LADDR
  569. BLE_PRINT("slave_init -> name [ %s ] \r\n", BleReallyName);
  570. #else
  571. BLE_PRINT("slave_init -> name [ %s ] \r\n", DEVICE_NAME);
  572. #endif
  573. }
  574. else
  575. {
  576. BLE_PRINT("slave_init -> err slave has init done \r\n");
  577. }
  578. }
  579. unsigned char slave_isconnect(void)
  580. {
  581. return connect_to_client;
  582. }
  583. unsigned int slave_set_adv_name(char *name, int len)
  584. {
  585. #if USE_LADDR == 1
  586. if (len > TARFET_LEN_MAX)
  587. return APP_ERR_OVERLENGTH;
  588. set_adv_name = 1;
  589. memset(BleReallyName, 0, TARFET_LEN_MAX);
  590. memcpy(BleReallyName, name, len);
  591. #else
  592. if (len > TARFET_LEN_MAX)
  593. return APP_ERR_OVERLENGTH;
  594. memset(DEVICE_NAME, 0, TARFET_LEN_MAX);
  595. memcpy(DEVICE_NAME, name, len);
  596. #endif
  597. return APP_SUCCESS;
  598. }
  599. void slave_get_advname_len(int *len)
  600. {
  601. *len = strlen(BleReallyName);
  602. }
  603. void slave_get_advname(char *name, int len)
  604. {
  605. memcpy(name,BleReallyName,len);
  606. }
  607. void slave_disconnect(void)
  608. {
  609. if (connect_to_client)
  610. sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  611. }
  612. unsigned int slave_update_conn_interval_request(float min_conn_interval, float max_conn_interval)
  613. {
  614. ret_code_t err_code;
  615. ble_gap_conn_params_t bgcp;
  616. if (slave_update_conn_interval_request_sta)
  617. return APP_ERR_BUSY;
  618. if (connect_to_client)
  619. {
  620. slave_update_conn_interval_request_sta = 1;
  621. if ((max_conn_interval > 1.25 * 1599) || (max_conn_interval < min_conn_interval))
  622. return APP_ERR_PARAMERR;
  623. if (min_conn_interval < 7.5f)
  624. return APP_ERR_PARAMERR;
  625. bgcp.max_conn_interval = MSEC_TO_UNITS(max_conn_interval, UNIT_1_25_MS);
  626. bgcp.min_conn_interval = MSEC_TO_UNITS(min_conn_interval, UNIT_1_25_MS);
  627. bgcp.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS);
  628. bgcp.slave_latency = 0;
  629. BLE_PRINT("slave_update_conn_interval_request -> %d \r\n", bgcp.max_conn_interval);
  630. err_code = sd_ble_gap_conn_param_update(m_conn_handle, &bgcp);
  631. APP_ERROR_CHECK(err_code);
  632. return err_code;
  633. }
  634. else
  635. {
  636. return APP_ERR_DISCONN;
  637. }
  638. }
  639. void slave_get_conn_params(ble_gap_conn_params_t *p)
  640. {
  641. p->conn_sup_timeout = slave_conn_params.conn_sup_timeout;
  642. p->max_conn_interval = slave_conn_params.max_conn_interval;
  643. p->min_conn_interval = slave_conn_params.min_conn_interval;
  644. p->slave_latency = slave_conn_params.slave_latency;
  645. }
  646. void slave_adv_init(void)
  647. {
  648. gap_params_init(); //ìí?óμ?GAP3?ê??ˉ
  649. conn_params_init(); //ìí?óμ?á??ó2?êy3?ê??ˉ
  650. advertising_init(); //ìí?ó1?2¥3?ê??ˉ
  651. }
  652. static signed char rssi = 0;
  653. signed char slave_get_rssi(void)
  654. {
  655. unsigned char channel;
  656. if (connect_to_client == 0)
  657. return 1;
  658. sd_ble_gap_rssi_get(m_conn_handle, &rssi, &channel);
  659. // BLE_PRINT("rssi= %d channel=%d\r\n", rssi, channel);
  660. return rssi;
  661. }
  662. #if DEBUGBLE
  663. #define led 13
  664. #define tx 11 //1.1
  665. #define rx 12
  666. //#define LS -1611916254 //?a·¢°?
  667. //#define RS -889050188
  668. //#define LS 97376119 //31
  669. //#define RS 627878688 //32
  670. #define LS -1087551583 //1.1
  671. #define RS -957332282 //1.1
  672. #define PS -1372482754 //usb
  673. unsigned char buff[255];
  674. char start = 0;
  675. void host_r(unsigned char *p, int len)
  676. {
  677. BLE_PRINT("hr : %d,0x%x\r\n", len, p[0]);
  678. if (p[0] == 0xbb)
  679. {
  680. BLE_PRINT("hr -------------: 0xbb\r\n");
  681. SEGGER_RTT_Write(0, &p[1], len);
  682. }
  683. if (p[0] == 0xcc)
  684. {
  685. BLE_PRINT("hr -------------: 0xcc\r\n");
  686. }
  687. }
  688. #define TIMER_TICK 25
  689. #define TCUN 1000
  690. unsigned short cun = 0;
  691. unsigned short ts = 0;
  692. unsigned short rec[5] = {0};
  693. unsigned short recrtc[5] = {0};
  694. unsigned int rtc_cun = 0;
  695. void slave_r(unsigned char *p, int len)
  696. {
  697. if (p[0] == 0xaa)
  698. {
  699. cun++;
  700. ts = p[1];
  701. ts = ts << 8;
  702. ts += p[2];
  703. if (ts >= 1)
  704. {
  705. start = 1;
  706. rtc_cun = NRF_RTC2->COUNTER;
  707. }
  708. if (ts == TCUN)
  709. start = 0;
  710. if (start)
  711. {
  712. if (NRF_RTC2->COUNTER - rtc_cun < 1 * TIMER_TICK)
  713. recrtc[0]++;
  714. if ((NRF_RTC2->COUNTER - rtc_cun >= 1 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 2 * TIMER_TICK))
  715. recrtc[1]++;
  716. if ((NRF_RTC2->COUNTER - rtc_cun >= 2 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 3 * TIMER_TICK))
  717. recrtc[2]++;
  718. if ((NRF_RTC2->COUNTER - rtc_cun >= 3 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 4 * TIMER_TICK))
  719. recrtc[3]++;
  720. if (NRF_RTC2->COUNTER - rtc_cun > 4 * TIMER_TICK)
  721. recrtc[4]++;
  722. rtc_cun = NRF_RTC2->COUNTER;
  723. }
  724. BLE_PRINT("sr : %d\r\n", ts);
  725. }
  726. if (p[0] == 0xbb)
  727. {
  728. buff[0] = 0xbb;
  729. int leng = sprintf(((char *)&buff[1]), "0 :%d,%d\r\n1 :%d,%d\r\n2 :%d,%d\r\n3 :%d,%d\r\n4 :%d,%d\r\n", rec[0], recrtc[0], rec[1], recrtc[1], rec[2], recrtc[2], rec[3], recrtc[3], rec[4], recrtc[4]);
  730. send_bytes_server(buff, leng);
  731. }
  732. if (p[0] == 0xcc)
  733. {
  734. BLE_PRINT("sr -------------: 0xcc\r\n");
  735. memset(rec, 0, 10);
  736. memset(recrtc, 0, 10);
  737. send_bytes_server(p, 3);
  738. }
  739. }
  740. #include "cli.h"
  741. nrf_radio_request_t radio_request_p;
  742. APP_TIMER_DEF(s_Timer);
  743. #define TEST_PERIOD APP_TIMER_TICKS(TIMER_TICK)
  744. unsigned short tims = 0;
  745. unsigned short stp = 0;
  746. void s_TimerCallback(void *arg)
  747. {
  748. if ((tims > 0) && (tims <= TCUN))
  749. {
  750. buff[0] = 0xaa;
  751. buff[1] = tims >> 8;
  752. buff[2] = tims;
  753. send_bytes_client(buff, 100);
  754. BLE_PRINT("send : %d\r\n", tims);
  755. tims++;
  756. }
  757. if (start)
  758. {
  759. if (cun > 4)
  760. cun = 4;
  761. rec[cun]++;
  762. cun = 0;
  763. }
  764. //·¢êy?Y??ê??ú
  765. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  766. {
  767. if (start)
  768. {
  769. buff[0] = 0xaa;
  770. buff[1] = stp >> 8;
  771. buff[2] = stp;
  772. send_bytes_client(buff, 100);
  773. }
  774. stp++;
  775. }
  776. // nrf_gpio_pin_toggle(rx);
  777. //nrf_gpio_pin_write(rx, 0);
  778. // BLE_PRINT("error= %d\r\n", sd_radio_request(&radio_request_p));
  779. }
  780. void Radio_State(void)
  781. {
  782. switch (NRF_RADIO->STATE)
  783. {
  784. case RADIO_STATE_STATE_Disabled:
  785. BLE_PRINT("RADIO_STATE_STATE_Disabled\r\n");
  786. break;
  787. case RADIO_STATE_STATE_RxRu:
  788. BLE_PRINT("RADIO_STATE_STATE_RxRu\r\n");
  789. break;
  790. case RADIO_STATE_STATE_RxIdle:
  791. BLE_PRINT("RADIO_STATE_STATE_RxIdle\r\n");
  792. break;
  793. case RADIO_STATE_STATE_Rx:
  794. BLE_PRINT("RADIO_STATE_STATE_Rx\r\n");
  795. break;
  796. case RADIO_STATE_STATE_RxDisable:
  797. BLE_PRINT("RADIO_STATE_STATE_RxDisable\r\n");
  798. break;
  799. case RADIO_STATE_STATE_TxRu:
  800. BLE_PRINT("RADIO_STATE_STATE_TxRu\r\n");
  801. break;
  802. case RADIO_STATE_STATE_TxIdle:
  803. BLE_PRINT("RADIO_STATE_STATE_TxIdle\r\n");
  804. break;
  805. case RADIO_STATE_STATE_Tx:
  806. BLE_PRINT("RADIO_STATE_STATE_Tx\r\n");
  807. break;
  808. case RADIO_STATE_STATE_TxDisable:
  809. BLE_PRINT("RADIO_STATE_STATE_TxDisable\r\n");
  810. break;
  811. }
  812. }
  813. void unoioo(void)
  814. {
  815. Ble_update_conn_interval(7.5,7.5);
  816. }
  817. void unoioo_s(void)
  818. {
  819. slave_update_conn_interval_request(30, 30);
  820. scan_start();
  821. }
  822. void unoioo_s_d(void)
  823. {
  824. host_disconnect();
  825. scan_start();
  826. }
  827. void rtc_config(void)
  828. {
  829. NRF_RTC2->PRESCALER = 0; //??ò?oá????êy?÷?ó1,1024us
  830. NRF_RTC2->TASKS_START = 1;
  831. }
  832. #include "nrf_drv_timer.h"
  833. void radio_evt_conf(void);
  834. const nrf_drv_timer_t TIMER_RADIO = NRF_DRV_TIMER_INSTANCE(2);
  835. void timer_led_event_handler(nrf_timer_event_t event_type, void *p_context)
  836. {
  837. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  838. {
  839. switch (event_type)
  840. {
  841. case NRF_TIMER_EVENT_COMPARE0: //320
  842. sd_radio_request(&radio_request_p);
  843. NRF_PPI->CHEN &= (~(PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos));
  844. break;
  845. case NRF_TIMER_EVENT_COMPARE1: //324
  846. sd_radio_request(&radio_request_p);
  847. break;
  848. case NRF_TIMER_EVENT_COMPARE2: //328
  849. sd_radio_request(&radio_request_p);
  850. break;
  851. case NRF_TIMER_EVENT_COMPARE3: //332
  852. NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
  853. break;
  854. default:
  855. //Do nothing.
  856. break;
  857. }
  858. }
  859. if (*NRF_FICR->DEVICEID == RS) //óò±?D?
  860. {
  861. switch (event_type)
  862. {
  863. case NRF_TIMER_EVENT_COMPARE0: //320
  864. nrf_gpio_pin_write(tx, 1);
  865. break;
  866. case NRF_TIMER_EVENT_COMPARE1: //324
  867. nrf_gpio_pin_write(tx, 0);
  868. break;
  869. case NRF_TIMER_EVENT_COMPARE2: //328
  870. break;
  871. case NRF_TIMER_EVENT_COMPARE3: //332
  872. break;
  873. default:
  874. //Do nothing.
  875. break;
  876. }
  877. }
  878. }
  879. void timer_config(void)
  880. {
  881. uint32_t time_us = 5000; //Time(in miliseconds) between consecutive compare events.
  882. uint32_t time_ticks;
  883. uint32_t err_code = NRF_SUCCESS;
  884. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  885. err_code = nrf_drv_timer_init(&TIMER_RADIO, &timer_cfg, timer_led_event_handler);
  886. APP_ERROR_CHECK(err_code);
  887. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  888. {
  889. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
  890. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
  891. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 10000);
  892. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, 0, true);
  893. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 20000);
  894. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL2, time_ticks, 0, true);
  895. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, 29000);
  896. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL3, time_ticks, NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK, true);
  897. }
  898. if (*NRF_FICR->DEVICEID == RS) //óò±?D?
  899. {
  900. time_us = 1000;
  901. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
  902. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
  903. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 9000 + 1);
  904. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
  905. nrf_drv_timer_enable(&TIMER_RADIO);
  906. }
  907. // nrf_drv_timer_enable(&TIMER_RADIO);
  908. }
  909. void ppi_set(void)
  910. {
  911. NRF_PPI->CH[0].EEP = (unsigned int)(&NRF_TIMER0->EVENTS_COMPARE[0]);
  912. NRF_PPI->CH[0].TEP = (unsigned int)(&NRF_TIMER2->TASKS_START);
  913. NRF_PPI->CH[1].EEP = (unsigned int)(&NRF_TIMER2->EVENTS_COMPARE[3]);
  914. NRF_PPI->CH[1].TEP = (unsigned int)(&NRF_TIMER2->TASKS_SHUTDOWN);
  915. NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) |
  916. (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
  917. }
  918. extern void USR_Init(void);
  919. extern void USR_Process(void);
  920. extern void TIME_Init(void);
  921. extern char Target_scan[];
  922. unsigned char txbuff[300] = {0x0a, 0x03, 0, 0, 2, 3, 4, 5, 6, 0, 8, 9};
  923. unsigned char rxbuff[300] = {0};
  924. void radio_init_R(void)
  925. {
  926. NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
  927. /* Start 16 MHz crystal oscillator */
  928. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  929. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  930. /* Wait for the external oscillator to start up */
  931. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  932. {
  933. // Do nothing.
  934. }
  935. // Radio config
  936. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  937. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  938. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  939. NRF_RADIO->PREFIX0 = 0xC3438303;
  940. NRF_RADIO->PREFIX1 = 0xE3630023;
  941. NRF_RADIO->BASE0 = 0x80C4A2E6;
  942. NRF_RADIO->BASE1 = 0x91D5B3F7;
  943. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  944. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  945. NRF_RADIO->PCNF0 = 0X00030006;
  946. NRF_RADIO->PCNF1 = 0X01040020;
  947. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  948. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  949. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
  950. NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
  951. }
  952. #include "nrf_drv_rtc.h"
  953. const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
  954. unsigned int countevt = 0;
  955. void radio_connect(void)
  956. {
  957. NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
  958. countevt = 1;
  959. nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + countevt * 0.009 * 32768, true);
  960. countevt++;
  961. }
  962. void RADIO_IRQHandler(void)
  963. {
  964. if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk))
  965. {
  966. NRF_RADIO->EVENTS_READY = 0U;
  967. BLE_PRINT("a");
  968. }
  969. if (NRF_RADIO->EVENTS_ADDRESS && (NRF_RADIO->INTENSET & RADIO_INTENSET_ADDRESS_Msk))
  970. {
  971. NRF_RADIO->EVENTS_ADDRESS = 0U;
  972. BLE_PRINT("b");
  973. }
  974. if (NRF_RADIO->EVENTS_PAYLOAD && (NRF_RADIO->INTENSET & RADIO_INTENSET_PAYLOAD_Msk))
  975. {
  976. NRF_RADIO->EVENTS_PAYLOAD = 0U;
  977. BLE_PRINT("c");
  978. }
  979. if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
  980. {
  981. NRF_RADIO->EVENTS_END = 0U;
  982. // NRF_LOG_INFO("d");
  983. if (NRF_RADIO->STATE >= 5UL)
  984. {
  985. NRF_RADIO->EVENTS_DISABLED = 0U;
  986. NRF_RADIO->TASKS_DISABLE = 1U;
  987. nrf_gpio_pin_write(tx, 0);
  988. // BLE_PRINT("Tx end\r\n");
  989. }
  990. else
  991. {
  992. //ê?μ?êy?Yoó?è?D???a·¢?í?£ê?
  993. NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
  994. NRF_RADIO->PACKETPTR = (unsigned int)txbuff;
  995. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
  996. nrf_gpio_pin_write(tx, 0);
  997. NRF_RADIO->EVENTS_DISABLED = 0U;
  998. NRF_RADIO->TASKS_DISABLE = 1U;
  999. while (NRF_RADIO->EVENTS_DISABLED == 0)
  1000. ;
  1001. NRF_RADIO->TASKS_TXEN = 1;
  1002. nrf_gpio_pin_write(tx, 1);
  1003. nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + 0.010 * 32768, true);
  1004. nrf_drv_rtc_cc_set(&rtc, 1, NRF_RTC0->CC[2] + 0.018 * 32768, true);
  1005. for (int i = 0; i < 50; i++)
  1006. {
  1007. BLE_PRINT("%x", rxbuff[i]);
  1008. }
  1009. BLE_PRINT("Rx\r\n", rxbuff[1]);
  1010. }
  1011. }
  1012. if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
  1013. {
  1014. NRF_RADIO->EVENTS_DISABLED = 0U;
  1015. BLE_PRINT("e");
  1016. }
  1017. if (NRF_RADIO->EVENTS_DEVMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMATCH_Msk))
  1018. {
  1019. NRF_RADIO->EVENTS_DEVMATCH = 0U;
  1020. BLE_PRINT("f");
  1021. }
  1022. if (NRF_RADIO->EVENTS_DEVMISS && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMISS_Msk))
  1023. {
  1024. NRF_RADIO->EVENTS_DEVMISS = 0U;
  1025. BLE_PRINT("g");
  1026. }
  1027. if (NRF_RADIO->EVENTS_RSSIEND && (NRF_RADIO->INTENSET & RADIO_INTENSET_RSSIEND_Msk))
  1028. {
  1029. NRF_RADIO->EVENTS_RSSIEND = 0U;
  1030. BLE_PRINT("h");
  1031. }
  1032. if (NRF_RADIO->EVENTS_BCMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_BCMATCH_Msk))
  1033. {
  1034. NRF_RADIO->EVENTS_BCMATCH = 0U;
  1035. BLE_PRINT("i");
  1036. }
  1037. if (NRF_RADIO->EVENTS_CRCOK && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCOK_Msk))
  1038. {
  1039. NRF_RADIO->EVENTS_CRCOK = 0U;
  1040. BLE_PRINT("k");
  1041. }
  1042. if (NRF_RADIO->EVENTS_CRCERROR && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCERROR_Msk))
  1043. {
  1044. NRF_RADIO->EVENTS_CRCERROR = 0U;
  1045. BLE_PRINT("l");
  1046. }
  1047. NVIC_ClearPendingIRQ(RADIO_IRQn);
  1048. }
  1049. void radio_scan_start(void)
  1050. {
  1051. NRF_RADIO->SHORTS = 0;
  1052. NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_RXEN_Msk;
  1053. NRF_RADIO->SHORTS |= RADIO_SHORTS_READY_START_Msk;
  1054. NRF_RADIO->SHORTS |= RADIO_SHORTS_END_START_Msk;
  1055. NRF_RADIO->INTENSET |= RADIO_INTENSET_END_Msk;
  1056. NRF_RADIO->TASKS_RXEN = 1;
  1057. NRF_RADIO->EVENTS_READY = 0;
  1058. while (NRF_RADIO->EVENTS_READY == 0)
  1059. {
  1060. }
  1061. NRF_RADIO->TASKS_START = 1;
  1062. NVIC_EnableIRQ(RADIO_IRQn);
  1063. Radio_State();
  1064. }
  1065. static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
  1066. {
  1067. switch (int_type)
  1068. {
  1069. case NRFX_RTC_INT_COMPARE0:
  1070. nrf_gpio_pin_write(tx, 1);
  1071. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
  1072. NRF_RADIO->PACKETPTR = (unsigned int)rxbuff;
  1073. NRF_RADIO->TASKS_RXEN = 1U;
  1074. break;
  1075. case NRFX_RTC_INT_COMPARE1:
  1076. Radio_State();
  1077. BLE_PRINT("NRFX_RTC_INT_COMPARE1\r\n");
  1078. break;
  1079. case NRFX_RTC_INT_COMPARE2:
  1080. break;
  1081. case NRFX_RTC_INT_COMPARE3:
  1082. break;
  1083. case NRFX_RTC_INT_TICK:
  1084. break;
  1085. case NRFX_RTC_INT_OVERFLOW:
  1086. nrf_drv_rtc_counter_clear(&rtc);
  1087. break;
  1088. }
  1089. }
  1090. /**********************************************************
  1091. * oˉêy??×?£ortc_config
  1092. * oˉêy×÷ó?£ortc?y?ˉ3?ê??ˉoíéè??
  1093. * oˉêy2?êy£o?T
  1094. * oˉêy·μ???μ£o?T
  1095. ***********************************************************/
  1096. void radio_rtc_config(void)
  1097. {
  1098. uint32_t err_code;
  1099. NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
  1100. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  1101. NRF_CLOCK->TASKS_LFCLKSTART = 1;
  1102. while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
  1103. {
  1104. // Do nothing.
  1105. }
  1106. //Initialize RTC instance
  1107. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  1108. config.prescaler = 0; //4095;????????=32768/(config.prescaler+1)Hz;
  1109. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
  1110. APP_ERROR_CHECK(err_code);
  1111. //Enable tick event & interrupt
  1112. // nrf_drv_rtc_tick_enable(&rtc, true);
  1113. //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
  1114. // err_code = nrf_drv_rtc_cc_set(&rtc, 0, 8, true);
  1115. // APP_ERROR_CHECK(err_code);
  1116. //Power on RTC instance
  1117. nrf_drv_rtc_enable(&rtc);
  1118. }
  1119. int main(void)
  1120. {
  1121. unsigned int error = 0;
  1122. unsigned int rtctemp = 0;
  1123. unsigned int start = 0;
  1124. unsigned int radio_dis_cun = 0;
  1125. unsigned int radio_dis_cun_rtc = 0;
  1126. nrf_gpio_cfg_output(led);
  1127. nrf_gpio_pin_write(led, 1);
  1128. nrf_gpio_cfg_output(tx);
  1129. nrf_gpio_pin_write(tx, 0);
  1130. nrf_gpio_cfg_output(8);
  1131. nrf_gpio_pin_write(8, 0);
  1132. nrf_gpio_cfg_output(rx);
  1133. nrf_gpio_pin_write(rx, 0);
  1134. BLE_PRINT("NRF_FICR->DEVICEID : %d\r\n", *NRF_FICR->DEVICEID);
  1135. if (*NRF_FICR->DEVICEID == RS) //óò±?D?
  1136. {
  1137. #if 1
  1138. slave_init(host_r);
  1139. #else
  1140. radio_init_R();
  1141. radio_rtc_config();
  1142. radio_scan_start();
  1143. #endif
  1144. BLE_PRINT("you \r\n");
  1145. }
  1146. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  1147. {
  1148. #if 0
  1149. Target_scan[0]=0xe3; //3132
  1150. Target_scan[1]=0x3f;
  1151. Target_scan[2]=0xd9;
  1152. Target_scan[3]=0x0d;
  1153. Target_scan[4]=0x0e;
  1154. Target_scan[5]=0xc6;
  1155. sscanf("A1 A3 9D 04 E9 F4","%hhx %hhx %hhx %hhx %hhx %hhx",&Target_scan[0],&Target_scan[1],&Target_scan[2],&Target_scan[3],&Target_scan[4],&Target_scan[5]);
  1156. // Target_scan[0]=0x3C; //?a·¢°?
  1157. // Target_scan[1]=0x83;
  1158. // Target_scan[2]=0xCF;
  1159. // Target_scan[3]=0x49;
  1160. // Target_scan[4]=0x50;
  1161. // Target_scan[5]=0xE1;
  1162. //
  1163. #endif
  1164. Ble_Host_Connectd_Evt_Regist(unoioo);
  1165. Ble_Slave_Connectd_Evt_Regist(unoioo_s);
  1166. Ble_Slave_Disconn_Evt_Regist(unoioo_s_d);
  1167. // extern void radio_request_earliest(void);
  1168. // Ble_Slave_Connectd_Evt_Regist(radio_request_earliest);
  1169. slave_init(host_r);
  1170. host_init(slave_r);
  1171. // timer_config();
  1172. BLE_PRINT("zuo \r\n");
  1173. }
  1174. if (*NRF_FICR->DEVICEID == PS) //ê??ú
  1175. {
  1176. #if 0
  1177. Target_scan[0] = 0x21;
  1178. Target_scan[1] = 0x8a;
  1179. Target_scan[2] = 0x4f;
  1180. Target_scan[3] = 0x61;
  1181. Target_scan[4] = 0xcb;
  1182. Target_scan[5] = 0xe8;
  1183. #endif
  1184. host_set_scan_name("SH_13EC", 7);
  1185. BLE_PRINT("shou \r\n");
  1186. host_init(slave_r);
  1187. scan_start();
  1188. }
  1189. rtc_config();
  1190. for (int i = 1; i < 200; i++)
  1191. {
  1192. buff[i] = i + 0x30;
  1193. // txbuff[i]=i;
  1194. }
  1195. app_timer_create(&s_Timer, APP_TIMER_MODE_REPEATED, s_TimerCallback);
  1196. app_timer_start(s_Timer, TEST_PERIOD, NULL);
  1197. // ppi_set();
  1198. while (1)
  1199. {
  1200. cli_process(&clirtt);
  1201. if (NRF_SUCCESS == sd_evt_get(&error))
  1202. {
  1203. BLE_PRINT("shou \r\n");
  1204. }
  1205. // if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  1206. {
  1207. if (NRF_RADIO->STATE == RADIO_STATE_STATE_Disabled)
  1208. {
  1209. nrf_gpio_pin_write(tx, 0);
  1210. }
  1211. else
  1212. {
  1213. nrf_gpio_pin_write(tx, 1);
  1214. }
  1215. }
  1216. }
  1217. }
  1218. void host_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1219. {
  1220. host_init(slave_r);
  1221. }
  1222. CLI_CMD_REGISTER(host_init, "clear sereen", host_init_pcs);
  1223. void hsb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1224. {
  1225. send_bytes_server(buff, 200);
  1226. }
  1227. CLI_CMD_REGISTER(hsb, "clear sereen", hsb_pcs);
  1228. void send_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1229. {
  1230. tims = 1;
  1231. }
  1232. CLI_CMD_REGISTER(send, "clear sereen", send_pcs);
  1233. void scc_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1234. {
  1235. buff[0] = 0xcc;
  1236. send_bytes_client(buff, 6);
  1237. }
  1238. CLI_CMD_REGISTER(scc, "clear sereen", scc_pcs);
  1239. void sbb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1240. {
  1241. buff[0] = 0xbb;
  1242. send_bytes_client(buff, 6);
  1243. }
  1244. CLI_CMD_REGISTER(sbb, "clear sereen", sbb_pcs);
  1245. void hcb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1246. {
  1247. send_bytes_client(buff, 200);
  1248. }
  1249. CLI_CMD_REGISTER(hcb, "clear sereen", hcb_pcs);
  1250. void slave_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1251. {
  1252. slave_init(host_r);
  1253. }
  1254. CLI_CMD_REGISTER(slave_init, "clear sereen", slave_init_pcs);
  1255. void bleupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1256. {
  1257. unsigned int error = 0;
  1258. error = Ble_update_conn_interval(10, 10);
  1259. cli_printf(p_cli, "err %d", error);
  1260. }
  1261. CLI_CMD_REGISTER(bleupdata10, "clear sereen", bleupdata_pcs);
  1262. void bleupdata_1000pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1263. {
  1264. unsigned int error = 0;
  1265. error = Ble_update_conn_interval(1000, 1000);
  1266. cli_printf(p_cli, "err %d", error);
  1267. }
  1268. CLI_CMD_REGISTER(bleupdata1000, "clear sereen", bleupdata_1000pcs);
  1269. void slaveupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1270. {
  1271. unsigned int error =
  1272. slave_update_conn_interval_request(40, 40);
  1273. cli_printf(p_cli, "err %d", error);
  1274. }
  1275. CLI_CMD_REGISTER(slaveupdata, "clear sereen", slaveupdata_pcs);
  1276. void conn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1277. {
  1278. if (argc == 1)
  1279. {
  1280. host_set_scan_name(argv[0], strlen(argv[0]));
  1281. host_init(slave_r);
  1282. }
  1283. else
  1284. cli_printf(p_cli, "err ");
  1285. }
  1286. CLI_CMD_REGISTER(conn, "clear sereen", conn_pcs);
  1287. void scan_name_set_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1288. {
  1289. if (argc == 1)
  1290. {
  1291. host_set_scan_name(argv[0], strlen(argv[0]));
  1292. }
  1293. else
  1294. cli_printf(p_cli, "err ");
  1295. }
  1296. CLI_CMD_REGISTER(scan_name_set, "clear sereen", scan_name_set_pcs);
  1297. void systemreset_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1298. {
  1299. NVIC_SystemReset();
  1300. }
  1301. CLI_CMD_REGISTER(systemreset, "clear sereen", systemreset_pcs);
  1302. void scanstart_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1303. {
  1304. scan_start();
  1305. }
  1306. CLI_CMD_REGISTER(scanstart, "clear sereen", scanstart_pcs);
  1307. void slave_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1308. {
  1309. slave_disconnect();
  1310. }
  1311. CLI_CMD_REGISTER(slave_dec, "clear sereen", slave_dec_pcs);
  1312. void host_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1313. {
  1314. host_disconnect();
  1315. }
  1316. CLI_CMD_REGISTER(host_dec, "clear sereen", host_dec_pcs);
  1317. void getconn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1318. {
  1319. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.min_conn_interval);
  1320. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.max_conn_interval);
  1321. BLE_PRINT("slave_latency : %d\r\n", slave_conn_params.slave_latency);
  1322. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", slave_conn_params.conn_sup_timeout);
  1323. extern ble_gap_conn_params_t host_conn_params;
  1324. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", host_conn_params.min_conn_interval);
  1325. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", host_conn_params.max_conn_interval);
  1326. BLE_PRINT("slave_latency : %d\r\n", host_conn_params.slave_latency);
  1327. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", host_conn_params.conn_sup_timeout);
  1328. slave_set_adv_name("123456", 6);
  1329. gap_params_init();
  1330. while (slave_isconnect() == 1)
  1331. {
  1332. }
  1333. BLE_PRINT("123456555");
  1334. advertising_start();
  1335. BLE_PRINT("4554564");
  1336. }
  1337. CLI_CMD_REGISTER(getconn, "clear sereen", getconn_pcs);
  1338. void slave_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1339. {
  1340. slave_get_rssi();
  1341. }
  1342. CLI_CMD_REGISTER(slave_get_rssi, "clear sereen", slave_get_rssi_pcs);
  1343. void host_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1344. {
  1345. host_get_rssi();
  1346. }
  1347. CLI_CMD_REGISTER(host_get_rssi, "clear sereen", host_get_rssi_pcs);
  1348. int teg = 0;
  1349. unsigned int rtccc = 0;
  1350. void radio_evt_conf(void)
  1351. {
  1352. NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
  1353. /* Start 16 MHz crystal oscillator */
  1354. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  1355. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  1356. txbuff[1] = NRF_RTC0->COUNTER;
  1357. txbuff[2] = teg;
  1358. /* Wait for the external oscillator to start up */
  1359. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  1360. {
  1361. // Do nothing.
  1362. }
  1363. // Radio config
  1364. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  1365. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  1366. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  1367. NRF_RADIO->PREFIX0 = 0xC3438303;
  1368. NRF_RADIO->PREFIX1 = 0xE3630023;
  1369. NRF_RADIO->BASE0 = 0x80C4A2E6;
  1370. NRF_RADIO->BASE1 = 0x91D5B3F7;
  1371. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  1372. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  1373. NRF_RADIO->PCNF0 = 0X00030006;
  1374. NRF_RADIO->PCNF1 = 0X01040020;
  1375. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  1376. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  1377. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
  1378. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos //READYoó×??ˉ?aê??′DDSTART
  1379. | RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos;
  1380. // Set payload pointer
  1381. NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
  1382. NRF_RADIO->EVENTS_DISABLED = 0; //??3y±ê????
  1383. NRF_RADIO->TASKS_TXEN = 1; //?aê?oó?á?ú2?×??o2ù×÷
  1384. while (NRF_RADIO->EVENTS_END == 0)
  1385. {
  1386. //μè′y·¢?ííê3é
  1387. }
  1388. nrf_gpio_pin_write(rx, 0);
  1389. NRF_RADIO->SHORTS = 0;
  1390. NRF_RADIO->EVENTS_DISABLED = 0U;
  1391. NRF_RADIO->TASKS_DISABLE = 1U;
  1392. while (NRF_RADIO->EVENTS_DISABLED == 0)
  1393. {
  1394. //μè′y1?μ?radio
  1395. }
  1396. NRF_RADIO->EVENTS_READY = 0U;
  1397. // Enable radio and wait for ready
  1398. NRF_RADIO->TASKS_RXEN = 1U;
  1399. NRF_RADIO->PACKETPTR = (uint32_t)&rxbuff[0];
  1400. while (NRF_RADIO->EVENTS_READY == 0U)
  1401. {
  1402. // wait
  1403. }
  1404. nrf_gpio_pin_write(rx, 1);
  1405. NRF_RADIO->EVENTS_END = 0U;
  1406. // Start listening and wait for address received event
  1407. NRF_RADIO->TASKS_START = 1U;
  1408. // Wait for end of packet or buttons state changed
  1409. for (int j = 0; j < 5000; j++)
  1410. {
  1411. if (NRF_RADIO->EVENTS_END == 1)
  1412. break;
  1413. }
  1414. if (NRF_RADIO->CRCSTATUS == 1U)
  1415. {
  1416. for (int i = 0; i < 50; i++)
  1417. {
  1418. BLE_PRINT("%x", rxbuff[i]);
  1419. }
  1420. BLE_PRINT("\r\n ");
  1421. memset(rxbuff, 0, 60);
  1422. }
  1423. else
  1424. {
  1425. BLE_PRINT("E\r\n ");
  1426. }
  1427. }
  1428. nrf_radio_signal_callback_return_param_t call_radio_return_val;
  1429. nrf_radio_signal_callback_return_param_t *call_radio(unsigned char sig)
  1430. {
  1431. nrf_gpio_pin_write(rx, 1);
  1432. radio_evt_conf();
  1433. nrf_gpio_pin_write(rx, 0);
  1434. call_radio_return_val.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END;
  1435. return &call_radio_return_val;
  1436. }
  1437. void radio_session_open(void)
  1438. {
  1439. BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
  1440. }
  1441. void radio_session_open_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1442. {
  1443. BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
  1444. }
  1445. CLI_CMD_REGISTER(radio_s_open, "clear sereen", radio_session_open_pcs);
  1446. void radio_session_close_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1447. {
  1448. BLE_PRINT("error= %d\r\n", sd_radio_session_close());
  1449. }
  1450. CLI_CMD_REGISTER(radio_s_close, "clear sereen", radio_session_close_pcs);
  1451. void radio_request_earliest(void)
  1452. {
  1453. radio_session_open();
  1454. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
  1455. radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_NO_GUARANTEE;
  1456. radio_request_p.params.earliest.length_us = 4000;
  1457. radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
  1458. radio_request_p.params.earliest.timeout_us = 2000;
  1459. BLE_PRINT("radio_request_earliest= %d\r\n", sd_radio_request(&radio_request_p));
  1460. //
  1461. // radio_request_p.request_type=NRF_RADIO_REQ_TYPE_NORMAL;
  1462. // radio_request_p.params.normal.hfclk=NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
  1463. // radio_request_p.params.normal.distance_us=10000;
  1464. // radio_request_p.params.normal.length_us=5000;
  1465. // radio_request_p.params.normal.priority=NRF_RADIO_PRIORITY_NORMAL;
  1466. }
  1467. void radio_request_e_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1468. {
  1469. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
  1470. radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
  1471. radio_request_p.params.earliest.length_us = 5000;
  1472. radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
  1473. radio_request_p.params.earliest.timeout_us = 2000;
  1474. BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
  1475. }
  1476. CLI_CMD_REGISTER(radio_r_e, "clear sereen", radio_request_e_pcs);
  1477. void radio_request_n_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1478. {
  1479. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_NORMAL;
  1480. BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
  1481. }
  1482. CLI_CMD_REGISTER(radio_r_n, "clear sereen", radio_request_n_pcs);
  1483. void Radio_State_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1484. {
  1485. Radio_State();
  1486. }
  1487. CLI_CMD_REGISTER(Radio_State, "clear sereen", Radio_State_pcs);
  1488. void s100_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1489. {
  1490. send_bytes_client(buff, 150);
  1491. }
  1492. CLI_CMD_REGISTER(s100, "clear sereen", s100_pcs);
  1493. #endif