|
| 1 | +const std = @import("std"); |
| 2 | +const microzig = @import("microzig"); |
| 3 | +const time = microzig.drivers.time; |
| 4 | +const board = microzig.board; |
| 5 | + |
| 6 | +const nrf = microzig.hal; |
| 7 | +const TMP117 = microzig.drivers.sensor.TMP117; |
| 8 | + |
| 9 | +const i2c = nrf.i2c; |
| 10 | +const i2cdma = nrf.i2cdma; |
| 11 | +const gpio = nrf.gpio; |
| 12 | +const peripherals = microzig.chip.peripherals; |
| 13 | +const I2C_Device = nrf.drivers.I2C_Device; |
| 14 | + |
| 15 | +const uart = nrf.uart.num(0); |
| 16 | +const i2c0 = i2c.num(0); |
| 17 | +const i2c0dma = i2cdma.num(0); |
| 18 | + |
| 19 | +const sleep_ms = nrf.time.sleep_ms; |
| 20 | + |
| 21 | +pub const microzig_options = microzig.Options{ |
| 22 | + .log_level = .debug, |
| 23 | + .logFn = nrf.uart.logFn, |
| 24 | +}; |
| 25 | + |
| 26 | +pub fn main() !void { |
| 27 | + board.init(); |
| 28 | + |
| 29 | + uart.apply(.{ |
| 30 | + .tx_pin = board.uart_tx, |
| 31 | + .rx_pin = board.uart_rx, |
| 32 | + .baud_rate = .@"115200", |
| 33 | + }); |
| 34 | + |
| 35 | + nrf.uart.init_logger(uart); |
| 36 | + defer i2c0.reset(); |
| 37 | + defer i2c0dma.reset(); |
| 38 | + |
| 39 | + // ------------------------ TMP117 w/driver ------------------------ |
| 40 | + std.log.info("I2C temp sensor (using driver)", .{}); |
| 41 | + try i2c0.apply(.{ |
| 42 | + .scl_pin = gpio.num(0, 9), |
| 43 | + .sda_pin = gpio.num(0, 10), |
| 44 | + }); |
| 45 | + |
| 46 | + // Create i2c datagram device |
| 47 | + var i2c_device = I2C_Device.init(i2c0, @enumFromInt(0x48)); |
| 48 | + // Pass i2c device to driver to create sensor instance |
| 49 | + const temp_sensor = try TMP117.init(i2c_device.datagram_device()); |
| 50 | + |
| 51 | + const temp_sensor_address = 0x48; |
| 52 | + var temp_buf: [2]u8 = undefined; |
| 53 | + const cbuf = [3]u8{ 1, 0, 0 }; |
| 54 | + |
| 55 | + std.log.info("Writing config", .{}); |
| 56 | + try temp_sensor.write_configuration(.{}); |
| 57 | + |
| 58 | + std.log.info("Reading device ID config", .{}); |
| 59 | + const id = try temp_sensor.read_device_id(); |
| 60 | + std.log.info("ID: {any}", .{id}); |
| 61 | + |
| 62 | + for (0..5) |_| { |
| 63 | + const temp = try temp_sensor.read_temperature(); |
| 64 | + std.log.info("Temp: {d:0.2}°C", .{temp}); |
| 65 | + sleep_ms(500); |
| 66 | + } |
| 67 | + i2c0.reset(); |
| 68 | + |
| 69 | + // ------------------------ TMP117 raw ------------------------ |
| 70 | + std.log.info("I2C temp sensor (raw writes)", .{}); |
| 71 | + |
| 72 | + try i2c0.apply(.{ |
| 73 | + .scl_pin = gpio.num(0, 9), |
| 74 | + .sda_pin = gpio.num(0, 10), |
| 75 | + }); |
| 76 | + |
| 77 | + std.log.info("Writing config", .{}); |
| 78 | + try i2c0.writev_blocking(@enumFromInt(temp_sensor_address), &.{&cbuf}, null); |
| 79 | + |
| 80 | + std.log.info("Reading temp", .{}); |
| 81 | + for (0..5) |_| { |
| 82 | + const write_buf = [_]u8{0}; // Read temperature register |
| 83 | + try i2c0.writev_then_readv_blocking(@enumFromInt(temp_sensor_address), &.{&write_buf}, &.{&temp_buf}, null); |
| 84 | + const temp_u = std.mem.readInt(u16, &temp_buf, .big); |
| 85 | + const temp = @as(f32, @floatFromInt(temp_u)) * 7.8125E-3; |
| 86 | + std.log.info("Temp: {d:0.2}°C", .{temp}); |
| 87 | + sleep_ms(500); |
| 88 | + } |
| 89 | + i2c0.reset(); |
| 90 | + |
| 91 | + // ------------------------ TMP117 raw (DMA) ------------------------ |
| 92 | + std.log.info("I2C temp sensor (raw writes w/DMA)", .{}); |
| 93 | + |
| 94 | + try i2c0dma.apply(.{ |
| 95 | + .scl_pin = gpio.num(0, 9), |
| 96 | + .sda_pin = gpio.num(0, 10), |
| 97 | + }); |
| 98 | + |
| 99 | + std.log.info("Writing config", .{}); |
| 100 | + try i2c0dma.write_blocking(@enumFromInt(temp_sensor_address), &cbuf, null); |
| 101 | + |
| 102 | + std.log.info("Reading temp", .{}); |
| 103 | + for (0..5) |_| { |
| 104 | + const write_buf = [_]u8{0}; // Read temperature register |
| 105 | + try i2c0dma.write_then_read_blocking(@enumFromInt(temp_sensor_address), &write_buf, &temp_buf, null); |
| 106 | + const temp_u = std.mem.readInt(u16, &temp_buf, .big); |
| 107 | + const temp = @as(f32, @floatFromInt(temp_u)) * 7.8125E-3; |
| 108 | + std.log.info("Temp: {d:0.2}°C", .{temp}); |
| 109 | + sleep_ms(500); |
| 110 | + } |
| 111 | + i2c0dma.reset(); |
| 112 | +} |
0 commit comments