From 538ae1d21f7fd78b628d6b9f0b85b2b54eb28795 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Mon, 13 Mar 2023 09:29:05 +0800 Subject: [PATCH] Added GPIO gem to machine gembox. Signed-off-by: Yilin Sun --- CMakeLists.txt | 1 + mrbgems/machine-devmem/src/devmem.c | 17 ++++++----- mrbgems/machine-gpio/mrbgem.rake | 5 ++++ mrbgems/machine-gpio/src/gpio.c | 45 +++++++++++++++++++++++++++++ mrbgems/machine-gpio/src/gpio.h | 15 ++++++++++ mrbgems/utilities.gembox | 1 + 6 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 mrbgems/machine-gpio/mrbgem.rake create mode 100644 mrbgems/machine-gpio/src/gpio.c create mode 100644 mrbgems/machine-gpio/src/gpio.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ff6cf5..72a5b17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ add_dependencies(${PROJECT_NAME} mruby_libs) target_include_directories(${PROJECT_NAME} INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/mruby/${MRUBY_MCUX_TARGET}/include" "mruby/include" + "mrbgems" ) target_link_directories(${PROJECT_NAME} INTERFACE diff --git a/mrbgems/machine-devmem/src/devmem.c b/mrbgems/machine-devmem/src/devmem.c index 91be90b..8da2f0c 100644 --- a/mrbgems/machine-devmem/src/devmem.c +++ b/mrbgems/machine-devmem/src/devmem.c @@ -72,7 +72,7 @@ static mrb_value mrb_write8(mrb_state *mrb, mrb_value self) { mrb_get_args(mrb, "ii", &addr, &value); uint32_t addr_int = (uint32_t)addr; - uint8_t value_int = (uint8_t)value; + uint8_t value_int = (uint8_t)value; *(volatile uint8_t *)(addr_int) = value_int; @@ -80,15 +80,16 @@ static mrb_value mrb_write8(mrb_state *mrb, mrb_value self) { } void mrb_machine_devmem_gem_init(mrb_state *mrb) { - struct RClass *class_peekpoke = mrb_define_module(mrb, "Machine"); + struct RClass *module_machine = mrb_define_module(mrb, "Machine"); + struct RClass *class_devmem = mrb_define_class_under(mrb, module_machine, "DevMem", mrb->object_class); - mrb_define_class_method(mrb, class_peekpoke, "read32", mrb_read32, MRB_ARGS_ARG(1, 0)); - mrb_define_class_method(mrb, class_peekpoke, "read16", mrb_read16, MRB_ARGS_ARG(1, 0)); - mrb_define_class_method(mrb, class_peekpoke, "read8", mrb_read8, MRB_ARGS_ARG(1, 0)); + mrb_define_class_method(mrb, class_devmem, "read32", mrb_read32, MRB_ARGS_ARG(1, 0)); + mrb_define_class_method(mrb, class_devmem, "read16", mrb_read16, MRB_ARGS_ARG(1, 0)); + mrb_define_class_method(mrb, class_devmem, "read8", mrb_read8, MRB_ARGS_ARG(1, 0)); - mrb_define_class_method(mrb, class_peekpoke, "write32", mrb_write32, MRB_ARGS_ARG(2, 0)); - mrb_define_class_method(mrb, class_peekpoke, "write16", mrb_write16, MRB_ARGS_ARG(2, 0)); - mrb_define_class_method(mrb, class_peekpoke, "write8", mrb_write8, MRB_ARGS_ARG(2, 0)); + mrb_define_class_method(mrb, class_devmem, "write32", mrb_write32, MRB_ARGS_ARG(2, 0)); + mrb_define_class_method(mrb, class_devmem, "write16", mrb_write16, MRB_ARGS_ARG(2, 0)); + mrb_define_class_method(mrb, class_devmem, "write8", mrb_write8, MRB_ARGS_ARG(2, 0)); } void mrb_machine_devmem_gem_final(mrb_state *mrb) { diff --git a/mrbgems/machine-gpio/mrbgem.rake b/mrbgems/machine-gpio/mrbgem.rake new file mode 100644 index 0000000..ba0207d --- /dev/null +++ b/mrbgems/machine-gpio/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('machine-gpio') do |spec| + spec.license = 'MIT' + spec.author = 'imi415' + spec.summary = 'GPIO pin resource access' +end \ No newline at end of file diff --git a/mrbgems/machine-gpio/src/gpio.c b/mrbgems/machine-gpio/src/gpio.c new file mode 100644 index 0000000..dccc98f --- /dev/null +++ b/mrbgems/machine-gpio/src/gpio.c @@ -0,0 +1,45 @@ +#include +#include + +/* Private */ +#include "gpio.h" + +typedef struct { + uint32_t pin; + mrbgems_machine_gpio_mode_t mode; + bool value; +} mrb_gpio_t; + +static mrb_value mrb_gpio_initialize(mrb_state *mrb, mrb_value self) { + mrb_gpio_t *pin = mrb_malloc(mrb, sizeof(mrb_gpio_t)); + + mrb_value init_cfg = mrb_get_arg1(mrb); + mrb_ensure_hash_type(mrb, init_cfg); + + /* TODO: Allocate mrb_gpio_t and store configuration */ + + /* TODO: Initialize GPIO */ + mrb_machine_impl_gpio_init(0, MRBGEMS_MACHINE_GPIO_MODE_INPUT); + return self; +} + +void mrb_machine_gpio_gem_init(mrb_state *mrb) { + /** + * Example: + * pin_led = Machine::GPIO.new({pin: 10, mode: Machine::GPIO::OUTPUT_OD}) + * pin_btn = Machine::GPIO.new({pin: 2}) # Default mode is Machine::GPIO::INPUT + * pin_led.on # Set to logical high + * pin_led.off # Set to logical low + * pin_led.toggle # Toggle. + */ + + + struct RClass *module_machine = mrb_define_module(mrb, "Machine"); + struct RClass *class_gpio = mrb_define_class_under(mrb, module_machine, "GPIO", mrb->object_class); + + mrb_define_method(mrb, class_gpio, "initialize", mrb_gpio_initialize, MRB_ARGS_ARG(1, 0)); +} + +void mrb_machine_gpio_gem_final(mrb_state *mrb) { + /* Unused */ +} \ No newline at end of file diff --git a/mrbgems/machine-gpio/src/gpio.h b/mrbgems/machine-gpio/src/gpio.h new file mode 100644 index 0000000..4361108 --- /dev/null +++ b/mrbgems/machine-gpio/src/gpio.h @@ -0,0 +1,15 @@ +#ifndef MRBGEMS_MACHINE_GPIO_GPIO_H +#define MRBGEMS_MACHINE_GPIO_GPIO_H + +#include + +typedef enum { + MRBGEMS_MACHINE_GPIO_MODE_DISABLED, + MRBGEMS_MACHINE_GPIO_MODE_INPUT, + MRBGEMS_MACHINE_GPIO_MODE_OUTPUT_PUSHPULL, + MRBGEMS_MACHINE_GPIO_MODE_OUTPUT_OPENDRAIN, +} mrbgems_machine_gpio_mode_t; + +int mrb_machine_impl_gpio_init(uint32_t pin, mrbgems_machine_gpio_mode_t mode); + +#endif // MRBGEMS_MACHINE_GPIO_GPIO_H diff --git a/mrbgems/utilities.gembox b/mrbgems/utilities.gembox index 59cb29e..b80dac2 100644 --- a/mrbgems/utilities.gembox +++ b/mrbgems/utilities.gembox @@ -1,3 +1,4 @@ MRuby::GemBox.new do |conf| conf.gem :core => "../../mrbgems/machine-devmem" + conf.gem :core => "../../mrbgems/machine-gpio" end \ No newline at end of file