Esempio n. 1
0
/**
 ****************************************************************************************
 * @brief Enable pad's and peripheral clocks assuming that peripherals' power domain is down. The Uart and SPi clocks are set.
 *
 * @return void
 ****************************************************************************************
 */
void periph_init(void)  // set i2c, spi, uart, uart2 serial clks
{
	// Power up peripherals' power domain
    SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);
    while (!(GetWord16(SYS_STAT_REG) & PER_IS_UP)) ; 
    
    SetBits16(CLK_16M_REG,XTAL16_BIAS_SH_DISABLE, 1);
	
	//rom patch
	patch_func();
	
	//Init pads
	set_pad_functions();


#if (BLE_APP_PRESENT)
    
#if BLE_PROX_MONITOR
    app_proxm_port_reinit(XCY_LED0_GPIO);
    app_button_enable();
#elif BLE_FINDME_LOCATOR
    app_button_enable();
#endif //BLE_PROX_REPORTER
#if BLE_BATTERY_SERVER
    app_batt_port_reinit();
#endif //BLE_BATTERY_SERVER

#endif //BLE_APP_PRESENT

    // Enable the pads
	SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);
}
Esempio n. 2
0
/**
 * Function for application main entry.
 */
int main(void)
{	
    init_leds();
    init_clock();

    uint32_t err_code;

    // Button configuration structure.
    static app_button_cfg_t p_button[] = {  {BUTTON_1, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler},
                                            {BUTTON_2, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler},
                                            {BUTTON_3, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler},
                                            {BUTTON_4, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}};
        
    // Macro for initializing the application timer module.
    // It will handle dimensioning and allocation of the memory buffer required by the timer, making sure that the buffer is correctly aligned. It will also connect the timer module to the scheduler (if specified).
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL);

    // Macro for initializing the GPIOTE module.
    // It will handle dimensioning and allocation of the memory buffer required by the module, making sure that the buffer is correctly aligned.
    APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS);

    // Initializing the buttons.
    err_code = app_button_init(p_button, sizeof(p_button) / sizeof(p_button[0]), BUTTON_DEBOUNCE_DELAY);
    APP_ERROR_CHECK(err_code);
                                            
    // Enabling the buttons.										
    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);

    while(true)
    {
        // Do nothing.
    }
}
Esempio n. 3
0
void nsec_buttons_init(void) {
    ret_code_t err_code;

    /*
     * The array must be static because a pointer to it will be saved in the
     * button handler module.
     */
    static
    app_button_cfg_t buttons[] =
    {
        {INPUT_UP,    false, NRF_GPIO_PIN_PULLUP, nsec_button_event_handler},
        {INPUT_DOWN,  false, NRF_GPIO_PIN_PULLUP, nsec_button_event_handler},
        {INPUT_BACK,  false, NRF_GPIO_PIN_PULLUP, nsec_button_event_handler},
        {INPUT_ENTER, false, NRF_GPIO_PIN_PULLUP, nsec_button_event_handler}
    };

    /*
     * Configure the button library
     */
    err_code = app_button_init(buttons,
            sizeof(buttons) / sizeof(buttons[0]),
                    BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    /*
     * Enable the buttons
     */
    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 4
0
uint32_t bsp_buttons_enable()
{
#if (BUTTONS_NUMBER > 0) && !defined(BSP_SIMPLE)
    return app_button_enable();
#else
    return NRF_ERROR_NOT_SUPPORTED;
#endif
}
Esempio n. 5
0
/**@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      = NRF_SUCCESS;
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            led_stop();
            
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            
            // Initialize the current heart rate to the average of max and min values. So that
            // everytime a new connection is made, the heart rate starts from the same value.
            m_cur_heart_rate = (MAX_HEART_RATE + MIN_HEART_RATE) / 2;

            // Start timers used to generate battery and HR measurements.
            application_timers_start();

            // Start handling button presses
            err_code = app_button_enable();
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            // Since we are not in a connection and have not started advertising, store bonds
            err_code = ble_bondmngr_bonded_masters_store();
            APP_ERROR_CHECK(err_code);

            // Go to system-off mode, should not return from this function, wakeup will trigger
            // a reset.
            system_off_mode_enter();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, 
                                                BLE_GAP_SEC_STATUS_SUCCESS, 
                                                &m_sec_params);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                led_stop();
                GPIO_WAKEUP_BUTTON_CONFIG(HR_INC_BUTTON_PIN_NO);
                GPIO_WAKEUP_BUTTON_CONFIG(HR_DEC_BUTTON_PIN_NO);
                system_off_mode_enter();
            }
            break;

        default:
            break;
    }

    APP_ERROR_CHECK(err_code);
}
Esempio n. 6
0
/**@brief Function for processing ANTFS pairing request event.
 */
static __INLINE void event_pairing_request_handle(void)
{
    const char * p_name     = antfs_hostname_get();     
    const uint32_t err_code = app_button_enable();   
    APP_ERROR_CHECK(err_code);    

    if (p_name != NULL)
    {
        printf("host name: %s\n", p_name);     
    }
}
Esempio n. 7
0
void buttons_init(void) {
    uint32_t err_code;
	  err_code = app_button_init(buttons, BUTTONS_NO, BUTTON_DETECTION_DELAY);
		APP_ERROR_CHECK(err_code);
	
	  err_code = app_timer_create(&m_button_long_press_timer_id,
                                APP_TIMER_MODE_SINGLE_SHOT,
                                button_long_press_timeout_handler);
    APP_ERROR_CHECK(err_code);
	
		app_button_enable();
}
Esempio n. 8
0
/**@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:
            led_stop();
            
            // Initialize the current heart rate to the average of max and min values. So that
            // everytime a new connection is made, the heart rate starts from the same value.
            m_cur_heart_rate = (MAX_HEART_RATE + MIN_HEART_RATE) / 2;

            // Start timers used to generate battery and HR measurements.
            application_timers_start();

            // Start handling button presses
            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:            
            // @note Flash access may not be complete on return of this API. System attributes are now
            // stored to flash when they are updated to ensure flash access on disconnect does not
            // result in system powering off before data was successfully written.

            // Go to system-off mode, should not return from this function, wakeup will trigger
            // a reset.
            system_off_mode_enter();
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                led_stop();
                
                nrf_gpio_cfg_sense_input(HR_INC_BUTTON_PIN_NO,
                                         BUTTON_PULL, 
                                         NRF_GPIO_PIN_SENSE_LOW);

                nrf_gpio_cfg_sense_input(HR_DEC_BUTTON_PIN_NO,
                                         BUTTON_PULL, 
                                         NRF_GPIO_PIN_SENSE_LOW);

                system_off_mode_enter();
            }
            break;

        default:
            // No implementation needed.
            break;
    }
}
Esempio n. 9
0
/**@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:
            nrf_gpio_pin_clear(LED_CONNECTED_PIN);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);

            user_connected = true;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_set(LED_CONNECTED_PIN);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);
            
            user_connected = false;
            advertising_start();
            twi_clear_motorshield();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            // Pairing not supported
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                   BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
                                                   NULL,
                                                   NULL);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            // No system attributes have been stored.
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            // No implementation needed.
            break;
    }
}
Esempio n. 10
0
/**@brief Function for the Button initialization.
 *
 * @details Initializes all Buttons used by this application.
 */
static void buttons_init(void)
{
    uint32_t err_code;

    static app_button_cfg_t buttons[] =
    {
        {BUTTON_ONE, false, BUTTON_PULL, button_event_handler}
    };

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the button module.
 */
static void buttons_init(void)
{
    uint32_t err_code;
    // Configure HR_INC_BUTTON_PIN_NO and HR_DEC_BUTTON_PIN_NO as wake up buttons and also configure
    // for 'pull up' because the eval board does not have external pull up resistors connected to
    // the buttons.
    static app_button_cfg_t buttons[] =
    {
        {BUTTON_CLEAN_LIST, false, BUTTON_PULL, button_event_handler} // Note: This pin is also BONDMNGR_DELETE_BUTTON_PIN_NO
    };

    APP_BUTTON_INIT(buttons, sizeof (buttons) / sizeof (buttons[0]), BUTTON_DETECTION_DELAY, false);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 12
0
/**@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 = NRF_SUCCESS;
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
    
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            led_stop();
            err_code = app_button_enable();
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            
            // Stop detecting button presses when not connected.
            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);
            
            err_code = ble_ams_c_service_store();
            APP_ERROR_CHECK(err_code);
            
            advertising_start();
            break;
            
        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                advertising_start();
            }
            break;
            
        case BLE_GATTC_EVT_TIMEOUT:
        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server and Client timeout events.
            err_code = sd_ble_gap_disconnect(m_conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;
            
        default:
            // No implementation needed.
            break;
    }
    APP_ERROR_CHECK(err_code);
}
Esempio n. 13
0
/**@brief Function for initializing the button handler module.
 */
void buttons_init(void)
{
    uint32_t err_code;
    
    // Configure buttons.
    static app_button_cfg_t buttons[] =
    {
        {BLE_BUTTON_PIN_NO,  false, BUTTON_PULL, button_event_handler},
        {GZLL_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler}
    };
    
    APP_BUTTON_INIT(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY, false);

    // Start handling button presses immediately.
    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 14
0
/**@brief Function for the Button initialization.
 *
 * @details Initializes all Buttons used by this application.
 */
static void button_init(void)
{
    uint32_t err_code;

    static app_button_cfg_t buttons[] =
    {
        {BSP_BUTTON_0, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_1, false, BUTTON_PULL, button_event_handler}
    };

    #define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 15
0
/**@brief Handles events coming from the LED Button central module.
 *
 * @param[in] p_lbs_c     The instance of LBS_C that triggered the event.
 * @param[in] p_lbs_c_evt The LBS_C event.
 */
static void lbs_c_evt_handler(ble_lbs_c_t * p_lbs_c, ble_lbs_c_evt_t * p_lbs_c_evt)
{
    const uint16_t conn_handle = p_lbs_c_evt->conn_handle;
    switch (p_lbs_c_evt->evt_type)
    {
    case BLE_LBS_C_EVT_DISCOVERY_COMPLETE:
    {
        ret_code_t err_code;

        NRF_LOG_PRINTF("[APP]: LED Button service discovered on conn_handle 0x%x\r\n",
                       conn_handle);

        err_code = app_button_enable();
        APP_ERROR_CHECK(err_code);

        // LED Button service discovered. Enable notification of Button.
        err_code = ble_lbs_c_button_notif_enable(p_lbs_c);
        APP_ERROR_CHECK(err_code);
    }
    break; // BLE_LBS_C_EVT_DISCOVERY_COMPLETE

    case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:
    {
        NRF_LOG_PRINTF("[APP]: Link 0x%x, Button state changed on peer to 0x%x\r\n",
                       conn_handle,
                       p_lbs_c_evt->params.button.button_state);
        if (p_lbs_c_evt->params.button.button_state)
        {
            LEDS_ON(LEDBUTTON_LED);
        }
        else
        {
            LEDS_OFF(LEDBUTTON_LED);
        }
    }
    break; // BLE_LBS_C_EVT_BUTTON_NOTIFICATION

    default:
        // No implementation needed.
        break;
    }
}
Esempio n. 16
0
/**@brief Function for initializing buttons.
 */
void button_init(void)
{
    // Initialize timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL);

    // Initialize and enable button handler module.
    static app_button_cfg_t buttons[] =
    {
        {BSP_BUTTON_0, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_1, false, BUTTON_PULL, button_event_handler},
    };

    uint32_t err_code = app_button_init(buttons,
                                        sizeof(buttons) / sizeof(buttons[0]),
                                        BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 17
0
int main (void)
{
    spi_hci_pad.pin = SPI_CS_PIN;
    spi_hci_pad.port = SPI_GPIO_PORT;

    // peripherals init
    periph_init();

    // App Initialization
    app_env.slave_on_sleep = SLAVE_NOT_ACTIVE;
    app_env.size_tx_queue = 0;
    app_env.size_rx_queue = 0;

#ifdef SPI_BOOTER
    spi_send_image();
#endif

    spi_init(&spi_hci_pad, SPI_WORD_MODE, SPI_ROLE, SPI_POL_MODE, SPI_PHA_MODE, SPI_MINT_MODE, SPI_FREQ_MODE);
    dready_irq_enable();
    app_button_enable();

    while(1)
    {
        if (app_env.slave_on_sleep == SLAVE_ACTIVE)
        {
            if (app_env.size_tx_queue > 0)
            {
                struct QueueElement *q = DeQueue(&SPITxQueue);
                spi_send_hci_msg((uint16_t)(q->bLength), (uint8_t *) (q->Data));
                free(q->Data);
                app_env.size_tx_queue--;
            }
        }

        if (app_env.size_rx_queue > 0)
        {
            BleReceiveMsg();
            app_env.size_rx_queue--;
        }
    }
}
Esempio n. 18
0
/**@brief Function for the Button initialization.
 *
 * @details Initializes all Buttons used by this application.
 */
static void buttons_init(void)
{
    uint32_t err_code;

    static app_button_cfg_t buttons[] =
    {
        {START_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler},
        {STOP_BUTTON_PIN_NO,  false, BUTTON_PULL, button_event_handler},
#ifdef COMMISSIONING_ENABLED
        {ERASE_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler}
#endif // COMMISSIONING_ENABLED
    };

    #define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 19
0
/**@brief Application main function.
 */
int main(void)
{
    uint32_t err_code;

    connected = false;

    mlog_init();
    timers_init();
    gpiote_init();
    buttons_init();
    step_counter_init();
    motor_init();
    led1_init();

    mlog_str("Starting MAIN...\r\n");

    bond_manager_init();
    ble_stack_init();
    radio_notification_init();

    // Initialize Bluetooth Stack parameters
    gap_params_init();
    advertising_init();
    services_init();
    conn_params_init();
    sec_params_init();

    // Actually start advertising
    //advertising_start();
    app_button_enable();

    // Enter main loop
    for (;;)
    {
        // Switch to a low power state until an event is available for the application
        err_code = sd_app_event_wait();
        APP_ERROR_CHECK(err_code);
    }
}
Esempio n. 20
0
static void button_init(void)
{
    uint32_t err_code;

    // Configure HR_INC_BUTTON_PIN_NO and HR_DEC_BUTTON_PIN_NO as wake up buttons and also configure
    // for 'pull up' because the eval board does not have external pull up resistors connected to
    // the buttons.
    static app_button_cfg_t buttons[] =
    {
        {BSP_BUTTON_0, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_1, false, BUTTON_PULL, button_event_handler},
        {BSP_BUTTON_2, false, BUTTON_PULL, button_event_handler}
    };

    #define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 21
0
/**@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 = NRF_SUCCESS;
	static ble_gap_evt_auth_status_t m_auth_status;
	ble_gap_enc_info_t * p_enc_info;
	switch (p_ble_evt->header.evt_id) {
	case BLE_GAP_EVT_CONNECTED:
		nrf_gpio_pin_set(CONNECTED_LED_PIN_N);
		nrf_gpio_pin_clear(ADVERTISING_LED_PIN_N);
		m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

		err_code = app_button_enable();
		break;
	case BLE_GAP_EVT_DISCONNECTED:
		nrf_gpio_pin_clear(CONNECTED_LED_PIN_N);
		m_conn_handle = BLE_CONN_HANDLE_INVALID;

		err_code = app_button_disable();
		if (err_code == NRF_SUCCESS) {
			advertising_start();
		}
		break;
	case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
		err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
				BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_params);
		break;
	case BLE_GATTS_EVT_SYS_ATTR_MISSING:
		err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
		break;

	case BLE_GAP_EVT_AUTH_STATUS:
		m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
		break;
	case BLE_GAP_EVT_SEC_INFO_REQUEST:
		p_enc_info = &m_auth_status.periph_keys.enc_info;
		if (p_enc_info->div
				== p_ble_evt->evt.gap_evt.params.sec_info_request.div) {
			err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info,
					NULL);
		} else {
// No keys found for this device
			err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL);
		}
		break;

	case BLE_GAP_EVT_TIMEOUT:
		if (p_ble_evt->evt.gap_evt.params.timeout.src
				== BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
			nrf_gpio_pin_clear(ADVERTISING_LED_PIN_N);

// Go to system-off mode (this function will not return; wakeup will cause a reset)
			GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN);
			uart_tx_str("reset by timeout");
			err_code = sd_power_system_off();
		}
		break;

	case BLE_GATTS_EVT_WRITE:
		break;

	case BLE_GAP_EVT_CONN_PARAM_UPDATE:
		nrf_gpio_pin_toggle (LEDBUTTON_LED_PIN_NO);
		break;

	default:
		break;
	}

	APP_ERROR_CHECK(err_code);
}
/**@brief Function for application main entry.
 */
int main(void)
{

    // Initialize	
//		nrf_gpio_cfg_output(23);
//		nrf_gpio_pin_write(23, 0);
//		nrf_gpio_pin_write(23, 1);
//		nrf_delay_ms(1000);
//		nrf_gpio_pin_write(23, 0);
		uart_init();
    leds_init();
    timers_init();
    gpiote_init();
    buttons_init();
		app_button_enable();
		wdt_init();	
#ifdef  DEBUG_LOG
		printf("uart_init\r\n");
		printf("leds_init\r\n");
		printf("timers_init\r\n");
		printf("gpiote_init\r\n");
		printf("buttons_init\r\n");
		printf("wdt_init\r\n");
#endif	
	
    bond_manager_init();
    ble_stack_init();
    gap_params_init();
#ifdef  DEBUG_LOG
		printf("bond_manager_init\r\n");
		printf("ble_stack_init\r\n");
		printf("gap_params_init\r\n");
#endif


    advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
    services_init();
    conn_params_init();
    sec_params_init();	
    radio_notification_init();
#ifdef  DEBUG_LOG
		printf("advertising_init\r\n");
		printf("services_init\r\n");
		printf("conn_params_init\r\n");
		printf("radio_notification_init\r\n");
#endif

    advertising_start();
#ifdef  DEBUG_LOG
		printf("advertising_start\r\n");
#endif		
		
		
			
		HTU21D_Init();
		
#ifdef  DEBUG_LOG
		printf("HTU21D_Init\r\n");
		printf("OLED_Init\r\n");
#endif		
		
		my_timer_init();
		Auto_Time_Set();
		
#ifdef  DEBUG_LOG
		printf("rtc my_timer_init\r\n");
		printf("Auto_Time_Set\r\n");
#endif
	
		wdt_start();
#ifdef  DEBUG_LOG
		printf("wdt_start\r\n");
		printf("Enter main loop\r\n");		
#endif	

		OLED_POWER_CONTROL(1);
		nrf_delay_ms(2000);
		OLED_POWER_CONTROL(0);
    for (;;)
    {
			
			updata_by_min();
			updata_by_hour();
			power_manage();
    }
}
Esempio n. 23
0
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t        err_code;
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
            nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO);
            nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO);
            
            // Start handling button presses
            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);

            m_advertising_mode = BLE_NO_ADV;
            m_conn_handle      = p_ble_evt->evt.gap_evt.conn_handle;
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);

            if (!m_is_link_loss_alerting)
            {
                nrf_gpio_pin_clear(ALERT_LEVEL_MILD_LED_PIN_NO);
                nrf_gpio_pin_clear(ALERT_LEVEL_HIGH_LED_PIN_NO);
            }

            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            // Since we are not in a connection and have not started advertising, store bonds.
            err_code = ble_bondmngr_bonded_centrals_store();
            APP_ERROR_CHECK(err_code);

            // Stop detecting button presses when not connected.
            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);

            advertising_start();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                   BLE_GAP_SEC_STATUS_SUCCESS,
                                                   &m_sec_params);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                if (m_advertising_mode == BLE_SLEEP)
                {
                    m_advertising_mode = BLE_NO_ADV;

                    nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
                    nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO);
                    nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO);

                    // Configure buttons with sense level low as wakeup source.
                    nrf_gpio_cfg_sense_input(SIGNAL_ALERT_BUTTON,
                                             BUTTON_PULL,
                                             NRF_GPIO_PIN_SENSE_LOW);
                    
                    nrf_gpio_cfg_sense_input(BONDMNGR_DELETE_BUTTON_PIN_NO,
                                             BUTTON_PULL,
                                             NRF_GPIO_PIN_SENSE_LOW);
                        
                    // 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);
                }
                else
                {
                    advertising_start();
                }
            }
            break;

        case BLE_GATTC_EVT_TIMEOUT:
        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server and Client timeout events.
            err_code = sd_ble_gap_disconnect(m_conn_handle, 
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            // No implementation needed.
            break;
    }
Esempio n. 24
0
/**@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;
    static ble_gap_evt_auth_status_t m_auth_status;
    ble_gap_enc_info_t *             p_enc_info;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);
            
            advertising_start();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                   BLE_GAP_SEC_STATUS_SUCCESS,
                                                   &m_sec_params);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_AUTH_STATUS:
            m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
            break;

        case BLE_GAP_EVT_SEC_INFO_REQUEST:
            p_enc_info = &m_auth_status.periph_keys.enc_info;
            if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div)
            {
                err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL);
                APP_ERROR_CHECK(err_code);
            }
            else
            {
                // No keys found for this device
                err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL);
                APP_ERROR_CHECK(err_code);
            }
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

                // Configure buttons with sense level low as wakeup source.
                nrf_gpio_cfg_sense_input(WAKEUP_BUTTON_PIN,
                                         BUTTON_PULL,
                                         NRF_GPIO_PIN_SENSE_LOW);
                
                // 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);
            }
            break;

        default:
            // No implementation needed.
            break;
    }
}
/**@brief Application's BLE Stack event handler.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t        err_code      = NRF_SUCCESS;
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
    
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            adv_led_blink_stop();

            if (m_is_link_loss_alerting)
            {
                alert_led_blink_stop();
            }
            
            m_advertising_mode = BLE_NO_ADV;
            m_conn_handle      = p_ble_evt->evt.gap_evt.conn_handle;
            
            // Start handling button presses
            err_code = app_button_enable();  // enable the button
						#if OLED096_Func 
						OLED_ShowString(0,16, (uint8_t*)"BT:CON");
	           OLED_Refresh_Gram();
						
						#endif
						
            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            if (!m_is_link_loss_alerting)
            {
                alert_led_blink_stop();
            }

            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            // Since we are not in a connection and have not started advertising, store bonds
            err_code = ble_bondmngr_bonded_masters_store();
            APP_ERROR_CHECK(err_code);

            advertising_start();
						#if OLED096_Func 
						OLED_ShowString(0,16, (uint8_t*)"BT:DIS");
	           OLED_Refresh_Gram();
						
						#endif
            break;
            
        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, 
                                                   BLE_GAP_SEC_STATUS_SUCCESS, 
                                                   &m_sec_params);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            { 
                if (m_advertising_mode == BLE_SLEEP)
                {
                    m_advertising_mode = BLE_NO_ADV;
                    adv_led_blink_stop();
                    alert_led_blink_stop();

									  GPIO_WAKEUP_BUTTON_CONFIG(SIGNAL_ALERT_BUTTON);
									  GPIO_WAKEUP_BUTTON_CONFIG(BONDMNGR_DELETE_BUTTON_PIN_NO);
									
                    // Go to system-off mode
                    // (this function will not return; wakeup will cause a reset)
								 #if 0	
                    err_code = sd_power_system_off();
                 #endif	
                 #if OLED096_Func 
					       	OLED_ShowString(0,16, (uint8_t*)"BT:Tout");
	               OLED_Refresh_Gram();
					       #endif									
                }
                else
                {
                    advertising_start();
                }
            }
            break;

        case BLE_GATTC_EVT_TIMEOUT:
        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server and Client timeout events.
            err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            break;

        default:
            break;
    }

    APP_ERROR_CHECK(err_code);
}
Esempio n. 26
0
/**@brief Application's BLE Stack event handler.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code = NRF_SUCCESS;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
            // Start detecting button presses
            err_code = app_button_enable();
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
            
            m_conn_handle               = BLE_CONN_HANDLE_INVALID;
            m_bps_meas_ind_conf_pending = false;

            // Stop detecting button presses when not connected
            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);

            // Since we are not in a connection and have not started advertising, store bonds
            err_code = ble_bondmngr_bonded_masters_store();
            APP_ERROR_CHECK(err_code);

            advertising_start();
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, 
                                                   BLE_GAP_SEC_STATUS_SUCCESS, 
                                                   &m_sec_params);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            {
                nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);

                // Go to system-off mode (this function will not return; wakeup will cause a reset)
                GPIO_WAKEUP_BUTTON_CONFIG(SEND_MEAS_BUTTON_PIN_NO);
                err_code = sd_power_system_off();    
            }
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            if (p_ble_evt->evt.gatts_evt.params.timeout.src == BLE_GATT_TIMEOUT_SRC_PROTOCOL)
            {
                err_code = sd_ble_gap_disconnect(m_conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            }
            break;

        default:
            break;
    }

    APP_ERROR_CHECK(err_code);
}
Esempio n. 27
0
/**@brief Function for enabling button input.
 */
static void buttons_enable(void)
{
    ret_code_t err_code;
    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}
Esempio n. 28
0
void app_findl_init(void)
{
    app_button_enable();
}
Esempio n. 29
0
uint32_t bsp_init(uint32_t type, uint32_t ticks_per_100ms, bsp_event_callback_t callback)
{
    uint32_t err_code = NRF_SUCCESS;

#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
    m_app_ticks_per_100ms = ticks_per_100ms;
    m_indication_type     = type;
#else
    UNUSED_VARIABLE(ticks_per_100ms);
#endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)

#if (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)
    m_registered_callback = callback;

    // BSP will support buttons and generate events
    if (type & BSP_INIT_BUTTONS)
    {
        uint32_t num;

        for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++)
        {
            err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);
        }

        if (err_code == NRF_SUCCESS)
        {
            err_code = app_button_init((app_button_cfg_t *)app_buttons,
                                       BUTTONS_NUMBER,
                                       ticks_per_100ms / 2);
        }

        if (err_code == NRF_SUCCESS)
        {
            err_code = app_button_enable();
        }

        if (err_code == NRF_SUCCESS)
        {
            err_code = app_timer_create(&m_button_timer_id,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        button_timer_handler);
        }
    }
#elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE)

    if (type & BSP_INIT_BUTTONS)
    {
        uint32_t cnt;
        uint32_t buttons[] = BUTTONS_LIST;

        for (cnt = 0; cnt < BUTTONS_NUMBER; cnt++)
        {
            nrf_gpio_cfg_input(buttons[cnt], BUTTON_PULL);
        }
    }
#endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)

#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)

    if (type & BSP_INIT_LED)
    {
        LEDS_OFF(LEDS_MASK);
        NRF_GPIO->DIRSET = LEDS_MASK;
    }

    // timers module must be already initialized!
    if (err_code == NRF_SUCCESS)
    {
        err_code =
            app_timer_create(&m_leds_timer_id, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler);
    }

    if (err_code == NRF_SUCCESS)
    {
        err_code =
            app_timer_create(&m_alert_timer_id, APP_TIMER_MODE_REPEATED, alert_timer_handler);
    }
#endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)

    return err_code;
}
Esempio n. 30
0
/**@brief Application's BLE Stack event handler.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t        err_code = NRF_SUCCESS;
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
    
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
            nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
            err_code = app_button_enable();
            m_advertising_mode = BLE_NO_ADVERTISING;
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            break;
            
        case BLE_GAP_EVT_DISCONNECTED:
            nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            // Since we are not in a connection and have not start to advertise either, store bonds.
            err_code = ble_bondmngr_bonded_masters_store();
            APP_ERROR_CHECK(err_code);

            // Stop detecting button presses when not connected
            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);

            // Stop detecting button presses when not connected
            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);

            err_code = ble_ans_c_service_store();

            advertising_start();
            break;
            
        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, 
                                                   BLE_GAP_SEC_STATUS_SUCCESS, 
                                                   &m_sec_params);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
            { 
                if (m_advertising_mode == BLE_FAST_ADVERTISING)
                {
                    advertising_start();
                }
                else
                {
                    nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
                    m_advertising_mode = BLE_NO_ADVERTISING;

                    // Go to system-off mode (function will not return; wakeup will cause a reset)
                    GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN);
                    err_code = sd_power_system_off();
                }
            }
            break;
            
        case BLE_GATTC_EVT_TIMEOUT:
        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server and Client timeout events.
            err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            break;

        default:
            break;
    }

    APP_ERROR_CHECK(err_code);
}