@@ -114,6 +114,8 @@ public void start() {
114
114
115
115
Logger .debug ("Found {} GPIO chips" , Integer .valueOf (chips .size ()));
116
116
117
+ final boolean unknown_board = !board_info .isRecognised ();
118
+
117
119
// Validate the data in BoardPinInfo
118
120
if (board_info instanceof GenericLinuxArmBoardInfo ) {
119
121
board_info .getChipMapping ()
@@ -123,7 +125,7 @@ public void start() {
123
125
// Validate the board pin info matches the GPIO chip and line offsets
124
126
Logger .debug ("Validating BoardPinInfo against detected GPIO chip and line offsets..." );
125
127
board_info .getGpioPins ().forEach (pin_info -> {
126
- GpioChip chip = chips .get (Integer .valueOf (pin_info .getChip ()));
128
+ final GpioChip chip = chips .get (Integer .valueOf (pin_info .getChip ()));
127
129
if (chip == null ) {
128
130
if (pin_info .getChip () != -1 ) {
129
131
Logger .warn ("No such chip for id {}" , Integer .valueOf (pin_info .getChip ()));
@@ -138,75 +140,7 @@ public void start() {
138
140
139
141
// Validate the GPIO chip and line offsets match those detected
140
142
Logger .debug ("Validating detected GPIO chip and line offsets against BoardPinInfo..." );
141
- chips .values ().forEach (chip -> {
142
- for (GpioLine gpio_line : chip .getLines ()) {
143
- PinInfo pin_info = null ;
144
- final String line_name = gpio_line .getName ().trim ();
145
-
146
- // Try to find this GPIO in the board pin info by the assumed system name
147
- if (!line_name .isEmpty ()) {
148
- pin_info = board_info .getByName (line_name );
149
- }
150
-
151
- // If the pin couldn't be found for the assigned name try to find the pin info
152
- // by chip and line offset number
153
- if (pin_info == null ) {
154
- // Note that getByChipAndLineOffset doesn't create missing entries
155
- pin_info = board_info .getByChipAndLineOffset (chip .getChipId (), gpio_line .getOffset ())
156
- .orElse (null );
157
- }
158
-
159
- // Finally, if still not found see if the name is in the format GPIOnn and
160
- // lookup by GPIO number
161
- if (pin_info == null && line_name .matches (GPIO_LINE_NUMBER_PATTERN )) {
162
- // Note that this isn't reliable - GPIO names are often missing or not in this
163
- // format
164
- // Note that the unknown / generic board info classes will create missing pin
165
- // info objects if you call getByGpioNumber - we don't want that to happen here
166
- pin_info = board_info .getGpios ()
167
- .get (Integer .valueOf (line_name .replaceAll (GPIO_LINE_NUMBER_PATTERN , "$1" )));
168
- }
169
-
170
- // XXX This includes a bit of a hack to ignore pins with the name "NC" - the BBB
171
- // does this for quite a few pins which triggers the warning message
172
- if (pin_info == null && !line_name .isEmpty () && !line_name .equals ("NC" )
173
- && !line_name .equals ("-" )) {
174
- Logger .debug ("Detected GPIO line ({} {}-{}) that isn't configured in BoardPinInfo" ,
175
- line_name , Integer .valueOf (chip .getChipId ()),
176
- Integer .valueOf (gpio_line .getOffset ()));
177
- if (addUnconfiguredGpios ) {
178
- // Add a new pin info to the board pin info
179
- int gpio_num ;
180
- if (line_name .matches (GPIO_LINE_NUMBER_PATTERN )) {
181
- gpio_num = Integer .parseInt (line_name .replaceAll (GPIO_LINE_NUMBER_PATTERN , "$1" ));
182
- } else {
183
- // Calculate the GPIO number
184
- gpio_num = chip .getLineOffset () + gpio_line .getOffset ();
185
- }
186
- pin_info = board_info .addGpioPinInfo (gpio_num , line_name , PinInfo .NOT_DEFINED ,
187
- PinInfo .DIGITAL_IN_OUT , chip .getChipId (), gpio_line .getOffset ());
188
- Logger .debug ("Added pin info {}" , pin_info );
189
- }
190
- } else if (pin_info != null ) {
191
- if (pin_info .getChip () != chip .getChipId ()
192
- || pin_info .getLineOffset () != gpio_line .getOffset ()) {
193
- Logger .warn (
194
- "Configured pin chip and line offset ({}-{}) doesn't match that detected ({}-{}), line name '{}' - updating" ,
195
- Integer .valueOf (pin_info .getChip ()), Integer .valueOf (pin_info .getLineOffset ()),
196
- Integer .valueOf (chip .getChipId ()), Integer .valueOf (gpio_line .getOffset ()),
197
- gpio_line .getName ());
198
- pin_info .setChip (chip .getChipId ());
199
- pin_info .setLineOffset (gpio_line .getOffset ());
200
- }
201
-
202
- if (!line_name .isEmpty () && !pin_info .getName ().equals (line_name )) {
203
- Logger .info ("Configured pin name ({}) doesn't match that detected ({})" ,
204
- pin_info .getName (), line_name );
205
- // XXX What to do about it - update the board pin info? Just ignore for now.
206
- }
207
- }
208
- }
209
- });
143
+ chips .values ().forEach (chip -> loadChip (board_info , unknown_board , chip ));
210
144
} catch (IOException e ) {
211
145
throw new RuntimeIOException ("Error initialising GPIO chips: " + e .getMessage (), e );
212
146
}
@@ -235,6 +169,76 @@ public void start() {
235
169
}
236
170
}
237
171
172
+ private void loadChip (BoardInfo boardInfo , boolean unknownBoard , GpioChip chip ) {
173
+ for (GpioLine gpio_line : chip .getLines ()) {
174
+ PinInfo pin_info = null ;
175
+ final String line_name = gpio_line .getName ().trim ();
176
+
177
+ // Try to find this GPIO in the board pin info by the assumed system name
178
+ if (!line_name .isEmpty ()) {
179
+ pin_info = boardInfo .getByName (line_name );
180
+ }
181
+
182
+ // If the pin couldn't be found for the assigned name try to find the pin info
183
+ // by chip and line offset number
184
+ if (pin_info == null ) {
185
+ // Note that getByChipAndLineOffset doesn't create missing entries
186
+ pin_info = boardInfo .getByChipAndLineOffset (chip .getChipId (), gpio_line .getOffset ()).orElse (null );
187
+ }
188
+
189
+ // Finally, if still not found see if the name is in the format GPIOnn and
190
+ // lookup by GPIO number
191
+ if (pin_info == null && line_name .matches (GPIO_LINE_NUMBER_PATTERN )) {
192
+ // Note that this isn't reliable - GPIO names are often missing or not in this
193
+ // format
194
+ // Note that the unknown / generic board info classes will create missing pin
195
+ // info objects if you call getByGpioNumber - we don't want that to happen here
196
+ pin_info = boardInfo .getGpios ()
197
+ .get (Integer .valueOf (line_name .replaceAll (GPIO_LINE_NUMBER_PATTERN , "$1" )));
198
+ }
199
+
200
+ // XXX This includes a bit of a hack to ignore pins with the name "NC" - the BBB
201
+ // does this for quite a few pins which triggers the warning message
202
+ if (pin_info == null && !line_name .isEmpty () && !line_name .equals ("NC" ) && !line_name .equals ("-" )) {
203
+ Logger .debug ("Detected GPIO line ({} {}-{}) that isn't configured in BoardPinInfo" , line_name ,
204
+ Integer .valueOf (chip .getChipId ()), Integer .valueOf (gpio_line .getOffset ()));
205
+ if (addUnconfiguredGpios || unknownBoard ) {
206
+ // Add a new pin info to the board pin info
207
+ int gpio_num ;
208
+ if (line_name .matches (GPIO_LINE_NUMBER_PATTERN )) {
209
+ gpio_num = Integer .parseInt (line_name .replaceAll (GPIO_LINE_NUMBER_PATTERN , "$1" ));
210
+ } else {
211
+ // Calculate the GPIO number
212
+ gpio_num = chip .getLineOffset () + gpio_line .getOffset ();
213
+ }
214
+ int physical_pin = PinInfo .NOT_DEFINED ;
215
+ if (line_name .startsWith ("PIN_" )) {
216
+ physical_pin = Integer .parseInt (line_name .substring ("PIN_" .length ()));
217
+ }
218
+ pin_info = boardInfo .addGpioPinInfo (gpio_num , line_name , physical_pin , PinInfo .DIGITAL_IN_OUT ,
219
+ chip .getChipId (), gpio_line .getOffset ());
220
+ Logger .debug ("Added {}" , pin_info );
221
+ }
222
+ } else if (pin_info != null ) {
223
+ if (pin_info .getChip () != chip .getChipId () || pin_info .getLineOffset () != gpio_line .getOffset ()) {
224
+ Logger .warn (
225
+ "Configured pin chip and line offset ({}-{}) doesn't match that detected ({}-{}), line name '{}' - updating" ,
226
+ Integer .valueOf (pin_info .getChip ()), Integer .valueOf (pin_info .getLineOffset ()),
227
+ Integer .valueOf (chip .getChipId ()), Integer .valueOf (gpio_line .getOffset ()),
228
+ gpio_line .getName ());
229
+ pin_info .setChip (chip .getChipId ());
230
+ pin_info .setLineOffset (gpio_line .getOffset ());
231
+ }
232
+
233
+ if (!line_name .isEmpty () && !pin_info .getName ().equals (line_name )) {
234
+ Logger .info ("Configured pin name ({}) doesn't match that detected ({})" , pin_info .getName (),
235
+ line_name );
236
+ // XXX What to do about it - update the board pin info? Just ignore for now.
237
+ }
238
+ }
239
+ }
240
+ }
241
+
238
242
private static void updateChipIds (Map <Integer , GpioChip > chips , Map <String , Integer > chipMapping ,
239
243
BoardInfo boardInfo ) {
240
244
if (chips == null || chips .isEmpty ()) {
@@ -261,7 +265,7 @@ public void shutdown() {
261
265
Logger .trace ("shutdown()" );
262
266
263
267
if (chips != null ) {
264
- chips .values ().forEach (chip -> chip . close () );
268
+ chips .values ().forEach (GpioChip :: close );
265
269
chips .clear ();
266
270
}
267
271
@@ -312,6 +316,9 @@ public int getGpioValue(int gpio) {
312
316
if (mmapGpio != null ) {
313
317
return mmapGpio .gpioRead (gpio ) ? 1 : 0 ;
314
318
}
319
+ // TODO Use GpioChip to read the device and clean up afterwards - check how gpioget works
320
+ // (sets top input)
321
+ // https://github.com/brgl/libgpiod/blob/master/tools/gpioget.c
315
322
return Diozero .UNKNOWN_VALUE ;
316
323
}
317
324
0 commit comments