xtensa/esp32s2: Add support to TWAI/CANBus controller

This commit is contained in:
Eren Terzioglu
2023-09-28 09:35:08 +08:00
committed by Xiang Xiao
parent fc76b9955c
commit ded321a515
20 changed files with 2472 additions and 17 deletions
@@ -295,4 +295,22 @@ After successfully built and flashed, run on the boards' terminal::
16 bits/sample and 48KHz. These arguments can be supplied to select
different audio formats, for instance::
nxlooper> loopback 2 8 44100
nxlooper> loopback 2 8 44100
twai
----
This configuration enables the support for the TWAI (Two-Wire Automotive Interface) driver.
You can test it by connecting TWAI RX and TWAI TX pins which are GPIO0 and GPIO2 by default
to a external transceiver or connecting TWAI RX to TWAI TX pin by enabling
the ``Device Drivers -> CAN Driver Support -> CAN loopback mode`` option and running the ``can`` example::
nsh> can
nmsgs: 0
min ID: 1 max ID: 2047
Bit timing:
Baud: 1000000
TSEG1: 15
TSEG2: 4
SJW: 3
ID: 1 DLC: 1
@@ -348,6 +348,24 @@ To test it, just run the following::
Where x in the timer instance.
twai
----
This configuration enables the support for the TWAI (Two-Wire Automotive Interface) driver.
You can test it by connecting TWAI RX and TWAI TX pins which are GPIO0 and GPIO2 by default
to a external transceiver or connecting TWAI RX to TWAI TX pin by enabling
the ``Device Drivers -> CAN Driver Support -> CAN loopback mode`` option and running the ``can`` example::
nsh> can
nmsgs: 0
min ID: 1 max ID: 2047
Bit timing:
Baud: 1000000
TSEG1: 15
TSEG2: 4
SJW: 3
ID: 1 DLC: 1
watchdog
--------
@@ -98,7 +98,7 @@ Peripheral Support NOTES
========== ======= =====
ADC No
AES No
CAN/TWAI No
CAN/TWAI Yes
DMA Yes
eFuse No
Ethernet No
+2 -2
View File
@@ -98,7 +98,7 @@
#define ESP32S2_PERIPH_LEDC 45
#define ESP32S2_PERIPH_EFUSE 46
#define ESP32S2_PERIPH_CAN 47
#define ESP32S2_PERIPH_TWAI 47
#define ESP32S2_PERIPH_USB 48
#define ESP32S2_PERIPH_RTC_CORE 49
@@ -227,7 +227,7 @@
#define ESP32S2_IRQ_LEDC (XTENSA_IRQ_FIRSTPERIPH + ESP32S2_PERIPH_LEDC)
#define ESP32S2_IRQ_EFUSE (XTENSA_IRQ_FIRSTPERIPH + ESP32S2_PERIPH_EFUSE)
#define ESP32S2_IRQ_CAN (XTENSA_IRQ_FIRSTPERIPH + ESP32S2_PERIPH_CAN)
#define ESP32S2_IRQ_TWAI (XTENSA_IRQ_FIRSTPERIPH + ESP32S2_PERIPH_TWAI)
#define ESP32S2_IRQ_USB (XTENSA_IRQ_FIRSTPERIPH + ESP32S2_PERIPH_USB)
#define ESP32S2_IRQ_RTC_CORE (XTENSA_IRQ_FIRSTPERIPH + ESP32S2_PERIPH_RTC_CORE)
+56
View File
@@ -446,6 +446,11 @@ config ESP32S2_I2C1
select ESP32S2_I2C
select I2C
config ESP32S2_TWAI
bool "TWAI (CAN)"
default n
select CAN
config ESP32S2_LEDC
bool "LEDC (PWM)"
default n
@@ -671,6 +676,57 @@ config ESP32S2_I2CTIMEOMS
endmenu # I2C Configuration
menu "TWAI driver options"
depends on ESP32S2_TWAI
if ESP32S2_TWAI
config ESP32S2_TWAI_TXPIN
int "TWAI TX Pin"
default 0
config ESP32S2_TWAI_RXPIN
int "TWAI RX Pin"
default 2
config ESP32S2_TWAI_BITRATE
int "TWAI bitrate"
default 1000000
---help---
TWAI bit rate.
config ESP32S2_TWAI_SAMPLEP
int "TWAI sample point"
default 80
---help---
TWAI sample point location as a percent value.
config ESP32S2_TWAI_SJW
int "TWAI synchronization jump width"
default 3
---help---
SJW limits the number of Time Quanta corrections during bit
Resynchronization.
config ESP32S2_TWAI_SAM
bool "TWAI sampling"
default n
---help---
The bus is sampled 3 times (recommended for low to medium speed buses
to spikes on the bus-line).
endif # ESP32S2_TWAI
config ESP32S2_TWAI_REGDEBUG
bool "TWAI register level debug"
depends on DEBUG_CAN_INFO
default n
---help---
Output detailed register-level TWAI debug information. Requires also
CONFIG_DEBUG_CAN_INFO.
endmenu #ESP32S2_TWAI
menu "SPI Flash Configuration"
choice ESP32S2_FLASH_MODE
+4
View File
@@ -59,6 +59,10 @@ ifeq ($(CONFIG_ESP32S2_I2S),y)
CHIP_CSRCS += esp32s2_i2s.c
endif
ifeq ($(CONFIG_ESP32S2_TWAI),y)
CHIP_CSRCS += esp32s2_twai.c
endif
ifeq ($(CONFIG_ESP32S2_LEDC),y)
CHIP_CSRCS += esp32s2_ledc.c
endif
File diff suppressed because it is too large Load Diff
+77
View File
@@ -0,0 +1,77 @@
/****************************************************************************
* arch/xtensa/src/esp32s2/esp32s2_twai.h
*
* 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.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_TWAI_H
#define __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_TWAI_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/can/can.h>
#include "hardware/esp32s2_twai.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#ifndef __ASSEMBLY__
#ifdef __cplusplus
extern "C"
{
#endif
/****************************************************************************
* Public Functions Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32s2_twaiinitialize
*
* Description:
* Initialize the selected CAN port
*
* Input Parameters:
* Port number (for hardware that has multiple TWAI interfaces)
*
* Returned Value:
* Valid TWAI device structure reference on success; a NULL on failure
*
****************************************************************************/
#if defined(CONFIG_CAN) && defined(CONFIG_ESP32S2_TWAI)
struct can_dev_s *esp32s2_twaiinitialize(void);
#endif
#ifdef __cplusplus
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_TWAI_H */
@@ -264,6 +264,7 @@
#define DR_REG_SPI4_BASE 0x3f437000
#define DR_REG_USB_WRAP_BASE 0x3f439000
#define DR_REG_APB_SARADC_BASE 0x3f440000
#define DR_REG_TWAI_BASE 0x6002B000
#define DR_REG_USB_BASE 0x60080000
#define REG_UHCI_BASE(i) (DR_REG_UHCI0_BASE)
@@ -455,14 +455,14 @@
#define SYSTEM_PWM1_CLK_EN_V 0x00000001
#define SYSTEM_PWM1_CLK_EN_S 20
/* SYSTEM_CAN_CLK_EN : R/W; bitpos: [19]; default: 0;
* Set this bit to enable clock of CAN.
/* SYSTEM_TWAI_CLK_EN : R/W; bitpos: [19]; default: 0;
* Set this bit to enable clock of TWAI.
*/
#define SYSTEM_CAN_CLK_EN (BIT(19))
#define SYSTEM_CAN_CLK_EN_M (SYSTEM_CAN_CLK_EN_V << SYSTEM_CAN_CLK_EN_S)
#define SYSTEM_CAN_CLK_EN_V 0x00000001
#define SYSTEM_CAN_CLK_EN_S 19
#define SYSTEM_TWAI_CLK_EN (BIT(19))
#define SYSTEM_TWAI_CLK_EN_M (SYSTEM_TWAI_CLK_EN_V << SYSTEM_TWAI_CLK_EN_S)
#define SYSTEM_TWAI_CLK_EN_V 0x00000001
#define SYSTEM_TWAI_CLK_EN_S 19
/* SYSTEM_I2C_EXT1_CLK_EN : R/W; bitpos: [18]; default: 0;
* Set this bit to enable clock of I2C EXT1.
@@ -809,14 +809,14 @@
#define SYSTEM_PWM1_RST_V 0x00000001
#define SYSTEM_PWM1_RST_S 20
/* SYSTEM_CAN_RST : R/W; bitpos: [19]; default: 0;
* Set this bit to reset CAN.
/* SYSTEM_TWAI_RST : R/W; bitpos: [19]; default: 0;
* Set this bit to reset TWAI.
*/
#define SYSTEM_CAN_RST (BIT(19))
#define SYSTEM_CAN_RST_M (SYSTEM_CAN_RST_V << SYSTEM_CAN_RST_S)
#define SYSTEM_CAN_RST_V 0x00000001
#define SYSTEM_CAN_RST_S 19
#define SYSTEM_TWAI_RST (BIT(19))
#define SYSTEM_TWAI_RST_M (SYSTEM_TWAI_RST_V << SYSTEM_TWAI_RST_S)
#define SYSTEM_TWAI_RST_V 0x00000001
#define SYSTEM_TWAI_RST_S 19
/* SYSTEM_I2C_EXT1_RST : R/W; bitpos: [18]; default: 0;
* Set this bit to reset I2C EXT1.
File diff suppressed because it is too large Load Diff
@@ -44,9 +44,9 @@ PROVIDE ( SPI2 = 0x3f424000 );
PROVIDE ( SPI3 = 0x3f425000 );
PROVIDE ( SYSCON = 0x3f426000 );
PROVIDE ( I2C1 = 0x3f427000 );
PROVIDE ( TWAI = 0x3f42b000 );
PROVIDE ( APB_SARADC = 0x3f440000 );
PROVIDE ( DEDIC_GPIO = 0x3f4cf000 );
PROVIDE ( TWAI = 0x6002B000 );
PROVIDE ( USB0 = 0x60080000 );
PROVIDE ( USBH = 0x60080000 );
PROVIDE ( USB_WRAP = 0x3f439000 );
@@ -36,6 +36,10 @@ ifeq ($(CONFIG_ESP32S2_I2S),y)
CSRCS += esp32s2_board_i2sdev.c
endif
ifeq ($(CONFIG_ESP32S2_TWAI),y)
CSRCS += esp32s2_board_twai.c
endif
ifeq ($(CONFIG_AUDIO_CS4344),y)
CSRCS += esp32s2_cs4344.c
endif
@@ -0,0 +1,89 @@
/****************************************************************************
* boards/xtensa/esp32s2/common/src/esp32s2_board_twai.c
*
* 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 <errno.h>
#include <debug.h>
#include <nuttx/can/can.h>
#include <arch/board/board.h>
#include "chip.h"
#include "esp32s2_twai.h"
#ifdef CONFIG_CAN
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_twai_setup
*
* Description:
* Initialize TWAI and register the TWAI device
*
****************************************************************************/
int board_twai_setup(void)
{
#ifdef CONFIG_ESP32S2_TWAI
struct can_dev_s *twai;
int ret;
/* Call esp32s2_twaiinitialize() to get an instance of the TWAI
* interface
* */
twai = esp32s2_twaiinitialize();
if (twai == NULL)
{
canerr("ERROR: Failed to get TWAI interface\n");
return -ENODEV;
}
/* Register the TWAI driver at "/dev/can0" */
ret = can_register("/dev/can0", twai);
if (ret < 0)
{
canerr("ERROR: TWAI1 register failed: %d\n", ret);
return ret;
}
return OK;
#else
return -ENODEV;
#endif
}
#endif /* CONFIG_CAN */
@@ -0,0 +1,46 @@
#
# 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_LEDS is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32s2-kaluga-1"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32S2_KALUGA_1=y
CONFIG_ARCH_CHIP="esp32s2"
CONFIG_ARCH_CHIP_ESP32S2=y
CONFIG_ARCH_CHIP_ESP32S2WROVER=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_ESP32S2_TWAI=y
CONFIG_ESP32S2_UART0=y
CONFIG_EXAMPLES_CAN=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSLOG_BUFFER=y
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y
@@ -166,6 +166,17 @@ int board_i2c_init(void);
int board_i2sdev_initialize(void);
#endif
/****************************************************************************
* Name: board_twai_setup
*
* Description:
* Initialize TWAI and register the TWAI device
*
****************************************************************************/
#ifdef CONFIG_ESP32S2_TWAI
int board_twai_setup(void);
#endif
/****************************************************************************
* Name: esp32s2_es8311_initialize
*
@@ -218,6 +218,17 @@ int esp32s2_bringup(void)
}
#endif
#ifdef CONFIG_ESP32S2_TWAI
/* Initialize TWAI and register the TWAI driver. */
ret = board_twai_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_twai_setup failed: %d\n", ret);
}
#endif
#ifdef CONFIG_INPUT_BUTTONS
/* Register the BUTTON driver */
@@ -0,0 +1,46 @@
#
# 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_LEDS is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32s2-saola-1"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32S2_SAOLA_1=y
CONFIG_ARCH_CHIP="esp32s2"
CONFIG_ARCH_CHIP_ESP32S2=y
CONFIG_ARCH_CHIP_ESP32S2WROVER=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_ESP32S2_TWAI=y
CONFIG_ESP32S2_UART0=y
CONFIG_EXAMPLES_CAN=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSLOG_BUFFER=y
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y
@@ -220,5 +220,16 @@ int esp32s2_cs4344_initialize(void);
int esp32s2_pwm_setup(void);
#endif
/****************************************************************************
* Name: board_twai_setup
*
* Description:
* Initialize TWAI and register the TWAI device
*
****************************************************************************/
#ifdef CONFIG_ESP32S2_TWAI
int board_twai_setup(void);
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32S2_ESP32S2_SAOLA_1_SRC_ESP32S2_SAOLA_1_H */
@@ -242,6 +242,17 @@ int esp32s2_bringup(void)
}
#endif
#ifdef CONFIG_ESP32S2_TWAI
/* Initialize TWAI and register the TWAI driver. */
ret = board_twai_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_twai_setup failed: %d\n", ret);
}
#endif
#ifdef CONFIG_SENSORS_BMP180
/* Try to register BMP180 device in I2C0 */