slave.c 50 KB

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