diff --git a/boards/arm/stm32f0l0g0/nucleo-g070rb/configs/nsh/defconfig b/boards/arm/stm32f0l0g0/nucleo-g070rb/configs/nsh/defconfig index 88139cca0e4..b3ebaf95438 100644 --- a/boards/arm/stm32f0l0g0/nucleo-g070rb/configs/nsh/defconfig +++ b/boards/arm/stm32f0l0g0/nucleo-g070rb/configs/nsh/defconfig @@ -25,6 +25,7 @@ CONFIG_DISABLE_MQUEUE=y CONFIG_DISABLE_POSIX_TIMERS=y CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y CONFIG_EXAMPLES_HELLO=y +CONFIG_EXAMPLES_PWM=y CONFIG_EXAMPLES_TIMER=y CONFIG_INTELHEX_BINARY=y CONFIG_MAX_TASKS=8 @@ -41,6 +42,7 @@ CONFIG_PREALLOC_TIMERS=0 CONFIG_PREALLOC_WDOGS=4 CONFIG_PTHREAD_MUTEX_UNSAFE=y CONFIG_PTHREAD_STACK_DEFAULT=1536 +CONFIG_PWM_NCHANNELS=2 CONFIG_RAM_SIZE=20480 CONFIG_RAM_START=0x20000000 CONFIG_RAW_BINARY=y @@ -51,7 +53,15 @@ CONFIG_START_DAY=19 CONFIG_START_MONTH=5 CONFIG_START_YEAR=2013 CONFIG_STDIO_DISABLE_BUFFERING=y +CONFIG_STM32F0L0G0_PWM_MULTICHAN=y CONFIG_STM32F0L0G0_PWR=y +CONFIG_STM32F0L0G0_TIM1=y +CONFIG_STM32F0L0G0_TIM3=y +CONFIG_STM32F0L0G0_TIM3_CH1OUT=y +CONFIG_STM32F0L0G0_TIM3_CH2OUT=y +CONFIG_STM32F0L0G0_TIM3_CHANNEL1=y +CONFIG_STM32F0L0G0_TIM3_CHANNEL2=y +CONFIG_STM32F0L0G0_TIM3_PWM=y CONFIG_STM32F0L0G0_TIM6=y CONFIG_STM32F0L0G0_USART2=y CONFIG_SYSTEM_NSH=y diff --git a/boards/arm/stm32f0l0g0/nucleo-g070rb/include/board.h b/boards/arm/stm32f0l0g0/nucleo-g070rb/include/board.h index b0c1bd4d277..aec34c63948 100644 --- a/boards/arm/stm32f0l0g0/nucleo-g070rb/include/board.h +++ b/boards/arm/stm32f0l0g0/nucleo-g070rb/include/board.h @@ -214,6 +214,13 @@ /* Alternate function pin selections ****************************************/ +/* TIM */ + +#define GPIO_TIM1_CH2OUT GPIO_TIM1_CH2OUT_2 /* PB3 - CN9 4 (D3) */ +#define GPIO_TIM3_CH1OUT GPIO_TIM3_CH1OUT_2 /* PB4 - CN9 6 (D5) */ +#define GPIO_TIM3_CH2OUT GPIO_TIM3_CH2OUT_3 /* PC7 - CN5 2 (D9) */ +#define GPIO_TIM3_CH3OUT GPIO_TIM3_CH3OUT_1 /* PB0 - CN5 3 (D10) */ + /* USART */ /* By default the USART2 is connected to STLINK Virtual COM Port: diff --git a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/Makefile b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/Makefile index 622ec5c1652..727c63c2401 100644 --- a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/Makefile +++ b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/Makefile @@ -55,6 +55,10 @@ ifeq ($(CONFIG_LIB_BOARDCTL),y) CSRCS += stm32_appinit.c endif +ifeq ($(CONFIG_PWM),y) +CSRCS += stm32_pwm.c +endif + ifeq ($(CONFIG_TIMER),y) CSRCS += stm32_timer.c endif diff --git a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/nucleo-g070rb.h b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/nucleo-g070rb.h index 650f76f6f4c..2137cf91d16 100644 --- a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/nucleo-g070rb.h +++ b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/nucleo-g070rb.h @@ -108,6 +108,18 @@ * Public Function Prototypes ****************************************************************************/ +/**************************************************************************** + * Name: stm32_pwm_setup + * + * Description: + * Initialize PWM and register the PWM device. + * + ****************************************************************************/ + +#ifdef CONFIG_PWM +int stm32_pwm_setup(void); +#endif + /**************************************************************************** * Name: stm32_timer_driver_setup * @@ -115,7 +127,8 @@ * Configure the timer driver. * * Input Parameters: - * devpath - The full path to the timer device. This should be of the form /dev/timer0 + * devpath - The full path to the timer device. This should be of the + * form /dev/timer0 * timer - The timer's number. * * Returned Value: diff --git a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_bringup.c b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_bringup.c index 6a866f5f6c0..50e62106355 100644 --- a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_bringup.c +++ b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_bringup.c @@ -112,23 +112,35 @@ int stm32_bringup(void) #endif #ifdef CONFIG_TIMER - /*Initialize basic timers */ -# if defined(CONFIG_STM32F0L0G0_TIM6) + /* Initialize basic timers */ + +#if defined(CONFIG_STM32F0L0G0_TIM6) syslog(LOG_ERR, "Init timer\n"); ret = stm32_timer_driver_setup("/dev/timer0", 6); if (ret < 0) { syslog(LOG_ERR, "ERROR: stm32_timer_driver_setup failed. TIM6: %d\n", ret); } -# endif -# if defined(CONFIG_STM32F0L0G0_TIM7) +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM7) ret = stm32_timer_driver_setup("/dev/timer1", 7); if (ret < 0) { syslog(LOG_ERR, "ERROR: stm32_timer_driver_setup failed. TIM7: %d\n", ret); } -# endif +#endif +#endif + +#if defined(CONFIG_PWM) + /* Initialize PWM and register the PWM device */ + + ret = stm32_pwm_setup(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: stm32_pwm_setup() failed: %d\n", ret); + } #endif UNUSED(ret); diff --git a/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_pwm.c b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_pwm.c new file mode 100644 index 00000000000..1b6b6a5c8ce --- /dev/null +++ b/boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_pwm.c @@ -0,0 +1,207 @@ +/**************************************************************************** + * boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_pwm.c + * + * Copyright (C) 2019 Fundação CERTI. All rights reserved. + * Author: Daniel Pereira Volpato + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include +#include + +#include "nucleo-g070rb.h" +#include "stm32_pwm.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HAVE_PWM 1 +#ifndef CONFIG_PWM +# undef HAVE_PWM +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_pwm_setup + * + * Description: + * Initialize PWM and register the PWM device. + * + ****************************************************************************/ + +int stm32_pwm_setup(void) +{ +#ifdef HAVE_PWM + static bool initialized = false; + struct pwm_lowerhalf_s *pwm; + int ret; + + /* Have we already initialized? */ + + if (!initialized) + { + /* Call stm32_pwminitialize() to get an instance of the PWM interface */ + +#if defined(CONFIG_STM32F0L0G0_TIM1_PWM) + pwm = stm32_pwminitialize(1); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm0", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM2_PWM) + pwm = stm32_pwminitialize(2); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm1", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM3_PWM) + pwm = stm32_pwminitialize(3); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm2", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM14_PWM) + pwm = stm32_pwminitialize(14); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm13", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM15_PWM) + pwm = stm32_pwminitialize(15); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm14", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM16_PWM) + pwm = stm32_pwminitialize(16); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm15", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#if defined(CONFIG_STM32F0L0G0_TIM17_PWM) + pwm = stm32_pwminitialize(17); + if (!pwm) + { + aerr("ERROR: Failed to get the STM32 PWM lower half\n"); + return -ENODEV; + } + + ret = pwm_register("/dev/pwm16", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + + /* Now we are initialized */ + + initialized = true; + } + + return OK; +#else + return -ENODEV; +#endif +}