Skip to content

Commit 66d2ecd

Browse files
committed
[bsp][gd32]: can add support for GD32F5xx
1 parent 550d072 commit 66d2ecd

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

bsp/gd32/arm/gd32527I-eval/board/Kconfig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,48 @@ menu "On-chip Peripheral Drivers"
371371
bool "Enable SDRAM"
372372
select RT_USING_SDRAM
373373
default n
374+
375+
menuconfig BSP_USING_CAN
376+
bool "Enable CAN BUS"
377+
default n
378+
select RT_USING_CAN
379+
select RT_USING_DEVICE_IPC
380+
if BSP_USING_CAN
381+
config BSP_USING_CAN0
382+
bool "Enable CAN0"
383+
default n
384+
385+
if BSP_USING_CAN0
386+
choice
387+
prompt "Select CAN0 TX source"
388+
default BSP_CAN0_TX_PH13
389+
390+
config BSP_CAN0_TX_PA12
391+
bool "GPIOA pin 12"
392+
config BSP_CAN0_TX_PB9
393+
bool "GPIOB pin 9"
394+
config BSP_CAN0_TX_PD1
395+
bool "GPIOD pin 1"
396+
config BSP_CAN0_TX_PH13
397+
bool "GPIOH pin 13"
398+
endchoice
399+
400+
choice
401+
prompt "Select CAN0 RX source"
402+
default BSP_CAN0_RX_PI9
403+
404+
config BSP_CAN0_RX_PA11
405+
bool "GPIOA pin 11"
406+
config BSP_CAN0_RX_PB8
407+
bool "GPIOB pin 8"
408+
config BSP_CAN0_RX_PD0
409+
bool "GPIOD pin 0"
410+
config BSP_CAN0_RX_PI9
411+
bool "GPIOI pin 9"
412+
endchoice
413+
endif
414+
415+
endif
374416

375417
source "$(BSP_DIR)/../libraries/gd32_drivers/Kconfig"
376418

bsp/gd32/arm/libraries/gd32_drivers/drv_can.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Change Logs:
99
* Date Author Notes
1010
* 2025-18-03 Dmitriy Chernov first implementation for GD32F4xx
11+
* 2025-09-24 CYFS add support for GD32F5xx
1112
*/
1213

1314
#include "drv_can.h"
@@ -31,7 +32,7 @@ static const struct gd32_baudrate_tbl can_baudrate_tbl[] =
3132
{CAN20kBaud, CAN_BT_SJW_1TQ, CAN_BT_BS1_11TQ, CAN_BT_BS2_2TQ, 150},
3233
{CAN10kBaud, CAN_BT_SJW_1TQ, CAN_BT_BS1_11TQ, CAN_BT_BS2_2TQ, 300},
3334
};
34-
#elif defined(GD32F425) || defined(GD32F427) || defined(GD32F450) /* 50MHz(max) */
35+
#elif defined(GD32F425) || defined(GD32F427) || defined(GD32F450) || defined(GD32F527)/* 50MHz(max) */
3536
static const struct gd32_baudrate_tbl can_baudrate_tbl[] =
3637
{
3738
{CAN1MBaud, CAN_BT_SJW_1TQ, CAN_BT_BS1_8TQ, CAN_BT_BS2_1TQ, 5},
@@ -82,7 +83,10 @@ static const struct gd32_can gd32_can_gpio[] =
8283
#ifdef BSP_USING_CAN0
8384
{
8485
.can_clk = RCU_CAN0,
86+
#if defined SOC_SERIES_GD32F4xx || defined SOC_SERIES_GD32F5xx
8587
.alt_func_num = GPIO_AF_9,
88+
#endif
89+
8690
#if defined BSP_CAN0_TX_PA12
8791
.tx_clk = RCU_GPIOA,
8892
.tx_pin = GET_PIN(A, 12),
@@ -119,7 +123,10 @@ static const struct gd32_can gd32_can_gpio[] =
119123
#ifdef BSP_USING_CAN1
120124
{
121125
.can_clk = RCU_CAN1,
126+
#if defined SOC_SERIES_GD32F4xx || defined SOC_SERIES_GD32F5xx
122127
.alt_func_num = GPIO_AF_9,
128+
#endif
129+
123130
#if defined BSP_CAN1_TX_PB6
124131
.tx_clk = RCU_GPIOB,
125132
.tx_pin = GET_PIN(B, 6),
@@ -150,7 +157,7 @@ static void gd32_can_gpio_init(void)
150157
rcu_periph_clock_enable(gd32_can_gpio[i].tx_clk);
151158
rcu_periph_clock_enable(gd32_can_gpio[i].rx_clk);
152159

153-
#if defined SOC_SERIES_GD32F4xx
160+
#if defined SOC_SERIES_GD32F4xx || defined SOC_SERIES_GD32F5xx
154161
gpio_af_set(PIN_GDPORT(gd32_can_gpio[i].tx_pin), gd32_can_gpio[i].alt_func_num, PIN_GDPIN(gd32_can_gpio[i].tx_pin));
155162
gpio_af_set(PIN_GDPORT(gd32_can_gpio[i].rx_pin), gd32_can_gpio[i].alt_func_num, PIN_GDPIN(gd32_can_gpio[i].rx_pin));
156163

@@ -515,7 +522,8 @@ static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg)
515522
return RT_EOK;
516523
}
517524

518-
static int _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num)
525+
526+
static rt_ssize_t _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num)
519527
{
520528
RT_ASSERT(can);
521529

@@ -605,7 +613,7 @@ static int _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t
605613
return RT_EOK;
606614
}
607615

608-
static int _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo)
616+
static rt_ssize_t _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo)
609617
{
610618
RT_ASSERT(can);
611619

@@ -735,23 +743,23 @@ static void _can_ewmc_isr(struct rt_can_device *can)
735743
can->status.ackerrcnt++;
736744
if (can_interrupt_flag_get(can_x, CAN_INT_FLAG_MTF0))
737745
{
738-
if (!can_interrupt_flag_get(can_x, CAN_FLAG_MTFNERR0))
746+
if (!can_flag_get(can_x, CAN_FLAG_MTFNERR0))
739747
{
740748
rt_hw_can_isr(can, RT_CAN_EVENT_TX_FAIL | 0 << 8);
741749
}
742750
can_interrupt_flag_clear(can_x, CAN_INT_FLAG_MTF0);
743751
}
744752
else if (can_interrupt_flag_get(can_x, CAN_INT_FLAG_MTF1))
745753
{
746-
if (!can_interrupt_flag_get(can_x, CAN_FLAG_MTFNERR1))
754+
if (!can_flag_get(can_x, CAN_FLAG_MTFNERR1))
747755
{
748756
rt_hw_can_isr(can, RT_CAN_EVENT_TX_FAIL | 1 << 8);
749757
}
750758
can_interrupt_flag_clear(can_x, CAN_INT_FLAG_MTF1);
751759
}
752760
else if (can_interrupt_flag_get(can_x, CAN_INT_FLAG_MTF2))
753761
{
754-
if (!can_interrupt_flag_get(can_x, CAN_FLAG_MTFNERR2))
762+
if (!can_flag_get(can_x, CAN_FLAG_MTFNERR2))
755763
{
756764
rt_hw_can_isr(can, RT_CAN_EVENT_TX_FAIL | 2 << 8);
757765
}
@@ -796,7 +804,7 @@ static void _can_tx_isr(struct rt_can_device *can)
796804

797805
if (can_interrupt_flag_get(can_x, CAN_INT_FLAG_MTF0))
798806
{
799-
if (can_interrupt_flag_get(can_x, CAN_FLAG_MTFNERR0))
807+
if (can_flag_get(can_x, CAN_FLAG_MTFNERR0))
800808
{
801809
rt_hw_can_isr(can, RT_CAN_EVENT_TX_DONE | 0 << 8);
802810
}
@@ -809,7 +817,7 @@ static void _can_tx_isr(struct rt_can_device *can)
809817
}
810818
else if (can_interrupt_flag_get(can_x, CAN_INT_FLAG_MTF1))
811819
{
812-
if (can_interrupt_flag_get(can_x, CAN_FLAG_MTFNERR1))
820+
if (can_flag_get(can_x, CAN_FLAG_MTFNERR1))
813821
{
814822
rt_hw_can_isr(can, RT_CAN_EVENT_TX_DONE | 1 << 8);
815823
}
@@ -822,7 +830,7 @@ static void _can_tx_isr(struct rt_can_device *can)
822830
}
823831
else if (can_interrupt_flag_get(can_x, CAN_INT_FLAG_MTF2))
824832
{
825-
if (can_interrupt_flag_get(can_x, CAN_FLAG_MTFNERR2))
833+
if (can_flag_get(can_x, CAN_FLAG_MTFNERR2))
826834
{
827835
rt_hw_can_isr(can, RT_CAN_EVENT_TX_DONE | 2 << 8);
828836
}

bsp/gd32/arm/libraries/gd32_drivers/drv_can.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Change Logs:
99
* Date Author Notes
1010
* 2025-18-03 Dmitriy Chernov first implementation
11+
* 2025-09-24 CYFS add support for GD32F5xx
1112
*/
1213

1314
#ifndef __DRV_CAN_H__
@@ -34,7 +35,7 @@ struct gd32_can
3435
rcu_periph_enum can_clk;
3536
rcu_periph_enum tx_clk;
3637
rcu_periph_enum rx_clk;
37-
#if defined SOC_SERIES_GD32F4xx
38+
#if defined SOC_SERIES_GD32F4xx || defined SOC_SERIES_GD32F5xx
3839
uint32_t alt_func_num;
3940
#endif
4041
rt_base_t tx_pin;

0 commit comments

Comments
 (0)