etl/src/io_port.h
2017-06-12 23:08:49 +01:00

917 lines
16 KiB
C++

///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
http://www.etlcpp.com
Copyright(c) 2014 jwellbelove
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.
******************************************************************************/
#ifndef __ETL_IO_PORT__
#define __ETL_IO_PORT__
///\defgroup io_port io port
/// IO port access
///\ingroup utilities
#include <stdint.h>
#include <iterator>
#include "nullptr.h"
#include "parameter_type.h"
namespace etl
{
//***************************************************************************
/// Read write port.
//***************************************************************************
template <typename T, uintptr_t ADDRESS = 0>
class io_port_rw
{
private:
typedef volatile T* pointer_t;
typedef typename etl::parameter_type<T>::type parameter_t;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_rw<T, ADDRESS> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
iop_t& operator *()
{
return *p_iop;
}
const iop_t& operator *() const
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
/// Read.
operator T() volatile const
{
return *reinterpret_cast<pointer_t>(ADDRESS);
}
/// Read.
T value() volatile const
{
return *reinterpret_cast<pointer_t>(ADDRESS);
}
/// Write.
io_port_rw& operator =(parameter_t value)
{
*reinterpret_cast<pointer_t>(ADDRESS) = value;
return *this;
}
/// Get the IO port address.
pointer_t get_address()
{
return reinterpret_cast<pointer_t>(ADDRESS);
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
};
//***************************************************************************
/// Read only port.
//***************************************************************************
template <typename T, uintptr_t ADDRESS = 0>
class io_port_ro
{
private:
typedef volatile const T* pointer_t;
typedef typename etl::parameter_type<T>::type parameter_t;
/// Write disabled.
void operator =(parameter_t value);
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_ro<T, ADDRESS> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
const iop_t& operator *() const
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
/// Read.
operator T() volatile const
{
return *reinterpret_cast<pointer_t>(ADDRESS);
}
/// Read.
T value() volatile const
{
return *reinterpret_cast<pointer_t>(ADDRESS);
}
/// Get the IO port address.
pointer_t get_address()
{
return reinterpret_cast<pointer_t>(ADDRESS);
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
};
//***************************************************************************
/// Write only port.
//***************************************************************************
template <typename T, uintptr_t ADDRESS = 0>
class io_port_wo
{
private:
typedef T* pointer_t;
typedef typename etl::parameter_type<T>::type parameter_t;
/// Read disabled.
operator T() volatile const;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_wo<T, ADDRESS> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
iop_t& operator *()
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
/// Write.
void operator =(parameter_t value)
{
*reinterpret_cast<pointer_t>(ADDRESS) = value;
}
/// Get the IO port address.
pointer_t get_address()
{
return reinterpret_cast<pointer_t>(ADDRESS);
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
};
//***************************************************************************
/// Write only port with shadow register.
//***************************************************************************
template <typename T, uintptr_t ADDRESS = 0>
class io_port_wos
{
private:
typedef T* pointer_t;
typedef typename etl::parameter_type<T>::type parameter_t;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_wos<T, ADDRESS> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
iop_t& operator *()
{
return *p_iop;
}
const iop_t& operator *() const
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
/// Read.
operator T() const
{
return shadow_value;
}
/// Read.
T value() const
{
return shadow_value;
}
/// Write.
io_port_wos& operator =(parameter_t value)
{
shadow_value = value;
*reinterpret_cast<pointer_t>(ADDRESS) = shadow_value;
return *this;
}
/// Get the IO port address.
pointer_t get_address()
{
return reinterpret_cast<pointer_t>(ADDRESS);
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
private:
T shadow_value;
};
//***************************************************************************
/// Read write port.
/// Specialisation for dynamic addresses.
//***************************************************************************
template <typename T>
class io_port_rw<T, 0>
{
private:
typedef volatile T* pointer_t;
typedef typename etl::parameter_type<T>::type parameter_t;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_rw<T, 0> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
iop_t& operator *()
{
return *p_iop;
}
const iop_t& operator *() const
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
// Default constructor.
io_port_rw()
: address(nullptr)
{
}
// Constructor.
io_port_rw(uint8_t* address_)
: address(reinterpret_cast<pointer_t>(address_))
{
}
/// Set the IO port address.
void set_address(uintptr_t address_)
{
address = reinterpret_cast<pointer_t>(address_);
}
/// Get the IO port address.
pointer_t get_address()
{
return address;
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
/// Read.
operator T() volatile const
{
return *address;
}
/// Read.
T value() volatile const
{
return *address;
}
/// Write.
io_port_rw& operator =(parameter_t value)
{
*address = value;
return *this;
}
private:
pointer_t address;
};
//***************************************************************************
/// Read only port.
/// Specialisation for dynamic addresses.
//***************************************************************************
template <typename T>
class io_port_ro<T, 0>
{
private:
typedef volatile const T* pointer_t;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_ro<T, 0> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
const iop_t& operator *() const
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
// Default constructor.
io_port_ro()
: address(nullptr)
{
}
// Constructor.
io_port_ro(void* address_)
: address(reinterpret_cast<pointer_t>(address_))
{
}
void set_address(uintptr_t address_)
{
address = reinterpret_cast<pointer_t>(address_);
}
/// Get the IO port address.
pointer_t get_address()
{
return address;
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
/// Read.
operator T() volatile const
{
return *address;
}
/// Read.
T value() volatile const
{
return *address;
}
private:
typedef typename etl::parameter_type<T>::type parameter_t;
/// Write disabled.
void operator =(parameter_t value);
pointer_t address;
};
//***************************************************************************
/// Write only port.
/// Specialisation for dynamic addresses.
//***************************************************************************
template <typename T>
class io_port_wo<T, 0>
{
private:
typedef T* pointer_t;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_wo<T, 0> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
iop_t& operator *()
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
typedef typename etl::parameter_type<T>::type parameter_t;
// Default constructor.
io_port_wo()
: address(nullptr)
{
}
// Constructor.
io_port_wo(void* address_)
: address(reinterpret_cast<pointer_t>(address_))
{
}
/// Set the IO port address.
void set_address(uintptr_t address_)
{
address = reinterpret_cast<pointer_t>(address_);
}
/// Get the IO port address.
pointer_t get_address()
{
return address;
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
/// Write.
void operator =(parameter_t value)
{
*address = value;
}
private:
/// Read disabled.
operator T() volatile const;
pointer_t address;
};
//***************************************************************************
/// Write only port with shadow register.
/// Specialisation for dynamic addresses.
//***************************************************************************
template <typename T>
class io_port_wos<T, 0>
{
private:
typedef T* pointer_t;
typedef typename etl::parameter_type<T>::type parameter_t;
public:
class iterator : public std::iterator<std::bidirectional_iterator_tag, T>
{
typedef io_port_wos<T, 0> iop_t;
public:
iterator(iop_t& iop)
: p_iop(&iop)
{
}
iterator(const iterator& other)
: p_iop(other.p_iop)
{
}
iterator& operator =(const iterator& other)
{
p_iop = other.p_iop;
return *this;
}
iop_t& operator *()
{
return *p_iop;
}
const iop_t& operator *() const
{
return *p_iop;
}
iterator& operator ++()
{
return *this;
}
iterator operator ++(int)
{
return *this;
}
iterator& operator --()
{
return *this;
}
iterator operator --(int)
{
return *this;
}
private:
iop_t* p_iop;
};
// Default constructor.
io_port_wos()
: address(nullptr)
{
}
// Constructor.
io_port_wos(void* address_)
: address(reinterpret_cast<pointer_t>(address_))
{
}
/// Set the IO port address.
void set_address(uintptr_t address_)
{
address = reinterpret_cast<pointer_t>(address_);
}
/// Get the IO port address.
pointer_t get_address()
{
return address;
}
/// Get the iterator.
iterator get_iterator()
{
return iterator(*this);
}
/// Read.
operator T() const
{
return shadow_value;
}
/// Read.
T value() const
{
return shadow_value;
}
/// Write.
io_port_wos& operator =(parameter_t value)
{
shadow_value = value;
*address = shadow_value;
return *this;
}
private:
T shadow_value;
pointer_t address;
};
}
#endif