hi hi ;
i'm trying to control gpio pin 20 with my smart phone and with event at gpio pin 5 (when the state of the pin 5 changes the gpio pin 20 toggeles )
but my problem is when i add the function gpio_init() the ble stops working .
any help will be appreciated
i'm using nrf51822 s110 sdk 9 pca 20006
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. * * The information contained herein is property of Nordic Semiconductor ASA. * Terms and conditions of usage are described in detail in NORDIC * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. * * Licensees are granted free, non-transferable use of the information. NO * WARRANTY of ANY KIND is provided. This heading must NOT be removed from * the file. * */ /** @file * * @brief BLE Services, a beginner's tutorial project main file. * * This example is meant to be used togheter with the tutorial "BLE Services, a beginner's tutorial" * found at https://devzone.nordicsemi.com/tutorials/8/ble-services-a-beginners-tutorial/. * The purpose of this tutorial is to get you started with the basics of the nRF51x22 and Bluetooth Low Energy (BLE). * More specifically this tutorial will go through the bare minimum of steps to get your first services * up and running. The tutorial is fairly superficial and is meant to be a hands-on introduction to BLE services. * It is intended to be a natural continuation of the tutorial "BLE Advertising, a beginner's tutorial". * It can easily be used as a starting point for creating a new application, the comments identified * with 'OUR_JOB' indicates where the work described in the tutorial needs to be done. */ #include <stdint.h> #include <string.h> #include "nordic_common.h" #include "nrf.h" #include "app_error.h" #include "nrf51_bitfields.h" #include "ble.h" #include "ble_hci.h" #include "ble_srv_common.h" #include "ble_advdata.h" #include "ble_advertising.h" #include "ble_conn_params.h" #include "boards.h" #include "softdevice_handler.h" #include "app_timer.h" #include "device_manager.h" #include "pstorage.h" #include "app_trace.h" #include "bsp.h" #include "bsp_btn_ble.h" #include "our_service.h" #include "SEGGER_RTT.h" #include "msgs.h" #include "nrf_drv_gpiote.h" #define IS_SRVC_CHANGED_CHARACT_PRESENT 1 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/ #define DEVICE_NAME "OhmaneService" /**< Name of device. Will be included in the advertising data. */ #define APP_ADV_INTERVAL 50 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 25 ms). */ #define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout in units of seconds. */ #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ #define APP_TIMER_MAX_TIMERS (6+BSP_APP_TIMERS_NUMBER) /**< Maximum number of simultaneously created timers. */ #define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */ #define MIN_CONN_INTERVAL MSEC_TO_UNITS(100, UNIT_1_25_MS) /**< Minimum acceptable connection interval (0.1 seconds). */ #define MAX_CONN_INTERVAL MSEC_TO_UNITS(200, UNIT_1_25_MS) /**< Maximum acceptable connection interval (0.2 second). */ #define SLAVE_LATENCY 0 /**< Slave latency. */ #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */ #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< 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(30000, APP_TIMER_PRESCALER)/**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */ #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */ #define SEC_PARAM_BOND 1 /**< Perform bonding. */ #define SEC_PARAM_MITM 0 /**< Man In The Middle protection not required. */ #define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_NONE /**< No I/O capabilities. */ #define SEC_PARAM_OOB 0 /**< Out Of Band data not available. */ #define SEC_PARAM_MIN_KEY_SIZE 7 /**< Minimum encryption key size. */ #define SEC_PARAM_MAX_KEY_SIZE 16 /**< Maximum encryption key size. */ #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */ static dm_application_instance_t m_app_handle; /**< Application identifier allocated by device manager */ static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ int h=0; // OUR_JOB: Declare a ble_os_t service structure for our application // FROM_SERVICE_TUTORIAL: Declare a service structure for our application ble_os_t m_our_service; //int i=01; // OUR_JOB: Step 3.G, Declare an app_timer id variable and define our timer interval and define a timer interval static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_OUR_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN}}; // OUR_JOB: For advertising, declare a ble_uuid_t variable holding our service UUID /**@brief Callback function for asserts in the SoftDevice. * * @details This function will be called in case of an assert in the SoftDevice. * * @warning This handler is an example only and does not fit a final product. You need to analyze * how your product is supposed to react in case of Assert. * @warning On assert from the SoftDevice, the system can only recover on reset. * * @param[in] line_num Line number of the failing ASSERT call. * @param[in] file_name File name of the failing ASSERT call. */ #define LED_20 9 //etteint #define LED_11 5 //allumer #define PIR 20 #ifdef BSP_BUTTON_0 #define PIN_IN 5 #endif #ifndef PIN_IN #error "Please indicate input pin" #endif #ifdef BSP_LED_0 #define PIN_OUT 20 #endif #ifndef PIN_OUT #error "Please indicate output pin" #endif /** ce programme permet de alimenter une led relier � un relay par les deux sortie pin 5 et pin 11 en se basant sur l'information r�cup�r�e de PIR qui branch� a l'entr�e pin 20. la dur�e de l"allimentation est 1 seconde la dur�e d'excitation desz deux pin 5 et 11 est une seconde * @brief Function for application main entry. */ int i=0; int ii=0; int iii=0; int j=0; int time=6250; bool temoin= true; /*void init_timer(void) { NRF_TIMER0->TASKS_STOP = 1; // Create an Event-Task shortcut to clear TIMER0 on COMPARE[0] event NRF_TIMER0->MODE = TIMER_MODE_MODE_Timer; NRF_TIMER0->BITMODE = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos); NRF_TIMER0->PRESCALER = 4; // 1us resolution NRF_TIMER0->TASKS_CLEAR = 1; // clear the task first to be usable for later NRF_TIMER0->CC[0] = 1000; //first timeout //NRF_TIMER0->CC[1] = 1000; //second timeout NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos; NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos; // Create an Event-Task shortcut to clear TIMER0 on COMPARE[0] event. NRF_TIMER0->SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos); NRF_TIMER0->TASKS_START = 1; } void TIMER0_IRQHandler(void) { if ((NRF_TIMER0->EVENTS_COMPARE[0] != 0) ) { NRF_TIMER0->EVENTS_COMPARE[0] = 0; i+=1; if(i==45) { i=0; nrf_gpio_pin_clear(20); NRF_TIMER0->TASKS_STOP = 1; //stoptimer2 } } }*/ void start_timer(void) { NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; // Set the timer in Counter Mode NRF_TIMER2->TASKS_CLEAR = 1; // clear the task first to be usable for later NRF_TIMER2->PRESCALER = 4; //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit; //Set counter to 16 bit resolution NRF_TIMER2->CC[0] = 1000; //Set value for TIMER2 compare register 0 NRF_TIMER2->CC[1] = 1000; NRF_TIMER2->CC[2] = 1000; //Set value for TIMER2 compare register 1 // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos) | (TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos); NVIC_EnableIRQ(TIMER2_IRQn); NRF_TIMER2->TASKS_START = 1; // Start TIMER2 } void stop_timer(void) { NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; // Set the timer in Counter Mode NRF_TIMER2->TASKS_CLEAR = 1; // clear the task first to be usable for later NRF_TIMER2->PRESCALER = 4; //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit; //Set counter to 16 bit resolution NRF_TIMER2->CC[0] = 1000; //Set value for TIMER2 compare register 0 NRF_TIMER2->CC[1] = 1000; NRF_TIMER2->CC[2] = 1000; //Set value for TIMER2 compare register 1 // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos) | (TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos); NVIC_EnableIRQ(TIMER2_IRQn); NRF_TIMER2->TASKS_STOP = 1; // STOP TIMER2 } void TIMER2_IRQHandler(void) { if ((NRF_TIMER2->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) { NRF_TIMER2->EVENTS_COMPARE[0] = 0; i+=1; if(i==15) { i=0; // nrf_gpio_pin_set(20); } } //****** if ((NRF_TIMER2->EVENTS_COMPARE[1] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) { NRF_TIMER2->EVENTS_COMPARE[1] = 0; ii+=1; if(ii==time) { ii=0; nrf_gpio_pin_clear(20); setvalue(1); stop_timer(); } } //******* if ((NRF_TIMER2->EVENTS_COMPARE[2] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) { NRF_TIMER2->EVENTS_COMPARE[2] = 0; iii+=1; if(iii==2730) { iii=0; } } } void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { nrf_drv_gpiote_out_toggle(PIN_OUT); } /** * @brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output, * and configures GPIOTE to give an interrupt on pin change. */ static void gpio_init(void) { ret_code_t err_code; err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); //config.sense = NRF_GPIOTE_POLARITY_LOTOHI; nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false); err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_in_config_t config; memset(&config, 0, sizeof(nrf_drv_gpiote_in_config_t)); // config.sense = NRF_GPIOTE_POLARITY_HITOLO; config.sense = NRF_GPIOTE_POLARITY_LOTOHI; //config.pull = NRF_GPIO_PIN_PULLUP; // config.pull = GPIO_PIN_CNF_PULL_Disabled; config.pull =GPIO_PIN_CNF_PULL_Pulldown; config.hi_accuracy = false; config.is_watcher = false; nrf_drv_gpiote_in_init(PIR, &config, in_pin_handler); nrf_drv_gpiote_in_event_enable(PIN_IN, true); } void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name) { app_error_handler(DEAD_BEEF, line_num, p_file_name); } /**@brief Function for the Timer initialization. * * @details Initializes the timer module. This creates and starts application timers. */ static void timers_init(void) { APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false); } /**@brief Function for the GAP initialization. * * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the * device including the device name, appearance, and the preferred connection parameters. */ static void gap_params_init(void) { 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); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); 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); } /**@brief Function for initializing services that will be used by the application. */ static void services_init(void) { // OUR_JOB: Add code to initialize the services used by the application. //our_service_init(&m_our_service); our_service_init(&m_our_service); } /**@brief Function for handling the Connection Parameters Module. * * @details This function will be called for all events in the Connection Parameters Module which * are passed to the application. * @note All this function does is to disconnect. This could have been done by simply * setting the disconnect_on_fail config parameter, but instead we use the event * handler mechanism to demonstrate its use. * * @param[in] p_evt Event received from the Connection Parameters Module. */ static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) { /* uint32_t err_code; if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) { err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); APP_ERROR_CHECK(err_code); }*/ uint32_t err_code; if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) { err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); APP_ERROR_CHECK(err_code); } } /**@brief Function for handling a Connection Parameters error. * * @param[in] nrf_error Error code containing information about what went wrong. */ static void conn_params_error_handler(uint32_t nrf_error) { // APP_ERROR_HANDLER(nrf_error); APP_ERROR_HANDLER(nrf_error); } /**@brief Function for initializing the Connection Parameters module. */ static void conn_params_init(void) { uint32_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_GATT_HANDLE_INVALID; cp_init.disconnect_on_fail = false; cp_init.evt_handler = on_conn_params_evt; cp_init.error_handler = conn_params_error_handler; err_code = ble_conn_params_init(&cp_init); APP_ERROR_CHECK(err_code); } /**@brief Function for starting timers. */ static void application_timers_start(void) { } /**@brief Function for putting the chip into sleep mode. * * @note This function will not return. */ static void sleep_mode_enter(void) { uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE); APP_ERROR_CHECK(err_code); // Prepare wakeup buttons. err_code = bsp_btn_ble_sleep_mode_prepare(); APP_ERROR_CHECK(err_code); // Go to system-off mode (this function will not return; wakeup will cause a reset). err_code = sd_power_system_off(); APP_ERROR_CHECK(err_code); } /**@brief Function for handling advertising events. * * @details This function will be called for advertising events which are passed to the application. * * @param[in] ble_adv_evt Advertising event. */ static void on_adv_evt(ble_adv_evt_t ble_adv_evt) { uint32_t err_code; switch (ble_adv_evt) { case BLE_ADV_EVT_FAST: err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); APP_ERROR_CHECK(err_code); break; case BLE_ADV_EVT_IDLE: sleep_mode_enter(); break; default: break; } } /**@brief Function for handling the Application's BLE Stack events. * * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_evt(ble_evt_t * p_ble_evt) { uint32_t err_code; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: err_code = bsp_indication_set(BSP_INDICATE_CONNECTED); APP_ERROR_CHECK(err_code); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; break; case BLE_GAP_EVT_DISCONNECTED: m_conn_handle = BLE_CONN_HANDLE_INVALID; break; default: // No implementation needed. break; } } /**@brief Function for dispatching a BLE stack event to all modules with a BLE stack event handler. * * @details This function is called from the BLE Stack event interrupt handler after a BLE stack * event has been received. * * @param[in] p_ble_evt Bluetooth stack event. */ static void ble_evt_dispatch(ble_evt_t * p_ble_evt) { dm_ble_evt_handler(p_ble_evt); ble_conn_params_on_ble_evt(p_ble_evt); bsp_btn_ble_on_ble_evt(p_ble_evt); on_ble_evt(p_ble_evt); ble_advertising_on_ble_evt(p_ble_evt); ble_our_service_on_ble_evt(&m_our_service, p_ble_evt); } /**@brief Function for dispatching a system event to interested modules. * * @details This function is called from the System event interrupt handler after a system * event has been received. * * @param[in] sys_evt System stack event. */ static void sys_evt_dispatch(uint32_t sys_evt) { /* pstorage_sys_event_handler(sys_evt); ble_advertising_on_sys_evt(sys_evt);*/ pstorage_sys_event_handler(sys_evt); ble_advertising_on_sys_evt(sys_evt); } /**@brief Function for initializing the BLE stack. * * @details Initializes the SoftDevice and the BLE event interrupt. */ static void ble_stack_init(void) { uint32_t err_code; // Initialize the SoftDevice handler module. SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL); #if defined(S110) || defined(S130) // Enable BLE stack. ble_enable_params_t ble_enable_params; memset(&ble_enable_params, 0, sizeof(ble_enable_params)); #ifdef S130 ble_enable_params.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT; #endif ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT; err_code = sd_ble_enable(&ble_enable_params); APP_ERROR_CHECK(err_code); #endif // Register with the SoftDevice handler module for BLE events. err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch); APP_ERROR_CHECK(err_code); // Register with the SoftDevice handler module for BLE events. err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch); APP_ERROR_CHECK(err_code); } /**@brief Function for handling events from the BSP module. * * @param[in] event Event generated by button press. */ void bsp_event_handler(bsp_event_t event) { uint32_t err_code; switch (event) { case BSP_EVENT_SLEEP: sleep_mode_enter(); break; case BSP_EVENT_DISCONNECT: err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } break; case BSP_EVENT_WHITELIST_OFF: err_code = ble_advertising_restart_without_whitelist(); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } break; default: break; } } /**@brief Function for handling the Device Manager events. * * @param[in] p_evt Data associated to the device manager event. */ static uint32_t device_manager_evt_handler(dm_handle_t const * p_handle, dm_event_t const * p_event, ret_code_t event_result) { /* APP_ERROR_CHECK(event_result); #ifdef BLE_DFU_APP_SUPPORT if (p_event->event_id == DM_EVT_LINK_SECURED) { app_context_load(p_handle); } #endif // BLE_DFU_APP_SUPPORT return NRF_SUCCESS;*/ APP_ERROR_CHECK(event_result); #ifdef BLE_DFU_APP_SUPPORT if (p_event->event_id == DM_EVT_LINK_SECURED) { app_context_load(p_handle); } #endif // BLE_DFU_APP_SUPPORT return NRF_SUCCESS; } /**@brief Function for the Device Manager initialization. * * @param[in] erase_bonds Indicates whether bonding information should be cleared from * persistent storage during initialization of the Device Manager. */ static void device_manager_init(bool erase_bonds) { uint32_t err_code; dm_init_param_t init_param = {.clear_persistent_data = erase_bonds}; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID; err_code = dm_register(&m_app_handle, ®ister_param); APP_ERROR_CHECK(err_code); } /**@brief Function for initializing the Advertising functionality. */ static void advertising_init(void) { uint32_t err_code; ble_advdata_t advdata; // Build advertising data struct to pass into ble_advertising_init(). memset(&advdata, 0, sizeof(advdata)); advdata.name_type = BLE_ADVDATA_FULL_NAME; advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; ble_adv_modes_config_t options = {0}; options.ble_adv_fast_enabled = BLE_ADV_FAST_ENABLED; options.ble_adv_fast_interval = APP_ADV_INTERVAL; options.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS; // OUR_JOB: Create a scan response packet and include the list of UUIDs ble_advdata_t srdata; memset(&srdata, 0, sizeof(srdata)); srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]); srdata.uuids_complete.p_uuids = m_adv_uuids; err_code = ble_advertising_init(&advdata, &srdata, &options, on_adv_evt, NULL); APP_ERROR_CHECK(err_code); } /**@brief Function for initializing buttons and leds. * * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up. */ static void buttons_leds_init(bool * p_erase_bonds) { /* bsp_event_t startup_event; uint32_t err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), bsp_event_handler); APP_ERROR_CHECK(err_code); err_code = bsp_btn_ble_init(NULL, &startup_event); APP_ERROR_CHECK(err_code); *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);*/ bsp_event_t startup_event; uint32_t err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), bsp_event_handler); APP_ERROR_CHECK(err_code); err_code = bsp_btn_ble_init(NULL, &startup_event); APP_ERROR_CHECK(err_code); *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA); } /**@brief Function for the Power manager. */ static void power_manage(void) { /* uint32_t err_code = sd_app_evt_wait(); APP_ERROR_CHECK(err_code);*/ uint32_t err_code = sd_app_evt_wait(); APP_ERROR_CHECK(err_code); } /**@brief Function for application main entry. */ int main(void) { time=6250; uint32_t err_code; bool erase_bonds; // Initialize. timers_init(); buttons_leds_init(&erase_bonds); ble_stack_init(); device_manager_init(erase_bonds); gap_params_init(); services_init(); advertising_init(); conn_params_init(); // Start execution. application_timers_start(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); nrf_gpio_cfg_output(20); services_init(); start_timer(); gpio_init(); for (;;) { power_manage(); __WFE(); __SEV(); // sd_nvic_EnableIRQ(TIMER2_IRQn); // start_timer(); if(returnei()==0) { nrf_gpio_pin_set(20); //NVIC_EnableIRQ(TIMER0_IRQn); // sd_nvic_EnableIRQ(TIMER2_IRQn); // start_timer(); } else if(returnei()==1) { nrf_gpio_pin_clear(20); // stop_timer(); } else if(returnei()==2) { time=6250*2;//20secondes } else if(returnei()==3) { time=6250*3;//30secondes } else if(returnei()==4) { time=6250;//10secondes } } } /** * @} */
#include <stdint.h> #include <string.h> #include "nrf_gpio.h" #include "our_service.h" #include "ble_srv_common.h" #include "app_error.h" volatile int iiii=2; void setvalue(int f) { iiii=f; } int returnei () { return iiii; } static void on_ble_write(ble_os_t * p_our_service, ble_evt_t * p_ble_evt) { // Declare buffer variable to hold received data. The data can only be 32 bit long. uint32_t data_buffer; // Populate ble_gatts_value_t structure to hold received data and metadata. ble_gatts_value_t rx_data; rx_data.len = sizeof(uint32_t); rx_data.offset = 0; rx_data.p_value = (uint8_t*)&data_buffer; // Check if write event is performed on our characteristic or the CCCD if(p_ble_evt->evt.gatts_evt.params.write.handle == p_our_service->char_handles.value_handle) { //ii=0; // Get data sd_ble_gatts_value_get(p_our_service->conn_handle, p_our_service->char_handles.value_handle, &rx_data); // Print handle and value //printf("Value received on handle %#06x: %#010x\r\n", p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer); if(data_buffer == 0x00000001) { // printf("Notification enabled\r\n"); iiii=1; } else if(data_buffer == 0x00000000) { // printf("Notification disabled\r\n"); iiii=0; } else if(data_buffer == 0x00000002) { // printf("Notification disabled\r\n"); iiii=2; } else if(data_buffer == 0x00000003) { // printf("Notification disabled\r\n"); iiii=3; } else if(data_buffer == 0x00000004) { // printf("Notification disabled\r\n"); iiii=4; } } else if(p_ble_evt->evt.gatts_evt.params.write.handle == p_our_service->char_handles.cccd_handle) { // Get data // ii=0; sd_ble_gatts_value_get(p_our_service->conn_handle, p_our_service->char_handles.cccd_handle, &rx_data); // Print handle and value // printf("Value received on handle %#06x: %#06x\r\n", p_ble_evt->evt.gatts_evt.params.write.handle, data_buffer); if(data_buffer == 0x0001) { // printf("Notification enabled\r\n"); // ii=0; } else if(data_buffer == 0x0000) { // printf("Notification disabled\r\n"); // ii=1; } } } // ALREADY_DONE_FOR_YOU: Declaration of a function that will take care of some housekeeping of ble connections related to our service and characteristic void ble_our_service_on_ble_evt(ble_os_t * p_our_service, ble_evt_t * p_ble_evt) { switch (p_ble_evt->header.evt_id) { case BLE_GATTS_EVT_WRITE: on_ble_write(p_our_service, p_ble_evt); break; case BLE_GAP_EVT_CONNECTED: p_our_service->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; break; case BLE_GAP_EVT_DISCONNECTED: p_our_service->conn_handle = BLE_CONN_HANDLE_INVALID; break; default: // No implementation needed. break; } } /**@brief Function for adding our new characterstic to "Our service" that we initiated in the previous tutorial. * * @param[in] p_our_service Our Service structure. * */ static uint32_t our_char_add(ble_os_t * p_our_service) { uint32_t err_code = 0; // Variable to hold return codes from library and softdevice functions ble_uuid_t char_uuid; ble_uuid128_t base_uuid = BLE_UUID_OUR_BASE_UUID; char_uuid.uuid = BLE_UUID_OUR_CHARACTERISTC_UUID; sd_ble_uuid_vs_add(&base_uuid, &char_uuid.type); APP_ERROR_CHECK(err_code); ble_gatts_char_md_t char_md; memset(&char_md, 0, sizeof(char_md)); char_md.char_props.read = 1; char_md.char_props.write = 1; ble_gatts_attr_md_t cccd_md; memset(&cccd_md, 0, sizeof(cccd_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); cccd_md.vloc = BLE_GATTS_VLOC_STACK; char_md.p_cccd_md = &cccd_md; char_md.char_props.notify = 1; ble_gatts_attr_md_t attr_md; memset(&attr_md, 0, sizeof(attr_md)); attr_md.vloc = BLE_GATTS_VLOC_STACK; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); ble_gatts_attr_t attr_char_value; memset(&attr_char_value, 0, sizeof(attr_char_value)); attr_char_value.p_uuid = &char_uuid; attr_char_value.p_attr_md = &attr_md; attr_char_value.max_len = 8; attr_char_value.init_len = 8; uint8_t value[8] = {0x12,0x34,0x56,0x78,0x12,0x34,0x56,0x78}; attr_char_value.p_value = value; err_code = sd_ble_gatts_characteristic_add(p_our_service->service_handle, &char_md, &attr_char_value, &p_our_service->char_handles); APP_ERROR_CHECK(err_code); return NRF_SUCCESS; } /**@brief Function for initiating our new service. * * @param[in] p_our_service Our Service structure. * */ void our_service_init(ble_os_t * p_our_service) { uint32_t err_code; // Variable to hold return codes from library and softdevice functions // FROM_SERVICE_TUTORIAL: Declare 16-bit service and 128-bit base UUIDs and add them to the BLE stack ble_uuid_t service_uuid; ble_uuid128_t base_uuid = BLE_UUID_OUR_BASE_UUID; service_uuid.uuid = BLE_UUID_OUR_SERVICE_UUID; err_code = sd_ble_uuid_vs_add(&base_uuid, &service_uuid.type); APP_ERROR_CHECK(err_code); // OUR_JOB: Step 3.B, Set our service connection handle to default value. I.e. an invalid handle since we are not yet in a connection. p_our_service->conn_handle = BLE_CONN_HANDLE_INVALID; // FROM_SERVICE_TUTORIAL: Add our service err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &service_uuid, &p_our_service->service_handle); APP_ERROR_CHECK(err_code); // OUR_JOB: Call the function our_char_add() to add our new characteristic to the service. our_char_add(p_our_service); } // ALREADY_DONE_FOR_YOU: Function to be called when updating characteristic value void our_temperature_characteristic_update(ble_os_t *p_our_service, int32_t *temperature_value) { // OUR_JOB: Step 3.E, Update characteristic value if (p_our_service->conn_handle != BLE_CONN_HANDLE_INVALID) { uint16_t len = 4; ble_gatts_hvx_params_t hvx_params; memset(&hvx_params, 0, sizeof(hvx_params)); hvx_params.handle = p_our_service->char_handles.value_handle; hvx_params.type = BLE_GATT_HVX_NOTIFICATION; hvx_params.offset = 0; hvx_params.p_len = &len; hvx_params.p_data = (uint8_t*)temperature_value; sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params); } }