I am writing a program that waits for Button 1 to be pressed. Once it is press, then it will call the function blecomm() in another c file. My code is as below:
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "app_error.h"
#include "boards.h"
#include "softdevice_handler.h"
#include "app_timer.h"
#include "bsp.h"
#include "bsp_btn_ble.h"
#include "sensorsim.h"
#include "nrf_gpio.h"
#include "nrf_error.h"
#define NRF_LOG_MODULE_NAME "APP"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "SEGGER_RTT.h"
#define NRF_LOG_USES_RTT 1
#define DEVICE_NAME "Main"
#define MANUFACTURER_NAME "NordicSemiconductor"
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
void blecomm();
/**@brief Function for the Timer initialization.
*
* @details Initializes the timer module. This creates and starts application timers.
*/
static void timers_init(void)
{
// Initialize timer module.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
}
/**@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 events from the BSP module.
*
* @param[in] event Event generated when button is pressed.
*/
static void bsp_event_handler(bsp_event_t event)
{
SEGGER_RTT_printf(0, "bsp_event_handler function");
switch (event)
{
case BSP_EVENT_SLEEP:
sleep_mode_enter();
break; // BSP_EVENT_SLEEP
case BSP_EVENT_KEY_0:
SEGGER_RTT_printf(0, "Button 1 is pressed!\n");
blecomm();
break;
case BSP_EVENT_KEY_1:
SEGGER_RTT_printf(0, "Button 2 is pressed!\n");
break;
default:
break;
}
}
/**@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);
}
/**@brief Function for application main entry.
*/
int main(void)
{
uint32_t err_code;
bool erase_bonds;
// Initialize.
err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
timers_init();
buttons_leds_init(&erase_bonds);
SEGGER_RTT_printf(0, "Template started\r\n");
nrf_gpio_pin_toggle(LED_1);
// Enter main loop.
while(true){
SEGGER_RTT_printf(0, "in loop \n");
if (NRF_LOG_PROCESS() == false)
{
SEGGER_RTT_printf(0, "false\n");
} else{
SEGGER_RTT_printf(0, "true\n");
}
}
// for(;;){
// SEGGER_RTT_printf(0, "for loop\n");
// }
}
When running this, it enters the while loop but when I press Button 1 on Nordic nRF51dk, it does not enter into the 'bsp_event_handler' function. What am I doing wrong here?
(Function blecomm() needs to be triggered upon Button 1 press)
I am using S130 with nRF51DK and Eclipse IDE.