@@ -37,7 +37,30 @@ defmodule Circuits.I2C do
37
37
"""
38
38
@ type backend ( ) :: { module ( ) , keyword ( ) }
39
39
40
- @ type opt ( ) :: { :retries , non_neg_integer ( ) }
40
+ @ typedoc """
41
+ I2C open options
42
+
43
+ See I2C backend documentation, device driver caveats, and function calls in this
44
+ module for notes. In general,
45
+
46
+ * `:retries` - the number of times to retry a transaction. I.e. 2 retries means
47
+ the transaction is attempted at most 3 times. Defaults to 0 retries.
48
+ * `:timeout` - the time in milliseconds to wait for a transaction to complete.
49
+ Any value <0 means to use the device driver or hardware default. The default
50
+ value is -1 to avoid setting it, but it is very common for the default to be 1000 ms.
51
+ """
52
+ @ type open_options ( ) :: keyword ( )
53
+
54
+ @ typedoc """
55
+ I2C transfer options
56
+
57
+ See I2C backend documentation, device driver caveats, and function calls in this
58
+ module for notes. In general,
59
+
60
+ * `:retries` - override the number of times to retry a transaction from what was
61
+ passed to `open/3`.
62
+ """
63
+ @ type transfer_options ( ) :: keyword ( )
41
64
42
65
@ typedoc """
43
66
Connection to a real or virtual I2C controller
@@ -79,10 +102,12 @@ defmodule Circuits.I2C do
79
102
Options depend on the backend. The following are for the I2CDev (default)
80
103
backend:
81
104
82
- * `:retries` - the default number of retries to automatically do on reads
83
- and writes (defaults to no retries)
105
+ * `:retries` - the number of times to retry a transaction. I.e. 2 retries means
106
+ the transaction is attempted at most 3 times. Defaults to 0 retries.
107
+ * `:timeout` - the time in milliseconds to wait for a transaction to complete
108
+ or <0 to avoid setting it. Defaults to -1.
84
109
"""
85
- @ spec open ( String . t ( ) , keyword ( ) ) :: { :ok , Bus . t ( ) } | { :error , term ( ) }
110
+ @ spec open ( String . t ( ) , open_options ( ) ) :: { :ok , Bus . t ( ) } | { :error , term ( ) }
86
111
def open ( bus_name , options \\ [ ] ) when is_binary ( bus_name ) do
87
112
{ module , default_options } = default_backend ( )
88
113
module . open ( bus_name , Keyword . merge ( default_options , options ) )
@@ -91,43 +116,38 @@ defmodule Circuits.I2C do
91
116
@ doc """
92
117
Initiate a read transaction to the I2C device at the specified `address`
93
118
94
- Options:
95
-
96
- * `:retries` - number of retries before failing (defaults to no retries)
119
+ See `t:transfer_options/0` for options, but backend may provide more.
97
120
"""
98
- @ spec read ( Bus . t ( ) , address ( ) , pos_integer ( ) , [ opt ( ) ] ) :: { :ok , binary ( ) } | { :error , term ( ) }
121
+ @ spec read ( Bus . t ( ) , address ( ) , pos_integer ( ) , transfer_options ( ) ) ::
122
+ { :ok , binary ( ) } | { :error , term ( ) }
99
123
def read ( bus , address , bytes_to_read , opts \\ [ ] ) do
100
124
Bus . read ( bus , address , bytes_to_read , opts )
101
125
end
102
126
103
127
@ doc """
104
128
Initiate a read transaction and raise on error
105
129
"""
106
- @ spec read! ( Bus . t ( ) , address ( ) , pos_integer ( ) , [ opt ( ) ] ) :: binary ( )
130
+ @ spec read! ( Bus . t ( ) , address ( ) , pos_integer ( ) , transfer_options ( ) ) :: binary ( )
107
131
def read! ( bus , address , bytes_to_read , opts \\ [ ] ) do
108
132
unwrap_or_raise ( read ( bus , address , bytes_to_read , opts ) )
109
133
end
110
134
111
135
@ doc """
112
136
Write `data` to the I2C device at `address`.
113
137
114
- Options:
115
-
116
- * `:retries` - number of retries before failing (defaults to no retries)
138
+ See `t:transfer_options/0` for options, but backend may provide more.
117
139
"""
118
- @ spec write ( Bus . t ( ) , address ( ) , iodata ( ) , [ opt ( ) ] ) :: :ok | { :error , term ( ) }
140
+ @ spec write ( Bus . t ( ) , address ( ) , iodata ( ) , transfer_options ( ) ) :: :ok | { :error , term ( ) }
119
141
def write ( bus , address , data , opts \\ [ ] ) do
120
142
Bus . write ( bus , address , data , opts )
121
143
end
122
144
123
145
@ doc """
124
146
Write `data` to the I2C device at `address` and raise on error
125
147
126
- Options:
127
-
128
- * `:retries` - number of retries before failing (defaults to no retries)
148
+ See `t:transfer_options/0` for options, but backend may provide more.
129
149
"""
130
- @ spec write! ( Bus . t ( ) , address ( ) , iodata ( ) , [ opt ( ) ] ) :: :ok
150
+ @ spec write! ( Bus . t ( ) , address ( ) , iodata ( ) , transfer_options ( ) ) :: :ok
131
151
def write! ( bus , address , data , opts \\ [ ] ) do
132
152
unwrap_or_raise_ok ( write ( bus , address , data , opts ) )
133
153
end
@@ -143,11 +163,9 @@ defmodule Circuits.I2C do
143
163
transaction will be issued that way. On the Raspberry Pi, this can be enabled
144
164
globally with `File.write!("/sys/module/i2c_bcm2708/parameters/combined", "1")`
145
165
146
- Options:
147
-
148
- * `:retries` - number of retries before failing (defaults to no retries)
166
+ See `t:transfer_options/0` for options, but backend may provide more.
149
167
"""
150
- @ spec write_read ( Bus . t ( ) , address ( ) , iodata ( ) , pos_integer ( ) , [ opt ( ) ] ) ::
168
+ @ spec write_read ( Bus . t ( ) , address ( ) , iodata ( ) , pos_integer ( ) , transfer_options ( ) ) ::
151
169
{ :ok , binary ( ) } | { :error , term ( ) }
152
170
def write_read ( bus , address , write_data , bytes_to_read , opts \\ [ ] ) do
153
171
Bus . write_read ( bus , address , write_data , bytes_to_read , opts )
@@ -156,11 +174,9 @@ defmodule Circuits.I2C do
156
174
@ doc """
157
175
Write `data` to an I2C device and then immediately issue a read. Raise on errors.
158
176
159
- Options:
160
-
161
- * `:retries` - number of retries before failing (defaults to no retries)
177
+ See `t:transfer_options/0` for options, but backend may provide more.
162
178
"""
163
- @ spec write_read! ( Bus . t ( ) , address ( ) , iodata ( ) , pos_integer ( ) , [ opt ( ) ] ) :: binary ( )
179
+ @ spec write_read! ( Bus . t ( ) , address ( ) , iodata ( ) , pos_integer ( ) , transfer_options ( ) ) :: binary ( )
164
180
def write_read! ( bus , address , write_data , bytes_to_read , opts \\ [ ] ) do
165
181
unwrap_or_raise ( write_read ( bus , address , write_data , bytes_to_read , opts ) )
166
182
end
0 commit comments