Skip to content

Commit 9e254e8

Browse files
eren-terziogluxiaoxiang781216
authored andcommitted
boards/risc-v/espressif: Fix twai initialization for esp32[-c6|-p4]
Fix twai initialization when multiple twai devices enabled for esp32c6 and esp32p4 Signed-off-by: Eren Terzioglu <eren.terzioglu@espressif.com>
1 parent 5b94c80 commit 9e254e8

2 files changed

Lines changed: 18 additions & 45 deletions

File tree

boards/risc-v/esp32c6/common/src/esp_board_twai.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <nuttx/config.h>
2828

2929
#include <errno.h>
30+
#include <stdio.h>
3031
#include <nuttx/debug.h>
3132

3233
#include <nuttx/can/can.h>
@@ -40,6 +41,9 @@
4041
* Pre-processor Definitions
4142
****************************************************************************/
4243

44+
#define DEVNAME_FMT "/dev/can%d"
45+
#define DEVNAME_FMTLEN (8 + 3 + 1)
46+
4347
/****************************************************************************
4448
* Public Functions
4549
****************************************************************************/
@@ -63,6 +67,7 @@ int board_twai_setup(int port)
6367
{
6468
#ifdef CONFIG_ESPRESSIF_TWAI
6569
struct can_dev_s *twai;
70+
char devname[DEVNAME_FMTLEN];
6671
int ret;
6772

6873
/* Call esp_twaiinitialize() to get an instance of the TWAI
@@ -72,31 +77,18 @@ int board_twai_setup(int port)
7277
twai = esp_twaiinitialize(port);
7378
if (twai == NULL)
7479
{
75-
canerr("ERROR: Failed to get TWAI interface\n");
80+
canerr("ERROR: Failed to get TWAI interface for port %d\n", port);
7681
return -ENODEV;
7782
}
7883

79-
#ifdef CONFIG_ESPRESSIF_TWAI0
80-
/* Register the TWAI driver at "/dev/can0" */
81-
82-
ret = can_register("/dev/can0", twai);
83-
if (ret < 0)
84-
{
85-
canerr("ERROR: TWAI0 register failed: %d\n", ret);
86-
return ret;
87-
}
88-
#endif /* CONFIG_ESPRESSIF_TWAI0 */
89-
90-
#ifdef CONFIG_ESPRESSIF_TWAI1
91-
/* Register the TWAI driver at "/dev/can1" */
84+
snprintf(devname, sizeof(devname), DEVNAME_FMT, port);
9285

93-
ret = can_register("/dev/can1", twai);
86+
ret = can_register(devname, twai);
9487
if (ret < 0)
9588
{
96-
canerr("ERROR: TWAI1 register failed: %d\n", ret);
89+
canerr("ERROR: TWAI%d register failed: %d\n", port, ret);
9790
return ret;
9891
}
99-
#endif /* CONFIG_ESPRESSIF_TWAI1 */
10092

10193
return OK;
10294
#else

boards/risc-v/esp32p4/common/src/esp_board_twai.c

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <nuttx/config.h>
2828

2929
#include <errno.h>
30+
#include <stdio.h>
3031
#include <nuttx/debug.h>
3132

3233
#include <nuttx/can/can.h>
@@ -40,6 +41,9 @@
4041
* Pre-processor Definitions
4142
****************************************************************************/
4243

44+
#define DEVNAME_FMT "/dev/can%d"
45+
#define DEVNAME_FMTLEN (8 + 3 + 1)
46+
4347
/****************************************************************************
4448
* Public Functions
4549
****************************************************************************/
@@ -63,6 +67,7 @@ int board_twai_setup(int port)
6367
{
6468
#ifdef CONFIG_ESPRESSIF_TWAI
6569
struct can_dev_s *twai;
70+
char devname[DEVNAME_FMTLEN];
6671
int ret;
6772

6873
/* Call esp_twaiinitialize() to get an instance of the TWAI
@@ -72,42 +77,18 @@ int board_twai_setup(int port)
7277
twai = esp_twaiinitialize(port);
7378
if (twai == NULL)
7479
{
75-
canerr("ERROR: Failed to get TWAI interface\n");
80+
canerr("ERROR: Failed to get TWAI interface for port %d\n", port);
7681
return -ENODEV;
7782
}
7883

79-
#ifdef CONFIG_ESPRESSIF_TWAI0
80-
/* Register the TWAI driver at "/dev/can0" */
81-
82-
ret = can_register("/dev/can0", twai);
83-
if (ret < 0)
84-
{
85-
canerr("ERROR: TWAI0 register failed: %d\n", ret);
86-
return ret;
87-
}
88-
#endif /* CONFIG_ESPRESSIF_TWAI0 */
89-
90-
#ifdef CONFIG_ESPRESSIF_TWAI1
91-
/* Register the TWAI driver at "/dev/can1" */
92-
93-
ret = can_register("/dev/can1", twai);
94-
if (ret < 0)
95-
{
96-
canerr("ERROR: TWAI1 register failed: %d\n", ret);
97-
return ret;
98-
}
99-
#endif /* CONFIG_ESPRESSIF_TWAI1 */
100-
101-
#ifdef CONFIG_ESPRESSIF_TWAI2
102-
/* Register the TWAI driver at "/dev/can2" */
84+
snprintf(devname, sizeof(devname), DEVNAME_FMT, port);
10385

104-
ret = can_register("/dev/can2", twai);
86+
ret = can_register(devname, twai);
10587
if (ret < 0)
10688
{
107-
canerr("ERROR: TWAI2 register failed: %d\n", ret);
89+
canerr("ERROR: TWAI%d register failed: %d\n", port, ret);
10890
return ret;
10991
}
110-
#endif /* CONFIG_ESPRESSIF_TWAI2 */
11192

11293
return OK;
11394
#else

0 commit comments

Comments
 (0)