Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCP3008 SPI ADC driver #1028

Merged
merged 2 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -770,45 +770,47 @@ you specific needs.
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp3008">MCP3008</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5837">MS5837</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5837">MS5837</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
</tr><tr>
</tr>
</table>
<!--/drivertable-->
Expand Down
58 changes: 58 additions & 0 deletions examples/samv71_xplained_ultra/mcp3008/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023, Christopher Durand
*
* 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/adc/mcp3008.hpp>
#include <utility>

using namespace Board;
using namespace modm::platform;

using SpiMaster = SpiMaster0;
using Sck = GpioD22;
using Mosi = GpioD21;
using Miso = GpioD20;
using Cs = GpioD25;

int main()
{
Board::initialize();

MODM_LOG_INFO << "MCP3004/8 ADC example" << modm::endl;

SpiMaster::connect<Sck::Spck, Mosi::Mosi, Miso::Miso>();
Cs::setOutput(true);

SpiMaster::initialize<SystemClock, 1500_kHz, 10_pct>();

modm::Mcp3008<SpiMaster, Cs> adc;
adc.initialize();

constexpr std::array channels = {
std::make_pair(0, modm::mcp3008::Channel::Ch0),
std::make_pair(1, modm::mcp3008::Channel::Ch1),
std::make_pair(2, modm::mcp3008::Channel::Ch2),
std::make_pair(3, modm::mcp3008::Channel::Ch3)
};

while (true) {
for (auto [i, ch] : channels) {
const auto value = RF_CALL_BLOCKING(adc.read(ch));
MODM_LOG_INFO << "channel " << i << ": " << value << '\n';
}

Led0::toggle();
Led1::toggle();
modm::delay(1s);
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/samv71_xplained_ultra/mcp3008/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:samv71-xplained-ultra</extends>
<options>
<option name="modm:build:build.path">../../../build/samv71_xplained_ultra/mcp3008</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:driver:mcp3008</module>
<module>modm:platform:spi:0</module>
</modules>
</library>
80 changes: 80 additions & 0 deletions src/modm/driver/adc/mcp3008.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2023, Christopher Durand
*
* 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_MCP3008_HPP
#define MODM_MCP3008_HPP

#include <array>
#include <modm/architecture/interface/spi_device.hpp>
#include <modm/processing/resumable.hpp>

namespace modm
{

/// @ingroup modm_driver_mcp3008
struct mcp3008
{
/**
* ADC channels
* For MCP3004 only channels 0-3 are valid.
*/
enum class
Channel : uint8_t
{
Ch0 = 0b1000,
Ch1 = 0b1001,
Ch2 = 0b1010,
Ch3 = 0b1011,
Ch4 = 0b1100,
Ch5 = 0b1101,
Ch6 = 0b1110,
Ch7 = 0b1111,
Ch0Ch1Diff = 0b0000,
Ch1Ch0Diff = 0b0001,
Ch2Ch3Diff = 0b0010,
Ch3Ch2Diff = 0b0011,
Ch4Ch5Diff = 0b0100,
Ch5Ch4Diff = 0b0101,
Ch6Ch7Diff = 0b0110,
Ch7Ch6Diff = 0b0111
};
};

/**
* Simple driver for MCP3004/MCP3008 10-bit SAR SPI ADCs.
* Max SPI frequency 3.6 MHz at 5V, 1.36 MHz at 2.7V.
*
* @author Christopher Durand
* @ingroup modm_driver_mcp3008
*/
template <typename SpiMaster, typename Cs>
class Mcp3008 : public mcp3008, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<1>
{
public:
Mcp3008() = default;

/// Call initialize() before reading ADC
void initialize();

/// Read ADC channel
modm::ResumableResult<uint16_t>
read(Channel channel);

private:
uint8_t rxBuffer_[3]{};
uint8_t txBuffer_[3]{1, 0, 0};
};

} // namespace modm

#include "mcp3008_impl.hpp"

#endif // MODM_MCP3008_HPP
30 changes: 30 additions & 0 deletions src/modm/driver/adc/mcp3008.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022, Christopher Durand
#
# 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:mcp3008"
module.description = """\
# MCP3004/MCP3008 ADC
MCP3004/3008 are 10-bit 200 ksps SAR ADCs with SPI interface and 4/8 channels.
"""

def prepare(module, options):
module.depends(
":architecture:spi.device",
":processing:resumable")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/adc"
env.copy("mcp3008.hpp")
env.copy("mcp3008_impl.hpp")
50 changes: 50 additions & 0 deletions src/modm/driver/adc/mcp3008_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, Christopher Durand
*
* 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_MCP3008_HPP
#error "Don't include this file directly, use 'mcp3008.hpp' instead!"
#endif

namespace modm
{

template <typename SpiMaster, typename Cs>
void
Mcp3008<SpiMaster, Cs>::initialize()
{
this->attachConfigurationHandler([]() {
SpiMaster::setDataMode(SpiMaster::DataMode::Mode0);
});

Cs::setOutput(true);
}

template <typename SpiMaster, typename Cs>
modm::ResumableResult<uint16_t>
Mcp3008<SpiMaster, Cs>::read(Channel channel)
{
RF_BEGIN();

RF_WAIT_UNTIL(this->acquireMaster());
Cs::reset();

txBuffer_[1] = static_cast<uint8_t>(channel) << 4;

RF_CALL(SpiMaster::transfer(txBuffer_, rxBuffer_, 3));

if (this->releaseMaster()) {
Cs::set();
}

RF_END_RETURN(uint16_t(((rxBuffer_[1] & 0b11) << 8) | rxBuffer_[2]));
}

} // namespace modm