slave.c 50 KB

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