Skip to content

Commit cec6800

Browse files
authored
ch32v: Improve clock configuration (ZigEmbeddedGroup#767)
1 parent 514da17 commit cec6800

File tree

13 files changed

+328
-53
lines changed

13 files changed

+328
-53
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// CH32V003F4P6_MINI
22
// CH32V003
33
pub const chip = @import("chip");
4+
pub const microzig = @import("microzig");
5+
const ch32v = microzig.hal;
46

5-
pub const cpu_frequency = 24_000_000; // 24 MHz
7+
/// Clock configuration for this board
8+
/// CH32V003 runs at 24 MHz when using HSI with PLL
9+
pub const clock_config: ch32v.clocks.Config = .{
10+
.source = .hsi,
11+
.target_frequency = 24_000_000,
12+
};
13+
14+
/// CPU frequency is derived from clock config
15+
pub const cpu_frequency = clock_config.target_frequency;

port/wch/ch32v/src/boards/CH32V103-R1-1v1.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ pub const chip = @import("chip");
44
pub const microzig = @import("microzig");
55
const ch32v = microzig.hal;
66

7-
pub const cpu_frequency = 48_000_000; // 48 MHz
7+
/// Clock configuration for this board
8+
pub const clock_config: ch32v.clocks.Config = .{
9+
.source = .hsi,
10+
.target_frequency = 48_000_000,
11+
};
12+
13+
/// CPU frequency is derived from clock config
14+
pub const cpu_frequency = clock_config.target_frequency;
815

916
/// Board-specific init: set 48 MHz clock, enable SysTick time
1017
pub fn init() void {
11-
ch32v.clocks.init_48mhz_hsi();
18+
ch32v.clocks.init(clock_config);
1219
ch32v.time.init();
1320
}

port/wch/ch32v/src/boards/CH32V307V-R1-1v0.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ pub const microzig = @import("microzig");
44
pub const chip = @import("chip");
55
const ch32v = microzig.hal;
66

7-
pub const cpu_frequency = 48_000_000; // 48 MHz
7+
/// Clock configuration for this board
8+
pub const clock_config: ch32v.clocks.Config = .{
9+
.source = .hsi,
10+
.target_frequency = 48_000_000,
11+
};
12+
13+
/// CPU frequency is derived from clock config
14+
pub const cpu_frequency = clock_config.target_frequency;
815

916
/// Board-specific init: set 48 MHz clock, enable SysTick time
1017
pub fn init() void {
11-
ch32v.clocks.init_48mhz_hsi();
18+
ch32v.clocks.init(clock_config);
1219
ch32v.time.init();
1320
}

port/wch/ch32v/src/boards/CH32Vx03C-R0-1v0.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ pub const microzig = @import("microzig");
44
pub const chip = @import("chip");
55
const ch32v = microzig.hal;
66

7-
pub const cpu_frequency = 48_000_000; // 48 MHz
7+
/// Clock configuration for this board
8+
pub const clock_config: ch32v.clocks.Config = .{
9+
.source = .hsi,
10+
.target_frequency = 48_000_000,
11+
};
12+
13+
/// CPU frequency is derived from clock config
14+
pub const cpu_frequency = clock_config.target_frequency;
815

916
/// Board-specific init: set 48 MHz clock, enable SysTick time
1017
pub fn init() void {
11-
ch32v.clocks.init_48mhz_hsi();
18+
ch32v.clocks.init(clock_config);
1219
ch32v.time.init();
1320
}

port/wch/ch32v/src/boards/LANA_TNY.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ pub const microzig = @import("microzig");
55
pub const chip = @import("chip");
66
const ch32v = microzig.hal;
77

8-
pub const cpu_frequency = 48_000_000; // 48 MHz
8+
/// Clock configuration for this board
9+
pub const clock_config: ch32v.clocks.Config = .{
10+
.source = .hsi,
11+
.target_frequency = 48_000_000,
12+
};
13+
14+
/// CPU frequency is derived from clock config
15+
pub const cpu_frequency = clock_config.target_frequency;
916

1017
/// Board-specific init: set 48 MHz clock, enable SysTick time
1118
pub fn init() void {
12-
ch32v.clocks.init_48mhz_hsi();
19+
ch32v.clocks.init(clock_config);
1320
ch32v.time.init();
1421
}
1522

port/wch/ch32v/src/boards/Suzuduino_Uno_V1b.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ pub const microzig = @import("microzig");
55
pub const chip = @import("chip");
66
const ch32v = microzig.hal;
77

8-
pub const cpu_frequency = 48_000_000; // 48 MHz
8+
/// Clock configuration for this board
9+
pub const clock_config: ch32v.clocks.Config = .{
10+
.source = .hsi,
11+
.target_frequency = 48_000_000,
12+
};
13+
14+
/// CPU frequency is derived from clock config
15+
pub const cpu_frequency = clock_config.target_frequency;
916

1017
pub const pin_config = ch32v.pins.GlobalConfiguration{
1118
.GPIOA = .{
@@ -18,6 +25,6 @@ pub const pin_config = ch32v.pins.GlobalConfiguration{
1825

1926
/// Board-specific init: set 48 MHz clock, enable SysTick time
2027
pub fn init() void {
21-
ch32v.clocks.init_48mhz_hsi();
28+
ch32v.clocks.init(clock_config);
2229
ch32v.time.init();
2330
}

port/wch/ch32v/src/boards/nanoCH32V305.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ pub const chip = @import("chip");
55
pub const microzig = @import("microzig");
66
const ch32v = microzig.hal;
77

8-
pub const cpu_frequency = 48_000_000; // 48 MHz
8+
/// Clock configuration for this board
9+
pub const clock_config: ch32v.clocks.Config = .{
10+
.source = .hsi,
11+
.target_frequency = 48_000_000,
12+
};
13+
14+
/// CPU frequency is derived from clock config
15+
pub const cpu_frequency = clock_config.target_frequency;
916

1017
pub const pin_config = ch32v.pins.GlobalConfiguration{
1118
.GPIOA = .{
@@ -18,6 +25,6 @@ pub const pin_config = ch32v.pins.GlobalConfiguration{
1825

1926
/// Board-specific init: set 48 MHz clock, enable SysTick time
2027
pub fn init() void {
21-
ch32v.clocks.init_48mhz_hsi();
28+
ch32v.clocks.init(clock_config);
2229
ch32v.time.init();
2330
}

port/wch/ch32v/src/hals/ch32v003.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ pub const peripherals = microzig.chip.peripherals;
33

44
pub const pins = @import("ch32v003/pins.zig");
55
pub const gpio = @import("ch32v003/gpio.zig");
6+
pub const clocks = @import("clocks.zig");
67
pub const time = @import("time.zig");
78

9+
/// HSI (High Speed Internal) oscillator frequency
10+
/// This is the fixed internal RC oscillator frequency for CH32V003
11+
pub const hsi_frequency: u32 = 8_000_000; // 8 MHz
12+
813
// pub fn init() void {}
914

1015
const RCC = peripherals.RCC;

port/wch/ch32v/src/hals/ch32v103.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ pub const clocks = @import("clocks.zig");
55
pub const time = @import("time.zig");
66
pub const usart = @import("usart.zig");
77

8+
/// HSI (High Speed Internal) oscillator frequency
9+
/// This is the fixed internal RC oscillator frequency for CH32V103
10+
pub const hsi_frequency: u32 = 8_000_000; // 8 MHz
11+
812
/// Initialize HAL subsystems used by default
913
/// CH32V103: set clock to 48 MHz via HSI PLL; SysTick driver differs on 103, so time is not enabled here.
1014
pub fn init() void {
11-
clocks.init_48mhz_hsi();
15+
clocks.init(.{
16+
.source = .hsi,
17+
.target_frequency = 48_000_000,
18+
});
1219
}

port/wch/ch32v/src/hals/ch32v20x.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ pub const clocks = @import("clocks.zig");
66
pub const time = @import("time.zig");
77
pub const usart = @import("usart.zig");
88

9+
/// HSI (High Speed Internal) oscillator frequency
10+
/// This is the fixed internal RC oscillator frequency for CH32V20x
11+
pub const hsi_frequency: u32 = 8_000_000; // 8 MHz
12+
913
pub const default_interrupts: microzig.cpu.InterruptOptions = .{
1014
// Default TIM2 handler provided by the HAL for 1ms timekeeping
1115
.TIM2 = time.tim2_handler,

0 commit comments

Comments
 (0)