Skip to content

Commit c002c8d

Browse files
committed
Refactor RTC handling and update logic analyzer pins
Moved RTC-related code from hal.h to a new hal_rtc.h header and updated usage throughout the codebase. Refactored logic analyzer GPIO pin definitions for clarity and consistency. Enabled DMA interrupt in memcpy32, fixed minor error message formatting, and updated RDMHandler to use SetAutoIp and the new RTC interface.
1 parent 25d3cf5 commit c002c8d

File tree

13 files changed

+185
-131
lines changed

13 files changed

+185
-131
lines changed

firmware-template-gd32/lib/Rules.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LD = $(PREFIX)ld
1010
AR = $(PREFIX)ar
1111

1212
BOARD?=BOARD_GD32F303RC
13-
ENET_PHY?=
1413
MCU?=GD32F303RC
1514

1615
$(info $$BOARD [${BOARD}])

lib-clib/src/malloc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ extern "C"
150150

151151
if (next > block_limit)
152152
{
153-
console::Error("malloc: out of memory\n");
153+
console::Error("malloc: out of memory");
154154
#ifdef DEBUG_HEAP
155155
debug_heap();
156156
#endif

lib-gd32/include/board/logic_analyzer.h

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file logic_analyzer.h
33
*
44
*/
5-
/* Copyright (C) 2022-2024 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2022-2025 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -26,31 +26,38 @@
2626
#ifndef BOARD_LOGIC_ANALYZER_H_
2727
#define BOARD_LOGIC_ANALYZER_H_
2828

29-
#define LOGIC_ANALYZER_CH0_GPIO_PINx GPIO_PIN_15 // PB15
30-
#define LOGIC_ANALYZER_CH1_GPIO_PINx GPIO_PIN_11 // PA13
31-
#define LOGIC_ANALYZER_CH2_GPIO_PINx GPIO_PIN_15 // PA11
32-
//#define LOGIC_ANALYZER_CH3_GPIO_PINx GPIO_PIN_11 // PA11
33-
//#define LOGIC_ANALYZER_CH4_GPIO_PINx GPIO_PIN_9
29+
// Do not use GPIOA_6, GPIOB_15
30+
31+
#define LOGIC_ANALYZER_CH0_GPIO_PINx GPIO_PIN_11
32+
#define LOGIC_ANALYZER_CH0_GPIOx GPIOA
33+
#define LOGIC_ANALYZER_CH0_RCU_GPIOx RCU_GPIOA
34+
35+
#define LOGIC_ANALYZER_CH1_GPIO_PINx GPIO_PIN_4
36+
#define LOGIC_ANALYZER_CH1_GPIOx GPIOA
37+
#define LOGIC_ANALYZER_CH1_RCU_GPIOx RCU_GPIOA
38+
39+
#define LOGIC_ANALYZER_CH2_GPIO_PINx GPIO_PIN_5
40+
#define LOGIC_ANALYZER_CH2_GPIOx GPIOA
41+
#define LOGIC_ANALYZER_CH2_RCU_GPIOx RCU_GPIOA
42+
43+
#define LOGIC_ANALYZER_CH3_GPIO_PINx GPIO_PIN_13
44+
#define LOGIC_ANALYZER_CH3_GPIOx GPIOA
45+
#define LOGIC_ANALYZER_CH3_RCU_GPIOx RCU_GPIOA
46+
47+
#define LOGIC_ANALYZER_CH4_GPIO_PINx GPIO_PIN_15
48+
#define LOGIC_ANALYZER_CH4_GPIOx GPIOA
49+
#define LOGIC_ANALYZER_CH4_RCU_GPIOx RCU_GPIOA
50+
3451
//#define LOGIC_ANALYZER_CH5_GPIO_PINx GPIO_PIN_13
52+
#define LOGIC_ANALYZER_CH5_GPIOx GPIOC
53+
#define LOGIC_ANALYZER_CH5_RCU_GPIOx RCU_GPIOC
54+
3555
//#define LOGIC_ANALYZER_CH6_GPIO_PINx GPIO_PIN_11
36-
//#define LOGIC_ANALYZER_CH7_GPIO_PINx GPIO_PIN_15
56+
#define LOGIC_ANALYZER_CH6_GPIOx GPIOA
57+
#define LOGIC_ANALYZER_CH6_RCU_GPIOx RCU_GPIOA
3758

38-
#define LOGIC_ANALYZER_CH0_GPIOx GPIOB
39-
#define LOGIC_ANALYZER_CH1_GPIOx GPIOA
40-
#define LOGIC_ANALYZER_CH2_GPIOx GPIOA
41-
#define LOGIC_ANALYZER_CH3_GPIOx GPIOA
42-
#define LOGIC_ANALYZER_CH4_GPIOx GPIOC
43-
#define LOGIC_ANALYZER_CH5_GPIOx GPIOC
44-
#define LOGIC_ANALYZER_CH6_GPIOx GPIOA
45-
#define LOGIC_ANALYZER_CH7_GPIOx GPIOA
46-
47-
#define LOGIC_ANALYZER_CH0_RCU_GPIOx RCU_GPIOA
48-
#define LOGIC_ANALYZER_CH1_RCU_GPIOx RCU_GPIOB
49-
#define LOGIC_ANALYZER_CH2_RCU_GPIOx RCU_GPIOB
50-
#define LOGIC_ANALYZER_CH3_RCU_GPIOx RCU_GPIOA
51-
#define LOGIC_ANALYZER_CH4_RCU_GPIOx RCU_GPIOC
52-
#define LOGIC_ANALYZER_CH5_RCU_GPIOx RCU_GPIOC
53-
#define LOGIC_ANALYZER_CH6_RCU_GPIOx RCU_GPIOA
59+
//#define LOGIC_ANALYZER_CH7_GPIO_PINx GPIO_PIN_15
60+
#define LOGIC_ANALYZER_CH7_GPIOx GPIOA
5461
#define LOGIC_ANALYZER_CH7_RCU_GPIOx RCU_GPIOA
5562

56-
#endif /* BOARD_LOGIC_ANALYZER_H_ */
63+
#endif // BOARD_LOGIC_ANALYZER_H_

lib-gd32/include/gd32_dma_memcpy32.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ inline void StartDma(const void* destination, const void* source, uint32_t lengt
4949
DMA_CHMADDR(DMA0, DMA_CH3) = reinterpret_cast<uint32_t>(destination);
5050
DMA_CHCNT(DMA0, DMA_CH3) = (length & DMA_CHXCNT_CNT);
5151

52-
dma_chctl |= DMA_CHXCTL_CHEN;
52+
dma_chctl |= (DMA_CHXCTL_CHEN | DMA_INT_FTF);
5353
DMA_CHCTL(DMA0, DMA_CH3) = dma_chctl;
5454
#else
5555
uint32_t dma_chctl = DMA_CHCTL(DMA1, DMA_CH0);
@@ -62,7 +62,7 @@ inline void StartDma(const void* destination, const void* source, uint32_t lengt
6262
DMA_CHPADDR(DMA1, DMA_CH0) = reinterpret_cast<uint32_t>(source);
6363
DMA_CHCNT(DMA1, DMA_CH0) = length;
6464

65-
dma_chctl |= DMA_CHXCTL_CHEN;
65+
dma_chctl |= (DMA_CHXCTL_CHEN | DMA_INT_FTF);
6666
DMA_CHCTL(DMA1, DMA_CH0) = dma_chctl;
6767
#endif
6868
}

lib-gd32/src/f/gd32_dma_memcpy32.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void Init()
5252
Gd32DmaInterruptFlagClear<DMA0, DMA_CH3, (DMA_INT_FLAG_FTF | DMA_INT_FLAG_G)>();
5353

5454
NVIC_SetPriority(DMA0_Channel3_IRQn, 0);
55-
// NVIC_EnableIRQ(DMA0_Channel3_IRQn);
55+
NVIC_EnableIRQ(DMA0_Channel3_IRQn);
5656
#else
5757
rcu_periph_clock_enable(RCU_DMA1);
5858
dma_deinit(DMA1, DMA_CH0);
@@ -76,7 +76,7 @@ void Init()
7676
Gd32DmaInterruptFlagClear<DMA1, DMA_CH3, (DMA_INT_FLAG_FTF | DMA_INT_FLAG_TAE)>();
7777

7878
NVIC_SetPriority(DMA1_Channel0_IRQn, 0);
79-
// NVIC_EnableIRQ(DMA1_Channel0_IRQn);
79+
NVIC_EnableIRQ(DMA1_Channel0_IRQn);
8080
#endif
8181
}
8282
} // namespace dma::memcpy32

lib-gd32/src/f/gd32_spi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ static void GpioConfig()
7575
#if defined(GPIO_INIT)
7676
#if defined(SPI_REMAP_GPIO)
7777
gpio_pin_remap_config(SPI_REMAP_GPIO, ENABLE);
78-
if (SPI_PERIPH == SPI0)
78+
if constexpr (SPI_PERIPH == SPI0)
7979
{
8080
gpio_pin_remap_config(GPIO_SWJ_DISABLE_REMAP, ENABLE);
8181
}
8282
#else
83-
if (SPI_PERIPH == SPI2)
83+
if constexpr (SPI_PERIPH == SPI2)
8484
{
8585
gpio_pin_remap_config(GPIO_SWJ_DISABLE_REMAP, ENABLE);
8686
}

lib-gd32/src/f/i2s_psc_config_dump.cpp

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -24,92 +24,106 @@
2424
*/
2525

2626
#include <cstdint>
27-
#include <cassert>
2827

2928
#include "gd32.h"
3029

3130
#ifndef NDEBUG
3231
#include <cstdio>
3332
/* SPI/I2S parameter initialization mask */
34-
#define SPI_INIT_MASK ((uint32_t)0x00003040U) /*!< SPI parameter initialization mask */
35-
#define I2S_INIT_MASK ((uint32_t)0x0000F047U) /*!< I2S parameter initialization mask */
33+
#define SPI_INIT_MASK ((uint32_t)0x00003040U) /*!< SPI parameter initialization mask */
34+
#define I2S_INIT_MASK ((uint32_t)0x0000F047U) /*!< I2S parameter initialization mask */
3635

3736
/* I2S clock source selection, multiplication and division mask */
38-
#define I2S1_CLOCK_SEL ((uint32_t)0x00020000U) /* I2S1 clock source selection */
39-
#define I2S2_CLOCK_SEL ((uint32_t)0x00040000U) /* I2S2 clock source selection */
40-
#define I2S_CLOCK_MUL_MASK ((uint32_t)0x0000F000U) /* I2S clock multiplication mask */
41-
#define I2S_CLOCK_DIV_MASK ((uint32_t)0x000000F0U) /* I2S clock division mask */
37+
#define I2S1_CLOCK_SEL ((uint32_t)0x00020000U) /* I2S1 clock source selection */
38+
#define I2S2_CLOCK_SEL ((uint32_t)0x00040000U) /* I2S2 clock source selection */
39+
#define I2S_CLOCK_MUL_MASK ((uint32_t)0x0000F000U) /* I2S clock multiplication mask */
40+
#define I2S_CLOCK_DIV_MASK ((uint32_t)0x000000F0U) /* I2S clock division mask */
4241

4342
/* default value and offset */
44-
#define SPI_I2SPSC_DEFAULT_VALUE ((uint32_t)0x00000002U) /* default value of SPI_I2SPSC register */
45-
#define RCU_CFG1_PREDV1_OFFSET 4U /* PREDV1 offset in RCU_CFG1 */
46-
#define RCU_CFG1_PLL2MF_OFFSET 12U /* PLL2MF offset in RCU_CFG1 */
47-
48-
void I2sPscConfigDump([[maybe_unused]] uint32_t spi_periph, uint32_t audiosample, uint32_t frameformat, uint32_t mckout) {
49-
uint32_t i2sdiv = 2U, i2sof = 0U;
50-
uint32_t clks = 0U;
51-
uint32_t i2sclock = 0U;
52-
53-
#if defined (GD32F10X_CL) || defined (GD32F20X_CL)
54-
/* get the I2S clock source */
55-
if (SPI1 == ((uint32_t) spi_periph)) {
56-
/* I2S1 clock source selection */
57-
clks = I2S1_CLOCK_SEL;
58-
} else {
59-
/* I2S2 clock source selection */
60-
clks = I2S2_CLOCK_SEL;
61-
}
62-
63-
if (0U != (RCU_CFG1 & clks)) {
64-
/* get RCU PLL2 clock multiplication factor */
65-
clks = (uint32_t) ((RCU_CFG1 & I2S_CLOCK_MUL_MASK)
66-
>> RCU_CFG1_PLL2MF_OFFSET);
67-
68-
if ((clks > 5U) && (clks < 15U)) {
69-
/* multiplier is between 8 and 14 */
70-
clks += 2U;
71-
} else {
72-
if (15U == clks) {
73-
/* multiplier is 20 */
74-
clks = 20U;
75-
}
76-
}
77-
78-
/* get the PREDV1 value */
79-
i2sclock = (uint32_t) (((RCU_CFG1 & I2S_CLOCK_DIV_MASK)
80-
>> RCU_CFG1_PREDV1_OFFSET) + 1U);
81-
/* calculate I2S clock based on PLL2 and PREDV1 */
82-
i2sclock = (uint32_t) ((HXTAL_VALUE / i2sclock) * clks * 2U);
83-
} else
43+
#define SPI_I2SPSC_DEFAULT_VALUE ((uint32_t)0x00000002U) /* default value of SPI_I2SPSC register */
44+
#define RCU_CFG1_PREDV1_OFFSET 4U /* PREDV1 offset in RCU_CFG1 */
45+
#define RCU_CFG1_PLL2MF_OFFSET 12U /* PLL2MF offset in RCU_CFG1 */
46+
47+
void I2sPscConfigDump([[maybe_unused]] uint32_t spi_periph, uint32_t audiosample, uint32_t frameformat, uint32_t mckout)
48+
{
49+
uint32_t i2sdiv = 2U, i2sof = 0U;
50+
uint32_t clks = 0U;
51+
uint32_t i2sclock = 0U;
52+
53+
#if defined(GD32F10X_CL) || defined(GD32F20X_CL)
54+
/* get the I2S clock source */
55+
if (SPI1 == spi_periph)
56+
{
57+
/* I2S1 clock source selection */
58+
clks = I2S1_CLOCK_SEL;
59+
}
60+
else
61+
{
62+
/* I2S2 clock source selection */
63+
clks = I2S2_CLOCK_SEL;
64+
}
65+
66+
if (0U != (RCU_CFG1 & clks))
67+
{
68+
/* get RCU PLL2 clock multiplication factor */
69+
clks = ((RCU_CFG1 & I2S_CLOCK_MUL_MASK) >> RCU_CFG1_PLL2MF_OFFSET);
70+
71+
if ((clks > 5U) && (clks < 15U))
72+
{
73+
/* multiplier is between 8 and 14 */
74+
clks += 2U;
75+
}
76+
else
77+
{
78+
if (15U == clks)
79+
{
80+
/* multiplier is 20 */
81+
clks = 20U;
82+
}
83+
}
84+
85+
/* get the PREDV1 value */
86+
i2sclock = static_cast<uint32_t>(((RCU_CFG1 & I2S_CLOCK_DIV_MASK) >> RCU_CFG1_PREDV1_OFFSET) + 1U);
87+
/* calculate I2S clock based on PLL2 and PREDV1 */
88+
i2sclock = static_cast<uint32_t>((HXTAL_VALUE / i2sclock) * clks * 2U);
89+
}
90+
else
8491
#endif
85-
{
86-
/* get system clock */
87-
i2sclock = rcu_clock_freq_get(CK_SYS);
88-
}
89-
90-
/* config the prescaler depending on the mclk output state, the frame format and audio sample rate */
91-
if (I2S_MCKOUT_ENABLE == mckout) {
92-
clks = (uint32_t) (((i2sclock / 256U) * 10U) / audiosample);
93-
} else {
94-
if (I2S_FRAMEFORMAT_DT16B_CH16B == frameformat) {
95-
clks = (uint32_t) (((i2sclock / 32U) * 10U) / audiosample);
96-
} else {
97-
clks = (uint32_t) (((i2sclock / 64U) * 10U) / audiosample);
98-
}
99-
}
100-
101-
/* remove the floating point */
102-
clks = (clks + 5U) / 10U;
103-
i2sof = (clks & 0x00000001U);
104-
i2sdiv = ((clks - i2sof) / 2U);
105-
i2sof = (i2sof << 8U);
106-
107-
/* set the default values */
108-
if ((i2sdiv < 2U) || (i2sdiv > 255U)) {
109-
i2sdiv = 2U;
110-
i2sof = 0U;
111-
}
112-
113-
printf("clks=%u, i2sclock=%u, i2sof=%u, i2sdiv=%u\n", clks, i2sclock, i2sof, i2sdiv);
92+
{
93+
/* get system clock */
94+
i2sclock = rcu_clock_freq_get(CK_SYS);
95+
}
96+
97+
/* config the prescaler depending on the mclk output state, the frame format and audio sample rate */
98+
if (I2S_MCKOUT_ENABLE == mckout)
99+
{
100+
clks = static_cast<uint32_t>(((i2sclock / 256U) * 10U) / audiosample);
101+
}
102+
else
103+
{
104+
if (I2S_FRAMEFORMAT_DT16B_CH16B == frameformat)
105+
{
106+
clks = static_cast<uint32_t>(((i2sclock / 32U) * 10U) / audiosample);
107+
}
108+
else
109+
{
110+
clks = static_cast<uint32_t>(((i2sclock / 64U) * 10U) / audiosample);
111+
}
112+
}
113+
114+
/* remove the floating point */
115+
clks = (clks + 5U) / 10U;
116+
i2sof = (clks & 0x00000001U);
117+
i2sdiv = ((clks - i2sof) / 2U);
118+
i2sof = (i2sof << 8U);
119+
120+
/* set the default values */
121+
if ((i2sdiv < 2U) || (i2sdiv > 255U))
122+
{
123+
i2sdiv = 2U;
124+
i2sof = 0U;
125+
}
126+
127+
printf("clks=%u, i2sclock=%u, i2sof=%u, i2sdiv=%u\n", clks, i2sclock, i2sof, i2sdiv);
114128
}
115129
#endif

lib-hal/include/hal.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @file hal.h
3+
*
34
*/
45
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
56
*
@@ -27,11 +28,6 @@
2728

2829
#include <cstdint>
2930

30-
#if !defined(DISABLE_RTC)
31-
#include <cassert>
32-
#include "hwclock.h"
33-
#endif
34-
3531
namespace hal
3632
{
3733
enum class BootDevice
@@ -53,17 +49,6 @@ uint32_t Uptime();
5349

5450
float CoreTemperatureCurrent();
5551

56-
inline bool SetRtc([[maybe_unused]] const struct tm* rtc_time)
57-
{
58-
#if !defined(DISABLE_RTC)
59-
assert(HwClock::Get() != nullptr);
60-
HwClock::Get()->Set(rtc_time);
61-
return true;
62-
#else
63-
return false;
64-
#endif
65-
}
66-
6752
bool Reboot();
6853
void RebootHandler();
6954
} // namespace hal

0 commit comments

Comments
 (0)