diff --git a/examples/platform/telink/common/include/AppConfigCommon.h b/examples/platform/telink/common/include/AppConfigCommon.h old mode 100755 new mode 100644 index 6af3da6bb213cc..b8850a76b6301c --- a/examples/platform/telink/common/include/AppConfigCommon.h +++ b/examples/platform/telink/common/include/AppConfigCommon.h @@ -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)) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index c49222db1a782e..4b918da82704a9 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -45,6 +45,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; @@ -256,28 +272,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 diff --git a/examples/platform/telink/util/include/ButtonManager.h b/examples/platform/telink/util/include/ButtonManager.h index d78837477e1918..e9ea1b60b17816 100644 --- a/examples/platform/telink/util/include/ButtonManager.h +++ b/examples/platform/telink/util/include/ButtonManager.h @@ -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; }; @@ -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)); diff --git a/examples/platform/telink/util/src/ButtonManager.cpp b/examples/platform/telink/util/src/ButtonManager.cpp index bdfa0b812a1ad4..3805a2640e193c 100644 --- a/examples/platform/telink/util/src/ButtonManager.cpp +++ b/examples/platform/telink/util/src/ButtonManager.cpp @@ -29,21 +29,19 @@ 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) @@ -51,22 +49,22 @@ 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); @@ -74,14 +72,14 @@ int Button::Init(void) } #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); @@ -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); @@ -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) @@ -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(); } diff --git a/examples/window-app/telink/src/AppTask.cpp b/examples/window-app/telink/src/AppTask.cpp index e79e03cc5b4058..12415839b6425c 100644 --- a/examples/window-app/telink/src/AppTask.cpp +++ b/examples/window-app/telink/src/AppTask.cpp @@ -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; @@ -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); diff --git a/src/platform/telink/tlsr9518adk80d.overlay b/src/platform/telink/tlsr9518adk80d.overlay index 6a9d70c32fb32c..9ff6b2bc3ebbf1 100644 --- a/src/platform/telink/tlsr9518adk80d.overlay +++ b/src/platform/telink/tlsr9518adk80d.overlay @@ -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 { @@ -77,4 +108,4 @@ }; /* region <0x1fe000 0x2000> is reserved for Telink B91 SDK's data */ }; -}; +}; \ No newline at end of file