Compare commits

..

3 Commits

6 changed files with 217 additions and 5 deletions

View File

@ -31,7 +31,6 @@
#ifndef TBX_COM_SEQUENCER_H_
#define TBX_COM_SEQUENCER_H_
#include <core/core.h>
#include <core/crtp.h>
#include <cont/range.h>

View File

@ -33,7 +33,6 @@
#include <core/core.h>
#include <core/ring_iterator.h>
#include <cont/range.h>
#include <array>
#include <atomic>

View File

@ -31,12 +31,9 @@
#ifndef TBX_DRV_CLI_DEVICE_H_
#define TBX_DRV_CLI_DEVICE_H_
#define __cplusplus 201703L
#include <core/core.h>
#include <core/crtp.h>
#include <cont/equeue.h>
#include <cont/range.h>
#include <com/sequencer.h>
#include <cstring>

107
include/drv/gpio.h Normal file
View File

@ -0,0 +1,107 @@
/*!
* \file drv/gpio.h
* \brief
* A STM32 gpio wrapper class
*
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
*
* <dl class=\"section copyright\"><dt>License</dt><dd>
* The MIT License (MIT)
*
* 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.
* </dd></dl>
*/
#ifndef TBX_DRV_GPIO_H_
#define TBX_DRV_GPIO_H_
#include <core/core.h>
namespace tbx {
/*!
* CRTP class for gpio digital input-output.
*
* The derived class requirements are:
* - bool read_impl ()
* - void write_impl(bool)
*
* \tparam Impl_t The type of derived class
*/
template <typename Impl_t>
class DigitalInOut {
public:
_CRTP_IMPL(Impl_t);
/*!
* \name Object lifetime
*/
//! @{
protected:
DigitalInOut() noexcept = default;
~DigitalInOut() = default;
DigitalInOut(const DigitalInOut&) = delete; //!< No copy constructions
DigitalInOut operator=(const DigitalInOut&) = delete; //!< No copies
//! @}
//! \name Public interface
//! @{
public:
//! Reads the state of the pin. This is true for both input and output pins.
//! \return The state of the pin
bool read () noexcept { return impl().read_impl (); }
//! Write a new state to pin. If the pin is set for output, otherwise this state will remain
//! to pin registers and reflect to the pin state if we select output mode
void write (bool state) noexcept { impl().write_impl(state); }
//! Implicit conversion to bool for reading operations
operator bool () noexcept {
return read();
}
//! Stream from bool for write operations
DigitalInOut& operator<< (bool state) noexcept {
write(state);
return *this;
}
//! Right stream to bool for read operations
DigitalInOut& operator>> (bool& state) noexcept {
state = read();
return *this;
}
//! @}
};
/*!
* This definition enables the "data << pin" syntax for read operation
*
* \tparam Impl_t The derived class type of the DigitalInOut
*
* \param lhs Left hand site operand
* \param rhs Right hand site operand
* \return The read value
*/
template<typename Impl_t>
bool operator<<(bool& lhs, DigitalInOut<Impl_t>& rhs) noexcept {
lhs = rhs.read();
return lhs;
}
}
#endif //#ifndef TBX_DRV_STM32GPIO_H_

View File

@ -37,6 +37,9 @@
#include <cont/queue.h>
#include <cont/equeue.h>
#include <utils/shared.h>
#include <drv/cli_device.h>
#include <drv/gpio.h>
#endif /* TBX_H_ */

107
include/utils/shared.h Normal file
View File

@ -0,0 +1,107 @@
/*!
* \file utils/shared.h
* \brief
* A CRTP base class to provide acquire/release functionality for shared resources
* without handle pointers.
*
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
*
* <dl class=\"section copyright\"><dt>License</dt><dd>
* The MIT License (MIT)
*
* 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.
* </dd></dl>
*/
#ifndef TBX_UTILS_SHARED_H_
#define TBX_UTILS_SHARED_H_
#include <core/core.h>
#include <core/crtp.h>
#include <utility>
namespace tbx {
/*!
* A CRTP base class to provide acquire/release functionality for shared resources
* without handle pointers.
*
* \example
* \code
* class GPIOClock : public shared<GPIOClock> {
* friend shared<GPIOClock>;
* void acquire_impl() { // HAL enable gpio clock }
* void release_impl() { // HAL disable gpio clock }
* };
* GPIOClock clk;
* class Pin {
* Pin() {
* clk.acquire();
* // init pin
* }
* ~Pin() {
* // de-init pin
* clk.release();
* }
* };
* \endcode
*
* \tparam Impl_t The derived class type
*/
template <typename Impl_t>
class shared {
_CRTP_IMPL(Impl_t);
int count {}; //!< acquisition counter
protected:
shared() noexcept = default;
shared(const shared&) = delete; //!< No copies
shared operator=(const shared&) = delete; //!< No copies
public:
/*!
* Acquires the recourse. If it is the first call to acquire the resource we actually acquire it.
* Otherwise we just keep track of how many acquisition have made.
*
* \tparam Ts The types of possible arguments
* \param args Possible arguments to pass to acquire_impl() of derived class
*/
template <typename ...Ts>
void acquire (Ts&&... args) {
if (!count++) impl().acquire_impl(std::forward<Ts>(args)...);
}
/*!
* Release the recourse. On every call we decrease the count of acquisitions. If we reach zero
* we actually release the resource.
*
* \tparam Ts The types of possible arguments
* \param args Possible arguments to pass to release_impl() of derived class
*/
template <typename ...Ts>
void release (Ts&&... args) noexcept {
if (--count <= 0) {
impl().release_impl(std::forward<Ts>(args)...);
count =0;
}
}
};
}
#endif /* TBX_UTILS_SHARED_H_ */