Skip to content

Commit e90b6fd

Browse files
Merge pull request #118 from Pi4J/1025_spiWR_FFM
update bmp280 SPI example to use new writeRead rather than separate …
2 parents 6294ce5 + 118c133 commit e90b6fd

3 files changed

Lines changed: 67 additions & 114 deletions

File tree

src/main/java/com/pi4j/devices/bmp280/BMP280DeviceSPI.java

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
/*
2+
* * #%L
3+
* * Copyright (C) 2012 - 2025 Pi4J
4+
* * %%
25
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
39
*
4-
* *
5-
* * -
6-
* * #%L
7-
* * **********************************************************************
8-
* * ORGANIZATION : Pi4J
9-
* * PROJECT : Pi4J :: EXTENSION
10-
* * FILENAME : BMP280DeviceSPI.java
11-
* *
12-
* * This file is part of the Pi4J project. More information about
13-
* * this project can be found here: https://pi4j.com/
14-
* * **********************************************************************
15-
* * %%
16-
* * * Copyright (C) 2012 - 2022 Pi4J
17-
* * %%
18-
* *
19-
* * Licensed under the Apache License, Version 2.0 (the "License");
20-
* * you may not use this file except in compliance with the License.
21-
* * You may obtain a copy of the License at
22-
* *
23-
* * http://www.apache.org/licenses/LICENSE-2.0
24-
* *
25-
* * Unless required by applicable law or agreed to in writing, software
26-
* * distributed under the License is distributed on an "AS IS" BASIS,
27-
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28-
* * See the License for the specific language governing permissions and
29-
* * limitations under the License.
30-
* * #L%
31-
* *
10+
* http://www.apache.org/licenses/LICENSE-2.0
3211
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
3317
*
18+
* -
19+
* #%L
20+
* **********************************************************************
21+
* ORGANIZATION : Pi4J
22+
* PROJECT : Pi4J :: EXTENSION
23+
* FILENAME : BMP280DeviceSPI.java
24+
*
25+
* This file is part of the Pi4J project. More information about
26+
* this project can be found here: https://pi4j.com/
27+
* **********************************************************************
28+
* %%
3429
*
3530
*/
3631

3732
package com.pi4j.devices.bmp280;
3833

3934
import com.pi4j.context.Context;
40-
import com.pi4j.io.gpio.digital.DigitalOutput;
41-
import com.pi4j.io.gpio.digital.DigitalState;
4235
import com.pi4j.io.spi.*;
4336
import com.pi4j.util.Console;
4437
import org.slf4j.LoggerFactory;
@@ -65,14 +58,11 @@ public class BMP280DeviceSPI extends BMP280Device {
6558
SpiBus spiBus;
6659

6760
SpiChipSelect chipSlct;
68-
int csPin;
69-
private DigitalOutput csGpio;
7061

7162
// local/internal SPI reference for communication with hardware chip
7263
Spi spi = null;
7364

74-
SpiConfig config = null;
75-
// SPI Provider name and unique ID
65+
// SPI Provider name and unique ID
7666
/**
7767
* Constant <code>SPI_PROVIDER_NAME="NAME + SPI Provider"</code>
7868
*/
@@ -83,12 +73,11 @@ public class BMP280DeviceSPI extends BMP280Device {
8373
public final String SPI_PROVIDER_ID = ID + "-spi";
8474

8575

86-
public BMP280DeviceSPI(Context pi4j, Console console, SpiBus spiBus, SpiChipSelect chipSlct, int csPin, String traceLevel) {
76+
public BMP280DeviceSPI(Context pi4j, Console console, SpiBus spiBus, SpiChipSelect chipSlct, String traceLevel) {
8777
super(pi4j, console, traceLevel);
8878
this.pi4j = pi4j;
8979
this.spiBus = spiBus;
9080
this.chipSlct = chipSlct;
91-
this.csPin = csPin;
9281
this.console = console;
9382
// "trace", "debug", "info", "warn", "error" or "off"). If not specified, defaults to "info"
9483
// must fully qualify logger as others exist and the slf4 code will use the first it
@@ -101,25 +90,7 @@ public BMP280DeviceSPI(Context pi4j, Console console, SpiBus spiBus, SpiChipSele
10190

10291
private void init() {
10392
this.logger.info(">>> Enter:init ");
104-
105-
// required all configs
106-
var outputConfig2 = DigitalOutput.newConfigBuilder(pi4j)
107-
.id("CS_pin")
108-
.name("CS")
109-
.address(this.csPin)
110-
.shutdown(DigitalState.HIGH)
111-
.initial(DigitalState.HIGH)
112-
.provider("gpiod-digital-output");
113-
try {
114-
this.csGpio = pi4j.create(outputConfig2);
115-
} catch (Exception e) {
116-
e.printStackTrace();
117-
console.println("create DigOut CS failed");
118-
System.exit(202);
119-
}
120-
12193
this.createSPIDevice();
122-
12394
this.logger.info("<<<Exit:init ");
12495
}
12596

@@ -128,7 +99,7 @@ private void init() {
12899
* a BMP280 device instance
129100
*/
130101
private void createSPIDevice() {
131-
this.logger.info(">>> Enter:createSPIDevice bus " + this.spiBus + " CS Gpio" + this.csGpio.toString());
102+
this.logger.info(">>> Enter:createSPIDevice bus " + this.spiBus);
132103
var spiConfig = Spi.newConfigBuilder(this.pi4j)
133104
.id("SPI" + this.spiBus + "_BMP280")
134105
.name("D/A converter")
@@ -152,14 +123,11 @@ private void createSPIDevice() {
152123
*/
153124
public int readRegister(int register) {
154125
this.logger.trace(">>> Enter readRegister : " + String.format("0X%02x: ", register));
155-
this.csGpio.low();
156126
byte[] data = new byte[]{(byte) (0b10000000 | register)};
157-
int bytesWritten = this.spi.write(data);
158127
byte[] value = new byte[1];
159-
byte rval = this.spi.readByte();
160-
this.csGpio.high();
161-
this.logger.trace("<<< Exit readRegister : " + String.format("0X%02x: ", rval));
162-
return (rval);
128+
this.spi.writeThenRead(data,value);
129+
this.logger.trace("<<< Exit readRegister : " + String.format("0X%02x: ", value[0]));
130+
return value[0]; //rval);
163131
}
164132

165133
/**
@@ -170,12 +138,9 @@ public int readRegister(int register) {
170138
public int readRegister(int register, byte[] buffer) {
171139
this.logger.trace(">>> Enter readRegister : " + String.format("0X%02x: ", register));
172140
byte[] data = new byte[]{(byte) (0b10000000 | register)};
173-
this.csGpio.low();
174-
int bytesWritten = this.spi.write(data);
175-
int bytesRead = this.spi.read(buffer);
176-
this.csGpio.high();
141+
this.spi.writeThenRead(data, buffer);
177142
this.logger.trace("<<< Exit readRegister : " + String.format("0X%02x: ", buffer[0]) + String.format("0X%02x: ", buffer[0]));
178-
return (bytesRead);
143+
return buffer.length;
179144
}
180145

181146

@@ -193,9 +158,7 @@ public int writeRegister(int register, int data) {
193158
};
194159
byte[] dummy = new byte[2];
195160
// send read request to BMP chip via SPI channel
196-
this.csGpio.low();
197161
byteswritten = this.spi.write(buffer);
198-
this.csGpio.high();
199162

200163
this.logger.trace("<<< Exit writeRegister wrote : " + byteswritten);
201164
return (rval);

src/main/java/com/pi4j/devices/bmp280/BMP280SpiExample.java

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
11
/*
2+
* * #%L
3+
* * Copyright (C) 2012 - 2025 Pi4J
4+
* * %%
25
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
39
*
4-
* *
5-
* * -
6-
* * #%L
7-
* * **********************************************************************
8-
* * ORGANIZATION : Pi4J
9-
* * PROJECT : Pi4J :: EXTENSION
10-
* * FILENAME : BMP280SpiExample.java
11-
* *
12-
* * This file is part of the Pi4J project. More information about
13-
* * this project can be found here: https://pi4j.com/
14-
* * **********************************************************************
15-
* * %%
16-
* * * Copyright (C) 2012 - 2022 Pi4J
17-
* * %%
18-
* *
19-
* * Licensed under the Apache License, Version 2.0 (the "License");
20-
* * you may not use this file except in compliance with the License.
21-
* * You may obtain a copy of the License at
22-
* *
23-
* * http://www.apache.org/licenses/LICENSE-2.0
24-
* *
25-
* * Unless required by applicable law or agreed to in writing, software
26-
* * distributed under the License is distributed on an "AS IS" BASIS,
27-
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28-
* * See the License for the specific language governing permissions and
29-
* * limitations under the License.
30-
* * #L%
31-
* *
10+
* http://www.apache.org/licenses/LICENSE-2.0
3211
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
3317
*
18+
* -
19+
* #%L
20+
* **********************************************************************
21+
* ORGANIZATION : Pi4J
22+
* PROJECT : Pi4J :: EXTENSION
23+
* FILENAME : BMP280SpiExample.java
24+
*
25+
* This file is part of the Pi4J project. More information about
26+
* this project can be found here: https://pi4j.com/
27+
* **********************************************************************
28+
* %%
3429
*
3530
*/
3631

@@ -60,7 +55,6 @@ public static void main(String[] args) throws Exception {
6055
SpiChipSelect chipSelect = SpiChipSelect.CS_0;
6156
SpiBus spiBus = SpiBus.BUS_0;
6257

63-
int csPin = 21;
6458

6559

6660
// ------------------------------------------------------------
@@ -91,16 +85,12 @@ public static void main(String[] args) throws Exception {
9185
console.print("==============================================================");
9286

9387

94-
String helpString = " parms: -t trace -csp chipSelectGPIO \n " +
88+
String helpString = " parms: -t trace \n " +
9589
" \n trace values : \"trace\", \"debug\", \"info\", \"warn\", \"error\" or \"off\" Default \"info\"";
9690
String traceLevel = "info";
9791
for (int i = 0; i < args.length; i++) {
9892
String o = args[i];
99-
if (o.contentEquals("-csp")) { // device address
100-
String a = args[i + 1];
101-
i++;
102-
csPin = Integer.parseInt(a);
103-
} else if (o.contentEquals("-t")) { // device address
93+
if (o.contentEquals("-t")) { // device address
10494
String a = args[i + 1];
10595
i++;
10696
traceLevel = a;
@@ -120,7 +110,7 @@ public static void main(String[] args) throws Exception {
120110
}
121111
}
122112

123-
var bmpDev = new BMP280DeviceSPI(pi4j, console, spiBus, chipSelect, csPin, traceLevel);
113+
var bmpDev = new BMP280DeviceSPI(pi4j, console, spiBus, chipSelect, traceLevel);
124114

125115

126116
bmpDev.initSensor();

src/main/java/com/pi4j/devices/bmp280/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The I2C example program has the assumption the BMP280 is connected to Pi bus 1,
88
operate as device address 0x77. If these assumptions are not possible, the bus and device address value can be passed
99
as parms. Or modify the BMP280I2cExample program to use your specific bus and device address.
1010

11-
The SPI example assumes SPI bus 0, ChipSelect GPIO 21. The GPIO is configurable as a program parm option.
11+
The SPI example assumes SPI bus 0, ChipSelect 0.
1212

1313
The program uses org.slf4j.simple Class SimpleLogger as suggested by the Pi4j_V2 documentation. Consult that logger
1414
class documentation to understand its various logging options.
@@ -40,7 +40,7 @@ SPI connection path.
4040

4141
1. ./mvnw clean package
4242
2. cd target/distribution
43-
3. sudo ./runBMP280SPI.sh -csp 21
43+
3. sudo ./runBMP280SPI.sh
4444

4545
Use of different bus or device address, use the applicable values in the following command.
4646
sudo ./runBMP280I2C.sh -b 0x01 -a 0x77
@@ -68,13 +68,13 @@ I2C connection
6868

6969
SPI connection using 4-wire SPI
7070

71-
| RPi | Color | Sensor |
72-
|:-------------|:-------|:-------|
73-
| Pin19 MOSI | Blue | SDI |
74-
| Pin21 MISO | Yellow | SDO |
75-
| Pin23 SCLK | Green | SCK |
76-
| Pin40 GPIO21 | Orange | CS |
77-
| Pin6 Gnd | Brown | GND |
78-
| Pin1 3.3V | Red | Vin |
71+
| RPi | Color | Sensor |
72+
|:---------------|:-------|:-------|
73+
| Pin19 MOSI | Blue | SDI |
74+
| Pin21 MISO | Yellow | SDO |
75+
| Pin23 SCLK | Green | SCK |
76+
| Pin24 SPICE0 | Orange | CS |
77+
| Pin6 Gnd | Brown | GND |
78+
| Pin1 3.3V | Red | Vin |
7979

8080

0 commit comments

Comments
 (0)