slave.c 50 KB

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