-
Notifications
You must be signed in to change notification settings - Fork 122
basic f1 HAL #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
RecursiveError
wants to merge
26
commits into
ZigEmbeddedGroup:main
Choose a base branch
from
RecursiveError:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
basic f1 HAL #431
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ddecc0a
add basic gpio
RecursiveError b4c9cd2
basic f1 hal
RecursiveError bd3fe66
update uart
RecursiveError 13554d4
fix uart errors
RecursiveError d6b9a8f
add config check
RecursiveError 27ef3ae
rename examples and small bug fixes
RecursiveError 79638f1
ADD i2c
RecursiveError a3469b3
add i2c example....
RecursiveError e9586fc
update i2c
RecursiveError aedd3a8
fix i2c hal typos
RecursiveError 9f2d14e
add uart log
RecursiveError 7abf77d
update examples
RecursiveError 35da98f
formatting changes in i2c
RecursiveError e6b9f56
change delay time in i2c example
RecursiveError 2f19ced
fix typo
RecursiveError 349d921
more typo fix
RecursiveError fafc456
Merge branch 'main' into main
RecursiveError 5d146ff
fix typo
RecursiveError 1c44105
add basic timer and update blink
RecursiveError 4f1e6ab
create time HAL and update examples
RecursiveError bf732da
remove TODO
RecursiveError e3d4035
update i2c
RecursiveError fcaa07e
fix i2c runtime
RecursiveError 5f0212b
Add I2C_Device to drivers
RecursiveError f441619
add examples
RecursiveError 9001205
Merge branch 'ZigEmbeddedGroup:main' into main
RecursiveError File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const std = @import("std"); | ||
const microzig = @import("microzig"); | ||
|
||
const RCC = microzig.chip.peripherals.RCC; | ||
const stm32 = microzig.hal; | ||
const gpio = stm32.gpio; | ||
const timer = stm32.timer.GPTimer.TIM2; | ||
|
||
pub fn main() !void { | ||
RCC.APB2ENR.modify(.{ | ||
.GPIOCEN = 1, | ||
}); | ||
|
||
RCC.APB1ENR.modify(.{ | ||
.TIM2EN = 1, | ||
}); | ||
|
||
const led = gpio.Pin.from_port(.C, 13); | ||
const counter = timer.into_counter(8_000_000); | ||
|
||
led.set_output_mode(.general_purpose_push_pull, .max_50MHz); | ||
while (true) { | ||
counter.sleep_ms(100); | ||
led.toggle(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const std = @import("std"); | ||
const microzig = @import("microzig"); | ||
|
||
const RCC = microzig.chip.peripherals.RCC; | ||
const stm32 = microzig.hal; | ||
const gpio = stm32.gpio; | ||
|
||
const drivers = microzig.drivers; | ||
const lcd_driver = drivers.display.hd44780; | ||
const lcd = drivers.display.HD44780; | ||
const PCF8574 = drivers.IO_expander.PCF8574; | ||
const State = drivers.base.Digital_IO.State; | ||
|
||
const timer = stm32.timer.GPTimer.TIM2; | ||
|
||
const I2c = stm32.i2c; | ||
const I2C_Device = stm32.drivers.I2C_Device; | ||
|
||
const uart: stm32.uart.UART = @enumFromInt(0); | ||
const TX = gpio.Pin.from_port(.A, 9); | ||
|
||
const i2c: I2c.I2C = @enumFromInt(1); | ||
const SCL = gpio.Pin.from_port(.B, 10); | ||
const SDA = gpio.Pin.from_port(.B, 11); | ||
const config = I2c.Config{ | ||
.pclk = 8_000_000, | ||
.speed = 100_000, | ||
.mode = .standard, | ||
}; | ||
|
||
pub const microzig_options = microzig.Options{ | ||
.logFn = stm32.uart.logFn, | ||
}; | ||
|
||
var global_counter: stm32.drivers.CounterDevice = undefined; | ||
|
||
const i2c_device = I2C_Device.init(i2c, I2c.Address.new(0x27), config, &global_counter, null); | ||
|
||
pub fn delay_us(time_delay: u32) void { | ||
global_counter.sleep_us(time_delay); | ||
} | ||
pub fn main() !void { | ||
RCC.APB2ENR.modify(.{ | ||
.GPIOBEN = 1, | ||
.GPIOAEN = 1, | ||
.AFIOEN = 1, | ||
.USART1EN = 1, | ||
}); | ||
|
||
RCC.APB1ENR.modify(.{ | ||
.I2C2EN = 1, | ||
.TIM2EN = 1, | ||
}); | ||
|
||
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz); | ||
|
||
//Set internal Pull-ups (not recommended for real applications) | ||
SCL.set_input_mode(.pull); | ||
SDA.set_input_mode(.pull); | ||
SCL.set_pull(.up); | ||
SDA.set_pull(.up); | ||
|
||
//Set SCL and SDA to alternate function open drain | ||
SCL.set_output_mode(.alternate_function_open_drain, .max_50MHz); | ||
SDA.set_output_mode(.alternate_function_open_drain, .max_50MHz); | ||
|
||
const counter = timer.into_counter(8_000_000); | ||
global_counter = counter; | ||
|
||
i2c.apply(config); | ||
|
||
uart.apply(.{ | ||
.baud_rate = 115200, | ||
.clock_speed = 8_000_000, | ||
}); | ||
|
||
stm32.uart.init_logger(uart); | ||
var expander = PCF8574(.{}).init(i2c_device.datagram_device()); | ||
const pins_config = lcd(.{}).pins_struct{ | ||
.high_pins = .{ | ||
expander.digital_IO(4), | ||
expander.digital_IO(5), | ||
expander.digital_IO(6), | ||
expander.digital_IO(7), | ||
}, | ||
.BK = expander.digital_IO(3), | ||
.RS = expander.digital_IO(0), | ||
.EN1 = expander.digital_IO(2), | ||
}; | ||
var my_lcd = lcd(.{}).init( | ||
pins_config, | ||
delay_us, | ||
); | ||
|
||
try my_lcd.init_device(.{}); | ||
try my_lcd.set_backlight(1); | ||
try my_lcd.write("hello world - From Zig"); | ||
try my_lcd.set_cursor(1, 0); | ||
try my_lcd.write("STM32F103 - I2C LCD"); | ||
|
||
while (true) { | ||
try my_lcd.shift_display_left(); | ||
global_counter.sleep_ms(300); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const std = @import("std"); | ||
const microzig = @import("microzig"); | ||
|
||
const RCC = microzig.chip.peripherals.RCC; | ||
const stm32 = microzig.hal; | ||
const gpio = stm32.gpio; | ||
|
||
const timer = stm32.timer.GPTimer.TIM2; | ||
|
||
const i2c = stm32.i2c; | ||
|
||
const uart: stm32.uart.UART = @enumFromInt(0); | ||
const TX = gpio.Pin.from_port(.A, 9); | ||
|
||
const i2c2: i2c.I2C = @enumFromInt(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could add a |
||
const SCL = gpio.Pin.from_port(.B, 10); | ||
const SDA = gpio.Pin.from_port(.B, 11); | ||
const ADDR = i2c.Address.new(0x27); | ||
const config = i2c.Config{ | ||
.pclk = 8_000_000, | ||
.speed = 100_000, | ||
.mode = .standard, | ||
}; | ||
|
||
pub const microzig_options = microzig.Options{ | ||
.logFn = stm32.uart.logFn, | ||
}; | ||
|
||
pub fn main() !void { | ||
RCC.APB2ENR.modify(.{ | ||
.GPIOBEN = 1, | ||
.GPIOAEN = 1, | ||
.AFIOEN = 1, | ||
.USART1EN = 1, | ||
}); | ||
|
||
RCC.APB1ENR.modify(.{ | ||
.I2C2EN = 1, | ||
.TIM2EN = 1, | ||
}); | ||
|
||
const counter = timer.into_counter(8_000_000); | ||
|
||
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz); | ||
|
||
//Set internal Pull-ups (not recommended for real applications) | ||
SCL.set_input_mode(.pull); | ||
SDA.set_input_mode(.pull); | ||
SCL.set_pull(.up); | ||
SDA.set_pull(.up); | ||
|
||
//Set SCL and SDA to alternate function open drain | ||
SCL.set_output_mode(.alternate_function_open_drain, .max_50MHz); | ||
SDA.set_output_mode(.alternate_function_open_drain, .max_50MHz); | ||
|
||
i2c2.apply(config); | ||
|
||
uart.apply(.{ | ||
.baud_rate = 115200, | ||
.clock_speed = 8_000_000, | ||
}); | ||
|
||
stm32.uart.init_logger(uart); | ||
|
||
var to_read: [1]u8 = undefined; | ||
std.log.info("start I2C master", .{}); | ||
while (true) { | ||
for (0..0xFF) |val| { | ||
RecursiveError marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std.log.info("sending {d}", .{val}); | ||
i2c2.write_blocking(ADDR, &.{@intCast(val)}, counter.make_ms_timeout(5000)) catch |err| { | ||
std.log.err("send to send data | error {any}", .{err}); | ||
if (err == error.UnrecoverableError) { | ||
//Reset I2C peripheral | ||
i2c2.apply(config); | ||
} else { | ||
i2c2.clear_errors(); | ||
} | ||
continue; | ||
}; | ||
|
||
std.log.info("receiving data from the slave", .{}); | ||
i2c2.read_blocking(ADDR, &to_read, counter.make_ms_timeout(1000)) catch |err| { | ||
std.log.err("fail to read data | error {any}", .{err}); | ||
if (err == error.UnrecoverableError) { | ||
//Reset I2C peripheral | ||
i2c2.apply(config); | ||
} else { | ||
i2c2.clear_errors(); | ||
} | ||
continue; | ||
}; | ||
std.log.info("data received: {}", .{to_read[0]}); | ||
counter.sleep_ms(1000); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const std = @import("std"); | ||
const microzig = @import("microzig"); | ||
|
||
const RCC = microzig.chip.peripherals.RCC; | ||
const stm32 = microzig.hal; | ||
const gpio = stm32.gpio; | ||
|
||
const timer = stm32.timer.GPTimer.TIM2; | ||
|
||
const I2c = stm32.i2c; | ||
|
||
const uart: stm32.uart.UART = @enumFromInt(0); | ||
const TX = gpio.Pin.from_port(.A, 9); | ||
|
||
const i2c: I2c.I2C = @enumFromInt(1); | ||
const SCL = gpio.Pin.from_port(.B, 10); | ||
const SDA = gpio.Pin.from_port(.B, 11); | ||
const config = I2c.Config{ | ||
.pclk = 8_000_000, | ||
.speed = 100_000, | ||
.mode = .standard, | ||
}; | ||
|
||
pub const microzig_options = microzig.Options{ | ||
.logFn = stm32.uart.logFn, | ||
}; | ||
|
||
pub fn main() !void { | ||
RCC.APB2ENR.modify(.{ | ||
.GPIOBEN = 1, | ||
.GPIOAEN = 1, | ||
.AFIOEN = 1, | ||
.USART1EN = 1, | ||
}); | ||
|
||
RCC.APB1ENR.modify(.{ | ||
.I2C2EN = 1, | ||
.TIM2EN = 1, | ||
}); | ||
|
||
const counter = timer.into_counter(8_000_000); | ||
|
||
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz); | ||
|
||
//Set internal Pull-ups (not recommended for real applications) | ||
SCL.set_input_mode(.pull); | ||
SDA.set_input_mode(.pull); | ||
SCL.set_pull(.up); | ||
SDA.set_pull(.up); | ||
|
||
//Set SCL and SDA to alternate function open drain | ||
SCL.set_output_mode(.alternate_function_open_drain, .max_50MHz); | ||
SDA.set_output_mode(.alternate_function_open_drain, .max_50MHz); | ||
|
||
i2c.apply(config); | ||
|
||
uart.apply(.{ | ||
.baud_rate = 115200, | ||
.clock_speed = 8_000_000, | ||
}); | ||
|
||
stm32.uart.init_logger(uart); | ||
|
||
std.log.info("start I2C master", .{}); | ||
while (true) { | ||
std.log.info("starting i2c scan:", .{}); | ||
for (1..127) |val| { | ||
const addr = I2c.Address.new(@intCast(val)); | ||
i2c.write_blocking(addr, &[_]u8{0}, counter.make_ms_timeout(100)) catch |err| { | ||
if (err == error.UnrecoverableError) { | ||
i2c.apply(config); | ||
} else { | ||
i2c.clear_errors(); | ||
} | ||
continue; | ||
}; | ||
std.log.info("found device at 0x{x}", .{val}); | ||
counter.sleep_ms(1000); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const std = @import("std"); | ||
const microzig = @import("microzig"); | ||
|
||
const RCC = microzig.chip.peripherals.RCC; | ||
const stm32 = microzig.hal; | ||
|
||
const timer = stm32.timer.GPTimer.TIM2; | ||
|
||
const uart: stm32.uart.UART = @enumFromInt(0); | ||
const gpio = stm32.gpio; | ||
const TX = gpio.Pin.from_port(.A, 9); | ||
const RX = gpio.Pin.from_port(.A, 10); | ||
|
||
pub fn main() !void { | ||
RCC.APB1ENR.modify(.{ | ||
.TIM2EN = 1, | ||
}); | ||
|
||
RCC.APB2ENR.modify(.{ | ||
.USART1EN = 1, | ||
.GPIOAEN = 1, | ||
}); | ||
|
||
const counter = timer.into_counter(8_000_000); | ||
|
||
TX.set_output_mode(.alternate_function_push_pull, .max_50MHz); | ||
RX.set_input_mode(.pull); | ||
|
||
uart.apply(.{ | ||
.baud_rate = 115200, | ||
.clock_speed = 8_000_000, | ||
}); | ||
|
||
var byte: [100]u8 = undefined; | ||
|
||
//simple USART echo | ||
try uart.write_blocking("START UART ECHO\n", null); | ||
while (true) { | ||
@memset(&byte, 0); | ||
uart.read_blocking(&byte, counter.make_ms_timeout(100)) catch |err| { | ||
if (err != error.Timeout) { | ||
uart.writer().print("Got error {any}\n", .{err}) catch unreachable; | ||
uart.clear_errors(); | ||
continue; | ||
} | ||
}; | ||
|
||
uart.write_blocking(&byte, null) catch unreachable; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add
instance.num