From 1da6a03f09ae2c0824598848a3347aa139d11f23 Mon Sep 17 00:00:00 2001 From: Gilles DOFFE Date: Mon, 15 Oct 2018 23:43:52 +0200 Subject: [PATCH] cpu/native: add pwm implementation Simulate PWM signals with a 2 dimensions array. Signed-off-by: Gilles DOFFE --- cpu/native/Makefile.features | 1 + cpu/native/include/periph_conf.h | 7 +++ cpu/native/periph/pwm.c | 103 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 cpu/native/periph/pwm.c diff --git a/cpu/native/Makefile.features b/cpu/native/Makefile.features index 29c641eaa3..9e0823bd94 100644 --- a/cpu/native/Makefile.features +++ b/cpu/native/Makefile.features @@ -2,3 +2,4 @@ FEATURES_PROVIDED += cpp FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_hwrng FEATURES_PROVIDED += periph_pm +FEATURES_PROVIDED += periph_pwm diff --git a/cpu/native/include/periph_conf.h b/cpu/native/include/periph_conf.h index 7eabc76c0d..6931fcbd3c 100644 --- a/cpu/native/include/periph_conf.h +++ b/cpu/native/include/periph_conf.h @@ -72,6 +72,13 @@ #endif /** @} */ +/** + * @brief PWM configuration + */ +#ifndef PWM_NUMOF +#define PWM_NUMOF (8U) +#endif + /** * @brief QDEC configuration */ diff --git a/cpu/native/periph/pwm.c b/cpu/native/periph/pwm.c new file mode 100644 index 0000000000..d0d936d723 --- /dev/null +++ b/cpu/native/periph/pwm.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2018 Gilles DOFFE + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup cpu_native + * @ingroup drivers_periph_pwm + * @{ + * + * @file + * @brief Low-level PWM driver implementation + * + * @author Gilles DOFFE + * @} + */ + +#include + +#include "periph/pwm.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +#ifdef PWM_NUMOF + +#define NATIVE_PWM_SPEED 1000000 +#define NATIVE_PWM_NB_CHAN 2 +#define NATIVE_PWM_MAX 65535 + +typedef struct { + uint16_t duty_cycle; + uint8_t on; +} native_pwm_t; + +native_pwm_t pwms[PWM_NUMOF][NATIVE_PWM_NB_CHAN]; + +uint32_t pwm_init(pwm_t pwm, pwm_mode_t mode, uint32_t freq, uint16_t res) +{ + uint8_t i = 0; + + (void) mode; + (void) freq; + (void) res; + + if (pwm >= PWM_NUMOF) + { + errno = ENODEV; + goto pwm_init_err; + } + + /* reset pwm channels */ + for (i = 0; i < NATIVE_PWM_NB_CHAN; i++) + { + pwms[pwm][i].duty_cycle = 0; + pwms[pwm][i].on = 0; + } + + return freq; + +pwm_init_err: + return 0; +} + +uint8_t pwm_channels(pwm_t pwm) +{ + (void) pwm; + return NATIVE_PWM_NB_CHAN; +} + +void pwm_set(pwm_t pwm, uint8_t channel, uint16_t value) +{ + assert(pwm < PWM_NUMOF); + + /* set new value */ + pwms[pwm][channel].duty_cycle = value; + DEBUG("%s pwms[%u][%u] = %u\n", __func__, pwm, channel, value); +} + +void pwm_poweron(pwm_t pwm) +{ + uint8_t i = 0; + + assert(pwm < PWM_NUMOF); + /* reset pwm channels */ + for (i = 0; i < NATIVE_PWM_NB_CHAN; i++) + pwms[pwm][i].on = 1; +} + +void pwm_poweroff(pwm_t pwm) +{ + uint8_t i = 0; + + assert(pwm < PWM_NUMOF); + /* reset pwm channels */ + for (i = 0; i < NATIVE_PWM_NB_CHAN; i++) + pwms[pwm][i].on = 0; +} + +#endif /* PWM_NUMOF */