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
3732package com .pi4j .devices .bmp280 ;
3833
3934import com .pi4j .context .Context ;
40- import com .pi4j .io .gpio .digital .DigitalOutput ;
41- import com .pi4j .io .gpio .digital .DigitalState ;
4235import com .pi4j .io .spi .*;
4336import com .pi4j .util .Console ;
4437import 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 );
0 commit comments