Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 69 additions & 0 deletions test/mtest/mtest_pwm/include/mtest_pwm/mtest_pwm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef MTEST_PWM_H
#define MTEST_PWM_H
#include "mtest/mtest.h"

#ifdef __cplusplus
extern "C" {
#endif

#define PWM_TEST_DEV MYNEWT_VAL(PWM_TEST_DEV_NAME)

#define TIMER_FREQ_HZ MYNEWT_VAL(OS_CPUTIME_FREQ)
#define SAMPLE_FREQ_HZ 1000
#define PWM_FREQ_HZ 200

#define TIMER_TICKS (TIMER_FREQ_HZ / SAMPLE_FREQ_HZ)
#define MEASURE_TIME_S 1
#define WINDOW_SIZE (SAMPLE_FREQ_HZ * MEASURE_TIME_S)

#if MYNEWT_VAL(BSP_IS_NUCLEO)
#define PWM_TEST_CH_CFG_PIN \
MCU_AFIO_GPIO(MYNEWT_VAL(PWM_TEST_OUT_PIN), MYNEWT_VAL(PWM_TEST_OUT_PIN_AF))
#else
#define PWM_TEST_CH_CFG_PIN MYNEWT_VAL(PWM_TEST_OUT_PIN)
#endif
#define PWM_TEST_CH_NUM MYNEWT_VAL(PWM_TEST_CHANNEL)
#define PWM_TEST_CH_CFG_INV MYNEWT_VAL(PWM_TEST_INVERT_OUTPUT)

struct pwm_test_ctx {
struct pwm_dev *pwm;
struct os_sem sem;
struct hal_timer timer;

int sample_cnt;
int high_cnt;
};

extern struct pwm_test_ctx test_ctx;

MTEST_CASE_DECL(pwm_test_case_1);
MTEST_CASE_DECL(pwm_test_case_2);
MTEST_CASE_DECL(pwm_test_case_3);
MTEST_CASE_DECL(pwm_test_case_4);
MTEST_CASE_DECL(pwm_test_case_5);
MTEST_CASE_DECL(pwm_test_case_6);

#ifdef __cplusplus
}
#endif

#endif
32 changes: 32 additions & 0 deletions test/mtest/mtest_pwm/pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

pkg.name: test/mtest/mtest_pwm
pkg.description: PWM duty cycle verification via GPIO loopback
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
pkg.type: app
pkg.deps:
- "@apache-mynewt-core/kernel/os"
- "@apache-mynewt-core/hw/hal"
- "@apache-mynewt-core/sys/log/"
- "@apache-mynewt-core/hw/drivers/pwm"
- "@apache-mynewt-core/test/mtest/mtest"
- "@apache-mynewt-core/hw/drivers/pwm"
120 changes: 120 additions & 0 deletions test/mtest/mtest_pwm/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include "sysinit/sysinit.h"
#include "bsp/bsp.h"
#include "pwm/pwm.h"
#include "hal/hal_gpio.h"
#include "mtest_pwm/mtest_pwm.h"

struct pwm_test_ctx test_ctx;

void
pwm_init(void)
{
struct pwm_chan_cfg chan_conf;
int rc;

chan_conf = (struct pwm_chan_cfg){ .pin = PWM_TEST_CH_CFG_PIN,
.inverted = PWM_TEST_CH_CFG_INV,
.data = NULL };

test_ctx.pwm =
(struct pwm_dev *)os_dev_open(PWM_TEST_DEV, OS_TIMEOUT_NEVER, NULL);
MTEST_INIT_ASSERT(test_ctx.pwm != NULL, "device %s not available\n", PWM_TEST_DEV);
rc = pwm_set_frequency(test_ctx.pwm, PWM_FREQ_HZ);
MTEST_INIT_ASSERT(rc > 0, "set frequency for pwm clock failed");

rc = pwm_configure_channel(test_ctx.pwm, PWM_TEST_CH_NUM, &chan_conf);
MTEST_INIT_ASSERT(rc == 0, "channel configuration failed");

rc = pwm_enable(test_ctx.pwm);
MTEST_INIT_ASSERT(rc == 0, "PWM enable");
}

void
timer_cb(void *arg)
{
if (test_ctx.sample_cnt < WINDOW_SIZE) {
test_ctx.sample_cnt++;
if (hal_gpio_read(MYNEWT_VAL(PWM_TEST_READ_PIN))) {
test_ctx.high_cnt++;
}

if (test_ctx.sample_cnt == WINDOW_SIZE) {
os_cputime_timer_stop(&test_ctx.timer);
os_sem_release(&test_ctx.sem);
} else {
os_cputime_timer_relative(&test_ctx.timer, TIMER_TICKS);
}
}
}

MTEST_INIT(pwm_test)
{
int rc;
os_error_t err;

rc = hal_gpio_init_in(MYNEWT_VAL(PWM_TEST_READ_PIN), HAL_GPIO_PULL_DOWN);
MTEST_INIT_ASSERT(rc == 0, "pin configuration failed");

err = os_sem_init(&test_ctx.sem, 0);
MTEST_INIT_ASSERT(err == 0, "semaphore init failed");

os_cputime_timer_init(&test_ctx.timer, timer_cb, NULL);

pwm_init();
}

MTEST_CLEANUP(pwm_test)
{
int rc;

rc = pwm_disable(test_ctx.pwm);
MTEST_CLEANUP_ASSERT(rc == 0, "disable PWM failed");

os_cputime_timer_stop(&test_ctx.timer);

rc = os_dev_close((struct os_dev *)test_ctx.pwm);
MTEST_CLEANUP_ASSERT(rc == 0, "dev close failed");
}

MTEST_SUITE(pwm_test)
{
MTEST_RUN_INIT(pwm_test);
pwm_test_case_1();
pwm_test_case_2();
pwm_test_case_3();
pwm_test_case_4();
pwm_test_case_5();
pwm_test_case_6();
MTEST_RUN_CLEANUP(pwm_test);
}

int
mynewt_main(int argc, char **argv)
{
sysinit();

pwm_test();

while (1) {
os_time_delay(OS_TICKS_PER_SEC);
}
}
90 changes: 90 additions & 0 deletions test/mtest/mtest_pwm/src/pwm_test_cases.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include "pwm/pwm.h"
#include "mtest_pwm/mtest_pwm.h"

void
test_pwm_duty_cycle(int duty_percent)
{
int top;
int duty_val;
int rc;
int measured_duty;
int diff;
test_ctx.sample_cnt = 0;
test_ctx.high_cnt = 0;

top = pwm_get_top_value(test_ctx.pwm);
MTEST_CASE_ASSERT(top > 0, "PWM get top value failed");

duty_val = top * duty_percent / 100;
rc = pwm_set_duty_cycle(test_ctx.pwm, PWM_TEST_CH_NUM, duty_val);
MTEST_CASE_ASSERT(rc == 0, "set duty cycle %u%% (value=%d) failed",
duty_percent, duty_val);

rc = os_cputime_timer_relative(&test_ctx.timer, TIMER_TICKS);
MTEST_CASE_ASSERT(rc == 0, "timer start failed");

rc = os_sem_pend(&test_ctx.sem, OS_TICKS_PER_SEC * 10);
MTEST_CASE_ASSERT(rc == 0, "measurement timeout for duty %d%%", duty_percent);

MTEST_CASE_ASSERT(test_ctx.sample_cnt > 0, "no samples collected");

measured_duty = test_ctx.high_cnt * 100 / test_ctx.sample_cnt;
diff = measured_duty - duty_percent;
if (diff < 0) {
diff = -diff;
}

MTEST_CASE_ASSERT(
diff < MYNEWT_VAL(PWM_TEST_TOLERANCE),
"duty tolerance exceeded: expected %d%%, measured %d%%, diff %d%% (max %d%%)",
duty_percent, measured_duty, diff, MYNEWT_VAL(PWM_TEST_TOLERANCE));
}

MTEST_CASE(pwm_test_case_1)
{
test_pwm_duty_cycle(0);
}

MTEST_CASE(pwm_test_case_2)
{
test_pwm_duty_cycle(20);
}

MTEST_CASE(pwm_test_case_3)
{
test_pwm_duty_cycle(40);
}

MTEST_CASE(pwm_test_case_4)
{
test_pwm_duty_cycle(60);
}

MTEST_CASE(pwm_test_case_5)
{
test_pwm_duty_cycle(80);
}

MTEST_CASE(pwm_test_case_6)
{
test_pwm_duty_cycle(100);
}
Loading
Loading