-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e264728
commit b5f55af
Showing
6 changed files
with
92 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |