From 00ad7458772fc90a5f2fed99ae54097e09a1449a Mon Sep 17 00:00:00 2001 From: Kyunghwan Kwon Date: Sat, 13 Jan 2024 22:16:59 +0900 Subject: [PATCH 1/2] Refactor main and add LED --- .gitignore | 1 + external/libmcu | 2 +- include/logging.h | 22 +++++++++ include/pinmap.h | 20 ++++++++ ports/esp-idf/gpio.c | 100 ++++++++++++++++++++++++++++++++++++++++ ports/esp-idf/start.c | 2 +- projects/external.cmake | 2 + projects/sources.cmake | 12 ++--- src/logging.c | 31 +++++++++++++ src/main.c | 42 ++++++----------- 10 files changed, 197 insertions(+), 37 deletions(-) create mode 100644 include/logging.h create mode 100644 include/pinmap.h create mode 100644 ports/esp-idf/gpio.c create mode 100644 src/logging.c diff --git a/.gitignore b/.gitignore index 378eac2..9785597 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build +.cache diff --git a/external/libmcu b/external/libmcu index 3038d5b..67e0722 160000 --- a/external/libmcu +++ b/external/libmcu @@ -1 +1 @@ -Subproject commit 3038d5b91f2ed705693763f95c682e049048c5e3 +Subproject commit 67e0722da61aea8cd91af7a3daaa9786667b672d diff --git a/include/logging.h b/include/logging.h new file mode 100644 index 0000000..0be737b --- /dev/null +++ b/include/logging.h @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LOGGING_H +#define LOGGING_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#include "libmcu/logging.h" + +void logging_stdout_backend_init(void); + +#if defined(__cplusplus) +} +#endif + +#endif /* LOGGING_H */ diff --git a/include/pinmap.h b/include/pinmap.h new file mode 100644 index 0000000..21ddc84 --- /dev/null +++ b/include/pinmap.h @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * + * SPDX-License-Identifier: MIT + */ + +#ifndef PINMAP_H +#define PINMAP_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#define PINMAP_LED 35 + +#if defined(__cplusplus) +} +#endif + +#endif /* PINMAP_H */ diff --git a/ports/esp-idf/gpio.c b/ports/esp-idf/gpio.c new file mode 100644 index 0000000..eed7852 --- /dev/null +++ b/ports/esp-idf/gpio.c @@ -0,0 +1,100 @@ +/* + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * + * SPDX-License-Identifier: MIT + */ + +#include "libmcu/gpio.h" +#include "libmcu/compiler.h" + +#include + +#include "driver/gpio.h" +#include "pinmap.h" + +struct gpio { + struct gpio_api api; + + uint16_t pin; + gpio_callback_t callback; + void *callback_ctx; +}; + +static void set_output(uint16_t pin) +{ + gpio_config_t io_conf = { + .intr_type = GPIO_INTR_DISABLE, + .mode = GPIO_MODE_INPUT_OUTPUT, + .pin_bit_mask = (1ULL << pin), + }; + gpio_config(&io_conf); +} + +static int enable_gpio(struct gpio *self) +{ + switch (self->pin) { + case PINMAP_LED: + set_output(self->pin); + break; + default: + return -ERANGE; + } + + return 0; +} + +static int disable_gpio(struct gpio *self) +{ + unused(self); + return 0; +} + +static int set_gpio(struct gpio *self, int value) +{ + return gpio_set_level(self->pin, (uint32_t)value); +} + +static int get_gpio(struct gpio *self) +{ + return gpio_get_level(self->pin); +} + +static int register_callback(struct gpio *self, + gpio_callback_t cb, void *cb_ctx) +{ + self->callback = cb; + self->callback_ctx = cb_ctx; + return 0; +} + +struct gpio *gpio_create(uint16_t pin) +{ + static struct gpio led; + + struct gpio *p; + + switch (pin) { + case PINMAP_LED: + p = &led; + break; + default: + return NULL; + } + + p->pin = pin; + + p->api = (struct gpio_api) { + .enable = enable_gpio, + .disable = disable_gpio, + .set = set_gpio, + .get = get_gpio, + .register_callback = register_callback, + }; + + return p; +} + +void gpio_delete(struct gpio *self) +{ + unused(self); +} diff --git a/ports/esp-idf/start.c b/ports/esp-idf/start.c index 14c0fd0..3fbcee5 100644 --- a/ports/esp-idf/start.c +++ b/ports/esp-idf/start.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon * * SPDX-License-Identifier: MIT */ diff --git a/projects/external.cmake b/projects/external.cmake index 7a24a01..96014bc 100644 --- a/projects/external.cmake +++ b/projects/external.cmake @@ -5,4 +5,6 @@ target_compile_definitions(libmcu PUBLIC _POSIX_C_SOURCE=200809L LIBMCU_NOINIT=__attribute__\(\(section\(\".rtc.data.libmcu\"\)\)\) METRICS_USER_DEFINES=\"${PROJECT_SOURCE_DIR}/include/metrics.def\" + + ${APP_DEFS} ) diff --git a/projects/sources.cmake b/projects/sources.cmake index e942a99..5f22f2b 100644 --- a/projects/sources.cmake +++ b/projects/sources.cmake @@ -1,12 +1,12 @@ -set(fpl-src-dirs src) -foreach(dir ${fpl-src-dirs}) - file(GLOB_RECURSE fpl_${dir}_SRCS RELATIVE ${CMAKE_SOURCE_DIR} ${dir}/*.c) - file(GLOB_RECURSE fpl_${dir}_CPP_SRCS RELATIVE ${CMAKE_SOURCE_DIR} ${dir}/*.cpp) - list(APPEND FPL_SRCS ${fpl_${dir}_SRCS} ${fpl_${dir}_CPP_SRCS}) +set(src-dirs src) +foreach(dir ${src-dirs}) + file(GLOB_RECURSE ${dir}_SRCS RELATIVE ${CMAKE_SOURCE_DIR} ${dir}/*.c) + file(GLOB_RECURSE ${dir}_CPP_SRCS RELATIVE ${CMAKE_SOURCE_DIR} ${dir}/*.cpp) + list(APPEND SRCS_TMP ${${dir}_SRCS} ${${dir}_CPP_SRCS}) endforeach() set(APP_SRCS - ${CMAKE_SOURCE_DIR}/src/main.c + ${SRCS_TMP} ) set(APP_INCS ${CMAKE_SOURCE_DIR}/include diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..85f1315 --- /dev/null +++ b/src/logging.c @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * + * SPDX-License-Identifier: MIT + */ + +#include "logging.h" +#include + +static size_t logging_stdout_writer(const void *data, size_t size) +{ + unused(size); + static char buf[LOGGING_MESSAGE_MAXLEN]; + size_t len = logging_stringify(buf, sizeof(buf)-1, data); + + buf[len++] = '\n'; + buf[len] = '\0'; + + const size_t rc = fwrite(buf, len, 1, stdout); + + return rc == 0? len : 0; +} + +void logging_stdout_backend_init(void) +{ + static struct logging_backend log_console = { + .write = logging_stdout_writer, + }; + + logging_add_backend(&log_console); +} diff --git a/src/main.c b/src/main.c index db0dc1f..e4789fd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,51 +1,35 @@ /* - * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon * * SPDX-License-Identifier: MIT */ -#include - #include "libmcu/board.h" -#include "libmcu/logging.h" - -static size_t logging_stdout_writer(const void *data, size_t size) -{ - unused(size); - static char buf[LOGGING_MESSAGE_MAXLEN]; - size_t len = logging_stringify(buf, sizeof(buf)-1, data); - - buf[len++] = '\n'; - buf[len] = '\0'; +#include "libmcu/timext.h" +#include "libmcu/gpio.h" - const size_t rc = fwrite(buf, len, 1, stdout); - - return rc == 0? len : 0; -} - -static void logging_stdout_backend_init(void) -{ - static struct logging_backend log_console = { - .write = logging_stdout_writer, - }; - - logging_add_backend(&log_console); -} +#include "pinmap.h" +#include "logging.h" int main(void) { board_init(); /* should be called very first. */ - logging_init(board_get_time_since_boot_ms); + logging_init(board_get_time_since_boot_ms); logging_stdout_backend_init(); const board_reboot_reason_t reboot_reason = board_get_reboot_reason(); - info("[%s] %s %s", board_get_reboot_reason_string(reboot_reason), board_get_serial_number_string(), board_get_version_string()); + struct gpio *led = gpio_create(PINMAP_LED); + gpio_enable(led); + while (1) { - /* hang */ + gpio_set(led, gpio_get(led) ^ 1); + sleep_ms(500); } + + return 0; } From f1aa784d65e8c2c6521e4d970dd6b05fb2262774 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 13 Jan 2024 13:19:37 +0000 Subject: [PATCH 2/2] Commit from GitHub Actions (CI) --- .github/memsize.baseline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/memsize.baseline b/.github/memsize.baseline index ca0d296..11703b7 100644 --- a/.github/memsize.baseline +++ b/.github/memsize.baseline @@ -1,2 +1,2 @@ text data bss dec hex filename - 177729 50704 371141 599574 92616 build/template.elf + 181721 51664 371173 604558 9398e build/template.elf