/** **************************************************************************************** * @brief Handles button press after cancel the jitter. * * @param[in] msgid Id of the message received * @param[in] param None * @param[in] dest_id TASK_APP * @param[in] src_id TASK_APP * * @return If the message was consumed or not. **************************************************************************************** */ int app_button_timer_handler(ke_msg_id_t const msgid, void const *param, ke_task_id_t const dest_id, ke_task_id_t const src_id) { switch(msgid) { case APP_SYS_BUTTON_1_TIMER: // make sure the button is pressed if(gpio_read_pin(BUTTON1_PIN) == GPIO_LOW) { if(APP_IDLE == ke_state_get(TASK_APP)) { struct app_proxr_env_tag *app_proxr_env = &app_env.proxr_ev; if(!app_proxr_env->enabled) { // start adv app_gap_adv_start_req(GAP_GEN_DISCOVERABLE|GAP_UND_CONNECTABLE, app_env.adv_data, app_set_adv_data(GAP_GEN_DISCOVERABLE), app_env.scanrsp_data, app_set_scan_rsp_data(app_get_local_service_flag()), GAP_ADV_FAST_INTV1, GAP_ADV_FAST_INTV2); #if (QN_DEEP_SLEEP_EN) // prevent entering into deep sleep mode sleep_set_pm(PM_SLEEP); #endif #if (FB_OLED) ke_timer_set(APP_OLED_STATE_DISPlAY_TIMER,TASK_APP,20); #endif } } else if(APP_ADV == ke_state_get(TASK_APP)) { // stop adv app_gap_adv_stop_req(); #if (QN_DEEP_SLEEP_EN) // allow entering into deep sleep mode sleep_set_pm(PM_DEEP_SLEEP); #endif #if (FB_OLED) ke_timer_set(APP_OLED_STATE_DISPlAY_TIMER,TASK_APP,20); #endif } } break; case APP_SYS_BUTTON_2_TIMER: if(gpio_read_pin(BUTTON2_PIN) == GPIO_LOW) { buzzer_off(); } break; default: ASSERT_ERR(0); break; } return (KE_MSG_CONSUMED); }
void app_event_accel_handler(void) { #if ((QN_DEEP_SLEEP_EN) && (!QN_32K_RCO)) if (sleep_env.deep_sleep) { sleep_env.deep_sleep = false; // start 32k xtal wakeup timer wakeup_32k_xtal_start_timer(); } #endif //QPRINTF("."); int8_t xdat[ACCEL_FIFO_DEPTH]; //int8_t ydat[ACCEL_FIFO_DEPTH]; accel_get_X_samples(xdat, ACCEL_FIFO_DEPTH); for(uint8_t i=0;i<ACCEL_FIFO_DEPTH;i++) { //fancy_pedometer(xdat[i], ydat[i]); //basic_pedometer(); better_pedometer(xdat[i]); } /*if(send_packet_bool) { send_packet(PROTOCOL_MODE_TX2); send_packet_bool = 0; }*/ ke_evt_clear(1UL << EVENT_ACCEL_INT_ID); sleep_set_pm(PM_SLEEP); }
/** **************************************************************************************** * @brief Handles button press after cancel the jitter. * * @param[in] msgid Id of the message received * @param[in] param None * @param[in] dest_id TASK_APP * @param[in] src_id TASK_APP * * @return If the message was consumed or not. **************************************************************************************** */ int app_button_timer_handler(ke_msg_id_t const msgid, void const *param, ke_task_id_t const dest_id, ke_task_id_t const src_id) { switch(msgid) { case APP_SYS_BUTTON_1_TIMER: // make sure the button is pressed if (gpio_read_pin(BUTTON1_PIN) == GPIO_LOW) { if (APP_IDLE == ke_state_get(TASK_APP)) { // start adv app_gap_adv_start_req(GAP_GEN_DISCOVERABLE, adv_data, sizeof(adv_data), scan_data, sizeof(scan_data), GAP_ADV_INTV1, GAP_ADV_INTV2); ke_state_set(TASK_APP, APP_ADV); #if (QN_DEEP_SLEEP_EN) // prevent entering into deep sleep mode sleep_set_pm(PM_SLEEP); #endif } else if (APP_ADV == ke_state_get(TASK_APP)) { // stop adv app_gap_adv_stop_req(); ke_state_set(TASK_APP, APP_IDLE); #if (QN_DEEP_SLEEP_EN) // allow entering into deep sleep mode sleep_set_pm(PM_DEEP_SLEEP); #endif } } break; default: ASSERT_ERR(0); break; } return (KE_MSG_CONSUMED); }
/** **************************************************************************************** * @brief BLE main function. * * This function is called right after the booting process has completed. **************************************************************************************** */ int main(void) { int ble_sleep_st, usr_sleep_st; // DC-DC dc_dc_enable(QN_DC_DC_ENABLE); // QN platform initialization #if QN_NVDS_WRITE plf_init(QN_POWER_MODE, __XTAL, QN_32K_RCO, nvds_tmp_buf, NVDS_TMP_BUF_SIZE); #else plf_init(QN_POWER_MODE, __XTAL, QN_32K_RCO, NULL, 0); #endif #if (defined(QN_9020_B1) && (!QN_PMU_VOLTAGE)) disable_patch_b1(); #endif // System initialization, user configuration SystemInit(); // Profiles register #if (QN_WORK_MODE != WORK_MODE_HCI) prf_register(); #endif // BLE stack initialization // Notes: // 1. When the chip works on Network Processor Mode, UART flow control signal is used to implement sleep mode. // UART 's flow control feature shall be enabled. Enable this feature in the uart.c file. // 2. Controller mode does not support sleep mode. // 3. So far client example project does not support sleep mode. It will be implemented later. // Check to go normal work mode or test mode. // If the input of test control pin is low level, the program will enter into test mode, otherwise the program will // enter into work mode which is defined in the user configuration file. #if (defined(QN_TEST_CTRL_PIN)) if(gpio_read_pin(QN_TEST_CTRL_PIN) == GPIO_HIGH) { #endif // Work mode defined in the usr_config.h ble_init((enum WORK_MODE)QN_WORK_MODE, QN_HCI_PORT, QN_HCI_RD, QN_HCI_WR, ble_heap, BLE_HEAP_SIZE, QN_BLE_SLEEP); #if (defined(QN_TEST_CTRL_PIN)) } else { // Test mode (controller mode) ble_init((enum WORK_MODE)WORK_MODE_HCI, QN_HCI_PORT, QN_HCI_RD, QN_HCI_WR, ble_heap, BLE_HEAP_SIZE, false); // In the test mode, the program moniter test control pin. If the input of test control ping changes to low level, // it means work mode should be switched to the mode defined in the user configuration file. gpio_set_interrupt(QN_TEST_CTRL_PIN, GPIO_INT_HIGH_LEVEL); gpio_enable_interrupt(QN_TEST_CTRL_PIN); } #endif set_max_sleep_duration(QN_BLE_MAX_SLEEP_DUR); // If QN902x works on wireless SoC mode, initialize APP task #if (QN_WORK_MODE == WORK_MODE_SOC) app_init(); #endif usr_init(); sleep_init(); wakeup_by_sleep_timer(__32K_TYPE); GLOBAL_INT_START(); ke_schedule(); //ke_timer_set(APP_SYS_SLEEP_TIMER, TASK_APP, 300); // start advertizing as soon as power is gained. // start adv app_gap_adv_start_req(GAP_GEN_DISCOVERABLE|GAP_UND_CONNECTABLE, app_env.adv_data, app_set_adv_data(GAP_GEN_DISCOVERABLE), app_env.scanrsp_data, app_set_scan_rsp_data(app_get_local_service_flag()), GAP_ADV_FAST_INTV1, GAP_ADV_FAST_INTV2); // prevent entering into deep sleep mode sleep_set_pm(PM_SLEEP); while(1) { ke_schedule(); // Checks for sleep have to be done with interrupt disabled GLOBAL_INT_DISABLE_WITHOUT_TUNER(); // Check whether the chip can enters sleep mode // // Chip enter sleep condition: // +--------+--------+--------+--------+--------+ // | USR | | | | | // | BLE | ACTIVE | IDLE | SLEEP | DEEP | // +--------+--------+--------+--------+--------+ // | ACTIVE | active | active | active | active | // | IDLE | active | idle | idle | idle | // | SLEEP | active | idle | sleep | deep | // +--------+--------+--------+--------+--------+ // Obtain the status of the user program usr_sleep_st = usr_sleep(); // If the user program can be sleep or deep sleep then check ble status if(usr_sleep_st != PM_ACTIVE) { // Obtain the status of ble sleep mode ble_sleep_st = ble_sleep(usr_sleep_st); // Check if the processor clock can be gated if(((ble_sleep_st == PM_IDLE) || (usr_sleep_st == PM_IDLE)) && (ble_sleep_st != PM_ACTIVE)) { // Debug led_set(5, LED_OFF); led_set(4, LED_ON); // led4 is on when enter into gating mode enter_sleep(SLEEP_CPU_CLK_OFF, WAKEUP_BY_ALL_IRQ_SOURCE, NULL); // Debug led_set(4, LED_OFF); led_set(5, LED_ON); // led5 is on when enter into active mode } // Check if the processor can be power down else if((ble_sleep_st == PM_SLEEP) && (usr_sleep_st == PM_SLEEP)) { // Debug led_set(5, LED_OFF); led_set(3, LED_ON); // led3 is on when enter into sleep mode enter_sleep(SLEEP_NORMAL, (WAKEUP_BY_OSC_EN | WAKEUP_BY_GPIO), sleep_cb); // Debug led_set(3, LED_OFF); led_set(5, LED_ON); // led5 is on when enter into active mode } // Check if the system can be deep sleep else if((ble_sleep_st == PM_SLEEP) && (usr_sleep_st == PM_DEEP_SLEEP)) { // Debug led_set(5, LED_OFF); led_set(2, LED_ON); // led2 is on when enter into deep sleep mode enter_sleep(SLEEP_DEEP, WAKEUP_BY_GPIO, sleep_cb); // Debug led_set(2, LED_OFF); led_set(5, LED_ON); // led5 is on when enter into active mode } } // Checks for sleep have to be done with interrupt disabled GLOBAL_INT_RESTORE_WITHOUT_TUNER(); } }
/** **************************************************************************************** * @brief Application task message handler **************************************************************************************** */ void app_task_msg_hdl(ke_msg_id_t const msgid, void const *param) { switch(msgid) { case GAP_SET_MODE_REQ_CMP_EVT: if(APP_IDLE == ke_state_get(TASK_APP)) { //usr_led1_set(LED_ON_DUR_ADV_FAST, LED_OFF_DUR_ADV_FAST); ke_timer_set(APP_ADV_INTV_UPDATE_TIMER, TASK_APP, 30 * 100); #if (defined(QN_ADV_WDT)) usr_env.adv_wdt_enable = true; #endif } else if(APP_ADV == ke_state_get(TASK_APP)) { //usr_led1_set(LED_ON_DUR_ADV_SLOW, LED_OFF_DUR_ADV_SLOW); #if (defined(QN_ADV_WDT)) usr_env.adv_wdt_enable = true; #endif } break; case GAP_ADV_REQ_CMP_EVT: //usr_led1_set(LED_ON_DUR_IDLE, LED_OFF_DUR_IDLE); ke_timer_clear(APP_ADV_INTV_UPDATE_TIMER, TASK_APP); break; case GAP_DISCON_CMP_EVT: //usr_led1_set(LED_ON_DUR_IDLE, LED_OFF_DUR_IDLE); // start adv app_gap_adv_start_req(GAP_GEN_DISCOVERABLE|GAP_UND_CONNECTABLE, app_env.adv_data, app_set_adv_data(GAP_GEN_DISCOVERABLE), app_env.scanrsp_data, app_set_scan_rsp_data(app_get_local_service_flag()), GAP_ADV_FAST_INTV1, GAP_ADV_FAST_INTV2); break; case GAP_LE_CREATE_CONN_REQ_CMP_EVT: if(((struct gap_le_create_conn_req_cmp_evt *)param)->conn_info.status == CO_ERROR_NO_ERROR) { if(GAP_PERIPHERAL_SLV == app_get_role()) { ke_timer_clear(APP_ADV_INTV_UPDATE_TIMER, TASK_APP); //usr_led1_set(LED_ON_DUR_CON, LED_OFF_DUR_CON); #if (defined(QN_ADV_WDT)) usr_env.adv_wdt_enable = false; #endif // Update cnx parameters //if (((struct gap_le_create_conn_req_cmp_evt *)param)->conn_info.con_interval > IOS_CONN_INTV_MAX) { // Update connection parameters here struct gap_conn_param_update conn_par; /// Connection interval minimum conn_par.intv_min = IOS_CONN_INTV_MIN; /// Connection interval maximum conn_par.intv_max = IOS_CONN_INTV_MAX; /// Latency conn_par.latency = IOS_SLAVE_LATENCY; /// Supervision timeout, Time = N * 10 msec conn_par.time_out = IOS_STO_MULT; app_gap_param_update_req(((struct gap_le_create_conn_req_cmp_evt *)param)->conn_info.conhdl, &conn_par); } //Added for WW timer activation after pairing restart_protocol_timer(); sleep_set_pm(PM_SLEEP); } } break; case QPPS_DISABLE_IND: break; case QPPS_CFG_INDNTF_IND: break; default: break; } }