Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Arm Cortex-M0+ delayer #44

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/delayer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Blocking Delay Facilities
The `::picolibrary::Arm::Cortex::M0PLUS::Delayer` blocking delay nullary functor is
defined in the
[`include/picolibrary/arm/cortex/m0plus/delayer.h`](https://github.com/apcountryman/picolibrary-arm-cortex-m0plus/blob/main/include/picolibrary/arm/cortex/m0plus/delayer.h)/`source/picolibrary/arm/cortex/m0plus/delayer.cc`](https://github.com/apcountryman/picolibrary-arm-cortex-m0plus/blob/main/source/picolibrary/arm/cortex/m0plus/delayer.cc)
header/source file pair.
`::picolibrary::Arm::Cortex::M0PLUS::Delayer` supports the following operations:
- To delay, use the `::picolibrary::Arm::Cortex::M0PLUS::Delayer::operator()()` member
function.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
1. [Library Configuration](library_configuration.md)
1. [Peripheral Facilities](peripheral.md)
1. [Interrupt Facilities](interrupt.md)
1. [Blocking Delay Facilities](delayer.md)
144 changes: 144 additions & 0 deletions include/picolibrary/arm/cortex/m0plus/delayer.h
apcountryman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* picolibrary-arm-cortex-m0plus
*
* Copyright 2023, Andrew Countryman <apcountryman@gmail.com> and the
* picolibrary-arm-cortex-m0plus contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

/**
* \file
* \brief picolibrary::Arm::Cortex::M0PLUS::Delayer interface.
*/

#ifndef PICOLIBRARY_ARM_CORTEX_M0PLUS_DELAYER_H
#define PICOLIBRARY_ARM_CORTEX_M0PLUS_DELAYER_H

#include <cstdint>

#include "picolibrary/arm/cortex/m0plus/peripheral/systick.h"
#include "picolibrary/utility.h"

namespace picolibrary::Arm::Cortex::M0PLUS {

/**
* \brief Blocking delayer.
*/
class Delayer {
public:
/**
* \brief SYSTICK clock source.
*/
enum class Clock_Source : std::uint32_t {
EXTERNAL_REFERENCE_CLOCK = 0 << Peripheral::SYSTICK::CSR::Bit::CLKSOURCE, ///< External reference clock.
PROCESSOR_CLOCK = 1 << Peripheral::SYSTICK::CSR::Bit::CLKSOURCE, ///< Processor clock.
};

/**
* \brief Constructor.
*/
constexpr Delayer() noexcept = default;

/**
* \brief Constructor.
*
* \param[in] systick The SYSTICK peripheral instance to use to create blocking
* delays.
* \param[in] clock_source The SYSTICK peripheral clock source to use.
* \param[in] reload_value The SYSTICK peripheral counter reload value to use to
* create blocking delays of the desired duration.
*
* \attention The delayer will use processor clock if the external reference clock is
* requested but is not available.
apcountryman marked this conversation as resolved.
Show resolved Hide resolved
*/
constexpr Delayer( Peripheral::SYSTICK & systick, Clock_Source clock_source, std::uint32_t reload_value ) noexcept
:
m_systick{ &systick }
{
m_systick->rvr = reload_value;
m_systick->cvr = 0;
m_systick->csr = to_underlying( clock_source ) | Peripheral::SYSTICK::CSR::Mask::ENABLE;
}

/**
* \brief Constructor.
*
* \param[in] source The source of the move.
*/
constexpr Delayer( Delayer && source ) noexcept : m_systick{ source.m_systick }
{
source.m_systick = nullptr;
}

Delayer( Delayer const & ) = delete;

/**
* \brief Destructor.
*/
~Delayer() noexcept
{
disable();
}

/**
* \brief Assignment operator.
*
* \param[in] expression The expression to be assigned.
*
* \return The assigned to object.
*/
constexpr auto operator=( Delayer && expression ) noexcept -> Delayer &
{
if ( &expression != this ) {
disable();

m_systick = expression.m_systick;

expression.m_systick = nullptr;
} // if

return *this;
}

auto operator=( Delayer const & ) = delete;

/**
* \brief Delay.
*/
void operator()() const noexcept
{
m_systick->cvr = 0;

while ( not( m_systick->csr & Peripheral::SYSTICK::CSR::Mask::COUNTFLAG ) ) {} // while
}

private:
/**
* \brief The SYSTICK peripheral instance used to create blocking delays.
*/
Peripheral::SYSTICK * m_systick{};

/**
* \brief Disable the counter.
*/
constexpr void disable() noexcept
{
if ( m_systick ) {
m_systick->rvr = 0;
m_systick->csr = 0;
} // if
}
};

} // namespace picolibrary::Arm::Cortex::M0PLUS

#endif // PICOLIBRARY_ARM_CORTEX_M0PLUS_DELAYER_H
1 change: 1 addition & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(
PICOLIBRARY_ARM_CORTEX_M0PLUS_SOURCE_FILES
"picolibrary/arm/cortex/m0plus.cc"
"picolibrary/arm/cortex/m0plus/configuration.cc"
"picolibrary/arm/cortex/m0plus/delayer.cc"
"picolibrary/arm/cortex/m0plus/interrupt.cc"
"picolibrary/arm/cortex/m0plus/peripheral.cc"
"picolibrary/arm/cortex/m0plus/peripheral/mpu.cc"
Expand Down
23 changes: 23 additions & 0 deletions source/picolibrary/arm/cortex/m0plus/delayer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* picolibrary-arm-cortex-m0plus
*
* Copyright 2023, Andrew Countryman <apcountryman@gmail.com> and the
* picolibrary-arm-cortex-m0plus contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

/**
* \file
* \brief picolibrary::Arm::Cortex::M0PLUS::Delayer implementation.
*/

#include "picolibrary/arm/cortex/m0plus/delayer.h"