slave.c.bak 49 KB

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