boards/arduino-r4-minima: Add PWM support using GPT timer

- Added PWM support on Arduino R4 Minima board.
This board has 5 PWM channels available using the GPT timer from the RA4M1 microcontroller.
- Added a pwm config file to enable PWM supporting GTP2 GTIOCB on P102.

Signed-off-by: leocafonso <leocafonso@gmail.com>
This commit is contained in:
leocafonso
2025-11-16 12:22:59 -08:00
committed by Alan C. Assis
parent 2667f51c82
commit 80b9fc30de
7 changed files with 213 additions and 0 deletions
+16
View File
@@ -5,4 +5,20 @@
if ARCH_BOARD_ARDUINO_R4_MINIMA
config ARDUINO_R4_MINIMA_PWM_CHANNEL
int "PWM channel number"
default 0 if RA_GPT0_PWM
default 1 if RA_GPT1_PWM
default 2 if RA_GPT2_PWM
default 3 if RA_GPT3_PWM
default 4 if RA_GPT4_PWM
default 5 if RA_GPT5_PWM
default 6 if RA_GPT6_PWM
default 7 if RA_GPT7_PWM
range 0 7
depends on PWM && RA_PWM
---help---
Selects the PWM channel number that will be used to perform the PWM
test. See apps/examples/pwm.
endif
@@ -0,0 +1,41 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
CONFIG_ARCH="arm"
CONFIG_ARCH_BOARD="arduino-r4-minima"
CONFIG_ARCH_BOARD_ARDUINO_R4_MINIMA=y
CONFIG_ARCH_CHIP="ra4"
CONFIG_ARCH_CHIP_R7FA4M1ABxxFM=y
CONFIG_ARCH_CHIP_RA4=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARD_LOOPSPERMSEC=6965
CONFIG_BUILTIN=y
CONFIG_EXAMPLES_PWM=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_DISABLE_IFUPDOWN=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=32768
CONFIG_RAM_START=0x20000000
CONFIG_RA_GPT2=y
CONFIG_RA_GPT2_OUTPUTB=y
CONFIG_RA_GPT2_PWM=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_SCI2_SERIAL_CONSOLE=y
CONFIG_START_DAY=28
CONFIG_START_MONTH=6
CONFIG_START_YEAR=2013
CONFIG_SYSTEM_NSH=y
CONFIG_TASK_NAME_SIZE=32
@@ -77,6 +77,23 @@
#define GPIO_SCI9_RX GPIO_RXD9_MISO9_SCL9_1 /* P110 */
#define GPIO_SCI9_TX GPIO_TXD9_MOSI9_SDA9_1 /* P109 */
/* PWM Pins *****************************************************************/
/* PWM output pins:
* D3 - P104
* D5 - P102
* D6 - P106
* D9 - P303
* D10 - P112
* D11 - P109
*/
#define GPIO_GPT1_GTIOCB GPIO_GPT_GTIOC1B_P104
#define GPIO_GPT2_GTIOCB GPIO_GPT_GTIOC2B_P102
#define GPIO_GPT0_GTIOCB GPIO_GPT_GTIOC0B_P106
#define GPIO_GPT7_GTIOCB GPIO_GPT_GTIOC7B_P303
#define GPIO_GPT3_GTIOCB GPIO_GPT_GTIOC3B_P112
#define GPIO_GPT1_GTIOCA GPIO_GPT_GTIOC1A_P109
/* LED pin selections */
#define GPIO_L_LED (gpio_pinset_t){ PORT1,PIN11, (GPIO_OUPUT | GPIO_LOW_DRIVE | GPIO_OUTPUT_LOW)} /* P111 */
@@ -26,6 +26,10 @@ else()
list(APPEND SRCS ra4m1_userleds.c)
endif()
if(CONFIG_PWM)
list(APPEND SRCS ra4m1_pwm.c)
endif()
target_sources(board PRIVATE ${SRCS})
set_property(GLOBAL PROPERTY LD_SCRIPT
@@ -32,4 +32,8 @@ ifeq ($(CONFIG_BOARDCTL),y)
CSRCS += ra4m1_appinit.c
endif
ifeq ($(CONFIG_PWM),y)
CSRCS += ra4m1_pwm.c
endif
include $(TOPDIR)/boards/Board.mk
@@ -45,6 +45,10 @@
# define HAVE_LEDS 1
#endif
#ifdef CONFIG_PWM
extern int ra4m1_pwm_setup(void);
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -75,6 +79,16 @@ int ra4m1_bringup(void)
}
#endif
#ifdef CONFIG_PWM
/* Initialize PWM and register the PWM device. */
ret = ra4m1_pwm_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: ra4m1_pwm_setup() failed: %d\n", ret);
}
#endif
UNUSED(ret);
return OK;
}
@@ -0,0 +1,117 @@
/****************************************************************************
* boards/arm/ra4/arduino-r4-minima/src/ra4m1_pwm.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
#include <nuttx/timers/pwm.h>
#include <arch/board/board.h>
#include "ra_pwm.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* PWM.
* There are no dedicated PWM output pins available to the user for PWM
* testing.
*/
#ifndef CONFIG_ARDUINO_R4_MINIMA_PWM_CHANNEL
# if defined(CONFIG_RA4M1_PWM_CHAN0)
# warning Assuming PWM channel 0
# define CONFIG_ARDUINO_R4_MINIMA_PWM_CHANNEL 0
# elif defined(CONFIG_RA4M1_PWM_CHAN1)
# warning Assuming PWM channel 1
# define CONFIG_ARDUINO_R4_MINIMA_PWM_CHANNEL 1
# elif defined(CONFIG_RA4M1_PWM_CHAN2)
# warning Assuming PWM channel 2
# define CONFIG_ARDUINO_R4_MINIMA_PWM_CHANNEL 2
# elif defined(CONFIG_RA4M1_PWM_CHAN3)
# warning Assuming PWM channel 3
# define CONFIG_ARDUINO_R4_MINIMA_PWM_CHANNEL 3
# endif
#endif
#if defined(CONFIG_PWM) && defined(CONFIG_RA_PWM)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ra4m1_pwm_setup
*
* Description:
* Initialize PWM and register the PWM device.
*
****************************************************************************/
int ra4m1_pwm_setup(void)
{
static bool initialized = false;
struct pwm_lowerhalf_s *pwm;
int ret;
/* Have we already initialized? */
if (!initialized)
{
/* Call ra_pwminitialize() to get an instance of the PWM interface */
pwm = ra_pwminitialize(CONFIG_ARDUINO_R4_MINIMA_PWM_CHANNEL);
if (!pwm)
{
_err("ERROR: Failed to get the RA4M1 PWM lower half\n");
return -ENODEV;
}
/* Register the PWM driver at "/dev/pwm0" */
ret = pwm_register("/dev/pwm0", pwm);
if (ret < 0)
{
aerr("ERROR: pwm_register failed: %d\n", ret);
return ret;
}
/* Now we are initialized */
initialized = true;
}
return OK;
}
#endif /* CONFIG_PWM */