slave.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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_RSSI_CHANGED:
  318. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_RSSI_CHANGED\r\n");
  319. break;
  320. case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
  321. {
  322. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST\r\n");
  323. ble_gap_data_length_params_t const dlp =
  324. {
  325. .max_rx_octets = BLE_GAP_DATA_LENGTH_AUTO,
  326. .max_tx_octets = BLE_GAP_DATA_LENGTH_AUTO,
  327. };
  328. err_code = sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, &dlp, NULL);
  329. APP_ERROR_CHECK(err_code);
  330. }
  331. break;
  332. case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
  333. {
  334. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE\r\n");
  335. BLE_PRINT("max_rx_octets : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_rx_octets);
  336. BLE_PRINT("max_rx_time_us : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_rx_time_us);
  337. BLE_PRINT("max_tx_octets : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_tx_octets);
  338. BLE_PRINT("max_tx_time_us : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_tx_time_us);
  339. }
  340. break;
  341. case BLE_GAP_EVT_ADV_SET_TERMINATED:
  342. BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_ADV_SET_TERMINATED\r\n");
  343. break;
  344. case BLE_GATTS_EVT_HVN_TX_COMPLETE:
  345. // BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTS_EVT_HVN_TX_COMPLETE\r\n");
  346. break;
  347. case BLE_GATTS_EVT_WRITE: //D′è?2ù×÷ò??-íê3é
  348. break;
  349. case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
  350. // err_code = sd_ble_gattc_exchange_mtu_request(p_ble_evt->evt.gattc_evt.conn_handle,247);
  351. // APP_ERROR_CHECK(err_code);
  352. 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);
  353. break;
  354. case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST://?÷?ú?ò′ó?úéê??mtuê±μ?ê??t
  355. {
  356. sd_ble_gatts_exchange_mtu_reply(m_conn_handle, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  357. 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);
  358. }break;
  359. default:
  360. BLE_PRINT("on_ble_peripheral_evt -> default : 0x%2x\r\n", p_ble_evt->header.evt_id);
  361. // No implementation needed.
  362. break;
  363. }
  364. }
  365. #if USE_LADDR == 1
  366. ble_gap_addr_t m_my_addr;
  367. char set_adv_name = 0;
  368. #endif
  369. static void gap_params_init(void) //GAP3?ê??ˉ
  370. {
  371. uint32_t err_code;
  372. ble_gap_conn_params_t gap_conn_params;
  373. ble_gap_conn_sec_mode_t sec_mode;
  374. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  375. #if USE_LADDR == 1
  376. err_code = sd_ble_gap_addr_get(&m_my_addr);
  377. APP_ERROR_CHECK(err_code);
  378. if (set_adv_name == 0)
  379. {
  380. 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]);
  381. sprintf(BleReallyName, "%s_%02X%02X", DEVICE_NAME, m_my_addr.addr[4], m_my_addr.addr[5]);
  382. err_code = sd_ble_gap_device_name_set(&sec_mode,
  383. (const uint8_t *)BleReallyName,
  384. strlen(DEVICE_NAME) + 5);
  385. }
  386. else
  387. {
  388. err_code = sd_ble_gap_device_name_set(&sec_mode,
  389. (const uint8_t *)BleReallyName,
  390. strlen(BleReallyName));
  391. }
  392. BLE_PRINT(">>>>>>>name:%d,%s",set_adv_name,BleReallyName);
  393. #else
  394. err_code = sd_ble_gap_device_name_set(&sec_mode,
  395. (const uint8_t *)DEVICE_NAME,
  396. strlen(DEVICE_NAME));
  397. #endif
  398. APP_ERROR_CHECK(err_code);
  399. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  400. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  401. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  402. gap_conn_params.slave_latency = SLAVE_LATENCY;
  403. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  404. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  405. APP_ERROR_CHECK(err_code);
  406. // err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN,m_conn_handle,0);
  407. // APP_ERROR_CHECK(err_code);
  408. }
  409. #if USEFIFO
  410. RINGFRAME_DEF(sbc,ringframe_size_1024);
  411. static unsigned int TIME_GetTicks_ms;
  412. unsigned int send_bytes_client(unsigned char *bytes, uint16_t len)
  413. {
  414. unsigned short length = len;
  415. if (connect_to_client)
  416. {
  417. do
  418. {
  419. if(ringframe_in(&sbc,bytes,length)==0)return 0;
  420. }while(ringframe_throw(&sbc)==0);
  421. Process_SetHoldOn(send_bytes_client_pcs,1);
  422. TIME_GetTicks_ms=TIME_GetTicks();
  423. return 0;
  424. }
  425. else
  426. {
  427. BLE_PRINT("send_bytes_client error. connect_to_client=0\r\n");
  428. return 1;
  429. }
  430. } //作为从机时发送数据给主机
  431. void send_bytes_client_pcs(void)
  432. {
  433. unsigned char sbuff[256];
  434. unsigned char len=0;
  435. while(ringframe_peek(&sbc,sbuff,&len)==0)
  436. {
  437. unsigned short length = len;
  438. uint32_t flag = 0;
  439. flag = ble_nus_data_send(&m_nus, sbuff, &length, m_conn_handle);
  440. if(flag==0)ringframe_throw(&sbc);
  441. else
  442. {
  443. if((TIME_GetTicks()-TIME_GetTicks_ms>100)||(TIME_GetTicks_ms>TIME_GetTicks()))
  444. {
  445. Process_SetHoldOn(send_bytes_client_pcs,0);
  446. }
  447. return;
  448. }
  449. }
  450. Process_SetHoldOn(send_bytes_client_pcs,0);
  451. }
  452. #else
  453. unsigned int send_bytes_client(unsigned char *bytes, uint16_t len)
  454. {
  455. unsigned int rev=0;
  456. unsigned short length = len;
  457. if (connect_to_client){
  458. rev=ble_nus_data_send(&m_nus, bytes, &length, m_conn_handle);
  459. return rev;
  460. }
  461. else{
  462. BLE_PRINT("send_bytes_client error. connect_to_client=0\r\n");
  463. return 1;
  464. }
  465. } //作为从机时发送数据给主机
  466. void send_bytes_client_pcs(void)
  467. {
  468. }
  469. #endif
  470. extern void timer_init(void);
  471. extern void power_management_init(void);
  472. extern void ble_stack_init(void);
  473. extern void gatt_init(void);
  474. extern char ble_stack_init_sta;
  475. extern uint8_t app_Get_isHost(void);
  476. #if USEMACNAME && USE_LADDR != 1
  477. ble_gap_addr_t mAddr;
  478. #endif
  479. void slave_init(Ble_receive_handler_t receive_handler)
  480. {
  481. static unsigned char init = 1;
  482. if (init)
  483. {
  484. if (receive_handler == NULL)
  485. {
  486. BLE_PRINT("slave_init -> param err \r\n");
  487. return;
  488. }
  489. Rec_h = receive_handler;
  490. if (ble_stack_init_sta)
  491. {
  492. timer_init(); //
  493. power_management_init(); //
  494. ble_stack_init(); //
  495. gatt_init(); //
  496. ble_stack_init_sta = 0;
  497. }
  498. #if USEMACNAME && USE_LADDR != 1
  499. if (!app_Get_isHost())
  500. {
  501. sd_ble_gap_addr_get(&mAddr);
  502. memset(DEVICE_NAME, 0, TARFET_LEN_MAX);
  503. 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]);
  504. }
  505. #endif
  506. gap_params_init();
  507. services_init();
  508. advertising_init();
  509. conn_params_init();
  510. advertising_start();
  511. init = 0;
  512. #if USE_LADDR
  513. BLE_PRINT("slave_init -> name [ %s ] \r\n", BleReallyName);
  514. #else
  515. BLE_PRINT("slave_init -> name [ %s ] \r\n", DEVICE_NAME);
  516. #endif
  517. }
  518. else
  519. {
  520. BLE_PRINT("slave_init -> err slave has init done \r\n");
  521. }
  522. }
  523. unsigned char slave_isconnect(void)
  524. {
  525. return connect_to_client;
  526. }
  527. unsigned int slave_set_adv_name(char *name, int len)
  528. {
  529. #if USE_LADDR == 1
  530. if (len > TARFET_LEN_MAX)
  531. return APP_ERR_OVERLENGTH;
  532. set_adv_name = 1;
  533. memset(BleReallyName, 0, TARFET_LEN_MAX);
  534. memcpy(BleReallyName, name, len);
  535. #else
  536. if (len > TARFET_LEN_MAX)
  537. return APP_ERR_OVERLENGTH;
  538. memset(DEVICE_NAME, 0, TARFET_LEN_MAX);
  539. memcpy(DEVICE_NAME, name, len);
  540. #endif
  541. return APP_SUCCESS;
  542. }
  543. void slave_get_advname_len(int *len)
  544. {
  545. *len = strlen(BleReallyName);
  546. }
  547. void slave_get_advname(char *name, int len)
  548. {
  549. memcpy(name,BleReallyName,len);
  550. }
  551. void slave_disconnect(void)
  552. {
  553. if (connect_to_client)
  554. sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  555. }
  556. unsigned int slave_update_conn_interval_request(float min_conn_interval, float max_conn_interval)
  557. {
  558. ret_code_t err_code;
  559. ble_gap_conn_params_t bgcp;
  560. if (slave_update_conn_interval_request_sta)
  561. return APP_ERR_BUSY;
  562. if (connect_to_client)
  563. {
  564. slave_update_conn_interval_request_sta = 1;
  565. if ((max_conn_interval > 1.25 * 1599) || (max_conn_interval < min_conn_interval))
  566. return APP_ERR_PARAMERR;
  567. if (min_conn_interval < 7.5f)
  568. return APP_ERR_PARAMERR;
  569. bgcp.max_conn_interval = MSEC_TO_UNITS(max_conn_interval, UNIT_1_25_MS);
  570. bgcp.min_conn_interval = MSEC_TO_UNITS(min_conn_interval, UNIT_1_25_MS);
  571. bgcp.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS);
  572. bgcp.slave_latency = 0;
  573. BLE_PRINT("slave_update_conn_interval_request -> %d \r\n", bgcp.max_conn_interval);
  574. err_code = sd_ble_gap_conn_param_update(m_conn_handle, &bgcp);
  575. APP_ERROR_CHECK(err_code);
  576. return err_code;
  577. }
  578. else
  579. {
  580. return APP_ERR_DISCONN;
  581. }
  582. }
  583. void slave_get_conn_params(ble_gap_conn_params_t *p)
  584. {
  585. p->conn_sup_timeout = slave_conn_params.conn_sup_timeout;
  586. p->max_conn_interval = slave_conn_params.max_conn_interval;
  587. p->min_conn_interval = slave_conn_params.min_conn_interval;
  588. p->slave_latency = slave_conn_params.slave_latency;
  589. }
  590. void slave_adv_init(void)
  591. {
  592. gap_params_init(); //ìí?óμ?GAP3?ê??ˉ
  593. conn_params_init(); //ìí?óμ?á??ó2?êy3?ê??ˉ
  594. advertising_init(); //ìí?ó1?2¥3?ê??ˉ
  595. }
  596. static signed char rssi = 0;
  597. signed char slave_get_rssi(void)
  598. {
  599. unsigned char channel;
  600. if (connect_to_client == 0)
  601. return 1;
  602. sd_ble_gap_rssi_get(m_conn_handle, &rssi, &channel);
  603. // BLE_PRINT("rssi= %d channel=%d\r\n", rssi, channel);
  604. return rssi;
  605. }
  606. #if DEBUGBLE
  607. #define led 13
  608. #define tx 11 //1.1
  609. #define rx 12
  610. //#define LS -1611916254 //?a·¢°?
  611. //#define RS -889050188
  612. //#define LS 97376119 //31
  613. //#define RS 627878688 //32
  614. #define LS -1087551583 //1.1
  615. #define RS -957332282 //1.1
  616. #define PS -1372482754 //usb
  617. unsigned char buff[255];
  618. char start = 0;
  619. void host_r(unsigned char *p, int len)
  620. {
  621. BLE_PRINT("hr : %d,0x%x\r\n", len, p[0]);
  622. if (p[0] == 0xbb)
  623. {
  624. BLE_PRINT("hr -------------: 0xbb\r\n");
  625. SEGGER_RTT_Write(0, &p[1], len);
  626. }
  627. if (p[0] == 0xcc)
  628. {
  629. BLE_PRINT("hr -------------: 0xcc\r\n");
  630. }
  631. }
  632. #define TIMER_TICK 25
  633. #define TCUN 1000
  634. unsigned short cun = 0;
  635. unsigned short ts = 0;
  636. unsigned short rec[5] = {0};
  637. unsigned short recrtc[5] = {0};
  638. unsigned int rtc_cun = 0;
  639. void slave_r(unsigned char *p, int len)
  640. {
  641. if (p[0] == 0xaa)
  642. {
  643. cun++;
  644. ts = p[1];
  645. ts = ts << 8;
  646. ts += p[2];
  647. if (ts >= 1)
  648. {
  649. start = 1;
  650. rtc_cun = NRF_RTC2->COUNTER;
  651. }
  652. if (ts == TCUN)
  653. start = 0;
  654. if (start)
  655. {
  656. if (NRF_RTC2->COUNTER - rtc_cun < 1 * TIMER_TICK)
  657. recrtc[0]++;
  658. if ((NRF_RTC2->COUNTER - rtc_cun >= 1 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 2 * TIMER_TICK))
  659. recrtc[1]++;
  660. if ((NRF_RTC2->COUNTER - rtc_cun >= 2 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 3 * TIMER_TICK))
  661. recrtc[2]++;
  662. if ((NRF_RTC2->COUNTER - rtc_cun >= 3 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 4 * TIMER_TICK))
  663. recrtc[3]++;
  664. if (NRF_RTC2->COUNTER - rtc_cun > 4 * TIMER_TICK)
  665. recrtc[4]++;
  666. rtc_cun = NRF_RTC2->COUNTER;
  667. }
  668. BLE_PRINT("sr : %d\r\n", ts);
  669. }
  670. if (p[0] == 0xbb)
  671. {
  672. buff[0] = 0xbb;
  673. 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]);
  674. send_bytes_server(buff, leng);
  675. }
  676. if (p[0] == 0xcc)
  677. {
  678. BLE_PRINT("sr -------------: 0xcc\r\n");
  679. memset(rec, 0, 10);
  680. memset(recrtc, 0, 10);
  681. send_bytes_server(p, 3);
  682. }
  683. }
  684. #include "cli.h"
  685. nrf_radio_request_t radio_request_p;
  686. APP_TIMER_DEF(s_Timer);
  687. #define TEST_PERIOD APP_TIMER_TICKS(TIMER_TICK)
  688. unsigned short tims = 0;
  689. unsigned short stp = 0;
  690. void s_TimerCallback(void *arg)
  691. {
  692. if ((tims > 0) && (tims <= TCUN))
  693. {
  694. buff[0] = 0xaa;
  695. buff[1] = tims >> 8;
  696. buff[2] = tims;
  697. send_bytes_client(buff, 100);
  698. BLE_PRINT("send : %d\r\n", tims);
  699. tims++;
  700. }
  701. if (start)
  702. {
  703. if (cun > 4)
  704. cun = 4;
  705. rec[cun]++;
  706. cun = 0;
  707. }
  708. //·¢êy?Y??ê??ú
  709. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  710. {
  711. if (start)
  712. {
  713. buff[0] = 0xaa;
  714. buff[1] = stp >> 8;
  715. buff[2] = stp;
  716. send_bytes_client(buff, 100);
  717. }
  718. stp++;
  719. }
  720. // nrf_gpio_pin_toggle(rx);
  721. //nrf_gpio_pin_write(rx, 0);
  722. // BLE_PRINT("error= %d\r\n", sd_radio_request(&radio_request_p));
  723. }
  724. void Radio_State(void)
  725. {
  726. switch (NRF_RADIO->STATE)
  727. {
  728. case RADIO_STATE_STATE_Disabled:
  729. BLE_PRINT("RADIO_STATE_STATE_Disabled\r\n");
  730. break;
  731. case RADIO_STATE_STATE_RxRu:
  732. BLE_PRINT("RADIO_STATE_STATE_RxRu\r\n");
  733. break;
  734. case RADIO_STATE_STATE_RxIdle:
  735. BLE_PRINT("RADIO_STATE_STATE_RxIdle\r\n");
  736. break;
  737. case RADIO_STATE_STATE_Rx:
  738. BLE_PRINT("RADIO_STATE_STATE_Rx\r\n");
  739. break;
  740. case RADIO_STATE_STATE_RxDisable:
  741. BLE_PRINT("RADIO_STATE_STATE_RxDisable\r\n");
  742. break;
  743. case RADIO_STATE_STATE_TxRu:
  744. BLE_PRINT("RADIO_STATE_STATE_TxRu\r\n");
  745. break;
  746. case RADIO_STATE_STATE_TxIdle:
  747. BLE_PRINT("RADIO_STATE_STATE_TxIdle\r\n");
  748. break;
  749. case RADIO_STATE_STATE_Tx:
  750. BLE_PRINT("RADIO_STATE_STATE_Tx\r\n");
  751. break;
  752. case RADIO_STATE_STATE_TxDisable:
  753. BLE_PRINT("RADIO_STATE_STATE_TxDisable\r\n");
  754. break;
  755. }
  756. }
  757. void unoioo(void)
  758. {
  759. Ble_update_conn_interval(7.5,7.5);
  760. }
  761. void unoioo_s(void)
  762. {
  763. slave_update_conn_interval_request(30, 30);
  764. scan_start();
  765. }
  766. void unoioo_s_d(void)
  767. {
  768. host_disconnect();
  769. scan_start();
  770. }
  771. void rtc_config(void)
  772. {
  773. NRF_RTC2->PRESCALER = 0; //??ò?oá????êy?÷?ó1,1024us
  774. NRF_RTC2->TASKS_START = 1;
  775. }
  776. #include "nrf_drv_timer.h"
  777. void radio_evt_conf(void);
  778. const nrf_drv_timer_t TIMER_RADIO = NRF_DRV_TIMER_INSTANCE(2);
  779. void timer_led_event_handler(nrf_timer_event_t event_type, void *p_context)
  780. {
  781. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  782. {
  783. switch (event_type)
  784. {
  785. case NRF_TIMER_EVENT_COMPARE0: //320
  786. sd_radio_request(&radio_request_p);
  787. NRF_PPI->CHEN &= (~(PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos));
  788. break;
  789. case NRF_TIMER_EVENT_COMPARE1: //324
  790. sd_radio_request(&radio_request_p);
  791. break;
  792. case NRF_TIMER_EVENT_COMPARE2: //328
  793. sd_radio_request(&radio_request_p);
  794. break;
  795. case NRF_TIMER_EVENT_COMPARE3: //332
  796. NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
  797. break;
  798. default:
  799. //Do nothing.
  800. break;
  801. }
  802. }
  803. if (*NRF_FICR->DEVICEID == RS) //óò±?D?
  804. {
  805. switch (event_type)
  806. {
  807. case NRF_TIMER_EVENT_COMPARE0: //320
  808. nrf_gpio_pin_write(tx, 1);
  809. break;
  810. case NRF_TIMER_EVENT_COMPARE1: //324
  811. nrf_gpio_pin_write(tx, 0);
  812. break;
  813. case NRF_TIMER_EVENT_COMPARE2: //328
  814. break;
  815. case NRF_TIMER_EVENT_COMPARE3: //332
  816. break;
  817. default:
  818. //Do nothing.
  819. break;
  820. }
  821. }
  822. }
  823. void timer_config(void)
  824. {
  825. uint32_t time_us = 5000; //Time(in miliseconds) between consecutive compare events.
  826. uint32_t time_ticks;
  827. uint32_t err_code = NRF_SUCCESS;
  828. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  829. err_code = nrf_drv_timer_init(&TIMER_RADIO, &timer_cfg, timer_led_event_handler);
  830. APP_ERROR_CHECK(err_code);
  831. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  832. {
  833. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
  834. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
  835. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 10000);
  836. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, 0, true);
  837. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 20000);
  838. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL2, time_ticks, 0, true);
  839. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, 29000);
  840. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL3, time_ticks, NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK, true);
  841. }
  842. if (*NRF_FICR->DEVICEID == RS) //óò±?D?
  843. {
  844. time_us = 1000;
  845. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
  846. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
  847. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 9000 + 1);
  848. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
  849. nrf_drv_timer_enable(&TIMER_RADIO);
  850. }
  851. // nrf_drv_timer_enable(&TIMER_RADIO);
  852. }
  853. void ppi_set(void)
  854. {
  855. NRF_PPI->CH[0].EEP = (unsigned int)(&NRF_TIMER0->EVENTS_COMPARE[0]);
  856. NRF_PPI->CH[0].TEP = (unsigned int)(&NRF_TIMER2->TASKS_START);
  857. NRF_PPI->CH[1].EEP = (unsigned int)(&NRF_TIMER2->EVENTS_COMPARE[3]);
  858. NRF_PPI->CH[1].TEP = (unsigned int)(&NRF_TIMER2->TASKS_SHUTDOWN);
  859. NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) |
  860. (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
  861. }
  862. extern void USR_Init(void);
  863. extern void USR_Process(void);
  864. extern void TIME_Init(void);
  865. extern char Target_scan[];
  866. unsigned char txbuff[300] = {0x0a, 0x03, 0, 0, 2, 3, 4, 5, 6, 0, 8, 9};
  867. unsigned char rxbuff[300] = {0};
  868. void radio_init_R(void)
  869. {
  870. NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
  871. /* Start 16 MHz crystal oscillator */
  872. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  873. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  874. /* Wait for the external oscillator to start up */
  875. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  876. {
  877. // Do nothing.
  878. }
  879. // Radio config
  880. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  881. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  882. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  883. NRF_RADIO->PREFIX0 = 0xC3438303;
  884. NRF_RADIO->PREFIX1 = 0xE3630023;
  885. NRF_RADIO->BASE0 = 0x80C4A2E6;
  886. NRF_RADIO->BASE1 = 0x91D5B3F7;
  887. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  888. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  889. NRF_RADIO->PCNF0 = 0X00030006;
  890. NRF_RADIO->PCNF1 = 0X01040020;
  891. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  892. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  893. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
  894. NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
  895. }
  896. #include "nrf_drv_rtc.h"
  897. const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
  898. unsigned int countevt = 0;
  899. void radio_connect(void)
  900. {
  901. NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
  902. countevt = 1;
  903. nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + countevt * 0.009 * 32768, true);
  904. countevt++;
  905. }
  906. void RADIO_IRQHandler(void)
  907. {
  908. if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk))
  909. {
  910. NRF_RADIO->EVENTS_READY = 0U;
  911. BLE_PRINT("a");
  912. }
  913. if (NRF_RADIO->EVENTS_ADDRESS && (NRF_RADIO->INTENSET & RADIO_INTENSET_ADDRESS_Msk))
  914. {
  915. NRF_RADIO->EVENTS_ADDRESS = 0U;
  916. BLE_PRINT("b");
  917. }
  918. if (NRF_RADIO->EVENTS_PAYLOAD && (NRF_RADIO->INTENSET & RADIO_INTENSET_PAYLOAD_Msk))
  919. {
  920. NRF_RADIO->EVENTS_PAYLOAD = 0U;
  921. BLE_PRINT("c");
  922. }
  923. if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
  924. {
  925. NRF_RADIO->EVENTS_END = 0U;
  926. // NRF_LOG_INFO("d");
  927. if (NRF_RADIO->STATE >= 5UL)
  928. {
  929. NRF_RADIO->EVENTS_DISABLED = 0U;
  930. NRF_RADIO->TASKS_DISABLE = 1U;
  931. nrf_gpio_pin_write(tx, 0);
  932. // BLE_PRINT("Tx end\r\n");
  933. }
  934. else
  935. {
  936. //ê?μ?êy?Yoó?è?D???a·¢?í?£ê?
  937. NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
  938. NRF_RADIO->PACKETPTR = (unsigned int)txbuff;
  939. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
  940. nrf_gpio_pin_write(tx, 0);
  941. NRF_RADIO->EVENTS_DISABLED = 0U;
  942. NRF_RADIO->TASKS_DISABLE = 1U;
  943. while (NRF_RADIO->EVENTS_DISABLED == 0)
  944. ;
  945. NRF_RADIO->TASKS_TXEN = 1;
  946. nrf_gpio_pin_write(tx, 1);
  947. nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + 0.010 * 32768, true);
  948. nrf_drv_rtc_cc_set(&rtc, 1, NRF_RTC0->CC[2] + 0.018 * 32768, true);
  949. for (int i = 0; i < 50; i++)
  950. {
  951. BLE_PRINT("%x", rxbuff[i]);
  952. }
  953. BLE_PRINT("Rx\r\n", rxbuff[1]);
  954. }
  955. }
  956. if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
  957. {
  958. NRF_RADIO->EVENTS_DISABLED = 0U;
  959. BLE_PRINT("e");
  960. }
  961. if (NRF_RADIO->EVENTS_DEVMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMATCH_Msk))
  962. {
  963. NRF_RADIO->EVENTS_DEVMATCH = 0U;
  964. BLE_PRINT("f");
  965. }
  966. if (NRF_RADIO->EVENTS_DEVMISS && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMISS_Msk))
  967. {
  968. NRF_RADIO->EVENTS_DEVMISS = 0U;
  969. BLE_PRINT("g");
  970. }
  971. if (NRF_RADIO->EVENTS_RSSIEND && (NRF_RADIO->INTENSET & RADIO_INTENSET_RSSIEND_Msk))
  972. {
  973. NRF_RADIO->EVENTS_RSSIEND = 0U;
  974. BLE_PRINT("h");
  975. }
  976. if (NRF_RADIO->EVENTS_BCMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_BCMATCH_Msk))
  977. {
  978. NRF_RADIO->EVENTS_BCMATCH = 0U;
  979. BLE_PRINT("i");
  980. }
  981. if (NRF_RADIO->EVENTS_CRCOK && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCOK_Msk))
  982. {
  983. NRF_RADIO->EVENTS_CRCOK = 0U;
  984. BLE_PRINT("k");
  985. }
  986. if (NRF_RADIO->EVENTS_CRCERROR && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCERROR_Msk))
  987. {
  988. NRF_RADIO->EVENTS_CRCERROR = 0U;
  989. BLE_PRINT("l");
  990. }
  991. NVIC_ClearPendingIRQ(RADIO_IRQn);
  992. }
  993. void radio_scan_start(void)
  994. {
  995. NRF_RADIO->SHORTS = 0;
  996. NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_RXEN_Msk;
  997. NRF_RADIO->SHORTS |= RADIO_SHORTS_READY_START_Msk;
  998. NRF_RADIO->SHORTS |= RADIO_SHORTS_END_START_Msk;
  999. NRF_RADIO->INTENSET |= RADIO_INTENSET_END_Msk;
  1000. NRF_RADIO->TASKS_RXEN = 1;
  1001. NRF_RADIO->EVENTS_READY = 0;
  1002. while (NRF_RADIO->EVENTS_READY == 0)
  1003. {
  1004. }
  1005. NRF_RADIO->TASKS_START = 1;
  1006. NVIC_EnableIRQ(RADIO_IRQn);
  1007. Radio_State();
  1008. }
  1009. static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
  1010. {
  1011. switch (int_type)
  1012. {
  1013. case NRFX_RTC_INT_COMPARE0:
  1014. nrf_gpio_pin_write(tx, 1);
  1015. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
  1016. NRF_RADIO->PACKETPTR = (unsigned int)rxbuff;
  1017. NRF_RADIO->TASKS_RXEN = 1U;
  1018. break;
  1019. case NRFX_RTC_INT_COMPARE1:
  1020. Radio_State();
  1021. BLE_PRINT("NRFX_RTC_INT_COMPARE1\r\n");
  1022. break;
  1023. case NRFX_RTC_INT_COMPARE2:
  1024. break;
  1025. case NRFX_RTC_INT_COMPARE3:
  1026. break;
  1027. case NRFX_RTC_INT_TICK:
  1028. break;
  1029. case NRFX_RTC_INT_OVERFLOW:
  1030. nrf_drv_rtc_counter_clear(&rtc);
  1031. break;
  1032. }
  1033. }
  1034. /**********************************************************
  1035. * oˉêy??×?£ortc_config
  1036. * oˉêy×÷ó?£ortc?y?ˉ3?ê??ˉoíéè??
  1037. * oˉêy2?êy£o?T
  1038. * oˉêy·μ???μ£o?T
  1039. ***********************************************************/
  1040. void radio_rtc_config(void)
  1041. {
  1042. uint32_t err_code;
  1043. NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
  1044. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  1045. NRF_CLOCK->TASKS_LFCLKSTART = 1;
  1046. while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
  1047. {
  1048. // Do nothing.
  1049. }
  1050. //Initialize RTC instance
  1051. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  1052. config.prescaler = 0; //4095;????????=32768/(config.prescaler+1)Hz;
  1053. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
  1054. APP_ERROR_CHECK(err_code);
  1055. //Enable tick event & interrupt
  1056. // nrf_drv_rtc_tick_enable(&rtc, true);
  1057. //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
  1058. // err_code = nrf_drv_rtc_cc_set(&rtc, 0, 8, true);
  1059. // APP_ERROR_CHECK(err_code);
  1060. //Power on RTC instance
  1061. nrf_drv_rtc_enable(&rtc);
  1062. }
  1063. int main(void)
  1064. {
  1065. unsigned int error = 0;
  1066. unsigned int rtctemp = 0;
  1067. unsigned int start = 0;
  1068. unsigned int radio_dis_cun = 0;
  1069. unsigned int radio_dis_cun_rtc = 0;
  1070. nrf_gpio_cfg_output(led);
  1071. nrf_gpio_pin_write(led, 1);
  1072. nrf_gpio_cfg_output(tx);
  1073. nrf_gpio_pin_write(tx, 0);
  1074. nrf_gpio_cfg_output(8);
  1075. nrf_gpio_pin_write(8, 0);
  1076. nrf_gpio_cfg_output(rx);
  1077. nrf_gpio_pin_write(rx, 0);
  1078. BLE_PRINT("NRF_FICR->DEVICEID : %d\r\n", *NRF_FICR->DEVICEID);
  1079. if (*NRF_FICR->DEVICEID == RS) //óò±?D?
  1080. {
  1081. #if 1
  1082. slave_init(host_r);
  1083. #else
  1084. radio_init_R();
  1085. radio_rtc_config();
  1086. radio_scan_start();
  1087. #endif
  1088. BLE_PRINT("you \r\n");
  1089. }
  1090. if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  1091. {
  1092. #if 0
  1093. Target_scan[0]=0xe3; //3132
  1094. Target_scan[1]=0x3f;
  1095. Target_scan[2]=0xd9;
  1096. Target_scan[3]=0x0d;
  1097. Target_scan[4]=0x0e;
  1098. Target_scan[5]=0xc6;
  1099. 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]);
  1100. // Target_scan[0]=0x3C; //?a·¢°?
  1101. // Target_scan[1]=0x83;
  1102. // Target_scan[2]=0xCF;
  1103. // Target_scan[3]=0x49;
  1104. // Target_scan[4]=0x50;
  1105. // Target_scan[5]=0xE1;
  1106. //
  1107. #endif
  1108. Ble_Host_Connectd_Evt_Regist(unoioo);
  1109. Ble_Slave_Connectd_Evt_Regist(unoioo_s);
  1110. Ble_Slave_Disconn_Evt_Regist(unoioo_s_d);
  1111. // extern void radio_request_earliest(void);
  1112. // Ble_Slave_Connectd_Evt_Regist(radio_request_earliest);
  1113. slave_init(host_r);
  1114. host_init(slave_r);
  1115. // timer_config();
  1116. BLE_PRINT("zuo \r\n");
  1117. }
  1118. if (*NRF_FICR->DEVICEID == PS) //ê??ú
  1119. {
  1120. #if 0
  1121. Target_scan[0] = 0x21;
  1122. Target_scan[1] = 0x8a;
  1123. Target_scan[2] = 0x4f;
  1124. Target_scan[3] = 0x61;
  1125. Target_scan[4] = 0xcb;
  1126. Target_scan[5] = 0xe8;
  1127. #endif
  1128. host_set_scan_name("SH_13EC", 7);
  1129. BLE_PRINT("shou \r\n");
  1130. host_init(slave_r);
  1131. scan_start();
  1132. }
  1133. rtc_config();
  1134. for (int i = 1; i < 200; i++)
  1135. {
  1136. buff[i] = i + 0x30;
  1137. // txbuff[i]=i;
  1138. }
  1139. app_timer_create(&s_Timer, APP_TIMER_MODE_REPEATED, s_TimerCallback);
  1140. app_timer_start(s_Timer, TEST_PERIOD, NULL);
  1141. // ppi_set();
  1142. while (1)
  1143. {
  1144. cli_process(&clirtt);
  1145. if (NRF_SUCCESS == sd_evt_get(&error))
  1146. {
  1147. BLE_PRINT("shou \r\n");
  1148. }
  1149. // if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
  1150. {
  1151. if (NRF_RADIO->STATE == RADIO_STATE_STATE_Disabled)
  1152. {
  1153. nrf_gpio_pin_write(tx, 0);
  1154. }
  1155. else
  1156. {
  1157. nrf_gpio_pin_write(tx, 1);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. void host_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1163. {
  1164. host_init(slave_r);
  1165. }
  1166. CLI_CMD_REGISTER(host_init, "clear sereen", host_init_pcs);
  1167. void hsb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1168. {
  1169. send_bytes_server(buff, 200);
  1170. }
  1171. CLI_CMD_REGISTER(hsb, "clear sereen", hsb_pcs);
  1172. void send_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1173. {
  1174. tims = 1;
  1175. }
  1176. CLI_CMD_REGISTER(send, "clear sereen", send_pcs);
  1177. void scc_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1178. {
  1179. buff[0] = 0xcc;
  1180. send_bytes_client(buff, 6);
  1181. }
  1182. CLI_CMD_REGISTER(scc, "clear sereen", scc_pcs);
  1183. void sbb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1184. {
  1185. buff[0] = 0xbb;
  1186. send_bytes_client(buff, 6);
  1187. }
  1188. CLI_CMD_REGISTER(sbb, "clear sereen", sbb_pcs);
  1189. void hcb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1190. {
  1191. send_bytes_client(buff, 200);
  1192. }
  1193. CLI_CMD_REGISTER(hcb, "clear sereen", hcb_pcs);
  1194. void slave_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1195. {
  1196. slave_init(host_r);
  1197. }
  1198. CLI_CMD_REGISTER(slave_init, "clear sereen", slave_init_pcs);
  1199. void bleupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1200. {
  1201. unsigned int error = 0;
  1202. error = Ble_update_conn_interval(10, 10);
  1203. cli_printf(p_cli, "err %d", error);
  1204. }
  1205. CLI_CMD_REGISTER(bleupdata10, "clear sereen", bleupdata_pcs);
  1206. void bleupdata_1000pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1207. {
  1208. unsigned int error = 0;
  1209. error = Ble_update_conn_interval(1000, 1000);
  1210. cli_printf(p_cli, "err %d", error);
  1211. }
  1212. CLI_CMD_REGISTER(bleupdata1000, "clear sereen", bleupdata_1000pcs);
  1213. void slaveupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1214. {
  1215. unsigned int error =
  1216. slave_update_conn_interval_request(40, 40);
  1217. cli_printf(p_cli, "err %d", error);
  1218. }
  1219. CLI_CMD_REGISTER(slaveupdata, "clear sereen", slaveupdata_pcs);
  1220. void conn_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. host_init(slave_r);
  1226. }
  1227. else
  1228. cli_printf(p_cli, "err ");
  1229. }
  1230. CLI_CMD_REGISTER(conn, "clear sereen", conn_pcs);
  1231. void scan_name_set_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1232. {
  1233. if (argc == 1)
  1234. {
  1235. host_set_scan_name(argv[0], strlen(argv[0]));
  1236. }
  1237. else
  1238. cli_printf(p_cli, "err ");
  1239. }
  1240. CLI_CMD_REGISTER(scan_name_set, "clear sereen", scan_name_set_pcs);
  1241. void systemreset_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1242. {
  1243. NVIC_SystemReset();
  1244. }
  1245. CLI_CMD_REGISTER(systemreset, "clear sereen", systemreset_pcs);
  1246. void scanstart_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1247. {
  1248. scan_start();
  1249. }
  1250. CLI_CMD_REGISTER(scanstart, "clear sereen", scanstart_pcs);
  1251. void slave_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1252. {
  1253. slave_disconnect();
  1254. }
  1255. CLI_CMD_REGISTER(slave_dec, "clear sereen", slave_dec_pcs);
  1256. void host_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1257. {
  1258. host_disconnect();
  1259. }
  1260. CLI_CMD_REGISTER(host_dec, "clear sereen", host_dec_pcs);
  1261. void getconn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1262. {
  1263. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.min_conn_interval);
  1264. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.max_conn_interval);
  1265. BLE_PRINT("slave_latency : %d\r\n", slave_conn_params.slave_latency);
  1266. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", slave_conn_params.conn_sup_timeout);
  1267. extern ble_gap_conn_params_t host_conn_params;
  1268. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", host_conn_params.min_conn_interval);
  1269. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", host_conn_params.max_conn_interval);
  1270. BLE_PRINT("slave_latency : %d\r\n", host_conn_params.slave_latency);
  1271. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", host_conn_params.conn_sup_timeout);
  1272. slave_set_adv_name("123456", 6);
  1273. gap_params_init();
  1274. while (slave_isconnect() == 1)
  1275. {
  1276. }
  1277. BLE_PRINT("123456555");
  1278. advertising_start();
  1279. BLE_PRINT("4554564");
  1280. }
  1281. CLI_CMD_REGISTER(getconn, "clear sereen", getconn_pcs);
  1282. void slave_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1283. {
  1284. slave_get_rssi();
  1285. }
  1286. CLI_CMD_REGISTER(slave_get_rssi, "clear sereen", slave_get_rssi_pcs);
  1287. void host_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1288. {
  1289. host_get_rssi();
  1290. }
  1291. CLI_CMD_REGISTER(host_get_rssi, "clear sereen", host_get_rssi_pcs);
  1292. int teg = 0;
  1293. unsigned int rtccc = 0;
  1294. void radio_evt_conf(void)
  1295. {
  1296. NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
  1297. /* Start 16 MHz crystal oscillator */
  1298. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  1299. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  1300. txbuff[1] = NRF_RTC0->COUNTER;
  1301. txbuff[2] = teg;
  1302. /* Wait for the external oscillator to start up */
  1303. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  1304. {
  1305. // Do nothing.
  1306. }
  1307. // Radio config
  1308. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  1309. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  1310. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  1311. NRF_RADIO->PREFIX0 = 0xC3438303;
  1312. NRF_RADIO->PREFIX1 = 0xE3630023;
  1313. NRF_RADIO->BASE0 = 0x80C4A2E6;
  1314. NRF_RADIO->BASE1 = 0x91D5B3F7;
  1315. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  1316. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  1317. NRF_RADIO->PCNF0 = 0X00030006;
  1318. NRF_RADIO->PCNF1 = 0X01040020;
  1319. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  1320. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  1321. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
  1322. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos //READYoó×??ˉ?aê??′DDSTART
  1323. | RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos;
  1324. // Set payload pointer
  1325. NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
  1326. NRF_RADIO->EVENTS_DISABLED = 0; //??3y±ê????
  1327. NRF_RADIO->TASKS_TXEN = 1; //?aê?oó?á?ú2?×??o2ù×÷
  1328. while (NRF_RADIO->EVENTS_END == 0)
  1329. {
  1330. //μè′y·¢?ííê3é
  1331. }
  1332. nrf_gpio_pin_write(rx, 0);
  1333. NRF_RADIO->SHORTS = 0;
  1334. NRF_RADIO->EVENTS_DISABLED = 0U;
  1335. NRF_RADIO->TASKS_DISABLE = 1U;
  1336. while (NRF_RADIO->EVENTS_DISABLED == 0)
  1337. {
  1338. //μè′y1?μ?radio
  1339. }
  1340. NRF_RADIO->EVENTS_READY = 0U;
  1341. // Enable radio and wait for ready
  1342. NRF_RADIO->TASKS_RXEN = 1U;
  1343. NRF_RADIO->PACKETPTR = (uint32_t)&rxbuff[0];
  1344. while (NRF_RADIO->EVENTS_READY == 0U)
  1345. {
  1346. // wait
  1347. }
  1348. nrf_gpio_pin_write(rx, 1);
  1349. NRF_RADIO->EVENTS_END = 0U;
  1350. // Start listening and wait for address received event
  1351. NRF_RADIO->TASKS_START = 1U;
  1352. // Wait for end of packet or buttons state changed
  1353. for (int j = 0; j < 5000; j++)
  1354. {
  1355. if (NRF_RADIO->EVENTS_END == 1)
  1356. break;
  1357. }
  1358. if (NRF_RADIO->CRCSTATUS == 1U)
  1359. {
  1360. for (int i = 0; i < 50; i++)
  1361. {
  1362. BLE_PRINT("%x", rxbuff[i]);
  1363. }
  1364. BLE_PRINT("\r\n ");
  1365. memset(rxbuff, 0, 60);
  1366. }
  1367. else
  1368. {
  1369. BLE_PRINT("E\r\n ");
  1370. }
  1371. }
  1372. nrf_radio_signal_callback_return_param_t call_radio_return_val;
  1373. nrf_radio_signal_callback_return_param_t *call_radio(unsigned char sig)
  1374. {
  1375. nrf_gpio_pin_write(rx, 1);
  1376. radio_evt_conf();
  1377. nrf_gpio_pin_write(rx, 0);
  1378. call_radio_return_val.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END;
  1379. return &call_radio_return_val;
  1380. }
  1381. void radio_session_open(void)
  1382. {
  1383. BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
  1384. }
  1385. void radio_session_open_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1386. {
  1387. BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
  1388. }
  1389. CLI_CMD_REGISTER(radio_s_open, "clear sereen", radio_session_open_pcs);
  1390. void radio_session_close_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1391. {
  1392. BLE_PRINT("error= %d\r\n", sd_radio_session_close());
  1393. }
  1394. CLI_CMD_REGISTER(radio_s_close, "clear sereen", radio_session_close_pcs);
  1395. void radio_request_earliest(void)
  1396. {
  1397. radio_session_open();
  1398. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
  1399. radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_NO_GUARANTEE;
  1400. radio_request_p.params.earliest.length_us = 4000;
  1401. radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
  1402. radio_request_p.params.earliest.timeout_us = 2000;
  1403. BLE_PRINT("radio_request_earliest= %d\r\n", sd_radio_request(&radio_request_p));
  1404. //
  1405. // radio_request_p.request_type=NRF_RADIO_REQ_TYPE_NORMAL;
  1406. // radio_request_p.params.normal.hfclk=NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
  1407. // radio_request_p.params.normal.distance_us=10000;
  1408. // radio_request_p.params.normal.length_us=5000;
  1409. // radio_request_p.params.normal.priority=NRF_RADIO_PRIORITY_NORMAL;
  1410. }
  1411. void radio_request_e_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1412. {
  1413. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
  1414. radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
  1415. radio_request_p.params.earliest.length_us = 5000;
  1416. radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
  1417. radio_request_p.params.earliest.timeout_us = 2000;
  1418. BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
  1419. }
  1420. CLI_CMD_REGISTER(radio_r_e, "clear sereen", radio_request_e_pcs);
  1421. void radio_request_n_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1422. {
  1423. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_NORMAL;
  1424. BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
  1425. }
  1426. CLI_CMD_REGISTER(radio_r_n, "clear sereen", radio_request_n_pcs);
  1427. void Radio_State_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1428. {
  1429. Radio_State();
  1430. }
  1431. CLI_CMD_REGISTER(Radio_State, "clear sereen", Radio_State_pcs);
  1432. void s100_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1433. {
  1434. send_bytes_client(buff, 150);
  1435. }
  1436. CLI_CMD_REGISTER(s100, "clear sereen", s100_pcs);
  1437. #endif