diff --git a/.vscode/settings.json b/.vscode/settings.json index fa393981b857a5dbcae6300ee78bf8cd29a623cb..68d67b3caea60c5b380abb264f35df3c141156c2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -335,5 +335,6 @@ "Zyxda" ], "cSpell.language": "en", - "cSpell.enabled": true + "cSpell.enabled": true, + "C_Cpp.errorSquiggles": "enabled" } \ No newline at end of file diff --git a/src/shared/sensors/ND015X/ND015X.cpp b/src/shared/sensors/ND015X/ND015X.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99f4d0060ba630e665a17349514ec59f099505b7 --- /dev/null +++ b/src/shared/sensors/ND015X/ND015X.cpp @@ -0,0 +1,59 @@ +/* Copyright (c) 2025 Skyward Experimental Rocketry + * Author: Pietro Bortolus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "ND015X.h" + +#include <drivers/timer/TimestampTimer.h> + +namespace Boardcore +{ +ND015X::ND015X(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig) + : slave(bus, cs, spiConfig) +{ +} + +bool ND015X::init() { return true; } + +bool ND015X::selfTest() {} + +void ND015X::setOutputDataRate(OutputDataRate odr) +{ + SPIDataIn = (SPIDataIn & (255 << 8)) | odr; +} + +void ND015X::setFullScaleRange(FullScaleRange fs) +{ + SPIDataIn = (SPIDataIn & 255) | (fs << 8); +} + +ND015XData ND015X::sampleImpl() +{ + ND015XData data; + + SPITransaction spi(slave); + + SPIDataOut = spi.transfer16(SPIDataOut); + + return data; +} + +} // namespace Boardcore diff --git a/src/shared/sensors/ND015X/ND015X.h b/src/shared/sensors/ND015X/ND015X.h new file mode 100644 index 0000000000000000000000000000000000000000..9aaf401127ebb6e32e7198e803cabe039493268d --- /dev/null +++ b/src/shared/sensors/ND015X/ND015X.h @@ -0,0 +1,70 @@ +/* Copyright (c) 2025 Skyward Experimental Rocketry + * Author: Pietro Bortolus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <drivers/spi/SPIDriver.h> +#include <sensors/Sensor.h> + +#include "ND015XData.h" + +namespace Boardcore +{ + +class ND015X : public Sensor<ND015XData> +{ +public: + enum OutputDataRate : uint16_t + { + ODR_50 = 0x00, + ODR_100 = 0x08, + ODR_400 = 0x10, + ODR_1000 = 0x18, + }; + + enum FullScaleRange : uint8_t + { + FS_6 = 0x00, + FS_12 = 0x10, + FS_24 = 0x30, + }; + + ND015X(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig); + + bool init() override; + + bool selfTest() override; + + void setOutputDataRate(OutputDataRate odr); + + void setFullScaleRange(FullScaleRange fs); + +protected: + ND015XData sampleImpl() override; + +private: + SPISlave slave; + uint16_t SPIDataIn; + uint16_t SPIDataOut; +} + +} // namespace Boardcore diff --git a/src/shared/sensors/ND015X/ND015XData.h b/src/shared/sensors/ND015X/ND015XData.h new file mode 100644 index 0000000000000000000000000000000000000000..931e8f25e7ec5a92c53aa38a985a2c859407342d --- /dev/null +++ b/src/shared/sensors/ND015X/ND015XData.h @@ -0,0 +1,43 @@ +/* Copyright (c) 2025 Skyward Experimental Rocketry + * Author: Pietro Bortolus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <sensors/SensorData.h> + +namespace Boardcore +{ + +struct ND015XData : public PressureData +{ + uint64_t pressureTimestamp = 0; + float pressure = 0; + + static std::string header() { return "timestamp,pressure\n"; } + + void print(std::ostream& os) const + { + os << pressureTimestamp << "," << pressure << "\n"; + } +}; + +} // namespace Boardcore