Skip to content

Commit

Permalink
Add/update port-based PWM class
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Feb 25, 2024
1 parent e264728 commit b5f55af
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 11 deletions.
1 change: 1 addition & 0 deletions ref_app/ref_app.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,7 @@
<ClInclude Include="src\mcal_pwm\mcal_pwm_base.h" />
<ClInclude Include="src\mcal_pwm\mcal_pwm_console.h" />
<ClInclude Include="src\mcal_pwm\mcal_pwm_dummy.h" />
<ClInclude Include="src\mcal_pwm\mcal_pwm_port.h" />
<ClInclude Include="src\mcal_spi\mcal_spi_software_dummy.h" />
<ClInclude Include="src\mcal_spi\mcal_spi_software_port_driver.h" />
<ClInclude Include="src\mcal_vfd\mcal_vfd_base.h" />
Expand Down
3 changes: 3 additions & 0 deletions ref_app/ref_app.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,9 @@
<ClInclude Include="src\util\utility\util_constexpr_cmath_unsafe.h">
<Filter>src\util\utility</Filter>
</ClInclude>
<ClInclude Include="src\mcal_pwm\mcal_pwm_port.h">
<Filter>src\mcal_pwm</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="src\util\STL\algorithm">
Expand Down
10 changes: 5 additions & 5 deletions ref_app/src/mcal/avr/mcal_benchmark.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2014 - 2023.
// Copyright Christopher Kormanyos 2014 - 2024.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -17,10 +17,10 @@
{
namespace benchmark
{
typedef mcal::port::port_pin<std::uint8_t,
std::uint8_t,
mcal::reg::portd,
UINT8_C(3)> benchmark_port_type;
using benchmark_port_type = mcal::port::port_pin<std::uint8_t,
std::uint8_t,
mcal::reg::portd,
UINT8_C(3)>;
}
}

Expand Down
2 changes: 1 addition & 1 deletion ref_app/src/mcal/avr/mcal_pwm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2023.
// Copyright Christopher Kormanyos 2007 - 2024.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand Down
13 changes: 8 additions & 5 deletions ref_app/src/mcal_pwm/mcal_pwm_base.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2022.
// Copyright Christopher Kormanyos 2020 - 2024.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -17,19 +17,22 @@
class pwm_base : private util::noncopyable
{
public:
using duty_type = std::uint16_t;

virtual ~pwm_base() noexcept = default;

virtual auto init() noexcept -> bool = 0;

virtual auto set_duty(const std::uint16_t duty_cycle) noexcept -> void { my_duty_cycle = duty_cycle; }
virtual auto set_duty(const duty_type duty_cycle) noexcept -> void { my_duty_cycle = duty_cycle; }

auto get_duty() const noexcept -> std::uint16_t { return my_duty_cycle; }
auto get_duty() const noexcept -> duty_type { return my_duty_cycle; }

protected:
pwm_base() noexcept = default;
explicit pwm_base(const duty_type initial_duty = static_cast<duty_type>(UINT8_C(0))) noexcept
: my_duty_cycle(initial_duty) { }

private:
std::uint16_t my_duty_cycle { };
duty_type my_duty_cycle { };
};

} // namespace pwm
Expand Down
74 changes: 74 additions & 0 deletions ref_app/src/mcal_pwm/mcal_pwm_port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2024.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef MCAL_PWM_PORT_2023_08_23_H
#define MCAL_PWM_PORT_2023_08_23_H

#include <mcal_pwm/mcal_pwm_base.h>

namespace mcal { namespace pwm {

// A software PWM template for a port-pin having the standard
// port interface for ref_app. The default resolution is 100 ticks.

template<typename PortPinType,
const pwm_base::duty_type MyResolution = static_cast<pwm_base::duty_type>(UINT8_C(100))>
class pwm_port : public pwm_base
{
private:
using base_class_type = pwm_base;
using port_pin_type = PortPinType;

public:
using base_class_type::duty_type;

explicit pwm_port(const duty_type initial_duty = static_cast<duty_type>(UINT8_C(0)))
: base_class_type(initial_duty),
my_duty_shadow(initial_duty)
{
port_pin_type::set_pin_low();
port_pin_type::set_direction_output();
}

~pwm_port() noexcept override = default;

auto init() noexcept -> bool override
{
return true;
}

auto set_duty(const duty_type duty_cycle) noexcept -> void override { my_duty_shadow = duty_cycle; }

static constexpr auto get_resolution() noexcept -> duty_type { return MyResolution; }

auto service() noexcept -> void
{
// Increment the cycle counter.
++my_cycle_counter;

((my_cycle_counter <= base_class_type::get_duty()) ? port_pin_type::set_pin_high() : port_pin_type::set_pin_low());

if(my_cycle_counter == get_resolution())
{
// Latch the duty cycle from the shadow register.
// This is done at the end of the running cycle.
base_class_type::set_duty(my_duty_shadow);

// Reset the cycle counter for a new PWM period.
my_cycle_counter = static_cast<duty_type>(UINT8_C(0));
}
}

private:
duty_type my_cycle_counter { };
duty_type my_duty_shadow { };
};

} // namespace pwm
} // namespace mcal

#endif // MCAL_PWM_PORT_2023_08_23_H

0 comments on commit b5f55af

Please sign in to comment.