slave.c 50 KB

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