Skip to content

Commit

Permalink
[platform] DAC: Extend stm32 driver for stm32g4
Browse files Browse the repository at this point in the history
  • Loading branch information
rleh committed Oct 6, 2019
1 parent 97bed1d commit fb52706
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 24 deletions.
26 changes: 26 additions & 0 deletions src/modm/platform/comp/stm32/base.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
#ifndef MODM_STM32_COMP_BASE_HPP
#define MODM_STM32_COMP_BASE_HPP

#include <modm/platform/device.hpp>

namespace modm::platform
{
/// @ingroup modm_platform_comp
class CompBase
{
{% if driver.type not in ["stm32-tsmc90_g4_rockfish_cube"] -%}
public:
enum class
Mode
Expand All @@ -36,6 +38,7 @@ namespace modm::platform
};
protected:
static constexpr uint32_t ModeMask = 0b11 << 2;
{% endif -%}

public:
enum class
Expand All @@ -47,6 +50,7 @@ namespace modm::platform
protected:
static constexpr uint32_t PolarityMask = 0b1 << 15;

{% if driver.type in ["stm32-v1.3", "stm32-tsmc90_cube"] -%}
public:
enum class
Hysteresis
Expand All @@ -58,6 +62,28 @@ namespace modm::platform
};
protected:
static constexpr uint32_t HysteresisMask = 0b11 << 16;
{% elif driver.type in ["stm32-tsmc90_g4_rockfish_cube"] -%}
public:
enum class
Hysteresis
{
NoHysteresis = 0b000 << COMP_CSR_HYST_Pos,
Hysteresis10mV = 0b001 << COMP_CSR_HYST_Pos,
Hysteresis20mV = 0b010 << COMP_CSR_HYST_Pos,
Hysteresis30mV = 0b011 << COMP_CSR_HYST_Pos,
Hysteresis40mV = 0b100 << COMP_CSR_HYST_Pos,
Hysteresis50mV = 0b101 << COMP_CSR_HYST_Pos,
Hysteresis60mV = 0b110 << COMP_CSR_HYST_Pos,
Hysteresis70mV = 0b111 << COMP_CSR_HYST_Pos,

// for compatibility:
LowHysteresis = Hysteresis10mV,
MediumHysteresis = Hysteresis40mV,
HighHysteresis = Hysteresis70mV,
};
protected:
static constexpr uint32_t HysteresisMask = COMP_CSR_HYST_Msk;
{% endif -%}
};
}

Expand Down
109 changes: 88 additions & 21 deletions src/modm/platform/comp/stm32/comp.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace modm::platform
Vref1Div4 = (0b000 << 4) | (0b11 << 22),
Vref1Div2 = (0b001 << 4) | (0b11 << 22),
Vref3Div4 = (0b010 << 4) | (0b11 << 22),
Vref = 0b011 << 4,
Vref = (0b011 << 4) | (0b10 << 22),
DacChannel1 = 0b100 << 4,
DacChannel2 = 0b101 << 4,
{% if id == 1 -%}
Expand All @@ -67,13 +67,56 @@ namespace modm::platform
{% endif -%}
GpioA4 = (0b111 << 4) | (0b10 << 25),
GpioA5 = (0b111 << 4) | (0b11 << 25),
{% elif target.family == "g4" -%}
Vref1Div4 = (0b000 << 4) | (0b11 << 22),
Vref1Div2 = (0b001 << 4) | (0b11 << 22),
Vref3Div4 = (0b010 << 4) | (0b11 << 22),
Vref = (0b011 << 4) | (0b10 << 22),
{% if id == 1 -%}
Dac3Ch1 = 0b100 << 4,
Dac1Ch1 = 0b101 << 4,
GpioA4 = 0b110 << 4,
GpioA0 = 0b111 << 4,
{% elif id == 2 -%}
Dac3Ch2 = 0b100 << 4,
Dac1Ch2 = 0b101 << 4,
GpioA5 = 0b110 << 4,
GpioA2 = 0b111 << 4,
{% elif id == 3 -%}
Dac3Ch1 = 0b100 << 4,
Dac1Ch1 = 0b101 << 4,
GpioF1 = 0b110 << 4,
GpioC0 = 0b111 << 4,
{% elif id == 4 -%}
Dac3Ch2 = 0b100 << 4,
Dac1Ch1 = 0b101 << 4,
GpioE8 = 0b110 << 4,
GpioB2 = 0b111 << 4,
{% elif id == 5 -%}
Dac4Ch1 = 0b100 << 4,
Dac1Ch2 = 0b101 << 4,
GpioB10 = 0b110 << 4,
GpioD13 = 0b111 << 4,
{% elif id == 6 -%}
Dac4Ch2 = 0b100 << 4,
Dac2Ch1 = 0b101 << 4,
GpioD10 = 0b110 << 4,
GpioB15 = 0b111 << 4,
{% elif id == 7 -%}
Dac4Ch1 = 0b100 << 4,
Dac2Ch1 = 0b101 << 4,
GpioD15 = 0b110 << 4,
GpioB12 = 0b111 << 4,
{% endif -%}
{% endif -%}
};
protected:
{% if target.family == "f3" -%}
static constexpr uint32_t InvertingInputMask = (0b111 << 4) | (0b1 << 22);
{% elif target.family == "l4" -%}
{% elif target.family in ["l4"] -%}
static constexpr uint32_t InvertingInputMask = (0b111 << 4) | (0b11 << 22) | (0b11 << 25);
{% elif target.family in ["g4"] -%}
static constexpr uint32_t InvertingInputMask = (0b1111 << 4) | (0b11 << 22);
{% endif -%}

public:
Expand All @@ -84,7 +127,7 @@ namespace modm::platform
// TODO: Gpio names (with data from parsed datasheets
BitUnset = 0b0 << 7,
BitSet = 0b1 << 7,
{% elif target.family == "l4" -%}
{% elif target.family in ["l4"] -%}
{% if id == 1 -%}
GpioC5 = 0b00 << 7,
GpioB2 = 0b01 << 7,
Expand All @@ -94,12 +137,37 @@ namespace modm::platform
GpioB6 = 0b01 << 7,
GpioA3 = 0b10 << 7,
{% endif -%}
{% elif target.family in ["g4"] -%}
{% if id == 1 -%}
GpioA1 = 0b0 << 8,
GpioB1 = 0b1 << 8,
{% elif id == 2 -%}
GpioA7 = 0b0 << 8,
GpioA3 = 0b1 << 8,
{% elif id == 3 -%}
GpioA0 = 0b0 << 8,
GpioC1 = 0b1 << 8,
{% elif id == 4 -%}
GpioB0 = 0b0 << 8,
GpioE7 = 0b1 << 8,
{% elif id == 5 -%}
GpioB13 = 0b0 << 8,
GpioD12 = 0b1 << 8,
{% elif id == 6 -%}
GpioB11 = 0b0 << 8,
GpioD11 = 0b1 << 8,
{% elif id == 7 -%}
GpioB14 = 0b0 << 8,
GpioD14 = 0b1 << 8,
{% endif -%}
{% endif -%}
};
protected:
{% if target.family == "f3" -%}
static constexpr uint32_t NonInvertingInputMask = 0b1 << 7;
{% elif target.family == "l4" -%}
{% elif target.family in ["g4"] -%}
static constexpr uint32_t NonInvertingInputMask = 0b1 << 8;
{% elif target.family in ["l4"] -%}
static constexpr uint32_t NonInvertingInputMask = 0b11 << 7;
{% endif -%}

Expand Down Expand Up @@ -137,21 +205,15 @@ namespace modm::platform
BlankingSource
{
NoBlanking = 0b000 << 18,
{% if target.family == "f3" -%}
Tim1Oc5 = 0b001 << 18,
Tim2Oc3 = 0b010 << 18,
Tim3Oc3 = 0b011 << 18,
{% elif target.family == "l4" -%}
{% if id == 1 -%}
Tim1Oc5 = 0b001 << 18,
Tim2Oc3 = 0b010 << 18,
{% elif id == 2 -%}
Tim15Oc1 = 0b100 << 18,
{% endif -%}

{% for k, v in blanking_source.items() -%}
{% if k is not none and v is not none -%}
{{ v }} = 0{{ k }} << {{ csr }}BLANKING_Pos,
{% endif -%}
{% endfor -%}
};
protected:
static constexpr uint32_t BlankingSourceMask = 0b111 << 18;
static constexpr uint32_t BlankingSourceMask = {{ csr }}BLANKING_Msk;

public:
/**
Expand All @@ -165,26 +227,30 @@ namespace modm::platform
static inline void
initialize(
InvertingInput n_in,
{% if (target.family == "f3" and id > 1) or target.family == "l4" -%}
{% if (target.family == "f3" and id > 1) or target.family in ["l4", "g4"] -%}
NonInvertingInput p_in,
{% endif -%}
{% if target.family == "f3" -%}
Output out = Output::NoSelection,
{% endif -%}
Hysteresis hyst = Hysteresis::NoHysteresis,
{% if driver.type not in ["stm32-tsmc90_g4_rockfish_cube"] -%}
Mode mode = Mode::HighSpeed,
{% endif -%}
Polarity pol = Polarity::NonInverted,
bool lock_comp = false)
{
setInvertingInput(n_in);
{% if (target.family == "f3" and id > 1) or target.family == "l4" -%}
{% if (target.family == "f3" and id > 1) or target.family in ["l4", "g4"] -%}
setNonInvertingInput(p_in);
{% endif -%}
{% if target.family == "f3" -%}
setOutputSelection(out);
{% endif -%}
setHysteresis(hyst);
{% if driver.type not in ["stm32-tsmc90_g4_rockfish_cube"] -%}
setMode(mode);
{% endif -%}
setPolarity(pol);
setEnabled(true); // enable comparator
if(lock_comp) {
Expand Down Expand Up @@ -242,6 +308,7 @@ namespace modm::platform
}
{% endif -%}

{% if driver.type not in ["stm32-tsmc90_g4_rockfish_cube"] -%}
/**
* \brief Sets the mode that determins speed/power consumption.
*
Expand All @@ -263,6 +330,7 @@ namespace modm::platform
{
return static_cast<Mode>(COMP{{ id }}->CSR & ModeMask);
}
{% endif -%}

/**
* \brief Selects what the inverting input is connected to.
Expand All @@ -282,7 +350,7 @@ namespace modm::platform
return static_cast<InvertingInput>(COMP{{ id }}->CSR & InvertingInputMask);
}

{% if (target.family == "f3" and id > 1) or target.family == "l4" -%}
{% if (target.family == "f3" and id > 1) or target.family in ["l4", "g4"] -%}
/**
* \brief Selects what the non-inverting input is connected to.
*/
Expand All @@ -308,7 +376,6 @@ namespace modm::platform
{% elif target.family == "l4" -%}
{% set windowmode = "WINMODE" %}
{% endif -%}

/**
* \brief Enable/Disable window mode for COMP{{ (id-1) }}/{{ id }}.
*/
Expand Down Expand Up @@ -414,7 +481,7 @@ namespace modm::platform
{
{% if target.family == "f3" -%}
return COMP{{ id }}->CSR & {{ csr }}OUT;
{% elif target.family == "l4" -%}
{% elif target.family in ["l4", "g4"] -%}
return COMP{{ id }}->CSR & {{ csr }}VALUE;
{% endif -%}
}
Expand Down
43 changes: 40 additions & 3 deletions src/modm/platform/comp/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,27 @@ class Instance(Module):
properties["target"] = device.identifier
instance_id = int(self.instance)
properties["id"] = instance_id
properties["csr"] = "COMP_CSR_" if device.identifier["family"] in ["l4"] else "COMP_CSR_COMPx"
properties["driver"] = driver
properties["csr"] = "COMP_CSR_" if device.identifier["family"] in ["l4", "g4"] else "COMP_CSR_COMPx"
properties["blanking_source"] = dict()
if device.identifier["family"] in ["g4"]:
properties["blanking_source"]["b001"] = ["Tim1Oc5", "Tim1Oc5", "Tim1Oc5", "Tim3Oc4", "Tim2Oc3", "Tim8Oc5", "Tim1Oc5"][instance_id - 1]
properties["blanking_source"]["b010"] = ["Tim2Oc3", "Tim2Oc3", "Tim3Oc3", "Tim8Oc5", "Tim8Oc5", "Tim2Oc4", "Tim8Oc5"][instance_id - 1]
properties["blanking_source"]["b011"] = ["Tim3Oc3", "Tim3Oc3", "Tim2Oc4", "Tim15Oc1_2", "Tim3Oc3", "Tim15Oc2", "Tim3Oc3"][instance_id - 1]
properties["blanking_source"]["b100"] = ["Tim8Oc5", "Tim8Oc5", "Tim8Oc5", "Tim1Oc5", "Tim1Oc5", "Tim1Oc5", "Tim15Oc2"][instance_id - 1]
properties["blanking_source"]["b101"] = ["Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5"][instance_id - 1]
properties["blanking_source"]["b110"] = ["Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1"][instance_id - 1]
properties["blanking_source"]["b111"] = ["Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3"][instance_id - 1]
elif device.identifier["family"] in ["l4"]:
properties["blanking_source"]["b001"] = ["Tim1Oc5", None][instance_id - 1]
properties["blanking_source"]["b010"] = ["Tim2Oc3", None][instance_id - 1]
properties["blanking_source"]["b100"] = [None, "Tim15Oc1"][instance_id - 1]
elif device.identifier["family"] in ["f3"]:
properties["blanking_source"]["b001"] = "Tim1Oc5"
properties["blanking_source"]["b010"] = "Tim2Oc3"
properties["blanking_source"]["b011"] = "Tim3Oc3"



env.substitutions = properties
env.outbasepath = "modm/src/modm/platform/comp"
Expand All @@ -48,7 +68,22 @@ def prepare(module, options):
if not device.has_driver("comp:stm32*"):
return False

if not device.get_driver("comp")["type"] in ["stm32-v1.3", "stm32-tsmc90_cube"]:
"""
Existing comparator IPs:
"stm32-tsmc90_cube"
"stm32-tsmc90_dory_cube"
"stm32-tsmc90_g4_rockfish_cube"
"stm32-tsmc90_h7_cube"
"stm32-tsmc90_orca128_cube"
"stm32-tsmc90_orca512_cube"
"stm32-tsmc90_orcazero_cube"
"stm32-v1.0"
"stm32-v1.2"
"stm32-v1.3"
"stm32-v3.4"
"stm32-v3.6"
"""
if not device.get_driver("comp")["type"] in ["stm32-v1.3", "stm32-tsmc90_cube", "stm32-tsmc90_g4_rockfish_cube"]:
return False

# Only some STM32F3 and STM32L4
Expand All @@ -58,6 +93,8 @@ def prepare(module, options):
elif device.identifier["family"] == "l4":
if not device.identifier["name"] in ["31", "32", "33", "42", "43", "51", "52", "62"]:
return False
elif device.identifier["family"] == "g4":
pass
else:
return False

Expand All @@ -75,7 +112,7 @@ def build(env):
properties = device.properties
properties["target"] = device.identifier
properties["driver"] = driver
properties["csr"] = "COMP_CSR_" if device.identifier["family"] in ["l4"] else "COMP_CSR_COMPx"
properties["csr"] = "COMP_CSR_" if device.identifier["family"] in ["l4", "g4"] else "COMP_CSR_COMPx"

env.substitutions = properties
env.outbasepath = "modm/src/modm/platform/comp"
Expand Down

0 comments on commit fb52706

Please sign in to comment.