slave.c 49 KB

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