slave.c 51 KB

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