Skip to content

Commit f186140

Browse files
authored
esp: hal: i2c (#500)
1 parent de9c166 commit f186140

5 files changed

Lines changed: 630 additions & 3 deletions

File tree

examples/espressif/esp/build.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn build(b: *std.Build) void {
2121
.{ .name = "blinky", .file = "src/blinky.zig" },
2222
.{ .name = "custom_clock_config", .file = "src/custom_clock_config.zig" },
2323
.{ .name = "gpio_input", .file = "src/gpio_input.zig" },
24+
.{ .name = "i2c_bus_scan", .file = "src/i2c_bus_scan.zig" },
2425
.{ .name = "stepper_driver", .file = "src/stepper_driver.zig" },
2526
.{ .name = "stepper_driver_dumb", .file = "src/stepper_driver_dumb.zig" },
2627
.{ .name = "systimer", .file = "src/systimer.zig" },
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const std = @import("std");
2+
const microzig = @import("microzig");
3+
const time = microzig.drivers.time;
4+
5+
const esp = microzig.hal;
6+
const i2c = esp.i2c;
7+
const gpio = esp.gpio;
8+
const peripherals = microzig.chip.peripherals;
9+
10+
const i2c0 = i2c.instance.num(0);
11+
12+
const usb_serial_jtag = esp.usb_serial_jtag;
13+
14+
pub const microzig_options = microzig.Options{
15+
.logFn = usb_serial_jtag.logger.logFn,
16+
};
17+
18+
pub fn main() !void {
19+
const sda_pin = gpio.num(6);
20+
const scl_pin = gpio.num(5);
21+
22+
// TODO: Should this be done in apply?
23+
// Enable I2C peripheral clock
24+
peripherals.SYSTEM.PERIP_CLK_EN0.modify(.{ .I2C_EXT0_CLK_EN = 1 });
25+
// Take I2C out of reset
26+
peripherals.SYSTEM.PERIP_RST_EN0.modify(.{ .I2C_EXT0_RST = 0 });
27+
28+
try i2c0.apply(
29+
.{ .sda = sda_pin, .scl = scl_pin },
30+
100_000,
31+
);
32+
33+
// for (0x20..0x21) |addr| {
34+
for (0..std.math.maxInt(u7)) |addr| {
35+
const a: i2c.Address = @enumFromInt(addr);
36+
37+
var rx_data: [1]u8 = undefined;
38+
_ = i2c0.read_blocking(a, &rx_data, time.Duration.from_ms(250)) catch continue;
39+
40+
std.log.info("I2C device found at address {X}.", .{addr});
41+
}
42+
}

examples/raspberrypi/rp2xxx/src/i2c_bus_scan.zig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ pub fn main() !void {
4242
for (0..std.math.maxInt(u7)) |addr| {
4343
const a: i2c.Address = @enumFromInt(addr);
4444

45-
// Skip over any reserved addresses.
46-
if (a.is_reserved()) continue;
47-
4845
var rx_data: [1]u8 = undefined;
4946
_ = i2c0.read_blocking(a, &rx_data, time.Duration.from_ms(100)) catch continue;
5047

port/espressif/esp/src/hal.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub const clocks = @import("hal/clocks.zig");
44
pub const compatibility = @import("hal/compatibility.zig");
55
pub const drivers = @import("hal/drivers.zig");
66
pub const gpio = @import("hal/gpio.zig");
7+
pub const i2c = @import("hal/i2c.zig");
78
pub const rom = @import("hal/rom.zig");
89
pub const system = @import("hal/system.zig");
910
pub const systimer = @import("hal/systimer.zig");

0 commit comments

Comments
 (0)