This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

System ON/OFF Button doesn't work when UART is disconnected

Hi, Nordic Team. I'm using BC805M EK (nRF52805). In my code, I'm using SAADC (PPI interruption), TWI, BLE and System ON/OFF button

Q1) I don't need RTS, CTS on the UART. However, in uart_init() function, it is required to assign the pin number to RTS and CTS. What should I do, please? 

Q2) On the BC805M development board, there is a switch to connect/disconnect the UART with nRF52805. It also has a reset button. I disabled the reset function and changed this button to the System ON/OFF button. If I connect the UART, the button function works fine. However, if I disconnect the UART, and delete all of the UART functions in the code : uart_event_handle() and uart_init(), the System ON/OFF button function doesn't work. Any idea, please? 

Thank you! 

void uart_event_handle(app_uart_evt_t * p_event)
{
    
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
                if (index > 1)
                {
                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    do
                    {
                        uint16_t length = (uint16_t)index;
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);
                }

                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }

    
}




//snippet [UART Initialization] 
static void uart_init(void)
{
    
    uint32_t                     err_code;
    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = 12,   // BC805M EK: Rx = 12
        .tx_pin_no    = 16,   // BC805M EK: Tx = 16
        .rts_pin_no   = 197,
        .cts_pin_no   = 196,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
#if defined (UART_PRESENT)
        .baud_rate    = NRF_UART_BAUDRATE_115200
#else
        .baud_rate    = NRF_UARTE_BAUDRATE_115200
#endif
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);
    APP_ERROR_CHECK(err_code);
    
}

#define BTN_ID                0

/**@brief Handler for shutdown preparation.
 */
bool shutdown_handler(nrf_pwr_mgmt_evt_t event)
{

    uint32_t err_code;

    switch (event)
    {
        case NRF_PWR_MGMT_EVT_PREPARE_WAKEUP:
            NRF_LOG_INFO("NRF_PWR_MGMT_EVT_PREPARE_WAKEUP");
            err_code = bsp_buttons_disable();
            // Suppress NRF_ERROR_NOT_SUPPORTED return code.
            UNUSED_VARIABLE(err_code);

            err_code = bsp_wakeup_button_enable(BTN_ID);
            // Suppress NRF_ERROR_NOT_SUPPORTED return code.
            UNUSED_VARIABLE(err_code);

            break;
    }

    err_code = app_timer_stop_all();
    APP_ERROR_CHECK(err_code);

    return true;
}




/**@brief Register application shutdown handler with priority 0. */
NRF_PWR_MGMT_HANDLER_REGISTER(shutdown_handler, 0);




/**@brief Function for handling BSP events.
 */
static void bsp_evt_handler(bsp_event_t evt)
{
#if NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED
    nrf_pwr_mgmt_feed();
    NRF_LOG_INFO("Power management fed");
#endif // NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED

    switch (evt)
    {
        
        case BSP_EVENT_DEFAULT:
            printf("1button pressed\r\n");
            break;

        case BSP_EVENT_SYSOFF:
                printf("2button released\r\n");
                // Application will prepare the wakeup mechanism: NRF_PWR_MGMT_EVT_PREPARE_WAKEUP
                /**@brief Function for shutting down the system.
                 * @param[in] shutdown_type     Type of operation.
                 * @details All callbacks will be executed prior to shutdown.
                 */
                // sd_power_system_off();
                 nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
                // NRF_LOG_INFO("2) System OFF is entered\r\n");
                // NRF_POWER->SYSTEMOFF = 1;
            
            break;


        default:
            return; // no implementation needed
    }
}




/**@brief Function for initializing the BSP module.
 */
static void bsp_configuration()
{
    uint32_t err_code;

    err_code = bsp_init(BSP_INIT_BUTTONS, bsp_evt_handler);
    APP_ERROR_CHECK(err_code);

    err_code = bsp_event_to_button_action_assign(BTN_ID,
                                                 BSP_BUTTON_ACTION_LONG_PUSH,
                                                 BSP_EVENT_DEFAULT);
    APP_ERROR_CHECK(err_code);

    err_code = bsp_event_to_button_action_assign(BTN_ID,
                                                 BSP_BUTTON_ACTION_RELEASE,
                                                 BSP_EVENT_SYSOFF);
    APP_ERROR_CHECK(err_code);

}




// In pca10040.h, I did some modifications

#define BUTTONS_NUMBER 1

#define BUTTON_START   21
#define BUTTON_1       21
#define BUTTON_STOP    21
#define BUTTON_PULL    NRF_GPIO_PIN_PULLUP

#define BUTTONS_ACTIVE_STATE 0

#define BUTTONS_LIST { BUTTON_1 }

#define BSP_BUTTON_0   BUTTON_1

Parents
  • Hi,

    Q1) I don't need RTS, CTS on the UART. However, in uart_init() function, it is required to assign the pin number to RTS and CTS. What should I do, please? 

    Set it to UART_PIN_DISCONNECTED

     it's defined in app_uart.h like this:

    #define  UART_PIN_DISCONNECTED 0xFFFFFFFF /**< Value indicating that no pin is connected to this UART register. */
    

    Q2)

    You are using pin.21? If you have CONFIG_GPIO_AS_PINRESET , then you cannot use this as a normal GPIO pin.

  • Hi Sigurd, Thanks for your help! 

    1) Yes, I have disabled the reset pin. I mean, I deleted the following word. 

    Deleted the following codes from system_nrf52805.c

        /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
          defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
          reserved for PinReset and not available as normal GPIO. */
        #if defined (CONFIG_GPIO_AS_PINRESET)
    
            #define RESET_PIN 21
    
            if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
                ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
                NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
                while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
                NRF_UICR->PSELRESET[0] = RESET_PIN;
                while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
                NRF_UICR->PSELRESET[1] = RESET_PIN;
                while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
                NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
                while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
                NVIC_SystemReset();
            }
        #endif

    2) According to your info, I have used UART_PIN_DISCONNECTED for UART rts and cts unused pins. However, the button pin still doesn't work. 

    What I found is: 

    When I connect the button to pin 21: pin 21 is pulled up and

    - When BLE is advertising (not connected), System ON/OFF button works well. 

    - When BLE is connected, System ON/OFF button doesn't work. When I press the button, nothing happens. 

    Any idea, please? Thanks in advance! 

  • StevenW807 said:
    Any idea, please? Thanks in advance! 

     If you have bsp_btn_ble.c in your project, then connection_buttons_configure() is called when you connect (BLE_GAP_EVT_CONNECTED)

    Snippet from bsp_btn_ble.c

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        uint32_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                if (m_num_connections == 0)
                {
                    err_code = connection_buttons_configure();
                    CALL_HANDLER_ON_ERROR(err_code);
                }

    This might interfere with how you configured bsp in your bsp_configuration() function.

  • After I commented 

    err_code = connection_buttons_configure();
    CALL_HANDLER_ON_ERROR(err_code);

    in bsp_btn_ble.c, problem solved! 

    Now I can turn ON/OFF the PCB at anytime (no matter BLE is connected or not). Thanks Sigurd! 

Reply Children
No Data
Related