LPCXpresso55S69_MRuby/src/mrb_machine_impl/mrb_machine_gpio_impl.c
Yilin Sun ef714637c5
Implemented GPIO.
Signed-off-by: Yilin Sun <imi415@imi.moe>
2023-03-14 22:56:39 +08:00

69 lines
1.9 KiB
C

#include <stdio.h>
/* Board */
#include "board.h"
/* 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) {
/**
* 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_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;
}