Slide Switch Module Application Note

V1.2

2024/03/11

Revision History

Version

Date

Description

V1.0.0.0

2021/12/01

Stable release

V1.1

2023/05/12

Optimize content

V1.2

2024/03/11

Optimize content

Table List

Figure List

Glossary

Terms

Definitions

GPIO

General-Purpose Input/Output

1 Introduction

This document provides overview of slide switch module. The following topics are included:

  • Configuration in McuConfigTool

  • Source code review

Macro F_APP_SLIDE_SWITCH_SUPPORT should be defined when slide switch module is used.

2 McuConfigTool Configuration

This chapter will show you how to use McuConfigTool for slide switch configuration.

Slide switch can be enabled in “HW Feature” page as shown in following figure.

Figure 2-1 Slide Switch Enable


Slide switch pin can be choosed from available GPIO pins.

Figure 2-2 Slide Switch Pinmux Config


Following action items can be selected as slide switch high/low action.

Figure 2-3 Slide Switch Action Selection


3 Source Code Overview

This chapter will show the source code of slide switch module, and its flow chart is as follows.

Figure 3-1 Slide Switch Flow

3.1 Slide Switch Initialization

The components of slide switch initialization are as follows:

  • Configuration initialization

  • Driver initialization

  • Register callback function

The following API copys app configuration to local storage. Vendor actions can be added here without using McuConfig Tool.

void app_slide_switch_cfg_init(void)
{
    uint8_t i = 0;
    for (i = 0; i < APP_SLIDE_SWITCH_TOTAL; i++)
    {
        if (i == 0)
        {
            slide_switch[i].support = app_cfg_const.slide_switch_0_support;
            slide_switch[i].low_action = (T_SWITCH_ACTION)app_cfg_const.slide_switch_0_low_action;
            slide_switch[i].high_action = (T_SWITCH_ACTION)app_cfg_const.slide_switch_0_high_action;
            slide_switch[i].pinmux = app_cfg_const.slide_switch_0_pinmux;
        }
        else if (i == 1)
        {
            slide_switch[i].support = app_cfg_const.slide_switch_1_support;
            slide_switch[i].low_action = (T_SWITCH_ACTION)app_cfg_const.slide_switch_1_low_action;
            slide_switch[i].high_action = (T_SWITCH_ACTION)app_cfg_const.slide_switch_1_high_action;
            slide_switch[i].pinmux = app_cfg_const.slide_switch_1_pinmux;
        }
    }
}

The following API does driver initialization.

void app_slide_switch_driver_init(void)
{
    uint8_t i = 0;
    for (i = 0; i < APP_SLIDE_SWITCH_TOTAL; i++)
    {
        if (slide_switch[i].support)
        {
            app_slide_switch_x_driver_init((T_APP_SLIDE_SWITCH_ID)i);
            app_dlps_pad_wake_up_polarity_invert(slide_switch[i].pinmux);
        }
    }
}

In the following API, registering necessary functions is done.

void app_slide_switch_init(void)
{
    sys_mgr_cback_register(app_slide_switch_dm_cback);
    app_timer_reg_cb(app_slide_switch_timeout_cb, &app_slide_switch_timer_id);
}

3.2 Deal with Slide Switch Interruption

This chapter will show the procedure for slide switch interruption.
IO message is sent in slide switch interrupt callback function.

void app_slide_switch_intr_cb(uint32_t param)
{
    ...

    gpio_msg.type = IO_MSG_TYPE_GPIO;
    if (id == APP_SLIDE_SWITCH_0)
    {
        gpio_msg.subtype = IO_MSG_GPIO_SLIDE_SWITCH_0;
    }
    else if (id == APP_SLIDE_SWITCH_1)
    {
        gpio_msg.subtype = IO_MSG_GPIO_SLIDE_SWITCH_1;
    }

    gpio_msg.u.param = gpio_status;

    app_io_send_msg(&gpio_msg);
}

When slide switch message received, debounce timer is started.

void app_slide_switch_handle_msg(T_IO_MSG *msg)
{
    ...

    app_start_timer(&timer_idx_slide_switch_debounce[channel], "slide_switch_debouce",
                    app_slide_switch_timer_id, timer_chann, 0, false,
                    APP_SLIDE_SWITCH_DEBOUCE_TIME);
}

After debounce timer timeout, specific level action can be exeuted.

static void app_slide_switch_timeout_cb(uint8_t timer_evt, uint16_t param)
{
    ...
                if (gpio_status)
                {
                    app_slide_switch_action_handle(slide_switch[channel].high_action);
                }
                else
                {
                    app_slide_switch_action_handle(slide_switch[channel].low_action);
                }
    ...
}

Executing the actions in the following function, and vendor actions can be handled here if needed.

static void app_slide_switch_action_handle(T_SWITCH_ACTION action)
{
    APP_PRINT_INFO1("app_slide_switch_action_handle: action %d", action);
    switch (action)
    {
    case ACTION_ANC_ON:
        {
            ...
        }
        break;

    case ACTION_ANC_OFF:
        {
            ...
        }
        break;

        ...

    default:
        break;
    }
}

3.3 Slide Switch Low Power Mode

Low power mode related procedure will include the following parts:

  • Pad setting before entering power down mode

Considering power consumption, pad will be pulled up or down according to current io level.

static void app_slide_switch_enter_dlps(void)
{
    uint8_t i = 0;
    POWERMode lps_mode = power_mode_get();

    if (lps_mode == POWER_POWERDOWN_MODE)
    {
        for (i = 0; i < APP_SLIDE_SWITCH_TOTAL; i++)
        {
             ...

            //to reduce power consumption
            if (slide_switch[i].support)
            {
                if (hal_gpio_get_input_level(slide_switch[i].pinmux))
                {
                    Pad_Config(slide_switch[i].pinmux,
                               PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH);
                }
                else
                {
                    Pad_Config(slide_switch[i].pinmux,
                               PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_DOWN, PAD_OUT_DISABLE, PAD_OUT_HIGH);
                }
            }
        }
    }
}