From 1f323fe6beecb847b31848c99280efe4be729100 Mon Sep 17 00:00:00 2001 From: Michal Lenc Date: Fri, 16 Oct 2020 16:02:25 +0200 Subject: [PATCH] imxrt1060-evk: Added support for FlexCAN driver Signed-off-by: Michal Lenc --- boards/arm/imxrt/imxrt1060-evk/README.txt | 25 +++++ .../imxrt/imxrt1060-evk/configs/can/defconfig | 80 ++++++++++++++++ .../imxrt1060-evk/configs/canfd/defconfig | 81 ++++++++++++++++ .../arm/imxrt/imxrt1060-evk/include/board.h | 12 +++ boards/arm/imxrt/imxrt1060-evk/src/Makefile | 4 + .../imxrt/imxrt1060-evk/src/imxrt1060-evk.h | 14 +++ .../imxrt/imxrt1060-evk/src/imxrt_appinit.c | 17 +++- .../imxrt/imxrt1060-evk/src/imxrt_bringup.c | 14 +++ .../imxrt/imxrt1060-evk/src/imxrt_flexcan.c | 96 +++++++++++++++++++ 9 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 boards/arm/imxrt/imxrt1060-evk/configs/can/defconfig create mode 100644 boards/arm/imxrt/imxrt1060-evk/configs/canfd/defconfig create mode 100644 boards/arm/imxrt/imxrt1060-evk/src/imxrt_flexcan.c diff --git a/boards/arm/imxrt/imxrt1060-evk/README.txt b/boards/arm/imxrt/imxrt1060-evk/README.txt index c39f7d8765..06ecda5820 100644 --- a/boards/arm/imxrt/imxrt1060-evk/README.txt +++ b/boards/arm/imxrt/imxrt1060-evk/README.txt @@ -184,6 +184,31 @@ Configurations Configuration sub-directories ----------------------------- + can: + + This is an nsh configuration (see below) with added support of CAN driver. + FlexCAN3 is chosen as default, the change can be made at System type peripheral + selection. Please note that only FlexCAN3 and FlexCAN2 is available on this board. + + Bitrate and sample point can be also changed at System type peripheral selection, + basic values are 1 MHz for bitrate and 0.80 for sample point. The FlexCAN driver + for imxrt runs at 80 MHz clock frequency. + + The configuration also includes CAN utilities as candump and cansend. + + canfd: + + This is an nsh configuration (see below) with added support of CAN_FD driver. + FlexCAN3 is chosen as default, please note that only FlexCAN3 is capable of + providing CAN_FD support. + + Bitrate and sample point can be also changed at System type peripheral selection, + basic values are 1 MHz for bitrate and 0.80 for sample point for arbitration phase + and 4 MHz (bitrate) and 0.90 (sample point) for data phase. The FlexCAN driver + for imxrt runs at 80 MHz clock frequency. + + The configuration also includes CAN utilities as candump and cansend. + knsh: This is identical to the nsh configuration below except that NuttX diff --git a/boards/arm/imxrt/imxrt1060-evk/configs/can/defconfig b/boards/arm/imxrt/imxrt1060-evk/configs/can/defconfig new file mode 100644 index 0000000000..d23f50a5b2 --- /dev/null +++ b/boards/arm/imxrt/imxrt1060-evk/configs/can/defconfig @@ -0,0 +1,80 @@ +# +# 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_NET_CAN_CANFD is not set +# CONFIG_NET_ETHERNET is not set +# CONFIG_NET_IPv4 is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="imxrt1060-evk" +CONFIG_ARCH_BOARD_IMXRT1060_EVK=y +CONFIG_ARCH_CHIP="imxrt" +CONFIG_ARCH_CHIP_IMXRT=y +CONFIG_ARCH_CHIP_MIMXRT1062DVL6A=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARMV7M_DCACHE=y +CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y +CONFIG_ARMV7M_ICACHE=y +CONFIG_ARMV7M_USEBASEPRI=y +CONFIG_BOARD_LOOPSPERMSEC=104926 +CONFIG_BUILTIN=y +CONFIG_CAN=y +CONFIG_CANUTILS_CANDUMP=y +CONFIG_CANUTILS_CANSEND=y +CONFIG_DEBUG_BINFMT=y +CONFIG_DEBUG_BINFMT_ERROR=y +CONFIG_DEBUG_BINFMT_INFO=y +CONFIG_DEBUG_BINFMT_WARN=y +CONFIG_DEBUG_CAN=y +CONFIG_DEBUG_CAN_ERROR=y +CONFIG_DEBUG_CAN_INFO=y +CONFIG_DEBUG_CAN_WARN=y +CONFIG_DEBUG_ERROR=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_INFO=y +CONFIG_DEBUG_IRQ=y +CONFIG_DEBUG_IRQ_ERROR=y +CONFIG_DEBUG_IRQ_INFO=y +CONFIG_DEBUG_IRQ_WARN=y +CONFIG_DEBUG_NET=y +CONFIG_DEBUG_NET_ERROR=y +CONFIG_DEBUG_NET_INFO=y +CONFIG_DEBUG_NET_WARN=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEBUG_WARN=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_IMXRT_FLEXCAN3=y +CONFIG_IMXRT_LPUART1=y +CONFIG_INTELHEX_BINARY=y +CONFIG_LPUART1_SERIAL_CONSOLE=y +CONFIG_MAX_TASKS=16 +CONFIG_NET=y +CONFIG_NETDEVICES=y +CONFIG_NETDEV_IFINDEX=y +CONFIG_NETDEV_LATEINIT=y +CONFIG_NETDEV_STATISTICS=y +CONFIG_NET_CAN=y +CONFIG_NET_CAN_NOTIFIER=y +CONFIG_NET_SOCKOPTS=y +CONFIG_NET_STATISTICS=y +CONFIG_NET_TIMESTAMP=y +CONFIG_NFILE_DESCRIPTORS=8 +CONFIG_NFILE_STREAMS=8 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_RAM_SIZE=1048576 +CONFIG_RAM_START=0x20200000 +CONFIG_SCHED_HPWORK=y +CONFIG_SIG_DEFAULT=y +CONFIG_START_DAY=14 +CONFIG_START_MONTH=3 +CONFIG_SYSTEM_NSH=y +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/boards/arm/imxrt/imxrt1060-evk/configs/canfd/defconfig b/boards/arm/imxrt/imxrt1060-evk/configs/canfd/defconfig new file mode 100644 index 0000000000..db5b96bfe3 --- /dev/null +++ b/boards/arm/imxrt/imxrt1060-evk/configs/canfd/defconfig @@ -0,0 +1,81 @@ +# +# 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_NET_ETHERNET is not set +# CONFIG_NET_IPv4 is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="imxrt1060-evk" +CONFIG_ARCH_BOARD_IMXRT1060_EVK=y +CONFIG_ARCH_CHIP="imxrt" +CONFIG_ARCH_CHIP_IMXRT=y +CONFIG_ARCH_CHIP_MIMXRT1062DVL6A=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARMV7M_DCACHE=y +CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y +CONFIG_ARMV7M_ICACHE=y +CONFIG_ARMV7M_USEBASEPRI=y +CONFIG_BOARD_LOOPSPERMSEC=104926 +CONFIG_BUILTIN=y +CONFIG_CAN=y +CONFIG_CANUTILS_CANDUMP=y +CONFIG_CANUTILS_CANSEND=y +CONFIG_CAN_EXTID=y +CONFIG_CAN_FD=y +CONFIG_DEBUG_BINFMT=y +CONFIG_DEBUG_BINFMT_ERROR=y +CONFIG_DEBUG_BINFMT_INFO=y +CONFIG_DEBUG_BINFMT_WARN=y +CONFIG_DEBUG_CAN=y +CONFIG_DEBUG_CAN_ERROR=y +CONFIG_DEBUG_CAN_INFO=y +CONFIG_DEBUG_CAN_WARN=y +CONFIG_DEBUG_ERROR=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_INFO=y +CONFIG_DEBUG_IRQ=y +CONFIG_DEBUG_IRQ_ERROR=y +CONFIG_DEBUG_IRQ_INFO=y +CONFIG_DEBUG_IRQ_WARN=y +CONFIG_DEBUG_NET=y +CONFIG_DEBUG_NET_ERROR=y +CONFIG_DEBUG_NET_INFO=y +CONFIG_DEBUG_NET_WARN=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEBUG_WARN=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_IMXRT_FLEXCAN3=y +CONFIG_IMXRT_LPUART1=y +CONFIG_INTELHEX_BINARY=y +CONFIG_LPUART1_SERIAL_CONSOLE=y +CONFIG_MAX_TASKS=16 +CONFIG_NET=y +CONFIG_NETDEVICES=y +CONFIG_NETDEV_IFINDEX=y +CONFIG_NETDEV_LATEINIT=y +CONFIG_NETDEV_STATISTICS=y +CONFIG_NET_CAN=y +CONFIG_NET_CAN_NOTIFIER=y +CONFIG_NET_CAN_SOCK_OPTS=y +CONFIG_NET_STATISTICS=y +CONFIG_NET_TIMESTAMP=y +CONFIG_NFILE_DESCRIPTORS=8 +CONFIG_NFILE_STREAMS=8 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_RAM_SIZE=1048576 +CONFIG_RAM_START=0x20200000 +CONFIG_SCHED_HPWORK=y +CONFIG_SIG_DEFAULT=y +CONFIG_START_DAY=14 +CONFIG_START_MONTH=3 +CONFIG_SYSTEM_NSH=y +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/boards/arm/imxrt/imxrt1060-evk/include/board.h b/boards/arm/imxrt/imxrt1060-evk/include/board.h index d83a9e47b8..87b6b9539a 100644 --- a/boards/arm/imxrt/imxrt1060-evk/include/board.h +++ b/boards/arm/imxrt/imxrt1060-evk/include/board.h @@ -126,6 +126,9 @@ #define IMXRT_LPI2C_CLK_SELECT CCM_CSCDR2_LPI2C_CLK_SEL_PLL3_60M #define IMXRT_LSI2C_PODF_DIVIDER 5 +#define IMXRT_CAN_CLK_SELECT CCM_CSCMR2_CAN_CLK_SEL_PLL3_SW_80 +#define IMXRT_CAN_PODF_DIVIDER 1 + #define IMXRT_SYS_PLL_SELECT CCM_ANALOG_PLL_SYS_DIV_SELECT_22 #define IMXRT_USB1_PLL_DIV_SELECT CCM_ANALOG_PLL_USB1_DIV_SELECT_20 @@ -323,6 +326,15 @@ #define GPIO_LPSPI3_MISO (GPIO_LPSPI3_SDI_2|IOMUX_LPSPI_DEFAULT) /* GPIO_AD_B0_02 */ #define GPIO_LPSPI3_MOSI (GPIO_LPSPI3_SDO_2|IOMUX_LPSPI_DEFAULT) /* GPIO_AD_B0_01 */ + +/* FlexCAN */ + +#define GPIO_FLEXCAN2_TX (GPIO_FLEXCAN2_TX_3|IOMUX_CAN_DEFAULT) +#define GPIO_FLEXCAN2_RX (GPIO_FLEXCAN2_RX_3|IOMUX_CAN_DEFAULT) + +#define GPIO_FLEXCAN3_TX (GPIO_FLEXCAN3_TX_2|IOMUX_CAN_DEFAULT) +#define GPIO_FLEXCAN3_RX (GPIO_FLEXCAN3_RX_2|IOMUX_CAN_DEFAULT) + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/boards/arm/imxrt/imxrt1060-evk/src/Makefile b/boards/arm/imxrt/imxrt1060-evk/src/Makefile index 9efc123ea2..c17981ae1d 100644 --- a/boards/arm/imxrt/imxrt1060-evk/src/Makefile +++ b/boards/arm/imxrt/imxrt1060-evk/src/Makefile @@ -89,4 +89,8 @@ ifeq ($(CONFIG_IMXRT_USBOTG),y) CSRCS += imxrt_usbhost.c endif +ifeq ($(CONFIG_IMXRT_FLEXCAN),y) +CSRCS += imxrt_flexcan.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/boards/arm/imxrt/imxrt1060-evk/src/imxrt1060-evk.h b/boards/arm/imxrt/imxrt1060-evk/src/imxrt1060-evk.h index 216b916d6f..b76dc08729 100644 --- a/boards/arm/imxrt/imxrt1060-evk/src/imxrt1060-evk.h +++ b/boards/arm/imxrt/imxrt1060-evk/src/imxrt1060-evk.h @@ -88,6 +88,8 @@ #define GPIO_LED (GPIO_OUTPUT | IOMUX_LED_DEFAULT | \ GPIO_OUTPUT_ZERO | GPIO_PORT1 | GPIO_PIN9) /* AD_BO_09 */ +#define LED_DRIVER_PATH "/dev/userleds" + /* Buttons ******************************************************************/ /* The IMXRT board has one external user button @@ -275,6 +277,18 @@ void imxrt_autoled_initialize(void); int imxrt_gpio_initialize(void); #endif +/**************************************************************************** + * Name: imxrt_can_setup + * + * Description: + * Initialize CAN and register the CAN device + * + ****************************************************************************/ + +#ifdef CONFIG_IMXRT_FLEXCAN +int imxrt_can_setup(void); +#endif + /**************************************************************************** * Name: imxrt_adc_initialize * diff --git a/boards/arm/imxrt/imxrt1060-evk/src/imxrt_appinit.c b/boards/arm/imxrt/imxrt1060-evk/src/imxrt_appinit.c index aa4a70165f..77f798b92c 100644 --- a/boards/arm/imxrt/imxrt1060-evk/src/imxrt_appinit.c +++ b/boards/arm/imxrt/imxrt1060-evk/src/imxrt_appinit.c @@ -39,13 +39,17 @@ ****************************************************************************/ #include - +#include #include #include #include "imxrt1060-evk.h" +#if !defined(CONFIG_ARCH_LEDS) && defined(CONFIG_USERLED_LOWER) +# define HAVE_LEDS 0 +#endif + #ifdef CONFIG_LIB_BOARDCTL /**************************************************************************** @@ -79,6 +83,17 @@ int board_app_initialize(uintptr_t arg) { +#ifdef HAVE_LEDS + /* Register the LED driver */ + int ret; + ret = userled_lower_initialize(LED_DRIVER_PATH); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: userled_lower_initialize() failed: %d\n", ret); + return ret; + } +#endif + #ifndef CONFIG_BOARD_LATE_INITIALIZE /* Perform board initialization */ diff --git a/boards/arm/imxrt/imxrt1060-evk/src/imxrt_bringup.c b/boards/arm/imxrt/imxrt1060-evk/src/imxrt_bringup.c index c0e07ba79d..e9c0c774a0 100644 --- a/boards/arm/imxrt/imxrt1060-evk/src/imxrt_bringup.c +++ b/boards/arm/imxrt/imxrt1060-evk/src/imxrt_bringup.c @@ -63,6 +63,7 @@ # include #endif +#include "imxrt_enet.h" #include "imxrt1060-evk.h" #include /* Must always be included last */ @@ -212,6 +213,19 @@ int imxrt_bringup(void) } #endif +#if defined(CONFIG_IMXRT_ENET) && defined(CONFIG_NETDEV_LATEINIT) + ret = imxrt_netinitialize(0); +#endif + +#ifdef CONFIG_IMXRT_FLEXCAN + ret = imxrt_can_setup(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: imxrt_can_setup() failed: %d\n", ret); + return ret; + } +#endif + #ifdef CONFIG_DEV_GPIO /* Initialize the GPIO driver */ diff --git a/boards/arm/imxrt/imxrt1060-evk/src/imxrt_flexcan.c b/boards/arm/imxrt/imxrt1060-evk/src/imxrt_flexcan.c new file mode 100644 index 0000000000..2fc525ff40 --- /dev/null +++ b/boards/arm/imxrt/imxrt1060-evk/src/imxrt_flexcan.c @@ -0,0 +1,96 @@ +/**************************************************************************** + * boards/arm/imxrt/imxrt1060-evk/src/imxrt_flexcan.c + * + * + * 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 "imxrt_flexcan.h" +#include "imxrt1060-evk.h" + +#ifdef CONFIG_IMXRT_FLEXCAN + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: imxrt_can_setup + * + * Description: + * Initialize CAN and register the CAN device + * + ****************************************************************************/ + +int imxrt_can_setup(void) +{ + int ret; +#if defined(CONFIG_IMXRT_FLEXCAN3) && defined(CONFIG_IMXRT_FLEXCAN2) + canerr("ERROR: Only one FlexCAN interface can be defined at the same time on imxrt1060-evk\n"); + return -ENODEV; +#endif + +#ifdef CONFIG_IMXRT_FLEXCAN3 + /* Call arm_caninitialize() to get an instance of the CAN interface */ + + ret = imxrt_caninitialize(3); + if (ret < 0) + { + canerr("ERROR: Failed to get CAN interface\n"); + return -ENODEV; + } +#elif CONFIG_IMXRT_FLEXCAN2 + ret = imxrt_caninitialize(2); + if (ret < 0) + { + canerr("ERROR: Failed to get CAN interface\n"); + return -ENODEV; + } +#elif CONFIG_IMXRT_FLEXCAN1 + canerr("ERROR: FlexCAN1 is not available on imxrt1060-evk. Please choose FlexCAN2 or FlexCAN3 (CAN_FD available)\n"); + return -ENODEV; +#else + return -ENODEV; +#endif + return OK; +} + +#endif /* CONFIG_IMXRT_FLEXCAN */