blob: 1b6b6a5c8cec0878f55c4b8ae75175331fe4ca60 [file] [log] [blame]
/****************************************************************************
* boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_pwm.c
*
* Copyright (C) 2019 Fundação CERTI. All rights reserved.
* Author: Daniel Pereira Volpato <dpo@certi.org.br>
*
* 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 <nuttx/config.h>
#include <errno.h>
#include <debug.h>
#include <sys/types.h>
#include <nuttx/timers/pwm.h>
#include <arch/board/board.h>
#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
}