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

[stm32] Add comparator platform driver for f3 and l4 #41

Merged
merged 3 commits into from
Jul 17, 2018
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
49 changes: 49 additions & 0 deletions examples/nucleo_l432kc/comp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2018, 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>

#include <modm/platform/comp/comp_1.hpp>

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

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
MODM_LOG_DEBUG << "debug" << modm::endl;
MODM_LOG_INFO << "info" << modm::endl;
MODM_LOG_WARNING << "warning" << modm::endl;
MODM_LOG_ERROR << "error" << modm::endl;

using Comparator = modm::platform::Comp1;

Comparator::connect<GpioA0::Out, GpioA1::Inp>();

Comparator::initialize(
Comparator::InvertingInput::Vref1Div2,
Comparator::NonInvertingInput::GpioA1,
Comparator::Hysteresis::NoHysteresis,
Comparator::Mode::HighSpeed,
Comparator::Polarity::NonInverted,
false);

while (1)
{
modm::delayMilliseconds(250);
MODM_LOG_INFO << "Comparator: " << Comparator::getOutput() << modm::endl;
Board::LedD13::set(Comparator::getOutput());
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_l432kc/comp/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8'?>
<library>
<extends>../../../src/modm/board/nucleo_l432kc/board.xml</extends>
<options>
<option name=":build.scons:build.path">../../../build/nucleo_l432kc/blink</option>
</options>
<modules>
<module>modm:platform:comp:*</module>
<module>:build.scons</module>
</modules>
</library>
68 changes: 68 additions & 0 deletions examples/stm32f3_discovery/comp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2018, 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/debug/logger.hpp>

#include <modm/platform/comp/comp_2.hpp>

// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;

// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);

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

// initialize Uart2 for MODM_LOG_
Usart2::connect<GpioOutputA2::Tx>();
Usart2::initialize<Board::systemClock, 115200>();

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
MODM_LOG_DEBUG << "debug" << modm::endl;
MODM_LOG_INFO << "info" << modm::endl;
MODM_LOG_WARNING << "warning" << modm::endl;
MODM_LOG_ERROR << "error" << modm::endl;

using Comparator = modm::platform::Comp2;

Comparator::connect<GpioA7::Inp, GpioA2::Out>();

Comparator::initialize(
Comparator::InvertingInput::Vref1Div2,
Comparator::NonInvertingInput::BitUnset, // GpioA7
Comparator::Output::Tim1Or8BkIn2,
Comparator::Hysteresis::NoHysteresis,
Comparator::Mode::HighSpeed,
Comparator::Polarity::NonInverted,
false);

while (1)
{
modm::delayMilliseconds(250);
MODM_LOG_INFO << "Comparator: " << Comparator::getOutput() << modm::endl;
Board::LedNorth::set(Comparator::getOutput());
}

return 0;
}
13 changes: 13 additions & 0 deletions examples/stm32f3_discovery/comp/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<library>
<extends>../../../src/modm/board/disco_f303vc/board.xml</extends>
<options>
<option name=":build.scons:build.path">../../../build/stm32f3_discovery/blink</option>
</options>
<modules>
<module>:debug</module>
<module>:platform:uart:2</module>
<module>modm:platform:comp:*</module>
<module>:build.scons</module>
</modules>
</library>
66 changes: 66 additions & 0 deletions src/modm/platform/comp/stm32/base.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2018, 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_STM32_COMP_BASE_HPP
#define MODM_STM32_COMP_BASE_HPP

/**
* @ingroup {{partname}}
* @defgroup {{partname}}_comp Comparator
*/
namespace modm::platform
{
class CompBase
{
public:
enum class
Mode
{
{% if driver.type in ["stm32-v1.3"] -%}
UltraLowPower = 0b00 << 2,
LowPower = 0b01 << 2,
MediumSpeed = 0b10 << 2,
HighSpeed = 0b11 << 2,
{% elif driver.type in ["stm32-tsmc90_cube"] -%}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be renamed in this file:
https://github.com/modm-io/modm-devices/blob/develop/tools/generator/dfg/stm32/stm_peripherals.py

Would it be a lot of work to check the binary compatiblity of all STM32 Comp devices and add them too?
You'd need to compare the peripheral memory map overview of all devices with this functionality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how the different comparator IPs should be grouped, so I don't think a renaming makes sense at the moment:

  • stm32-tsmc90_cube
  • stm32-tsmc90_h7_cube
  • stm32-tsmc90_orca512_cube
  • stm32-v1.0
  • stm32-v1.2
  • stm32-v1.3
  • stm32-v1.4
  • stm32-v3.4
  • stm32-v3.6

I don't think it makes sense to compare the memory maps. I prefer to wait a few weeks and compare the data of the register descriptions from the PDF data sheets.

The memory maps of L43x and F303/F3x8 look very similar, but some bits have other meaning: On L43x the HighSpeed mode is 0b00, on F303/F3x8 the same bit have to be 0b11 to enable the HighSpeed mode 😒

Data about the configurations of the input- or output- selection (etc.) bits is not present in the memory maps and CMSIS header files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to wait a few weeks and compare the data of the register descriptions from the PDF data sheets.

Someone is optimistic 😳

HighSpeed = 0b00 << 2,
MediumSpeed = 0b01 << 2,
//MediumSpeed2 = 0b10 << 2,
UltraLowPower = 0b11 << 2,
{% endif -%}
};
protected:
static constexpr uint32_t ModeMask = 0b11 << 2;

public:
enum class
Polarity
{
NonInverted = 0b0 << 15,
Inverted = 0b1 << 15,
};
protected:
static constexpr uint32_t PolarityMask = 0b1 << 15;

public:
enum class
Hysteresis
{
NoHysteresis = 0b00 << 16,
LowHysteresis = 0b01 << 16,
MediumHysteresis = 0b10 << 16,
HighHysteresis = 0b11 << 16,
};
protected:
static constexpr uint32_t HysteresisMask = 0b11 << 16;
};
}

#endif // MODM_STM32_COMP_BASE_HPP
Loading