Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor main and add LED #13

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/memsize.baseline
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
.cache
2 changes: 1 addition & 1 deletion external/libmcu
Submodule libmcu updated 49 files
+1 −1 .github/workflows/release.yml
+28 −0 CHANGELOG.md
+0 −2 README.md
+18 −22 examples/memory_kvstore.c
+38 −7 interfaces/adc/include/libmcu/adc.h
+0 −30 interfaces/adc/include/libmcu/port/adc.h
+0 −53 interfaces/adc/src/adc.c
+54 −0 interfaces/flash/include/libmcu/flash.h
+31 −6 interfaces/gpio/include/libmcu/gpio.h
+0 −29 interfaces/gpio/include/libmcu/port/gpio.h
+0 −43 interfaces/gpio/src/gpio.c
+49 −14 interfaces/i2c/include/libmcu/i2c.h
+0 −35 interfaces/i2c/include/libmcu/port/i2c.h
+0 −91 interfaces/i2c/src/i2c.c
+23 −0 interfaces/kvstore/include/libmcu/flash_kvstore.h
+13 −16 interfaces/kvstore/include/libmcu/kvstore.h
+0 −0 interfaces/kvstore/include/libmcu/nvs_kvstore.h
+0 −30 interfaces/pwm/include/libmcu/port/pwm.h
+37 −10 interfaces/pwm/include/libmcu/pwm.h
+0 −99 interfaces/pwm/src/pwm.c
+0 −29 interfaces/spi/include/libmcu/port/spi.h
+32 −6 interfaces/spi/include/libmcu/spi.h
+0 −44 interfaces/spi/src/spi.c
+0 −29 interfaces/timer/include/libmcu/port/timer.h
+30 −10 interfaces/timer/include/libmcu/timer.h
+0 −81 interfaces/timer/src/timer.c
+0 −27 interfaces/uart/include/libmcu/port/uart.h
+34 −4 interfaces/uart/include/libmcu/uart.h
+0 −38 interfaces/uart/src/uart.c
+0 −32 interfaces/wifi/include/libmcu/port/wifi.h
+48 −10 interfaces/wifi/include/libmcu/wifi.h
+0 −55 interfaces/wifi/src/wifi.c
+9 −0 modules/common/include/libmcu/board.h
+23 −0 modules/common/include/libmcu/hash.h
+6 −0 modules/common/src/board.c
+35 −0 modules/common/src/hash.c
+1 −1 ports/armcm/fault.c
+15 −22 ports/esp-idf/nvs_kvstore.c
+42 −30 ports/esp-idf/wifi.c
+408 −0 ports/kvstore/flash_kvstore.c
+20 −29 ports/nrf52/nvs_kvstore.c
+151 −0 ports/stm32/flash.c
+1 −5 projects/interfaces.cmake
+1 −4 projects/interfaces.mk
+58 −0 tests/mocks/gpio.cpp
+33 −13 tests/mocks/spi.cpp
+26 −0 tests/runners/common/flash_kvstore.mk
+1 −0 tests/runners/examples/memory_kvstore.mk
+472 −0 tests/src/common/flash_kvstore_test.cpp
22 changes: 22 additions & 0 deletions include/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@mononn.com>
*
* 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 */
20 changes: 20 additions & 0 deletions include/pinmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@mononn.com>
*
* 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 */
100 changes: 100 additions & 0 deletions ports/esp-idf/gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@libmcu.org>
*
* SPDX-License-Identifier: MIT
*/

#include "libmcu/gpio.h"
#include "libmcu/compiler.h"

#include <errno.h>

#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);
}
2 changes: 1 addition & 1 deletion ports/esp-idf/start.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@mononn.com>
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@libmcu.org>
*
* SPDX-License-Identifier: MIT
*/
Expand Down
2 changes: 2 additions & 0 deletions projects/external.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
12 changes: 6 additions & 6 deletions projects/sources.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 31 additions & 0 deletions src/logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@mononn.com>
*
* SPDX-License-Identifier: MIT
*/

#include "logging.h"
#include <stdio.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';

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);
}
42 changes: 13 additions & 29 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@mononn.com>
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@libmcu.org>
*
* SPDX-License-Identifier: MIT
*/

#include <stdio.h>

#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;
}