6 #include "libpropeller/pwm32/pwm32.h"
7 #include "libpropeller/pulsewidthreader/pulse_width_reader.h"
9 #include "libpropeller/board/board_unit_tester.h"
14 const int outputPin = libpropeller::Board::kPinTie1a;
15 const int inputPinMask = 1 << libpropeller::Board::kPinTie1b;
20 static int MicrosecondsToClockCycles(
const int microseconds) {
21 return (
CLKFREQ * microseconds) / 1000000;
24 static int ClockCyclesToMicroseconds(
const int clockCycles) {
25 return clockCycles / (
CLKFREQ / 1000000);
28 static void setUp(
void) {
33 pwr->
Start(inputPinMask);
39 static void tearDown(
void) {
50 static void helper_CheckWidths(
const int highMicroseconds,
const int lowMicroseconds) {
51 TEST_ASSERT_INT_WITHIN(libpropeller::PWM32::Resolution * 2 / 1000, highMicroseconds,
54 TEST_ASSERT_INT_WITHIN(libpropeller::PWM32::Resolution * 2 / 1000, lowMicroseconds,
55 ClockCyclesToMicroseconds(pwr->
getLowTime(0)));
58 static void helper_TestDuty(
const int percent,
const int period) {
59 sut->Duty(outputPin, percent, period);
64 helper_CheckWidths((period * percent) / 100, (period * (100 - percent)) / 100);
67 static void helper_TestServo(
const int pulsewidth) {
68 sut->Servo(outputPin, pulsewidth);
73 helper_CheckWidths(pulsewidth, 20000 - pulsewidth);
76 static void helper_TestPWM(
const int high,
const int low) {
85 static void test_50PercentDuty(
void) {
86 helper_TestDuty(50, 1000);
89 static void test_20PercentDuty(
void) {
90 helper_TestDuty(20, 1000);
93 static void test_80PercentDuty(
void) {
94 helper_TestDuty(80, 1000);
97 static void test_ServoMiddle(
void) {
98 helper_TestServo(1500);
101 static void test_ServoLow(
void) {
102 helper_TestServo(1000);
105 static void test_ServoHigh(
void) {
106 helper_TestServo(2000);
109 static void test_ServoVeryLow(
void) {
110 helper_TestServo(500);
113 static void test_ServoVeryHigh(
void) {
114 helper_TestServo(2500);
117 static void test_pwmEven(
void) {
118 helper_TestPWM(1000, 1000);
121 static void test_pwmLongHigh(
void) {
122 helper_TestPWM(2000, 100);
125 static void test_pwmLongLow(
void) {
126 helper_TestPWM(300, 1800);
129 static void test_pwmPrimeTimes(
void) {
130 helper_TestPWM(773, 907);