Skip to content

Commit 136c900

Browse files
committed
[stm32] Port ADC driver to STM32L5
1 parent d46e250 commit 136c900

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/modm/platform/adc/stm32f3/adc.hpp.in

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ public:
6262
%% elif target["family"] in ["l4", "g4"]
6363
Temperature = 17,
6464
BatDiv3 = 18,
65+
%% elif target["family"] in ["l5"]
66+
InternalReference = 0,
67+
Temperature = 17,
68+
BatDiv3 = 18,
6569
%% endif
6670
%% elif id == 2
6771
%% if target["family"] in ["f3"]
6872
Opamp2 = 17,
6973
InternalReference = 18,
70-
%% elif target["family"] in ["l4", "g4"]
74+
%% elif target["family"] in ["l4", "l5", "g4"]
7175
Dac1 = 17,
7276
Dac2 = 18,
7377
%% endif
@@ -107,16 +111,18 @@ public:
107111
NoClock = 0, // No clock selected.
108112
%% if target["family"] in ["g4"]
109113
%% if id in [1, 2]
110-
Pll = RCC_CCIPR_ADC12SEL_0, // PLL “P” clock selected as ADC clock
111-
SystemClock = RCC_CCIPR_ADC12SEL_1 , // System clock selected as ADCs clock
114+
Pll = RCC_{{ ccipr }}_ADC12SEL_0, // PLL “P” clock selected as ADC clock
115+
SystemClock = RCC_{{ ccipr }}_ADC12SEL_1 , // System clock selected as ADCs clock
112116
%% elif id in [3, 4, 5]
113-
Pll = RCC_CCIPR_ADC345SEL_0, // PLL “P” clock selected as ADC clock
114-
SystemClock = RCC_CCIPR_ADC345SEL_1 , // System clock selected as ADCs clock
117+
Pll = RCC_{{ ccipr }}_ADC345SEL_0, // PLL “P” clock selected as ADC clock
118+
SystemClock = RCC_{{ ccipr }}_ADC345SEL_1 , // System clock selected as ADCs clock
115119
%% endif
116120
%% else
117-
PllSai1 = RCC_CCIPR_ADCSEL_0, // PLLSAI1 "R" clock (PLLADC1CLK) selected as ADCs clock
118-
PllSai2 = RCC_CCIPR_ADCSEL_1, // PLLSAI2 "R" clock (PLLADC2CLK) selected as ADCs clock
119-
SystemClock = RCC_CCIPR_ADCSEL_1 | RCC_CCIPR_ADCSEL_0, // System clock selected as ADCs clock
121+
PllSai1 = RCC_{{ ccipr }}_ADCSEL_0, // PLLSAI1 "R" clock (PLLADC1CLK) selected as ADCs clock
122+
%% if target["family"] != "l5"
123+
PllSai2 = RCC_{{ ccipr }}_ADCSEL_1, // PLLSAI2 "R" clock (PLLADC2CLK) selected as ADCs clock
124+
%% endif
125+
SystemClock = RCC_{{ ccipr }}_ADCSEL_1 | RCC_{{ ccipr }}_ADCSEL_0, // System clock selected as ADCs clock
120126
%% endif
121127
};
122128
%% endif
@@ -139,7 +145,7 @@ public:
139145
Div128 = RCC_CFGR2_{{ adc_pre }}_DIV128,
140146
Div256 = RCC_CFGR2_{{ adc_pre }}_DIV256,
141147
Div256AllBits = RCC_CFGR2_{{ adc_pre }}, // for bit clear
142-
%% elif target["family"] in ["l4", "g4"]
148+
%% elif target["family"] in ["l4", "l5", "g4"]
143149
Div1 = 0,
144150
Div2 = ADC_CCR_PRESC_0,
145151
Div4 = ADC_CCR_PRESC_1,
@@ -156,7 +162,7 @@ public:
156162
%% endif
157163
};
158164

159-
%% if target["family"] in ["l4", "g4"]
165+
%% if target["family"] in ["l4", "l5", "g4"]
160166
enum class SampleTime : uint8_t // TODO: What is the best type?
161167
{
162168
Cycles2 = 0b000, //! 1.5 ADC clock cycles
@@ -190,7 +196,7 @@ public:
190196

191197
enum class VoltageRegulatorState : uint32_t
192198
{
193-
%% if target["family"] in ["l4", "g4"]
199+
%% if target["family"] in ["l4", "l5", "g4"]
194200
Enabled = ADC_CR_ADVREGEN,
195201
%% elif target["family"] in ["f3"]
196202
// Intermediate state is needed to move from enabled to disabled

src/modm/platform/adc/stm32f3/adc_impl.hpp.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ modm::platform::Adc{{ id }}::initialize(const ClockMode clk,
2929
uint32_t tmp = 0;
3030

3131
// enable clock
32-
%% if target["family"] in ["f3", "g4"]
32+
%% if target["family"] in ["f3", "g4", "l5"]
3333
RCC->{{ ahb }}ENR |= RCC_{{ ahb }}ENR_ADC{{ id_common }}EN;
3434
%% elif target["family"] in ["l4"]
3535
Rcc::enable<Peripheral::Adc1>();
3636
%% endif
3737

3838
%% if clock_mux
3939
// select clock source
40-
RCC->CCIPR |= static_cast<uint32_t>(clk_src);
40+
RCC->{{ ccipr }} |= static_cast<uint32_t>(clk_src);
4141
%% endif
4242

43-
%% if target["family"] in ["l4", "g4"]
43+
%% if target["family"] in ["l4", "l5", "g4"]
4444
// Disable deep power down
4545
ADC{{ id }}->CR &= ~ADC_CR_DEEPPWD;
4646
%% endif
@@ -90,7 +90,7 @@ modm::platform::Adc{{ id }}::disable(const bool blocking)
9090
while(ADC{{ id }}->CR & ADC_CR_ADDIS);
9191
}
9292
// disable clock
93-
%% if target["family"] in ["f3", "g4"]
93+
%% if target["family"] in ["f3", "g4", "l5"]
9494
RCC->{{ ahb }}ENR &= ~RCC_{{ ahb }}ENR_ADC{{ id_common }}EN;
9595
%% elif target["family"] in ["l4"]
9696
Rcc::disable<Peripheral::Adc1>();
@@ -106,7 +106,7 @@ modm::platform::Adc{{ id }}::setPrescaler(const Prescaler pre)
106106
tmp &= ~static_cast<uint32_t>(Prescaler::Div256AllBits);
107107
tmp |= static_cast<uint32_t>(pre);
108108
RCC->CFGR2 = tmp;
109-
%% elif target["family"] in ["l4", "g4"]
109+
%% elif target["family"] in ["l4", "l5", "g4"]
110110
tmp = ADC{{ id_common_u }}->CCR;
111111
tmp &= ~static_cast<uint32_t>(Prescaler::Div256AllBits);
112112
tmp |= static_cast<uint32_t>(pre);

src/modm/platform/adc/stm32f3/module.lb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Instance(Module):
3636
if target["family"] == "f3":
3737
# 13-14 reserved
3838
channels = [1,2,3,4,5,6,7,8,9,10,11,12,15,16,17,18]
39-
elif target["family"] == "l4":
39+
elif target["family"] in ["l4", "l5"]:
4040
# ADC1 is connected to 16 external channels + 3 internal channels
4141
channels = range(1,17)
4242
elif target["family"] == "g4":
@@ -52,6 +52,9 @@ class Instance(Module):
5252
elif target["family"] == "g4":
5353
# ADC2 is connected to 16 external channels + 2 internal channels
5454
channels = range(1,17)
55+
elif target["family"] == "l5":
56+
# ADC1 is connected to 16 external channels + 2 internal channels
57+
channels = range(1,17)
5558
else:
5659
# ADC2 is connected to 16 external channels + 2 internal channels
5760
channels = range(1,17)
@@ -99,6 +102,7 @@ class Instance(Module):
99102
properties["clock_mux"] = False
100103
elif target["family"] == "l4":
101104
properties["adc_ccr"] = "ADC_CCR"
105+
properties["ccipr"] = "CCIPR"
102106
if len(driver["instance"]) == 1 and "q5a" not in target.string:
103107
properties["id_common"] = "1"
104108
properties["id_common_u"] = "1_COMMON"
@@ -109,9 +113,17 @@ class Instance(Module):
109113
properties["id_common"] = "123"
110114
properties["id_common_u"] = "123_COMMON"
111115
properties["clock_mux"] = (target["name"] not in ("12", "22"))
116+
elif target["family"] == "l5":
117+
properties["adc_ccr"] = "ADC_CCR"
118+
properties["ccipr"] = "CCIPR1"
119+
properties["ahb"] = "AHB2"
120+
properties["id_common"] = ""
121+
properties["id_common_u"] = "12_COMMON"
122+
properties["clock_mux"] = True
112123
elif target["family"] == "g4":
113124
properties["ahb"] = "AHB2"
114125
properties["adc_ccr"] = "ADC_CCR"
126+
properties["ccipr"] = "CCIPR"
115127
if instance_id in [1, 2]:
116128
properties["id_common"] = "12"
117129
properties["id_common_u"] = "12_COMMON"

0 commit comments

Comments
 (0)