Skip to content

Commit

Permalink
stm32f7: Add basic PWM support to nucleo-144 board
Browse files Browse the repository at this point in the history
More PWMs and multi channels support
may be investigated and added later.

It was tested on nucleo-f767zi
with those features enabled in configuration:

```
CONFIG_EXAMPLES_PWM=y
CONFIG_PWM=y
CONFIG_STM32F7_TIM1=y
CONFIG_STM32F7_TIM1_PWM=y
CONFIG_STM32F7_TIM2=y
(...)
CONFIG_STM32F7_TIM4_PWM=y
```

Change-Id: I08ebd4a538d15661788a0a54c2113ad767f22747
Bug: https://bitbucket.org/nuttx/nuttx/issues/153
Relate-to: rzr/webthing-iotjs#3
Forwarded: https://bitbucket.org/nuttx/nuttx/pull-requests/862
Signed-off-by: Philippe Coval <p.coval@samsung.com>
  • Loading branch information
rzr committed Apr 27, 2019
1 parent b80ad0f commit 8bc7287
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
6 changes: 6 additions & 0 deletions configs/nucleo-144/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@

/* Alternate function pin selections ************************************************/

/* TIM */
#define GPIO_TIM1_CH1OUT GPIO_TIM1_CH1OUT_1
#define GPIO_TIM2_CH1OUT GPIO_TIM2_CH1OUT_1
#define GPIO_TIM3_CH1OUT GPIO_TIM3_CH1OUT_1
#define GPIO_TIM4_CH1OUT GPIO_TIM4_CH1OUT_1

#if defined(CONFIG_NUCLEO_CONSOLE_ARDUINO)
/* USART6:
*
Expand Down
4 changes: 4 additions & 0 deletions configs/nucleo-144/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ ifeq ($(CONFIG_ADC),y)
CSRCS += stm32_adc.c
endif

ifeq ($(CONFIG_PWM),y)
CSRCS += stm32_pwm.c
endif

ifeq ($(CONFIG_MMCSD),y)
CSRCS += stm32_sdio.c
endif
Expand Down
12 changes: 12 additions & 0 deletions configs/nucleo-144/src/nucleo-144.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ int stm32_sdio_initialize(void);
void stm32_usbinitialize(void);
#endif

/************************************************************************************
* Name: stm32_pwm_setup
*
* Description:
* Initialize PWM and register the PWM device.
*
************************************************************************************/

#ifdef CONFIG_PWM
int stm32_pwm_setup(void);
#endif

/************************************************************************************
* Name: stm32_adc_setup
*
Expand Down
10 changes: 10 additions & 0 deletions configs/nucleo-144/src/stm32_appinitialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ int board_app_initialize(uintptr_t arg)
}
#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);
return OK;
}
162 changes: 162 additions & 0 deletions configs/nucleo-144/src/stm32_pwm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/************************************************************************************
* configs/nucleo-144/src/stm32_pwm.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
* Author: Philippe Coval <p.coval@samsung.com>
*
* 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/board.h>
#include <nuttx/drivers/pwm.h>
#include <arch/board/board.h>

#include "chip.h"
#include "up_arch.h"
#include "stm32_pwm.h"
#include "nucleo-144.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_STM32F7_TIM1_PWM)
pwm = stm32_pwminitialize(1);
if (!pwm)
{
aerr("ERROR: Failed to get the STM32F7 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_STM32F7_TIM2_PWM)
pwm = stm32_pwminitialize(2);
if (!pwm)
{
aerr("ERROR: Failed to get the STM32F7 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_STM32F7_TIM3_PWM)
pwm = stm32_pwminitialize(3);
if (!pwm)
{
aerr("ERROR: Failed to get the STM32F7 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_STM32F7_TIM4_PWM)
pwm = stm32_pwminitialize(4);
if (!pwm)
{
aerr("ERROR: Failed to get the STM32F7 PWM lower half\n");
return -ENODEV;
}

ret = pwm_register("/dev/pwm3", 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
}

0 comments on commit 8bc7287

Please sign in to comment.