|
| 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 | +} |
0 commit comments