Implemented GPIO.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-03-14 22:56:39 +08:00
parent 13b19f4208
commit ef714637c5
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
3 changed files with 70 additions and 4 deletions

18
README.md Normal file
View File

@ -0,0 +1,18 @@
# WIP
## Implemented features:
```ruby
# Initialization: Takes a hash with configuration.
led_red = Machine::GPIO.new({pin: 38, mode: Machine::GPIO::OUTPUT_OD, init: Machine::GPIO::ON})
led_blue = Machine::GPIO.new({pin: 36, mode: Machine::GPIO::OUTPUT_OD, init: Machine::GPIO::ON})
led_green = Machine::GPIO.new({pin: 39, mode: Machine::GPIO::OUTPUT_OD, init: Machine::GPIO::ON})
```
```ruby
led_red.value # Get LED pin input value
led_red.value = Machine::GPIO::OFF # Turn on the LED (Level is reversed)
led_red.off # Same as above
led_red.on # Turn off the LED (Level is reversed)
led_red.toggle # Toggle output
```

@ -1 +1 @@
Subproject commit 3e175303e53c4126a94828f89d028bf57e57a889
Subproject commit 12b520e7873432d7135f9de86330c000f8464873

View File

@ -5,16 +5,64 @@
/* SDK drivers */
#include "fsl_gpio.h"
#include "fsl_iocon.h"
/* Gem private */
#include "machine-gpio/src/gpio.h"
#define GPIO_IMPL_PORT(x) (x / 32)
#define GPIO_IMPL_PIN(x) (x % 32)
int mrb_machine_gpio_impl_config(uint32_t pin, machine_gpio_config_t *cfg) {
printf("IO initialize: Pin %ld to %d, init %d\n", pin, cfg->mode, cfg->initial_value);
/**
* IO pin naming for LPC55S69:
* LPC5500 series has 2 IO ports, each with 32 pins, named PIO[0-1][0-31]
* Port 1 starts at 32, e.g. pin 32: PIO[1][0], pin 33: PIO[1][1]
*/
uint32_t iocon_mode = IOCON_PIO_FUNC(0) | IOCON_PIO_SLEW(0) | IOCON_DIGITAL_EN;
gpio_pin_direction_t dir = (cfg->mode == MACHINE_GPIO_MODE_INPUT) ? kGPIO_DigitalInput : kGPIO_DigitalOutput;
uint8_t init_val = (cfg->initial_value == MACHINE_GPIO_HIGH) ? 1 : 0;
if (cfg->pull == MACHINE_GPIO_PULL_UP) {
iocon_mode |= IOCON_PIO_MODE(2); /* Pull-up */
} else if (cfg->pull == MACHINE_GPIO_PULL_DOWN) {
iocon_mode |= IOCON_PIO_MODE(1); /* Pull-down */
}
if (cfg->mode == MACHINE_GPIO_MODE_OUTPUT_OPENDRAIN) {
iocon_mode |= IOCON_OPENDRAIN_EN;
}
IOCON_PinMuxSet(IOCON, GPIO_IMPL_PORT(pin), GPIO_IMPL_PIN(pin), iocon_mode);
gpio_pin_config_t pin_cfg = {
.pinDirection = dir,
.outputLogic = init_val,
};
GPIO_PinInit(GPIO, GPIO_IMPL_PORT(pin), GPIO_IMPL_PIN(pin), &pin_cfg);
return 0;
}
int mrb_machine_gpio_read(uint32_t pin) {
int mrb_machine_gpio_impl_read(uint32_t pin) {
if (GPIO_PinRead(GPIO, GPIO_IMPL_PORT(pin), GPIO_IMPL_PIN(pin))) {
return 1;
}
return 0;
}
}
int mrb_machine_gpio_impl_write(uint32_t pin, machine_gpio_value_t val) {
uint8_t pin_val = (val == MACHINE_GPIO_HIGH) ? 1 : 0;
GPIO_PinWrite(GPIO, GPIO_IMPL_PORT(pin), GPIO_IMPL_PIN(pin), pin_val);
return 0;
}
int mrb_machine_gpio_impl_toggle(uint32_t pin) {
GPIO_PortToggle(GPIO, GPIO_IMPL_PORT(pin), (1 << GPIO_IMPL_PIN(pin)));
return 0;
}