Skip to content

Commit 79d5400

Browse files
STM32: Apply RCC to all examples (#628)
1 parent 0d314bc commit 79d5400

File tree

21 files changed

+334
-327
lines changed

21 files changed

+334
-327
lines changed

examples/stmicro/stm32/src/stm32f1xx/adc.zig

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
22
const microzig = @import("microzig");
33

4-
const RCC = microzig.chip.peripherals.RCC;
54
const stm32 = microzig.hal;
5+
const rcc = stm32.rcc;
66
const timer = microzig.hal.timer.GPTimer.init(.TIM2).into_counter_mode();
77

88
const uart = stm32.uart.UART.init(.USART1);
@@ -26,26 +26,22 @@ pub const microzig_options = microzig.Options{
2626
};
2727

2828
pub fn main() !void {
29-
RCC.APB1ENR.modify(.{
30-
.TIM2EN = 1,
31-
});
32-
RCC.APB2ENR.modify(.{
33-
.AFIOEN = 1,
34-
.USART1EN = 1,
35-
.GPIOAEN = 1,
36-
.ADC1EN = 1,
37-
});
38-
const counter = timer.counter_device(8_000_000);
29+
rcc.enable_clock(.TIM2);
30+
rcc.enable_clock(.AFIO);
31+
rcc.enable_clock(.USART1);
32+
rcc.enable_clock(.GPIOA);
33+
rcc.enable_clock(.ADC1);
34+
const counter = timer.counter_device(rcc.get_clock(.TIM2));
3935
const adc = ADC.init(.ADC1);
4036
var adc_out_buf: [10]u16 = undefined;
4137

4238
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz);
4339
ADC_pin1.set_input_mode(.analog);
4440
ADC_pin2.set_input_mode(.analog);
4541

46-
uart.apply(.{
42+
try uart.apply_runtime(.{
4743
.baud_rate = 115200,
48-
.clock_speed = 8_000_000,
44+
.clock_speed = rcc.get_clock(.USART1),
4945
});
5046

5147
stm32.uart.init_logger(&uart);

examples/stmicro/stm32/src/stm32f1xx/adc_dualmode.zig

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
const std = @import("std");
55
const microzig = @import("microzig");
6-
7-
const RCC = microzig.chip.peripherals.RCC;
86
const DMA = microzig.chip.peripherals.DMA1;
97
const DMA_t = microzig.chip.types.peripherals.bdma_v1;
8+
109
const stm32 = microzig.hal;
10+
const rcc = stm32.rcc;
11+
1112
const timer = microzig.hal.timer.GPTimer.init(.TIM2).into_counter_mode();
1213

1314
const uart = stm32.uart.UART.init(.USART1);
@@ -56,20 +57,15 @@ fn DMA_init(arr_addr: u32, adc_addr: u32) void {
5657
}
5758

5859
pub fn main() !void {
59-
RCC.AHBENR.modify(.{
60-
.DMA1EN = 1,
61-
});
62-
RCC.APB1ENR.modify(.{
63-
.TIM2EN = 1,
64-
});
65-
RCC.APB2ENR.modify(.{
66-
.AFIOEN = 1,
67-
.USART1EN = 1,
68-
.GPIOAEN = 1,
69-
.ADC1EN = 1,
70-
.ADC2EN = 1,
71-
});
72-
const counter = timer.counter_device(8_000_000);
60+
rcc.enable_clock(.DMA1);
61+
rcc.enable_clock(.TIM2);
62+
rcc.enable_clock(.AFIO);
63+
rcc.enable_clock(.USART1);
64+
rcc.enable_clock(.GPIOA);
65+
rcc.enable_clock(.ADC1);
66+
rcc.enable_clock(.ADC2);
67+
68+
const counter = timer.counter_device(rcc.get_clock(.TIM2));
7369
const adc1 = AdvancedADC.init(.ADC1);
7470
const adc2 = AdvancedADC.init(.ADC2);
7571

@@ -81,9 +77,9 @@ pub fn main() !void {
8177
ADC_pin1.set_input_mode(.analog);
8278
ADC_pin2.set_input_mode(.analog);
8379

84-
uart.apply(.{
80+
try uart.apply_runtime(.{
8581
.baud_rate = 115200,
86-
.clock_speed = 8_000_000,
82+
.clock_speed = rcc.get_clock(.USART1),
8783
});
8884

8985
stm32.uart.init_logger(&uart);

examples/stmicro/stm32/src/stm32f1xx/advanced_adc.zig

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
const std = @import("std");
1313
const microzig = @import("microzig");
14-
const interrupt = microzig.interrupt;
1514

16-
const RCC = microzig.chip.peripherals.RCC;
15+
const interrupt = microzig.interrupt;
1716
const DMA = microzig.chip.peripherals.DMA1;
1817
const DMA_t = microzig.chip.types.peripherals.bdma_v1;
18+
1919
const stm32 = microzig.hal;
20+
const rcc = stm32.rcc;
2021
const timer = microzig.hal.timer.GPTimer.init(.TIM2).into_counter_mode();
2122

2223
const uart = stm32.uart.UART.init(.USART1);
@@ -69,20 +70,16 @@ fn DMA_init(arr_addr: u32, adc_addr: u32) void {
6970
}
7071

7172
pub fn main() !void {
72-
RCC.AHBENR.modify(.{
73-
.DMA1EN = 1,
74-
});
75-
RCC.APB1ENR.modify(.{
76-
.TIM2EN = 1,
77-
});
78-
RCC.APB2ENR.modify(.{
79-
.AFIOEN = 1,
80-
.USART1EN = 1,
81-
.GPIOAEN = 1,
82-
.ADC1EN = 1,
83-
});
73+
try rcc.clock_init(.{ .ADCprescaler = .RCC_ADCPCLK2_DIV2 });
74+
75+
rcc.enable_clock(.DMA1);
76+
rcc.enable_clock(.TIM2);
77+
rcc.enable_clock(.AFIO);
78+
rcc.enable_clock(.USART1);
79+
rcc.enable_clock(.GPIOA);
80+
rcc.enable_clock(.ADC1);
8481

85-
const counter = timer.counter_device(8_000_000);
82+
const counter = timer.counter_device(rcc.get_clock(.TIM2));
8683

8784
const adc_data_addr: u32 = @intFromPtr(&adc.regs.DR);
8885
var adc_buf: [10]u16 = .{0} ** 10;
@@ -92,9 +89,9 @@ pub fn main() !void {
9289
//configure UART log
9390

9491
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz);
95-
uart.apply(.{
92+
try uart.apply_runtime(.{
9693
.baud_rate = 115200,
97-
.clock_speed = 8_000_000,
94+
.clock_speed = rcc.get_clock(.USART1),
9895
});
9996
stm32.uart.init_logger(&uart);
10097

examples/stmicro/stm32/src/stm32f1xx/gpio.zig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
const std = @import("std");
22
const microzig = @import("microzig");
33

4-
const RCC = microzig.chip.peripherals.RCC;
54
const stm32 = microzig.hal;
5+
const rcc = stm32.rcc;
66
const gpio = stm32.gpio;
77
const timer = stm32.timer.GPTimer.init(.TIM2).into_counter_mode();
88

99
pub fn main() !void {
10-
RCC.APB2ENR.modify(.{
11-
.GPIOCEN = 1,
12-
});
13-
14-
RCC.APB1ENR.modify(.{
15-
.TIM2EN = 1,
16-
});
17-
10+
rcc.enable_clock(.GPIOC);
11+
rcc.enable_clock(.TIM2);
1812
const led = gpio.Pin.from_port(.C, 13);
19-
const counter = timer.counter_device(8_000_000);
13+
const counter = timer.counter_device(rcc.get_clock(.TIM2));
2014

2115
led.set_output_mode(.general_purpose_push_pull, .max_50MHz);
2216
while (true) {

examples/stmicro/stm32/src/stm32f1xx/hd44780.zig

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
22
const microzig = @import("microzig");
33

4-
const RCC = microzig.chip.peripherals.RCC;
54
const stm32 = microzig.hal;
5+
const rcc = stm32.rcc;
66
const gpio = stm32.gpio;
77

88
const drivers = microzig.drivers;
@@ -16,9 +16,6 @@ const timer = stm32.timer.GPTimer.init(.TIM2).into_counter_mode();
1616
const I2c = stm32.i2c;
1717
const I2C_Device = stm32.drivers.I2C_Device;
1818

19-
const uart = stm32.uart.UART.init(.USART1);
20-
const TX = gpio.Pin.from_port(.A, 9);
21-
2219
const i2c = I2c.I2C.init(.I2C2);
2320
const SCL = gpio.Pin.from_port(.B, 10);
2421
const SDA = gpio.Pin.from_port(.B, 11);
@@ -28,10 +25,6 @@ const config = I2c.Config{
2825
.mode = .standard,
2926
};
3027

31-
pub const microzig_options = microzig.Options{
32-
.logFn = stm32.uart.log,
33-
};
34-
3528
var global_counter: stm32.drivers.CounterDevice = undefined;
3629

3730
const i2c_device = I2C_Device.init(i2c, I2c.Address.new(0x27), config, null, null);
@@ -40,19 +33,11 @@ pub fn delay_us(time_delay: u32) void {
4033
global_counter.sleep_us(time_delay);
4134
}
4235
pub fn main() !void {
43-
RCC.APB2ENR.modify(.{
44-
.GPIOBEN = 1,
45-
.GPIOAEN = 1,
46-
.AFIOEN = 1,
47-
.USART1EN = 1,
48-
});
49-
50-
RCC.APB1ENR.modify(.{
51-
.I2C2EN = 1,
52-
.TIM2EN = 1,
53-
});
54-
55-
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz);
36+
rcc.enable_clock(.GPIOB);
37+
rcc.enable_clock(.GPIOC);
38+
rcc.enable_clock(.USART1);
39+
rcc.enable_clock(.I2C2);
40+
rcc.enable_clock(.TIM2);
5641

5742
//Set internal Pull-ups (not recommended for real applications)
5843
SCL.set_input_mode(.pull);
@@ -69,12 +54,6 @@ pub fn main() !void {
6954

7055
i2c.apply(config);
7156

72-
uart.apply(.{
73-
.baud_rate = 115200,
74-
.clock_speed = 8_000_000,
75-
});
76-
77-
stm32.uart.init_logger(&uart);
7857
var expander = PCF8574(.{}).init(i2c_device.datagram_device());
7958
const pins_config = lcd(.{}).pins_struct{
8059
.high_pins = .{

examples/stmicro/stm32/src/stm32f1xx/i2c.zig

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
22
const microzig = @import("microzig");
33

4-
const RCC = microzig.chip.peripherals.RCC;
54
const stm32 = microzig.hal;
5+
const rcc = stm32.rcc;
66
const gpio = stm32.gpio;
77

88
const timer = stm32.timer.GPTimer.init(.TIM2).into_counter_mode();
@@ -27,19 +27,13 @@ pub const microzig_options = microzig.Options{
2727
};
2828

2929
pub fn main() !void {
30-
RCC.APB2ENR.modify(.{
31-
.GPIOBEN = 1,
32-
.GPIOAEN = 1,
33-
.AFIOEN = 1,
34-
.USART1EN = 1,
35-
});
36-
37-
RCC.APB1ENR.modify(.{
38-
.I2C2EN = 1,
39-
.TIM2EN = 1,
40-
});
30+
rcc.enable_clock(.GPIOB);
31+
rcc.enable_clock(.GPIOA);
32+
rcc.enable_clock(.USART1);
33+
rcc.enable_clock(.I2C2);
34+
rcc.enable_clock(.TIM2);
4135

42-
const counter = timer.counter_device(8_000_000);
36+
const counter = timer.counter_device(rcc.get_clock(.TIM2));
4337

4438
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz);
4539

@@ -55,9 +49,9 @@ pub fn main() !void {
5549

5650
i2c2.apply(config);
5751

58-
uart.apply(.{
52+
try uart.apply_runtime(.{
5953
.baud_rate = 115200,
60-
.clock_speed = 8_000_000,
54+
.clock_speed = rcc.get_clock(.USART1),
6155
});
6256

6357
stm32.uart.init_logger(&uart);

examples/stmicro/stm32/src/stm32f1xx/i2c_bus_scan.zig

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const microzig = @import("microzig");
33

44
const RCC = microzig.chip.peripherals.RCC;
55
const stm32 = microzig.hal;
6+
const rcc = stm32.rcc;
67
const gpio = stm32.gpio;
78

89
const timer = stm32.timer.GPTimer.init(.TIM2).into_counter_mode();
@@ -26,19 +27,11 @@ pub const microzig_options = microzig.Options{
2627
};
2728

2829
pub fn main() !void {
29-
RCC.APB2ENR.modify(.{
30-
.GPIOBEN = 1,
31-
.GPIOAEN = 1,
32-
.AFIOEN = 1,
33-
.USART1EN = 1,
34-
});
35-
36-
RCC.APB1ENR.modify(.{
37-
.I2C2EN = 1,
38-
.TIM2EN = 1,
39-
});
40-
41-
const counter = timer.counter_device(8_000_000);
30+
rcc.enable_clock(.GPIOB);
31+
rcc.enable_clock(.GPIOA);
32+
rcc.enable_clock(.USART1);
33+
rcc.enable_clock(.I2C2);
34+
rcc.enable_clock(.TIM2);
4235

4336
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz);
4437

@@ -52,11 +45,13 @@ pub fn main() !void {
5245
SCL.set_output_mode(.alternate_function_open_drain, .max_50MHz);
5346
SDA.set_output_mode(.alternate_function_open_drain, .max_50MHz);
5447

48+
const counter = timer.counter_device(rcc.get_clock(.TIM2));
49+
5550
i2c.apply(config);
5651

57-
uart.apply(.{
52+
try uart.apply_runtime(.{
5853
.baud_rate = 115200,
59-
.clock_speed = 8_000_000,
54+
.clock_speed = rcc.get_clock(.USART1),
6055
});
6156

6257
stm32.uart.init_logger(&uart);

0 commit comments

Comments
 (0)