slave.c 51 KB

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