From 740da3f39ab85025a80f0eb91c129808f2c29bf0 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Tue, 4 Apr 2023 22:13:55 +0300 Subject: [PATCH] peripherals: add GPIO pin lib [#2] --- source/shoh/src/peripherals/DigitalIoPin.cpp | 64 ++++++++++++++++++++ source/shoh/src/peripherals/DigitalIoPin.h | 43 +++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 source/shoh/src/peripherals/DigitalIoPin.cpp create mode 100644 source/shoh/src/peripherals/DigitalIoPin.h diff --git a/source/shoh/src/peripherals/DigitalIoPin.cpp b/source/shoh/src/peripherals/DigitalIoPin.cpp new file mode 100644 index 0000000..13f7d87 --- /dev/null +++ b/source/shoh/src/peripherals/DigitalIoPin.cpp @@ -0,0 +1,64 @@ +/* + * DigitalIoPin.cpp + * + * Created on: Aug 29, 2022 + * Author: Vasily Davydov + */ + +#include "DigitalIoPin.h" + +DigitalIoPin::DigitalIoPin (int port, int pin, bool input, bool pullup, + bool invert) +{ + assert ((port <= UINT8_MAX_VALUE) && (pin <= UINT8_MAX_VALUE)); + _io._port = (uint8_t)port; + _io._pin = (uint8_t)pin; + _io._input = input; + _io._pullup = pullup; + _io._invert = invert; + _io.IOCON_mode = IOCON_MODE_INACT; + _io.IOCON_inv = IOCON_FUNC0; + setIoPin (); +} + +DigitalIoPin::~DigitalIoPin () +{ +} + +void +DigitalIoPin::setIoPin () +{ + bool direction = true; + if (_io._input) + { + direction = false; + _io.IOCON_mode = IOCON_MODE_PULLUP; + if (!_io._pullup) + { + _io.IOCON_mode = IOCON_MODE_PULLDOWN; + } + if (_io._invert) + { + _io.IOCON_inv = IOCON_INV_EN; + } + } + Chip_IOCON_PinMuxSet (LPC_IOCON, _io._port, _io._pin, + (_io.IOCON_mode | _io.DigitalEn | _io.IOCON_inv)); + /** False direction equals input */ + Chip_GPIO_SetPinDIR (LPC_GPIO, _io._port, _io._pin, direction); +} + +bool +DigitalIoPin::read () +{ + bool state = (Chip_GPIO_GetPinState (LPC_GPIO, _io._port, _io._pin)); + return (_io._invert && !_io._input) ? !state : state; +} + +void +DigitalIoPin::write (bool value) +{ + assert (!(_io._input)); + Chip_GPIO_SetPinState (LPC_GPIO, _io._port, _io._pin, ((_io._invert) ? !value : value)); +} + diff --git a/source/shoh/src/peripherals/DigitalIoPin.h b/source/shoh/src/peripherals/DigitalIoPin.h new file mode 100644 index 0000000..de004af --- /dev/null +++ b/source/shoh/src/peripherals/DigitalIoPin.h @@ -0,0 +1,43 @@ +/* + * DigitalIoPin.h + * + * Created on: Aug 29, 2022 + * Author: Vasily Davydov + */ + +#ifndef DIGITALIOPIN_H_ +#define DIGITALIOPIN_H_ + +#define UINT8_MAX_VALUE 255 + +#include +#include + +typedef struct DigitalIOConfigStruct +{ + uint8_t _port; + uint8_t _pin; + bool _input; + bool _pullup; + bool _invert; + uint32_t IOCON_mode; + uint32_t IOCON_inv; + uint32_t DigitalEn; +} DigitalIOConfigStruct; + +class DigitalIoPin +{ +public: + DigitalIoPin (int port, int pin, bool input = true, bool pullup = true, + bool invert = false); + DigitalIoPin (const DigitalIoPin &) = delete; + virtual ~DigitalIoPin (); + bool read (); + void write (bool value); + +private: + DigitalIOConfigStruct _io = { 0, 0, false, false, false, 0, 0, IOCON_DIGMODE_EN}; + void setIoPin (); +}; + +#endif /* DIGITALIOPIN_H_ */