slave.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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,4);
  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_disconnect(void)
  503. {
  504. if (connect_to_client)
  505. sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  506. }
  507. unsigned int slave_update_conn_interval_request(float min_conn_interval, float max_conn_interval)
  508. {
  509. ret_code_t err_code;
  510. ble_gap_conn_params_t bgcp;
  511. if (slave_update_conn_interval_request_sta)
  512. return APP_ERR_BUSY;
  513. if (connect_to_client)
  514. {
  515. slave_update_conn_interval_request_sta = 1;
  516. if ((max_conn_interval > 1.25 * 1599) || (max_conn_interval < min_conn_interval))
  517. return APP_ERR_PARAMERR;
  518. if (min_conn_interval < 7.5f)
  519. return APP_ERR_PARAMERR;
  520. bgcp.max_conn_interval = MSEC_TO_UNITS(max_conn_interval, UNIT_1_25_MS);
  521. bgcp.min_conn_interval = MSEC_TO_UNITS(min_conn_interval, UNIT_1_25_MS);
  522. bgcp.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS);
  523. bgcp.slave_latency = 0;
  524. BLE_PRINT("slave_update_conn_interval_request -> %d \r\n", bgcp.max_conn_interval);
  525. err_code = sd_ble_gap_conn_param_update(m_conn_handle, &bgcp);
  526. APP_ERROR_CHECK(err_code);
  527. return err_code;
  528. }
  529. else
  530. {
  531. return APP_ERR_DISCONN;
  532. }
  533. }
  534. void slave_get_conn_params(ble_gap_conn_params_t *p)
  535. {
  536. p->conn_sup_timeout = slave_conn_params.conn_sup_timeout;
  537. p->max_conn_interval = slave_conn_params.max_conn_interval;
  538. p->min_conn_interval = slave_conn_params.min_conn_interval;
  539. p->slave_latency = slave_conn_params.slave_latency;
  540. }
  541. void slave_adv_init(void)
  542. {
  543. gap_params_init(); //添加的GAP初始化
  544. conn_params_init(); //添加的连接参数初始化
  545. advertising_init(); //添加广播初始化
  546. }
  547. static signed char rssi = 0;
  548. signed char slave_get_rssi(void)
  549. {
  550. unsigned char channel;
  551. if (connect_to_client == 0)
  552. return 1;
  553. sd_ble_gap_rssi_get(m_conn_handle, &rssi, &channel);
  554. // BLE_PRINT("rssi= %d channel=%d\r\n", rssi, channel);
  555. return rssi;
  556. }
  557. #if DEBUGBLE
  558. #define led 13
  559. #define tx 11 //1.1
  560. #define rx 12
  561. //#define LS -1611916254 //开发板
  562. //#define RS -889050188
  563. //#define LS 97376119 //31
  564. //#define RS 627878688 //32
  565. #define LS -1087551583 //1.1
  566. #define RS -957332282 //1.1
  567. #define PS -1372482754 //usb
  568. unsigned char buff[255];
  569. char start = 0;
  570. void host_r(unsigned char *p, int len)
  571. {
  572. BLE_PRINT("hr : %d,0x%x\r\n", len, p[0]);
  573. if (p[0] == 0xbb)
  574. {
  575. BLE_PRINT("hr -------------: 0xbb\r\n");
  576. SEGGER_RTT_Write(0, &p[1], len);
  577. }
  578. if (p[0] == 0xcc)
  579. {
  580. BLE_PRINT("hr -------------: 0xcc\r\n");
  581. }
  582. }
  583. #define TIMER_TICK 25
  584. #define TCUN 1000
  585. unsigned short cun = 0;
  586. unsigned short ts = 0;
  587. unsigned short rec[5] = {0};
  588. unsigned short recrtc[5] = {0};
  589. unsigned int rtc_cun = 0;
  590. void slave_r(unsigned char *p, int len)
  591. {
  592. if (p[0] == 0xaa)
  593. {
  594. cun++;
  595. ts = p[1];
  596. ts = ts << 8;
  597. ts += p[2];
  598. if (ts >= 1)
  599. {
  600. start = 1;
  601. rtc_cun = NRF_RTC2->COUNTER;
  602. }
  603. if (ts == TCUN)
  604. start = 0;
  605. if (start)
  606. {
  607. if (NRF_RTC2->COUNTER - rtc_cun < 1 * TIMER_TICK)
  608. recrtc[0]++;
  609. if ((NRF_RTC2->COUNTER - rtc_cun >= 1 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 2 * TIMER_TICK))
  610. recrtc[1]++;
  611. if ((NRF_RTC2->COUNTER - rtc_cun >= 2 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 3 * TIMER_TICK))
  612. recrtc[2]++;
  613. if ((NRF_RTC2->COUNTER - rtc_cun >= 3 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 4 * TIMER_TICK))
  614. recrtc[3]++;
  615. if (NRF_RTC2->COUNTER - rtc_cun > 4 * TIMER_TICK)
  616. recrtc[4]++;
  617. rtc_cun = NRF_RTC2->COUNTER;
  618. }
  619. BLE_PRINT("sr : %d\r\n", ts);
  620. }
  621. if (p[0] == 0xbb)
  622. {
  623. buff[0] = 0xbb;
  624. 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]);
  625. send_bytes_server(buff, leng);
  626. }
  627. if (p[0] == 0xcc)
  628. {
  629. BLE_PRINT("sr -------------: 0xcc\r\n");
  630. memset(rec, 0, 10);
  631. memset(recrtc, 0, 10);
  632. send_bytes_server(p, 3);
  633. }
  634. }
  635. #include "cli.h"
  636. nrf_radio_request_t radio_request_p;
  637. APP_TIMER_DEF(s_Timer);
  638. #define TEST_PERIOD APP_TIMER_TICKS(TIMER_TICK)
  639. unsigned short tims = 0;
  640. unsigned short stp = 0;
  641. void s_TimerCallback(void *arg)
  642. {
  643. if ((tims > 0) && (tims <= TCUN))
  644. {
  645. buff[0] = 0xaa;
  646. buff[1] = tims >> 8;
  647. buff[2] = tims;
  648. send_bytes_client(buff, 100);
  649. BLE_PRINT("send : %d\r\n", tims);
  650. tims++;
  651. }
  652. if (start)
  653. {
  654. if (cun > 4)
  655. cun = 4;
  656. rec[cun]++;
  657. cun = 0;
  658. }
  659. //发数据给手机
  660. if (*NRF_FICR->DEVICEID == LS) //左边鞋
  661. {
  662. if (start)
  663. {
  664. buff[0] = 0xaa;
  665. buff[1] = stp >> 8;
  666. buff[2] = stp;
  667. send_bytes_client(buff, 100);
  668. }
  669. stp++;
  670. }
  671. // nrf_gpio_pin_toggle(rx);
  672. //nrf_gpio_pin_write(rx, 0);
  673. // BLE_PRINT("error= %d\r\n", sd_radio_request(&radio_request_p));
  674. }
  675. void Radio_State(void)
  676. {
  677. switch (NRF_RADIO->STATE)
  678. {
  679. case RADIO_STATE_STATE_Disabled:
  680. BLE_PRINT("RADIO_STATE_STATE_Disabled\r\n");
  681. break;
  682. case RADIO_STATE_STATE_RxRu:
  683. BLE_PRINT("RADIO_STATE_STATE_RxRu\r\n");
  684. break;
  685. case RADIO_STATE_STATE_RxIdle:
  686. BLE_PRINT("RADIO_STATE_STATE_RxIdle\r\n");
  687. break;
  688. case RADIO_STATE_STATE_Rx:
  689. BLE_PRINT("RADIO_STATE_STATE_Rx\r\n");
  690. break;
  691. case RADIO_STATE_STATE_RxDisable:
  692. BLE_PRINT("RADIO_STATE_STATE_RxDisable\r\n");
  693. break;
  694. case RADIO_STATE_STATE_TxRu:
  695. BLE_PRINT("RADIO_STATE_STATE_TxRu\r\n");
  696. break;
  697. case RADIO_STATE_STATE_TxIdle:
  698. BLE_PRINT("RADIO_STATE_STATE_TxIdle\r\n");
  699. break;
  700. case RADIO_STATE_STATE_Tx:
  701. BLE_PRINT("RADIO_STATE_STATE_Tx\r\n");
  702. break;
  703. case RADIO_STATE_STATE_TxDisable:
  704. BLE_PRINT("RADIO_STATE_STATE_TxDisable\r\n");
  705. break;
  706. }
  707. }
  708. void unoioo(void)
  709. {
  710. Ble_update_conn_interval(7.5,7.5);
  711. }
  712. void unoioo_s(void)
  713. {
  714. slave_update_conn_interval_request(30, 30);
  715. scan_start();
  716. }
  717. void unoioo_s_d(void)
  718. {
  719. host_disconnect();
  720. scan_start();
  721. }
  722. void rtc_config(void)
  723. {
  724. NRF_RTC2->PRESCALER = 0; //每一毫秒计数器加1,1024us
  725. NRF_RTC2->TASKS_START = 1;
  726. }
  727. #include "nrf_drv_timer.h"
  728. void radio_evt_conf(void);
  729. const nrf_drv_timer_t TIMER_RADIO = NRF_DRV_TIMER_INSTANCE(2);
  730. void timer_led_event_handler(nrf_timer_event_t event_type, void *p_context)
  731. {
  732. if (*NRF_FICR->DEVICEID == LS) //左边鞋
  733. {
  734. switch (event_type)
  735. {
  736. case NRF_TIMER_EVENT_COMPARE0: //320
  737. sd_radio_request(&radio_request_p);
  738. NRF_PPI->CHEN &= (~(PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos));
  739. break;
  740. case NRF_TIMER_EVENT_COMPARE1: //324
  741. sd_radio_request(&radio_request_p);
  742. break;
  743. case NRF_TIMER_EVENT_COMPARE2: //328
  744. sd_radio_request(&radio_request_p);
  745. break;
  746. case NRF_TIMER_EVENT_COMPARE3: //332
  747. NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
  748. break;
  749. default:
  750. //Do nothing.
  751. break;
  752. }
  753. }
  754. if (*NRF_FICR->DEVICEID == RS) //右边鞋
  755. {
  756. switch (event_type)
  757. {
  758. case NRF_TIMER_EVENT_COMPARE0: //320
  759. nrf_gpio_pin_write(tx, 1);
  760. break;
  761. case NRF_TIMER_EVENT_COMPARE1: //324
  762. nrf_gpio_pin_write(tx, 0);
  763. break;
  764. case NRF_TIMER_EVENT_COMPARE2: //328
  765. break;
  766. case NRF_TIMER_EVENT_COMPARE3: //332
  767. break;
  768. default:
  769. //Do nothing.
  770. break;
  771. }
  772. }
  773. }
  774. void timer_config(void)
  775. {
  776. uint32_t time_us = 5000; //Time(in miliseconds) between consecutive compare events.
  777. uint32_t time_ticks;
  778. uint32_t err_code = NRF_SUCCESS;
  779. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  780. err_code = nrf_drv_timer_init(&TIMER_RADIO, &timer_cfg, timer_led_event_handler);
  781. APP_ERROR_CHECK(err_code);
  782. if (*NRF_FICR->DEVICEID == LS) //左边鞋
  783. {
  784. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
  785. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
  786. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 10000);
  787. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, 0, true);
  788. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 20000);
  789. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL2, time_ticks, 0, true);
  790. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, 29000);
  791. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL3, time_ticks, NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK, true);
  792. }
  793. if (*NRF_FICR->DEVICEID == RS) //右边鞋
  794. {
  795. time_us = 1000;
  796. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
  797. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
  798. time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 9000 + 1);
  799. nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
  800. nrf_drv_timer_enable(&TIMER_RADIO);
  801. }
  802. // nrf_drv_timer_enable(&TIMER_RADIO);
  803. }
  804. void ppi_set(void)
  805. {
  806. NRF_PPI->CH[0].EEP = (unsigned int)(&NRF_TIMER0->EVENTS_COMPARE[0]);
  807. NRF_PPI->CH[0].TEP = (unsigned int)(&NRF_TIMER2->TASKS_START);
  808. NRF_PPI->CH[1].EEP = (unsigned int)(&NRF_TIMER2->EVENTS_COMPARE[3]);
  809. NRF_PPI->CH[1].TEP = (unsigned int)(&NRF_TIMER2->TASKS_SHUTDOWN);
  810. NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) |
  811. (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
  812. }
  813. extern void USR_Init(void);
  814. extern void USR_Process(void);
  815. extern void TIME_Init(void);
  816. extern char Target_scan[];
  817. unsigned char txbuff[300] = {0x0a, 0x03, 0, 0, 2, 3, 4, 5, 6, 0, 8, 9};
  818. unsigned char rxbuff[300] = {0};
  819. void radio_init_R(void)
  820. {
  821. NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
  822. /* Start 16 MHz crystal oscillator */
  823. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  824. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  825. /* Wait for the external oscillator to start up */
  826. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  827. {
  828. // Do nothing.
  829. }
  830. // Radio config
  831. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  832. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  833. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  834. NRF_RADIO->PREFIX0 = 0xC3438303;
  835. NRF_RADIO->PREFIX1 = 0xE3630023;
  836. NRF_RADIO->BASE0 = 0x80C4A2E6;
  837. NRF_RADIO->BASE1 = 0x91D5B3F7;
  838. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  839. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  840. NRF_RADIO->PCNF0 = 0X00030006;
  841. NRF_RADIO->PCNF1 = 0X01040020;
  842. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  843. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  844. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
  845. NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
  846. }
  847. #include "nrf_drv_rtc.h"
  848. const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
  849. unsigned int countevt = 0;
  850. void radio_connect(void)
  851. {
  852. NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
  853. countevt = 1;
  854. nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + countevt * 0.009 * 32768, true);
  855. countevt++;
  856. }
  857. void RADIO_IRQHandler(void)
  858. {
  859. if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk))
  860. {
  861. NRF_RADIO->EVENTS_READY = 0U;
  862. BLE_PRINT("a");
  863. }
  864. if (NRF_RADIO->EVENTS_ADDRESS && (NRF_RADIO->INTENSET & RADIO_INTENSET_ADDRESS_Msk))
  865. {
  866. NRF_RADIO->EVENTS_ADDRESS = 0U;
  867. BLE_PRINT("b");
  868. }
  869. if (NRF_RADIO->EVENTS_PAYLOAD && (NRF_RADIO->INTENSET & RADIO_INTENSET_PAYLOAD_Msk))
  870. {
  871. NRF_RADIO->EVENTS_PAYLOAD = 0U;
  872. BLE_PRINT("c");
  873. }
  874. if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
  875. {
  876. NRF_RADIO->EVENTS_END = 0U;
  877. // NRF_LOG_INFO("d");
  878. if (NRF_RADIO->STATE >= 5UL)
  879. {
  880. NRF_RADIO->EVENTS_DISABLED = 0U;
  881. NRF_RADIO->TASKS_DISABLE = 1U;
  882. nrf_gpio_pin_write(tx, 0);
  883. // BLE_PRINT("Tx end\r\n");
  884. }
  885. else
  886. {
  887. //收到数据后先切换为发送模式
  888. NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
  889. NRF_RADIO->PACKETPTR = (unsigned int)txbuff;
  890. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
  891. nrf_gpio_pin_write(tx, 0);
  892. NRF_RADIO->EVENTS_DISABLED = 0U;
  893. NRF_RADIO->TASKS_DISABLE = 1U;
  894. while (NRF_RADIO->EVENTS_DISABLED == 0)
  895. ;
  896. NRF_RADIO->TASKS_TXEN = 1;
  897. nrf_gpio_pin_write(tx, 1);
  898. nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + 0.010 * 32768, true);
  899. nrf_drv_rtc_cc_set(&rtc, 1, NRF_RTC0->CC[2] + 0.018 * 32768, true);
  900. for (int i = 0; i < 50; i++)
  901. {
  902. BLE_PRINT("%x", rxbuff[i]);
  903. }
  904. BLE_PRINT("Rx\r\n", rxbuff[1]);
  905. }
  906. }
  907. if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
  908. {
  909. NRF_RADIO->EVENTS_DISABLED = 0U;
  910. BLE_PRINT("e");
  911. }
  912. if (NRF_RADIO->EVENTS_DEVMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMATCH_Msk))
  913. {
  914. NRF_RADIO->EVENTS_DEVMATCH = 0U;
  915. BLE_PRINT("f");
  916. }
  917. if (NRF_RADIO->EVENTS_DEVMISS && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMISS_Msk))
  918. {
  919. NRF_RADIO->EVENTS_DEVMISS = 0U;
  920. BLE_PRINT("g");
  921. }
  922. if (NRF_RADIO->EVENTS_RSSIEND && (NRF_RADIO->INTENSET & RADIO_INTENSET_RSSIEND_Msk))
  923. {
  924. NRF_RADIO->EVENTS_RSSIEND = 0U;
  925. BLE_PRINT("h");
  926. }
  927. if (NRF_RADIO->EVENTS_BCMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_BCMATCH_Msk))
  928. {
  929. NRF_RADIO->EVENTS_BCMATCH = 0U;
  930. BLE_PRINT("i");
  931. }
  932. if (NRF_RADIO->EVENTS_CRCOK && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCOK_Msk))
  933. {
  934. NRF_RADIO->EVENTS_CRCOK = 0U;
  935. BLE_PRINT("k");
  936. }
  937. if (NRF_RADIO->EVENTS_CRCERROR && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCERROR_Msk))
  938. {
  939. NRF_RADIO->EVENTS_CRCERROR = 0U;
  940. BLE_PRINT("l");
  941. }
  942. NVIC_ClearPendingIRQ(RADIO_IRQn);
  943. }
  944. void radio_scan_start(void)
  945. {
  946. NRF_RADIO->SHORTS = 0;
  947. NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_RXEN_Msk;
  948. NRF_RADIO->SHORTS |= RADIO_SHORTS_READY_START_Msk;
  949. NRF_RADIO->SHORTS |= RADIO_SHORTS_END_START_Msk;
  950. NRF_RADIO->INTENSET |= RADIO_INTENSET_END_Msk;
  951. NRF_RADIO->TASKS_RXEN = 1;
  952. NRF_RADIO->EVENTS_READY = 0;
  953. while (NRF_RADIO->EVENTS_READY == 0)
  954. {
  955. }
  956. NRF_RADIO->TASKS_START = 1;
  957. NVIC_EnableIRQ(RADIO_IRQn);
  958. Radio_State();
  959. }
  960. static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
  961. {
  962. switch (int_type)
  963. {
  964. case NRFX_RTC_INT_COMPARE0:
  965. nrf_gpio_pin_write(tx, 1);
  966. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
  967. NRF_RADIO->PACKETPTR = (unsigned int)rxbuff;
  968. NRF_RADIO->TASKS_RXEN = 1U;
  969. break;
  970. case NRFX_RTC_INT_COMPARE1:
  971. Radio_State();
  972. BLE_PRINT("NRFX_RTC_INT_COMPARE1\r\n");
  973. break;
  974. case NRFX_RTC_INT_COMPARE2:
  975. break;
  976. case NRFX_RTC_INT_COMPARE3:
  977. break;
  978. case NRFX_RTC_INT_TICK:
  979. break;
  980. case NRFX_RTC_INT_OVERFLOW:
  981. nrf_drv_rtc_counter_clear(&rtc);
  982. break;
  983. }
  984. }
  985. /**********************************************************
  986. * 函数名字:rtc_config
  987. * 函数作用:rtc驱动初始化和设置
  988. * 函数参数:无
  989. * 函数返回值:无
  990. ***********************************************************/
  991. void radio_rtc_config(void)
  992. {
  993. uint32_t err_code;
  994. NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
  995. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  996. NRF_CLOCK->TASKS_LFCLKSTART = 1;
  997. while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
  998. {
  999. // Do nothing.
  1000. }
  1001. //Initialize RTC instance
  1002. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  1003. config.prescaler = 0; //4095;????????=32768/(config.prescaler+1)Hz;
  1004. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
  1005. APP_ERROR_CHECK(err_code);
  1006. //Enable tick event & interrupt
  1007. // nrf_drv_rtc_tick_enable(&rtc, true);
  1008. //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
  1009. // err_code = nrf_drv_rtc_cc_set(&rtc, 0, 8, true);
  1010. // APP_ERROR_CHECK(err_code);
  1011. //Power on RTC instance
  1012. nrf_drv_rtc_enable(&rtc);
  1013. }
  1014. int main(void)
  1015. {
  1016. unsigned int error = 0;
  1017. unsigned int rtctemp = 0;
  1018. unsigned int start = 0;
  1019. unsigned int radio_dis_cun = 0;
  1020. unsigned int radio_dis_cun_rtc = 0;
  1021. nrf_gpio_cfg_output(led);
  1022. nrf_gpio_pin_write(led, 1);
  1023. nrf_gpio_cfg_output(tx);
  1024. nrf_gpio_pin_write(tx, 0);
  1025. nrf_gpio_cfg_output(8);
  1026. nrf_gpio_pin_write(8, 0);
  1027. nrf_gpio_cfg_output(rx);
  1028. nrf_gpio_pin_write(rx, 0);
  1029. BLE_PRINT("NRF_FICR->DEVICEID : %d\r\n", *NRF_FICR->DEVICEID);
  1030. if (*NRF_FICR->DEVICEID == RS) //右边鞋
  1031. {
  1032. #if 1
  1033. slave_init(host_r);
  1034. #else
  1035. radio_init_R();
  1036. radio_rtc_config();
  1037. radio_scan_start();
  1038. #endif
  1039. BLE_PRINT("you \r\n");
  1040. }
  1041. if (*NRF_FICR->DEVICEID == LS) //左边鞋
  1042. {
  1043. #if 0
  1044. Target_scan[0]=0xe3; //3132
  1045. Target_scan[1]=0x3f;
  1046. Target_scan[2]=0xd9;
  1047. Target_scan[3]=0x0d;
  1048. Target_scan[4]=0x0e;
  1049. Target_scan[5]=0xc6;
  1050. 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]);
  1051. // Target_scan[0]=0x3C; //开发板
  1052. // Target_scan[1]=0x83;
  1053. // Target_scan[2]=0xCF;
  1054. // Target_scan[3]=0x49;
  1055. // Target_scan[4]=0x50;
  1056. // Target_scan[5]=0xE1;
  1057. //
  1058. #endif
  1059. Ble_Host_Connectd_Evt_Regist(unoioo);
  1060. Ble_Slave_Connectd_Evt_Regist(unoioo_s);
  1061. Ble_Slave_Disconn_Evt_Regist(unoioo_s_d);
  1062. // extern void radio_request_earliest(void);
  1063. // Ble_Slave_Connectd_Evt_Regist(radio_request_earliest);
  1064. slave_init(host_r);
  1065. host_init(slave_r);
  1066. // timer_config();
  1067. BLE_PRINT("zuo \r\n");
  1068. }
  1069. if (*NRF_FICR->DEVICEID == PS) //手机
  1070. {
  1071. #if 0
  1072. Target_scan[0] = 0x21;
  1073. Target_scan[1] = 0x8a;
  1074. Target_scan[2] = 0x4f;
  1075. Target_scan[3] = 0x61;
  1076. Target_scan[4] = 0xcb;
  1077. Target_scan[5] = 0xe8;
  1078. #endif
  1079. host_set_scan_name("SH_13EC", 7);
  1080. BLE_PRINT("shou \r\n");
  1081. host_init(slave_r);
  1082. scan_start();
  1083. }
  1084. rtc_config();
  1085. for (int i = 1; i < 200; i++)
  1086. {
  1087. buff[i] = i + 0x30;
  1088. // txbuff[i]=i;
  1089. }
  1090. app_timer_create(&s_Timer, APP_TIMER_MODE_REPEATED, s_TimerCallback);
  1091. app_timer_start(s_Timer, TEST_PERIOD, NULL);
  1092. // ppi_set();
  1093. while (1)
  1094. {
  1095. cli_process(&clirtt);
  1096. if (NRF_SUCCESS == sd_evt_get(&error))
  1097. {
  1098. BLE_PRINT("shou \r\n");
  1099. }
  1100. // if (*NRF_FICR->DEVICEID == LS) //左边鞋
  1101. {
  1102. if (NRF_RADIO->STATE == RADIO_STATE_STATE_Disabled)
  1103. {
  1104. nrf_gpio_pin_write(tx, 0);
  1105. }
  1106. else
  1107. {
  1108. nrf_gpio_pin_write(tx, 1);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. void host_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1114. {
  1115. host_init(slave_r);
  1116. }
  1117. CLI_CMD_REGISTER(host_init, "clear sereen", host_init_pcs);
  1118. void hsb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1119. {
  1120. send_bytes_server(buff, 200);
  1121. }
  1122. CLI_CMD_REGISTER(hsb, "clear sereen", hsb_pcs);
  1123. void send_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1124. {
  1125. tims = 1;
  1126. }
  1127. CLI_CMD_REGISTER(send, "clear sereen", send_pcs);
  1128. void scc_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1129. {
  1130. buff[0] = 0xcc;
  1131. send_bytes_client(buff, 6);
  1132. }
  1133. CLI_CMD_REGISTER(scc, "clear sereen", scc_pcs);
  1134. void sbb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1135. {
  1136. buff[0] = 0xbb;
  1137. send_bytes_client(buff, 6);
  1138. }
  1139. CLI_CMD_REGISTER(sbb, "clear sereen", sbb_pcs);
  1140. void hcb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1141. {
  1142. send_bytes_client(buff, 200);
  1143. }
  1144. CLI_CMD_REGISTER(hcb, "clear sereen", hcb_pcs);
  1145. void slave_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1146. {
  1147. slave_init(host_r);
  1148. }
  1149. CLI_CMD_REGISTER(slave_init, "clear sereen", slave_init_pcs);
  1150. void bleupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1151. {
  1152. unsigned int error = 0;
  1153. error = Ble_update_conn_interval(10, 10);
  1154. cli_printf(p_cli, "err %d", error);
  1155. }
  1156. CLI_CMD_REGISTER(bleupdata10, "clear sereen", bleupdata_pcs);
  1157. void bleupdata_1000pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1158. {
  1159. unsigned int error = 0;
  1160. error = Ble_update_conn_interval(1000, 1000);
  1161. cli_printf(p_cli, "err %d", error);
  1162. }
  1163. CLI_CMD_REGISTER(bleupdata1000, "clear sereen", bleupdata_1000pcs);
  1164. void slaveupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1165. {
  1166. unsigned int error =
  1167. slave_update_conn_interval_request(40, 40);
  1168. cli_printf(p_cli, "err %d", error);
  1169. }
  1170. CLI_CMD_REGISTER(slaveupdata, "clear sereen", slaveupdata_pcs);
  1171. void conn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1172. {
  1173. if (argc == 1)
  1174. {
  1175. host_set_scan_name(argv[0], strlen(argv[0]));
  1176. host_init(slave_r);
  1177. }
  1178. else
  1179. cli_printf(p_cli, "err ");
  1180. }
  1181. CLI_CMD_REGISTER(conn, "clear sereen", conn_pcs);
  1182. void scan_name_set_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1183. {
  1184. if (argc == 1)
  1185. {
  1186. host_set_scan_name(argv[0], strlen(argv[0]));
  1187. }
  1188. else
  1189. cli_printf(p_cli, "err ");
  1190. }
  1191. CLI_CMD_REGISTER(scan_name_set, "clear sereen", scan_name_set_pcs);
  1192. void systemreset_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1193. {
  1194. NVIC_SystemReset();
  1195. }
  1196. CLI_CMD_REGISTER(systemreset, "clear sereen", systemreset_pcs);
  1197. void scanstart_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1198. {
  1199. scan_start();
  1200. }
  1201. CLI_CMD_REGISTER(scanstart, "clear sereen", scanstart_pcs);
  1202. void slave_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1203. {
  1204. slave_disconnect();
  1205. }
  1206. CLI_CMD_REGISTER(slave_dec, "clear sereen", slave_dec_pcs);
  1207. void host_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1208. {
  1209. host_disconnect();
  1210. }
  1211. CLI_CMD_REGISTER(host_dec, "clear sereen", host_dec_pcs);
  1212. void getconn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1213. {
  1214. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.min_conn_interval);
  1215. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.max_conn_interval);
  1216. BLE_PRINT("slave_latency : %d\r\n", slave_conn_params.slave_latency);
  1217. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", slave_conn_params.conn_sup_timeout);
  1218. extern ble_gap_conn_params_t host_conn_params;
  1219. BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", host_conn_params.min_conn_interval);
  1220. BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", host_conn_params.max_conn_interval);
  1221. BLE_PRINT("slave_latency : %d\r\n", host_conn_params.slave_latency);
  1222. BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", host_conn_params.conn_sup_timeout);
  1223. slave_set_adv_name("123456", 6);
  1224. gap_params_init();
  1225. while (slave_isconnect() == 1)
  1226. {
  1227. }
  1228. BLE_PRINT("123456555");
  1229. advertising_start();
  1230. BLE_PRINT("4554564");
  1231. }
  1232. CLI_CMD_REGISTER(getconn, "clear sereen", getconn_pcs);
  1233. void slave_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1234. {
  1235. slave_get_rssi();
  1236. }
  1237. CLI_CMD_REGISTER(slave_get_rssi, "clear sereen", slave_get_rssi_pcs);
  1238. void host_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1239. {
  1240. host_get_rssi();
  1241. }
  1242. CLI_CMD_REGISTER(host_get_rssi, "clear sereen", host_get_rssi_pcs);
  1243. int teg = 0;
  1244. unsigned int rtccc = 0;
  1245. void radio_evt_conf(void)
  1246. {
  1247. NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
  1248. /* Start 16 MHz crystal oscillator */
  1249. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  1250. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  1251. txbuff[1] = NRF_RTC0->COUNTER;
  1252. txbuff[2] = teg;
  1253. /* Wait for the external oscillator to start up */
  1254. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  1255. {
  1256. // Do nothing.
  1257. }
  1258. // Radio config
  1259. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  1260. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  1261. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  1262. NRF_RADIO->PREFIX0 = 0xC3438303;
  1263. NRF_RADIO->PREFIX1 = 0xE3630023;
  1264. NRF_RADIO->BASE0 = 0x80C4A2E6;
  1265. NRF_RADIO->BASE1 = 0x91D5B3F7;
  1266. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  1267. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  1268. NRF_RADIO->PCNF0 = 0X00030006;
  1269. NRF_RADIO->PCNF1 = 0X01040020;
  1270. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  1271. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  1272. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
  1273. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos //READY后自动开始执行START
  1274. | RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos;
  1275. // Set payload pointer
  1276. NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
  1277. NRF_RADIO->EVENTS_DISABLED = 0; //清除标志位
  1278. NRF_RADIO->TASKS_TXEN = 1; //开始后会内部自己操作
  1279. while (NRF_RADIO->EVENTS_END == 0)
  1280. {
  1281. //等待发送完成
  1282. }
  1283. nrf_gpio_pin_write(rx, 0);
  1284. NRF_RADIO->SHORTS = 0;
  1285. NRF_RADIO->EVENTS_DISABLED = 0U;
  1286. NRF_RADIO->TASKS_DISABLE = 1U;
  1287. while (NRF_RADIO->EVENTS_DISABLED == 0)
  1288. {
  1289. //等待关掉radio
  1290. }
  1291. NRF_RADIO->EVENTS_READY = 0U;
  1292. // Enable radio and wait for ready
  1293. NRF_RADIO->TASKS_RXEN = 1U;
  1294. NRF_RADIO->PACKETPTR = (uint32_t)&rxbuff[0];
  1295. while (NRF_RADIO->EVENTS_READY == 0U)
  1296. {
  1297. // wait
  1298. }
  1299. nrf_gpio_pin_write(rx, 1);
  1300. NRF_RADIO->EVENTS_END = 0U;
  1301. // Start listening and wait for address received event
  1302. NRF_RADIO->TASKS_START = 1U;
  1303. // Wait for end of packet or buttons state changed
  1304. for (int j = 0; j < 5000; j++)
  1305. {
  1306. if (NRF_RADIO->EVENTS_END == 1)
  1307. break;
  1308. }
  1309. if (NRF_RADIO->CRCSTATUS == 1U)
  1310. {
  1311. for (int i = 0; i < 50; i++)
  1312. {
  1313. BLE_PRINT("%x", rxbuff[i]);
  1314. }
  1315. BLE_PRINT("\r\n ");
  1316. memset(rxbuff, 0, 60);
  1317. }
  1318. else
  1319. {
  1320. BLE_PRINT("E\r\n ");
  1321. }
  1322. }
  1323. nrf_radio_signal_callback_return_param_t call_radio_return_val;
  1324. nrf_radio_signal_callback_return_param_t *call_radio(unsigned char sig)
  1325. {
  1326. nrf_gpio_pin_write(rx, 1);
  1327. radio_evt_conf();
  1328. nrf_gpio_pin_write(rx, 0);
  1329. call_radio_return_val.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END;
  1330. return &call_radio_return_val;
  1331. }
  1332. void radio_session_open(void)
  1333. {
  1334. BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
  1335. }
  1336. void radio_session_open_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1337. {
  1338. BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
  1339. }
  1340. CLI_CMD_REGISTER(radio_s_open, "clear sereen", radio_session_open_pcs);
  1341. void radio_session_close_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1342. {
  1343. BLE_PRINT("error= %d\r\n", sd_radio_session_close());
  1344. }
  1345. CLI_CMD_REGISTER(radio_s_close, "clear sereen", radio_session_close_pcs);
  1346. void radio_request_earliest(void)
  1347. {
  1348. radio_session_open();
  1349. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
  1350. radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_NO_GUARANTEE;
  1351. radio_request_p.params.earliest.length_us = 4000;
  1352. radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
  1353. radio_request_p.params.earliest.timeout_us = 2000;
  1354. BLE_PRINT("radio_request_earliest= %d\r\n", sd_radio_request(&radio_request_p));
  1355. //
  1356. // radio_request_p.request_type=NRF_RADIO_REQ_TYPE_NORMAL;
  1357. // radio_request_p.params.normal.hfclk=NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
  1358. // radio_request_p.params.normal.distance_us=10000;
  1359. // radio_request_p.params.normal.length_us=5000;
  1360. // radio_request_p.params.normal.priority=NRF_RADIO_PRIORITY_NORMAL;
  1361. }
  1362. void radio_request_e_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1363. {
  1364. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
  1365. radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
  1366. radio_request_p.params.earliest.length_us = 5000;
  1367. radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
  1368. radio_request_p.params.earliest.timeout_us = 2000;
  1369. BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
  1370. }
  1371. CLI_CMD_REGISTER(radio_r_e, "clear sereen", radio_request_e_pcs);
  1372. void radio_request_n_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1373. {
  1374. radio_request_p.request_type = NRF_RADIO_REQ_TYPE_NORMAL;
  1375. BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
  1376. }
  1377. CLI_CMD_REGISTER(radio_r_n, "clear sereen", radio_request_n_pcs);
  1378. void Radio_State_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1379. {
  1380. Radio_State();
  1381. }
  1382. CLI_CMD_REGISTER(Radio_State, "clear sereen", Radio_State_pcs);
  1383. void s100_pcs(cli_t *p_cli, unsigned short argc, char **argv)
  1384. {
  1385. send_bytes_client(buff, 150);
  1386. }
  1387. CLI_CMD_REGISTER(s100, "clear sereen", s100_pcs);
  1388. #endif