From 63ec6d3eae3b18019174133a7d2f005882ce24e8 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 22 Feb 2022 13:39:04 +0100 Subject: [PATCH] add max1227 module --- mission/devices/CMakeLists.txt | 37 ++++++++------- mission/devices/max1227.cpp | 28 ++++++++++++ mission/devices/max1227.h | 84 ++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 19 deletions(-) create mode 100644 mission/devices/max1227.cpp create mode 100644 mission/devices/max1227.h diff --git a/mission/devices/CMakeLists.txt b/mission/devices/CMakeLists.txt index 3f5b5073..680cc64c 100644 --- a/mission/devices/CMakeLists.txt +++ b/mission/devices/CMakeLists.txt @@ -1,21 +1,20 @@ target_sources(${LIB_EIVE_MISSION} PRIVATE - GPSHyperionLinuxController.cpp - GomspaceDeviceHandler.cpp - BpxBatteryHandler.cpp - Tmp1075Handler.cpp - PCDUHandler.cpp - P60DockHandler.cpp - PDU1Handler.cpp - PDU2Handler.cpp - ACUHandler.cpp - SyrlinksHkHandler.cpp - Max31865PT1000Handler.cpp - IMTQHandler.cpp - HeaterHandler.cpp - PlocMPSoCHandler.cpp - RadiationSensorHandler.cpp - GyroADIS1650XHandler.cpp - RwHandler.cpp + GPSHyperionLinuxController.cpp + GomspaceDeviceHandler.cpp + BpxBatteryHandler.cpp + Tmp1075Handler.cpp + PCDUHandler.cpp + P60DockHandler.cpp + PDU1Handler.cpp + PDU2Handler.cpp + ACUHandler.cpp + SyrlinksHkHandler.cpp + Max31865PT1000Handler.cpp + IMTQHandler.cpp + HeaterHandler.cpp + PlocMPSoCHandler.cpp + RadiationSensorHandler.cpp + GyroADIS1650XHandler.cpp + RwHandler.cpp + max1227.cpp ) - - diff --git a/mission/devices/max1227.cpp b/mission/devices/max1227.cpp new file mode 100644 index 00000000..f89e161e --- /dev/null +++ b/mission/devices/max1227.cpp @@ -0,0 +1,28 @@ +#include "max1227.h" + +#include + +void max1227::prepareExternallyClockedSingleChannelRead(uint8_t *spiBuf, uint8_t channel, + size_t &sz) { + spiBuf[0] = buildConvByte(ScanModes::N_ONCE, channel, false); + spiBuf[1] = 0x00; + spiBuf[2] = 0x00; + sz = 3; +} + +void max1227::prepareExternallyClockedRead0ToN(uint8_t *spiBuf, uint8_t n, size_t &sz) { + for (uint8_t idx = 0; idx <= n; idx++) { + spiBuf[idx * 2] = buildConvByte(ScanModes::N_ONCE, idx, false); + spiBuf[idx * 2 + 1] = 0x00; + } + spiBuf[(n + 1) * 2] = 0x00; + sz += (n + 1) * 2 + 1; +} + +void max1227::prepareExternallyClockedTemperatureRead(uint8_t *spiBuf, size_t &sz) { + spiBuf[0] = buildConvByte(ScanModes::N_ONCE, 0, true); + std::memset(spiBuf + 1, 0, 24); + sz += 25; +} + +float max1227::getTemperature(int16_t temp) { return static_cast(temp) * 0.125; } diff --git a/mission/devices/max1227.h b/mission/devices/max1227.h new file mode 100644 index 00000000..1a6082fb --- /dev/null +++ b/mission/devices/max1227.h @@ -0,0 +1,84 @@ +#ifndef MISSION_DEVICES_MAX1227_H_ +#define MISSION_DEVICES_MAX1227_H_ + +#include +#include + +namespace max1227 { + +enum ScanModes : uint8_t { + CHANNELS_0_TO_N = 0b00, + CHANNEL_N_TO_HIGHEST = 0b01, + N_REPEATEDLY = 0b10, + N_ONCE = 0b11 +}; + +enum ClkSel : uint8_t { + INT_CONV_INT_TIMED_CNVST_AS_CNVST = 0b00, + INT_CONV_EXT_TIMED_CNVST = 0b01, + // Default mode upon power-up + INT_CONV_INT_TIMED_CNVST_AS_AIN = 0b10, + // Use external SPI clock for conversion and timing + EXT_CONV_EXT_TIMED = 0b11 +}; + +enum RefSel : uint8_t { + INT_REF_WITH_WAKEUP = 0b00, + // No wakeup delay needed + EXT_REF_SINGLE_ENDED = 0b01, + INT_REF_NO_WAKEUP = 0b10, + // No wakeup delay needed + EXT_REF_DIFFERENTIAL = 0b11 +}; + +enum DiffSel : uint8_t { + NONE_0 = 0b00, + NONE_1 = 0b01, + // One unipolar config byte follows the setup byte + UNIPOLAR_CFG = 0b10, + // One bipolar config byte follows the setup byte + BIPOLAR_CFG = 0b11 +}; + +constexpr uint8_t buildResetByte(bool fifoOnly) { return (1 << 4) | (fifoOnly << 3); } + +constexpr uint8_t buildConvByte(ScanModes scanMode, uint8_t channel, bool readTemp) { + return (1 << 7) | (channel << 3) | (scanMode << 1) | readTemp; +} + +constexpr uint8_t buildSetupByte(ClkSel clkSel, RefSel refSel, DiffSel diffSel) { + return (1 << 6) | (clkSel << 4) | (refSel << 2) | diffSel; +} + +/** + * If there is a wakeup delay, there needs to be a 65 us delay between sending + * the first byte (conversion byte) and the the rest of the SPI buffer. + * The raw ADC value will be located in the first and second reply byte. + * @param spiBuf + * @param n + * @param sz + */ +void prepareExternallyClockedSingleChannelRead(uint8_t* spiBuf, uint8_t channel, size_t& sz); + +/** + * If there is a wakeup delay, there needs to be a 65 us delay between sending + * the first byte (first conversion byte) the the rest of the SPI buffer. + * @param spiBuf + * @param n Channel number. Example: If the ADC has 6 channels, n will be 5 + * @param sz + */ +void prepareExternallyClockedRead0ToN(uint8_t* spiBuf, uint8_t n, size_t& sz); + +/** + * Prepare an externally clocked temperature read. 25 bytes have to be sent + * and the raw temperature value will appear on the last 2 bytes of the reply. + * @param spiBuf + * @param sz + */ +void prepareExternallyClockedTemperatureRead(uint8_t* spiBuf, size_t& sz); + +float getTemperature(int16_t temp); + +} // namespace max1227 + +#endif /* MISSION_DEVICES_MAX1227_H_ */