-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
310 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// coding: utf-8 | ||
// ---------------------------------------------------------------------------- | ||
/* | ||
* Copyright (c) 2021, Raphael Lehmann | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#ifndef MODM_ADS816X_HPP | ||
#define MODM_ADS816X_HPP | ||
|
||
#include <array> | ||
#include <modm/architecture/interface/spi_device.hpp> | ||
#include <modm/architecture/interface/gpio.hpp> | ||
#include <modm/processing/resumable.hpp> | ||
#include <modm/processing/timer/timeout.hpp> | ||
|
||
namespace modm | ||
{ | ||
|
||
/// @ingroup modm_driver_ads816x | ||
struct ads816x | ||
{ | ||
enum class | ||
Register : uint16_t | ||
{ | ||
REG_ACCESS = 0x000, | ||
PD_CNTL = 0x004, | ||
SDI_CNTL = 0x008, | ||
SDO_CNTL1 = 0x00C, | ||
SDO_CNTL2 = 0x00D, | ||
SDO_CNTL3 = 0x00E, | ||
SDO_CNTL4 = 0x00F, | ||
DATA_CNTL = 0x010, | ||
PARITY_CNTL = 0x011, | ||
|
||
OFST_CAL = 0x018, | ||
REF_MRG1 = 0x019, | ||
REF_MRG2 = 0x01A, | ||
REFby2_MRG = 0x01B, | ||
|
||
AIN_CFG = 0x024, | ||
COM_CFG = 0x027, | ||
|
||
DEVICE_CFG = 0x01C, | ||
CHANNEL_ID = 0x01D, | ||
SEQ_START = 0x01E, | ||
SEQ_STOP = 0x01F, | ||
ON_THE_FLY_CFG = 0x02A, | ||
AUTO_SEQ_CFG1 = 0x080, | ||
AUTO_SEQ_CFG2 = 0x082, | ||
|
||
// Alert, etc... | ||
}; | ||
|
||
enum class | ||
Command : uint8_t | ||
{ | ||
NoOperation = 0b00000 << 3, | ||
Write = 0b00001 << 3, | ||
Read = 0b00010 << 3, | ||
SetBits = 0b00011 << 3, | ||
ClearBits = 0b00100 << 3, | ||
}; | ||
|
||
enum class | ||
Mode : uint8_t | ||
{ | ||
Manual = 0b00, | ||
OnTheFly = 0b01, | ||
AutoSequence = 0b10, | ||
CustomSequence = 0b11, | ||
}; | ||
}; | ||
|
||
/** | ||
* @tparam SpiMaster SpiMaster interface | ||
* @tparam Cs Chip-select pin | ||
* | ||
* @author Raphael Lehmann | ||
* @ingroup modm_driver_ads816x | ||
*/ | ||
template <typename SpiMaster, typename Cs> | ||
class Ads816x : public ads816x, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<3> | ||
{ | ||
public: | ||
Ads816x() = default; | ||
|
||
/// Call this function before using the device or to change operation mode | ||
modm::ResumableResult<void> | ||
initialize(Mode mode); | ||
|
||
/// Initiate a single conversion and return the result of the conversion | ||
/// Simultanously the channel for the next conversion will be set | ||
modm::ResumableResult<uint16_t> | ||
manualModeConversion(uint8_t nextChannel); | ||
|
||
/// Start a "auto sequence" conversion. | ||
/// The device will automatically cycle through the specified channels in | ||
/// the bitmask. | ||
modm::ResumableResult<void> | ||
autoSequenceConversion(uint8_t channelsBitmask, std::array<uint16_t, 8>& result); | ||
|
||
private: | ||
modm::ResumableResult<uint8_t> | ||
registerAccess(Command command, Register reg, uint8_t value = 0); | ||
|
||
uint8_t buffer[3]; | ||
uint8_t buffer2[3]; | ||
|
||
modm::ShortPreciseTimeout timeout; | ||
}; | ||
|
||
} // namespace modm | ||
|
||
#include "ads816x_impl.hpp" | ||
|
||
#endif // MODM_ADS816X_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021, Raphael Lehmann | ||
# | ||
# This file is part of the modm project. | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def init(module): | ||
module.name = ":driver:ads816x" | ||
module.description = """\ | ||
# ADS816x ADC | ||
The ADS8166/ADS8167/ADS8168 are, 16 bit analog-digital converters with, sample | ||
rates of 1 MSPS (ADS8168), 500 kSPS (ADS8167), and 250 kSPS (ADS8166). | ||
The conversion time is determined by the chip select signal. | ||
A maximum Spi clock of 70 MHz is supported. | ||
This driver only implements the _manual mode_ and _auto sequence mode_ using | ||
the SPI interface. | ||
""" | ||
|
||
|
||
def prepare(module, options): | ||
module.depends( | ||
":architecture:gpio", | ||
":architecture:spi.device", | ||
":io", | ||
":processing:resumable", | ||
":processing:timer") | ||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/driver/adc" | ||
env.copy("ads816x.hpp") | ||
env.copy("ads816x_impl.hpp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// coding: utf-8 | ||
// ---------------------------------------------------------------------------- | ||
/* | ||
* Copyright (c) 2021, Raphael Lehmann | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#ifndef MODM_ADS816X_HPP | ||
# error "Don't include this file directly! Use 'ads816x.hpp' instead." | ||
#endif | ||
|
||
#include <bit> | ||
|
||
namespace modm | ||
{ | ||
|
||
template <typename SpiMaster, typename Cs> | ||
ResumableResult<void> | ||
Ads816x<SpiMaster, Cs>::initialize(Mode mode) | ||
{ | ||
RF_BEGIN(); | ||
|
||
// Unlock register access | ||
RF_CALL(registerAccess(Command::Write, Register::REG_ACCESS, 0b1010'1010)); | ||
|
||
timeout.restart(4us); | ||
RF_WAIT_UNTIL(timeout.isExpired()); | ||
|
||
// Set mode | ||
RF_CALL(registerAccess(Command::Write, Register::DEVICE_CFG, uint8_t(mode))); | ||
|
||
RF_END(); | ||
} | ||
|
||
template <typename SpiMaster, typename Cs> | ||
ResumableResult<uint16_t> | ||
Ads816x<SpiMaster, Cs>::manualModeConversion(uint8_t nextChannel) | ||
{ | ||
RF_BEGIN(); | ||
|
||
RF_WAIT_UNTIL(this->acquireMaster()); | ||
Cs::reset(); | ||
|
||
buffer[0] = uint8_t(Command::Write) | (uint16_t(Register::CHANNEL_ID) >> 8); | ||
buffer[1] = uint16_t(Register::CHANNEL_ID); | ||
buffer[2] = (nextChannel & 0b0000'0111); | ||
|
||
RF_CALL(SpiMaster::transfer(buffer, buffer2, 3)); | ||
|
||
if (this->releaseMaster()) { | ||
Cs::set(); | ||
} | ||
|
||
RF_END_RETURN( static_cast<uint16_t>((static_cast<uint16_t>(buffer2[0]) << 8) | buffer2[1]) ); | ||
} | ||
|
||
template <typename SpiMaster, typename Cs> | ||
ResumableResult<void> | ||
Ads816x<SpiMaster, Cs>::autoSequenceConversion(uint8_t channelsBitmask, std::array<uint16_t, 8>& result) | ||
{ | ||
using namespace std::chrono_literals; | ||
RF_BEGIN(); | ||
|
||
// Write channel bitmask config (AUTO_SEQ_CH) | ||
RF_CALL(registerAccess(Command::Write, Register::AUTO_SEQ_CFG1, channelsBitmask)); | ||
|
||
timeout.restart(4us); | ||
RF_WAIT_UNTIL(timeout.isExpired()); | ||
|
||
// Set SEQ_START (0b1 << 0) bit | ||
RF_CALL(registerAccess(Command::SetBits, Register::SEQ_START, 0b1)); | ||
|
||
// Read conversion results | ||
for (buffer2[0] = 0; buffer2[0] < std::popcount(channelsBitmask); buffer2[0]++) { | ||
timeout.restart(4us); | ||
RF_WAIT_UNTIL(timeout.isExpired()); | ||
|
||
RF_WAIT_UNTIL(this->acquireMaster()); | ||
Cs::reset(); | ||
|
||
RF_CALL(SpiMaster::transfer(nullptr, buffer, 2)); | ||
|
||
if (this->releaseMaster()) { | ||
Cs::set(); | ||
} | ||
|
||
result[buffer2[0]] = buffer[0] << 8 | buffer[1]; | ||
} | ||
|
||
RF_END(); | ||
} | ||
|
||
template <typename SpiMaster, typename Cs> | ||
modm::ResumableResult<uint8_t> | ||
Ads816x<SpiMaster, Cs>::registerAccess(Command command, Register reg, uint8_t value) | ||
{ | ||
using namespace std::chrono_literals; | ||
RF_BEGIN(); | ||
|
||
buffer[0] = uint8_t(command) | (uint16_t(reg) >> 8); | ||
buffer[1] = uint16_t(reg); | ||
buffer[2] = value; | ||
|
||
RF_WAIT_UNTIL(this->acquireMaster()); | ||
Cs::reset(); | ||
|
||
RF_CALL(SpiMaster::transfer(buffer, nullptr, 3)); | ||
|
||
if (command == Command::Read) { | ||
Cs::set(); | ||
timeout.restart(4us); | ||
RF_WAIT_UNTIL(timeout.isExpired()); | ||
|
||
// To retrieve the result of the read command we issue another 3 byte | ||
// transfer, nullptr instead of manual NoOperation (= 0b000...) command | ||
Cs::reset(); | ||
RF_CALL(SpiMaster::transfer(nullptr, buffer, 3)); | ||
} | ||
|
||
if (this->releaseMaster()) { | ||
Cs::set(); | ||
} | ||
|
||
RF_END_RETURN(buffer[0]); | ||
} | ||
|
||
} // namespace modm |