123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623 |
- #include "ble_comm.h"
- #include "ble_advertising.h"
- #include "ble_conn_params.h"
- #include "nrf_ble_qwr.h"
- #include "nrf_fstorage.h"
- #include "nrf_soc.h"
- #include "ble_nus.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "app_flash.h"
- // <<< Use Configuration Wizard in Context Menu >>>\r\n
- #define APP_ADV_INTERVAL 320 /**< The advertising interval (in units of 0.625 ms). This value corresponds to 187.5 ms. */
- #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
- #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). */
- #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). */
- #define MAX_CONN_PARAMS_UPDATE_COUNT 1
- static char DEVICE_NAME[TARFET_LEN_MAX] = "SH";
- #if USE_LADDR == 1
- char BleReallyName[TARFET_LEN_MAX] = {0};
- #endif
- #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. */
- #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. */
- #define SLAVE_LATENCY 0 /**< Slave latency. */
- #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
- #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
- static ble_uuid_t m_adv_uuids[] =
- {
- {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};
- static unsigned char connect_to_client = 0;
- static Ble_receive_handler_t Rec_h = NULL;
- BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);
- BLE_ADVERTISING_DEF(m_advertising);
- NRF_BLE_QWRS_DEF(m_qwr, NRF_SDH_BLE_TOTAL_LINK_COUNT);
- uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
- ble_gap_conn_params_t slave_conn_params = {0};
- static void nrf_qwr_error_handler(uint32_t nrf_error) //?óáDD′′í?ó2ù×÷
- {
- APP_ERROR_HANDLER(nrf_error);
- }
- //′ó BLE ?óêüêy?Y
- static void nus_data_handler(ble_nus_evt_t *p_evt)
- {
- if (p_evt->type == BLE_NUS_EVT_RX_DATA)
- {
- Rec_h((unsigned char *)(p_evt->params.rx_data.p_data), p_evt->params.rx_data.length);
- }
- }
- static void services_init(void) //·t??3?ê??ˉ
- {
- uint32_t err_code;
- ble_nus_init_t nus_init;
- nrf_ble_qwr_init_t qwr_init = {0};
- // Initialize Queued Write Module.
- qwr_init.error_handler = nrf_qwr_error_handler;
- for (uint32_t i = 0; i < NRF_SDH_BLE_TOTAL_LINK_COUNT; i++)
- {
- err_code = nrf_ble_qwr_init(&m_qwr[i], &qwr_init);
- APP_ERROR_CHECK(err_code);
- }
- // Initialize NUS.
- memset(&nus_init, 0, sizeof(nus_init));
- nus_init.data_handler = nus_data_handler;
- err_code = ble_nus_init(&m_nus, &nus_init);
- APP_ERROR_CHECK(err_code);
- }
- static void on_adv_evt(ble_adv_evt_t ble_adv_evt) //1?2¥ê??t
- {
- switch (ble_adv_evt)
- {
- case BLE_ADV_EVT_FAST:
- {
- BLE_PRINT("Fast advertising.\r\n");
- }
- break;
- case BLE_ADV_EVT_IDLE:
- {
- BLE_PRINT("on_adv_evt->BLE_ADV_EVT_IDLE\r\n");
- ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST); //?aê?1?2¥
- APP_ERROR_CHECK(err_code);
- }
- break;
- default:
- // No implementation needed.
- break;
- }
- }
- static void advertising_init(void)
- {
- uint32_t err_code;
- ble_advertising_init_t init;
- int8_t txpower = 4;
- memset(&init, 0, sizeof(init));
- init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
- init.advdata.include_appearance = false;
- init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
- init.advdata.p_tx_power_level = &txpower;
- init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
- init.srdata.uuids_complete.p_uuids = m_adv_uuids;
- init.config.ble_adv_fast_enabled = true;
- init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
- init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
- init.evt_handler = on_adv_evt;
- err_code = ble_advertising_init(&m_advertising, &init);
- APP_ERROR_CHECK(err_code);
- ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
- }
- static void conn_params_error_handler(uint32_t nrf_error)
- {
- APP_ERROR_HANDLER(nrf_error);
- }
- static void conn_params_init(void)
- {
- ret_code_t err_code;
- ble_conn_params_init_t cp_init;
- memset(&cp_init, 0, sizeof(cp_init));
- cp_init.p_conn_params = NULL;
- cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
- cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
- cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
- cp_init.start_on_notify_cccd_handle = BLE_CONN_HANDLE_INVALID; // Start upon connection.
- cp_init.disconnect_on_fail = true;
- cp_init.evt_handler = NULL; // Ignore events.
- cp_init.error_handler = conn_params_error_handler;
- err_code = ble_conn_params_init(&cp_init);
- APP_ERROR_CHECK(err_code);
- }
- void advertising_start(void)
- {
- ret_code_t err_code;
- err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST); //同时开始广播
- DEBUG_LOG("advertising_start !\r\n");
- if(NRF_ERROR_INVALID_STATE != err_code){
- APP_ERROR_CHECK(err_code);
- }
- }
- void advertising_stop(void)
- {
- ret_code_t err_code;
- err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle); //停止广播
- DEBUG_LOG("advertising_stop !\r\n");
- if(NRF_ERROR_INVALID_STATE != err_code){
- APP_ERROR_CHECK(err_code);
- }
- }
- bool ble_evt_is_advertising_timeout(ble_evt_t const *p_ble_evt)
- {
- return (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_SET_TERMINATED);
- }
- static void multi_qwr_conn_handle_assign(uint16_t conn_handle)
- {
- for (uint32_t i = 0; i < NRF_SDH_BLE_TOTAL_LINK_COUNT; i++)
- {
- if (m_qwr[i].conn_handle == BLE_CONN_HANDLE_INVALID)
- {
- ret_code_t err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr[i], conn_handle);
- APP_ERROR_CHECK(err_code);
- break;
- }
- }
- }
- #define slave_connected_evt_num_max 16
- static uint8_t slave_connected_evt_num = 0;
- static Ble_evt_cb ble_Slave_evt_cb[slave_connected_evt_num_max] = {0};
- int Ble_Slave_Connectd_Evt_Regist(Ble_evt_cb cb)
- {
- for (int i = 0; i < slave_connected_evt_num_max; i++)
- {
- if (ble_Slave_evt_cb[i] == cb)
- return -1;
- if (ble_Slave_evt_cb[i] == 0)
- {
- slave_connected_evt_num++;
- ble_Slave_evt_cb[i] = cb; //??μ÷oˉêy
- return 0;
- }
- }
- DEBUG_LOG( "ble_evt_Regist -> too many!\n");
- return -2;
- }
- void ble_slave_connected_evt_pcs(void)
- {
- for (int i = 0; i < slave_connected_evt_num; i++)
- { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
- if (ble_Slave_evt_cb[i])
- {
- ble_Slave_evt_cb[i](); //??μ÷oˉêy
- }
- }
- }
- #define slave_disconn_evt_num_max 16
- static uint8_t slave_disconn_evt_num = 0;
- static Ble_evt_cb ble_Slave_disconn_evt_cb[slave_disconn_evt_num_max] = {0};
- int Ble_Slave_Disconn_Evt_Regist(Ble_evt_cb cb)
- {
- for (int i = 0; i < slave_disconn_evt_num_max; i++)
- {
- if (ble_Slave_disconn_evt_cb[i] == cb)
- return -1;
- if (ble_Slave_disconn_evt_cb[i] == 0)
- {
- slave_disconn_evt_num++;
- ble_Slave_disconn_evt_cb[i] = cb; //??μ÷oˉêy
- return 0;
- }
- }
- DEBUG_LOG( "Ble_Slave_Disconn_Evt_Regist -> too many!\r\n");
- return -2;
- }
- void ble_slave_dicconn_evt_pcs(void)
- {
- for (int i = 0; i < slave_disconn_evt_num; i++)
- { //DEBUG_LOG("time_cb[%d]=%d\n",i,time_cb[i]);
- if (ble_Slave_disconn_evt_cb[i])
- {
- ble_Slave_disconn_evt_cb[i](); //??μ÷oˉêy
- }
- }
- }
- unsigned char slave_update_conn_interval_request_sta = 0;
- static ble_gap_phys_t const phys =
- {
- .rx_phys = BLE_GAP_PHY_1MBPS,
- .tx_phys = BLE_GAP_PHY_1MBPS,
- };
- static uint8_t _7_5ms_intervalFlag =0;
- uint8_t Slave_Get7_5ms_interval(void){
- return _7_5ms_intervalFlag;
- }
- void on_ble_peripheral_evt(ble_evt_t const *p_ble_evt) //×÷?a′óéè±?μ?′|àí
- {
- ret_code_t err_code;
- ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt;
-
- switch (p_ble_evt->header.evt_id)
- {
- case BLE_GAP_EVT_CONNECTED:{
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_CONNECTED\r\n");
- m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
- multi_qwr_conn_handle_assign(p_ble_evt->evt.gap_evt.conn_handle); //QWR句柄分配
-
- err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN,m_conn_handle,4);
- APP_ERROR_CHECK(err_code);
- connect_to_client = 1;
- ble_slave_connected_evt_pcs();
- #if 1
- BLE_PRINT("PHY update request.");
- err_code = sd_ble_gap_phy_update(p_gap_evt->conn_handle, &phys);
- APP_ERROR_CHECK(err_code);
- #endif
-
- BLE_PRINT("Connection 0x%x Received ble gap evt data length update request.", p_ble_evt->evt.gap_evt.conn_handle);
- ble_gap_data_length_params_t dlp =
- {
- .max_rx_time_us= BLE_GAP_DATA_LENGTH_AUTO,
- .max_tx_time_us= BLE_GAP_DATA_LENGTH_AUTO,
- .max_rx_octets = BLE_GAP_DATA_LENGTH_AUTO,
- .max_tx_octets = BLE_GAP_DATA_LENGTH_AUTO,
- };
- err_code = sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, &dlp, NULL);
- APP_ERROR_CHECK(err_code);
-
-
- sd_ble_gap_rssi_start(m_conn_handle, BLE_GAP_RSSI_THRESHOLD_INVALID, 0);
- }
- break;
- case BLE_GAP_EVT_DISCONNECTED:
- connect_to_client = 0;
- ble_slave_dicconn_evt_pcs();
- sd_ble_gap_rssi_stop(m_conn_handle);
- _7_5ms_intervalFlag =0;
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DISCONNECTED,reason:%d\r\n",p_gap_evt->params.disconnected.reason);
- break;
- case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
- {
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_PHY_UPDATE_REQUEST\r\n");
- err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
- APP_ERROR_CHECK(err_code);
- }
- break;
- case BLE_GATTC_EVT_TIMEOUT:
- // Disconnect on GATT Client timeout event.
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTC_EVT_TIMEOUT\r\n");
- err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- APP_ERROR_CHECK(err_code);
- break;
- case BLE_GATTS_EVT_TIMEOUT:
- // Disconnect on GATT Server timeout event.
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTS_EVT_TIMEOUT\r\n");
- err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- APP_ERROR_CHECK(err_code);
- break;
- case BLE_GAP_EVT_CONN_PARAM_UPDATE:
- {
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE\r\n");
- slave_update_conn_interval_request_sta = 0;
- memcpy(&slave_conn_params, &p_gap_evt->params.conn_param_update_request.conn_params, sizeof(ble_gap_conn_params_t));
- BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
- BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
- BLE_PRINT("slave_latency : %d\r\n", p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
- BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
-
- 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)
- _7_5ms_intervalFlag =1;
- else _7_5ms_intervalFlag =0;
- }BLE_PRINT("_7_5ms_intervalFlag : %d\r\n", _7_5ms_intervalFlag);
- break;
-
- case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
- {
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST\r\n");
- ble_gap_conn_params_t params;
- params = p_gap_evt->params.conn_param_update_request.conn_params;
- err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle, ¶ms);
- BLE_PRINT("=====>BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST error:%d\r\n",err_code);
- APP_ERROR_CHECK(err_code);
-
- memcpy(&slave_conn_params, &p_gap_evt->params.conn_param_update_request.conn_params, sizeof(ble_gap_conn_params_t));
- BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
- BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
- BLE_PRINT("slave_latency : %d\r\n", p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
- BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
-
- } break;
- case BLE_GAP_EVT_RSSI_CHANGED:
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_RSSI_CHANGED\r\n");
- break;
-
- case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
- {
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST\r\n");
- ble_gap_data_length_params_t const dlp =
- {
- .max_rx_octets = BLE_GAP_DATA_LENGTH_AUTO,
- .max_tx_octets = BLE_GAP_DATA_LENGTH_AUTO,
- };
- err_code = sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, &dlp, NULL);
- APP_ERROR_CHECK(err_code);
- }
- break;
- case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
- {
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE\r\n");
- BLE_PRINT("max_rx_octets : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_rx_octets);
- BLE_PRINT("max_rx_time_us : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_rx_time_us);
- BLE_PRINT("max_tx_octets : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_tx_octets);
- BLE_PRINT("max_tx_time_us : %d \r\n", p_gap_evt->params.data_length_update.effective_params.max_tx_time_us);
- }
- break;
-
- case BLE_GAP_EVT_ADV_SET_TERMINATED:
- BLE_PRINT("on_ble_peripheral_evt -> BLE_GAP_EVT_ADV_SET_TERMINATED\r\n");
- break;
- case BLE_GATTS_EVT_HVN_TX_COMPLETE:
- // BLE_PRINT("on_ble_peripheral_evt -> BLE_GATTS_EVT_HVN_TX_COMPLETE\r\n");
- break;
- case BLE_GATTS_EVT_WRITE: //D′è?2ù×÷ò??-íê3é
- break;
-
- case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
- // err_code = sd_ble_gattc_exchange_mtu_request(p_ble_evt->evt.gattc_evt.conn_handle,247);
- // APP_ERROR_CHECK(err_code);
- 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);
- break;
- case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST://?÷?ú?ò′ó?úéê??mtuê±μ?ê??t
- {
- sd_ble_gatts_exchange_mtu_reply(m_conn_handle, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
- 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);
- }break;
-
- default:
- BLE_PRINT("on_ble_peripheral_evt -> default : 0x%2x\r\n", p_ble_evt->header.evt_id);
- // No implementation needed.
- break;
- }
- }
- #if USE_LADDR == 1
- ble_gap_addr_t m_my_addr;
- char set_adv_name = 0;
- #endif
- static void gap_params_init(void) //GAP3?ê??ˉ
- {
- uint32_t err_code;
- ble_gap_conn_params_t gap_conn_params;
- ble_gap_conn_sec_mode_t sec_mode;
- BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
- #if USE_LADDR == 1
- err_code = sd_ble_gap_addr_get(&m_my_addr);
- APP_ERROR_CHECK(err_code);
- if (set_adv_name == 0)
- {
- 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]);
- sprintf(BleReallyName, "%s_%02X%02X", DEVICE_NAME, m_my_addr.addr[4], m_my_addr.addr[5]);
- err_code = sd_ble_gap_device_name_set(&sec_mode,
- (const uint8_t *)BleReallyName,
- strlen(DEVICE_NAME) + 5);
- }
- else
- {
- err_code = sd_ble_gap_device_name_set(&sec_mode,
- (const uint8_t *)BleReallyName,
- strlen(BleReallyName));
- }
- BLE_PRINT(">>>>>>>name:%d,%s",set_adv_name,BleReallyName);
- #else
- err_code = sd_ble_gap_device_name_set(&sec_mode,
- (const uint8_t *)DEVICE_NAME,
- strlen(DEVICE_NAME));
- #endif
- APP_ERROR_CHECK(err_code);
- memset(&gap_conn_params, 0, sizeof(gap_conn_params));
- gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
- gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
- gap_conn_params.slave_latency = SLAVE_LATENCY;
- gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
- err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
- APP_ERROR_CHECK(err_code);
- // err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN,m_conn_handle,0);
- // APP_ERROR_CHECK(err_code);
- }
- #if USEFIFO
- RINGFRAME_DEF(sbc,ringframe_size_1024);
- static unsigned int TIME_GetTicks_ms;
- unsigned int send_bytes_client(unsigned char *bytes, uint16_t len)
- {
- unsigned short length = len;
- if (connect_to_client)
- {
- do
- {
- if(ringframe_in(&sbc,bytes,length)==0)return 0;
- }while(ringframe_throw(&sbc)==0);
- Process_SetHoldOn(send_bytes_client_pcs,1);
- TIME_GetTicks_ms=TIME_GetTicks();
- return 0;
- }
- else
- {
- BLE_PRINT("send_bytes_client error. connect_to_client=0\r\n");
- return 1;
- }
- } //作为从机时发送数据给主机
- void send_bytes_client_pcs(void)
- {
- unsigned char sbuff[256];
- unsigned char len=0;
- while(ringframe_peek(&sbc,sbuff,&len)==0)
- {
- unsigned short length = len;
- uint32_t flag = 0;
- flag = ble_nus_data_send(&m_nus, sbuff, &length, m_conn_handle);
- if(flag==0)ringframe_throw(&sbc);
- else
- {
- if((TIME_GetTicks()-TIME_GetTicks_ms>100)||(TIME_GetTicks_ms>TIME_GetTicks()))
- {
- Process_SetHoldOn(send_bytes_client_pcs,0);
- }
- return;
- }
- }
- Process_SetHoldOn(send_bytes_client_pcs,0);
- }
- #else
- unsigned int send_bytes_client(unsigned char *bytes, uint16_t len)
- {
- unsigned int rev=0;
- unsigned short length = len;
- if (connect_to_client){
- rev=ble_nus_data_send(&m_nus, bytes, &length, m_conn_handle);
- return rev;
- }
- else{
- BLE_PRINT("send_bytes_client error. connect_to_client=0\r\n");
- return 1;
- }
- } //作为从机时发送数据给主机
- void send_bytes_client_pcs(void)
- {
-
- }
- #endif
- extern void timer_init(void);
- extern void power_management_init(void);
- extern void ble_stack_init(void);
- extern void gatt_init(void);
- extern char ble_stack_init_sta;
- extern uint8_t app_Get_isHost(void);
- #if USEMACNAME && USE_LADDR != 1
- ble_gap_addr_t mAddr;
- #endif
- void slave_init(Ble_receive_handler_t receive_handler)
- {
- static unsigned char init = 1;
- if (init)
- {
- if (receive_handler == NULL)
- {
- BLE_PRINT("slave_init -> param err \r\n");
- return;
- }
- Rec_h = receive_handler;
- if (ble_stack_init_sta)
- {
- timer_init(); //
- power_management_init(); //
- ble_stack_init(); //
- gatt_init(); //
- ble_stack_init_sta = 0;
- }
- #if USEMACNAME && USE_LADDR != 1
- if (!app_Get_isHost())
- {
- sd_ble_gap_addr_get(&mAddr);
- memset(DEVICE_NAME, 0, TARFET_LEN_MAX);
- 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]);
- }
- #endif
-
- gap_params_init();
- services_init();
- advertising_init();
- conn_params_init();
- advertising_start();
- init = 0;
- #if USE_LADDR
- BLE_PRINT("slave_init -> name [ %s ] \r\n", BleReallyName);
- #else
- BLE_PRINT("slave_init -> name [ %s ] \r\n", DEVICE_NAME);
- #endif
- }
- else
- {
- BLE_PRINT("slave_init -> err slave has init done \r\n");
- }
- }
- unsigned char slave_isconnect(void)
- {
- return connect_to_client;
- }
- unsigned int slave_set_adv_name(char *name, int len)
- {
- #if USE_LADDR == 1
- if (len > TARFET_LEN_MAX)
- return APP_ERR_OVERLENGTH;
- set_adv_name = 1;
- memset(BleReallyName, 0, TARFET_LEN_MAX);
- memcpy(BleReallyName, name, len);
- #else
- if (len > TARFET_LEN_MAX)
- return APP_ERR_OVERLENGTH;
- memset(DEVICE_NAME, 0, TARFET_LEN_MAX);
- memcpy(DEVICE_NAME, name, len);
- #endif
- return APP_SUCCESS;
- }
- void slave_get_advname_len(int *len)
- {
- *len = strlen(BleReallyName);
- }
- void slave_get_advname(char *name, int len)
- {
- memcpy(name,BleReallyName,len);
- }
- void slave_disconnect(void)
- {
- if (connect_to_client)
- sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- }
- unsigned int slave_update_conn_interval_request(float min_conn_interval, float max_conn_interval)
- {
- ret_code_t err_code;
- ble_gap_conn_params_t bgcp;
- if (slave_update_conn_interval_request_sta)
- return APP_ERR_BUSY;
- if (connect_to_client)
- {
- slave_update_conn_interval_request_sta = 1;
- if ((max_conn_interval > 1.25 * 1599) || (max_conn_interval < min_conn_interval))
- return APP_ERR_PARAMERR;
- if (min_conn_interval < 7.5f)
- return APP_ERR_PARAMERR;
- bgcp.max_conn_interval = MSEC_TO_UNITS(max_conn_interval, UNIT_1_25_MS);
- bgcp.min_conn_interval = MSEC_TO_UNITS(min_conn_interval, UNIT_1_25_MS);
- bgcp.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS);
- bgcp.slave_latency = 0;
- BLE_PRINT("slave_update_conn_interval_request -> %d \r\n", bgcp.max_conn_interval);
- err_code = sd_ble_gap_conn_param_update(m_conn_handle, &bgcp);
- APP_ERROR_CHECK(err_code);
- return err_code;
- }
- else
- {
- return APP_ERR_DISCONN;
- }
- }
- void slave_get_conn_params(ble_gap_conn_params_t *p)
- {
- p->conn_sup_timeout = slave_conn_params.conn_sup_timeout;
- p->max_conn_interval = slave_conn_params.max_conn_interval;
- p->min_conn_interval = slave_conn_params.min_conn_interval;
- p->slave_latency = slave_conn_params.slave_latency;
- }
- void slave_adv_init(void)
- {
- gap_params_init(); //ìí?óμ?GAP3?ê??ˉ
- conn_params_init(); //ìí?óμ?á??ó2?êy3?ê??ˉ
- advertising_init(); //ìí?ó1?2¥3?ê??ˉ
- }
- static signed char rssi = 0;
- signed char slave_get_rssi(void)
- {
- unsigned char channel;
- if (connect_to_client == 0)
- return 1;
- sd_ble_gap_rssi_get(m_conn_handle, &rssi, &channel);
- // BLE_PRINT("rssi= %d channel=%d\r\n", rssi, channel);
- return rssi;
- }
- #if DEBUGBLE
- #define led 13
- #define tx 11 //1.1
- #define rx 12
- //#define LS -1611916254 //?a·¢°?
- //#define RS -889050188
- //#define LS 97376119 //31
- //#define RS 627878688 //32
- #define LS -1087551583 //1.1
- #define RS -957332282 //1.1
- #define PS -1372482754 //usb
- unsigned char buff[255];
- char start = 0;
- void host_r(unsigned char *p, int len)
- {
- BLE_PRINT("hr : %d,0x%x\r\n", len, p[0]);
- if (p[0] == 0xbb)
- {
- BLE_PRINT("hr -------------: 0xbb\r\n");
- SEGGER_RTT_Write(0, &p[1], len);
- }
- if (p[0] == 0xcc)
- {
- BLE_PRINT("hr -------------: 0xcc\r\n");
- }
- }
- #define TIMER_TICK 25
- #define TCUN 1000
- unsigned short cun = 0;
- unsigned short ts = 0;
- unsigned short rec[5] = {0};
- unsigned short recrtc[5] = {0};
- unsigned int rtc_cun = 0;
- void slave_r(unsigned char *p, int len)
- {
- if (p[0] == 0xaa)
- {
- cun++;
- ts = p[1];
- ts = ts << 8;
- ts += p[2];
- if (ts >= 1)
- {
- start = 1;
- rtc_cun = NRF_RTC2->COUNTER;
- }
- if (ts == TCUN)
- start = 0;
- if (start)
- {
- if (NRF_RTC2->COUNTER - rtc_cun < 1 * TIMER_TICK)
- recrtc[0]++;
- if ((NRF_RTC2->COUNTER - rtc_cun >= 1 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 2 * TIMER_TICK))
- recrtc[1]++;
- if ((NRF_RTC2->COUNTER - rtc_cun >= 2 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 3 * TIMER_TICK))
- recrtc[2]++;
- if ((NRF_RTC2->COUNTER - rtc_cun >= 3 * TIMER_TICK) && (NRF_RTC2->COUNTER - rtc_cun < 4 * TIMER_TICK))
- recrtc[3]++;
- if (NRF_RTC2->COUNTER - rtc_cun > 4 * TIMER_TICK)
- recrtc[4]++;
- rtc_cun = NRF_RTC2->COUNTER;
- }
- BLE_PRINT("sr : %d\r\n", ts);
- }
- if (p[0] == 0xbb)
- {
- buff[0] = 0xbb;
- 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]);
- send_bytes_server(buff, leng);
- }
- if (p[0] == 0xcc)
- {
- BLE_PRINT("sr -------------: 0xcc\r\n");
- memset(rec, 0, 10);
- memset(recrtc, 0, 10);
- send_bytes_server(p, 3);
- }
- }
- #include "cli.h"
- nrf_radio_request_t radio_request_p;
- APP_TIMER_DEF(s_Timer);
- #define TEST_PERIOD APP_TIMER_TICKS(TIMER_TICK)
- unsigned short tims = 0;
- unsigned short stp = 0;
- void s_TimerCallback(void *arg)
- {
- if ((tims > 0) && (tims <= TCUN))
- {
- buff[0] = 0xaa;
- buff[1] = tims >> 8;
- buff[2] = tims;
- send_bytes_client(buff, 100);
- BLE_PRINT("send : %d\r\n", tims);
- tims++;
- }
- if (start)
- {
- if (cun > 4)
- cun = 4;
- rec[cun]++;
- cun = 0;
- }
- //·¢êy?Y??ê??ú
- if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
- {
- if (start)
- {
- buff[0] = 0xaa;
- buff[1] = stp >> 8;
- buff[2] = stp;
- send_bytes_client(buff, 100);
- }
- stp++;
- }
- // nrf_gpio_pin_toggle(rx);
- //nrf_gpio_pin_write(rx, 0);
- // BLE_PRINT("error= %d\r\n", sd_radio_request(&radio_request_p));
- }
- void Radio_State(void)
- {
- switch (NRF_RADIO->STATE)
- {
- case RADIO_STATE_STATE_Disabled:
- BLE_PRINT("RADIO_STATE_STATE_Disabled\r\n");
- break;
- case RADIO_STATE_STATE_RxRu:
- BLE_PRINT("RADIO_STATE_STATE_RxRu\r\n");
- break;
- case RADIO_STATE_STATE_RxIdle:
- BLE_PRINT("RADIO_STATE_STATE_RxIdle\r\n");
- break;
- case RADIO_STATE_STATE_Rx:
- BLE_PRINT("RADIO_STATE_STATE_Rx\r\n");
- break;
- case RADIO_STATE_STATE_RxDisable:
- BLE_PRINT("RADIO_STATE_STATE_RxDisable\r\n");
- break;
- case RADIO_STATE_STATE_TxRu:
- BLE_PRINT("RADIO_STATE_STATE_TxRu\r\n");
- break;
- case RADIO_STATE_STATE_TxIdle:
- BLE_PRINT("RADIO_STATE_STATE_TxIdle\r\n");
- break;
- case RADIO_STATE_STATE_Tx:
- BLE_PRINT("RADIO_STATE_STATE_Tx\r\n");
- break;
- case RADIO_STATE_STATE_TxDisable:
- BLE_PRINT("RADIO_STATE_STATE_TxDisable\r\n");
- break;
- }
- }
- void unoioo(void)
- {
- Ble_update_conn_interval(7.5,7.5);
-
- }
- void unoioo_s(void)
- {
- slave_update_conn_interval_request(30, 30);
- scan_start();
- }
- void unoioo_s_d(void)
- {
- host_disconnect();
- scan_start();
- }
- void rtc_config(void)
- {
- NRF_RTC2->PRESCALER = 0; //??ò?oá????êy?÷?ó1,1024us
- NRF_RTC2->TASKS_START = 1;
- }
- #include "nrf_drv_timer.h"
- void radio_evt_conf(void);
- const nrf_drv_timer_t TIMER_RADIO = NRF_DRV_TIMER_INSTANCE(2);
- void timer_led_event_handler(nrf_timer_event_t event_type, void *p_context)
- {
- if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
- {
- switch (event_type)
- {
- case NRF_TIMER_EVENT_COMPARE0: //320
- sd_radio_request(&radio_request_p);
- NRF_PPI->CHEN &= (~(PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos));
- break;
- case NRF_TIMER_EVENT_COMPARE1: //324
- sd_radio_request(&radio_request_p);
- break;
- case NRF_TIMER_EVENT_COMPARE2: //328
- sd_radio_request(&radio_request_p);
- break;
- case NRF_TIMER_EVENT_COMPARE3: //332
- NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) | (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
- break;
- default:
- //Do nothing.
- break;
- }
- }
- if (*NRF_FICR->DEVICEID == RS) //óò±?D?
- {
- switch (event_type)
- {
- case NRF_TIMER_EVENT_COMPARE0: //320
- nrf_gpio_pin_write(tx, 1);
- break;
- case NRF_TIMER_EVENT_COMPARE1: //324
- nrf_gpio_pin_write(tx, 0);
- break;
- case NRF_TIMER_EVENT_COMPARE2: //328
- break;
- case NRF_TIMER_EVENT_COMPARE3: //332
- break;
- default:
- //Do nothing.
- break;
- }
- }
- }
- void timer_config(void)
- {
- uint32_t time_us = 5000; //Time(in miliseconds) between consecutive compare events.
- uint32_t time_ticks;
- uint32_t err_code = NRF_SUCCESS;
- nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
- err_code = nrf_drv_timer_init(&TIMER_RADIO, &timer_cfg, timer_led_event_handler);
- APP_ERROR_CHECK(err_code);
- if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
- {
- time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
- nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
- time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 10000);
- nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, 0, true);
- time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 20000);
- nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL2, time_ticks, 0, true);
- time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, 29000);
- nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL3, time_ticks, NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK, true);
- }
- if (*NRF_FICR->DEVICEID == RS) //óò±?D?
- {
- time_us = 1000;
- time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us);
- nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL0, time_ticks, 0, true);
- time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_RADIO, time_us + 9000 + 1);
- nrf_drv_timer_extended_compare(&TIMER_RADIO, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
- nrf_drv_timer_enable(&TIMER_RADIO);
- }
- // nrf_drv_timer_enable(&TIMER_RADIO);
- }
- void ppi_set(void)
- {
- NRF_PPI->CH[0].EEP = (unsigned int)(&NRF_TIMER0->EVENTS_COMPARE[0]);
- NRF_PPI->CH[0].TEP = (unsigned int)(&NRF_TIMER2->TASKS_START);
- NRF_PPI->CH[1].EEP = (unsigned int)(&NRF_TIMER2->EVENTS_COMPARE[3]);
- NRF_PPI->CH[1].TEP = (unsigned int)(&NRF_TIMER2->TASKS_SHUTDOWN);
- NRF_PPI->CHEN |= (PPI_CHENCLR_CH0_Enabled << PPI_CHEN_CH0_Pos) |
- (PPI_CHENCLR_CH1_Enabled << PPI_CHEN_CH1_Pos);
- }
- extern void USR_Init(void);
- extern void USR_Process(void);
- extern void TIME_Init(void);
- extern char Target_scan[];
- unsigned char txbuff[300] = {0x0a, 0x03, 0, 0, 2, 3, 4, 5, 6, 0, 8, 9};
- unsigned char rxbuff[300] = {0};
- void radio_init_R(void)
- {
- NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
- /* Start 16 MHz crystal oscillator */
- NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
- NRF_CLOCK->TASKS_HFCLKSTART = 1;
- /* Wait for the external oscillator to start up */
- while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
- {
- // Do nothing.
- }
- // Radio config
- NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
- NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
- NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
- NRF_RADIO->PREFIX0 = 0xC3438303;
- NRF_RADIO->PREFIX1 = 0xE3630023;
- NRF_RADIO->BASE0 = 0x80C4A2E6;
- NRF_RADIO->BASE1 = 0x91D5B3F7;
- NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
- NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
- NRF_RADIO->PCNF0 = 0X00030006;
- NRF_RADIO->PCNF1 = 0X01040020;
- NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
- NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
- NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
- NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
- }
- #include "nrf_drv_rtc.h"
- const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
- unsigned int countevt = 0;
- void radio_connect(void)
- {
- NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
- countevt = 1;
- nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + countevt * 0.009 * 32768, true);
- countevt++;
- }
- void RADIO_IRQHandler(void)
- {
- if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk))
- {
- NRF_RADIO->EVENTS_READY = 0U;
- BLE_PRINT("a");
- }
- if (NRF_RADIO->EVENTS_ADDRESS && (NRF_RADIO->INTENSET & RADIO_INTENSET_ADDRESS_Msk))
- {
- NRF_RADIO->EVENTS_ADDRESS = 0U;
- BLE_PRINT("b");
- }
- if (NRF_RADIO->EVENTS_PAYLOAD && (NRF_RADIO->INTENSET & RADIO_INTENSET_PAYLOAD_Msk))
- {
- NRF_RADIO->EVENTS_PAYLOAD = 0U;
- BLE_PRINT("c");
- }
- if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
- {
- NRF_RADIO->EVENTS_END = 0U;
- // NRF_LOG_INFO("d");
- if (NRF_RADIO->STATE >= 5UL)
- {
- NRF_RADIO->EVENTS_DISABLED = 0U;
- NRF_RADIO->TASKS_DISABLE = 1U;
- nrf_gpio_pin_write(tx, 0);
- // BLE_PRINT("Tx end\r\n");
- }
- else
- {
- //ê?μ?êy?Yoó?è?D???a·¢?í?£ê?
- NRF_RTC0->CC[2] = NRF_RTC0->COUNTER;
- NRF_RADIO->PACKETPTR = (unsigned int)txbuff;
- NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
- nrf_gpio_pin_write(tx, 0);
- NRF_RADIO->EVENTS_DISABLED = 0U;
- NRF_RADIO->TASKS_DISABLE = 1U;
- while (NRF_RADIO->EVENTS_DISABLED == 0)
- ;
- NRF_RADIO->TASKS_TXEN = 1;
- nrf_gpio_pin_write(tx, 1);
- nrf_drv_rtc_cc_set(&rtc, 0, NRF_RTC0->CC[2] + 0.010 * 32768, true);
- nrf_drv_rtc_cc_set(&rtc, 1, NRF_RTC0->CC[2] + 0.018 * 32768, true);
- for (int i = 0; i < 50; i++)
- {
- BLE_PRINT("%x", rxbuff[i]);
- }
- BLE_PRINT("Rx\r\n", rxbuff[1]);
- }
- }
- if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
- {
- NRF_RADIO->EVENTS_DISABLED = 0U;
- BLE_PRINT("e");
- }
- if (NRF_RADIO->EVENTS_DEVMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMATCH_Msk))
- {
- NRF_RADIO->EVENTS_DEVMATCH = 0U;
- BLE_PRINT("f");
- }
- if (NRF_RADIO->EVENTS_DEVMISS && (NRF_RADIO->INTENSET & RADIO_INTENSET_DEVMISS_Msk))
- {
- NRF_RADIO->EVENTS_DEVMISS = 0U;
- BLE_PRINT("g");
- }
- if (NRF_RADIO->EVENTS_RSSIEND && (NRF_RADIO->INTENSET & RADIO_INTENSET_RSSIEND_Msk))
- {
- NRF_RADIO->EVENTS_RSSIEND = 0U;
- BLE_PRINT("h");
- }
- if (NRF_RADIO->EVENTS_BCMATCH && (NRF_RADIO->INTENSET & RADIO_INTENSET_BCMATCH_Msk))
- {
- NRF_RADIO->EVENTS_BCMATCH = 0U;
- BLE_PRINT("i");
- }
- if (NRF_RADIO->EVENTS_CRCOK && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCOK_Msk))
- {
- NRF_RADIO->EVENTS_CRCOK = 0U;
- BLE_PRINT("k");
- }
- if (NRF_RADIO->EVENTS_CRCERROR && (NRF_RADIO->INTENSET & RADIO_INTENSET_CRCERROR_Msk))
- {
- NRF_RADIO->EVENTS_CRCERROR = 0U;
- BLE_PRINT("l");
- }
- NVIC_ClearPendingIRQ(RADIO_IRQn);
- }
- void radio_scan_start(void)
- {
- NRF_RADIO->SHORTS = 0;
- NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_RXEN_Msk;
- NRF_RADIO->SHORTS |= RADIO_SHORTS_READY_START_Msk;
- NRF_RADIO->SHORTS |= RADIO_SHORTS_END_START_Msk;
- NRF_RADIO->INTENSET |= RADIO_INTENSET_END_Msk;
- NRF_RADIO->TASKS_RXEN = 1;
- NRF_RADIO->EVENTS_READY = 0;
- while (NRF_RADIO->EVENTS_READY == 0)
- {
- }
- NRF_RADIO->TASKS_START = 1;
- NVIC_EnableIRQ(RADIO_IRQn);
- Radio_State();
- }
- static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
- {
- switch (int_type)
- {
- case NRFX_RTC_INT_COMPARE0:
- nrf_gpio_pin_write(tx, 1);
- NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk;
- NRF_RADIO->PACKETPTR = (unsigned int)rxbuff;
- NRF_RADIO->TASKS_RXEN = 1U;
- break;
- case NRFX_RTC_INT_COMPARE1:
- Radio_State();
- BLE_PRINT("NRFX_RTC_INT_COMPARE1\r\n");
- break;
- case NRFX_RTC_INT_COMPARE2:
- break;
- case NRFX_RTC_INT_COMPARE3:
- break;
- case NRFX_RTC_INT_TICK:
- break;
- case NRFX_RTC_INT_OVERFLOW:
- nrf_drv_rtc_counter_clear(&rtc);
- break;
- }
- }
- /**********************************************************
- * oˉêy??×?£ortc_config
- * oˉêy×÷ó?£ortc?y?ˉ3?ê??ˉoíéè??
- * oˉêy2?êy£o?T
- * oˉêy·μ???μ£o?T
- ***********************************************************/
- void radio_rtc_config(void)
- {
- uint32_t err_code;
- NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
- NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
- NRF_CLOCK->TASKS_LFCLKSTART = 1;
- while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
- {
- // Do nothing.
- }
- //Initialize RTC instance
- nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
- config.prescaler = 0; //4095;????????=32768/(config.prescaler+1)Hz;
- err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
- APP_ERROR_CHECK(err_code);
- //Enable tick event & interrupt
- // nrf_drv_rtc_tick_enable(&rtc, true);
- //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
- // err_code = nrf_drv_rtc_cc_set(&rtc, 0, 8, true);
- // APP_ERROR_CHECK(err_code);
- //Power on RTC instance
- nrf_drv_rtc_enable(&rtc);
- }
- int main(void)
- {
- unsigned int error = 0;
- unsigned int rtctemp = 0;
- unsigned int start = 0;
- unsigned int radio_dis_cun = 0;
- unsigned int radio_dis_cun_rtc = 0;
- nrf_gpio_cfg_output(led);
- nrf_gpio_pin_write(led, 1);
- nrf_gpio_cfg_output(tx);
- nrf_gpio_pin_write(tx, 0);
- nrf_gpio_cfg_output(8);
- nrf_gpio_pin_write(8, 0);
- nrf_gpio_cfg_output(rx);
- nrf_gpio_pin_write(rx, 0);
- BLE_PRINT("NRF_FICR->DEVICEID : %d\r\n", *NRF_FICR->DEVICEID);
- if (*NRF_FICR->DEVICEID == RS) //óò±?D?
- {
- #if 1
- slave_init(host_r);
- #else
- radio_init_R();
- radio_rtc_config();
- radio_scan_start();
- #endif
- BLE_PRINT("you \r\n");
- }
- if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
- {
- #if 0
- Target_scan[0]=0xe3; //3132
- Target_scan[1]=0x3f;
- Target_scan[2]=0xd9;
- Target_scan[3]=0x0d;
- Target_scan[4]=0x0e;
- Target_scan[5]=0xc6;
-
- 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]);
-
- // Target_scan[0]=0x3C; //?a·¢°?
- // Target_scan[1]=0x83;
- // Target_scan[2]=0xCF;
- // Target_scan[3]=0x49;
- // Target_scan[4]=0x50;
- // Target_scan[5]=0xE1;
- //
- #endif
- Ble_Host_Connectd_Evt_Regist(unoioo);
- Ble_Slave_Connectd_Evt_Regist(unoioo_s);
- Ble_Slave_Disconn_Evt_Regist(unoioo_s_d);
- // extern void radio_request_earliest(void);
- // Ble_Slave_Connectd_Evt_Regist(radio_request_earliest);
- slave_init(host_r);
- host_init(slave_r);
-
- // timer_config();
- BLE_PRINT("zuo \r\n");
- }
- if (*NRF_FICR->DEVICEID == PS) //ê??ú
- {
- #if 0
- Target_scan[0] = 0x21;
- Target_scan[1] = 0x8a;
- Target_scan[2] = 0x4f;
- Target_scan[3] = 0x61;
- Target_scan[4] = 0xcb;
- Target_scan[5] = 0xe8;
- #endif
- host_set_scan_name("SH_13EC", 7);
- BLE_PRINT("shou \r\n");
- host_init(slave_r);
- scan_start();
- }
- rtc_config();
- for (int i = 1; i < 200; i++)
- {
- buff[i] = i + 0x30;
- // txbuff[i]=i;
- }
- app_timer_create(&s_Timer, APP_TIMER_MODE_REPEATED, s_TimerCallback);
- app_timer_start(s_Timer, TEST_PERIOD, NULL);
- // ppi_set();
- while (1)
- {
- cli_process(&clirtt);
- if (NRF_SUCCESS == sd_evt_get(&error))
- {
- BLE_PRINT("shou \r\n");
- }
- // if (*NRF_FICR->DEVICEID == LS) //×ó±?D?
- {
- if (NRF_RADIO->STATE == RADIO_STATE_STATE_Disabled)
- {
- nrf_gpio_pin_write(tx, 0);
- }
- else
- {
- nrf_gpio_pin_write(tx, 1);
- }
- }
- }
- }
- void host_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- host_init(slave_r);
- }
- CLI_CMD_REGISTER(host_init, "clear sereen", host_init_pcs);
- void hsb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- send_bytes_server(buff, 200);
- }
- CLI_CMD_REGISTER(hsb, "clear sereen", hsb_pcs);
- void send_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- tims = 1;
- }
- CLI_CMD_REGISTER(send, "clear sereen", send_pcs);
- void scc_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- buff[0] = 0xcc;
- send_bytes_client(buff, 6);
- }
- CLI_CMD_REGISTER(scc, "clear sereen", scc_pcs);
- void sbb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- buff[0] = 0xbb;
- send_bytes_client(buff, 6);
- }
- CLI_CMD_REGISTER(sbb, "clear sereen", sbb_pcs);
- void hcb_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- send_bytes_client(buff, 200);
- }
- CLI_CMD_REGISTER(hcb, "clear sereen", hcb_pcs);
- void slave_init_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- slave_init(host_r);
- }
- CLI_CMD_REGISTER(slave_init, "clear sereen", slave_init_pcs);
- void bleupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- unsigned int error = 0;
- error = Ble_update_conn_interval(10, 10);
- cli_printf(p_cli, "err %d", error);
- }
- CLI_CMD_REGISTER(bleupdata10, "clear sereen", bleupdata_pcs);
- void bleupdata_1000pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- unsigned int error = 0;
- error = Ble_update_conn_interval(1000, 1000);
- cli_printf(p_cli, "err %d", error);
- }
- CLI_CMD_REGISTER(bleupdata1000, "clear sereen", bleupdata_1000pcs);
- void slaveupdata_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- unsigned int error =
- slave_update_conn_interval_request(40, 40);
- cli_printf(p_cli, "err %d", error);
- }
- CLI_CMD_REGISTER(slaveupdata, "clear sereen", slaveupdata_pcs);
- void conn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- if (argc == 1)
- {
- host_set_scan_name(argv[0], strlen(argv[0]));
- host_init(slave_r);
- }
- else
- cli_printf(p_cli, "err ");
- }
- CLI_CMD_REGISTER(conn, "clear sereen", conn_pcs);
- void scan_name_set_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- if (argc == 1)
- {
- host_set_scan_name(argv[0], strlen(argv[0]));
- }
- else
- cli_printf(p_cli, "err ");
- }
- CLI_CMD_REGISTER(scan_name_set, "clear sereen", scan_name_set_pcs);
- void systemreset_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- NVIC_SystemReset();
- }
- CLI_CMD_REGISTER(systemreset, "clear sereen", systemreset_pcs);
- void scanstart_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- scan_start();
- }
- CLI_CMD_REGISTER(scanstart, "clear sereen", scanstart_pcs);
- void slave_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- slave_disconnect();
- }
- CLI_CMD_REGISTER(slave_dec, "clear sereen", slave_dec_pcs);
- void host_dec_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- host_disconnect();
- }
- CLI_CMD_REGISTER(host_dec, "clear sereen", host_dec_pcs);
- void getconn_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.min_conn_interval);
- BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", slave_conn_params.max_conn_interval);
- BLE_PRINT("slave_latency : %d\r\n", slave_conn_params.slave_latency);
- BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", slave_conn_params.conn_sup_timeout);
- extern ble_gap_conn_params_t host_conn_params;
- BLE_PRINT("min_conn_interval : %d * 1.25 ms\r\n", host_conn_params.min_conn_interval);
- BLE_PRINT("max_conn_interval : %d * 1.25 ms\r\n", host_conn_params.max_conn_interval);
- BLE_PRINT("slave_latency : %d\r\n", host_conn_params.slave_latency);
- BLE_PRINT("conn_sup_timeout : %d * 10 ms\r\n", host_conn_params.conn_sup_timeout);
- slave_set_adv_name("123456", 6);
- gap_params_init();
- while (slave_isconnect() == 1)
- {
- }
- BLE_PRINT("123456555");
- advertising_start();
- BLE_PRINT("4554564");
- }
- CLI_CMD_REGISTER(getconn, "clear sereen", getconn_pcs);
- void slave_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- slave_get_rssi();
- }
- CLI_CMD_REGISTER(slave_get_rssi, "clear sereen", slave_get_rssi_pcs);
- void host_get_rssi_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- host_get_rssi();
- }
- CLI_CMD_REGISTER(host_get_rssi, "clear sereen", host_get_rssi_pcs);
- int teg = 0;
- unsigned int rtccc = 0;
- void radio_evt_conf(void)
- {
- NRF_RADIO->POWER = (RADIO_POWER_POWER_Enabled << RADIO_POWER_POWER_Pos);
- /* Start 16 MHz crystal oscillator */
- NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
- NRF_CLOCK->TASKS_HFCLKSTART = 1;
- txbuff[1] = NRF_RTC0->COUNTER;
- txbuff[2] = teg;
- /* Wait for the external oscillator to start up */
- while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
- {
- // Do nothing.
- }
- // Radio config
- NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
- NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
- NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
- NRF_RADIO->PREFIX0 = 0xC3438303;
- NRF_RADIO->PREFIX1 = 0xE3630023;
- NRF_RADIO->BASE0 = 0x80C4A2E6;
- NRF_RADIO->BASE1 = 0x91D5B3F7;
- NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
- NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
- NRF_RADIO->PCNF0 = 0X00030006;
- NRF_RADIO->PCNF1 = 0X01040020;
- NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
- NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
- NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
- NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos //READYoó×??ˉ?aê??′DDSTART
- | RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos;
- // Set payload pointer
- NRF_RADIO->PACKETPTR = (uint32_t)&txbuff[0];
- NRF_RADIO->EVENTS_DISABLED = 0; //??3y±ê????
- NRF_RADIO->TASKS_TXEN = 1; //?aê?oó?á?ú2?×??o2ù×÷
- while (NRF_RADIO->EVENTS_END == 0)
- {
- //μè′y·¢?ííê3é
- }
- nrf_gpio_pin_write(rx, 0);
- NRF_RADIO->SHORTS = 0;
- NRF_RADIO->EVENTS_DISABLED = 0U;
- NRF_RADIO->TASKS_DISABLE = 1U;
- while (NRF_RADIO->EVENTS_DISABLED == 0)
- {
- //μè′y1?μ?radio
- }
- NRF_RADIO->EVENTS_READY = 0U;
- // Enable radio and wait for ready
- NRF_RADIO->TASKS_RXEN = 1U;
- NRF_RADIO->PACKETPTR = (uint32_t)&rxbuff[0];
- while (NRF_RADIO->EVENTS_READY == 0U)
- {
- // wait
- }
- nrf_gpio_pin_write(rx, 1);
- NRF_RADIO->EVENTS_END = 0U;
- // Start listening and wait for address received event
- NRF_RADIO->TASKS_START = 1U;
- // Wait for end of packet or buttons state changed
- for (int j = 0; j < 5000; j++)
- {
- if (NRF_RADIO->EVENTS_END == 1)
- break;
- }
- if (NRF_RADIO->CRCSTATUS == 1U)
- {
- for (int i = 0; i < 50; i++)
- {
- BLE_PRINT("%x", rxbuff[i]);
- }
- BLE_PRINT("\r\n ");
- memset(rxbuff, 0, 60);
- }
- else
- {
- BLE_PRINT("E\r\n ");
- }
- }
- nrf_radio_signal_callback_return_param_t call_radio_return_val;
- nrf_radio_signal_callback_return_param_t *call_radio(unsigned char sig)
- {
- nrf_gpio_pin_write(rx, 1);
- radio_evt_conf();
- nrf_gpio_pin_write(rx, 0);
- call_radio_return_val.callback_action = NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END;
- return &call_radio_return_val;
- }
- void radio_session_open(void)
- {
- BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
- }
- void radio_session_open_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- BLE_PRINT("error= %d\r\n", sd_radio_session_open(call_radio));
- }
- CLI_CMD_REGISTER(radio_s_open, "clear sereen", radio_session_open_pcs);
- void radio_session_close_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- BLE_PRINT("error= %d\r\n", sd_radio_session_close());
- }
- CLI_CMD_REGISTER(radio_s_close, "clear sereen", radio_session_close_pcs);
- void radio_request_earliest(void)
- {
- radio_session_open();
- radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
- radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_NO_GUARANTEE;
- radio_request_p.params.earliest.length_us = 4000;
- radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
- radio_request_p.params.earliest.timeout_us = 2000;
- BLE_PRINT("radio_request_earliest= %d\r\n", sd_radio_request(&radio_request_p));
- //
- // radio_request_p.request_type=NRF_RADIO_REQ_TYPE_NORMAL;
- // radio_request_p.params.normal.hfclk=NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
- // radio_request_p.params.normal.distance_us=10000;
- // radio_request_p.params.normal.length_us=5000;
- // radio_request_p.params.normal.priority=NRF_RADIO_PRIORITY_NORMAL;
- }
- void radio_request_e_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- radio_request_p.request_type = NRF_RADIO_REQ_TYPE_EARLIEST;
- radio_request_p.params.earliest.hfclk = NRF_RADIO_HFCLK_CFG_XTAL_GUARANTEED;
- radio_request_p.params.earliest.length_us = 5000;
- radio_request_p.params.earliest.priority = NRF_RADIO_PRIORITY_NORMAL;
- radio_request_p.params.earliest.timeout_us = 2000;
- BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
- }
- CLI_CMD_REGISTER(radio_r_e, "clear sereen", radio_request_e_pcs);
- void radio_request_n_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- radio_request_p.request_type = NRF_RADIO_REQ_TYPE_NORMAL;
- BLE_PRINT("error= %d", sd_radio_request(&radio_request_p));
- }
- CLI_CMD_REGISTER(radio_r_n, "clear sereen", radio_request_n_pcs);
- void Radio_State_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- Radio_State();
- }
- CLI_CMD_REGISTER(Radio_State, "clear sereen", Radio_State_pcs);
- void s100_pcs(cli_t *p_cli, unsigned short argc, char **argv)
- {
- send_bytes_client(buff, 150);
- }
- CLI_CMD_REGISTER(s100, "clear sereen", s100_pcs);
- #endif
|