Added GPIO gem to machine gembox.
Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
parent
002dfde385
commit
538ae1d21f
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,45 @@
|
|||
#include <mruby.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* 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 */
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef MRBGEMS_MACHINE_GPIO_GPIO_H
|
||||
#define MRBGEMS_MACHINE_GPIO_GPIO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
|
@ -1,3 +1,4 @@
|
|||
MRuby::GemBox.new do |conf|
|
||||
conf.gem :core => "../../mrbgems/machine-devmem"
|
||||
conf.gem :core => "../../mrbgems/machine-gpio"
|
||||
end
|
Loading…
Reference in New Issue