slave.c 49 KB

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