@@ -44,7 +44,7 @@ pub const Address = enum(u7) {
4444 /// Reserved addresses are ones that match `0b0000XXX` or `0b1111XXX`.
4545 ///
4646 /// See more here: https://www.i2c-bus.org/addressing/
47- pub fn is_reserved (addr : Address ) Address.Error ! void {
47+ pub fn check_reserved (addr : Address ) Address.Error ! void {
4848 const value : u7 = @intFromEnum (addr );
4949
5050 switch (value ) {
@@ -77,58 +77,52 @@ ptr: *anyopaque,
7777/// Virtual table for the datagram device functions.
7878vtable : * const VTable ,
7979
80- pub fn set_address (dev : I2C_Device , addr : Address , allow_reserved : Allow_Reserved ) InterfaceError ! void {
81- const set_address_fn = dev .vtable .set_address_fn orelse return InterfaceError .Unsupported ;
82- return set_address_fn (dev .ptr , addr , allow_reserved );
83- }
84-
8580/// Writes a single `datagram` to the device.
86- pub fn write (dev : I2C_Device , datagram : []const u8 ) InterfaceError ! void {
87- return try dev .writev (&.{datagram });
81+ pub fn write (dev : I2C_Device , address : Address , datagram : []const u8 ) InterfaceError ! void {
82+ return try dev .writev (address , &.{datagram });
8883}
8984
9085/// Writes multiple `datagrams` to the device.
91- pub fn writev (dev : I2C_Device , datagrams : []const []const u8 ) InterfaceError ! void {
86+ pub fn writev (dev : I2C_Device , address : Address , datagrams : []const []const u8 ) InterfaceError ! void {
9287 const writev_fn = dev .vtable .writev_fn orelse return InterfaceError .Unsupported ;
93- return writev_fn (dev .ptr , datagrams );
88+ return writev_fn (dev .ptr , address , datagrams );
9489}
9590
9691/// Writes then reads a single `datagram` to the device.
97- pub fn write_then_read (dev : I2C_Device , src : []const u8 , dst : []u8 ) InterfaceError ! void {
98- return try dev .writev_then_readv (&.{src }, &.{dst });
92+ pub fn write_then_read (dev : I2C_Device , address : Address , src : []const u8 , dst : []u8 ) InterfaceError ! void {
93+ return try dev .writev_then_readv (address , &.{src }, &.{dst });
9994}
10095
10196/// Writes a slice of datagrams to the device, then reads back into another slice of datagrams
10297pub fn writev_then_readv (
10398 dev : I2C_Device ,
99+ address : Address ,
104100 write_chunks : []const []const u8 ,
105101 read_chunks : []const []u8 ,
106102) InterfaceError ! void {
107103 const writev_then_readv_fn = dev .vtable .writev_then_readv_fn orelse return InterfaceError .Unsupported ;
108- return writev_then_readv_fn (dev .ptr , write_chunks , read_chunks );
104+ return writev_then_readv_fn (dev .ptr , address , write_chunks , read_chunks );
109105}
110106
111107/// Reads a single `datagram` from the device.
112108/// Function returns the number of bytes written in `datagram`.
113- pub fn read (dev : I2C_Device , datagram : []u8 ) InterfaceError ! usize {
114- return try dev .readv (&.{datagram });
109+ pub fn read (dev : I2C_Device , address : Address , datagram : []u8 ) InterfaceError ! usize {
110+ return try dev .readv (address , &.{datagram });
115111}
116112
117113/// Reads multiple `datagrams` from the device.
118114/// Function returns the number of bytes written in `datagrams`.
119- pub fn readv (dev : I2C_Device , datagrams : []const []u8 ) InterfaceError ! usize {
115+ pub fn readv (dev : I2C_Device , address : Address , datagrams : []const []u8 ) InterfaceError ! usize {
120116 const readv_fn = dev .vtable .readv_fn orelse return InterfaceError .Unsupported ;
121- return readv_fn (dev .ptr , datagrams );
117+ return readv_fn (dev .ptr , address , datagrams );
122118}
123119
124- pub const Allow_Reserved = enum { allow_general , allow_reserved , dont_allow_reserved };
125-
126120pub const VTable = struct {
127- set_address_fn : ? * const fn (* anyopaque , Address , Allow_Reserved ) InterfaceError ! void ,
128- writev_fn : ? * const fn (* anyopaque , datagrams : []const []const u8 ) InterfaceError ! void ,
129- readv_fn : ? * const fn (* anyopaque , datagrams : []const []u8 ) InterfaceError ! usize ,
121+ writev_fn : ? * const fn (* anyopaque , Address , datagrams : []const []const u8 ) InterfaceError ! void ,
122+ readv_fn : ? * const fn (* anyopaque , Address , datagrams : []const []u8 ) InterfaceError ! usize ,
130123 writev_then_readv_fn : ? * const fn (
131124 * anyopaque ,
125+ Address ,
132126 write_chunks : []const []const u8 ,
133127 read_chunks : []const []u8 ,
134128 ) InterfaceError ! void = null ,
@@ -191,18 +185,9 @@ pub const Test_Device = struct {
191185 };
192186 }
193187
194- fn set_address (ctx : * anyopaque , addr : Address , allow_reserved : Allow_Reserved ) InterfaceError ! void {
195- const td : * Test_Device = @ptrCast (@alignCast (ctx ));
196- if (allow_reserved == .dont_allow_reserved )
197- addr .is_reserved () catch return Error .IllegalAddress
198- else if (allow_reserved == .allow_general )
199- addr .is_reserved () catch | err | if (err != Address .Error .GeneralCall )
200- return Error .IllegalAddress ;
201- td .addr = addr ;
202- }
203-
204- fn writev (ctx : * anyopaque , datagrams : []const []const u8 ) InterfaceError ! void {
188+ fn writev (ctx : * anyopaque , address : Address , datagrams : []const []const u8 ) InterfaceError ! void {
205189 const td : * Test_Device = @ptrCast (@alignCast (ctx ));
190+ _ = address ;
206191
207192 if (! td .write_enabled ) {
208193 return error .Unsupported ;
@@ -231,8 +216,9 @@ pub const Test_Device = struct {
231216 td .packets .append (dg ) catch return error .UnknownAbort ;
232217 }
233218
234- fn readv (ctx : * anyopaque , datagrams : []const []u8 ) InterfaceError ! usize {
219+ fn readv (ctx : * anyopaque , address : Address , datagrams : []const []u8 ) InterfaceError ! usize {
235220 const td : * Test_Device = @ptrCast (@alignCast (ctx ));
221+ _ = address ;
236222
237223 const inputs = td .input_sequence orelse return error .Unsupported ;
238224
@@ -271,20 +257,19 @@ pub const Test_Device = struct {
271257 return written ;
272258 }
273259
274- fn writev_then_readv (ctx : * anyopaque , write_chunks : []const []const u8 , read_chunks : []const []u8 ) InterfaceError ! void {
275- try Test_Device .writev (ctx , write_chunks );
276- _ = try Test_Device .readv (ctx , read_chunks );
260+ fn writev_then_readv (ctx : * anyopaque , address : Address , write_chunks : []const []const u8 , read_chunks : []const []u8 ) InterfaceError ! void {
261+ try Test_Device .writev (ctx , address , write_chunks );
262+ _ = try Test_Device .readv (ctx , address , read_chunks );
277263 }
278264
279265 const vtable = I2C_Device.VTable {
280- .set_address_fn = Test_Device .set_address ,
281266 .writev_fn = Test_Device .writev ,
282267 .readv_fn = Test_Device .readv ,
283268 .writev_then_readv_fn = Test_Device .writev_then_readv ,
284269 };
285270};
286271
287- test "Address.is_reserved returns correct error types" {
272+ test "Address.check_reserved returns correct error types" {
288273 const TestCase = struct {
289274 address : u7 ,
290275 expected_error : ? Address.Error ,
@@ -310,15 +295,15 @@ test "Address.is_reserved returns correct error types" {
310295 for (test_cases ) | test_case | {
311296 const addr : Address = @enumFromInt (test_case .address );
312297 if (test_case .expected_error ) | expected_error | {
313- std .testing .expectError (expected_error , addr .is_reserved ()) catch | err | {
298+ std .testing .expectError (expected_error , addr .check_reserved ()) catch | err | {
314299 std .debug .print (
315300 "Failed test case: {s} (address 0x{X:0>2})\n " ,
316301 .{ test_case .description , test_case .address },
317302 );
318303 return err ;
319304 };
320305 } else {
321- addr .is_reserved () catch | err | {
306+ addr .check_reserved () catch | err | {
322307 std .debug .print (
323308 "Expected valid address but got error for: {s} (address 0x{X:0>2})\n " ,
324309 .{ test_case .description , test_case .address },
@@ -339,34 +324,35 @@ test Test_Device {
339324
340325 var buffer : [16 ]u8 = undefined ;
341326
342- const dd = td .i2c_device ();
327+ const id = td .i2c_device ();
328+ const addr : Address = @enumFromInt (0 );
343329
344330 {
345331 // The first input datagram will be received here:
346- const recv_len = try dd .read (& buffer );
332+ const recv_len = try id .read (addr , & buffer );
347333 try std .testing .expectEqualStrings ("first datagram" , buffer [0.. recv_len ]);
348334 }
349335
350336 {
351337 // The second one here:
352- const recv_len = try dd .read (& buffer );
338+ const recv_len = try id .read (addr , & buffer );
353339 try std .testing .expectEqualStrings ("second datagram" , buffer [0.. recv_len ]);
354340 }
355341
356342 {
357343 // The third datagram will overrun our buffer, so we're receiving an error
358344 // which tells us that the whole buffer is filled, but there's data that
359345 // was discarded:
360- try std .testing .expectError (error .BufferOverrun , dd .read (& buffer ));
346+ try std .testing .expectError (error .BufferOverrun , id .read (addr , & buffer ));
361347 try std .testing .expectEqualStrings ("the very third d" , & buffer );
362348 }
363349
364350 // As there's no fourth datagram available, the test device will yield
365351 // an `IoError` for when no datagrams are available anymore:
366- try std .testing .expectError (error .NoData , dd .read (& buffer ));
352+ try std .testing .expectError (error .NoData , id .read (addr , & buffer ));
367353
368- try dd .write ("Hello, World!" );
369- try dd .writev (&.{ "See" , " you " , "soon!" });
354+ try id .write (addr , "Hello, World!" );
355+ try id .writev (addr , &.{ "See" , " you " , "soon!" });
370356
371357 // Check if we had exactly these datagrams:
372358 try td .expect_sent (&.{
0 commit comments