/* * Copyright (C) 2019 Max Regan * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #pragma once #include "Bsp/ReturnCode.h" #include "Bsp/TaskScheduler.h" #include "Mcu.h" namespace BSP { class GpioDriver { public: GpioDriver(GPIO_TypeDef *gpio); void init(); void enable(); void disable(); enum class output_mode_t { PUSH_PULL, OPEN_DRAIN, }; enum class output_speed_t { LOW, MEDIUM, HIGH, VERY_HIGH, }; enum class input_pull_t { FLOATING, PULL_UP, PULL_DOWN, }; friend class GpioPin; private: enum class moder_t { INPUT, OUTPUT, ALTERNATE_FUNCTION, }; void set_pin_moder(uint32_t index, moder_t mode); void set_pin_pupdr(uint32_t index, input_pull_t pull_mode); void set_pin_otyper(uint32_t index, output_mode_t mode); void set_pin_ospeedr(uint32_t index, output_speed_t speed); void configure_pin_input(uint32_t index, input_pull_t pull_mode); void configure_pin_output(uint32_t index, output_mode_t mode, output_speed_t speed); void configure_pin_alternate_function(uint32_t index, uint32_t function); void write_pin(uint32_t index, bool value); bool read_pin(uint32_t index); GPIO_TypeDef * const m_gpio; }; class GpioPin { public: GpioPin(GpioDriver &m_driver, uint32_t index); void configure_input(GpioDriver::input_pull_t pull_mode); void configure_output(GpioDriver::output_mode_t mode, GpioDriver::output_speed_t speed); void configure_alternate_function(uint32_t function); void write(bool value); bool read(); GpioDriver &get_driver(); uint32_t get_index(); private: GpioDriver &m_gpio; uint32_t const m_index; }; }