Skip to content

Commit

Permalink
[Telink]: Update button manager to new gpio IRQ system (#26785)
Browse files Browse the repository at this point in the history
* [Telink]: Updated button manager to new gpio IRQ system

Signed-off-by: Misha Tkachenko <[email protected]>

* Restyled by whitespace

* Restyled by clang-format

* [Telink]: Fixed window app build

Signed-off-by: Misha Tkachenko <[email protected]>

* Restyled by clang-format

---------

Signed-off-by: Misha Tkachenko <[email protected]>
Co-authored-by: Misha Tkachenko <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Nov 1, 2023
1 parent 7129c64 commit 2518928
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 59 deletions.
17 changes: 11 additions & 6 deletions examples/platform/telink/common/include/AppConfigCommon.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
#pragma once

// Buttons config
#define BUTTON_PORT DEVICE_DT_GET(DT_NODELABEL(gpioc))

#define BUTTON_PIN_1 2
#define BUTTON_PIN_3 3
#define BUTTON_PIN_4 1
#define BUTTON_PIN_2 0
#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
#define BUTTON_FACTORY_RESET GPIO_DT_SPEC_GET(DT_NODELABEL(key_1), gpios)
#define BUTTON_BLE_START GPIO_DT_SPEC_GET(DT_NODELABEL(key_2), gpios)
#define BUTTON_THREAD_START GPIO_DT_SPEC_GET(DT_NODELABEL(key_3), gpios)
#define BUTTON_EXAMPLE_ACTION GPIO_DT_SPEC_GET(DT_NODELABEL(key_4), gpios)
#else
#define BUTTON_COL_1 GPIO_DT_SPEC_GET(DT_NODELABEL(key_matrix_col1), gpios)
#define BUTTON_COL_2 GPIO_DT_SPEC_GET(DT_NODELABEL(key_matrix_col2), gpios)
#define BUTTON_ROW_1 GPIO_DT_SPEC_GET(DT_NODELABEL(key_matrix_row1), gpios)
#define BUTTON_ROW_2 GPIO_DT_SPEC_GET(DT_NODELABEL(key_matrix_row2), gpios)
#endif

// LEDs config
#define LEDS_PORT DEVICE_DT_GET(DT_NODELABEL(gpiob))
Expand Down
32 changes: 24 additions & 8 deletions examples/platform/telink/common/src/AppTaskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ constexpr int kFactoryResetCalcTimeout = 3000;
constexpr int kFactoryResetTriggerCntr = 3;
constexpr int kAppEventQueueSize = 10;

#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
const struct gpio_dt_spec sFactoryResetButtonDt = BUTTON_FACTORY_RESET;
const struct gpio_dt_spec sBleStartButtonDt = BUTTON_BLE_START;
#if APP_USE_THREAD_START_BUTTON
const struct gpio_dt_spec sThreadStartButtonDt = BUTTON_THREAD_START;
#endif
#if APP_USE_EXAMPLE_START_BUTTON
const struct gpio_dt_spec sExampleActionButtonDt = BUTTON_EXAMPLE_ACTION;
#endif
#else
const struct gpio_dt_spec sButtonCol1Dt = BUTTON_COL_1;
const struct gpio_dt_spec sButtonCol2Dt = BUTTON_COL_2;
const struct gpio_dt_spec sButtonRow1Dt = BUTTON_ROW_1;
const struct gpio_dt_spec sButtonRow2Dt = BUTTON_ROW_2;
#endif

#if APP_USE_IDENTIFY_PWM
constexpr uint32_t kIdentifyBlinkRateMs = 200;
constexpr uint32_t kIdentifyOkayOnRateMs = 50;
Expand Down Expand Up @@ -312,28 +328,28 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void)
void AppTaskCommon::InitButtons(void)
{
#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler);
sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler);
sFactoryResetButton.Configure(&sFactoryResetButtonDt, FactoryResetButtonEventHandler);
sBleAdvStartButton.Configure(&sBleStartButtonDt, StartBleAdvButtonEventHandler);
#if APP_USE_EXAMPLE_START_BUTTON
if (ExampleActionEventHandler)
{
sExampleActionButton.Configure(BUTTON_PORT, BUTTON_PIN_2, ExampleActionButtonEventHandler);
sExampleActionButton.Configure(&sExampleActionButtonDt, ExampleActionButtonEventHandler);
}
#endif
#if APP_USE_THREAD_START_BUTTON
sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler);
sThreadStartButton.Configure(&sThreadStartButtonDt, StartThreadButtonEventHandler);
#endif
#else
sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler);
sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler);
sFactoryResetButton.Configure(&sButtonRow1Dt, &sButtonCol1Dt, FactoryResetButtonEventHandler);
sBleAdvStartButton.Configure(&sButtonRow2Dt, &sButtonCol2Dt, StartBleAdvButtonEventHandler);
#if APP_USE_EXAMPLE_START_BUTTON
if (ExampleActionEventHandler)
{
sExampleActionButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, ExampleActionButtonEventHandler);
sExampleActionButton.Configure(&sButtonRow1Dt, &sButtonCol2Dt, ExampleActionButtonEventHandler);
}
#endif
#if APP_USE_THREAD_START_BUTTON
sThreadStartButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler);
sThreadStartButton.Configure(&sButtonRow2Dt, &sButtonCol1Dt, StartThreadButtonEventHandler);
#endif
#endif

Expand Down
16 changes: 8 additions & 8 deletions examples/platform/telink/util/include/ButtonManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
class Button
{
public:
void Configure(const struct device * port, gpio_pin_t outPin, gpio_pin_t inPin, void (*callback)(void));
void Configure(const struct device * port, gpio_pin_t inPin, void (*callback)(void));
void Configure(const gpio_dt_spec * input_button_dt, const gpio_dt_spec * output_button_dt, void (*callback)(void));
void Configure(const gpio_dt_spec * input_button_dt, void (*callback)(void));
void Poll(Button * previous);
void PollIRQ(void);
void PollIRQ(const struct device * dev, uint32_t pins);
void SetCallback(void (*callback)(void));

private:
int Init(void);
int Deinit(void);

const struct device * mPort;
gpio_pin_t mOutPin;
gpio_pin_t mInPin;
int mPreviousState = STATE_LOW;
const struct gpio_dt_spec * mInput_button;
const struct gpio_dt_spec * mOutput_matrix_pin;
int mPreviousState = STATE_LOW;
struct gpio_callback mButton_cb_data;
void (*mCallback)(void) = NULL;
};

Expand All @@ -48,7 +48,7 @@ class ButtonManager
public:
void Init(void);
void Poll(void);
void PollIRQ(void);
void PollIRQ(const struct device * dev, uint32_t pins);
void AddButton(Button & button);
void SetCallback(unsigned int index, void (*callback)(void));

Expand Down
57 changes: 25 additions & 32 deletions examples/platform/telink/util/src/ButtonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,57 @@ LOG_MODULE_REGISTER(ButtonManager);
ButtonManager ButtonManager::sInstance;

#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
static struct gpio_callback button_cb_data;
void button_pressed(const struct device * dev, struct gpio_callback * cb, uint32_t pins);
#endif

void Button::Configure(const struct device * port, gpio_pin_t outPin, gpio_pin_t inPin, void (*callback)(void))
void Button::Configure(const gpio_dt_spec * input_button_dt, const gpio_dt_spec * output_button_dt, void (*callback)(void))
{
if (!device_is_ready(port))
if (!device_is_ready(input_button_dt->port))
{
LOG_ERR("%s is not ready\n", port->name);
LOG_ERR("Input port %s is not ready\n", input_button_dt->port->name);
}

mPort = port;
mOutPin = outPin;
mInPin = inPin;
mCallback = callback;
mInput_button = input_button_dt;
mOutput_matrix_pin = output_button_dt;
mCallback = callback;
}

int Button::Init(void)
{
int ret = 0;

#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
ret = gpio_pin_configure(mPort, mInPin, GPIO_INPUT | GPIO_PULL_UP);
ret = gpio_pin_configure_dt(mInput_button, GPIO_INPUT);
if (ret < 0)
{
LOG_ERR("Config in pin err: %d", ret);
return ret;
}

ret = gpio_pin_interrupt_configure(mPort, mInPin, GPIO_INT_EDGE_FALLING);
ret = gpio_pin_interrupt_configure_dt(mInput_button, GPIO_INT_EDGE_TO_ACTIVE);
if (ret < 0)
{
LOG_ERR("Config irq pin err: %d", ret);
return ret;
}

gpio_init_callback(&button_cb_data, button_pressed, mInPin);
ret = gpio_add_callback(mPort, &button_cb_data);
gpio_init_callback(&mButton_cb_data, button_pressed, BIT(mInput_button->pin));
ret = gpio_add_callback(mInput_button->port, &mButton_cb_data);
if (ret < 0)
{
LOG_ERR("Config gpio_init_callback err: %d", ret);
return ret;
}
#else

ret = gpio_pin_configure(mPort, mOutPin, GPIO_OUTPUT_ACTIVE);
ret = gpio_pin_configure_dt(mOutput_matrix_pin, GPIO_OUTPUT_ACTIVE);
if (ret < 0)
{
LOG_ERR("Config out pin err: %d", ret);
return ret;
}

ret = gpio_pin_configure(mPort, mInPin, GPIO_INPUT | GPIO_PULL_DOWN);
ret = gpio_pin_configure_dt(mInput_button, GPIO_INPUT);
if (ret < 0)
{
LOG_ERR("Config in pin err: %d", ret);
Expand All @@ -97,7 +95,7 @@ int Button::Deinit(void)
int ret = 0;

/* Reconfigure output key pin to input */
ret = gpio_pin_configure(mPort, mOutPin, GPIO_INPUT | GPIO_PULL_DOWN);
ret = gpio_pin_configure(mOutput_matrix_pin->port, mOutput_matrix_pin->pin, GPIO_INPUT | GPIO_PULL_DOWN);
if (ret < 0)
{
LOG_ERR("Reconfig out pin err: %d", ret);
Expand All @@ -120,7 +118,7 @@ void Button::Poll(Button * previous)
ret = Init();
assert(ret >= 0);

ret = gpio_pin_get(mPort, mInPin);
ret = gpio_pin_get_dt(mInput_button);
assert(ret >= 0);

if (ret == STATE_HIGH && ret != mPreviousState)
Expand Down Expand Up @@ -183,39 +181,34 @@ void ButtonEntry(void * param1, void * param2, void * param3)
void button_pressed(const struct device * dev, struct gpio_callback * cb, uint32_t pins)
{
ButtonManager & sInstance = ButtonManagerInst();
sInstance.PollIRQ();
sInstance.PollIRQ(dev, pins);
}

void ButtonManager::PollIRQ(void)
void ButtonManager::PollIRQ(const struct device * dev, uint32_t pins)
{
for (unsigned int i = 0; i < mButtons.size(); i++)
{
mButtons[i].PollIRQ();
mButtons[i].PollIRQ(dev, pins);
}
}

void Button::PollIRQ()
void Button::PollIRQ(const struct device * dev, uint32_t pins)
{
int ret = gpio_pin_get(mPort, mInPin);
if (ret == STATE_LOW)
if ((BIT(mInput_button->pin) & pins) && (mCallback != NULL) && (dev == mInput_button->port))
{
if (mCallback != NULL)
{
mCallback();
}
mCallback();
}
}

void Button::Configure(const struct device * port, gpio_pin_t inPin, void (*callback)(void))
void Button::Configure(const gpio_dt_spec * input_button_dt, void (*callback)(void))
{
if (!device_is_ready(port))
if (!device_is_ready(input_button_dt->port))
{
LOG_ERR("%s is not ready\n", port->name);
LOG_ERR("%s is not ready\n", input_button_dt->port->name);
}

mPort = port;
mInPin = inPin;
mCallback = callback;
mInput_button = input_button_dt;
mCallback = callback;

Init();
}
Expand Down
21 changes: 17 additions & 4 deletions examples/window-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ constexpr int kToggleMoveTypeTriggerTimeout = 700;

k_timer sToggleMoveTypeTimer;

#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
#define OPEN_WINDOW_BUTTON BUTTON_EXAMPLE_ACTION
#define CLOSE_WINDOW_BUTTON BUTTON_THREAD_START

const struct gpio_dt_spec sOpenWindowButtonDt = OPEN_WINDOW_BUTTON;
const struct gpio_dt_spec sCloseWindowButtonDt = CLOSE_WINDOW_BUTTON;
#else
const struct gpio_dt_spec sButtonCol1Dt = BUTTON_COL_1;
const struct gpio_dt_spec sButtonCol2Dt = BUTTON_COL_2;
const struct gpio_dt_spec sButtonRow1Dt = BUTTON_ROW_1;
const struct gpio_dt_spec sButtonRow2Dt = BUTTON_ROW_2;
#endif

Button sOpenButton;
Button sCloseButton;

Expand All @@ -40,11 +53,11 @@ CHIP_ERROR AppTask::Init(void)
InitCommonParts();

#if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
sOpenButton.Configure(BUTTON_PORT, BUTTON_PIN_2, OpenActionAndToggleMoveTypeButtonEventHandler);
sCloseButton.Configure(BUTTON_PORT, BUTTON_PIN_3, CloseActionButtonEventHandler);
sOpenButton.Configure(&sOpenWindowButtonDt, OpenActionAndToggleMoveTypeButtonEventHandler);
sCloseButton.Configure(&sCloseWindowButtonDt, CloseActionButtonEventHandler);
#else
sOpenButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_1, OpenActionAndToggleMoveTypeButtonEventHandler);
sCloseButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, CloseActionButtonEventHandler);
sOpenButton.Configure(&sButtonRow1Dt, &sButtonCol2Dt, OpenActionAndToggleMoveTypeButtonEventHandler);
sCloseButton.Configure(&sButtonRow2Dt, &sButtonCol1Dt, CloseActionButtonEventHandler);
#endif
ButtonManagerInst().AddButton(sOpenButton);
ButtonManagerInst().AddButton(sCloseButton);
Expand Down
33 changes: 32 additions & 1 deletion src/platform/telink/tlsr9518adk80d.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,37 @@
label = "PWM IDENTIFY LED Green";
};
};

keys {
/delete-node/ button_1;
/delete-node/ button_3;
compatible = "gpio-keys";
key_1: button_1 {
gpios = <&gpioc 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
key_2: button_2 {
gpios = <&gpioc 0 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
key_3: button_3 {
gpios = <&gpioc 3 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};
key_4: button_4 {
gpios = <&gpioc 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
};

key_matrix_col1: key_matrix_col1 {
gpios = <&gpioc 3 GPIO_ACTIVE_HIGH>;
};
key_matrix_col2: key_matrix_col2 {
gpios = <&gpioc 1 GPIO_ACTIVE_HIGH>;
};
key_matrix_row1: key_matrix_row1 {
gpios = <&gpioc 2 GPIO_PULL_DOWN>;
};
key_matrix_row2: key_matrix_row2 {
gpios = <&gpioc 0 GPIO_PULL_DOWN>;
};
};
};

&pinctrl {
Expand Down Expand Up @@ -77,4 +108,4 @@
};
/* region <0x1fe000 0x2000> is reserved for Telink B91 SDK's data */
};
};
};

0 comments on commit 2518928

Please sign in to comment.