-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
197 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
build | ||
.cache |
Submodule libmcu
updated
49 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <[email protected]> | ||
* | ||
* 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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <[email protected]> | ||
* | ||
* 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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <[email protected]> | ||
* | ||
* 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <[email protected]> | ||
* | ||
* 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |