Skip to content

Commit 09095e5

Browse files
committed
Refactor to use PIO-specific driver on RP2040 platforms, rather than bitbang it
clean up max 7 clang
1 parent ca568b3 commit 09095e5

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

src/components/ds18x20/controller.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919
@brief DS18X20Controller constructor
2020
*/
2121
/***********************************************************************/
22-
DS18X20Controller::DS18X20Controller() { _DS18X20_model = new DS18X20Model(); }
22+
DS18X20Controller::DS18X20Controller() {
23+
_num_drivers = 0;
24+
_DS18X20_model = new DS18X20Model();
25+
}
2326

2427
/***********************************************************************/
2528
/*!
2629
@brief DS18X20Controller destructor
2730
*/
2831
/***********************************************************************/
29-
DS18X20Controller::~DS18X20Controller() { delete _DS18X20_model; }
32+
DS18X20Controller::~DS18X20Controller() {
33+
_num_drivers = 0;
34+
delete _DS18X20_model;
35+
}
3036

3137
/***********************************************************************/
3238
/*!
@@ -59,7 +65,8 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
5965
uint8_t pin_name = atoi(_DS18X20_model->GetDS18x20AddMsg()->onewire_pin + 1);
6066

6167
// Initialize the DS18X20Hardware object
62-
auto new_dsx_driver = std::make_unique<DS18X20Hardware>(pin_name);
68+
auto new_dsx_driver =
69+
std::make_unique<DS18X20Hardware>(pin_name, _num_drivers);
6370
// Attempt to get the sensor's ID on the OneWire bus to show it's been init'd
6471
bool is_initialized = new_dsx_driver->GetSensor();
6572

@@ -95,8 +102,10 @@ bool DS18X20Controller::Handle_Ds18x20Add(pb_istream_t *stream) {
95102
}
96103

97104
// If the sensor was successfully initialized, add it to the controller
98-
if (is_initialized == true)
105+
if (is_initialized == true) {
99106
_DS18X20_pins.push_back(std::move(new_dsx_driver));
107+
_num_drivers++;
108+
}
100109

101110
// Print out the details
102111
WS_DEBUG_PRINTLN("[ds18x] New Sensor Added!");
@@ -172,6 +181,7 @@ bool DS18X20Controller::Handle_Ds18x20Remove(pb_istream_t *stream) {
172181
}
173182
}
174183
WS_DEBUG_PRINTLN("Removed!");
184+
_num_drivers--;
175185
return true;
176186
}
177187

src/components/ds18x20/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DS18X20Controller {
4141
private:
4242
DS18X20Model *_DS18X20_model; ///< ds18x20 model
4343
std::vector<std::unique_ptr<DS18X20Hardware>> _DS18X20_pins;
44+
int _num_drivers;
4445
};
4546
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
4647
#endif // WS_DS18X20_CONTROLLER_H

src/components/ds18x20/hardware.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
@brief DS18X20Hardware constructor
2020
@param onewire_pin
2121
The OneWire bus pin to use.
22+
@param sensor_num
23+
Unique identifier for the sensor driver.
2224
*/
2325
/***********************************************************************/
24-
DS18X20Hardware::DS18X20Hardware(uint8_t onewire_pin) : _drv_therm(_ow) {
26+
DS18X20Hardware::DS18X20Hardware(uint8_t onewire_pin, int sensor_num)
27+
: _drv_therm(_ow) {
2528
is_read_temp_c = false;
2629
is_read_temp_f = false;
30+
_sensor_num = sensor_num;
2731
// Initialize the OneWire bus object
2832
_onewire_pin = onewire_pin;
29-
new (&_ow) OneWireNg_CurrentPlatform(onewire_pin, false);
3033
}
3134

3235
/***********************************************************************/
@@ -39,13 +42,40 @@ DS18X20Hardware::~DS18X20Hardware() {
3942
INPUT); // Set the pin to hi-z and release it for other uses
4043
}
4144

42-
/***********************************************************************/
45+
/****************************************************************************/
4346
/*!
44-
@brief Get the sensor's ID
45-
@returns True if the sensor was successfully identified, False otherwise.
47+
@brief Initializes the DS18X20 sensor driver and verifies that the
48+
sensor is present on the OneWire bus.
49+
@returns True if the sensor was successfully initialized, False
50+
otherwise.
4651
*/
47-
/***********************************************************************/
52+
/****************************************************************************/
4853
bool DS18X20Hardware::GetSensor() {
54+
// Initialize the DS18X20 driver
55+
#ifdef ARDUINO_ARCH_RP2040
56+
// RP2040 is special - it uses the PIO's rather than the standard bitbang
57+
// implementation In this implementation, we select the PIO instance based on
58+
// driver identifier
59+
60+
// NOTE: We are only going to allow *up to* 7 DS18x20 sensors on RP2040
61+
// because the status pixel requires a PIO SM to be allocated prior.
62+
int pio_num;
63+
if (_sensor_num <= 4) {
64+
// drivers 0 thru 3 are handled by PIO0/SM0..4
65+
new (&_ow) OneWireNg_CurrentPlatform(_onewire_pin, false, 0);
66+
} else if (_sensor_num <= 7) {
67+
// drivers 5 thru 8 are handled by PIO1/SM1..4
68+
new (&_ow) OneWireNg_CurrentPlatform(_onewire_pin, false, 1);
69+
} else {
70+
WS_DEBUG_PRINTLN(
71+
"[ds18x20] ERROR: You may only add up to 7 sensors on RP2040!");
72+
return false;
73+
}
74+
#else
75+
// Initialize the standard bit-banged DS18X20 driver object
76+
new (&_ow) OneWireNg_CurrentPlatform(_onewire_pin, false);
77+
#endif
78+
4979
OneWireNg::ErrorCode ec = _ow->readSingleId(_sensorId);
5080
return ec == OneWireNg::EC_SUCCESS;
5181
}

src/components/ds18x20/hardware.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#include "drivers/DSTherm.h"
2121
#include "utils/Placeholder.h"
2222

23+
#if defined(ARDUINO_ARCH_RP2040)
24+
#define CONFIG_RP2040_PIO_DRIVER ///< Enable the OneWireNg_PicoRP2040PIO driver
25+
#define CONFIG_RP2040_PIOSM_NUM_USED \
26+
1 ///< 4 drivers handled by PIO1, 4 drivers handled by PIO2
27+
#endif
28+
2329
/**************************************************************************/
2430
/*!
2531
@brief Interface for interacting with the's DallasTemp
@@ -28,7 +34,7 @@
2834
/**************************************************************************/
2935
class DS18X20Hardware {
3036
public:
31-
DS18X20Hardware(uint8_t onewire_pin);
37+
DS18X20Hardware(uint8_t onewire_pin, int sensor_num);
3238
~DS18X20Hardware();
3339
uint8_t GetOneWirePin();
3440
void SetResolution(int resolution);
@@ -55,6 +61,7 @@ class DS18X20Hardware {
5561
uint8_t
5662
_onewire_pin; ///< Pin utilized by the OneWire bus, used for addressing
5763
char _onewire_pin_name[5]; ///< Name of the OneWire bus pin
64+
int _sensor_num;
5865
float _period; ///< The desired period to read the sensor, in seconds
5966
float _prv_period; ///< Last time the sensor was polled, in seconds
6067
};

0 commit comments

Comments
 (0)